summaryrefslogtreecommitdiff
path: root/tools/net/ynl/ynltool/page-pool.c
blob: 4b24492abab7889bed984f1558c58464520279b0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
// SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <net/if.h>

#include <ynl.h>
#include "netdev-user.h"

#include "main.h"

struct pp_stat {
	unsigned int ifc;

	struct {
		unsigned int cnt;
		size_t refs, bytes;
	} live[2];

	size_t alloc_slow, alloc_fast, recycle_ring, recycle_cache;
};

struct pp_stats_array {
	unsigned int i, max;
	struct pp_stat *s;
};

static struct pp_stat *find_ifc(struct pp_stats_array *a, unsigned int ifindex)
{
	unsigned int i;

	for (i = 0; i < a->i; i++) {
		if (a->s[i].ifc == ifindex)
			return &a->s[i];
	}

	a->i++;
	if (a->i == a->max) {
		a->max *= 2;
		a->s = reallocarray(a->s, a->max, sizeof(*a->s));
	}
	a->s[i].ifc = ifindex;
	return &a->s[i];
}

static void count_pool(struct pp_stat *s, unsigned int l,
		       struct netdev_page_pool_get_rsp *pp)
{
	s->live[l].cnt++;
	if (pp->_present.inflight)
		s->live[l].refs += pp->inflight;
	if (pp->_present.inflight_mem)
		s->live[l].bytes += pp->inflight_mem;
}

/* We don't know how many pages are sitting in cache and ring
 * so we will under-count the recycling rate a bit.
 */
static void print_json_recycling_stats(struct pp_stat *s)
{
	double recycle;

	if (s->alloc_fast + s->alloc_slow) {
		recycle = (double)(s->recycle_ring + s->recycle_cache) /
			(s->alloc_fast + s->alloc_slow) * 100;
		jsonw_float_field(json_wtr, "recycling_pct", recycle);
	}

	jsonw_name(json_wtr, "alloc");
	jsonw_start_object(json_wtr);
	jsonw_uint_field(json_wtr, "slow", s->alloc_slow);
	jsonw_uint_field(json_wtr, "fast", s->alloc_fast);
	jsonw_end_object(json_wtr);

	jsonw_name(json_wtr, "recycle");
	jsonw_start_object(json_wtr);
	jsonw_uint_field(json_wtr, "ring", s->recycle_ring);
	jsonw_uint_field(json_wtr, "cache", s->recycle_cache);
	jsonw_end_object(json_wtr);
}

static void print_plain_recycling_stats(struct pp_stat *s)
{
	double recycle;

	if (s->alloc_fast + s->alloc_slow) {
		recycle = (double)(s->recycle_ring + s->recycle_cache) /
			(s->alloc_fast + s->alloc_slow) * 100;
		printf("recycling: %.1lf%% (alloc: %zu:%zu recycle: %zu:%zu)",
		       recycle, s->alloc_slow, s->alloc_fast,
		       s->recycle_ring, s->recycle_cache);
	}
}

static void print_json_stats(struct pp_stats_array *a)
{
	jsonw_start_array(json_wtr);

	for (unsigned int i = 0; i < a->i; i++) {
		char ifname[IF_NAMESIZE];
		struct pp_stat *s = &a->s[i];
		const char *name;

		jsonw_start_object(json_wtr);

		if (!s->ifc) {
			jsonw_string_field(json_wtr, "ifname", "<orphan>");
			jsonw_uint_field(json_wtr, "ifindex", 0);
		} else {
			name = if_indextoname(s->ifc, ifname);
			if (name)
				jsonw_string_field(json_wtr, "ifname", name);
			jsonw_uint_field(json_wtr, "ifindex", s->ifc);
		}

		jsonw_uint_field(json_wtr, "page_pools", s->live[1].cnt);
		jsonw_uint_field(json_wtr, "zombies", s->live[0].cnt);

		jsonw_name(json_wtr, "live");
		jsonw_start_object(json_wtr);
		jsonw_uint_field(json_wtr, "refs", s->live[1].refs);
		jsonw_uint_field(json_wtr, "bytes", s->live[1].bytes);
		jsonw_end_object(json_wtr);

		jsonw_name(json_wtr, "zombie");
		jsonw_start_object(json_wtr);
		jsonw_uint_field(json_wtr, "refs", s->live[0].refs);
		jsonw_uint_field(json_wtr, "bytes", s->live[0].bytes);
		jsonw_end_object(json_wtr);

		if (s->alloc_fast || s->alloc_slow)
			print_json_recycling_stats(s);

		jsonw_end_object(json_wtr);
	}

	jsonw_end_array(json_wtr);
}

