ia64/xen-unstable

changeset 12030:e834d5b965d0

[XEN] Implement rate-limited logging.

Taken from Andi Kleen's rate limit in the Linux kernel.

This keeps large amounts of prints in the HV down.

Signed-off-by: Steven Rostedt <srostedt@redhat.com>
author kaf24@localhost.localdomain
date Fri Oct 27 18:46:20 2006 +0100 (2006-10-27)
parents 50fa06501e54
children fd8036d06e10
files xen/arch/x86/mm.c xen/drivers/char/console.c xen/include/xen/lib.h
line diff
     1.1 --- a/xen/arch/x86/mm.c	Fri Oct 27 18:15:14 2006 +0100
     1.2 +++ b/xen/arch/x86/mm.c	Fri Oct 27 18:46:20 2006 +0100
     1.3 @@ -109,9 +109,12 @@
     1.4  #include <public/memory.h>
     1.5  
     1.6  #ifdef VERBOSE
     1.7 -#define MEM_LOG(_f, _a...)                                  \
     1.8 -  printk("DOM%u: (file=mm.c, line=%d) " _f "\n",            \
     1.9 -         current->domain->domain_id , __LINE__ , ## _a )
    1.10 +#define MEM_LOG(_f, _a...)                                          \
    1.11 +    do {                                                            \
    1.12 +        if ( printk_ratelimit() )                                   \
    1.13 +            printk("DOM%u: (file=mm.c, line=%d) " _f "\n",          \
    1.14 +                   current->domain->domain_id , __LINE__ , ## _a ); \
    1.15 +    } while (0)
    1.16  #else
    1.17  #define MEM_LOG(_f, _a...) ((void)0)
    1.18  #endif
     2.1 --- a/xen/drivers/char/console.c	Fri Oct 27 18:15:14 2006 +0100
     2.2 +++ b/xen/drivers/char/console.c	Fri Oct 27 18:46:20 2006 +0100
     2.3 @@ -4,6 +4,10 @@
     2.4   * Emergency console I/O for Xen and the domain-0 guest OS.
     2.5   * 
     2.6   * Copyright (c) 2002-2004, K A Fraser.
     2.7 + *
     2.8 + * Added printf_ratelimit
     2.9 + *     Taken from Linux - Author: Andi Kleen (net_ratelimit)
    2.10 + *     Ported to Xen - Steven Rostedt - Red Hat
    2.11   */
    2.12  
    2.13  #include <xen/stdarg.h>
    2.14 @@ -26,6 +30,7 @@
    2.15  #include <asm/current.h>
    2.16  #include <asm/debugger.h>
    2.17  #include <asm/io.h>
    2.18 +#include <asm/div64.h>
    2.19  
    2.20  /* console: comma-separated list of console outputs. */
    2.21  static char opt_console[30] = OPT_CONSOLE_STR;
    2.22 @@ -448,6 +453,56 @@ int console_getc(void)
    2.23      return serial_getc(sercon_handle);
    2.24  }
    2.25  
    2.26 +/*
    2.27 + * printk rate limiting, lifted from Linux.
    2.28 + *
    2.29 + * This enforces a rate limit: not more than one kernel message
    2.30 + * every printk_ratelimit_ms (millisecs).
    2.31 + */
    2.32 +int __printk_ratelimit(int ratelimit_ms, int ratelimit_burst)
    2.33 +{
    2.34 +    static DEFINE_SPINLOCK(ratelimit_lock);
    2.35 +    static unsigned long toks = 10 * 5 * 1000;
    2.36 +    static unsigned long last_msg;
    2.37 +    static int missed;
    2.38 +    unsigned long flags;
    2.39 +    unsigned long long now = NOW(); /* ns */
    2.40 +    unsigned long ms;
    2.41 +
    2.42 +    do_div(now, 1000000);
    2.43 +    ms = (unsigned long)now;
    2.44 +
    2.45 +    spin_lock_irqsave(&ratelimit_lock, flags);
    2.46 +    toks += ms - last_msg;
    2.47 +    last_msg = ms;
    2.48 +    if ( toks > (ratelimit_burst * ratelimit_ms))
    2.49 +        toks = ratelimit_burst * ratelimit_ms;
    2.50 +    if ( toks >= ratelimit_ms )
    2.51 +    {
    2.52 +        int lost = missed;
    2.53 +        missed = 0;
    2.54 +        toks -= ratelimit_ms;
    2.55 +        spin_unlock_irqrestore(&ratelimit_lock, flags);
    2.56 +        if ( lost )
    2.57 +            printk("printk: %d messages suppressed.\n", lost);
    2.58 +        return 1;
    2.59 +    }
    2.60 +    missed++;
    2.61 +    spin_unlock_irqrestore(&ratelimit_lock, flags);
    2.62 +    return 0;
    2.63 +}
    2.64 +
    2.65 +/* minimum time in ms between messages */
    2.66 +int printk_ratelimit_ms = 5 * 1000;
    2.67 +
    2.68 +/* number of messages we send before ratelimiting */
    2.69 +int printk_ratelimit_burst = 10;
    2.70 +
    2.71 +int printk_ratelimit(void)
    2.72 +{
    2.73 +    return __printk_ratelimit(printk_ratelimit_ms,
    2.74 +                              printk_ratelimit_burst);
    2.75 +}
    2.76  
    2.77  /*
    2.78   * **************************************************************
     3.1 --- a/xen/include/xen/lib.h	Fri Oct 27 18:15:14 2006 +0100
     3.2 +++ b/xen/include/xen/lib.h	Fri Oct 27 18:46:20 2006 +0100
     3.3 @@ -56,6 +56,8 @@ extern void printk(const char *format, .
     3.4  extern void panic(const char *format, ...)
     3.5      __attribute__ ((format (printf, 1, 2)));
     3.6  extern long vm_assist(struct domain *, unsigned int, unsigned int);
     3.7 +extern int __printk_ratelimit(int ratelimit_ms, int ratelimit_burst);
     3.8 +extern int printk_ratelimit(void);
     3.9  
    3.10  /* vsprintf.c */
    3.11  extern int sprintf(char * buf, const char * fmt, ...)