mars/brick_say.c

309 lines
5.8 KiB
C
Raw Normal View History

// (c) 2010 Thomas Schoebel-Theuer / 1&1 Internet AG
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/string.h>
#include "brick_say.h"
/////////////////////////////////////////////////////////////////////
// messaging
#include <linux/fs.h>
#include <linux/sched.h>
#include <linux/preempt.h>
#include <linux/hardirq.h>
#include <asm/uaccess.h>
2012-01-30 16:29:59 +00:00
#ifndef GFP_BRICK
#define GFP_BRICK GFP_NOIO
#endif
#define SAY_ORDER 0
#define SAY_BUFMAX (PAGE_SIZE << SAY_ORDER)
#define MAX_FILELEN 16
static char *say_buf[NR_CPUS] = {};
static int say_index[NR_CPUS] = {};
static int dump_max = 5;
static atomic_t overflow = ATOMIC_INIT(0);
static struct file *log_file = NULL;
2012-01-11 15:16:39 +00:00
static inline
void say_alloc(unsigned long cpu, bool use_atomic)
{
2012-01-30 16:29:59 +00:00
char *ptr;
if (likely(say_buf[cpu]) || unlikely(cpu >= NR_CPUS))
goto done;
2012-01-30 16:29:59 +00:00
ptr = (void*)__get_free_pages(use_atomic ? GFP_ATOMIC : GFP_BRICK, SAY_ORDER);
if (likely(ptr)) {
ptr[0] = '\0';
say_buf[cpu] = ptr;
say_index[cpu] = 0;
}
done: ;
}
2012-01-30 16:29:59 +00:00
static
void _say_mark(unsigned long cpu)
{
2012-01-30 16:29:59 +00:00
char *ptr;
bool use_atomic = (preempt_count() & (SOFTIRQ_MASK | HARDIRQ_MASK | NMI_MASK)) != 0;
2012-01-11 15:16:39 +00:00
say_alloc(cpu, use_atomic);
2012-01-30 16:29:59 +00:00
if (unlikely(use_atomic || cpu >= NR_CPUS))
goto done;
2012-01-30 16:29:59 +00:00
ptr = say_buf[cpu];
if (unlikely(!ptr) || !ptr[0])
goto done;
if (log_file) {
loff_t log_pos = 0;
int rest = say_index[cpu];
int len = 0;
while (rest > 0) {
2012-01-11 15:16:39 +00:00
int status;
mm_segment_t oldfs;
oldfs = get_fs();
set_fs(get_ds());
2012-01-30 16:29:59 +00:00
status = vfs_write(log_file, ptr + len, rest, &log_pos);
2012-01-11 15:16:39 +00:00
set_fs(oldfs);
2012-01-30 16:29:59 +00:00
if (unlikely(status <= 0))
break;
len += status;
rest -= status;
}
2012-01-11 15:16:39 +00:00
#ifdef CONFIG_MARS_USE_SYSLOG
} else {
2012-01-30 16:29:59 +00:00
printk("%s", ptr);
#endif
2012-01-11 15:16:39 +00:00
}
2012-01-30 16:29:59 +00:00
ptr[0] = '\0';
say_index[cpu] = 0;
2012-01-11 15:16:39 +00:00
{
static long long old_jiffies = 0;
if (((long long)jiffies) - old_jiffies >= HZ * 5) {
static spinlock_t lock = SPIN_LOCK_UNLOCKED;
bool won_the_race = false;
spin_lock(&lock);
if (((long long)jiffies) - old_jiffies >= HZ * 5) {
old_jiffies = jiffies;
won_the_race = true;
}
spin_unlock(&lock);
if (won_the_race)
check_close(CONFIG_MARS_LOGFILE, false, true);
}
}
done: ;
}
void say_mark(void)
{
unsigned long cpu = get_cpu();
_say_mark(cpu);
put_cpu();
}
EXPORT_SYMBOL_GPL(say_mark);
static
void _say(unsigned long cpu, va_list args, bool use_args, const char *fmt, ...)
{
char *start;
int rest;
int written;
if (!say_buf[cpu])
goto done;
rest = SAY_BUFMAX - say_index[cpu];
if (rest <= 0)
goto done;
start = say_buf[cpu] + say_index[cpu];
if (use_args) {
/* bug in gcc: use register variable
* shading the parameter
*/
va_list args;
va_start(args, fmt);
written = vsnprintf(start, rest, fmt, args);
va_end(args);
} else {
written = vsnprintf(start, rest, fmt, args);
}
if (likely(rest > written)) {
start[written] = '\0';
say_index[cpu] += written;
} else {
// indicate overflow
2012-01-30 16:29:59 +00:00
start[0] = '\0';
atomic_inc(&overflow);
}
done: ;
}
static inline
void _check_overflow(unsigned long cpu)
{
2012-01-30 16:29:59 +00:00
int count = 0;
atomic_xchg(&overflow, count);
if (unlikely(count > 0)) {
if (likely(say_index[cpu] < SAY_BUFMAX - 8)) {
_say(cpu, NULL, true, "#%d#\n", count);
}
}
}
void say(const char *fmt, ...)
{
unsigned long cpu = get_cpu();
va_list args;
_say_mark(cpu);
2012-01-30 16:29:59 +00:00
if (unlikely(!say_buf[cpu]))
goto done;
_check_overflow(cpu);
va_start(args, fmt);
_say(cpu, args, false, fmt);
va_end(args);
_say_mark(cpu);
done:
put_cpu();
}
EXPORT_SYMBOL_GPL(say);
2012-01-11 15:16:39 +00:00
void brick_say(bool dump, const char *prefix, const char *file, int line, const char *func, const char *fmt, ...)
{
struct timespec now = CURRENT_TIME;
unsigned long cpu = get_cpu();
int filelen;
va_list args;
_say_mark(cpu);
2012-01-30 16:29:59 +00:00
if (unlikely(!say_buf[cpu]))
goto done;
_check_overflow(cpu);
// limit the filename
filelen = strlen(file);
if (filelen > MAX_FILELEN)
file += filelen - MAX_FILELEN;
_say(cpu, NULL, true, "%ld.%09ld %s %s[%d] %s %d %s(): ", now.tv_sec, now.tv_nsec, prefix, current->comm, (int)cpu, file, line, func);
va_start(args, fmt);
_say(cpu, args, false, fmt);
va_end(args);
_say_mark(cpu);
2012-01-11 15:16:39 +00:00
#ifdef CONFIG_DEBUG_KERNEL
if (dump)
brick_dump_stack();
#endif
done:
put_cpu();
}
EXPORT_SYMBOL_GPL(brick_say);
2012-01-11 15:16:39 +00:00
void check_open(const char *filename, bool must_exist)
{
2012-01-11 15:16:39 +00:00
int flags = O_EXCL | O_APPEND | O_WRONLY | O_LARGEFILE;
int prot = 0600;
mm_segment_t oldfs;
2012-01-11 15:16:39 +00:00
if (log_file)
return;
if (!must_exist)
flags |= O_CREAT;
oldfs = get_fs();
set_fs(get_ds());
2012-01-11 15:16:39 +00:00
log_file = filp_open(filename, flags, prot);
set_fs(oldfs);
2012-01-30 16:29:59 +00:00
if (unlikely(IS_ERR(log_file))) {
2012-01-11 15:16:39 +00:00
int status = PTR_ERR(log_file);
log_file = NULL;
2012-01-11 15:16:39 +00:00
say("cannot open logfile '%s', status = %d\n", filename, status);
} else {
say("opened logfile '%s' %p\n", filename, log_file);
}
}
void check_close(const char *filename, bool force, bool re_open)
{
struct kstat st = {};
int status;
if (!force) {
mm_segment_t oldfs;
oldfs = get_fs();
set_fs(get_ds());
status = vfs_stat((char*)filename, &st);
set_fs(oldfs);
force = (status < 0 || !st.size);
}
if (force) {
if (log_file) {
struct file *old;
say("closing logfile....\n");
old = log_file;
log_file = NULL;
// FIXME: this may race against vfs_write(). Use rcu here.
filp_close(old, NULL);
say("closed logfile.\n");
}
if (re_open)
check_open(filename, true);
}
2012-01-11 15:16:39 +00:00
}
void init_say(void)
{
check_open(CONFIG_MARS_LOGFILE, true);
}
EXPORT_SYMBOL_GPL(init_say);
void exit_say(void)
{
int i;
for (i = 0; i < NR_CPUS; i++) {
if (!say_buf[i])
continue;
__free_pages(virt_to_page((unsigned long)say_buf[i]), SAY_ORDER);
say_buf[i] = NULL;
}
2012-01-11 15:16:39 +00:00
check_close(CONFIG_MARS_LOGFILE, true, false);
}
EXPORT_SYMBOL_GPL(exit_say);
#ifdef CONFIG_DEBUG_KERNEL
void brick_dump_stack(void)
{
if (dump_max > 0) {
dump_max--; // racy, but does no harm
dump_stack();
}
}
EXPORT_SYMBOL(brick_dump_stack);
#endif