static void print_plain_stats(struct pp_stats_array *a)
{
	for (unsigned int i = 0; i < a->i; i++) {
		char ifname[IF_NAMESIZE];
		struct pp_stat *s = &a->s[i];
		const char *name;

		if (!s->ifc) {
			printf("<orphan>\t");
		} else {
			name = if_indextoname(s->ifc, ifname);
			if (name)
				printf("%8s", name);
			printf("[%u]\t", s->ifc);
		}

		printf("page pools: %u (zombies: %u)\n",
		       s->live[1].cnt, s->live[0].cnt);
		printf("\t\trefs: %zu bytes: %zu (refs: %zu bytes: %zu)\n",
		       s->live[1].refs, s->live[1].bytes,
		       s->live[0].refs, s->live[0].bytes);

		if (s->alloc_fast || s->alloc_slow) {
			printf("\t\t");
			print_plain_recycling_stats(s);
			printf("\n");
		}
	}
}

static bool
find_pool_stat_in_list(struct netdev_page_pool_stats_get_list *pp_stats,
		       __u64 pool_id, struct pp_stat *pstat)
{
	ynl_dump_foreach(pp_stats, pp) {
		if (!pp->_present.info || !pp->info._present.id)
			continue;
		if (pp->info.id != pool_id)
			continue;

		memset(pstat, 0, sizeof(*pstat));
		if (pp->_present.alloc_fast)
			pstat->alloc_fast = pp->alloc_fast;
		if (pp->_present.alloc_refill)
			pstat->alloc_fast += pp->alloc_refill;
		if (pp->_present.alloc_slow)
			pstat->alloc_slow = pp->alloc_slow;
		if (pp->_present.recycle_ring)
			pstat->recycle_ring = pp->recycle_ring;
		if (pp->_present.recycle_cached)
			pstat->recycle_cache = pp->recycle_cached;
		return true;
	}
	return false;
}

static void
print_json_pool_list(struct netdev_page_pool_get_list *pools,
		     struct netdev_page_pool_stats_get_list *pp_stats,
		     bool zombies_only)
{
	jsonw_start_array(json_wtr);

	ynl_dump_foreach(pools, pp) {
		char ifname[IF_NAMESIZE];
		struct pp_stat pstat;
		const char *name;

		if (zombies_only && !pp->_present.detach_time)
			continue;

		jsonw_start_object(json_wtr);

		jsonw_uint_field(json_wtr, "id", pp->id);

		if (pp->_present.ifindex) {
			name = if_indextoname(pp->ifindex, ifname);
			if (name)
				jsonw_string_field(json_wtr, "ifname", name);
			jsonw_uint_field(json_wtr, "ifindex", pp->ifindex);
		}

		if (pp->_present.napi_id)
			jsonw_uint_field(json_wtr, "napi_id", pp->napi_id);

		if (pp->_present.inflight)
			jsonw_uint_field(json_wtr, "refs", pp->inflight);

		if (pp->_present.inflight_mem)
			jsonw_uint_field(json_wtr, "bytes", pp->inflight_mem);

		if (pp->_present.detach_time)
			jsonw_uint_field(json_wtr, "detach_time", pp->detach_time);

		if (pp->_present.dmabuf)
			jsonw_uint_field(json_wtr, "dmabuf", pp->dmabuf);

		if (find_pool_stat_in_list(pp_stats, pp->id, &pstat) &&
		    (pstat.alloc_fast || pstat.alloc_slow))
			print_json_recycling_stats(&pstat);

		jsonw_end_object(json_wtr);
	}

	jsonw_end_array(json_wtr);
}

static void
print_plain_pool_list(struct netdev_page_pool_get_list *pools,
		      struct netdev_page_pool_stats_get_list *pp_stats,
		      bool zombies_only)
{
	ynl_dump_foreach(pools, pp) {
		char ifname[IF_NAMESIZE];
		struct pp_stat pstat;
		const char *name;

		if (zombies_only && !pp->_present.detach_time)
			continue;

		printf("pool id: %llu", pp->id);

		if (pp->_present.ifindex) {
			name = if_indextoname(pp->ifindex, ifname);
			if (name)
				printf("  dev: %s", name);
			printf("[%u]", pp->ifindex);
		}

		if (pp->_present.napi_id)
			printf("  napi: %llu", pp->napi_id);

		printf("\n");

		if (pp->_present.inflight || pp->_present.inflight_mem) {
			printf("  inflight:");
			if (pp->_present.inflight)
				printf(" %llu pages", pp->inflight);
			if (pp->_present.inflight_mem)
				printf(" %llu bytes", pp->inflight_mem);
			printf("\n");
		}

		if (pp->_present.detach_time)
			printf("  detached: %llu\n", pp->detach_time);

		if (pp->_present.dmabuf)
			printf("  dmabuf: %u\n", pp->dmabuf);

		if (find_pool_stat_in_list(pp_stats, pp->id, &pstat) &&
		    (pstat.alloc_fast || pstat.alloc_slow)) {
			printf("  ");
			print_plain_recycling_stats(&pstat);
			printf("\n");
		}
	}
}

