1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(C) 2020 Marvell International Ltd.
10 #include <rte_common.h>
11 #include <rte_errno.h>
12 #include <rte_string_fns.h>
14 #include "eal_filesystem.h"
15 #include "eal_trace.h"
18 trace_mode_to_string(enum rte_trace_mode mode)
21 case RTE_TRACE_MODE_OVERWRITE: return "overwrite";
22 case RTE_TRACE_MODE_DISCARD: return "discard";
23 default: return "unknown";
28 trace_area_to_string(enum trace_area_e area)
31 case TRACE_AREA_HEAP: return "heap";
32 case TRACE_AREA_HUGEPAGE: return "hugepage";
33 default: return "unknown";
38 trace_entry_compare(const char *name)
40 struct trace_point_head *tp_list = trace_list_head_get();
41 struct trace_point *tp;
44 STAILQ_FOREACH(tp, tp_list, next) {
45 if (strncmp(tp->name, name, TRACE_POINT_NAME_SIZE) == 0)
48 trace_err("found duplicate entry %s", name);
57 trace_has_duplicate_entry(void)
59 struct trace_point_head *tp_list = trace_list_head_get();
60 struct trace_point *tp;
62 /* Is duplicate trace name registered */
63 STAILQ_FOREACH(tp, tp_list, next)
64 if (trace_entry_compare(tp->name))
71 trace_uuid_generate(void)
73 struct trace_point_head *tp_list = trace_list_head_get();
74 struct trace *trace = trace_obj_get();
75 struct trace_point *tp;
76 uint64_t sz_total = 0;
78 /* Go over the registered trace points to get total size of events */
79 STAILQ_FOREACH(tp, tp_list, next) {
80 const uint16_t sz = *tp->handle & __RTE_TRACE_FIELD_SIZE_MASK;
84 rte_uuid_t uuid = RTE_UUID_INIT(sz_total, trace->nb_trace_points,
85 0x4370, 0x8f50, 0x222ddd514176ULL);
86 rte_uuid_copy(trace->uuid, uuid);
90 trace_session_name_generate(char *trace_dir)
100 tm_result = localtime(&tm);
101 if (tm_result == NULL)
104 rc = rte_strscpy(trace_dir, eal_get_hugefile_prefix(),
107 rc = TRACE_PREFIX_LEN - 1;
108 trace_dir[rc++] = '-';
110 rc = strftime(trace_dir + rc, TRACE_DIR_STR_LEN - rc,
111 "%Y-%m-%d-%p-%I-%M-%S", tm_result);
124 trace_dir_update(const char *str)
126 struct trace *trace = trace_obj_get();
129 remaining = sizeof(trace->dir) - trace->dir_offset;
130 rc = rte_strscpy(&trace->dir[0] + trace->dir_offset, str, remaining);
134 trace->dir_offset += rc;
140 eal_trace_args_save(const char *val)
142 struct trace *trace = trace_obj_get();
143 struct trace_arg *arg = malloc(sizeof(*arg));
146 trace_err("failed to allocate memory for %s", val);
150 arg->val = strdup(val);
151 if (arg->val == NULL) {
152 trace_err("failed to allocate memory for %s", val);
157 STAILQ_INSERT_TAIL(&trace->args, arg, next);
162 eal_trace_args_free(void)
164 struct trace *trace = trace_obj_get();
165 struct trace_arg *arg;
167 while (!STAILQ_EMPTY(&trace->args)) {
168 arg = STAILQ_FIRST(&trace->args);
169 STAILQ_REMOVE_HEAD(&trace->args, next);
176 trace_args_apply(const char *arg)
178 if (rte_trace_regexp(arg, true) < 0) {
179 trace_err("cannot enable trace for %s", arg);
187 eal_trace_bufsz_args_save(char const *val)
189 struct trace *trace = trace_obj_get();
192 bufsz = rte_str_to_size(val);
194 trace_err("buffer size cannot be zero");
198 trace->buff_len = bufsz;
203 trace_bufsz_args_apply(void)
205 struct trace *trace = trace_obj_get();
207 if (trace->buff_len == 0)
208 trace->buff_len = 1024 * 1024; /* 1MB */
212 eal_trace_mode_args_save(const char *val)
214 struct trace *trace = trace_obj_get();
215 size_t len = strlen(val);
220 trace_err("value is not provided with option");
224 pattern = (char *)calloc(1, len + 2);
225 if (pattern == NULL) {
226 trace_err("fail to allocate memory");
230 sprintf(pattern, "%s*", val);
232 if (fnmatch(pattern, "overwrite", 0) == 0)
233 tmp = RTE_TRACE_MODE_OVERWRITE;
234 else if (fnmatch(pattern, "discard", 0) == 0)
235 tmp = RTE_TRACE_MODE_DISCARD;
247 eal_trace_dir_args_save(char const *val)
249 struct trace *trace = trace_obj_get();
253 if (strlen(val) >= sizeof(trace->dir) - 1) {
254 trace_err("input string is too big");
255 return -ENAMETOOLONG;
258 if (asprintf(&dir_path, "%s/", val) == -1) {
259 trace_err("failed to copy directory: %s", strerror(errno));
263 rc = trace_dir_update(dir_path);
270 trace_epoch_time_save(void)
272 struct trace *trace = trace_obj_get();
273 struct timespec epoch = { 0, 0 };
274 uint64_t avg, start, end;
276 start = rte_get_tsc_cycles();
277 if (clock_gettime(CLOCK_REALTIME, &epoch) < 0) {
278 trace_err("failed to get the epoch time");
281 end = rte_get_tsc_cycles();
282 avg = (start + end) >> 1;
284 trace->epoch_sec = (uint64_t) epoch.tv_sec;
285 trace->epoch_nsec = (uint64_t) epoch.tv_nsec;
286 trace->uptime_ticks = avg;
292 trace_dir_default_path_get(char *dir_path)
294 struct trace *trace = trace_obj_get();
295 uint32_t size = sizeof(trace->dir);
299 /* First check for shell environment variable */
300 home_dir = getenv("HOME");
301 if (home_dir == NULL) {
302 /* Fallback to password file entry */
303 pwd = getpwuid(getuid());
307 home_dir = pwd->pw_dir;
310 /* Append dpdk-traces to directory */
311 if (snprintf(dir_path, size, "%s/dpdk-traces/", home_dir) < 0)
312 return -ENAMETOOLONG;
320 struct trace *trace = trace_obj_get();
321 char session[TRACE_DIR_STR_LEN];
325 if (!trace->dir_offset) {
326 dir_path = calloc(1, sizeof(trace->dir));
327 if (dir_path == NULL) {
328 trace_err("fail to allocate memory");
332 rc = trace_dir_default_path_get(dir_path);
334 trace_err("fail to get default path");
339 rc = trace_dir_update(dir_path);
345 /* Create the path if it t exist, no "mkdir -p" available here */
346 rc = mkdir(trace->dir, 0700);
347 if (rc < 0 && errno != EEXIST) {
348 trace_err("mkdir %s failed [%s]", trace->dir, strerror(errno));
353 rc = trace_session_name_generate(session);
356 rc = trace_dir_update(session);
360 rc = mkdir(trace->dir, 0700);
362 trace_err("mkdir %s failed [%s]", trace->dir, strerror(errno));
367 RTE_LOG(INFO, EAL, "Trace dir: %s\n", trace->dir);
372 trace_meta_save(struct trace *trace)
374 char file_name[PATH_MAX];
378 rc = snprintf(file_name, PATH_MAX, "%s/metadata", trace->dir);
382 f = fopen(file_name, "w");
386 rc = rte_trace_metadata_dump(f);
396 trace_file_sz(struct __rte_trace_header *hdr)
398 return sizeof(struct __rte_trace_stream_header) + hdr->offset;
402 trace_mem_save(struct trace *trace, struct __rte_trace_header *hdr,
405 char file_name[PATH_MAX];
409 rc = snprintf(file_name, PATH_MAX, "%s/channel0_%d", trace->dir, cnt);
413 f = fopen(file_name, "w");
417 rc = fwrite(&hdr->stream_header, trace_file_sz(hdr), 1, f);
418 rc = (rc == 1) ? 0 : -EACCES;
429 struct trace *trace = trace_obj_get();
430 struct __rte_trace_header *header;
434 if (trace->nb_trace_mem_list == 0)
437 rc = trace_meta_save(trace);
441 rte_spinlock_lock(&trace->lock);
442 for (count = 0; count < trace->nb_trace_mem_list; count++) {
443 header = trace->lcore_meta[count].mem;
444 rc = trace_mem_save(trace, header, count);
448 rte_spinlock_unlock(&trace->lock);