/*
 * procmod.c:  example of using /proc for debug output, using proc_read()
 * Copyright (C) 2002-2003 Randy Dunlap <rddunlap@ieee.org>
 */

/******************************************************************
#   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
#   the Free Software Foundation; either version 2 of the License, or
#   (at your option) any later version.
#
#   This program 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 this program; if not, write to the Free Software
#   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
******************************************************************/

#define MODULE

#include <linux/config.h>
#include <linux/module.h>
#include <linux/version.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/proc_fs.h>
#include <linux/stat.h>
#include <linux/kernel_stat.h>
#include <asm/div64.h>

/*
* This module can be used for printing debug info in /proc/numbers .
* /proc/numbers is currently read-only.
* It could be made to accept written data also.
*
* This procfs module exhibits a well-known syndrome:
* For each instance of 'cat /proc/numbers', numbers_read() is
* called 2 times, and <number_current> is incremented 2 times.
* This is a side-effect of how apps and/or libc read a file
* until read() returns 0, meaning EOF.
*
* version 0.2:  add 2.5 external module support + Makefile;
*/

#define VERSION		"0.2"
#define MODNAME		"numbers-debug"

extern struct proc_dir_entry proc_root;
static struct proc_dir_entry *numdir;
static int number_current;
/*
 * kstat.
	unsigned int pgpgin, pgpgout;
	unsigned int pswpin, pswpout;
	unsigned int context_swtch;
 */
/////extern int nr_running, nr_threads, last_pid;
extern unsigned long volatile jiffies;

#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,40)
__u64 paging_sum (void)
{
	return 0;	// not implemented
}
#endif

static int numbers_read (char *buf, char **start, off_t offset, int count,
	int *eof, void *data)
{
	int len = 0;
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,5,40)
	__u64 ctxtsw = kstat.context_swtch;
	__u64 pages = kstat.pgpgin + kstat.pgpgout + kstat.pswpin + kstat.pswpout;
#else
// use percpu kstat data after 2.5.something:
	extern unsigned long nr_context_switches(void);
	__u64 ctxtsw = nr_context_switches();
	__u64 pages = paging_sum();
#endif
	__u64 jifs = jiffies;
	__u64 t1, t2;

	/* do_div:  n = n / base; return rem; */

	/*
	__u64 mbps = 0;
	__u64 t1 = (info->pg_sofar*1000);
	__u64 t2 = do_div(total, 1000);
	__u64 pps = 0; // do_div(t1, t2);
	t1 = (info->pg_sofar * 1000);
	mbps = 0; // do_div(t1, t2);
	// mbps *= info->pkt_size;
	*/

	len = sprintf(buf, "module_name: %s\n", MODNAME);
	len += sprintf(buf + len, "version: %s\n", VERSION);

	/* paging * 1000 / ctxtsw ::= */

	t1 = pages * 1000;
	t2 = do_div(t1, ctxtsw);	// t1 = pages * 1000 / ctxtsw; t2 = rem.
	len += sprintf(buf + len, "pages, ctxtsw, t1, t2:  %lld, %lld, %lld, %lld\n",
			pages, ctxtsw, t1, t2);

	/* paging * 1000 / jifs ::= */

	t1 = pages * 1000;
	t2 = do_div(t1, jifs);		// t1 = pages * 1000 / jifs; t2 = rem.
	len += sprintf(buf + len, "pages, jifs, t1, t2:  %lld, %lld, %lld, %lld\n",
			pages, jifs, t1, t2);

	/* ctxtsw * 1000 / jifs ::= */
	t1 = ctxtsw * 1000;
	t2 = do_div(t1, jifs);		// t1 = ctxtsw * 1000 / jifs; t2 = rem.
	len += sprintf(buf + len, "ctxtsw, jifs, t1, t2:  %lld, %lld, %lld, %lld\n",
			ctxtsw, jifs, t1, t2);

	*eof = 1;
	return len;
}

/*
 * In this example, the <write> function doesn't write the
 * passed data.  It is only used to reset the <number> counter.
 */
static int numbers_write (struct file *file, const char *buf,
		unsigned long count, void *data)
{
	number_current = 0;
	return count < sizeof (number_current) ? count : sizeof (number_current);
}

static int __init numbers_init (void)
{
	numdir = create_proc_entry ("numbers", 0644, &proc_root);
	if (numdir) {
		numdir->owner = THIS_MODULE;
		numdir->uid = 0;
		numdir->read_proc = numbers_read;
		numdir->write_proc = numbers_write;
	}
	else
		printk (KERN_INFO MODNAME ": numdir create error\n");
	return 0;
}

static void __exit numbers_exit (void)
{
	if (numdir)
		remove_proc_entry ("numbers", NULL);
}

module_init (numbers_init);
module_exit (numbers_exit);
#ifdef MODULE_LICENSE
MODULE_LICENSE ("GPL");
#endif
MODULE_DESCRIPTION ("procfs-debug-example");
