/* allocinfo.c -- Installs and implements the /proc/allocinfo virtual file.
 * Copyright C2009 by EQware Engineering, Inc.
 *
 *    allocinfo.c is part of AllocInfo.
 *
 *    AllocInfo is free software: you can redistribute it and/or modify
 *    it under the terms of version 3 of the GNU General Public License
 *    as published by the Free Software Foundation
 *
 *    AllocInfo is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with AllocInfo.  If not, see http://www.gnu.org/licenses. 
 */
#include <linux/proc_fs.h>
#include <linux/vmstat.h>


/* proc_calc_metrics() --
 */
static int proc_calc_metrics(char *page, char **start, off_t off,
                             int count, int *eof, int len)
{
    if (len <= off + count) *eof = 1;
    *start = page + off;
    len -= off;
    if (len > count) len = count;
    if (len < 0) len = 0;
    return len;

}   /* proc_calc_metrics */


unsigned long long heap_alloc_count[6];

/* allocinfo_read_proc() -- This function is called when the /proc/allocinfo
 *   virtual file is read.
 *   
 *   Since there is no locking to prevent read-across-write 
 *   between here and alloc_page(), the numbers might be slightly 
 *   inconsistent.  Too bad.
 */
static int allocinfo_read_proc(char *page, char **start, off_t off,
                               int count, int *eof, void *data)
{
    /* Print the heap allocation counts.
     */
    int len = sprintf(page,
                  "%llu %llu %llu %llu %llu %llu\n",
                  heap_alloc_count[0], 
                  heap_alloc_count[1],
                  heap_alloc_count[2], 
                  heap_alloc_count[3],
                  heap_alloc_count[4], 
                  heap_alloc_count[5],
                  );
                  
    /* Zero the heap allocation counts.
     */
    memset(heap_alloc_count, 0, sizeof(heap_alloc_count));
    
    return proc_calc_metrics(page, start, off, count, eof, len);

}   /* allocinfo_read_proc */


/* proc_allocinfo_init() -- Initialize and create the /proc/allocinfo
 *   virtual file.
 */
static int __init proc_allocinfo_init(void)
{
    /* Zero the heap allocation counts.
     */
    memset(heap_alloc_count, 0, sizeof(heap_alloc_count));
    
    /* Create the /proc/allocinfo virtual file.
     */
    create_proc_read_entry("allocinfo", 0, NULL, allocinfo_read_proc, NULL);
    
    return 0;

}   /* proc_allocinfo_init */


module_init(proc_allocinfo_init);




