Imported Upstream version 16.04
[deb_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                 if (hist_buf) {
123                         STAILQ_REMOVE_HEAD(&log_history, next);
124                         log_history_size--;
125                 }
126         }
127         else {
128                 if (rte_mempool_mc_get(log_history_mp, &obj) < 0)
129                         obj = NULL;
130                 hist_buf = obj;
131         }
132
133         /* no buffer */
134         if (hist_buf == NULL) {
135                 rte_spinlock_unlock(&log_list_lock);
136                 return -ENOBUFS;
137         }
138
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);
143                 return -ENOBUFS;
144         }
145
146         /* add in history */
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);
151         log_history_size++;
152         rte_spinlock_unlock(&log_list_lock);
153
154         return 0;
155 }
156
157 void
158 rte_log_set_history(int enable)
159 {
160         history_enabled = enable;
161 }
162
163 /* Change the stream that will be used by logging system */
164 int
165 rte_openlog_stream(FILE *f)
166 {
167         if (f == NULL)
168                 rte_logs.file = default_log_stream;
169         else
170                 rte_logs.file = f;
171         return 0;
172 }
173
174 /* Set global log level */
175 void
176 rte_set_log_level(uint32_t level)
177 {
178         rte_logs.level = (uint32_t)level;
179 }
180
181 /* Get global log level */
182 uint32_t
183 rte_get_log_level(void)
184 {
185         return rte_logs.level;
186 }
187
188 /* Set global log type */
189 void
190 rte_set_log_type(uint32_t type, int enable)
191 {
192         if (enable)
193                 rte_logs.type |= type;
194         else
195                 rte_logs.type &= (~type);
196 }
197
198 /* Get global log type */
199 uint32_t
200 rte_get_log_type(void)
201 {
202         return rte_logs.type;
203 }
204
205 /* get the current loglevel for the message beeing processed */
206 int rte_log_cur_msg_loglevel(void)
207 {
208         unsigned lcore_id;
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;
213 }
214
215 /* get the current logtype for the message beeing processed */
216 int rte_log_cur_msg_logtype(void)
217 {
218         unsigned lcore_id;
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;
223 }
224
225 /* Dump log history to file */
226 void
227 rte_log_dump_history(FILE *out)
228 {
229         struct log_history_list tmp_log_history;
230         struct log_history *hist_buf;
231         unsigned i;
232
233         /* only one dump at a time */
234         rte_spinlock_lock(&log_dump_lock);
235
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);
242
243         for (i=0; i<RTE_LOG_HISTORY; i++) {
244
245                 /* remove one message from history list */
246                 hist_buf = STAILQ_FIRST(&tmp_log_history);
247
248                 if (hist_buf == NULL)
249                         break;
250
251                 STAILQ_REMOVE_HEAD(&tmp_log_history, next);
252
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);
256                         break;
257                 }
258
259                 /* put back message structure in pool */
260                 rte_mempool_mp_put(log_history_mp, hist_buf);
261         }
262         fflush(out);
263
264         rte_spinlock_unlock(&log_dump_lock);
265 }
266
267 /*
268  * Generates a log message The message will be sent in the stream
269  * defined by the previous call to rte_openlog_stream().
270  */
271 int
272 rte_vlog(uint32_t level, uint32_t logtype, const char *format, va_list ap)
273 {
274         int ret;
275         FILE *f = rte_logs.file;
276         unsigned lcore_id;
277
278         if ((level > rte_logs.level) || !(logtype & rte_logs.type))
279                 return 0;
280
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;
286         }
287
288         ret = vfprintf(f, format, ap);
289         fflush(f);
290         return ret;
291 }
292
293 /*
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().
297  */
298 int
299 rte_log(uint32_t level, uint32_t logtype, const char *format, ...)
300 {
301         va_list ap;
302         int ret;
303
304         va_start(ap, format);
305         ret = rte_vlog(level, logtype, format, ap);
306         va_end(ap);
307         return ret;
308 }
309
310 /*
311  * called by environment-specific log init function to initialize log
312  * history
313  */
314 int
315 rte_eal_common_log_init(FILE *default_log)
316 {
317         STAILQ_INIT(&log_history);
318
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,
322                                 LOG_ELT_SIZE, 0, 0,
323                                 NULL, NULL,
324                                 NULL, NULL,
325                                 SOCKET_ID_ANY, 0);
326
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",
330                         __func__);
331                 return -1;
332         }
333
334         default_log_stream = default_log;
335         rte_openlog_stream(default_log);
336         return 0;
337 }