source: mainline/uspace/srv/klog/klog.c@ 8820544

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 8820544 was 8820544, checked in by Martin Decky <martin@…>, 11 years ago

support for kernel notification multiplexing in the async framework

  • rename SYS_EVENT_* and SYS_IRQ_* syscalls to unify the terminology
  • add SYS_IPC_EVENT_UNSUBSCRIBE
  • remove IRQ handler multiplexing from DDF, the generic mechanism replaces it (unfortunatelly the order of arguments used by interrupt_handler_t needs to be permutated to align with the async framework conventions)
  • Property mode set to 100644
File size: 5.8 KB
RevLine 
[0caa075e]1/*
2 * Copyright (c) 2006 Ondrej Palkovsky
3 * Copyright (c) 2013 Martin Sucha
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * - Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * - Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * - The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30/** @addtogroup klog KLog
31 * @brief HelenOS KLog
32 * @{
33 */
34/**
35 * @file
36 */
37
38#include <stdio.h>
39#include <async.h>
40#include <as.h>
41#include <ddi.h>
42#include <errno.h>
43#include <str_error.h>
44#include <io/klog.h>
45#include <sysinfo.h>
46#include <malloc.h>
47#include <fibril_synch.h>
48#include <adt/list.h>
49#include <adt/prodcons.h>
50#include <io/log.h>
51#include <io/logctl.h>
52
53#define NAME "klog"
54
55typedef struct {
56 size_t entry_len;
57 uint32_t serial;
58 uint32_t facility;
59 uint32_t level;
60 char message[0];
61
62} __attribute__((__packed__)) log_entry_t;
63
64/* Producer/consumer buffers */
65typedef struct {
66 link_t link;
67 size_t size;
68 log_entry_t *data;
69} item_t;
70
71static prodcons_t pc;
72
73/* Pointer to buffer where kernel stores new entries */
74#define BUFFER_SIZE PAGE_SIZE
75static void *buffer;
76
77/* Notification mutex */
78static FIBRIL_MUTEX_INITIALIZE(mtx);
79
80static log_t kernel_ctx;
81static const char *facility_name[] = {
82 "other",
83 "uspace",
84 "arch"
85};
86
87#define facility_len (sizeof(facility_name) / sizeof(const char *))
88static log_t facility_ctx[facility_len];
89
90/** Klog producer
91 *
92 * Copies the log entries to a producer/consumer queue.
93 *
94 * @param length Number of characters to copy.
95 * @param data Pointer to the kernel klog buffer.
96 *
97 */
98static void producer()
99{
100 int read = klog_read(buffer, BUFFER_SIZE);
101
102 if (read < 0) {
103 log_msg(LOG_DEFAULT, LVL_ERROR, "klog_read failed, rc = %d",
104 read);
105 return;
106 }
107
108 size_t len = read;
109 size_t offset = 0;
110 while (offset < len) {
111 size_t entry_len = *((size_t *) (buffer + offset));
112
113 if (offset + entry_len > len || entry_len < sizeof(log_entry_t))
114 break;
115
116 log_entry_t *buf = malloc(entry_len + 1);
117 if (buf == NULL)
118 break;
119
120 item_t *item = malloc(sizeof(item_t));
121 if (item == NULL) {
122 free(buf);
123 break;
124 }
125
126 memcpy(buf, buffer + offset, entry_len);
127 *((uint8_t *) buf + entry_len) = 0;
128 link_initialize(&item->link);
129 item->size = entry_len;
130 item->data = buf;
131 prodcons_produce(&pc, &item->link);
132
133 offset += entry_len;
134 }
135}
136
137/** Klog consumer
138 *
139 * Waits in an infinite loop for the log data created by
140 * the producer and logs them to the logger.
141 *
142 * @param data Unused.
143 *
144 * @return Always EOK (unreachable).
145 *
146 */
147static int consumer(void *data)
148{
149
150 while (true) {
151 link_t *link = prodcons_consume(&pc);
152 item_t *item = list_get_instance(link, item_t, link);
153
154 if (item->size < sizeof(log_entry_t)) {
155 free(item->data);
156 free(item);
157 continue;
158 }
159
160 if (item->data->facility == LF_USPACE) {
161 /* Avoid reposting messages */
162 free(item->data);
163 free(item);
164 continue;
165 }
166
167 log_t ctx = kernel_ctx;
168 if (item->data->facility < facility_len) {
169 ctx = facility_ctx[item->data->facility];
170 }
171
172 log_level_t lvl = item->data->level;
173 if (lvl > LVL_LIMIT)
174 lvl = LVL_NOTE;
175
176 log_msg(ctx, lvl, "%s", item->data->message);
177
178 free(item->data);
179 free(item);
180 }
181
182 return EOK;
183}
184
185/** Kernel notification handler
186 *
187 * Receives kernel klog notifications.
188 *
189 * @param callid IPC call ID
190 * @param call IPC call structure
191 * @param arg Local argument
192 *
193 */
[8820544]194static void klog_notification_received(ipc_callid_t callid, ipc_call_t *call,
195 void *arg)
[0caa075e]196{
197 /*
198 * Make sure we process only a single notification
199 * at any time to limit the chance of the consumer
200 * starving.
201 */
202
203 fibril_mutex_lock(&mtx);
204
205 producer();
206
[8820544]207 async_event_unmask(EVENT_KLOG);
[0caa075e]208 fibril_mutex_unlock(&mtx);
209}
210
211int main(int argc, char *argv[])
212{
213 int rc = log_init(NAME);
214 if (rc != EOK) {
215 fprintf(stderr, "%s: Unable to initialize log\n", NAME);
216 return rc;
217 }
218
219 kernel_ctx = log_create("kernel", LOG_NO_PARENT);
220 for (unsigned int i = 0; i < facility_len; i++) {
221 facility_ctx[i] = log_create(facility_name[i], kernel_ctx);
222 }
223
224 buffer = malloc(BUFFER_SIZE);
225 if (buffer == NULL) {
226 log_msg(LOG_DEFAULT, LVL_ERROR, "Unable to allocate buffer");
227 return 1;
228 }
229
230 prodcons_initialize(&pc);
[8820544]231 rc = async_event_subscribe(EVENT_KLOG, klog_notification_received, NULL);
[0caa075e]232 if (rc != EOK) {
233 log_msg(LOG_DEFAULT, LVL_ERROR,
234 "Unable to register klog notifications");
235 return rc;
236 }
237
238 fid_t fid = fibril_create(consumer, NULL);
239 if (!fid) {
240 log_msg(LOG_DEFAULT, LVL_ERROR,
241 "Unable to create consumer fibril");
242 return ENOMEM;
243 }
244
245 fibril_add_ready(fid);
[8820544]246 async_event_unmask(EVENT_KLOG);
[0caa075e]247
248 fibril_mutex_lock(&mtx);
249 producer();
250 fibril_mutex_unlock(&mtx);
251
252 task_retval(0);
253 async_manager();
254
255 return 0;
256}
257
258/** @}
259 */
Note: See TracBrowser for help on using the repository browser.