e8dc94acbfcdef28532542e58894ab15798c2eab
[dpdk.git] / lib / librte_eal / common / eal_common_log.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2014 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
106 /* default logs */
107
108 int
109 rte_log_add_in_history(const char *buf, size_t size)
110 {
111         struct log_history *hist_buf = NULL;
112         static const unsigned hist_buf_size = LOG_ELT_SIZE - sizeof(*hist_buf);
113         void *obj;
114
115         if (history_enabled == 0)
116                 return 0;
117
118         rte_spinlock_lock(&log_list_lock);
119
120         /* get a buffer for adding in history */
121         if (log_history_size > RTE_LOG_HISTORY) {
122                 hist_buf = STAILQ_FIRST(&log_history);
123                 STAILQ_REMOVE_HEAD(&log_history, next);
124         }
125         else {
126                 if (rte_mempool_mc_get(log_history_mp, &obj) < 0)
127                         obj = NULL;
128                 hist_buf = obj;
129         }
130
131         /* no buffer */
132         if (hist_buf == NULL) {
133                 rte_spinlock_unlock(&log_list_lock);
134                 return -ENOBUFS;
135         }
136
137         /* not enough room for msg, buffer go back in mempool */
138         if (size >= hist_buf_size) {
139                 rte_mempool_mp_put(log_history_mp, hist_buf);
140                 rte_spinlock_unlock(&log_list_lock);
141                 return -ENOBUFS;
142         }
143
144         /* add in history */
145         memcpy(hist_buf->buf, buf, size);
146         hist_buf->buf[size] = hist_buf->buf[hist_buf_size-1] = '\0';
147         hist_buf->size = size;
148         STAILQ_INSERT_TAIL(&log_history, hist_buf, next);
149         log_history_size++;
150         rte_spinlock_unlock(&log_list_lock);
151
152         return 0;
153 }
154
155 void
156 rte_log_set_history(int enable)
157 {
158         history_enabled = enable;
159 }
160
161 /* Change the stream that will be used by logging system */
162 int
163 rte_openlog_stream(FILE *f)
164 {
165         if (f == NULL)
166                 rte_logs.file = default_log_stream;
167         else
168                 rte_logs.file = f;
169         return 0;
170 }
171
172 /* Set global log level */
173 void
174 rte_set_log_level(uint32_t level)
175 {
176         rte_logs.level = (uint32_t)level;
177 }
178
179 /* Get global log level */
180 uint32_t
181 rte_get_log_level(void)
182 {
183         return rte_logs.level;
184 }
185
186 /* Set global log type */
187 void
188 rte_set_log_type(uint32_t type, int enable)
189 {
190         if (enable)
191                 rte_logs.type |= type;
192         else
193                 rte_logs.type &= (~type);
194 }
195
196 /* Get global log type */
197 uint32_t
198 rte_get_log_type(void)
199 {
200         return rte_logs.type;
201 }
202
203 /* get the current loglevel for the message beeing processed */
204 int rte_log_cur_msg_loglevel(void)
205 {
206         unsigned lcore_id;
207         lcore_id = rte_lcore_id();
208         if (lcore_id >= RTE_MAX_LCORE)
209                 return rte_get_log_level();
210         return log_cur_msg[lcore_id].loglevel;
211 }
212
213 /* get the current logtype for the message beeing processed */
214 int rte_log_cur_msg_logtype(void)
215 {
216         unsigned lcore_id;
217         lcore_id = rte_lcore_id();
218         if (lcore_id >= RTE_MAX_LCORE)
219                 return rte_get_log_type();
220         return log_cur_msg[lcore_id].logtype;
221 }
222
223 /* Dump log history to file */
224 void
225 rte_log_dump_history(FILE *out)
226 {
227         struct log_history_list tmp_log_history;
228         struct log_history *hist_buf;
229         unsigned i;
230
231         /* only one dump at a time */
232         rte_spinlock_lock(&log_dump_lock);
233
234         /* save list, and re-init to allow logging during dump */
235         rte_spinlock_lock(&log_list_lock);
236         tmp_log_history = log_history;
237         STAILQ_INIT(&log_history);
238         rte_spinlock_unlock(&log_list_lock);
239
240         for (i=0; i<RTE_LOG_HISTORY; i++) {
241
242                 /* remove one message from history list */
243                 hist_buf = STAILQ_FIRST(&tmp_log_history);
244
245                 if (hist_buf == NULL)
246                         break;
247
248                 STAILQ_REMOVE_HEAD(&tmp_log_history, next);
249
250                 /* write on stdout */
251                 if (fwrite(hist_buf->buf, hist_buf->size, 1, out) == 0) {
252                         rte_mempool_mp_put(log_history_mp, hist_buf);
253                         break;
254                 }
255
256                 /* put back message structure in pool */
257                 rte_mempool_mp_put(log_history_mp, hist_buf);
258         }
259         fflush(out);
260
261         rte_spinlock_unlock(&log_dump_lock);
262 }
263
264 /*
265  * Generates a log message The message will be sent in the stream
266  * defined by the previous call to rte_openlog_stream().
267  */
268 int
269 rte_vlog(__attribute__((unused)) uint32_t level,
270          __attribute__((unused)) uint32_t logtype,
271            const char *format, va_list ap)
272 {
273         int ret;
274         FILE *f = rte_logs.file;
275         unsigned lcore_id;
276
277         /* save loglevel and logtype in a global per-lcore variable */
278         lcore_id = rte_lcore_id();
279         if (lcore_id < RTE_MAX_LCORE) {
280                 log_cur_msg[lcore_id].loglevel = level;
281                 log_cur_msg[lcore_id].logtype = logtype;
282         }
283
284         ret = vfprintf(f, format, ap);
285         fflush(f);
286         return ret;
287 }
288
289 /*
290  * Generates a log message The message will be sent in the stream
291  * defined by the previous call to rte_openlog_stream().
292  */
293 int
294 rte_log(uint32_t level, uint32_t logtype, const char *format, ...)
295 {
296         va_list ap;
297         int ret;
298
299         va_start(ap, format);
300         ret = rte_vlog(level, logtype, format, ap);
301         va_end(ap);
302         return ret;
303 }
304
305 /*
306  * called by environment-specific log init function to initialize log
307  * history
308  */
309 int
310 rte_eal_common_log_init(FILE *default_log)
311 {
312         STAILQ_INIT(&log_history);
313
314         /* reserve RTE_LOG_HISTORY*2 elements, so we can dump and
315          * keep logging during this time */
316         log_history_mp = rte_mempool_create(LOG_HISTORY_MP_NAME, RTE_LOG_HISTORY*2,
317                                 LOG_ELT_SIZE, 0, 0,
318                                 NULL, NULL,
319                                 NULL, NULL,
320                                 SOCKET_ID_ANY, 0);
321
322         if ((log_history_mp == NULL) &&
323             ((log_history_mp = rte_mempool_lookup(LOG_HISTORY_MP_NAME)) == NULL)){
324                 RTE_LOG(ERR, EAL, "%s(): cannot create log_history mempool\n",
325                         __func__);
326                 return -1;
327         }
328
329         default_log_stream = default_log;
330         rte_openlog_stream(default_log);
331         return 0;
332 }
333