static void aggregate_device_stats(struct pp_stats_array *a,
				   struct netdev_page_pool_get_list *pools,
				   struct netdev_page_pool_stats_get_list *pp_stats)
{
	ynl_dump_foreach(pools, pp) {
		struct pp_stat *s = find_ifc(a, pp->ifindex);

		count_pool(s, 1, pp);
		if (pp->_present.detach_time)
			count_pool(s, 0, pp);
	}

	ynl_dump_foreach(pp_stats, pp) {
		struct pp_stat *s = find_ifc(a, pp->info.ifindex);

		if (pp->_present.alloc_fast)
			s->alloc_fast += pp->alloc_fast;
		if (pp->_present.alloc_refill)
			s->alloc_fast += pp->alloc_refill;
		if (pp->_present.alloc_slow)
			s->alloc_slow += pp->alloc_slow;
		if (pp->_present.recycle_ring)
			s->recycle_ring += pp->recycle_ring;
		if (pp->_present.recycle_cached)
			s->recycle_cache += pp->recycle_cached;
	}
}

static int do_stats(int argc, char **argv)
{
	struct netdev_page_pool_stats_get_list *pp_stats;
	struct netdev_page_pool_get_list *pools;
	enum {
		GROUP_BY_DEVICE,
		GROUP_BY_POOL,
	} group_by = GROUP_BY_DEVICE;
	bool zombies_only = false;
	struct pp_stats_array a = {};
	struct ynl_error yerr;
	struct ynl_sock *ys;
	int ret = 0;

	/* Parse options */
	while (argc > 0) {
		if (is_prefix(*argv, "group-by")) {
			NEXT_ARG();

			if (!REQ_ARGS(1))
				return -1;

			if (is_prefix(*argv, "device")) {
				group_by = GROUP_BY_DEVICE;
			} else if (is_prefix(*argv, "pp") ||
				   is_prefix(*argv, "page-pool") ||
				   is_prefix(*argv, "none")) {
				group_by = GROUP_BY_POOL;
			} else {
				p_err("invalid group-by value '%s'", *argv);
				return -1;
			}
			NEXT_ARG();
		} else if (is_prefix(*argv, "zombies")) {
			zombies_only = true;
			group_by = GROUP_BY_POOL;
			NEXT_ARG();
		} else {
			p_err("unknown option '%s'", *argv);
			return -1;
		}
	}

	ys = ynl_sock_create(&ynl_netdev_family, &yerr);
	if (!ys) {
		p_err("YNL: %s", yerr.msg);
		return -1;
	}

	pools = netdev_page_pool_get_dump(ys);
	if (!pools) {
		p_err("failed to get page pools: %s", ys->err.msg);
		ret = -1;
		goto exit_close;
	}

	pp_stats = netdev_page_pool_stats_get_dump(ys);
	if (!pp_stats) {
		p_err("failed to get page pool stats: %s", ys->err.msg);
		ret = -1;
		goto exit_free_pp_list;
	}

	/* If grouping by pool, print individual pools */
	if (group_by == GROUP_BY_POOL) {
		if (json_output)
			print_json_pool_list(pools, pp_stats, zombies_only);
		else
			print_plain_pool_list(pools, pp_stats, zombies_only);
	} else {
		/* Aggregated stats mode (group-by device) */
		a.max = 64;
		a.s = calloc(a.max, sizeof(*a.s));
		if (!a.s) {
			p_err("failed to allocate stats array");
			ret = -1;
			goto exit_free_stats_list;
		}

		aggregate_device_stats(&a, pools, pp_stats);

		if (json_output)
			print_json_stats(&a);
		else
			print_plain_stats(&a);

		free(a.s);
	}

exit_free_stats_list:
	netdev_page_pool_stats_get_list_free(pp_stats);
exit_free_pp_list:
	netdev_page_pool_get_list_free(pools);
exit_close:
	ynl_sock_destroy(ys);
	return ret;
}

static int do_help(int argc __attribute__((unused)),
		   char **argv __attribute__((unused)))
{
	if (json_output) {
		jsonw_null(json_wtr);
		return 0;
	}

	fprintf(stderr,
		"Usage: %s page-pool { COMMAND | help }\n"
		"       %s page-pool stats [ OPTIONS ]\n"
		"\n"
		"       OPTIONS := { group-by { device | page-pool | none } | zombies }\n"
		"\n"
		"       stats                   - Display page pool statistics\n"
		"       stats group-by device   - Group statistics by network device (default)\n"
		"       stats group-by page-pool | pp | none\n"
		"                               - Show individual page pool details (no grouping)\n"
		"       stats zombies           - Show only zombie page pools (detached but with\n"
		"                                 pages in flight). Implies group-by page-pool.\n"
		"",
		bin_name, bin_name);

	return 0;
}

static const struct cmd page_pool_cmds[] = {
	{ "help",	do_help },
	{ "stats",	do_stats },
	{ 0 }
};

int do_page_pool(int argc, char **argv)
{
	return cmd_select(page_pool_cmds, argc, argv, do_help);
}