/*
 * arch/s390/appldata/appldata_os.c
 *
 * Data gathering module for Linux-VM Monitor Stream, Stage 1.
 * Collects misc. OS related data (CPU utilization, running processes).
 *
 * Copyright (C) 2003 IBM Corporation, IBM Deutschland Entwicklung GmbH.
 *
 * Author: Gerald Schaefer <geraldsc@de.ibm.com>
 */

#include <linux/config.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/slab.h>
#include <linux/errno.h> 
#include <linux/kernel_stat.h>
#include <linux/netdevice.h>

#include "appldata.h"


#define MY_PRINT_NAME	"appldata_os"		/* for debug messages, etc. */
#define LOAD_INT(x) ((x) >> FSHIFT)
#define LOAD_FRAC(x) LOAD_INT(((x) & (FIXED_1-1)) * 100)

/*
 * OS data
 */
struct appldata_os_per_cpu {
	u32 per_cpu_user;	/* timer ticks spent in user mode   */
	u32 per_cpu_nice;	/* ... spent with modified priority */
	u32 per_cpu_system;	/* ... spent in kernel mode         */
	u32 per_cpu_idle;	/* ... spent in idle mode           */
};

struct appldata_os_data {
	u64 timestamp;
	u32 sync_count_1;	/* after VM collected the record data, */
	u32 sync_count_2;	/* sync_count_1 and sync_count_2 should be the
				   same. If not, the record has been updated on
				   the Linux side while VM was collecting the
				   (possibly corrupt) data */

	u32 nr_cpus;		/* number of (virtual) CPUs        */
	u32 per_cpu_size;	/* size of the per-cpu data struct */
	u32 cpu_offset;		/* offset of the first per-cpu data struct */

	u32 nr_running;		/* number of runnable threads       */
	u32 nr_threads;		/* number of threads                */

	u32 avenrun[3];		/* average nr. of running processes during */
				/* the last 1, 5 and 15 minutes */
	
	/* per cpu data */
	struct appldata_os_per_cpu os_cpu[0];
};

static struct appldata_os_data *appldata_os_data;


static inline void appldata_print_debug(struct appldata_os_data *os_data)
{
	int i;
	unsigned int a0, a1, a2;

	P_DEBUG("--- OS - RECORD ---\n");
	P_DEBUG("nr_threads   = %u\n", os_data->nr_threads);
	P_DEBUG("nr_running   = %u\n", os_data->nr_running);
	P_DEBUG("avenrun(int) = %8x / %8x / %8x\n", os_data->avenrun[0],
		os_data->avenrun[1], os_data->avenrun[2]);
	a0 = os_data->avenrun[0];
	a1 = os_data->avenrun[1];
	a2 = os_data->avenrun[2];
	P_DEBUG("avenrun(float) = %d.%02d / %d.%02d / %d.%02d\n",
		LOAD_INT(a0), LOAD_FRAC(a0), LOAD_INT(a1), LOAD_FRAC(a1),
		LOAD_INT(a2), LOAD_FRAC(a2));

	for (i = 0; i < smp_num_cpus; i++) {
		P_DEBUG("cpu%u : user = %u, nice = %u, system = %u, "
			"idle = %u\n",
				i,
				os_data->os_cpu[i].per_cpu_user,
				os_data->os_cpu[i].per_cpu_nice,
				os_data->os_cpu[i].per_cpu_system,
				os_data->os_cpu[i].per_cpu_idle);
	}

	P_DEBUG("sync_count_1 = %u\n", os_data->sync_count_1);
	P_DEBUG("sync_count_2 = %u\n", os_data->sync_count_2);
	P_DEBUG("timestamp    = %llX\n", ULL(os_data->timestamp));
}

/*
 * appldata_get_os_data()
 *
 * gather OS data
 */
static void appldata_get_os_data(void *data)
{
	int i;
	struct appldata_os_data *os_data;

	os_data = data;
	os_data->sync_count_1++;
	os_data->nr_cpus = smp_num_cpus;

	os_data->nr_threads = nr_threads;
	os_data->nr_running = nr_running;
	os_data->avenrun[0] = (u32) avenrun[0] + (FIXED_1/200);
	os_data->avenrun[1] = (u32) avenrun[1] + (FIXED_1/200);
	os_data->avenrun[2] = (u32) avenrun[2] + (FIXED_1/200);

	for (i = 0; i < smp_num_cpus; i++) {
		os_data->os_cpu[i].per_cpu_user = 
				    kstat.per_cpu_user[cpu_logical_map(i)];
		os_data->os_cpu[i].per_cpu_nice =
				    kstat.per_cpu_nice[cpu_logical_map(i)];
		os_data->os_cpu[i].per_cpu_system =
				    kstat.per_cpu_system[cpu_logical_map(i)];
		os_data->os_cpu[i].per_cpu_idle = jiffies - (
					os_data->os_cpu[i].per_cpu_user
				      + os_data->os_cpu[i].per_cpu_nice
				      + os_data->os_cpu[i].per_cpu_system);
	}

	os_data->timestamp = get_clock();
	os_data->sync_count_2++;
	appldata_print_debug(os_data);
}


static struct appldata_ops ops = {
	.ctl_nr    = CTL_APPLDATA_OS,
	.name	   = "os",
	.record_nr = APPLDATA_RECORD_OS_ID,
	.callback  = &appldata_get_os_data,
	.owner     = THIS_MODULE,
};


/*
 * appldata_os_init()
 *
 * init data, register ops
 */
static int __init appldata_os_init(void)
{
	int rc, size;

	size = sizeof(struct appldata_os_data) + 
		(smp_num_cpus * sizeof(struct appldata_os_per_cpu));
	if (size > APPLDATA_MAX_REC_SIZE) {
		P_ERROR("Size of record = %i, bigger than maximum (%i)!\n",
			size, APPLDATA_MAX_REC_SIZE);
		rc = -ENOMEM;
		goto out;
	}
	P_DEBUG("sizeof(os) = %i, sizeof(os_cpu) = %lu\n", size,
		sizeof(struct appldata_os_per_cpu));

	appldata_os_data = kmalloc(size, GFP_DMA);
	if (appldata_os_data == NULL) {
		P_ERROR("No memory for %s!\n", ops.name);
		rc = -ENOMEM;
		goto out;
	}
	memset(appldata_os_data, 0, size);

	appldata_os_data->per_cpu_size = sizeof(struct appldata_os_per_cpu);
	appldata_os_data->cpu_offset   = offsetof(struct appldata_os_data,
							os_cpu);
	P_DEBUG("cpu offset = %u\n", appldata_os_data->cpu_offset);

	ops.data = appldata_os_data;
	ops.size = size;
	rc = appldata_register_ops(&ops);
	if (rc != 0) {
		P_ERROR("Error registering ops, rc = %i\n", rc);
		kfree(appldata_os_data);
	} else {
		P_DEBUG("%s-ops registered!\n", ops.name);
	}
out:
	return rc;
}

/*
 * appldata_os_exit()
 * 
 * unregister ops
 */
static void __exit appldata_os_exit(void)
{
	appldata_unregister_ops(&ops);
	kfree(appldata_os_data);
	P_DEBUG("%s-ops unregistered!\n", ops.name);
}


module_init(appldata_os_init);
module_exit(appldata_os_exit);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Gerald Schaefer");
MODULE_DESCRIPTION("Linux-VM Monitor Stream, OS statistics");
