eal: support dynamic log types
[dpdk.git] / lib / librte_eal / common / eal_common_log.c
index 454a785..68d879a 100644 (file)
@@ -1,13 +1,13 @@
 /*-
  *   BSD LICENSE
- * 
+ *
  *   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
  *   All rights reserved.
- * 
+ *
  *   Redistribution and use in source and binary forms, with or without
  *   modification, are permitted provided that the following conditions
  *   are met:
- * 
+ *
  *     * Redistributions of source code must retain the above copyright
  *       notice, this list of conditions and the following disclaimer.
  *     * Redistributions in binary form must reproduce the above copyright
@@ -17,7 +17,7 @@
  *     * Neither the name of Intel Corporation nor the names of its
  *       contributors may be used to endorse or promote products derived
  *       from this software without specific prior written permission.
- * 
+ *
  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
-#include <string.h>
 #include <stdio.h>
 #include <stdint.h>
 #include <stdarg.h>
-#include <sys/types.h>
 #include <stdlib.h>
-#include <unistd.h>
-#include <inttypes.h>
+#include <string.h>
 #include <errno.h>
-#include <sys/queue.h>
 
-#include <rte_log.h>
-#include <rte_memory.h>
-#include <rte_memzone.h>
-#include <rte_launch.h>
-#include <rte_common.h>
-#include <rte_cycles.h>
-#include <rte_tailq.h>
 #include <rte_eal.h>
+#include <rte_log.h>
 #include <rte_per_lcore.h>
-#include <rte_lcore.h>
-#include <rte_atomic.h>
-#include <rte_debug.h>
-#include <rte_spinlock.h>
-#include <rte_branch_prediction.h>
-#include <rte_ring.h>
-#include <rte_mempool.h>
 
 #include "eal_private.h"
 
-#define LOG_ELT_SIZE     2048
-
-#define LOG_HISTORY_MP_NAME "log_history"
-
-STAILQ_HEAD(log_history_list, log_history);
-
-/**
- * The structure of a message log in the log history.
- */
-struct log_history {
-       STAILQ_ENTRY(log_history) next;
-       unsigned size;
-       char buf[0];
-};
-
-static struct rte_mempool *log_history_mp = NULL;
-static unsigned log_history_size = 0;
-static struct log_history_list log_history;
-
 /* global log structure */
 struct rte_logs rte_logs = {
        .type = ~0,
@@ -87,85 +51,33 @@ struct rte_logs rte_logs = {
        .file = NULL,
 };
 
-static rte_spinlock_t log_dump_lock = RTE_SPINLOCK_INITIALIZER;
-static rte_spinlock_t log_list_lock = RTE_SPINLOCK_INITIALIZER;
+/* Stream to use for logging if rte_logs.file is NULL */
 static FILE *default_log_stream;
-static int history_enabled = 1;
 
 /**
  * This global structure stores some informations about the message
- * that is currently beeing processed by one lcore
+ * that is currently being processed by one lcore
  */
 struct log_cur_msg {
        uint32_t loglevel; /**< log level - see rte_log.h */
        uint32_t logtype;  /**< log type  - see rte_log.h */
-} __rte_cache_aligned;
-static struct log_cur_msg log_cur_msg[RTE_MAX_LCORE]; /**< per core log */
-
-
-/* default logs */
-
-int
-rte_log_add_in_history(const char *buf, size_t size)
-{
-       struct log_history *hist_buf = NULL;
-       static const unsigned hist_buf_size = LOG_ELT_SIZE - sizeof(*hist_buf);
-       void *obj;
-
-       if (history_enabled == 0)
-               return 0;
-
-       rte_spinlock_lock(&log_list_lock);
-
-       /* get a buffer for adding in history */
-       if (log_history_size > RTE_LOG_HISTORY) {
-               hist_buf = STAILQ_FIRST(&log_history);
-               STAILQ_REMOVE_HEAD(&log_history, next);
-       }
-       else {
-               if (rte_mempool_mc_get(log_history_mp, &obj) < 0)
-                       obj = NULL;
-               hist_buf = obj;
-       }
-
-       /* no buffer */
-       if (hist_buf == NULL) {
-               rte_spinlock_unlock(&log_list_lock);
-               return -ENOBUFS;
-       }
-
-       /* not enough room for msg, buffer go back in mempool */
-       if (size >= hist_buf_size) {
-               rte_mempool_mp_put(log_history_mp, hist_buf);
-               rte_spinlock_unlock(&log_list_lock);
-               return -ENOBUFS;
-       }
+};
 
-       /* add in history */
-       memcpy(hist_buf->buf, buf, size);
-       hist_buf->buf[size] = hist_buf->buf[hist_buf_size-1] = '\0';
-       hist_buf->size = size;
-       STAILQ_INSERT_TAIL(&log_history, hist_buf, next);
-       log_history_size++;
-       rte_spinlock_unlock(&log_list_lock);
+struct rte_log_dynamic_type {
+       const char *name;
+       uint32_t loglevel;
+};
 
-       return 0;
-}
+ /* per core log */
+static RTE_DEFINE_PER_LCORE(struct log_cur_msg, log_cur_msg);
 
