DPDK logo

Elixir Cross Referencer

  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
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
/* SPDX-License-Identifier: BSD-3-Clause
 * Copyright(C) 2020 Marvell International Ltd.
 */

#include <inttypes.h>
#include <time.h>

#include <rte_byteorder.h>
#include <rte_common.h>
#include <rte_time.h>
#include <rte_trace.h>
#include <rte_version.h>

#include "eal_trace.h"

__rte_format_printf(2, 0)
static int
metadata_printf(char **str, const char *fmt, ...)
{
	va_list ap;
	int rc;

	*str = NULL;
	va_start(ap, fmt);
	rc = vasprintf(str, fmt, ap);
	va_end(ap);

	return rc;
}

static int
meta_copy(char **meta, int *offset, char *str, int rc)
{
	int count = *offset;
	char *ptr = *meta;

	if (rc < 0)
		return rc;

	ptr = realloc(ptr, count + rc);
	if (ptr == NULL)
		goto free_str;

	memcpy(RTE_PTR_ADD(ptr, count), str, rc);
	count += rc;
	free(str);

	*meta = ptr;
	*offset = count;

	return rc;

free_str:
	if (str)
		free(str);
	return -ENOMEM;
}

static int
meta_data_type_emit(char **meta, int *offset)
{
	char *str = NULL;
	int rc;

	rc = metadata_printf(&str,
		"/* CTF 1.8 */\n"
		"typealias integer {size = 8; base = x;}:= uint8_t;\n"
		"typealias integer {size = 16; base = x;} := uint16_t;\n"
		"typealias integer {size = 32; base = x;} := uint32_t;\n"
		"typealias integer {size = 64; base = x;} := uint64_t;\n"
		"typealias integer {size = 8; signed = true;}  := int8_t;\n"
		"typealias integer {size = 16; signed = true;} := int16_t;\n"
		"typealias integer {size = 32; signed = true;} := int32_t;\n"
		"typealias integer {size = 64; signed = true;} := int64_t;\n"
#ifdef RTE_ARCH_64
		"typealias integer {size = 64; base = x;} := uintptr_t;\n"
#else
		"typealias integer {size = 32; base = x;} := uintptr_t;\n"
#endif
#ifdef RTE_ARCH_64
		"typealias integer {size = 64; base = x;} := long;\n"
#else
		"typealias integer {size = 32; base = x;} := long;\n"
#endif
		"typealias integer {size = 8; signed = false; encoding = ASCII; } := string_bounded_t;\n\n"
		"typealias floating_point {\n"
		"    exp_dig = 8;\n"
		"    mant_dig = 24;\n"
		"} := float;\n\n"
		"typealias floating_point {\n"
		"    exp_dig = 11;\n"
		"    mant_dig = 53;\n"
		"} := double;\n\n");

	return meta_copy(meta, offset, str, rc);
}

static int
is_be(void)
{
#if RTE_BYTE_ORDER == RTE_BIG_ENDIAN
	return 1;
#else
	return 0;
#endif
}

static int
meta_header_emit(char **meta, int *offset)
{
	struct trace *trace = trace_obj_get();
	char uustr[RTE_UUID_STRLEN];
	char *str = NULL;
	int rc;

	rte_uuid_unparse(trace->uuid, uustr, RTE_UUID_STRLEN);
	rc = metadata_printf(&str,
		"trace {\n"
		"    major = 1;\n"
		"    minor = 8;\n"
		"    uuid = \"%s\";\n"
		"    byte_order = %s;\n"
		"    packet.header := struct {\n"
		"	    uint32_t magic;\n"
		"	    uint8_t  uuid[16];\n"
		"    };\n"
		"};\n\n", uustr, is_be() ? "be" : "le");
	return meta_copy(meta, offset, str, rc);
}

static int
meta_env_emit(char **meta, int *offset)
{
	char *str = NULL;
	int rc;

	rc = metadata_printf(&str,
		"env {\n"
		"    dpdk_version = \"%s\";\n"
		"    tracer_name = \"dpdk\";\n"
		"};\n\n", rte_version());
	return meta_copy(meta, offset, str, rc);
}

static int
meta_clock_pass1_emit(char **meta, int *offset)
{
	char *str = NULL;
	int rc;

	rc = metadata_printf(&str,
		"clock {\n"
		"    name = \"dpdk\";\n"
		"    freq = ");
	return meta_copy(meta, offset, str, rc);
}

