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