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
/* SPDX-License-Identifier: BSD-3-Clause
 * Copyright(C) 2020 Marvell International Ltd.
 */

#include <fnmatch.h>
#include <pwd.h>
#include <sys/stat.h>
#include <time.h>

#include <rte_common.h>
#include <rte_errno.h>
#include <rte_string_fns.h>

#include "eal_filesystem.h"
#include "eal_trace.h"

const char *
trace_mode_to_string(enum rte_trace_mode mode)
{
	switch (mode) {
	case RTE_TRACE_MODE_OVERWRITE: return "overwrite";
	case RTE_TRACE_MODE_DISCARD: return "discard";
	default: return "unknown";
	}
}

const char *
trace_area_to_string(enum trace_area_e area)
{
	switch (area) {
	case TRACE_AREA_HEAP: return "heap";
	case TRACE_AREA_HUGEPAGE: return "hugepage";
	default: return "unknown";
	}
}

static bool
trace_entry_compare(const char *name)
{
	struct trace_point_head *tp_list = trace_list_head_get();
	struct trace_point *tp;
	int count = 0;

	STAILQ_FOREACH(tp, tp_list, next) {
		if (strncmp(tp->name, name, TRACE_POINT_NAME_SIZE) == 0)
			count++;
		if (count > 1) {
			trace_err("found duplicate entry %s", name);
			rte_errno = EEXIST;
			return true;
		}
	}
	return false;
}

bool
trace_has_duplicate_entry(void)
{
	struct trace_point_head *tp_list = trace_list_head_get();
	struct trace_point *tp;

	/* Is duplicate trace name registered */
	STAILQ_FOREACH(tp, tp_list, next)
		if (trace_entry_compare(tp->name))
			return true;

	return false;
}

void
trace_uuid_generate(void)
{
	struct trace_point_head *tp_list = trace_list_head_get();
	struct trace *trace = trace_obj_get();
	struct trace_point *tp;
	uint64_t sz_total = 0;

	/* Go over the registered trace points to get total size of events */
	STAILQ_FOREACH(tp, tp_list, next) {
		const uint16_t sz = *tp->handle & __RTE_TRACE_FIELD_SIZE_MASK;
		sz_total += sz;
	}

	rte_uuid_t uuid = RTE_UUID_INIT(sz_total, trace->nb_trace_points,
		0x4370, 0x8f50, 0x222ddd514176ULL);
	rte_uuid_copy(trace->uuid, uuid);
}

static int
trace_session_name_generate(char *trace_dir)
{
	struct tm *tm_result;
	time_t tm;
	int rc;

	tm = time(NULL);
	if ((int)tm == -1)
		goto fail;

	tm_result = localtime(&tm);
	if (tm_result == NULL)
		goto fail;

	rc = rte_strscpy(trace_dir, eal_get_hugefile_prefix(),
			TRACE_PREFIX_LEN);
	if (rc == -E2BIG)
		rc = TRACE_PREFIX_LEN;
	trace_dir[rc++] = '-';

	rc = strftime(trace_dir + rc, TRACE_DIR_STR_LEN - rc,
			"%Y-%m-%d-%p-%I-%M-%S", tm_result);
	if (rc == 0)
		goto fail;

	return rc;
fail:
	rte_errno = errno;
	return -rte_errno;
}

static int
trace_dir_update(const char *str)
{
	struct trace *trace = trace_obj_get();
	int rc, remaining;

	remaining = sizeof(trace->dir) - trace->dir_offset;
	rc = rte_strscpy(&trace->dir[0] + trace->dir_offset, str, remaining);
	if (rc < 0)
		goto fail;

	trace->dir_offset += rc;
fail:
	return rc;
}

int
eal_trace_args_save(const char *val)
{
	struct trace *trace = trace_obj_get();
	struct trace_arg *arg = malloc(sizeof(*arg));

	if (arg == NULL) {
		trace_err("failed to allocate memory for %s", val);
		return -ENOMEM;
	}

	arg->val = strdup(val);
	if (arg->val == NULL) {
		trace_err("failed to allocate memory for %s", val);
		free(arg);
		return -ENOMEM;
	}

	STAILQ_INSERT_TAIL(&trace->args, arg, next);
	return 0;
}

void
eal_trace_args_free(void)
{
	struct trace *trace = trace_obj_get();
	struct trace_arg *arg;

	while (!STAILQ_EMPTY(&trace->args)) {
		arg = STAILQ_FIRST(&trace->args);
		STAILQ_REMOVE_HEAD(&trace->args, next);
		free(arg->val);
		free(arg);
	}
}

int
trace_args_apply(const char *arg)
{
	if (rte_trace_regexp(arg, true) < 0) {
		trace_err("cannot enable trace for %s", arg);
		return -1;
	}

	return 0;
}

int
eal_trace_bufsz_args_save(char const *val)
{
	struct trace *trace = trace_obj_get();
	uint64_t bufsz;

	bufsz = rte_str_to_size(val);
	if (bufsz == 0) {
		trace_err("buffer size cannot be zero");
		return -EINVAL;
	}

	trace->buff_len = bufsz;
	return 0;
}

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

	if (trace->buff_len == 0)
		trace->buff_len = 1024 * 1024; /* 1MB */
}

int
eal_trace_mode_args_save(const char *val)
{
	struct trace *trace = trace_obj_get();
	size_t len = strlen(val);
	unsigned long tmp;
	char *pattern;

	if (len == 0) {
		trace_err("value is not provided with option");
		return -EINVAL;
	}

	pattern = (char *)calloc(1, len + 2);
	if (pattern == NULL) {
		trace_err("fail to allocate memory");
		return -ENOMEM;
	}

	sprintf(pattern, "%s*", val);

	if (fnmatch(pattern, "overwrite", 0) == 0)
		tmp = RTE_TRACE_MODE_OVERWRITE;
	else if (fnmatch(pattern, "discard", 0) == 0)
		tmp = RTE_TRACE_MODE_DISCARD;
	else {
		free(pattern);
		return -EINVAL;
	}

	trace->mode = tmp;
	free(pattern);
	return 0;
}