static int
meta_clock_pass2_emit(char **meta, int *offset)
{
	char *str = NULL;
	int rc;

	rc = metadata_printf(&str,
		"%20"PRIu64";\n"
		"    offset_s =", 0);
	return meta_copy(meta, offset, str, rc);
}

static int
meta_clock_pass3_emit(char **meta, int *offset)
{
	char *str = NULL;
	int rc;

	rc = metadata_printf(&str,
		"%20"PRIu64";\n"
		"    offset =", 0);
	return meta_copy(meta, offset, str, rc);
}

static int
meta_clock_pass4_emit(char **meta, int *offset)
{
	char *str = NULL;
	int rc;

	rc = metadata_printf(&str,
		"%20"PRIu64";\n};\n\n"
		"typealias integer {\n"
		"    size = 48; align = 1; signed = false;\n"
		"    map = clock.dpdk.value;\n"
		"} := uint48_clock_dpdk_t;\n\n", 0);

	return meta_copy(meta, offset, str, rc);
}

static int
meta_stream_emit(char **meta, int *offset)
{
	char *str = NULL;
	int rc;

	rc = metadata_printf(&str,
		"stream {\n"
		"    packet.context := struct {\n"
		"         uint32_t cpu_id;\n"
		"         string_bounded_t name[32];\n"
		"    };\n"
		"    event.header := struct {\n"
		"          uint48_clock_dpdk_t timestamp;\n"
		"          uint16_t id;\n"
		"    } align(64);\n"
		"};\n\n");
	return meta_copy(meta, offset, str, rc);
}

static void
string_fixed_replace(char *input, const char *search, const char *replace)
{
	char *found;
	size_t len;

	found = strstr(input, search);
	if (found == NULL)
		return;

	if (strlen(found) != strlen(search))
		return;

	len = strlen(replace);
	memcpy(found, replace, len);
	found[len] = '\0';
}

static void
ctf_fixup_align(char *str)
{
	string_fixed_replace(str, "align", "_align");
}

static void
ctf_fixup_arrow_deref(char *str)
{
	const char *replace = "_";
	const char *search = "->";
	char *found;
	size_t len;

	found = strstr(str, search);
	if (found == NULL)
		return;

	do {
		memcpy(found, replace, strlen(replace));
		len = strlen(found + 2);
		memcpy(found + 1, found + 2, len);
		found[len + 1] = '\0';
		found = strstr(str, search);
	} while (found != NULL);
}

static void
ctf_fixup_dot_deref(char *str)
{
	const char *replace = "_";
	const char *search = ".";
	char *found;
	size_t len;

	found = strstr(str, search);
	if (found == NULL)
		return;

	len = strlen(replace);
	do {
		memcpy(found, replace, len);
		found = strstr(str, search);
	} while (found != NULL);
}

static void
ctf_fixup_event(char *str)
{
	string_fixed_replace(str, "event", "_event");
}

static int
ctf_fixup_keyword(char *str)
{
	char dup_str[TRACE_CTF_FIELD_SIZE];
	char input[TRACE_CTF_FIELD_SIZE];
	const char *delim = ";";
	char *from;
	int len;

	if (str == NULL)
		return 0;

	len = strlen(str);
	if (len >= TRACE_CTF_FIELD_SIZE) {
		trace_err("ctf_field reached its maximum limit");
		return -EMSGSIZE;
	}

	/* Create duplicate string */
	strcpy(dup_str, str);

	len = 0;
	from = strtok(dup_str, delim);
	while (from != NULL) {
		strcpy(input, from);
		ctf_fixup_align(input);
		ctf_fixup_dot_deref(input);
		ctf_fixup_arrow_deref(input);
		ctf_fixup_event(input);

		strcpy(&input[strlen(input)], delim);
		if ((len + strlen(input)) >= TRACE_CTF_FIELD_SIZE) {
			trace_err("ctf_field reached its maximum limit");
			return -EMSGSIZE;
		}

		strcpy(str + len, input);
		len += strlen(input);
		from = strtok(NULL, delim);
	}

	return 0;
}

static int
meta_event_emit(char **meta, int *offset, struct trace_point *tp)
{
	char *str = NULL;
	int rc;

	/* Fixup ctf field string in case it using reserved ctf keywords */
	rc = ctf_fixup_keyword(tp->ctf_field);
	if (rc)
		return rc;

	rc = metadata_printf(&str,
		"event {\n"
		"    id = %d;\n"
		"    name = \"%s\";\n"
		"    fields := struct {\n"
		"        %s\n"
		"    };\n"
		"};\n\n", trace_id_get(tp->handle), tp->name, tp->ctf_field);
	return meta_copy(meta, offset, str, rc);
}

