allocinfo.c.html


    1  /* allocinfo.c -- Installs and implements the /proc/allocinfo virtual file.
    2   * Copyright C2009 by EQware Engineering, Inc.
    3   *
    4   *    allocinfo.c is part of AllocInfo.
    5   *
    6   *    AllocInfo is free software: you can redistribute it and/or modify
    7   *    it under the terms of version 3 of the GNU General Public License
    8   *    as published by the Free Software Foundation
    9   *
   10   *    AllocInfo is distributed in the hope that it will be useful,
   11   *    but WITHOUT ANY WARRANTY; without even the implied warranty of
   12   *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   13   *    GNU General Public License for more details.
   14   *
   15   *    You should have received a copy of the GNU General Public License
   16   *    along with AllocInfo.  If not, see http://www.gnu.org/licenses. 
   17   */
   18  #include <linux/proc_fs.h>
   19  #include <linux/vmstat.h>
   20  
   21  
   22  /* proc_calc_metrics() --
   23   */
   24  static int proc_calc_metrics(char *page, char **start, off_t off,
   25                               int count, int *eof, int len)
   26  {
   27      if (len <= off + count) *eof = 1;
   28      *start = page + off;
   29      len -= off;
   30      if (len > count) len = count;
   31      if (len < 0) len = 0;
   32      return len;
   33  
   34  }   /* proc_calc_metrics */
   35  
   36  
   37  unsigned long long heap_alloc_count[6];
   38  
   39  /* allocinfo_read_proc() -- This function is called when the /proc/allocinfo
   40   *   virtual file is read.
   41   *   
   42   *   Since there is no locking to prevent read-across-write 
   43   *   between here and alloc_page(), the numbers might be slightly 
   44   *   inconsistent.  Too bad.
   45   */
   46  static int allocinfo_read_proc(char *page, char **start, off_t off,
   47                                 int count, int *eof, void *data)
   48  {
   49      /* Print the heap allocation counts.
   50       */
   51      int len = sprintf(page,
   52                    "%llu %llu %llu %llu %llu %llu\n",
   53                    heap_alloc_count[0], 
   54                    heap_alloc_count[1],
   55                    heap_alloc_count[2], 
   56                    heap_alloc_count[3],
   57                    heap_alloc_count[4], 
   58                    heap_alloc_count[5],
   59                    );
   60                    
   61      /* Zero the heap allocation counts.
   62       */
   63      memset(heap_alloc_count, 0, sizeof(heap_alloc_count));
   64      
   65      return proc_calc_metrics(page, start, off, count, eof, len);
   66  
   67  }   /* allocinfo_read_proc */
   68  
   69  
   70  /* proc_allocinfo_init() -- Initialize and create the /proc/allocinfo
   71   *   virtual file.
   72   */
   73  static int __init proc_allocinfo_init(void)
   74  {
   75      /* Zero the heap allocation counts.
   76       */
   77      memset(heap_alloc_count, 0, sizeof(heap_alloc_count));
   78      
   79      /* Create the /proc/allocinfo virtual file.
   80       */
   81      create_proc_read_entry("allocinfo", 0, NULL, allocinfo_read_proc, NULL);
   82      
   83      return 0;
   84  
   85  }   /* proc_allocinfo_init */
   86  
   87  
   88  module_init(proc_allocinfo_init);