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>
28 #include <linux/notifier.h>
29 #include <linux/cpu.h>
34 #define MY_PRINT_NAME "appldata" /* for debug messages, etc. */
35 #define APPLDATA_CPU_INTERVAL 10000 /* default (CPU) time for
39 #define TOD_MICRO 0x01000 /* nr. of TOD clock units
41 #ifndef CONFIG_ARCH_S390X
43 #define APPLDATA_START_INTERVAL_REC 0x00 /* Function codes for */
44 #define APPLDATA_STOP_REC 0x01 /* DIAG 0xDC */
45 #define APPLDATA_GEN_EVENT_RECORD 0x02
46 #define APPLDATA_START_CONFIG_REC 0x03
50 #define APPLDATA_START_INTERVAL_REC 0x80
51 #define APPLDATA_STOP_REC 0x81
52 #define APPLDATA_GEN_EVENT_RECORD 0x82
53 #define APPLDATA_START_CONFIG_REC 0x83
55 #endif /* CONFIG_ARCH_S390X */
59 * Parameter list for DIAGNOSE X'DC'
61 #ifndef CONFIG_ARCH_S390X
62 struct appldata_parameter_list {
63 u16 diag; /* The DIAGNOSE code X'00DC' */
64 u8 function; /* The function code for the DIAGNOSE */
65 u8 parlist_length; /* Length of the parameter list */
66 u32 product_id_addr; /* Address of the 16-byte product ID */
68 u16 buffer_length; /* Length of the application data buffer */
69 u32 buffer_addr; /* Address of the application data buffer */
72 struct appldata_parameter_list {
83 #endif /* CONFIG_ARCH_S390X */
86 * /proc entries (sysctl)
88 static const char appldata_proc_name[APPLDATA_PROC_NAME_LENGTH] = "appldata";
89 static int appldata_timer_handler(ctl_table *ctl, int write, struct file *filp,
90 void __user *buffer, size_t *lenp);
91 static int appldata_interval_handler(ctl_table *ctl, int write,
96 static struct ctl_table_header *appldata_sysctl_header;
97 static struct ctl_table appldata_table[] = {
99 .ctl_name = CTL_APPLDATA_TIMER,
101 .mode = S_IRUGO | S_IWUSR,
102 .proc_handler = &appldata_timer_handler,
105 .ctl_name = CTL_APPLDATA_INTERVAL,
106 .procname = "interval",
107 .mode = S_IRUGO | S_IWUSR,
108 .proc_handler = &appldata_interval_handler,
113 static struct ctl_table appldata_dir_table[] = {
115 .ctl_name = CTL_APPLDATA,
116 .procname = appldata_proc_name,
118 .mode = S_IRUGO | S_IXUGO,
119 .child = appldata_table,
127 DEFINE_PER_CPU(struct vtimer_list, appldata_timer);
128 static atomic_t appldata_expire_count = ATOMIC_INIT(0);
130 static spinlock_t appldata_timer_lock = SPIN_LOCK_UNLOCKED;
131 static int appldata_interval = APPLDATA_CPU_INTERVAL;
132 static int appldata_timer_active;
137 static struct tasklet_struct appldata_tasklet_struct;
142 static spinlock_t appldata_ops_lock = SPIN_LOCK_UNLOCKED;
143 static LIST_HEAD(appldata_ops_list);
146 /************************* timer, tasklet, DIAG ******************************/
148 * appldata_timer_function()
150 * schedule tasklet and reschedule timer
152 static void appldata_timer_function(unsigned long data, struct pt_regs *regs)
154 P_DEBUG(" -= Timer =-\n");
155 P_DEBUG("CPU: %i, expire_count: %i\n", smp_processor_id(),
156 atomic_read(&appldata_expire_count));
157 if (atomic_dec_and_test(&appldata_expire_count)) {
158 atomic_set(&appldata_expire_count, num_online_cpus());
159 tasklet_schedule((struct tasklet_struct *) data);
164 * appldata_tasklet_function()
166 * call data gathering function for each (active) module
168 static void appldata_tasklet_function(unsigned long data)
170 struct list_head *lh;
171 struct appldata_ops *ops;
174 P_DEBUG(" -= Tasklet =-\n");
176 spin_lock(&appldata_ops_lock);
177 list_for_each(lh, &appldata_ops_list) {
178 ops = list_entry(lh, struct appldata_ops, list);
179 P_DEBUG("list_for_each loop: %i) active = %u, name = %s\n",
180 ++i, ops->active, ops->name);
181 if (ops->active == 1) {
182 ops->callback(ops->data);
185 spin_unlock(&appldata_ops_lock);
191 * prepare parameter list, issue DIAG 0xDC
193 static int appldata_diag(char record_nr, u16 function, unsigned long buffer,
197 struct appldata_product_id {
198 char prod_nr[7]; /* product nr. */
199 char prod_fn[2]; /* product function */
200 char record_nr; /* record nr. */
201 char version_nr[2]; /* version */
202 char release_nr[2]; /* release */
203 char mod_lvl[2]; /* modification lvl. */
204 } appldata_product_id = {
205 /* all strings are EBCDIC, record_nr is byte */
206 .prod_nr = {0xD3, 0xC9, 0xD5, 0xE4,
207 0xE7, 0xD2, 0xD9}, /* "LINUXKR" */
208 .prod_fn = {0xD5, 0xD3}, /* "NL" */
209 .record_nr = record_nr,
210 .version_nr = {0xF2, 0xF6}, /* "26" */
211 .release_nr = {0xF0, 0xF1}, /* "01" */
212 .mod_lvl = {0xF0, 0xF0}, /* "00" */
214 struct appldata_parameter_list appldata_parameter_list = {
216 .function = function,
218 sizeof(appldata_parameter_list),
219 .buffer_length = length,
221 (unsigned long) &appldata_product_id,
222 .buffer_addr = virt_to_phys((void *) buffer)
229 "diag %1,%0,0xDC\n\t"
230 : "=d" (ry) : "d" (&(appldata_parameter_list)) : "cc");
233 /********************** timer, tasklet, DIAG <END> ***************************/
236 /****************************** /proc stuff **********************************/
239 * appldata_mod_vtimer_wrap()
241 * wrapper function for mod_virt_timer(), because smp_call_function_on()
242 * accepts only one parameter.
244 static void __appldata_mod_vtimer_wrap(void *p) {
246 struct vtimer_list *timer;
249 mod_virt_timer(args->timer, args->expires);
252 #define APPLDATA_ADD_TIMER 0
253 #define APPLDATA_DEL_TIMER 1
254 #define APPLDATA_MOD_TIMER 2
257 * __appldata_vtimer_setup()
259 * Add, delete or modify virtual timers on all online cpus.
260 * The caller needs to get the appldata_timer_lock spinlock.
263 __appldata_vtimer_setup(int cmd)
265 u64 per_cpu_interval;
269 case APPLDATA_ADD_TIMER:
270 if (appldata_timer_active)
272 per_cpu_interval = (u64) (appldata_interval*1000 /
273 num_online_cpus()) * TOD_MICRO;
274 for_each_online_cpu(i) {
275 per_cpu(appldata_timer, i).expires = per_cpu_interval;
276 smp_call_function_on(add_virt_timer_periodic,
277 &per_cpu(appldata_timer, i),
280 appldata_timer_active = 1;
281 P_INFO("Monitoring timer started.\n");
283 case APPLDATA_DEL_TIMER:
284 for_each_online_cpu(i)
285 del_virt_timer(&per_cpu(appldata_timer, i));
286 if (!appldata_timer_active)
288 appldata_timer_active = 0;
289 atomic_set(&appldata_expire_count, num_online_cpus());
290 P_INFO("Monitoring timer stopped.\n");
292 case APPLDATA_MOD_TIMER:
293 per_cpu_interval = (u64) (appldata_interval*1000 /
294 num_online_cpus()) * TOD_MICRO;
295 if (!appldata_timer_active)
297 for_each_online_cpu(i) {
299 struct vtimer_list *timer;
302 args.timer = &per_cpu(appldata_timer, i);
303 args.expires = per_cpu_interval;
304 smp_call_function_on(__appldata_mod_vtimer_wrap,
311 * appldata_timer_handler()
313 * Start/Stop timer, show status of timer (0 = not active, 1 = active)
316 appldata_timer_handler(ctl_table *ctl, int write, struct file *filp,
317 void __user *buffer, size_t *lenp)
322 if (!*lenp || filp->f_pos) {
327 len = sprintf(buf, appldata_timer_active ? "1\n" : "0\n");
330 if (copy_to_user(buffer, buf, len))
335 if (copy_from_user(buf, buffer, len > sizeof(buf) ? sizeof(buf) : len))
337 spin_lock(&appldata_timer_lock);
339 __appldata_vtimer_setup(APPLDATA_ADD_TIMER);
340 else if (buf[0] == '0')
341 __appldata_vtimer_setup(APPLDATA_DEL_TIMER);
342 spin_unlock(&appldata_timer_lock);
350 * appldata_interval_handler()
352 * Set (CPU) timer interval for collection of data (in milliseconds), show
353 * current timer interval.
356 appldata_interval_handler(ctl_table *ctl, int write, struct file *filp,
357 void __user *buffer, size_t *lenp)
362 if (!*lenp || filp->f_pos) {
367 len = sprintf(buf, "%i\n", appldata_interval);
370 if (copy_to_user(buffer, buf, len))
375 if (copy_from_user(buf, buffer, len > sizeof(buf) ? sizeof(buf) : len)) {
378 sscanf(buf, "%i", &interval);
380 P_ERROR("Timer CPU interval has to be > 0!\n");
384 spin_lock(&appldata_timer_lock);
385 appldata_interval = interval;
386 __appldata_vtimer_setup(APPLDATA_MOD_TIMER);
387 spin_unlock(&appldata_timer_lock);
389 P_INFO("Monitoring CPU interval set to %u milliseconds.\n",
398 * appldata_generic_handler()
400 * Generic start/stop monitoring and DIAG, show status of
401 * monitoring (0 = not in process, 1 = in process)
404 appldata_generic_handler(ctl_table *ctl, int write, struct file *filp,
405 void __user *buffer, size_t *lenp)
407 struct appldata_ops *ops = NULL, *tmp_ops;
410 struct list_head *lh;
413 spin_lock_bh(&appldata_ops_lock);
414 list_for_each(lh, &appldata_ops_list) {
415 tmp_ops = list_entry(lh, struct appldata_ops, list);
416 if (&tmp_ops->ctl_table[2] == ctl) {
421 spin_unlock_bh(&appldata_ops_lock);
425 if (!try_module_get(ops->owner)) { // protect this function
426 spin_unlock_bh(&appldata_ops_lock);
429 spin_unlock_bh(&appldata_ops_lock);
431 if (!*lenp || filp->f_pos) {
433 module_put(ops->owner);
437 len = sprintf(buf, ops->active ? "1\n" : "0\n");
440 if (copy_to_user(buffer, buf, len)) {
441 module_put(ops->owner);
447 if (copy_from_user(buf, buffer,
448 len > sizeof(buf) ? sizeof(buf) : len)) {
449 module_put(ops->owner);
453 spin_lock_bh(&appldata_ops_lock);
454 if ((buf[0] == '1') && (ops->active == 0)) {
455 if (!try_module_get(ops->owner)) { // protect tasklet
456 spin_unlock_bh(&appldata_ops_lock);
457 module_put(ops->owner);
461 ops->callback(ops->data); // init record
462 rc = appldata_diag(ops->record_nr,
463 APPLDATA_START_INTERVAL_REC,
464 (unsigned long) ops->data, ops->size);
466 P_ERROR("START DIAG 0xDC for %s failed, "
467 "return code: %d\n", ops->name, rc);
468 module_put(ops->owner);
471 P_INFO("Monitoring %s data enabled, "
472 "DIAG 0xDC started.\n", ops->name);
474 } else if ((buf[0] == '0') && (ops->active == 1)) {
476 rc = appldata_diag(ops->record_nr, APPLDATA_STOP_REC,
477 (unsigned long) ops->data, ops->size);
479 P_ERROR("STOP DIAG 0xDC for %s failed, "
480 "return code: %d\n", ops->name, rc);
482 P_INFO("Monitoring %s data disabled, "
483 "DIAG 0xDC stopped.\n", ops->name);
485 module_put(ops->owner);
487 spin_unlock_bh(&appldata_ops_lock);
491 module_put(ops->owner);
495 /*************************** /proc stuff <END> *******************************/
498 /************************* module-ops management *****************************/
500 * appldata_register_ops()
502 * update ops list, register /proc/sys entries
504 int appldata_register_ops(struct appldata_ops *ops)
506 struct list_head *lh;
507 struct appldata_ops *tmp_ops;
512 if ((ops->size > APPLDATA_MAX_REC_SIZE) ||
514 P_ERROR("Invalid size of %s record = %i, maximum = %i!\n",
515 ops->name, ops->size, APPLDATA_MAX_REC_SIZE);
518 if ((ops->ctl_nr == CTL_APPLDATA) ||
519 (ops->ctl_nr == CTL_APPLDATA_TIMER) ||
520 (ops->ctl_nr == CTL_APPLDATA_INTERVAL)) {
521 P_ERROR("ctl_nr %i already in use!\n", ops->ctl_nr);
524 ops->ctl_table = kmalloc(4*sizeof(struct ctl_table), GFP_KERNEL);
525 if (ops->ctl_table == NULL) {
526 P_ERROR("Not enough memory for %s ctl_table!\n", ops->name);
529 memset(ops->ctl_table, 0, 4*sizeof(struct ctl_table));
531 spin_lock_bh(&appldata_ops_lock);
532 list_for_each(lh, &appldata_ops_list) {
533 tmp_ops = list_entry(lh, struct appldata_ops, list);
534 P_DEBUG("register_ops loop: %i) name = %s, ctl = %i\n",
535 ++i, tmp_ops->name, tmp_ops->ctl_nr);
536 P_DEBUG("Comparing %s (ctl %i) with %s (ctl %i)\n",
537 tmp_ops->name, tmp_ops->ctl_nr, ops->name,
539 if (strncmp(tmp_ops->name, ops->name,
540 APPLDATA_PROC_NAME_LENGTH) == 0) {
541 P_ERROR("Name \"%s\" already registered!\n", ops->name);
542 kfree(ops->ctl_table);
543 spin_unlock_bh(&appldata_ops_lock);
546 if (tmp_ops->ctl_nr == ops->ctl_nr) {
547 P_ERROR("ctl_nr %i already registered!\n", ops->ctl_nr);
548 kfree(ops->ctl_table);
549 spin_unlock_bh(&appldata_ops_lock);
553 list_add(&ops->list, &appldata_ops_list);
554 spin_unlock_bh(&appldata_ops_lock);
556 ops->ctl_table[0].ctl_name = CTL_APPLDATA;
557 ops->ctl_table[0].procname = appldata_proc_name;
558 ops->ctl_table[0].maxlen = 0;
559 ops->ctl_table[0].mode = S_IRUGO | S_IXUGO;
560 ops->ctl_table[0].child = &ops->ctl_table[2];
562 ops->ctl_table[1].ctl_name = 0;
564 ops->ctl_table[2].ctl_name = ops->ctl_nr;
565 ops->ctl_table[2].procname = ops->name;
566 ops->ctl_table[2].mode = S_IRUGO | S_IWUSR;
567 ops->ctl_table[2].proc_handler = appldata_generic_handler;
568 ops->ctl_table[2].data = ops;
570 ops->ctl_table[3].ctl_name = 0;
572 ops->sysctl_header = register_sysctl_table(ops->ctl_table,1);
574 P_INFO("%s-ops registered!\n", ops->name);
579 * appldata_unregister_ops()
581 * update ops list, unregister /proc entries, stop DIAG if necessary
583 void appldata_unregister_ops(struct appldata_ops *ops)
585 spin_lock_bh(&appldata_ops_lock);
586 unregister_sysctl_table(ops->sysctl_header);
587 list_del(&ops->list);
588 kfree(ops->ctl_table);
589 ops->ctl_table = NULL;
590 spin_unlock_bh(&appldata_ops_lock);
591 P_INFO("%s-ops unregistered!\n", ops->name);
593 /********************** module-ops management <END> **************************/
596 /******************************* init / exit *********************************/
599 appldata_online_cpu(int cpu)
601 init_virt_timer(&per_cpu(appldata_timer, cpu));
602 per_cpu(appldata_timer, cpu).function = appldata_timer_function;
603 per_cpu(appldata_timer, cpu).data = (unsigned long)
604 &appldata_tasklet_struct;
605 atomic_inc(&appldata_expire_count);
606 spin_lock(&appldata_timer_lock);
607 __appldata_vtimer_setup(APPLDATA_MOD_TIMER);
608 spin_unlock(&appldata_timer_lock);
612 appldata_offline_cpu(int cpu)
614 del_virt_timer(&per_cpu(appldata_timer, cpu));
615 if (atomic_dec_and_test(&appldata_expire_count)) {
616 atomic_set(&appldata_expire_count, num_online_cpus());
617 tasklet_schedule(&appldata_tasklet_struct);
619 spin_lock(&appldata_timer_lock);
620 __appldata_vtimer_setup(APPLDATA_MOD_TIMER);
621 spin_unlock(&appldata_timer_lock);
625 appldata_cpu_notify(struct notifier_block *self,
626 unsigned long action, void *hcpu)
630 appldata_online_cpu((long) hcpu);
632 #ifdef CONFIG_HOTPLUG_CPU
634 appldata_offline_cpu((long) hcpu);
643 static struct notifier_block __devinitdata appldata_nb = {
644 .notifier_call = appldata_cpu_notify,
650 * init timer and tasklet, register /proc entries
652 static int __init appldata_init(void)
656 P_DEBUG("sizeof(parameter_list) = %lu\n",
657 sizeof(struct appldata_parameter_list));
659 for_each_online_cpu(i)
660 appldata_online_cpu(i);
662 /* Register cpu hotplug notifier */
663 register_cpu_notifier(&appldata_nb);
665 appldata_sysctl_header = register_sysctl_table(appldata_dir_table, 1);
667 appldata_dir_table[0].de->owner = THIS_MODULE;
668 appldata_table[0].de->owner = THIS_MODULE;
669 appldata_table[1].de->owner = THIS_MODULE;
672 tasklet_init(&appldata_tasklet_struct, appldata_tasklet_function, 0);
673 P_DEBUG("Base interface initialized.\n");
680 * stop timer and tasklet, unregister /proc entries
682 static void __exit appldata_exit(void)
684 struct list_head *lh;
685 struct appldata_ops *ops;
688 P_DEBUG("Unloading module ...\n");
690 * ops list should be empty, but just in case something went wrong...
692 spin_lock_bh(&appldata_ops_lock);
693 list_for_each(lh, &appldata_ops_list) {
694 ops = list_entry(lh, struct appldata_ops, list);
695 rc = appldata_diag(ops->record_nr, APPLDATA_STOP_REC,
696 (unsigned long) ops->data, ops->size);
698 P_ERROR("STOP DIAG 0xDC for %s failed, "
699 "return code: %d\n", ops->name, rc);
702 spin_unlock_bh(&appldata_ops_lock);
704 for_each_online_cpu(i)
705 appldata_offline_cpu(i);
707 appldata_timer_active = 0;
709 unregister_sysctl_table(appldata_sysctl_header);
711 tasklet_kill(&appldata_tasklet_struct);
712 P_DEBUG("... module unloaded!\n");
714 /**************************** init / exit <END> ******************************/
717 module_init(appldata_init);
718 module_exit(appldata_exit);
719 MODULE_LICENSE("GPL");
720 MODULE_AUTHOR("Gerald Schaefer");
721 MODULE_DESCRIPTION("Linux-VM Monitor Stream, base infrastructure");
723 EXPORT_SYMBOL_GPL(appldata_register_ops);
724 EXPORT_SYMBOL_GPL(appldata_unregister_ops);
728 * Kernel symbols needed by appldata_mem and appldata_os modules.
729 * However, if this file is compiled as a module (for testing only), these
730 * symbols are not exported. In this case, we define them locally and export
733 void si_swapinfo(struct sysinfo *val)
735 val->freeswap = -1ul;
736 val->totalswap = -1ul;
739 unsigned long avenrun[3] = {-1 - FIXED_1/200, -1 - FIXED_1/200,
743 void get_full_page_state(struct page_state *ps)
745 memset(ps, -1, sizeof(struct page_state));
748 unsigned long nr_running(void)
753 unsigned long nr_iowait(void)
758 /*unsigned long nr_context_switches(void)
763 EXPORT_SYMBOL_GPL(si_swapinfo);
764 EXPORT_SYMBOL_GPL(nr_threads);
765 EXPORT_SYMBOL_GPL(avenrun);
766 EXPORT_SYMBOL_GPL(get_full_page_state);
767 EXPORT_SYMBOL_GPL(nr_running);
768 EXPORT_SYMBOL_GPL(nr_iowait);
769 //EXPORT_SYMBOL_GPL(nr_context_switches);