int
trace_metadata_create(void)
{
	struct trace_point_head *tp_list = trace_list_head_get();
	struct trace *trace = trace_obj_get();
	struct trace_point *tp;
	int rc, offset = 0;
	char *meta = NULL;

	rc = meta_data_type_emit(&meta, &offset);
	if (rc < 0)
		goto fail;

	rc = meta_header_emit(&meta, &offset);
	if (rc < 0)
		goto fail;

	rc = meta_env_emit(&meta, &offset);
	if (rc < 0)
		goto fail;

	rc = meta_clock_pass1_emit(&meta, &offset);
	if (rc < 0)
		goto fail;
	trace->ctf_meta_offset_freq = offset;

	rc = meta_clock_pass2_emit(&meta, &offset);
	if (rc < 0)
		goto fail;
	trace->ctf_meta_offset_freq_off_s = offset;

	rc = meta_clock_pass3_emit(&meta, &offset);
	if (rc < 0)
		goto fail;
	trace->ctf_meta_offset_freq_off = offset;

	rc = meta_clock_pass4_emit(&meta, &offset);
	if (rc < 0)
		goto fail;

	rc = meta_stream_emit(&meta, &offset);
	if (rc < 0)
		goto fail;

	STAILQ_FOREACH(tp, tp_list, next)
		if (meta_event_emit(&meta, &offset, tp) < 0)
			goto fail;

	trace->ctf_meta = meta;
	return 0;

fail:
	if (meta)
		free(meta);
	return -EBADF;
}

void
trace_metadata_destroy(void)
{
	struct trace *trace = trace_obj_get();

	if (trace->ctf_meta) {
		free(trace->ctf_meta);
		trace->ctf_meta = NULL;
	}
}

static void
meta_fix_freq(struct trace *trace, char *meta)
{
	char *str;
	int rc;

	str = RTE_PTR_ADD(meta, trace->ctf_meta_offset_freq);
	rc = sprintf(str, "%20"PRIu64"", rte_get_timer_hz());
	str[rc] = ';';
}

static void
meta_fix_freq_offset(struct trace *trace, char *meta)
{
	uint64_t uptime_tickes_floor, uptime_ticks, freq, uptime_sec;
	uint64_t offset, offset_s;
	char *str;
	int rc;

	uptime_ticks = trace->uptime_ticks &
			((1ULL << __RTE_TRACE_EVENT_HEADER_ID_SHIFT) - 1);
	freq = rte_get_tsc_hz();
	uptime_tickes_floor = RTE_ALIGN_MUL_FLOOR(uptime_ticks, freq);

	uptime_sec = uptime_tickes_floor / freq;
	offset_s = trace->epoch_sec - uptime_sec;

	offset = uptime_ticks - uptime_tickes_floor;
	offset += trace->epoch_nsec * (freq / NSEC_PER_SEC);

	str = RTE_PTR_ADD(meta, trace->ctf_meta_offset_freq_off_s);
	rc = sprintf(str, "%20"PRIu64"", offset_s);
	str[rc] = ';';
	str = RTE_PTR_ADD(meta, trace->ctf_meta_offset_freq_off);
	rc = sprintf(str, "%20"PRIu64"", offset);
	str[rc] = ';';
}

static void
meta_fixup(struct trace *trace, char *meta)
{
	meta_fix_freq(trace, meta);
	meta_fix_freq_offset(trace, meta);
}

int
rte_trace_metadata_dump(FILE *f)
{
	struct trace *trace = trace_obj_get();
	char *ctf_meta = trace->ctf_meta;
	int rc;

	if (!rte_trace_is_enabled())
		return 0;

	if (ctf_meta == NULL)
		return -EINVAL;

	if (!__atomic_load_n(&trace->ctf_fixup_done, __ATOMIC_SEQ_CST) &&
				rte_get_timer_hz()) {
		meta_fixup(trace, ctf_meta);
		__atomic_store_n(&trace->ctf_fixup_done, 1, __ATOMIC_SEQ_CST);
	}

	rc = fprintf(f, "%s", ctf_meta);
	return rc < 0 ? rc : 0;
}