4 * Copyright(c) 2010-2014 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.
38 #include <sys/types.h>
43 #include <sys/queue.h>
46 #include <rte_memory.h>
47 #include <rte_memzone.h>
48 #include <rte_launch.h>
49 #include <rte_common.h>
50 #include <rte_cycles.h>
52 #include <rte_per_lcore.h>
53 #include <rte_lcore.h>
54 #include <rte_atomic.h>
55 #include <rte_debug.h>
56 #include <rte_spinlock.h>
57 #include <rte_branch_prediction.h>
59 #include <rte_mempool.h>
61 #include "eal_private.h"
63 #define LOG_ELT_SIZE 2048
65 #define LOG_HISTORY_MP_NAME "log_history"
67 STAILQ_HEAD(log_history_list, log_history);
70 * The structure of a message log in the log history.
73 STAILQ_ENTRY(log_history) next;
78 static struct rte_mempool *log_history_mp = NULL;
79 static unsigned log_history_size = 0;
80 static struct log_history_list log_history;
82 /* global log structure */
83 struct rte_logs rte_logs = {
85 .level = RTE_LOG_DEBUG,
89 static rte_spinlock_t log_dump_lock = RTE_SPINLOCK_INITIALIZER;
90 static rte_spinlock_t log_list_lock = RTE_SPINLOCK_INITIALIZER;
91 static FILE *default_log_stream;
92 static int history_enabled = 1;
95 * This global structure stores some informations about the message
96 * that is currently beeing processed by one lcore
99 uint32_t loglevel; /**< log level - see rte_log.h */
100 uint32_t logtype; /**< log type - see rte_log.h */
101 } __rte_cache_aligned;
102 static struct log_cur_msg log_cur_msg[RTE_MAX_LCORE]; /**< per core log */
108 rte_log_add_in_history(const char *buf, size_t size)
110 struct log_history *hist_buf = NULL;
111 static const unsigned hist_buf_size = LOG_ELT_SIZE - sizeof(*hist_buf);
114 if (history_enabled == 0)
117 rte_spinlock_lock(&log_list_lock);
119 /* get a buffer for adding in history */
120 if (log_history_size > RTE_LOG_HISTORY) {
121 hist_buf = STAILQ_FIRST(&log_history);
123 STAILQ_REMOVE_HEAD(&log_history, next);
128 if (rte_mempool_mc_get(log_history_mp, &obj) < 0)
134 if (hist_buf == NULL) {
135 rte_spinlock_unlock(&log_list_lock);
139 /* not enough room for msg, buffer go back in mempool */
140 if (size >= hist_buf_size) {
141 rte_mempool_mp_put(log_history_mp, hist_buf);
142 rte_spinlock_unlock(&log_list_lock);
147 memcpy(hist_buf->buf, buf, size);
148 hist_buf->buf[size] = hist_buf->buf[hist_buf_size-1] = '\0';
149 hist_buf->size = size;
150 STAILQ_INSERT_TAIL(&log_history, hist_buf, next);
152 rte_spinlock_unlock(&log_list_lock);
158 rte_log_set_history(int enable)
160 history_enabled = enable;
163 /* Change the stream that will be used by logging system */
165 rte_openlog_stream(FILE *f)
168 rte_logs.file = default_log_stream;
174 /* Set global log level */
176 rte_set_log_level(uint32_t level)
178 rte_logs.level = (uint32_t)level;
181 /* Get global log level */
183 rte_get_log_level(void)
185 return rte_logs.level;
188 /* Set global log type */
190 rte_set_log_type(uint32_t type, int enable)
193 rte_logs.type |= type;
195 rte_logs.type &= (~type);
198 /* Get global log type */
200 rte_get_log_type(void)
202 return rte_logs.type;
205 /* get the current loglevel for the message beeing processed */
206 int rte_log_cur_msg_loglevel(void)
209 lcore_id = rte_lcore_id();
210 if (lcore_id >= RTE_MAX_LCORE)
211 return rte_get_log_level();
212 return log_cur_msg[lcore_id].loglevel;
215 /* get the current logtype for the message beeing processed */
216 int rte_log_cur_msg_logtype(void)
219 lcore_id = rte_lcore_id();
220 if (lcore_id >= RTE_MAX_LCORE)
221 return rte_get_log_type();
222 return log_cur_msg[lcore_id].logtype;
225 /* Dump log history to file */
227 rte_log_dump_history(FILE *out)
229 struct log_history_list tmp_log_history;
230 struct log_history *hist_buf;
233 /* only one dump at a time */
234 rte_spinlock_lock(&log_dump_lock);
236 /* save list, and re-init to allow logging during dump */
237 rte_spinlock_lock(&log_list_lock);
238 tmp_log_history = log_history;
239 STAILQ_INIT(&log_history);
240 log_history_size = 0;
241 rte_spinlock_unlock(&log_list_lock);
243 for (i=0; i<RTE_LOG_HISTORY; i++) {
245 /* remove one message from history list */
246 hist_buf = STAILQ_FIRST(&tmp_log_history);
248 if (hist_buf == NULL)
251 STAILQ_REMOVE_HEAD(&tmp_log_history, next);
253 /* write on stdout */
254 if (fwrite(hist_buf->buf, hist_buf->size, 1, out) == 0) {
255 rte_mempool_mp_put(log_history_mp, hist_buf);
259 /* put back message structure in pool */
260 rte_mempool_mp_put(log_history_mp, hist_buf);
264 rte_spinlock_unlock(&log_dump_lock);
268 * Generates a log message The message will be sent in the stream
269 * defined by the previous call to rte_openlog_stream().
272 rte_vlog(uint32_t level, uint32_t logtype, const char *format, va_list ap)
275 FILE *f = rte_logs.file;
278 if ((level > rte_logs.level) || !(logtype & rte_logs.type))
281 /* save loglevel and logtype in a global per-lcore variable */
282 lcore_id = rte_lcore_id();
283 if (lcore_id < RTE_MAX_LCORE) {
284 log_cur_msg[lcore_id].loglevel = level;
285 log_cur_msg[lcore_id].logtype = logtype;
288 ret = vfprintf(f, format, ap);
294 * Generates a log message The message will be sent in the stream
295 * defined by the previous call to rte_openlog_stream().
296 * No need to check level here, done by rte_vlog().
299 rte_log(uint32_t level, uint32_t logtype, const char *format, ...)
304 va_start(ap, format);
305 ret = rte_vlog(level, logtype, format, ap);
311 * called by environment-specific log init function to initialize log
315 rte_eal_common_log_init(FILE *default_log)
317 STAILQ_INIT(&log_history);
319 /* reserve RTE_LOG_HISTORY*2 elements, so we can dump and
320 * keep logging during this time */
321 log_history_mp = rte_mempool_create(LOG_HISTORY_MP_NAME, RTE_LOG_HISTORY*2,
327 if ((log_history_mp == NULL) &&
328 ((log_history_mp = rte_mempool_lookup(LOG_HISTORY_MP_NAME)) == NULL)){
329 RTE_LOG(ERR, EAL, "%s(): cannot create log_history mempool\n",
334 default_log_stream = default_log;
335 rte_openlog_stream(default_log);