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;
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);
122 trace_epoch_time_save(void)
124 struct trace *trace = trace_obj_get();
125 struct timespec epoch = { 0, 0 };
126 uint64_t avg, start, end;
128 start = rte_get_tsc_cycles();
129 if (clock_gettime(CLOCK_REALTIME, &epoch) < 0) {
130 trace_err("failed to get the epoch time");
133 end = rte_get_tsc_cycles();
134 avg = (start + end) >> 1;
136 trace->epoch_sec = (uint64_t) epoch.tv_sec;
137 trace->epoch_nsec = (uint64_t) epoch.tv_nsec;
138 trace->uptime_ticks = avg;
144 trace_dir_default_path_get(char *dir_path)
146 struct trace *trace = trace_obj_get();
147 uint32_t size = sizeof(trace->dir);
151 /* First check for shell environment variable */
152 home_dir = getenv("HOME");
153 if (home_dir == NULL) {
154 /* Fallback to password file entry */
155 pwd = getpwuid(getuid());
159 home_dir = pwd->pw_dir;
162 /* Append dpdk-traces to directory */
163 if (snprintf(dir_path, size, "%s/dpdk-traces/", home_dir) < 0)
164 return -ENAMETOOLONG;
172 struct trace *trace = trace_obj_get();
173 char session[TRACE_DIR_STR_LEN];
177 if (!trace->dir_offset) {
178 dir_path = calloc(1, sizeof(trace->dir));
179 if (dir_path == NULL) {
180 trace_err("fail to allocate memory");
184 rc = trace_dir_default_path_get(dir_path);
186 trace_err("fail to get default path");
193 /* Create the path if it t exist, no "mkdir -p" available here */
194 rc = mkdir(trace->dir, 0700);
195 if (rc < 0 && errno != EEXIST) {
196 trace_err("mkdir %s failed [%s]", trace->dir, strerror(errno));
201 rc = trace_session_name_generate(session);
205 rc = mkdir(trace->dir, 0700);
207 trace_err("mkdir %s failed [%s]", trace->dir, strerror(errno));
212 RTE_LOG(INFO, EAL, "Trace dir: %s\n", trace->dir);
217 trace_meta_save(struct trace *trace)
219 char file_name[PATH_MAX];
223 rc = snprintf(file_name, PATH_MAX, "%s/metadata", trace->dir);
227 f = fopen(file_name, "w");
231 rc = rte_trace_metadata_dump(f);
241 trace_file_sz(struct __rte_trace_header *hdr)
243 return sizeof(struct __rte_trace_stream_header) + hdr->offset;
247 trace_mem_save(struct trace *trace, struct __rte_trace_header *hdr,
250 char file_name[PATH_MAX];
254 rc = snprintf(file_name, PATH_MAX, "%s/channel0_%d", trace->dir, cnt);
258 f = fopen(file_name, "w");
262 rc = fwrite(&hdr->stream_header, trace_file_sz(hdr), 1, f);
263 rc = (rc == 1) ? 0 : -EACCES;
274 struct trace *trace = trace_obj_get();
275 struct __rte_trace_header *header;
279 if (trace->nb_trace_mem_list == 0)
282 rc = trace_meta_save(trace);
286 rte_spinlock_lock(&trace->lock);
287 for (count = 0; count < trace->nb_trace_mem_list; count++) {
288 header = trace->lcore_meta[count].mem;
289 rc = trace_mem_save(trace, header, count);
293 rte_spinlock_unlock(&trace->lock);