xref: /dpdk/lib/eal/common/eal_common_trace.c (revision 283d843722f11c4cb4714fa8661f4cfb7986b0e6)
199a2dd95SBruce Richardson /* SPDX-License-Identifier: BSD-3-Clause
299a2dd95SBruce Richardson  * Copyright(C) 2020 Marvell International Ltd.
399a2dd95SBruce Richardson  */
499a2dd95SBruce Richardson 
572b452c5SDmitry Kozlyuk #include <stdlib.h>
699a2dd95SBruce Richardson #include <fnmatch.h>
72744cb6eSThomas Monjalon #include <pthread.h>
899a2dd95SBruce Richardson #include <sys/queue.h>
999a2dd95SBruce Richardson #include <regex.h>
1099a2dd95SBruce Richardson 
1199a2dd95SBruce Richardson #include <rte_common.h>
1299a2dd95SBruce Richardson #include <rte_errno.h>
1399a2dd95SBruce Richardson #include <rte_lcore.h>
1499a2dd95SBruce Richardson #include <rte_per_lcore.h>
1599a2dd95SBruce Richardson #include <rte_string_fns.h>
1699a2dd95SBruce Richardson 
1799a2dd95SBruce Richardson #include "eal_trace.h"
1899a2dd95SBruce Richardson 
1999a2dd95SBruce Richardson RTE_DEFINE_PER_LCORE(volatile int, trace_point_sz);
2099a2dd95SBruce Richardson RTE_DEFINE_PER_LCORE(void *, trace_mem);
2199a2dd95SBruce Richardson static RTE_DEFINE_PER_LCORE(char *, ctf_field);
2299a2dd95SBruce Richardson 
2399a2dd95SBruce Richardson static struct trace_point_head tp_list = STAILQ_HEAD_INITIALIZER(tp_list);
2499a2dd95SBruce Richardson static struct trace trace = { .args = STAILQ_HEAD_INITIALIZER(trace.args), };
2599a2dd95SBruce Richardson 
2699a2dd95SBruce Richardson struct trace *
trace_obj_get(void)2799a2dd95SBruce Richardson trace_obj_get(void)
2899a2dd95SBruce Richardson {
2999a2dd95SBruce Richardson 	return &trace;
3099a2dd95SBruce Richardson }
3199a2dd95SBruce Richardson 
3299a2dd95SBruce Richardson struct trace_point_head *
trace_list_head_get(void)3399a2dd95SBruce Richardson trace_list_head_get(void)
3499a2dd95SBruce Richardson {
3599a2dd95SBruce Richardson 	return &tp_list;
3699a2dd95SBruce Richardson }
3799a2dd95SBruce Richardson 
3899a2dd95SBruce Richardson int
eal_trace_init(void)3999a2dd95SBruce Richardson eal_trace_init(void)
4099a2dd95SBruce Richardson {
4199a2dd95SBruce Richardson 	struct trace_arg *arg;
4299a2dd95SBruce Richardson 
4399a2dd95SBruce Richardson 	/* Trace memory should start with 8B aligned for natural alignment */
4499a2dd95SBruce Richardson 	RTE_BUILD_BUG_ON((offsetof(struct __rte_trace_header, mem) % 8) != 0);
4599a2dd95SBruce Richardson 
4699a2dd95SBruce Richardson 	/* One of the trace point registration failed */
4799a2dd95SBruce Richardson 	if (trace.register_errno) {
4899a2dd95SBruce Richardson 		rte_errno = trace.register_errno;
4999a2dd95SBruce Richardson 		goto fail;
5099a2dd95SBruce Richardson 	}
5199a2dd95SBruce Richardson 
5299a2dd95SBruce Richardson 	rte_spinlock_init(&trace.lock);
5399a2dd95SBruce Richardson 
5499a2dd95SBruce Richardson 	/* Is duplicate trace name registered */
5599a2dd95SBruce Richardson 	if (trace_has_duplicate_entry())
5699a2dd95SBruce Richardson 		goto fail;
5799a2dd95SBruce Richardson 
5899a2dd95SBruce Richardson 	/* Generate UUID ver 4 with total size of events and number of
5999a2dd95SBruce Richardson 	 * events
6099a2dd95SBruce Richardson 	 */
6199a2dd95SBruce Richardson 	trace_uuid_generate();
6299a2dd95SBruce Richardson 
6399a2dd95SBruce Richardson 	/* Apply buffer size configuration for trace output */
6499a2dd95SBruce Richardson 	trace_bufsz_args_apply();
6599a2dd95SBruce Richardson 
6699a2dd95SBruce Richardson 	/* Generate CTF TDSL metadata */
6799a2dd95SBruce Richardson 	if (trace_metadata_create() < 0)
6899a2dd95SBruce Richardson 		goto fail;
6999a2dd95SBruce Richardson 
7099a2dd95SBruce Richardson 	/* Save current epoch timestamp for future use */
7199a2dd95SBruce Richardson 	if (trace_epoch_time_save() < 0)
72d6fd5a01SDavid Marchand 		goto free_meta;
7399a2dd95SBruce Richardson 
7499a2dd95SBruce Richardson 	/* Apply global configurations */
7599a2dd95SBruce Richardson 	STAILQ_FOREACH(arg, &trace.args, next)
7699a2dd95SBruce Richardson 		trace_args_apply(arg->val);
7799a2dd95SBruce Richardson 
7899a2dd95SBruce Richardson 	rte_trace_mode_set(trace.mode);
7999a2dd95SBruce Richardson 
8099a2dd95SBruce Richardson 	return 0;
8199a2dd95SBruce Richardson 
8299a2dd95SBruce Richardson free_meta:
8399a2dd95SBruce Richardson 	trace_metadata_destroy();
8499a2dd95SBruce Richardson fail:
8599a2dd95SBruce Richardson 	trace_err("failed to initialize trace [%s]", rte_strerror(rte_errno));
8699a2dd95SBruce Richardson 	return -rte_errno;
8799a2dd95SBruce Richardson }
8899a2dd95SBruce Richardson 
8999a2dd95SBruce Richardson void
eal_trace_fini(void)9099a2dd95SBruce Richardson eal_trace_fini(void)
9199a2dd95SBruce Richardson {
9299a2dd95SBruce Richardson 	trace_mem_free();
9399a2dd95SBruce Richardson 	trace_metadata_destroy();
9499a2dd95SBruce Richardson 	eal_trace_args_free();
9599a2dd95SBruce Richardson }
9699a2dd95SBruce Richardson 
9799a2dd95SBruce Richardson bool
rte_trace_is_enabled(void)9899a2dd95SBruce Richardson rte_trace_is_enabled(void)
9999a2dd95SBruce Richardson {
1002a7a42a5STyler Retzlaff 	return rte_atomic_load_explicit(&trace.status, rte_memory_order_acquire) != 0;
10199a2dd95SBruce Richardson }
10299a2dd95SBruce Richardson 
10399a2dd95SBruce Richardson static void
trace_mode_set(rte_trace_point_t * t,enum rte_trace_mode mode)104d6fd5a01SDavid Marchand trace_mode_set(rte_trace_point_t *t, enum rte_trace_mode mode)
10599a2dd95SBruce Richardson {
10699a2dd95SBruce Richardson 	if (mode == RTE_TRACE_MODE_OVERWRITE)
1071ec6a845STyler Retzlaff 		rte_atomic_fetch_and_explicit(t, ~__RTE_TRACE_FIELD_ENABLE_DISCARD,
1081ec6a845STyler Retzlaff 			rte_memory_order_release);
10999a2dd95SBruce Richardson 	else
1101ec6a845STyler Retzlaff 		rte_atomic_fetch_or_explicit(t, __RTE_TRACE_FIELD_ENABLE_DISCARD,
1111ec6a845STyler Retzlaff 			rte_memory_order_release);
11299a2dd95SBruce Richardson }
11399a2dd95SBruce Richardson 
11499a2dd95SBruce Richardson void
rte_trace_mode_set(enum rte_trace_mode mode)11599a2dd95SBruce Richardson rte_trace_mode_set(enum rte_trace_mode mode)
11699a2dd95SBruce Richardson {
11799a2dd95SBruce Richardson 	struct trace_point *tp;
11899a2dd95SBruce Richardson 
11999a2dd95SBruce Richardson 	STAILQ_FOREACH(tp, &tp_list, next)
12099a2dd95SBruce Richardson 		trace_mode_set(tp->handle, mode);
12199a2dd95SBruce Richardson 
12299a2dd95SBruce Richardson 	trace.mode = mode;
12399a2dd95SBruce Richardson }
12499a2dd95SBruce Richardson 
12599a2dd95SBruce Richardson enum
rte_trace_mode_get(void)12699a2dd95SBruce Richardson rte_trace_mode rte_trace_mode_get(void)
12799a2dd95SBruce Richardson {
12899a2dd95SBruce Richardson 	return trace.mode;
12999a2dd95SBruce Richardson }
13099a2dd95SBruce Richardson 
13199a2dd95SBruce Richardson static bool
trace_point_is_invalid(rte_trace_point_t * t)13299a2dd95SBruce Richardson trace_point_is_invalid(rte_trace_point_t *t)
13399a2dd95SBruce Richardson {
13499a2dd95SBruce Richardson 	return (t == NULL) || (trace_id_get(t) >= trace.nb_trace_points);
13599a2dd95SBruce Richardson }
13699a2dd95SBruce Richardson 
13799a2dd95SBruce Richardson bool
rte_trace_point_is_enabled(rte_trace_point_t * t)138d6fd5a01SDavid Marchand rte_trace_point_is_enabled(rte_trace_point_t *t)
13999a2dd95SBruce Richardson {
14099a2dd95SBruce Richardson 	uint64_t val;
14199a2dd95SBruce Richardson 
142d6fd5a01SDavid Marchand 	if (trace_point_is_invalid(t))
14399a2dd95SBruce Richardson 		return false;
14499a2dd95SBruce Richardson 
1451ec6a845STyler Retzlaff 	val = rte_atomic_load_explicit(t, rte_memory_order_acquire);
14699a2dd95SBruce Richardson 	return (val & __RTE_TRACE_FIELD_ENABLE_MASK) != 0;
14799a2dd95SBruce Richardson }
14899a2dd95SBruce Richardson 
14999a2dd95SBruce Richardson int
rte_trace_point_enable(rte_trace_point_t * t)150d6fd5a01SDavid Marchand rte_trace_point_enable(rte_trace_point_t *t)
15199a2dd95SBruce Richardson {
152d6fd5a01SDavid Marchand 	uint64_t prev;
153d6fd5a01SDavid Marchand 
154d6fd5a01SDavid Marchand 	if (trace_point_is_invalid(t))
15599a2dd95SBruce Richardson 		return -ERANGE;
15699a2dd95SBruce Richardson 
1571ec6a845STyler Retzlaff 	prev = rte_atomic_fetch_or_explicit(t, __RTE_TRACE_FIELD_ENABLE_MASK,
1581ec6a845STyler Retzlaff 		rte_memory_order_release);
159d6fd5a01SDavid Marchand 	if ((prev & __RTE_TRACE_FIELD_ENABLE_MASK) == 0)
1602a7a42a5STyler Retzlaff 		rte_atomic_fetch_add_explicit(&trace.status, 1, rte_memory_order_release);
16199a2dd95SBruce Richardson 	return 0;
16299a2dd95SBruce Richardson }
16399a2dd95SBruce Richardson 
16499a2dd95SBruce Richardson int
rte_trace_point_disable(rte_trace_point_t * t)165d6fd5a01SDavid Marchand rte_trace_point_disable(rte_trace_point_t *t)
16699a2dd95SBruce Richardson {
167d6fd5a01SDavid Marchand 	uint64_t prev;
168d6fd5a01SDavid Marchand 
169d6fd5a01SDavid Marchand 	if (trace_point_is_invalid(t))
17099a2dd95SBruce Richardson 		return -ERANGE;
17199a2dd95SBruce Richardson 
1721ec6a845STyler Retzlaff 	prev = rte_atomic_fetch_and_explicit(t, ~__RTE_TRACE_FIELD_ENABLE_MASK,
1731ec6a845STyler Retzlaff 		rte_memory_order_release);
174d6fd5a01SDavid Marchand 	if ((prev & __RTE_TRACE_FIELD_ENABLE_MASK) != 0)
1752a7a42a5STyler Retzlaff 		rte_atomic_fetch_sub_explicit(&trace.status, 1, rte_memory_order_release);
17699a2dd95SBruce Richardson 	return 0;
17799a2dd95SBruce Richardson }
17899a2dd95SBruce Richardson 
17999a2dd95SBruce Richardson int
rte_trace_pattern(const char * pattern,bool enable)18099a2dd95SBruce Richardson rte_trace_pattern(const char *pattern, bool enable)
18199a2dd95SBruce Richardson {
18299a2dd95SBruce Richardson 	struct trace_point *tp;
18399a2dd95SBruce Richardson 	int rc = 0, found = 0;
18499a2dd95SBruce Richardson 
18599a2dd95SBruce Richardson 	STAILQ_FOREACH(tp, &tp_list, next) {
1863ee927d3SDavid Marchand 		if (fnmatch(pattern, tp->name, 0) != 0)
1873ee927d3SDavid Marchand 			continue;
1883ee927d3SDavid Marchand 
18999a2dd95SBruce Richardson 		if (enable)
19099a2dd95SBruce Richardson 			rc = rte_trace_point_enable(tp->handle);
19199a2dd95SBruce Richardson 		else
19299a2dd95SBruce Richardson 			rc = rte_trace_point_disable(tp->handle);
1933ee927d3SDavid Marchand 		if (rc < 0) {
1943ee927d3SDavid Marchand 			found = 0;
1953ee927d3SDavid Marchand 			break;
19699a2dd95SBruce Richardson 		}
1973ee927d3SDavid Marchand 		found = 1;
19899a2dd95SBruce Richardson 	}
19999a2dd95SBruce Richardson 
20099a2dd95SBruce Richardson 	return rc | found;
20199a2dd95SBruce Richardson }
20299a2dd95SBruce Richardson 
20399a2dd95SBruce Richardson int
rte_trace_regexp(const char * regex,bool enable)20499a2dd95SBruce Richardson rte_trace_regexp(const char *regex, bool enable)
20599a2dd95SBruce Richardson {
20699a2dd95SBruce Richardson 	struct trace_point *tp;
20799a2dd95SBruce Richardson 	int rc = 0, found = 0;
20899a2dd95SBruce Richardson 	regex_t r;
20999a2dd95SBruce Richardson 
21099a2dd95SBruce Richardson 	if (regcomp(&r, regex, 0) != 0)
21199a2dd95SBruce Richardson 		return -EINVAL;
21299a2dd95SBruce Richardson 
21399a2dd95SBruce Richardson 	STAILQ_FOREACH(tp, &tp_list, next) {
2143ee927d3SDavid Marchand 		if (regexec(&r, tp->name, 0, NULL, 0) != 0)
2153ee927d3SDavid Marchand 			continue;
2163ee927d3SDavid Marchand 
21799a2dd95SBruce Richardson 		if (enable)
21899a2dd95SBruce Richardson 			rc = rte_trace_point_enable(tp->handle);
21999a2dd95SBruce Richardson 		else
22099a2dd95SBruce Richardson 			rc = rte_trace_point_disable(tp->handle);
221b980ced0SDavid Marchand 		if (rc < 0) {
222b980ced0SDavid Marchand 			found = 0;
223b980ced0SDavid Marchand 			break;
224b980ced0SDavid Marchand 		}
2253ee927d3SDavid Marchand 		found = 1;
22699a2dd95SBruce Richardson 	}
22799a2dd95SBruce Richardson 	regfree(&r);
22899a2dd95SBruce Richardson 
22999a2dd95SBruce Richardson 	return rc | found;
23099a2dd95SBruce Richardson }
23199a2dd95SBruce Richardson 
23299a2dd95SBruce Richardson rte_trace_point_t *
rte_trace_point_lookup(const char * name)23399a2dd95SBruce Richardson rte_trace_point_lookup(const char *name)
23499a2dd95SBruce Richardson {
23599a2dd95SBruce Richardson 	struct trace_point *tp;
23699a2dd95SBruce Richardson 
23799a2dd95SBruce Richardson 	if (name == NULL)
23899a2dd95SBruce Richardson 		return NULL;
23999a2dd95SBruce Richardson 
24099a2dd95SBruce Richardson 	STAILQ_FOREACH(tp, &tp_list, next)
241477cc313SDavid Marchand 		if (strcmp(tp->name, name) == 0)
24299a2dd95SBruce Richardson 			return tp->handle;
24399a2dd95SBruce Richardson 
24499a2dd95SBruce Richardson 	return NULL;
24599a2dd95SBruce Richardson }
24699a2dd95SBruce Richardson 
24799a2dd95SBruce Richardson static void
trace_point_dump(FILE * f,struct trace_point * tp)24899a2dd95SBruce Richardson trace_point_dump(FILE *f, struct trace_point *tp)
24999a2dd95SBruce Richardson {
25099a2dd95SBruce Richardson 	rte_trace_point_t *handle = tp->handle;
25199a2dd95SBruce Richardson 
25299a2dd95SBruce Richardson 	fprintf(f, "\tid %d, %s, size is %d, %s\n",
25399a2dd95SBruce Richardson 		trace_id_get(handle), tp->name,
25499a2dd95SBruce Richardson 		(uint16_t)(*handle & __RTE_TRACE_FIELD_SIZE_MASK),
25599a2dd95SBruce Richardson 		rte_trace_point_is_enabled(handle) ? "enabled" : "disabled");
25699a2dd95SBruce Richardson }
25799a2dd95SBruce Richardson 
25899a2dd95SBruce Richardson static void
trace_lcore_mem_dump(FILE * f)25999a2dd95SBruce Richardson trace_lcore_mem_dump(FILE *f)
26099a2dd95SBruce Richardson {
26199a2dd95SBruce Richardson 	struct trace *trace = trace_obj_get();
26299a2dd95SBruce Richardson 	struct __rte_trace_header *header;
26399a2dd95SBruce Richardson 	uint32_t count;
26499a2dd95SBruce Richardson 
26599a2dd95SBruce Richardson 	rte_spinlock_lock(&trace->lock);
266782dbf17SDavid Marchand 	if (trace->nb_trace_mem_list == 0)
267782dbf17SDavid Marchand 		goto out;
26899a2dd95SBruce Richardson 	fprintf(f, "nb_trace_mem_list = %d\n", trace->nb_trace_mem_list);
26999a2dd95SBruce Richardson 	fprintf(f, "\nTrace mem info\n--------------\n");
27099a2dd95SBruce Richardson 	for (count = 0; count < trace->nb_trace_mem_list; count++) {
27199a2dd95SBruce Richardson 		header = trace->lcore_meta[count].mem;
27299a2dd95SBruce Richardson 		fprintf(f, "\tid %d, mem=%p, area=%s, lcore_id=%d, name=%s\n",
27399a2dd95SBruce Richardson 		count, header,
27499a2dd95SBruce Richardson 		trace_area_to_string(trace->lcore_meta[count].area),
27599a2dd95SBruce Richardson 		header->stream_header.lcore_id,
27699a2dd95SBruce Richardson 		header->stream_header.thread_name);
27799a2dd95SBruce Richardson 	}
278782dbf17SDavid Marchand out:
27999a2dd95SBruce Richardson 	rte_spinlock_unlock(&trace->lock);
28099a2dd95SBruce Richardson }
28199a2dd95SBruce Richardson 
28299a2dd95SBruce Richardson void
rte_trace_dump(FILE * f)28399a2dd95SBruce Richardson rte_trace_dump(FILE *f)
28499a2dd95SBruce Richardson {
28599a2dd95SBruce Richardson 	struct trace_point_head *tp_list = trace_list_head_get();
28699a2dd95SBruce Richardson 	struct trace *trace = trace_obj_get();
28799a2dd95SBruce Richardson 	struct trace_point *tp;
28899a2dd95SBruce Richardson 
28999a2dd95SBruce Richardson 	fprintf(f, "\nGlobal info\n-----------\n");
29099a2dd95SBruce Richardson 	fprintf(f, "status = %s\n",
29199a2dd95SBruce Richardson 		rte_trace_is_enabled() ? "enabled" : "disabled");
29299a2dd95SBruce Richardson 	fprintf(f, "mode = %s\n",
29399a2dd95SBruce Richardson 		trace_mode_to_string(rte_trace_mode_get()));
29499a2dd95SBruce Richardson 	fprintf(f, "dir = %s\n", trace->dir);
29599a2dd95SBruce Richardson 	fprintf(f, "buffer len = %d\n", trace->buff_len);
29699a2dd95SBruce Richardson 	fprintf(f, "number of trace points = %d\n", trace->nb_trace_points);
29799a2dd95SBruce Richardson 
29899a2dd95SBruce Richardson 	trace_lcore_mem_dump(f);
29999a2dd95SBruce Richardson 	fprintf(f, "\nTrace point info\n----------------\n");
30099a2dd95SBruce Richardson 	STAILQ_FOREACH(tp, tp_list, next)
30199a2dd95SBruce Richardson 		trace_point_dump(f, tp);
30299a2dd95SBruce Richardson }
30399a2dd95SBruce Richardson 
304e5702270STyler Retzlaff static void
thread_get_name(rte_thread_t id,char * name,size_t len)305e5702270STyler Retzlaff thread_get_name(rte_thread_t id, char *name, size_t len)
306e5702270STyler Retzlaff {
307e5702270STyler Retzlaff #if defined(RTE_EXEC_ENV_LINUX) && defined(__GLIBC__) && defined(__GLIBC_PREREQ)
308e5702270STyler Retzlaff #if __GLIBC_PREREQ(2, 12)
309e5702270STyler Retzlaff 	pthread_getname_np((pthread_t)id.opaque_id, name, len);
310e5702270STyler Retzlaff #endif
311e5702270STyler Retzlaff #endif
312e5702270STyler Retzlaff 	RTE_SET_USED(id);
313e5702270STyler Retzlaff 	RTE_SET_USED(name);
314e5702270STyler Retzlaff 	RTE_SET_USED(len);
315e5702270STyler Retzlaff }
316e5702270STyler Retzlaff 
31799a2dd95SBruce Richardson void
__rte_trace_mem_per_thread_alloc(void)31899a2dd95SBruce Richardson __rte_trace_mem_per_thread_alloc(void)
31999a2dd95SBruce Richardson {
32099a2dd95SBruce Richardson 	struct trace *trace = trace_obj_get();
32199a2dd95SBruce Richardson 	struct __rte_trace_header *header;
32299a2dd95SBruce Richardson 	uint32_t count;
32399a2dd95SBruce Richardson 
32499a2dd95SBruce Richardson 	if (!rte_trace_is_enabled())
32599a2dd95SBruce Richardson 		return;
32699a2dd95SBruce Richardson 
32799a2dd95SBruce Richardson 	if (RTE_PER_LCORE(trace_mem))
32899a2dd95SBruce Richardson 		return;
32999a2dd95SBruce Richardson 
33099a2dd95SBruce Richardson 	rte_spinlock_lock(&trace->lock);
33199a2dd95SBruce Richardson 
33299a2dd95SBruce Richardson 	count = trace->nb_trace_mem_list;
33399a2dd95SBruce Richardson 
33499a2dd95SBruce Richardson 	/* Allocate room for storing the thread trace mem meta */
33599a2dd95SBruce Richardson 	trace->lcore_meta = realloc(trace->lcore_meta,
33699a2dd95SBruce Richardson 		sizeof(trace->lcore_meta[0]) * (count + 1));
33799a2dd95SBruce Richardson 
33899a2dd95SBruce Richardson 	/* Provide dummy space for fast path to consume */
33999a2dd95SBruce Richardson 	if (trace->lcore_meta == NULL) {
34099a2dd95SBruce Richardson 		trace_crit("trace mem meta memory realloc failed");
34199a2dd95SBruce Richardson 		header = NULL;
34299a2dd95SBruce Richardson 		goto fail;
34399a2dd95SBruce Richardson 	}
34499a2dd95SBruce Richardson 
34599a2dd95SBruce Richardson 	/* First attempt from huge page */
34699a2dd95SBruce Richardson 	header = eal_malloc_no_trace(NULL, trace_mem_sz(trace->buff_len), 8);
34799a2dd95SBruce Richardson 	if (header) {
34899a2dd95SBruce Richardson 		trace->lcore_meta[count].area = TRACE_AREA_HUGEPAGE;
34999a2dd95SBruce Richardson 		goto found;
35099a2dd95SBruce Richardson 	}
35199a2dd95SBruce Richardson 
35299a2dd95SBruce Richardson 	/* Second attempt from heap */
35399a2dd95SBruce Richardson 	header = malloc(trace_mem_sz(trace->buff_len));
35499a2dd95SBruce Richardson 	if (header == NULL) {
35599a2dd95SBruce Richardson 		trace_crit("trace mem malloc attempt failed");
35699a2dd95SBruce Richardson 		header = NULL;
35799a2dd95SBruce Richardson 		goto fail;
35899a2dd95SBruce Richardson 
35999a2dd95SBruce Richardson 	}
36099a2dd95SBruce Richardson 
36199a2dd95SBruce Richardson 	/* Second attempt from heap is success */
36299a2dd95SBruce Richardson 	trace->lcore_meta[count].area = TRACE_AREA_HEAP;
36399a2dd95SBruce Richardson 
36499a2dd95SBruce Richardson 	/* Initialize the trace header */
36599a2dd95SBruce Richardson found:
36699a2dd95SBruce Richardson 	header->offset = 0;
36799a2dd95SBruce Richardson 	header->len = trace->buff_len;
36899a2dd95SBruce Richardson 	header->stream_header.magic = TRACE_CTF_MAGIC;
36999a2dd95SBruce Richardson 	rte_uuid_copy(header->stream_header.uuid, trace->uuid);
37099a2dd95SBruce Richardson 	header->stream_header.lcore_id = rte_lcore_id();
37199a2dd95SBruce Richardson 
37299a2dd95SBruce Richardson 	/* Store the thread name */
37399a2dd95SBruce Richardson 	char *name = header->stream_header.thread_name;
37499a2dd95SBruce Richardson 	memset(name, 0, __RTE_TRACE_EMIT_STRING_LEN_MAX);
375e5702270STyler Retzlaff 	thread_get_name(rte_thread_self(), name,
37699a2dd95SBruce Richardson 		__RTE_TRACE_EMIT_STRING_LEN_MAX);
37799a2dd95SBruce Richardson 
37899a2dd95SBruce Richardson 	trace->lcore_meta[count].mem = header;
37999a2dd95SBruce Richardson 	trace->nb_trace_mem_list++;
38099a2dd95SBruce Richardson fail:
38199a2dd95SBruce Richardson 	RTE_PER_LCORE(trace_mem) = header;
38299a2dd95SBruce Richardson 	rte_spinlock_unlock(&trace->lock);
38399a2dd95SBruce Richardson }
38499a2dd95SBruce Richardson 
38599a2dd95SBruce Richardson static void
trace_mem_per_thread_free_unlocked(struct thread_mem_meta * meta)38699a2dd95SBruce Richardson trace_mem_per_thread_free_unlocked(struct thread_mem_meta *meta)
38799a2dd95SBruce Richardson {
38899a2dd95SBruce Richardson 	if (meta->area == TRACE_AREA_HUGEPAGE)
38999a2dd95SBruce Richardson 		eal_free_no_trace(meta->mem);
39099a2dd95SBruce Richardson 	else if (meta->area == TRACE_AREA_HEAP)
39199a2dd95SBruce Richardson 		free(meta->mem);
39299a2dd95SBruce Richardson }
39399a2dd95SBruce Richardson 
39499a2dd95SBruce Richardson void
trace_mem_per_thread_free(void)39599a2dd95SBruce Richardson trace_mem_per_thread_free(void)
39699a2dd95SBruce Richardson {
39799a2dd95SBruce Richardson 	struct trace *trace = trace_obj_get();
39899a2dd95SBruce Richardson 	struct __rte_trace_header *header;
39999a2dd95SBruce Richardson 	uint32_t count;
40099a2dd95SBruce Richardson 
40199a2dd95SBruce Richardson 	header = RTE_PER_LCORE(trace_mem);
40299a2dd95SBruce Richardson 	if (header == NULL)
40399a2dd95SBruce Richardson 		return;
40499a2dd95SBruce Richardson 
40599a2dd95SBruce Richardson 	rte_spinlock_lock(&trace->lock);
40699a2dd95SBruce Richardson 	for (count = 0; count < trace->nb_trace_mem_list; count++) {
40799a2dd95SBruce Richardson 		if (trace->lcore_meta[count].mem == header)
40899a2dd95SBruce Richardson 			break;
40999a2dd95SBruce Richardson 	}
41099a2dd95SBruce Richardson 	if (count != trace->nb_trace_mem_list) {
41199a2dd95SBruce Richardson 		struct thread_mem_meta *meta = &trace->lcore_meta[count];
41299a2dd95SBruce Richardson 
41399a2dd95SBruce Richardson 		trace_mem_per_thread_free_unlocked(meta);
41499a2dd95SBruce Richardson 		if (count != trace->nb_trace_mem_list - 1) {
41599a2dd95SBruce Richardson 			memmove(meta, meta + 1,
41699a2dd95SBruce Richardson 				sizeof(*meta) *
41799a2dd95SBruce Richardson 				 (trace->nb_trace_mem_list - count - 1));
41899a2dd95SBruce Richardson 		}
41999a2dd95SBruce Richardson 		trace->nb_trace_mem_list--;
42099a2dd95SBruce Richardson 	}
42199a2dd95SBruce Richardson 	rte_spinlock_unlock(&trace->lock);
42299a2dd95SBruce Richardson }
42399a2dd95SBruce Richardson 
42499a2dd95SBruce Richardson void
trace_mem_free(void)42599a2dd95SBruce Richardson trace_mem_free(void)
42699a2dd95SBruce Richardson {
42799a2dd95SBruce Richardson 	struct trace *trace = trace_obj_get();
42899a2dd95SBruce Richardson 	uint32_t count;
42999a2dd95SBruce Richardson 
43099a2dd95SBruce Richardson 	rte_spinlock_lock(&trace->lock);
43199a2dd95SBruce Richardson 	for (count = 0; count < trace->nb_trace_mem_list; count++) {
43299a2dd95SBruce Richardson 		trace_mem_per_thread_free_unlocked(&trace->lcore_meta[count]);
43399a2dd95SBruce Richardson 	}
43499a2dd95SBruce Richardson 	trace->nb_trace_mem_list = 0;
43599a2dd95SBruce Richardson 	rte_spinlock_unlock(&trace->lock);
43699a2dd95SBruce Richardson }
43799a2dd95SBruce Richardson 
43899a2dd95SBruce Richardson void
__rte_trace_point_emit_field(size_t sz,const char * in,const char * datatype)43999a2dd95SBruce Richardson __rte_trace_point_emit_field(size_t sz, const char *in, const char *datatype)
44099a2dd95SBruce Richardson {
44199a2dd95SBruce Richardson 	char *field;
44299a2dd95SBruce Richardson 	char *fixup;
44399a2dd95SBruce Richardson 	int rc;
44499a2dd95SBruce Richardson 
44599a2dd95SBruce Richardson 	fixup = trace_metadata_fixup_field(in);
44699a2dd95SBruce Richardson 	if (fixup != NULL)
44799a2dd95SBruce Richardson 		in = fixup;
44899a2dd95SBruce Richardson 	rc = asprintf(&field, "%s        %s %s;\n",
44999a2dd95SBruce Richardson 		RTE_PER_LCORE(ctf_field) != NULL ?
45099a2dd95SBruce Richardson 			RTE_PER_LCORE(ctf_field) : "",
45199a2dd95SBruce Richardson 		datatype, in);
45299a2dd95SBruce Richardson 	free(RTE_PER_LCORE(ctf_field));
45399a2dd95SBruce Richardson 	free(fixup);
45499a2dd95SBruce Richardson 	if (rc == -1) {
45599a2dd95SBruce Richardson 		RTE_PER_LCORE(trace_point_sz) = 0;
45699a2dd95SBruce Richardson 		RTE_PER_LCORE(ctf_field) = NULL;
45799a2dd95SBruce Richardson 		trace_crit("could not allocate CTF field");
45899a2dd95SBruce Richardson 		return;
45999a2dd95SBruce Richardson 	}
46099a2dd95SBruce Richardson 	RTE_PER_LCORE(trace_point_sz) += sz;
46199a2dd95SBruce Richardson 	RTE_PER_LCORE(ctf_field) = field;
46299a2dd95SBruce Richardson }
46399a2dd95SBruce Richardson 
46499a2dd95SBruce Richardson int
__rte_trace_point_register(rte_trace_point_t * handle,const char * name,void (* register_fn)(void))46599a2dd95SBruce Richardson __rte_trace_point_register(rte_trace_point_t *handle, const char *name,
46699a2dd95SBruce Richardson 		void (*register_fn)(void))
46799a2dd95SBruce Richardson {
46899a2dd95SBruce Richardson 	struct trace_point *tp;
46999a2dd95SBruce Richardson 	uint16_t sz;
47099a2dd95SBruce Richardson 
47199a2dd95SBruce Richardson 	/* Sanity checks of arguments */
47299a2dd95SBruce Richardson 	if (name == NULL || register_fn == NULL || handle == NULL) {
47399a2dd95SBruce Richardson 		trace_err("invalid arguments");
47499a2dd95SBruce Richardson 		rte_errno = EINVAL;
47599a2dd95SBruce Richardson 		goto fail;
47699a2dd95SBruce Richardson 	}
47799a2dd95SBruce Richardson 
47899a2dd95SBruce Richardson 	/* Check the size of the trace point object */
47999a2dd95SBruce Richardson 	RTE_PER_LCORE(trace_point_sz) = 0;
48099a2dd95SBruce Richardson 	register_fn();
48199a2dd95SBruce Richardson 	if (RTE_PER_LCORE(trace_point_sz) == 0) {
48299a2dd95SBruce Richardson 		trace_err("missing rte_trace_emit_header() in register fn");
48399a2dd95SBruce Richardson 		rte_errno = EBADF;
48499a2dd95SBruce Richardson 		goto fail;
48599a2dd95SBruce Richardson 	}
48699a2dd95SBruce Richardson 
48799a2dd95SBruce Richardson 	/* Is size overflowed */
48899a2dd95SBruce Richardson 	if (RTE_PER_LCORE(trace_point_sz) > UINT16_MAX) {
48999a2dd95SBruce Richardson 		trace_err("trace point size overflowed");
49099a2dd95SBruce Richardson 		rte_errno = ENOSPC;
49199a2dd95SBruce Richardson 		goto fail;
49299a2dd95SBruce Richardson 	}
49399a2dd95SBruce Richardson 
49499a2dd95SBruce Richardson 	/* Are we running out of space to store trace points? */
49599a2dd95SBruce Richardson 	if (trace.nb_trace_points > UINT16_MAX) {
49699a2dd95SBruce Richardson 		trace_err("trace point exceeds the max count");
49799a2dd95SBruce Richardson 		rte_errno = ENOSPC;
49899a2dd95SBruce Richardson 		goto fail;
49999a2dd95SBruce Richardson 	}
50099a2dd95SBruce Richardson 
50199a2dd95SBruce Richardson 	/* Get the size of the trace point */
50299a2dd95SBruce Richardson 	sz = RTE_PER_LCORE(trace_point_sz);
50399a2dd95SBruce Richardson 	tp = calloc(1, sizeof(struct trace_point));
50499a2dd95SBruce Richardson 	if (tp == NULL) {
50599a2dd95SBruce Richardson 		trace_err("fail to allocate trace point memory");
50699a2dd95SBruce Richardson 		rte_errno = ENOMEM;
50799a2dd95SBruce Richardson 		goto fail;
50899a2dd95SBruce Richardson 	}
50999a2dd95SBruce Richardson 
51099a2dd95SBruce Richardson 	/* Initialize the trace point */
511477cc313SDavid Marchand 	tp->name = name;
51299a2dd95SBruce Richardson 
51399a2dd95SBruce Richardson 	/* Copy the accumulated fields description and clear it for the next
51499a2dd95SBruce Richardson 	 * trace point.
51599a2dd95SBruce Richardson 	 */
51699a2dd95SBruce Richardson 	tp->ctf_field = RTE_PER_LCORE(ctf_field);
51799a2dd95SBruce Richardson 	RTE_PER_LCORE(ctf_field) = NULL;
51899a2dd95SBruce Richardson 
51999a2dd95SBruce Richardson 	/* Form the trace handle */
52099a2dd95SBruce Richardson 	*handle = sz;
52199a2dd95SBruce Richardson 	*handle |= trace.nb_trace_points << __RTE_TRACE_FIELD_ID_SHIFT;
52212b627bfSDavid Marchand 	trace_mode_set(handle, trace.mode);
52399a2dd95SBruce Richardson 
52499a2dd95SBruce Richardson 	trace.nb_trace_points++;
52599a2dd95SBruce Richardson 	tp->handle = handle;
52699a2dd95SBruce Richardson 
52799a2dd95SBruce Richardson 	/* Add the trace point at tail */
52899a2dd95SBruce Richardson 	STAILQ_INSERT_TAIL(&tp_list, tp, next);
529*283d8437STyler Retzlaff 	rte_atomic_thread_fence(rte_memory_order_release);
53099a2dd95SBruce Richardson 
53199a2dd95SBruce Richardson 	/* All Good !!! */
53299a2dd95SBruce Richardson 	return 0;
533477cc313SDavid Marchand 
53499a2dd95SBruce Richardson fail:
53599a2dd95SBruce Richardson 	if (trace.register_errno == 0)
53699a2dd95SBruce Richardson 		trace.register_errno = rte_errno;
53799a2dd95SBruce Richardson 
53899a2dd95SBruce Richardson 	return -rte_errno;
53999a2dd95SBruce Richardson }
540