2 * arch/s390/appldata/appldata_base.c
4 * Base infrastructure for Linux-z/VM Monitor Stream, Stage 1.
5 * Exports appldata_register_ops() and appldata_unregister_ops() for the
6 * data gathering modules.
8 * Copyright (C) 2003 IBM Corporation, IBM Deutschland Entwicklung GmbH.
10 * Author: Gerald Schaefer <geraldsc@de.ibm.com>
13 #include <linux/config.h>
14 #include <linux/module.h>
15 #include <linux/init.h>
16 #include <linux/slab.h>
17 #include <linux/errno.h>
18 #include <asm/uaccess.h>
20 #include <linux/interrupt.h>
21 #include <linux/proc_fs.h>
22 #include <linux/page-flags.h>
23 #include <linux/swap.h>
24 #include <linux/pagemap.h>
25 #include <linux/sysctl.h>
26 #include <asm/timer.h>
27 //#include <linux/kernel_stat.h>
32 #define MY_PRINT_NAME "appldata" /* for debug messages, etc. */
33 #define APPLDATA_CPU_INTERVAL 10000 /* default (CPU) time for
37 #define TOD_MICRO 0x01000 /* nr. of TOD clock units
39 #ifndef CONFIG_ARCH_S390X
41 #define APPLDATA_START_INTERVAL_REC 0x00 /* Function codes for */
42 #define APPLDATA_STOP_REC 0x01 /* DIAG 0xDC */
43 #define APPLDATA_GEN_EVENT_RECORD 0x02
44 #define APPLDATA_START_CONFIG_REC 0x03
48 #define APPLDATA_START_INTERVAL_REC 0x80
49 #define APPLDATA_STOP_REC 0x81
50 #define APPLDATA_GEN_EVENT_RECORD 0x82
51 #define APPLDATA_START_CONFIG_REC 0x83
53 #endif /* CONFIG_ARCH_S390X */
57 * Parameter list for DIAGNOSE X'DC'
59 #ifndef CONFIG_ARCH_S390X
60 struct appldata_parameter_list {
61 u16 diag; /* The DIAGNOSE code X'00DC' */
62 u8 function; /* The function code for the DIAGNOSE */
63 u8 parlist_length; /* Length of the parameter list */
64 u32 product_id_addr; /* Address of the 16-byte product ID */
66 u16 buffer_length; /* Length of the application data buffer */
67 u32 buffer_addr; /* Address of the application data buffer */
70 struct appldata_parameter_list {
81 #endif /* CONFIG_ARCH_S390X */
84 * /proc entries (sysctl)
86 static const char appldata_proc_name[APPLDATA_PROC_NAME_LENGTH] = "appldata";
87 static int appldata_timer_handler(ctl_table *ctl, int write, struct file *filp,
88 void __user *buffer, size_t *lenp);
89 static int appldata_interval_handler(ctl_table *ctl, int write,
94 static struct ctl_table_header *appldata_sysctl_header;
95 static struct ctl_table appldata_table[] = {
97 .ctl_name = CTL_APPLDATA_TIMER,
99 .mode = S_IRUGO | S_IWUSR,
100 .proc_handler = &appldata_timer_handler,
103 .ctl_name = CTL_APPLDATA_INTERVAL,
104 .procname = "interval",
105 .mode = S_IRUGO | S_IWUSR,
106 .proc_handler = &appldata_interval_handler,
111 static struct ctl_table appldata_dir_table[] = {
113 .ctl_name = CTL_APPLDATA,
114 .procname = appldata_proc_name,
116 .mode = S_IRUGO | S_IXUGO,
117 .child = appldata_table,
125 DEFINE_PER_CPU(struct vtimer_list, appldata_timer);
126 static atomic_t appldata_expire_count = ATOMIC_INIT(0);
127 static struct appldata_mod_vtimer_args {
128 struct vtimer_list *timer;
130 } appldata_mod_vtimer_args;
132 static spinlock_t appldata_timer_lock = SPIN_LOCK_UNLOCKED;
133 static int appldata_interval = APPLDATA_CPU_INTERVAL;
134 static int appldata_timer_active;
139 static struct tasklet_struct appldata_tasklet_struct;
144 static spinlock_t appldata_ops_lock = SPIN_LOCK_UNLOCKED;
145 static LIST_HEAD(appldata_ops_list);
148 /************************* timer, tasklet, DIAG ******************************/
150 * appldata_timer_function()
152 * schedule tasklet and reschedule timer
154 static void appldata_timer_function(unsigned long data, struct pt_regs *regs)
156 P_DEBUG(" -= Timer =-\n");
157 P_DEBUG("CPU: %i, expire: %i\n", smp_processor_id(),
158 atomic_read(&appldata_expire_count));
159 if (atomic_dec_and_test(&appldata_expire_count)) {
160 atomic_set(&appldata_expire_count, num_online_cpus());
161 tasklet_schedule((struct tasklet_struct *) data);
166 * appldata_tasklet_function()
168 * call data gathering function for each (active) module
170 static void appldata_tasklet_function(unsigned long data)
172 struct list_head *lh;
173 struct appldata_ops *ops;
176 P_DEBUG(" -= Tasklet =-\n");
178 spin_lock(&appldata_ops_lock);
179 list_for_each(lh, &appldata_ops_list) {
180 ops = list_entry(lh, struct appldata_ops, list);
181 P_DEBUG("list_for_each loop: %i) active = %u, name = %s\n",
182 ++i, ops->active, ops->name);
183 if (ops->active == 1) {
184 ops->callback(ops->data);
187 spin_unlock(&appldata_ops_lock);
191 * appldata_mod_vtimer_wrap()
193 * wrapper function for mod_virt_timer(), because smp_call_function_on()
194 * accepts only one parameter.
196 static void appldata_mod_vtimer_wrap(void *p) {
197 struct appldata_mod_vtimer_args *args = p;
198 mod_virt_timer(args->timer, args->expires);
204 * prepare parameter list, issue DIAG 0xDC
206 static int appldata_diag(char record_nr, u16 function, unsigned long buffer,
210 struct appldata_product_id {
211 char prod_nr[7]; /* product nr. */
212 char prod_fn[2]; /* product function */
213 char record_nr; /* record nr. */
214 char version_nr[2]; /* version */
215 char release_nr[2]; /* release */
216 char mod_lvl[2]; /* modification lvl. */
217 } appldata_product_id = {
218 /* all strings are EBCDIC, record_nr is byte */
219 .prod_nr = {0xD3, 0xC9, 0xD5, 0xE4,
220 0xE7, 0xD2, 0xD9}, /* "LINUXKR" */
221 .prod_fn = {0xD5, 0xD3}, /* "NL" */
222 .record_nr = record_nr,
223 .version_nr = {0xF2, 0xF6}, /* "26" */
224 .release_nr = {0xF0, 0xF1}, /* "01" */
225 .mod_lvl = {0xF0, 0xF0}, /* "00" */
227 struct appldata_parameter_list appldata_parameter_list = {
229 .function = function,
231 sizeof(appldata_parameter_list),
232 .buffer_length = length,
234 (unsigned long) &appldata_product_id,
235 .buffer_addr = virt_to_phys((void *) buffer)
242 "diag %1,%0,0xDC\n\t"
243 : "=d" (ry) : "d" (&(appldata_parameter_list)) : "cc");
246 /********************** timer, tasklet, DIAG <END> ***************************/
249 /****************************** /proc stuff **********************************/
251 * appldata_timer_handler()
253 * Start/Stop timer, show status of timer (0 = not active, 1 = active)
256 appldata_timer_handler(ctl_table *ctl, int write, struct file *filp,
257 void __user *buffer, size_t *lenp)
261 u64 per_cpu_interval;
263 if (!*lenp || filp->f_pos) {
268 len = sprintf(buf, appldata_timer_active ? "1\n" : "0\n");
271 if (copy_to_user(buffer, buf, len))
275 per_cpu_interval = (u64) (appldata_interval*1000 /
276 num_online_cpus()) * TOD_MICRO;
278 if (copy_from_user(buf, buffer, len > sizeof(buf) ? sizeof(buf) : len))
280 spin_lock(&appldata_timer_lock);
281 per_cpu_interval = (u64) (appldata_interval*1000 /
282 num_online_cpus()) * TOD_MICRO;
283 if ((buf[0] == '1') && (!appldata_timer_active)) {
284 for (i = 0; i < num_online_cpus(); i++) {
285 per_cpu(appldata_timer, i).expires = per_cpu_interval;
286 smp_call_function_on(add_virt_timer_periodic,
287 &per_cpu(appldata_timer, i),
290 appldata_timer_active = 1;
291 P_INFO("Monitoring timer started.\n");
292 } else if ((buf[0] == '0') && (appldata_timer_active)) {
293 for (i = 0; i < num_online_cpus(); i++) {
294 del_virt_timer(&per_cpu(appldata_timer, i));
296 appldata_timer_active = 0;
297 P_INFO("Monitoring timer stopped.\n");
299 spin_unlock(&appldata_timer_lock);
307 * appldata_interval_handler()
309 * Set (CPU) timer interval for collection of data (in milliseconds), show
310 * current timer interval.
313 appldata_interval_handler(ctl_table *ctl, int write, struct file *filp,
314 void __user *buffer, size_t *lenp)
316 int len, i, interval;
318 u64 per_cpu_interval;
320 if (!*lenp || filp->f_pos) {
325 len = sprintf(buf, "%i\n", appldata_interval);
328 if (copy_to_user(buffer, buf, len))
333 if (copy_from_user(buf, buffer, len > sizeof(buf) ? sizeof(buf) : len)) {
336 sscanf(buf, "%i", &interval);
338 P_ERROR("Timer CPU interval has to be > 0!\n");
342 spin_lock(&appldata_timer_lock);
343 per_cpu_interval = (u64) (interval*1000 / num_online_cpus()) * TOD_MICRO;
344 appldata_interval = interval;
345 if (appldata_timer_active) {
346 for (i = 0; i < num_online_cpus(); i++) {
347 appldata_mod_vtimer_args.timer =
348 &per_cpu(appldata_timer, i);
349 appldata_mod_vtimer_args.expires =
351 smp_call_function_on(
352 appldata_mod_vtimer_wrap,
353 &appldata_mod_vtimer_args,
357 spin_unlock(&appldata_timer_lock);
359 P_INFO("Monitoring CPU interval set to %u milliseconds.\n",
368 * appldata_generic_handler()
370 * Generic start/stop monitoring and DIAG, show status of
371 * monitoring (0 = not in process, 1 = in process)
374 appldata_generic_handler(ctl_table *ctl, int write, struct file *filp,
375 void __user *buffer, size_t *lenp)
377 struct appldata_ops *ops = NULL, *tmp_ops;
380 struct list_head *lh;
383 spin_lock_bh(&appldata_ops_lock);
384 list_for_each(lh, &appldata_ops_list) {
385 tmp_ops = list_entry(lh, struct appldata_ops, list);
386 if (&tmp_ops->ctl_table[2] == ctl) {
391 spin_unlock_bh(&appldata_ops_lock);
395 if (!try_module_get(ops->owner)) { // protect this function
396 spin_unlock_bh(&appldata_ops_lock);
399 spin_unlock_bh(&appldata_ops_lock);
401 if (!*lenp || filp->f_pos) {
403 module_put(ops->owner);
407 len = sprintf(buf, ops->active ? "1\n" : "0\n");
410 if (copy_to_user(buffer, buf, len)) {
411 module_put(ops->owner);
417 if (copy_from_user(buf, buffer,
418 len > sizeof(buf) ? sizeof(buf) : len)) {
419 module_put(ops->owner);
423 spin_lock_bh(&appldata_ops_lock);
424 if ((buf[0] == '1') && (ops->active == 0)) {
425 if (!try_module_get(ops->owner)) { // protect tasklet
426 spin_unlock_bh(&appldata_ops_lock);
427 module_put(ops->owner);
431 ops->callback(ops->data); // init record
432 rc = appldata_diag(ops->record_nr,
433 APPLDATA_START_INTERVAL_REC,
434 (unsigned long) ops->data, ops->size);
436 P_ERROR("START DIAG 0xDC for %s failed, "
437 "return code: %d\n", ops->name, rc);
438 module_put(ops->owner);
441 P_INFO("Monitoring %s data enabled, "
442 "DIAG 0xDC started.\n", ops->name);
444 } else if ((buf[0] == '0') && (ops->active == 1)) {
446 rc = appldata_diag(ops->record_nr, APPLDATA_STOP_REC,
447 (unsigned long) ops->data, ops->size);
449 P_ERROR("STOP DIAG 0xDC for %s failed, "
450 "return code: %d\n", ops->name, rc);
452 P_INFO("Monitoring %s data disabled, "
453 "DIAG 0xDC stopped.\n", ops->name);
455 module_put(ops->owner);
457 spin_unlock_bh(&appldata_ops_lock);
461 module_put(ops->owner);
465 /*************************** /proc stuff <END> *******************************/
468 /************************* module-ops management *****************************/
470 * appldata_register_ops()
472 * update ops list, register /proc/sys entries
474 int appldata_register_ops(struct appldata_ops *ops)
476 struct list_head *lh;
477 struct appldata_ops *tmp_ops;
482 if ((ops->size > APPLDATA_MAX_REC_SIZE) ||
484 P_ERROR("Invalid size of %s record = %i, maximum = %i!\n",
485 ops->name, ops->size, APPLDATA_MAX_REC_SIZE);
488 if ((ops->ctl_nr == CTL_APPLDATA) ||
489 (ops->ctl_nr == CTL_APPLDATA_TIMER) ||
490 (ops->ctl_nr == CTL_APPLDATA_INTERVAL)) {
491 P_ERROR("ctl_nr %i already in use!\n", ops->ctl_nr);
494 ops->ctl_table = kmalloc(4*sizeof(struct ctl_table), GFP_KERNEL);
495 if (ops->ctl_table == NULL) {
496 P_ERROR("Not enough memory for %s ctl_table!\n", ops->name);
499 memset(ops->ctl_table, 0, 4*sizeof(struct ctl_table));
501 spin_lock_bh(&appldata_ops_lock);
502 list_for_each(lh, &appldata_ops_list) {
503 tmp_ops = list_entry(lh, struct appldata_ops, list);
504 P_DEBUG("register_ops loop: %i) name = %s, ctl = %i\n",
505 ++i, tmp_ops->name, tmp_ops->ctl_nr);
506 P_DEBUG("Comparing %s (ctl %i) with %s (ctl %i)\n",
507 tmp_ops->name, tmp_ops->ctl_nr, ops->name,
509 if (strncmp(tmp_ops->name, ops->name,
510 APPLDATA_PROC_NAME_LENGTH) == 0) {
511 P_ERROR("Name \"%s\" already registered!\n", ops->name);
512 kfree(ops->ctl_table);
513 spin_unlock_bh(&appldata_ops_lock);
516 if (tmp_ops->ctl_nr == ops->ctl_nr) {
517 P_ERROR("ctl_nr %i already registered!\n", ops->ctl_nr);
518 kfree(ops->ctl_table);
519 spin_unlock_bh(&appldata_ops_lock);
523 list_add(&ops->list, &appldata_ops_list);
524 spin_unlock_bh(&appldata_ops_lock);
526 ops->ctl_table[0].ctl_name = CTL_APPLDATA;
527 ops->ctl_table[0].procname = appldata_proc_name;
528 ops->ctl_table[0].maxlen = 0;
529 ops->ctl_table[0].mode = S_IRUGO | S_IXUGO;
530 ops->ctl_table[0].child = &ops->ctl_table[2];
532 ops->ctl_table[1].ctl_name = 0;
534 ops->ctl_table[2].ctl_name = ops->ctl_nr;
535 ops->ctl_table[2].procname = ops->name;
536 ops->ctl_table[2].mode = S_IRUGO | S_IWUSR;
537 ops->ctl_table[2].proc_handler = appldata_generic_handler;
538 ops->ctl_table[2].data = ops;
540 ops->ctl_table[3].ctl_name = 0;
542 ops->sysctl_header = register_sysctl_table(ops->ctl_table,1);
544 P_INFO("%s-ops registered!\n", ops->name);
549 * appldata_unregister_ops()
551 * update ops list, unregister /proc entries, stop DIAG if necessary
553 void appldata_unregister_ops(struct appldata_ops *ops)
555 spin_lock_bh(&appldata_ops_lock);
556 unregister_sysctl_table(ops->sysctl_header);
557 list_del(&ops->list);
558 kfree(ops->ctl_table);
559 ops->ctl_table = NULL;
560 spin_unlock_bh(&appldata_ops_lock);
561 P_INFO("%s-ops unregistered!\n", ops->name);
563 /********************** module-ops management <END> **************************/
566 /******************************* init / exit *********************************/
570 * init timer and tasklet, register /proc entries
572 static int __init appldata_init(void)
576 P_DEBUG("sizeof(parameter_list) = %lu\n",
577 sizeof(struct appldata_parameter_list));
579 for (i = 0; i < num_online_cpus(); i++) {
580 init_virt_timer(&per_cpu(appldata_timer, i));
581 per_cpu(appldata_timer, i).function = appldata_timer_function;
582 per_cpu(appldata_timer, i).data = (unsigned long)
583 &appldata_tasklet_struct;
585 atomic_set(&appldata_expire_count, num_online_cpus());
587 appldata_sysctl_header = register_sysctl_table(appldata_dir_table, 1);
589 appldata_dir_table[0].de->owner = THIS_MODULE;
590 appldata_table[0].de->owner = THIS_MODULE;
591 appldata_table[1].de->owner = THIS_MODULE;
594 tasklet_init(&appldata_tasklet_struct, appldata_tasklet_function, 0);
595 P_DEBUG("Base interface initialized.\n");
602 * stop timer and tasklet, unregister /proc entries
604 static void __exit appldata_exit(void)
606 struct list_head *lh;
607 struct appldata_ops *ops;
610 P_DEBUG("Unloading module ...\n");
612 * ops list should be empty, but just in case something went wrong...
614 spin_lock_bh(&appldata_ops_lock);
615 list_for_each(lh, &appldata_ops_list) {
616 ops = list_entry(lh, struct appldata_ops, list);
617 rc = appldata_diag(ops->record_nr, APPLDATA_STOP_REC,
618 (unsigned long) ops->data, ops->size);
620 P_ERROR("STOP DIAG 0xDC for %s failed, "
621 "return code: %d\n", ops->name, rc);
624 spin_unlock_bh(&appldata_ops_lock);
626 for (i = 0; i < num_online_cpus(); i++) {
627 del_virt_timer(&per_cpu(appldata_timer, i));
629 appldata_timer_active = 0;
631 unregister_sysctl_table(appldata_sysctl_header);
633 tasklet_kill(&appldata_tasklet_struct);
634 P_DEBUG("... module unloaded!\n");
636 /**************************** init / exit <END> ******************************/
639 module_init(appldata_init);
640 module_exit(appldata_exit);
641 MODULE_LICENSE("GPL");
642 MODULE_AUTHOR("Gerald Schaefer");
643 MODULE_DESCRIPTION("Linux-VM Monitor Stream, base infrastructure");
645 EXPORT_SYMBOL_GPL(appldata_register_ops);
646 EXPORT_SYMBOL_GPL(appldata_unregister_ops);
650 * Kernel symbols needed by appldata_mem and appldata_os modules.
651 * However, if this file is compiled as a module (for testing only), these
652 * symbols are not exported. In this case, we define them locally and export
655 void si_swapinfo(struct sysinfo *val)
657 val->freeswap = -1ul;
658 val->totalswap = -1ul;
661 unsigned long avenrun[3] = {-1 - FIXED_1/200, -1 - FIXED_1/200,
665 void get_full_page_state(struct page_state *ps)
667 memset(ps, -1, sizeof(struct page_state));
670 unsigned long nr_running(void)
675 unsigned long nr_iowait(void)
680 /*unsigned long nr_context_switches(void)
685 EXPORT_SYMBOL_GPL(si_swapinfo);
686 EXPORT_SYMBOL_GPL(nr_threads);
687 EXPORT_SYMBOL_GPL(avenrun);
688 EXPORT_SYMBOL_GPL(get_full_page_state);
689 EXPORT_SYMBOL_GPL(nr_running);
690 EXPORT_SYMBOL_GPL(nr_iowait);
691 //EXPORT_SYMBOL_GPL(nr_context_switches);