trace: add trace bufsize configuration parameter
[dpdk.git] / lib / librte_eal / common / eal_common_trace.c
index d88b2cd..5a365c6 100644 (file)
@@ -16,6 +16,7 @@
 #include "eal_trace.h"
 
 RTE_DEFINE_PER_LCORE(volatile int, trace_point_sz);
+RTE_DEFINE_PER_LCORE(void *, trace_mem);
 static RTE_DEFINE_PER_LCORE(char, ctf_field[TRACE_CTF_FIELD_SIZE]);
 static RTE_DEFINE_PER_LCORE(int, ctf_count);
 
@@ -37,12 +38,20 @@ trace_list_head_get(void)
 int
 eal_trace_init(void)
 {
+       uint8_t i;
+
+       /* Trace memory should start with 8B aligned for natural alignment */
+       RTE_BUILD_BUG_ON((offsetof(struct __rte_trace_header, mem) % 8) != 0);
+
        /* One of the trace point registration failed */
        if (trace.register_errno) {
                rte_errno = trace.register_errno;
                goto fail;
        }
 
+       if (trace.args.nb_args)
+               trace.status = true;
+
        if (!rte_trace_is_enabled())
                return 0;
 
@@ -57,18 +66,31 @@ eal_trace_init(void)
         */
        trace_uuid_generate();
 
+       /* Apply buffer size configuration for trace output */
+       trace_bufsz_args_apply();
+
+       /* Generate CTF TDSL metadata */
+       if (trace_metadata_create() < 0)
+               goto fail;
+
        /* Create trace directory */
        if (trace_mkdir())
-               goto fail;
+               goto free_meta;
 
        /* Save current epoch timestamp for future use */
        if (trace_epoch_time_save() < 0)
                goto fail;
 
+       /* Apply global configurations */
+       for (i = 0; i < trace.args.nb_args; i++)
+               trace_args_apply(trace.args.args[i]);
+
        rte_trace_mode_set(trace.mode);
 
        return 0;
 
+free_meta:
+       trace_metadata_destroy();
 fail:
        trace_err("failed to initialize trace [%s]", rte_strerror(rte_errno));
        return -rte_errno;
@@ -79,6 +101,9 @@ eal_trace_fini(void)
 {
        if (!rte_trace_is_enabled())
                return;
+       trace_mem_per_thread_free();
+       trace_metadata_destroy();
+       eal_trace_args_free();
 }
 
 bool
@@ -220,6 +245,171 @@ rte_trace_point_lookup(const char *name)
        return NULL;
 }
 