-void
-rte_log_set_history(int enable)
-{
-       history_enabled = enable;
-}
+/* default logs */
 
 /* Change the stream that will be used by logging system */
 int
 rte_openlog_stream(FILE *f)
 {
-       if (f == NULL)
-               rte_logs.file = default_log_stream;
-       else
-               rte_logs.file = f;
+       rte_logs.file = f;
        return 0;
 }
 
@@ -176,71 +88,159 @@ rte_set_log_level(uint32_t level)
        rte_logs.level = (uint32_t)level;
 }
 
+/* Get global log level */
+uint32_t
+rte_get_log_level(void)
+{
+       return rte_logs.level;
+}
+
 /* Set global log type */
 void
 rte_set_log_type(uint32_t type, int enable)
 {
+       if (type < RTE_LOGTYPE_FIRST_EXT_ID) {
+               if (enable)
+                       rte_logs.type |= type;
+               else
+                       rte_logs.type &= ~type;
+       }
+
        if (enable)
-               rte_logs.type |= type;
+               rte_log_set_level(type, 0);
        else
-               rte_logs.type &= (~type);
+               rte_log_set_level(type, RTE_LOG_DEBUG);
+}
+
+/* Get global log type */
+uint32_t
+rte_get_log_type(void)
+{
+       return rte_logs.type;
+}
+
+int
+rte_log_set_level(uint32_t type, uint32_t level)
+{
+       if (type >= rte_logs.dynamic_types_len)
+               return -1;
+       if (level > RTE_LOG_DEBUG)
+               return -1;
+
+       rte_logs.dynamic_types[type].loglevel = level;
+
+       return 0;
 }
 
 /* get the current loglevel for the message beeing processed */
 int rte_log_cur_msg_loglevel(void)
 {
-       unsigned lcore_id;
-       lcore_id = rte_lcore_id();
-       return log_cur_msg[lcore_id].loglevel;
+       return RTE_PER_LCORE(log_cur_msg).loglevel;
 }
 
 /* get the current logtype for the message beeing processed */
 int rte_log_cur_msg_logtype(void)
 {
-       unsigned lcore_id;
-       lcore_id = rte_lcore_id();
-       return log_cur_msg[lcore_id].logtype;
+       return RTE_PER_LCORE(log_cur_msg).logtype;
 }
 
-/* Dump log history on console */
-void
-rte_log_dump_history(void)
+static int
+rte_log_lookup(const char *name)
 {
-       struct log_history_list tmp_log_history;
-       struct log_history *hist_buf;
-       unsigned i;
+       size_t i;
 
-       /* only one dump at a time */
-       rte_spinlock_lock(&log_dump_lock);
+       for (i = 0; i < rte_logs.dynamic_types_len; i++) {
+               if (rte_logs.dynamic_types[i].name == NULL)
+                       continue;
+               if (strcmp(name, rte_logs.dynamic_types[i].name) == 0)
+                       return i;
+       }
 
-       /* save list, and re-init to allow logging during dump */
-       rte_spinlock_lock(&log_list_lock);
-       tmp_log_history = log_history;
-       STAILQ_INIT(&log_history);
-       rte_spinlock_unlock(&log_list_lock);
+       return -1;
+}
 
-       for (i=0; i<RTE_LOG_HISTORY; i++) {
+/* register an extended log type, assuming table is large enough, and id
+ * is not yet registered.
+ */
+static int
+__rte_log_register(const char *name, int id)
+{
+       char *dup_name = strdup(name);
 
-               /* remove one message from history list */
-               hist_buf = STAILQ_FIRST(&tmp_log_history);
+       if (dup_name == NULL)
+               return -ENOMEM;
 
-               if (hist_buf == NULL)
-                       break;
+       rte_logs.dynamic_types[id].name = dup_name;
+       rte_logs.dynamic_types[id].loglevel = RTE_LOG_DEBUG;
 
-               STAILQ_REMOVE_HEAD(&tmp_log_history, next);
+       return id;
+}
 
-               /* write on stdout */
-               if (fwrite(hist_buf->buf, hist_buf->size, 1, stdout) == 0) {
-                       rte_mempool_mp_put(log_history_mp, hist_buf);
-                       break;
-               }
+/* register an extended log type */
+int
+rte_log_register(const char *name)
+{
+       struct rte_log_dynamic_type *new_dynamic_types;
+       int id, ret;
 
-               /* put back message structure in pool */
-               rte_mempool_mp_put(log_history_mp, hist_buf);
-       }
-       fflush(stdout);
+       id = rte_log_lookup(name);
+       if (id >= 0)
+               return id;
 
-       rte_spinlock_unlock(&log_dump_lock);
+       new_dynamic_types = realloc(rte_logs.dynamic_types,
+               sizeof(struct rte_log_dynamic_type) *
+               (rte_logs.dynamic_types_len + 1));
+       if (new_dynamic_types == NULL)
+               return -ENOMEM;
+       rte_logs.dynamic_types = new_dynamic_types;
+
+       ret = __rte_log_register(name, rte_logs.dynamic_types_len);
+       if (ret < 0)
+               return ret;
+
+       rte_logs.dynamic_types_len++;
+
+       return ret;
+}
+
+RTE_INIT(rte_log_init);
+static void
+rte_log_init(void)
+{
+       rte_logs.dynamic_types = calloc(RTE_LOGTYPE_FIRST_EXT_ID,
+               sizeof(struct rte_log_dynamic_type));
+       if (rte_logs.dynamic_types == NULL)
+               return;
+
+       /* register legacy log types, keep sync'd with RTE_LOGTYPE_* */
+       __rte_log_register("eal", 0);
+       __rte_log_register("malloc", 1);
+       __rte_log_register("ring", 2);
+       __rte_log_register("mempool", 3);
+       __rte_log_register("timer", 4);
+       __rte_log_register("pmd", 5);
+       __rte_log_register("hash", 6);
+       __rte_log_register("lpm", 7);
+       __rte_log_register("kni", 8);
+       __rte_log_register("acl", 9);
+       __rte_log_register("power", 10);
+       __rte_log_register("meter", 11);
+       __rte_log_register("sched", 12);
+       __rte_log_register("port", 13);
+       __rte_log_register("table", 14);
+       __rte_log_register("pipeline", 15);
+       __rte_log_register("mbuf", 16);
+       __rte_log_register("cryptodev", 17);
+       __rte_log_register("user1", 24);
+       __rte_log_register("user2", 25);
+       __rte_log_register("user3", 26);
+       __rte_log_register("user4", 27);
+       __rte_log_register("user5", 28);
+       __rte_log_register("user6", 29);
+       __rte_log_register("user7", 30);
+       __rte_log_register("user8", 31);
+
+       rte_logs.dynamic_types_len = RTE_LOGTYPE_FIRST_EXT_ID;
 }
 
 /*
@@ -248,18 +248,34 @@ rte_log_dump_history(void)
  * defined by the previous call to rte_openlog_stream().
  */
 int
