remove version in all files
[dpdk.git] / lib / librte_eal / common / eal_common_log.c
1 /*-
2  *   BSD LICENSE
3  * 
4  *   Copyright(c) 2010-2012 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  * 
7  *   Redistribution and use in source and binary forms, with or without 
8  *   modification, are permitted provided that the following conditions 
9  *   are met:
10  * 
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 
16  *       distribution.
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.
20  * 
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.
32  * 
33  */
34
35 #include <string.h>
36 #include <stdio.h>
37 #include <stdint.h>
38 #include <stdarg.h>
39 #include <sys/types.h>
40 #include <stdlib.h>
41 #include <unistd.h>
42 #include <inttypes.h>
43 #include <errno.h>
44 #include <sys/queue.h>
45
46 #include <rte_log.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>
53 #include <rte_eal.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>
60 #include <rte_ring.h>
61 #include <rte_mempool.h>
62
63 #include "eal_private.h"
64
65 #define LOG_ELT_SIZE     2048
66
67 #define LOG_HISTORY_MP_NAME "log_history"
68
69 STAILQ_HEAD(log_history_list, log_history);
70
71 /**
72  * The structure of a message log in the log history.
73  */
74 struct log_history {
75         STAILQ_ENTRY(log_history) next;
76         unsigned size;
77         char buf[0];
78 };
79
80 static struct rte_mempool *log_history_mp = NULL;
81 static unsigned log_history_size = 0;
82 static struct log_history_list log_history;
83
84 /* global log structure */
85 struct rte_logs rte_logs = {
86         .type = ~0,
87         .level = RTE_LOG_DEBUG,
88         .file = NULL,
89 };
90
91 static rte_spinlock_t log_dump_lock = RTE_SPINLOCK_INITIALIZER;
92 static rte_spinlock_t log_list_lock = RTE_SPINLOCK_INITIALIZER;
93 static FILE *default_log_stream;
94 static int history_enabled = 1;
95
96 /**
97  * This global structure stores some informations about the message
98  * that is currently beeing processed by one lcore
99  */
100 struct log_cur_msg {
101         uint32_t loglevel; /**< log level - see rte_log.h */
102         uint32_t logtype;  /**< log type  - see rte_log.h */
103 } __rte_cache_aligned;
104 static struct log_cur_msg log_cur_msg[RTE_MAX_LCORE]; /**< per core log */
105
106 /* early logs */
107
108 /*
109  * early log function, used during boot when mempool (hence log
110  * history) is not available
111  */
112 static ssize_t
113 early_log_write(__attribute__((unused)) void *c, const char *buf, size_t size)
114 {
115         ssize_t ret;
116         ret = fwrite(buf, size, 1, stdout);
117         fflush(stdout);
118         if (ret == 0)
119                 return -1;
120         return ret;
121 }
122
123 static ssize_t
124 early_log_read(__attribute__((unused)) void *c,
125                __attribute__((unused)) char *buf,
126                __attribute__((unused)) size_t size)
127 {
128         return 0;
129 }
130
131 /*
132  * this is needed because cookies_io_functions_t has a different
133  * prototype between newlib and glibc
134  */
135 #ifdef RTE_EXEC_ENV_LINUXAPP
136 static int
137 early_log_seek(__attribute__((unused)) void *c,
138                __attribute__((unused)) off64_t *offset,
139                __attribute__((unused)) int whence)
140 {
141         return -1;
142 }
143 #else
144 static int
145 early_log_seek(__attribute__((unused)) void *c,
146                __attribute__((unused)) _off_t *offset,
147                __attribute__((unused)) int whence)
148 {
149         return -1;
150 }
151 #endif
152
153 static int
154 early_log_close(__attribute__((unused)) void *c)
155 {
156         return 0;
157 }
158
159 static cookie_io_functions_t early_log_func = {
160         .read  = early_log_read,
161         .write = early_log_write,
162         .seek  = early_log_seek,
163         .close = early_log_close
164 };
165 static FILE *early_log_stream;
166
167 /* default logs */
168
169 int
170 rte_log_add_in_history(const char *buf, size_t size)
171 {
172         struct log_history *hist_buf = NULL;
173         void *obj;
174
175         if (history_enabled == 0)
176                 return 0;
177
178         rte_spinlock_lock(&log_list_lock);
179
180         /* get a buffer for adding in history */
181         if (log_history_size > RTE_LOG_HISTORY) {
182                 hist_buf = STAILQ_FIRST(&log_history);
183                 STAILQ_REMOVE_HEAD(&log_history, next);
184         }
185         else {
186                 if (rte_mempool_mc_get(log_history_mp, &obj) < 0)
187                         obj = NULL;
188                 hist_buf = obj;
189         }
190
191         /* no buffer */
192         if (hist_buf == NULL) {
193                 rte_spinlock_unlock(&log_list_lock);
194                 return -ENOBUFS;
195         }
196
197         /* not enough room for msg, buffer go back in mempool */
198         if (size >= (LOG_ELT_SIZE - sizeof(*hist_buf))) {
199                 rte_mempool_mp_put(log_history_mp, hist_buf);
200                 rte_spinlock_unlock(&log_list_lock);
201                 return -ENOBUFS;
202         }
203
204         /* add in history */
205         memcpy(hist_buf->buf, buf, size);
206         hist_buf->buf[LOG_ELT_SIZE-1] = '\0';
207         hist_buf->size = size;
208         STAILQ_INSERT_TAIL(&log_history, hist_buf, next);
209         rte_spinlock_unlock(&log_list_lock);
210
211         return 0;
212 }
213
214 void
215 rte_log_set_history(int enable)
216 {
217         history_enabled = enable;
218 }
219
220 /* Change the stream that will be used by logging system */
221 int
222 rte_openlog_stream(FILE *f)
223 {
224         if (f == NULL)
225                 rte_logs.file = default_log_stream;
226         else
227                 rte_logs.file = f;
228         return 0;
229 }
230
231 /* Set global log level */
232 void
233 rte_set_log_level(uint32_t level)
234 {
235         rte_logs.level = (uint32_t)level;
236 }
237
238 /* Set global log type */
239 void
240 rte_set_log_type(uint32_t type, int enable)
241 {
242         if (enable)
243                 rte_logs.type |= type;
244         else
245                 rte_logs.type &= (~type);
246 }
247
248 /* get the current loglevel for the message beeing processed */
249 int rte_log_cur_msg_loglevel(void)
250 {
251         unsigned lcore_id;
252         lcore_id = rte_lcore_id();
253         return log_cur_msg[lcore_id].loglevel;
254 }
255
256 /* get the current logtype for the message beeing processed */
257 int rte_log_cur_msg_logtype(void)
258 {
259         unsigned lcore_id;
260         lcore_id = rte_lcore_id();
261         return log_cur_msg[lcore_id].logtype;
262 }
263
264 /* Dump log history on console */
265 void
266 rte_log_dump_history(void)
267 {
268         struct log_history_list tmp_log_history;
269         struct log_history *hist_buf;
270         unsigned i;
271
272         /* only one dump at a time */
273         rte_spinlock_lock(&log_dump_lock);
274
275         /* save list, and re-init to allow logging during dump */
276         rte_spinlock_lock(&log_list_lock);
277         tmp_log_history = log_history;
278         STAILQ_INIT(&log_history);
279         rte_spinlock_unlock(&log_list_lock);
280
281         for (i=0; i<RTE_LOG_HISTORY; i++) {
282
283                 /* remove one message from history list */
284                 hist_buf = STAILQ_FIRST(&tmp_log_history);
285
286                 if (hist_buf == NULL)
287                         break;
288
289                 STAILQ_REMOVE_HEAD(&tmp_log_history, next);
290
291                 /* write on stdout */
292                 if (fwrite(hist_buf->buf, hist_buf->size, 1, stdout) == 0) {
293                         rte_mempool_mp_put(log_history_mp, hist_buf);
294                         break;
295                 }
296
297                 /* put back message structure in pool */
298                 rte_mempool_mp_put(log_history_mp, hist_buf);
299         }
300         fflush(stdout);
301
302         rte_spinlock_unlock(&log_dump_lock);
303 }
304
305 /*
306  * Generates a log message The message will be sent in the stream
307  * defined by the previous call to rte_openlog_stream().
308  */
309 int
310 rte_vlog(__attribute__((unused)) uint32_t level,
311          __attribute__((unused)) uint32_t logtype,
312            const char *format, va_list ap)
313 {
314         int ret;
315         FILE *f = rte_logs.file;
316         unsigned lcore_id;
317
318         /* save loglevel and logtype in a global per-lcore variable */
319         lcore_id = rte_lcore_id();
320         log_cur_msg[lcore_id].loglevel = level;
321         log_cur_msg[lcore_id].logtype = logtype;
322
323         ret = vfprintf(f, format, ap);
324         fflush(f);
325         return ret;
326 }
327
328 /*
329  * Generates a log message The message will be sent in the stream
330  * defined by the previous call to rte_openlog_stream().
331  */
332 int
333 rte_log(uint32_t level, uint32_t logtype, const char *format, ...)
334 {
335         va_list ap;
336         int ret;
337
338         va_start(ap, format);
339         ret = rte_vlog(level, logtype, format, ap);
340         va_end(ap);
341         return ret;
342 }
343
344 /*
345  * init the log library, called by rte_eal_init() to enable early
346  * logs
347  */
348 int
349 rte_eal_log_early_init(void)
350 {
351         early_log_stream = fopencookie(NULL, "w+", early_log_func);
352         if (early_log_stream == NULL) {
353                 printf("Cannot configure early_log_stream\n");
354                 return -1;
355         }
356         rte_openlog_stream(early_log_stream);
357         return 0;
358 }
359
360 /*
361  * called by environment-specific log init function to initialize log
362  * history
363  */
364 int
365 rte_eal_common_log_init(FILE *default_log)
366 {
367         STAILQ_INIT(&log_history);
368
369         /* reserve RTE_LOG_HISTORY*2 elements, so we can dump and
370          * keep logging during this time */
371         if (rte_eal_process_type() == RTE_PROC_PRIMARY)
372                 log_history_mp = rte_mempool_create(LOG_HISTORY_MP_NAME, RTE_LOG_HISTORY*2,
373                                 LOG_ELT_SIZE, 0, 0,
374                                 NULL, NULL,
375                                 NULL, NULL,
376                                 SOCKET_ID_ANY, 0);
377         else
378                 log_history_mp = rte_mempool_lookup(LOG_HISTORY_MP_NAME);
379         if (log_history_mp == NULL) {
380                 RTE_LOG(ERR, EAL, "%s(): cannot create log_history mempool\n",
381                         __func__);
382                 return -1;
383         }
384
385         default_log_stream = default_log;
386         rte_openlog_stream(default_log);
387         return 0;
388 }
389