int
eal_trace_dir_args_save(char const *val)
{
	struct trace *trace = trace_obj_get();
	char *dir_path;
	int rc;

	if (strlen(val) >= sizeof(trace->dir) - 1) {
		trace_err("input string is too big");
		return -ENAMETOOLONG;
	}

	if (asprintf(&dir_path, "%s/", val) == -1) {
		trace_err("failed to copy directory: %s", strerror(errno));
		return -ENOMEM;
	}

	rc = trace_dir_update(dir_path);

	free(dir_path);
	return rc;
}

int
trace_epoch_time_save(void)
{
	struct trace *trace = trace_obj_get();
	struct timespec epoch = { 0, 0 };
	uint64_t avg, start, end;

	start = rte_get_tsc_cycles();
	if (clock_gettime(CLOCK_REALTIME, &epoch) < 0) {
		trace_err("failed to get the epoch time");
		return -1;
	}
	end = rte_get_tsc_cycles();
	avg = (start + end) >> 1;

	trace->epoch_sec = (uint64_t) epoch.tv_sec;
	trace->epoch_nsec = (uint64_t) epoch.tv_nsec;
	trace->uptime_ticks = avg;

	return 0;
}

static int
trace_dir_default_path_get(char *dir_path)
{
	struct trace *trace = trace_obj_get();
	uint32_t size = sizeof(trace->dir);
	struct passwd *pwd;
	char *home_dir;

	/* First check for shell environment variable */
	home_dir = getenv("HOME");
	if (home_dir == NULL) {
		/* Fallback to password file entry */
		pwd = getpwuid(getuid());
		if (pwd == NULL)
			return -EINVAL;

		home_dir = pwd->pw_dir;
	}

	/* Append dpdk-traces to directory */
	if (snprintf(dir_path, size, "%s/dpdk-traces/", home_dir) < 0)
		return -ENAMETOOLONG;

	return 0;
}

int
trace_mkdir(void)
{
	struct trace *trace = trace_obj_get();
	char session[TRACE_DIR_STR_LEN];
	char *dir_path;
	int rc;

	if (!trace->dir_offset) {
		dir_path = calloc(1, sizeof(trace->dir));
		if (dir_path == NULL) {
			trace_err("fail to allocate memory");
			return -ENOMEM;
		}

		rc = trace_dir_default_path_get(dir_path);
		if (rc < 0) {
			trace_err("fail to get default path");
			free(dir_path);
			return rc;
		}

		rc = trace_dir_update(dir_path);
		free(dir_path);
		if (rc < 0)
			return rc;
	}

	/* Create the path if it t exist, no "mkdir -p" available here */
	rc = mkdir(trace->dir, 0700);
	if (rc < 0 && errno != EEXIST) {
		trace_err("mkdir %s failed [%s]", trace->dir, strerror(errno));
		rte_errno = errno;
		return -rte_errno;
	}

	rc = trace_session_name_generate(session);
	if (rc < 0)
		return rc;
	rc = trace_dir_update(session);
	if (rc < 0)
		return rc;

	rc = mkdir(trace->dir, 0700);
	if (rc < 0) {
		trace_err("mkdir %s failed [%s]", trace->dir, strerror(errno));
		rte_errno = errno;
		return -rte_errno;
	}

	RTE_LOG(INFO, EAL, "Trace dir: %s\n", trace->dir);
	return 0;
}

static int
trace_meta_save(struct trace *trace)
{
	char file_name[PATH_MAX];
	FILE *f;
	int rc;

	rc = snprintf(file_name, PATH_MAX, "%s/metadata", trace->dir);
	if (rc < 0)
		return rc;

	f = fopen(file_name, "w");
	if (f == NULL)
		return -errno;

	rc = rte_trace_metadata_dump(f);

	if (fclose(f))
		rc = -errno;

	return rc;
}


static inline int
trace_file_sz(struct __rte_trace_header *hdr)
{
	return sizeof(struct __rte_trace_stream_header) + hdr->offset;
}

static int
trace_mem_save(struct trace *trace, struct __rte_trace_header *hdr,
		uint32_t cnt)
{
	char file_name[PATH_MAX];
	FILE *f;
	int rc;

	rc = snprintf(file_name, PATH_MAX, "%s/channel0_%d", trace->dir, cnt);
	if (rc < 0)
		return rc;

	f = fopen(file_name, "w");
	if (f == NULL)
		return -errno;

	rc = fwrite(&hdr->stream_header, trace_file_sz(hdr), 1, f);
	rc = (rc == 1) ?  0 : -EACCES;

	if (fclose(f))
		rc = -errno;

	return rc;
}

int
rte_trace_save(void)
{
	struct trace *trace = trace_obj_get();
	struct __rte_trace_header *header;
	uint32_t count;
	int rc = 0;

	if (trace->nb_trace_mem_list == 0)
		return rc;

	rc = trace_meta_save(trace);
	if (rc)
		return rc;

	rte_spinlock_lock(&trace->lock);
	for (count = 0; count < trace->nb_trace_mem_list; count++) {
		header = trace->lcore_meta[count].mem;
		rc =  trace_mem_save(trace, header, count);
		if (rc)
			break;
	}
	rte_spinlock_unlock(&trace->lock);
	return rc;
}