-rte_vlog(__attribute__((unused)) uint32_t level,
-        __attribute__((unused)) uint32_t logtype,
-          const char *format, va_list ap)
+rte_vlog(uint32_t level, uint32_t logtype, const char *format, va_list ap)
 {
        int ret;
        FILE *f = rte_logs.file;
-       unsigned lcore_id;
+       if (f == NULL) {
+               f = default_log_stream;
+               if (f == NULL) {
+                       /*
+                        * Grab the current value of stderr here, rather than
+                        * just initializing default_log_stream to stderr. This
+                        * ensures that we will always use the current value
+                        * of stderr, even if the application closes and
+                        * reopens it.
+                        */
+                       f = stderr;
+               }
+       }
+
+       if (level > rte_logs.level)
+               return 0;
+       if (logtype >= rte_logs.dynamic_types_len)
+               return -1;
+       if (level > rte_logs.dynamic_types[logtype].loglevel)
+               return 0;
 
        /* save loglevel and logtype in a global per-lcore variable */
-       lcore_id = rte_lcore_id();
-       log_cur_msg[lcore_id].loglevel = level;
-       log_cur_msg[lcore_id].logtype = logtype;
+       RTE_PER_LCORE(log_cur_msg).loglevel = level;
+       RTE_PER_LCORE(log_cur_msg).logtype = logtype;
 
        ret = vfprintf(f, format, ap);
        fflush(f);
@@ -269,6 +285,7 @@ rte_vlog(__attribute__((unused)) uint32_t level,
 /*
  * Generates a log message The message will be sent in the stream
  * defined by the previous call to rte_openlog_stream().
+ * No need to check level here, done by rte_vlog().
  */
 int
 rte_log(uint32_t level, uint32_t logtype, const char *format, ...)
@@ -283,31 +300,15 @@ rte_log(uint32_t level, uint32_t logtype, const char *format, ...)
 }
 
 /*
- * called by environment-specific log init function to initialize log
- * history
+ * Called by environment-specific initialization functions.
  */
-int
-rte_eal_common_log_init(FILE *default_log)
+void
+eal_log_set_default(FILE *default_log)
 {
-       STAILQ_INIT(&log_history);
-
-       /* reserve RTE_LOG_HISTORY*2 elements, so we can dump and
-        * keep logging during this time */
-       log_history_mp = rte_mempool_create(LOG_HISTORY_MP_NAME, RTE_LOG_HISTORY*2,
-                               LOG_ELT_SIZE, 0, 0,
-                               NULL, NULL,
-                               NULL, NULL,
-                               SOCKET_ID_ANY, 0);
-       
-       if ((log_history_mp == NULL) && 
-           ((log_history_mp = rte_mempool_lookup(LOG_HISTORY_MP_NAME)) == NULL)){
-               RTE_LOG(ERR, EAL, "%s(): cannot create log_history mempool\n",
-                       __func__);
-               return -1;
-       }
-
        default_log_stream = default_log;
-       rte_openlog_stream(default_log);
-       return 0;
-}
 
+#if RTE_LOG_DP_LEVEL >= RTE_LOG_DEBUG
+       RTE_LOG(NOTICE, EAL,
+               "Debug dataplane logs available - lower performance\n");
+#endif
+}