doc: whitespace changes in licenses
[dpdk.git] / lib / librte_eal / common / eal_common_log.c
1 /*-
2  *   BSD LICENSE
3  * 
4  *   Copyright(c) 2010-2013 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 #include <string.h>
35 #include <stdio.h>
36 #include <stdint.h>
37 #include <stdarg.h>
38 #include <sys/types.h>
39 #include <stdlib.h>
40 #include <unistd.h>
41 #include <inttypes.h>
42 #include <errno.h>
43 #include <sys/queue.h>
44
45 #include <rte_log.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>
51 #include <rte_tailq.h>
52 #include <rte_eal.h>
53 #include <rte_per_lcore.h>
54 #include <rte_lcore.h>
55 #include <rte_atomic.h>
56 #include <rte_debug.h>
57 #include <rte_spinlock.h>
58 #include <rte_branch_prediction.h>
59 #include <rte_ring.h>
60 #include <rte_mempool.h>
61
62 #include "eal_private.h"
63
64 #define LOG_ELT_SIZE     2048
65
66 #define LOG_HISTORY_MP_NAME "log_history"
67
68 STAILQ_HEAD(log_history_list, log_history);
69
70 /**
71  * The structure of a message log in the log history.
72  */
73 struct log_history {
74         STAILQ_ENTRY(log_history) next;
75         unsigned size;
76         char buf[0];
77 };
78
79 static struct rte_mempool *log_history_mp = NULL;
80 static unsigned log_history_size = 0;
81 static struct log_history_list log_history;
82
83 /* global log structure */
84 struct rte_logs rte_logs = {
85         .type = ~0,
86         .level = RTE_LOG_DEBUG,
87         .file = NULL,
88 };
89
90 static rte_spinlock_t log_dump_lock = RTE_SPINLOCK_INITIALIZER;
91 static rte_spinlock_t log_list_lock = RTE_SPINLOCK_INITIALIZER;
92 static FILE *default_log_stream;
93 static int history_enabled = 1;
94
95 /**
96  * This global structure stores some informations about the message
97  * that is currently beeing processed by one lcore
98  */
99 struct log_cur_msg {
100         uint32_t loglevel; /**< log level - see rte_log.h */
101         uint32_t logtype;  /**< log type  - see rte_log.h */
102 } __rte_cache_aligned;
103 static struct log_cur_msg log_cur_msg[RTE_MAX_LCORE]; /**< per core log */
104
105 /* early logs */
106
107 /*
108  * early log function, used during boot when mempool (hence log
109  * history) is not available
110  */
111 static ssize_t
112 early_log_write(__attribute__((unused)) void *c, const char *buf, size_t size)
113 {
114         ssize_t ret;
115         ret = fwrite(buf, size, 1, stdout);
116         fflush(stdout);
117         if (ret == 0)
118                 return -1;
119         return ret;
120 }
121
122 static ssize_t
123 early_log_read(__attribute__((unused)) void *c,
124                __attribute__((unused)) char *buf,
125                __attribute__((unused)) size_t size)
126 {
127         return 0;
128 }
129
130 /*
131  * this is needed because cookies_io_functions_t has a different
132  * prototype between newlib and glibc
133  */
134 #ifdef RTE_EXEC_ENV_LINUXAPP
135 static int
136 early_log_seek(__attribute__((unused)) void *c,
137                __attribute__((unused)) off64_t *offset,
138                __attribute__((unused)) int whence)
139 {
140         return -1;
141 }
142 #else
143 static int
144 early_log_seek(__attribute__((unused)) void *c,
145                __attribute__((unused)) _off_t *offset,
146                __attribute__((unused)) int whence)
147 {
148         return -1;
149 }
150 #endif
151
152 static int
153 early_log_close(__attribute__((unused)) void *c)
154 {
155         return 0;
156 }
157
158 static cookie_io_functions_t early_log_func = {
159         .read  = early_log_read,
160         .write = early_log_write,
161         .seek  = early_log_seek,
162         .close = early_log_close
163 };
164 static FILE *early_log_stream;
165
166 /* default logs */
167
168 int
169 rte_log_add_in_history(const char *buf, size_t size)
170 {
171         struct log_history *hist_buf = NULL;
172         static const unsigned hist_buf_size = LOG_ELT_SIZE - sizeof(*hist_buf);
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 >= hist_buf_size) {
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[size] = hist_buf->buf[hist_buf_size-1] = '\0';
207         hist_buf->size = size;
208         STAILQ_INSERT_TAIL(&log_history, hist_buf, next);
209         log_history_size++;
210         rte_spinlock_unlock(&log_list_lock);
211
212         return 0;
213 }
214
215 void
216 rte_log_set_history(int enable)
217 {
218         history_enabled = enable;
219 }
220
221 /* Change the stream that will be used by logging system */
222 int
223 rte_openlog_stream(FILE *f)
224 {
225         if (f == NULL)
226                 rte_logs.file = default_log_stream;
227         else
228                 rte_logs.file = f;
229         return 0;
230 }
231
232 /* Set global log level */
233 void
234 rte_set_log_level(uint32_t level)
235 {
236         rte_logs.level = (uint32_t)level;
237 }
238
239 /* Set global log type */
240 void
241 rte_set_log_type(uint32_t type, int enable)
242 {
243         if (enable)
244                 rte_logs.type |= type;
245         else
246                 rte_logs.type &= (~type);
247 }
248
249 /* get the current loglevel for the message beeing processed */
250 int rte_log_cur_msg_loglevel(void)
251 {
252         unsigned lcore_id;
253         lcore_id = rte_lcore_id();
254         return log_cur_msg[lcore_id].loglevel;
255 }
256
257 /* get the current logtype for the message beeing processed */
258 int rte_log_cur_msg_logtype(void)
259 {
260         unsigned lcore_id;
261         lcore_id = rte_lcore_id();
262         return log_cur_msg[lcore_id].logtype;
263 }
264
265 /* Dump log history on console */
266 void
267 rte_log_dump_history(void)
268 {
269         struct log_history_list tmp_log_history;
270         struct log_history *hist_buf;
271         unsigned i;
272
273         /* only one dump at a time */
274         rte_spinlock_lock(&log_dump_lock);
275
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);
281
282         for (i=0; i<RTE_LOG_HISTORY; i++) {
283
284                 /* remove one message from history list */
285                 hist_buf = STAILQ_FIRST(&tmp_log_history);
286
287                 if (hist_buf == NULL)
288                         break;
289
290                 STAILQ_REMOVE_HEAD(&tmp_log_history, next);
291
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);
295                         break;
296                 }
297
298                 /* put back message structure in pool */
299                 rte_mempool_mp_put(log_history_mp, hist_buf);
300         }
301         fflush(stdout);
302
303         rte_spinlock_unlock(&log_dump_lock);
304 }
305
306 /*
307  * Generates a log message The message will be sent in the stream
308  * defined by the previous call to rte_openlog_stream().
309  */
310 int
311 rte_vlog(__attribute__((unused)) uint32_t level,
312          __attribute__((unused)) uint32_t logtype,
313            const char *format, va_list ap)
314 {
315         int ret;
316         FILE *f = rte_logs.file;
317         unsigned lcore_id;
318
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;
323
324         ret = vfprintf(f, format, ap);
325         fflush(f);
326         return ret;
327 }
328
329 /*
330  * Generates a log message The message will be sent in the stream
331  * defined by the previous call to rte_openlog_stream().
332  */
333 int
334 rte_log(uint32_t level, uint32_t logtype, const char *format, ...)
335 {
336         va_list ap;
337         int ret;
338
339         va_start(ap, format);
340         ret = rte_vlog(level, logtype, format, ap);
341         va_end(ap);
342         return ret;
343 }
344
345 /*
346  * init the log library, called by rte_eal_init() to enable early
347  * logs
348  */
349 int
350 rte_eal_log_early_init(void)
351 {
352         early_log_stream = fopencookie(NULL, "w+", early_log_func);
353         if (early_log_stream == NULL) {
354                 printf("Cannot configure early_log_stream\n");
355                 return -1;
356         }
357         rte_openlog_stream(early_log_stream);
358         return 0;
359 }
360
361 /*
362  * called by environment-specific log init function to initialize log
363  * history
364  */
365 int
366 rte_eal_common_log_init(FILE *default_log)
367 {
368         STAILQ_INIT(&log_history);
369
370         /* reserve RTE_LOG_HISTORY*2 elements, so we can dump and
371          * keep logging during this time */
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        
378         if ((log_history_mp == NULL) && 
379             ((log_history_mp = rte_mempool_lookup(LOG_HISTORY_MP_NAME)) == 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