4 * Copyright(c) 2010-2012 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * * Neither the name of Intel Corporation nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 #include <sys/types.h>
44 #include <sys/queue.h>
47 #include <rte_memory.h>
48 #include <rte_memzone.h>
49 #include <rte_launch.h>
50 #include <rte_common.h>
51 #include <rte_cycles.h>
52 #include <rte_tailq.h>
54 #include <rte_per_lcore.h>
55 #include <rte_lcore.h>
56 #include <rte_atomic.h>
57 #include <rte_debug.h>
58 #include <rte_spinlock.h>
59 #include <rte_branch_prediction.h>
61 #include <rte_mempool.h>
63 #include "eal_private.h"
65 #define LOG_ELT_SIZE 2048
66 #define LOG_BUF_SIZE (LOG_ELT_SIZE - sizeof(struct log_history))
68 #define LOG_HISTORY_MP_NAME "log_history"
70 STAILQ_HEAD(log_history_list, log_history);
73 * The structure of a message log in the log history.
76 STAILQ_ENTRY(log_history) next;
81 static struct rte_mempool *log_history_mp = NULL;
82 static unsigned log_history_size = 0;
83 static struct log_history_list log_history;
85 /* global log structure */
86 struct rte_logs rte_logs = {
88 .level = RTE_LOG_DEBUG,
92 static rte_spinlock_t log_dump_lock = RTE_SPINLOCK_INITIALIZER;
93 static rte_spinlock_t log_list_lock = RTE_SPINLOCK_INITIALIZER;
94 static FILE *default_log_stream;
95 static int history_enabled = 1;
98 * This global structure stores some informations about the message
99 * that is currently beeing processed by one lcore
102 uint32_t loglevel; /**< log level - see rte_log.h */
103 uint32_t logtype; /**< log type - see rte_log.h */
104 } __rte_cache_aligned;
105 static struct log_cur_msg log_cur_msg[RTE_MAX_LCORE]; /**< per core log */
110 * early log function, used during boot when mempool (hence log
111 * history) is not available
114 early_log_write(__attribute__((unused)) void *c, const char *buf, size_t size)
117 ret = fwrite(buf, size, 1, stdout);
125 early_log_read(__attribute__((unused)) void *c,
126 __attribute__((unused)) char *buf,
127 __attribute__((unused)) size_t size)
133 * this is needed because cookies_io_functions_t has a different
134 * prototype between newlib and glibc
136 #ifdef RTE_EXEC_ENV_LINUXAPP
138 early_log_seek(__attribute__((unused)) void *c,
139 __attribute__((unused)) off64_t *offset,
140 __attribute__((unused)) int whence)
146 early_log_seek(__attribute__((unused)) void *c,
147 __attribute__((unused)) _off_t *offset,
148 __attribute__((unused)) int whence)
155 early_log_close(__attribute__((unused)) void *c)
160 static cookie_io_functions_t early_log_func = {
161 .read = early_log_read,
162 .write = early_log_write,
163 .seek = early_log_seek,
164 .close = early_log_close
166 static FILE *early_log_stream;
171 rte_log_add_in_history(const char *buf, size_t size)
173 struct log_history *hist_buf = NULL;
176 if (history_enabled == 0)
179 rte_spinlock_lock(&log_list_lock);
181 /* get a buffer for adding in history */
182 if (log_history_size > RTE_LOG_HISTORY) {
183 hist_buf = STAILQ_FIRST(&log_history);
184 STAILQ_REMOVE_HEAD(&log_history, next);
187 if (rte_mempool_mc_get(log_history_mp, &obj) < 0)
193 if (hist_buf == NULL) {
194 rte_spinlock_unlock(&log_list_lock);
198 /* not enough room for msg, buffer go back in mempool */
199 if (size >= LOG_BUF_SIZE - 1) {
200 rte_mempool_mp_put(log_history_mp, hist_buf);
201 rte_spinlock_unlock(&log_list_lock);
206 memcpy(hist_buf->buf, buf, size);
207 hist_buf->buf[size] = '\0';
208 hist_buf->size = size;
209 STAILQ_INSERT_TAIL(&log_history, hist_buf, next);
210 rte_spinlock_unlock(&log_list_lock);
216 rte_log_set_history(int enable)
218 history_enabled = enable;
221 /* Change the stream that will be used by logging system */
223 rte_openlog_stream(FILE *f)
226 rte_logs.file = default_log_stream;
232 /* Set global log level */
234 rte_set_log_level(uint32_t level)
236 rte_logs.level = (uint32_t)level;
239 /* Set global log type */
241 rte_set_log_type(uint32_t type, int enable)
244 rte_logs.type |= type;
246 rte_logs.type &= (~type);
249 /* get the current loglevel for the message beeing processed */
250 int rte_log_cur_msg_loglevel(void)
253 lcore_id = rte_lcore_id();
254 return log_cur_msg[lcore_id].loglevel;
257 /* get the current logtype for the message beeing processed */
258 int rte_log_cur_msg_logtype(void)
261 lcore_id = rte_lcore_id();
262 return log_cur_msg[lcore_id].logtype;
265 /* Dump log history on console */
267 rte_log_dump_history(void)
269 struct log_history_list tmp_log_history;
270 struct log_history *hist_buf;
273 /* only one dump at a time */
274 rte_spinlock_lock(&log_dump_lock);
276 /* save list, and re-init to allow logging during dump */
277 rte_spinlock_lock(&log_list_lock);
278 tmp_log_history = log_history;
279 STAILQ_INIT(&log_history);
280 rte_spinlock_unlock(&log_list_lock);
282 for (i=0; i<RTE_LOG_HISTORY; i++) {
284 /* remove one message from history list */
285 hist_buf = STAILQ_FIRST(&tmp_log_history);
287 if (hist_buf == NULL)
290 STAILQ_REMOVE_HEAD(&tmp_log_history, next);
292 /* write on stdout */
293 if (fwrite(hist_buf->buf, hist_buf->size, 1, stdout) == 0) {
294 rte_mempool_mp_put(log_history_mp, hist_buf);
298 /* put back message structure in pool */
299 rte_mempool_mp_put(log_history_mp, hist_buf);
303 rte_spinlock_unlock(&log_dump_lock);
307 * Generates a log message The message will be sent in the stream
308 * defined by the previous call to rte_openlog_stream().
311 rte_vlog(__attribute__((unused)) uint32_t level,
312 __attribute__((unused)) uint32_t logtype,
313 const char *format, va_list ap)
316 FILE *f = rte_logs.file;
319 /* save loglevel and logtype in a global per-lcore variable */
320 lcore_id = rte_lcore_id();
321 log_cur_msg[lcore_id].loglevel = level;
322 log_cur_msg[lcore_id].logtype = logtype;
324 ret = vfprintf(f, format, ap);
330 * Generates a log message The message will be sent in the stream
331 * defined by the previous call to rte_openlog_stream().
334 rte_log(uint32_t level, uint32_t logtype, const char *format, ...)
339 va_start(ap, format);
340 ret = rte_vlog(level, logtype, format, ap);
346 * init the log library, called by rte_eal_init() to enable early
350 rte_eal_log_early_init(void)
352 early_log_stream = fopencookie(NULL, "w+", early_log_func);
353 if (early_log_stream == NULL) {
354 printf("Cannot configure early_log_stream\n");
357 rte_openlog_stream(early_log_stream);
362 * called by environment-specific log init function to initialize log
366 rte_eal_common_log_init(FILE *default_log)
368 STAILQ_INIT(&log_history);
370 /* reserve RTE_LOG_HISTORY*2 elements, so we can dump and
371 * keep logging during this time */
372 if (rte_eal_process_type() == RTE_PROC_PRIMARY)
373 log_history_mp = rte_mempool_create(LOG_HISTORY_MP_NAME, RTE_LOG_HISTORY*2,
379 log_history_mp = rte_mempool_lookup(LOG_HISTORY_MP_NAME);
380 if (log_history_mp == NULL) {
381 RTE_LOG(ERR, EAL, "%s(): cannot create log_history mempool\n",
386 default_log_stream = default_log;
387 rte_openlog_stream(default_log);