+static void
+trace_point_dump(FILE *f, struct trace_point *tp)
+{
+       rte_trace_point_t *handle = tp->handle;
+
+       fprintf(f, "\tid %d, %s, size is %d, %s\n",
+               trace_id_get(handle), tp->name,
+               (uint16_t)(*handle & __RTE_TRACE_FIELD_SIZE_MASK),
+               rte_trace_point_is_enabled(handle) ? "enabled" : "disabled");
+}
+
+static void
+trace_lcore_mem_dump(FILE *f)
+{
+       struct trace *trace = trace_obj_get();
+       struct __rte_trace_header *header;
+       uint32_t count;
+
+       if (trace->nb_trace_mem_list == 0)
+               return;
+
+       rte_spinlock_lock(&trace->lock);
+       fprintf(f, "nb_trace_mem_list = %d\n", trace->nb_trace_mem_list);
+       fprintf(f, "\nTrace mem info\n--------------\n");
+       for (count = 0; count < trace->nb_trace_mem_list; count++) {
+               header = trace->lcore_meta[count].mem;
+               fprintf(f, "\tid %d, mem=%p, area=%s, lcore_id=%d, name=%s\n",
+               count, header,
+               trace_area_to_string(trace->lcore_meta[count].area),
+               header->stream_header.lcore_id,
+               header->stream_header.thread_name);
+       }
+       rte_spinlock_unlock(&trace->lock);
+}
+
+void
+rte_trace_dump(FILE *f)
+{
+       struct trace_point_head *tp_list = trace_list_head_get();
+       struct trace *trace = trace_obj_get();
+       struct trace_point *tp;
+
+       fprintf(f, "\nGlobal info\n-----------\n");
+       fprintf(f, "status = %s\n",
+               rte_trace_is_enabled() ? "enabled" : "disabled");
+       fprintf(f, "mode = %s\n",
+               trace_mode_to_string(rte_trace_mode_get()));
+       fprintf(f, "dir = %s\n", trace->dir);
+       fprintf(f, "buffer len = %d\n", trace->buff_len);
+       fprintf(f, "number of trace points = %d\n", trace->nb_trace_points);
+
+       trace_lcore_mem_dump(f);
+       fprintf(f, "\nTrace point info\n----------------\n");
+       STAILQ_FOREACH(tp, tp_list, next)
+               trace_point_dump(f, tp);
+}
+
+void
+__rte_trace_mem_per_thread_alloc(void)
+{
+       struct trace *trace = trace_obj_get();
+       struct __rte_trace_header *header;
+       uint32_t count;
+
+       if (!rte_trace_is_enabled())
+               return;
+
+       if (RTE_PER_LCORE(trace_mem))
+               return;
+
+       rte_spinlock_lock(&trace->lock);
+
+       count = trace->nb_trace_mem_list;
+
+       /* Allocate room for storing the thread trace mem meta */
+       trace->lcore_meta = realloc(trace->lcore_meta,
+               sizeof(trace->lcore_meta[0]) * (count + 1));
+
+       /* Provide dummy space for fast path to consume */
+       if (trace->lcore_meta == NULL) {
+               trace_crit("trace mem meta memory realloc failed");
+               header = NULL;
+               goto fail;
+       }
+
+       /* First attempt from huge page */
+       header = eal_malloc_no_trace(NULL, trace_mem_sz(trace->buff_len), 8);
+       if (header) {
+               trace->lcore_meta[count].area = TRACE_AREA_HUGEPAGE;
+               goto found;
+       }
+
+       /* Second attempt from heap */
+       header = malloc(trace_mem_sz(trace->buff_len));
+       if (header == NULL) {
+               trace_crit("trace mem malloc attempt failed");
+               header = NULL;
+               goto fail;
+
+       }
+
+       /* Second attempt from heap is success */
+       trace->lcore_meta[count].area = TRACE_AREA_HEAP;
+
+       /* Initialize the trace header */
+found:
+       header->offset = 0;
+       header->len = trace->buff_len;
+       header->stream_header.magic = TRACE_CTF_MAGIC;
+       rte_uuid_copy(header->stream_header.uuid, trace->uuid);
+       header->stream_header.lcore_id = rte_lcore_id();
+
+       /* Store the thread name */
+       char *name = header->stream_header.thread_name;
+       memset(name, 0, __RTE_TRACE_EMIT_STRING_LEN_MAX);
+       rte_thread_getname(pthread_self(), name,
+               __RTE_TRACE_EMIT_STRING_LEN_MAX);
+
+       trace->lcore_meta[count].mem = header;
+       trace->nb_trace_mem_list++;
+fail:
+       RTE_PER_LCORE(trace_mem) = header;
+       rte_spinlock_unlock(&trace->lock);
+}
+
+void
+trace_mem_per_thread_free(void)
+{
+       struct trace *trace = trace_obj_get();
+       uint32_t count;
+       void *mem;
+
+       if (!rte_trace_is_enabled())
+               return;
+
+       rte_spinlock_lock(&trace->lock);
+       for (count = 0; count < trace->nb_trace_mem_list; count++) {
+               mem = trace->lcore_meta[count].mem;
+               if (trace->lcore_meta[count].area == TRACE_AREA_HUGEPAGE)
+                       eal_free_no_trace(mem);
+               else if (trace->lcore_meta[count].area == TRACE_AREA_HEAP)
+                       free(mem);
+       }
+       rte_spinlock_unlock(&trace->lock);
+}
+
+void
+__rte_trace_point_emit_field(size_t sz, const char *in, const char *datatype)
+{
+       char *field = RTE_PER_LCORE(ctf_field);
+       int count = RTE_PER_LCORE(ctf_count);
+       size_t size;
+       int rc;
+
+       size = RTE_MAX(0, TRACE_CTF_FIELD_SIZE - 1 - count);
+       RTE_PER_LCORE(trace_point_sz) += sz;
+       rc = snprintf(RTE_PTR_ADD(field, count), size, "%s %s;", datatype, in);
+       if (rc <= 0 || (size_t)rc >= size) {
+               RTE_PER_LCORE(trace_point_sz) = 0;
+               trace_crit("CTF field is too long");
+               return;
+       }
+       RTE_PER_LCORE(ctf_count) += rc;
+}
+
 int
 __rte_trace_point_register(rte_trace_point_t *handle, const char *name,
                void (*register_fn)(void))