diff options
author | Richard Braun <rbraun@sceen.net> | 2019-05-22 21:59:19 +0200 |
---|---|---|
committer | Richard Braun <rbraun@sceen.net> | 2019-05-22 21:59:19 +0200 |
commit | 5bc2263b54a89e28dd5092c807a86cd3c078e4d3 (patch) | |
tree | ad399e98a6d46bad25543a4f8680335f3abb1bf8 /kern | |
parent | c45d94a590d778c26dc78386c41231fed9df1b14 (diff) |
Add a log print function type for information reporting
This type allows the use of either printf-based or log-based functions
when reporting information.
Diffstat (limited to 'kern')
-rw-r--r-- | kern/kernel.c | 3 | ||||
-rw-r--r-- | kern/kmem.c | 72 | ||||
-rw-r--r-- | kern/kmem.h | 7 | ||||
-rw-r--r-- | kern/log.h | 11 | ||||
-rw-r--r-- | kern/printf.c | 46 | ||||
-rw-r--r-- | kern/printf.h | 8 | ||||
-rw-r--r-- | kern/syscnt.c | 12 | ||||
-rw-r--r-- | kern/syscnt.h | 5 | ||||
-rw-r--r-- | kern/task.c | 35 | ||||
-rw-r--r-- | kern/task.h | 5 |
10 files changed, 128 insertions, 76 deletions
diff --git a/kern/kernel.c b/kern/kernel.c index 418793d7..98184e26 100644 --- a/kern/kernel.c +++ b/kern/kernel.c @@ -17,6 +17,7 @@ #include <kern/init.h> #include <kern/kernel.h> +#include <kern/log.h> #include <kern/thread.h> #include <machine/cpu.h> #include <test/test.h> @@ -28,7 +29,7 @@ kernel_main(void) assert(!cpu_intr_enabled()); init_setup(); - vm_page_log_info(); + vm_page_info(log_info); #ifdef CONFIG_TEST_MODULE test_setup(); diff --git a/kern/kmem.c b/kern/kmem.c index bd2e1d56..f5c74a96 100644 --- a/kern/kmem.c +++ b/kern/kmem.c @@ -51,6 +51,7 @@ #include <kern/init.h> #include <kern/list.h> +#include <kern/log.h> #include <kern/log2.h> #include <kern/kmem.h> #include <kern/kmem_i.h> @@ -449,7 +450,7 @@ kmem_cache_error(struct kmem_cache *cache, void *buf, int error, void *arg) { struct kmem_buftag *buftag; - printf("kmem: error: cache: %s, buffer: %p\n", cache->name, buf); + printf_ln("kmem: error: cache: %s, buffer: %p", cache->name, buf); switch(error) { case KMEM_ERR_INVALID: @@ -1107,7 +1108,7 @@ fast_free: } void -kmem_cache_info(struct kmem_cache *cache) +kmem_cache_info(struct kmem_cache *cache, log_print_fn_t print_fn) { char flags_str[64]; @@ -1117,28 +1118,23 @@ kmem_cache_info(struct kmem_cache *cache) mutex_lock(&cache->lock); - printf("kmem: flags: 0x%x%s\n" - "kmem: obj_size: %zu\n" - "kmem: align: %zu\n" - "kmem: buf_size: %zu\n" - "kmem: bufctl_dist: %zu\n" - "kmem: slab_size: %zu\n" - "kmem: color_max: %zu\n" - "kmem: bufs_per_slab: %lu\n" - "kmem: nr_objs: %lu\n" - "kmem: nr_bufs: %lu\n" - "kmem: nr_slabs: %lu\n" - "kmem: nr_free_slabs: %lu\n" - "kmem: buftag_dist: %zu\n" - "kmem: redzone_pad: %zu\n", - cache->flags, flags_str, cache->obj_size, - cache->align, cache->buf_size, cache->bufctl_dist, - cache->slab_size, cache->color_max, cache->bufs_per_slab, - cache->nr_objs, cache->nr_bufs, cache->nr_slabs, - cache->nr_free_slabs, cache->buftag_dist, cache->redzone_pad); + print_fn("kmem: flags: 0x%x%s", cache->flags, flags_str); + print_fn("kmem: obj_size: %zu", cache->obj_size); + print_fn("kmem: align: %zu", cache->align); + print_fn("kmem: buf_size: %zu", cache->buf_size); + print_fn("kmem: bufctl_dist: %zu", cache->bufctl_dist); + print_fn("kmem: slab_size: %zu", cache->slab_size); + print_fn("kmem: color_max: %zu", cache->color_max); + print_fn("kmem: bufs_per_slab: %lu", cache->bufs_per_slab); + print_fn("kmem: nr_objs: %lu", cache->nr_objs); + print_fn("kmem: nr_bufs: %lu", cache->nr_bufs); + print_fn("kmem: nr_slabs: %lu", cache->nr_slabs); + print_fn("kmem: nr_free_slabs: %lu", cache->nr_free_slabs); + print_fn("kmem: buftag_dist: %zu", cache->buftag_dist); + print_fn("kmem: redzone_pad: %zu", cache->redzone_pad); #ifdef KMEM_USE_CPU_LAYER - printf("kmem: cpu_pool_size: %d\n", cache->cpu_pool_type->array_size); + print_fn("kmem: cpu_pool_size: %d", cache->cpu_pool_type->array_size); #endif /* KMEM_USE_CPU_LAYER */ mutex_unlock(&cache->lock); @@ -1175,16 +1171,16 @@ kmem_shell_info(struct shell *shell, int argc, char **argv) (void)shell; if (argc < 2) { - kmem_info(); + kmem_info(printf_ln); } else { cache = kmem_lookup_cache(argv[1]); if (cache == NULL) { - printf("kmem: info: invalid argument\n"); + printf_ln("kmem: info: invalid argument"); return; } - kmem_cache_info(cache); + kmem_cache_info(cache, printf_ln); } } @@ -1383,7 +1379,7 @@ kmem_free(void *ptr, size_t size) } void -kmem_info(void) +kmem_info(log_print_fn_t print_fn) { size_t total_reclaim, total_reclaim_physical, total_reclaim_virtual; size_t total, total_physical, total_virtual; @@ -1397,10 +1393,10 @@ kmem_info(void) total_reclaim_physical = 0; total_reclaim_virtual = 0; - printf("kmem: cache obj slab bufs objs bufs " - " total reclaimable\n" - "kmem: name size size /slab usage count " - " memory memory\n"); + print_fn("kmem: cache obj slab bufs objs bufs " + " total reclaimable"); + print_fn("kmem: name size size /slab usage count " + " memory memory"); mutex_lock(&kmem_cache_list_lock); @@ -1420,18 +1416,18 @@ kmem_info(void) total_reclaim_physical += mem_reclaim; } - printf("kmem: %-19s %6zu %3zuk %4lu %6lu %6lu %7zuk %10zuk\n", - cache->name, cache->obj_size, cache->slab_size >> 10, - cache->bufs_per_slab, cache->nr_objs, cache->nr_bufs, - mem_usage, mem_reclaim); + print_fn("kmem: %-19s %6zu %3zuk %4lu %6lu %6lu %7zuk %10zuk", + cache->name, cache->obj_size, cache->slab_size >> 10, + cache->bufs_per_slab, cache->nr_objs, cache->nr_bufs, + mem_usage, mem_reclaim); mutex_unlock(&cache->lock); } mutex_unlock(&kmem_cache_list_lock); - printf("total: %zuk (phys: %zuk virt: %zuk), " - "reclaim: %zuk (phys: %zuk virt: %zuk)\n", - total, total_physical, total_virtual, - total_reclaim, total_reclaim_physical, total_reclaim_virtual); + print_fn("total: %zuk (phys: %zuk virt: %zuk), " + "reclaim: %zuk (phys: %zuk virt: %zuk)", + total, total_physical, total_virtual, + total_reclaim, total_reclaim_physical, total_reclaim_virtual); } diff --git a/kern/kmem.h b/kern/kmem.h index 3a3afb54..aee1dac2 100644 --- a/kern/kmem.h +++ b/kern/kmem.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2010-2014 Richard Braun. + * Copyright (c) 2010-2019 Richard Braun. * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -24,6 +24,7 @@ #include <stddef.h> #include <kern/init.h> +#include <kern/log.h> /* * Object cache. @@ -76,7 +77,7 @@ void kmem_cache_free(struct kmem_cache *cache, void *obj); * * If cache is NULL, this function displays all managed caches. */ -void kmem_cache_info(struct kmem_cache *cache); +void kmem_cache_info(struct kmem_cache *cache, log_print_fn_t print_fn); /* * Allocate size bytes of uninitialized memory. @@ -98,7 +99,7 @@ void kmem_free(void *ptr, size_t size); /* * Display global kernel memory information. */ -void kmem_info(void); +void kmem_info(log_print_fn_t print_fn); /* * This init operation provides : @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017 Richard Braun. + * Copyright (c) 2017-2019 Richard Braun. * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -38,6 +38,15 @@ enum { }; /* + * Type for function pointers that may be used as either a log function + * or printf. + * + * One call to a log print function produces a single log line, with a + * newline character. + */ +typedef int (*log_print_fn_t)(const char *format, ...); + +/* * Generate a message and send it to the log thread. * * The arguments and return value are similar to printf(), with diff --git a/kern/printf.c b/kern/printf.c index 29267a6e..8ef3e863 100644 --- a/kern/printf.c +++ b/kern/printf.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2010, 2012, 2013 Richard Braun. + * Copyright (c) 2010-2019 Richard Braun. * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -32,6 +32,20 @@ static char printf_buffer[PRINTF_BUFSIZE]; static struct spinlock printf_lock; +static int +vprintf_common(const char *format, va_list ap) +{ + int length; + + length = fmt_vsnprintf(printf_buffer, sizeof(printf_buffer), format, ap); + + for (char *ptr = printf_buffer; *ptr != '\0'; ptr++) { + console_putchar(*ptr); + } + + return length; +} + int printf(const char *format, ...) { @@ -50,16 +64,36 @@ vprintf(const char *format, va_list ap) { unsigned long flags; int length; - char *ptr; spinlock_lock_intr_save(&printf_lock, &flags); + length = vprintf_common(format, ap); + spinlock_unlock_intr_restore(&printf_lock, flags); - length = fmt_vsnprintf(printf_buffer, sizeof(printf_buffer), format, ap); + return length; +} - for (ptr = printf_buffer; *ptr != '\0'; ptr++) { - console_putchar(*ptr); - } +int +printf_ln(const char *format, ...) +{ + va_list ap; + int length; + + va_start(ap, format); + length = vprintf_ln(format, ap); + va_end(ap); + + return length; +} + +int +vprintf_ln(const char *format, va_list ap) +{ + unsigned long flags; + int length; + spinlock_lock_intr_save(&printf_lock, &flags); + length = vprintf_common(format, ap); + console_putchar('\n'); spinlock_unlock_intr_restore(&printf_lock, flags); return length; diff --git a/kern/printf.h b/kern/printf.h index eba5562b..e5e5f1b5 100644 --- a/kern/printf.h +++ b/kern/printf.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2010 Richard Braun. + * Copyright (c) 2010-2019 Richard Braun. * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -41,6 +41,12 @@ int printf(const char *format, ...) int vprintf(const char *format, va_list ap) __attribute__((format(printf, 1, 0))); +int printf_ln(const char *format, ...) + __attribute__((format(printf, 1, 2))); + +int vprintf_ln(const char *format, va_list ap) + __attribute__((format(printf, 1, 0))); + /* * This init operation provides : * - printf is usable diff --git a/kern/syscnt.c b/kern/syscnt.c index 1fd3cf06..93ca75f8 100644 --- a/kern/syscnt.c +++ b/kern/syscnt.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2014-2017 Richard Braun. + * Copyright (c) 2014-2019 Richard Braun. * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -15,12 +15,14 @@ * along with this program. If not, see <http://www.gnu.org/licenses/>. */ +#include <stddef.h> #include <stdio.h> #include <string.h> #include <kern/atomic.h> #include <kern/init.h> #include <kern/list.h> +#include <kern/log.h> #include <kern/mutex.h> #include <kern/shell.h> #include <kern/spinlock.h> @@ -43,7 +45,7 @@ syscnt_shell_info(struct shell *shell, int argc, char **argv) (void)shell; prefix = (argc >= 2) ? argv[1] : NULL; - syscnt_info(prefix); + syscnt_info(prefix, printf_ln); } static struct shell_cmd syscnt_shell_cmds[] = { @@ -96,7 +98,7 @@ syscnt_register(struct syscnt *syscnt, const char *name) } void -syscnt_info(const char *prefix) +syscnt_info(const char *prefix, log_print_fn_t print_fn) { struct syscnt *syscnt; size_t length, prefix_length; @@ -118,8 +120,8 @@ syscnt_info(const char *prefix) value = syscnt_read(syscnt); - printf("syscnt: %40s %20llu\n", syscnt->name, - (unsigned long long)value); + print_fn("syscnt: %40s %20llu", syscnt->name, + (unsigned long long)value); } mutex_unlock(&syscnt_lock); diff --git a/kern/syscnt.h b/kern/syscnt.h index 167be099..ee9c930d 100644 --- a/kern/syscnt.h +++ b/kern/syscnt.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2014-2017 Richard Braun. + * Copyright (c) 2014-2019 Richard Braun. * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -28,6 +28,7 @@ #include <kern/atomic.h> #include <kern/init.h> +#include <kern/log.h> #include <kern/macros.h> #include <kern/spinlock.h> @@ -125,7 +126,7 @@ syscnt_dec(struct syscnt *syscnt) * A prefix can be used to filter the output, where only counters with the * given prefix are displayed. If NULL, all counters are reported. */ -void syscnt_info(const char *prefix); +void syscnt_info(const char *prefix, log_print_fn_t print_fn); /* * This init operation provides : diff --git a/kern/task.c b/kern/task.c index ebdb021e..00cfb3a0 100644 --- a/kern/task.c +++ b/kern/task.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2012-2014 Richard Braun. + * Copyright (c) 2012-2019 Richard Braun. * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -23,6 +23,7 @@ #include <kern/init.h> #include <kern/kmem.h> #include <kern/list.h> +#include <kern/log.h> #include <kern/macros.h> #include <kern/shell.h> #include <kern/spinlock.h> @@ -70,7 +71,7 @@ task_shell_info(struct shell *shell, int argc, char *argv[]) (void)shell; if (argc == 1) { - task_info(NULL); + task_info(NULL, printf_ln); return; } else { task = task_lookup(argv[1]); @@ -80,14 +81,14 @@ task_shell_info(struct shell *shell, int argc, char *argv[]) goto error; } - task_info(task); + task_info(task, printf_ln); task_unref(task); } return; error: - printf("task: info: %s\n", strerror(error)); + printf_ln("task: info: %s", strerror(error)); } static struct shell_cmd task_shell_cmds[] = { @@ -227,7 +228,7 @@ task_lookup_thread(struct task *task, const char *name) } void -task_info(struct task *task) +task_info(struct task *task, log_print_fn_t print_fn) { struct thread *thread; @@ -235,7 +236,7 @@ task_info(struct task *task) spinlock_lock(&task_list_lock); list_for_each_entry(&task_list, task, node) { - task_info(task); + task_info(task, print_fn); } spinlock_unlock(&task_list_lock); @@ -245,7 +246,7 @@ task_info(struct task *task) spinlock_lock(&task->lock); - printf("task: name: %s, threads:\n", task->name); + print_fn("task: name: %s, threads:", task->name); /* * Don't grab any lock when accessing threads, so that the function @@ -256,16 +257,16 @@ task_info(struct task *task) * TODO Handle tasks and threads names modifications. */ list_for_each_entry(&task->threads, thread, task_node) { - printf(TASK_INFO_ADDR_FMT " %c %8s:" TASK_INFO_ADDR_FMT - " %.2s:%02hu %02u %s\n", - (unsigned long)thread, - thread_state_to_chr(thread_state(thread)), - thread_wchan_desc(thread), - (unsigned long)thread_wchan_addr(thread), - thread_sched_class_to_str(thread_user_sched_class(thread)), - thread_user_priority(thread), - thread_real_global_priority(thread), - thread_name(thread)); + print_fn(TASK_INFO_ADDR_FMT " %c %8s:" TASK_INFO_ADDR_FMT + " %.2s:%02hu %02u %s", + (unsigned long)thread, + thread_state_to_chr(thread_state(thread)), + thread_wchan_desc(thread), + (unsigned long)thread_wchan_addr(thread), + thread_sched_class_to_str(thread_user_sched_class(thread)), + thread_user_priority(thread), + thread_real_global_priority(thread), + thread_name(thread)); } spinlock_unlock(&task->lock); diff --git a/kern/task.h b/kern/task.h index 12e29ac8..d4f84c5c 100644 --- a/kern/task.h +++ b/kern/task.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2012 Richard Braun. + * Copyright (c) 2012-2019 Richard Braun. * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -21,6 +21,7 @@ #include <kern/atomic.h> #include <kern/init.h> #include <kern/list.h> +#include <kern/log.h> #include <kern/spinlock.h> #include <kern/thread.h> #include <vm/vm_map.h> @@ -116,7 +117,7 @@ struct thread * task_lookup_thread(struct task *task, const char *name); * * If task is NULL, this function displays all tasks. */ -void task_info(struct task *task); +void task_info(struct task *task, log_print_fn_t print_fn); /* * This init operation provides : |