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,2006 IBM Corporation, IBM Deutschland Entwicklung GmbH.
10 * Author: Gerald Schaefer <gerald.schaefer@de.ibm.com>
13 #include <linux/module.h>
14 #include <linux/init.h>
15 #include <linux/slab.h>
16 #include <linux/errno.h>
17 #include <linux/interrupt.h>
18 #include <linux/proc_fs.h>
20 #include <linux/swap.h>
21 #include <linux/pagemap.h>
22 #include <linux/sysctl.h>
23 #include <linux/notifier.h>
24 #include <linux/cpu.h>
25 #include <linux/workqueue.h>
26 #include <asm/appldata.h>
27 #include <asm/timer.h>
28 #include <asm/uaccess.h>
35 #define MY_PRINT_NAME "appldata" /* for debug messages, etc. */
36 #define APPLDATA_CPU_INTERVAL 10000 /* default (CPU) time for
40 #define TOD_MICRO 0x01000 /* nr. of TOD clock units
43 * /proc entries (sysctl)
45 static const char appldata_proc_name[APPLDATA_PROC_NAME_LENGTH] = "appldata";
46 static int appldata_timer_handler(ctl_table *ctl, int write, struct file *filp,
47 void __user *buffer, size_t *lenp, loff_t *ppos);
48 static int appldata_interval_handler(ctl_table *ctl, int write,
51 size_t *lenp, loff_t *ppos);
53 static struct ctl_table_header *appldata_sysctl_header;
54 static struct ctl_table appldata_table[] = {
56 .ctl_name = CTL_APPLDATA_TIMER,
58 .mode = S_IRUGO | S_IWUSR,
59 .proc_handler = &appldata_timer_handler,
62 .ctl_name = CTL_APPLDATA_INTERVAL,
63 .procname = "interval",
64 .mode = S_IRUGO | S_IWUSR,
65 .proc_handler = &appldata_interval_handler,
70 static struct ctl_table appldata_dir_table[] = {
72 .ctl_name = CTL_APPLDATA,
73 .procname = appldata_proc_name,
75 .mode = S_IRUGO | S_IXUGO,
76 .child = appldata_table,
84 static DEFINE_PER_CPU(struct vtimer_list, appldata_timer);
85 static atomic_t appldata_expire_count = ATOMIC_INIT(0);
87 static DEFINE_SPINLOCK(appldata_timer_lock);
88 static int appldata_interval = APPLDATA_CPU_INTERVAL;
89 static int appldata_timer_active;
94 static struct workqueue_struct *appldata_wq;
95 static void appldata_work_fn(struct work_struct *work);
96 static DECLARE_WORK(appldata_work, appldata_work_fn);
102 static DEFINE_SPINLOCK(appldata_ops_lock);
103 static LIST_HEAD(appldata_ops_list);
106 /*************************** timer, work, DIAG *******************************/
108 * appldata_timer_function()
110 * schedule work and reschedule timer
112 static void appldata_timer_function(unsigned long data)
114 P_DEBUG(" -= Timer =-\n");
115 P_DEBUG("CPU: %i, expire_count: %i\n", smp_processor_id(),
116 atomic_read(&appldata_expire_count));
117 if (atomic_dec_and_test(&appldata_expire_count)) {
118 atomic_set(&appldata_expire_count, num_online_cpus());
119 queue_work(appldata_wq, (struct work_struct *) data);
126 * call data gathering function for each (active) module
128 static void appldata_work_fn(struct work_struct *work)
130 struct list_head *lh;
131 struct appldata_ops *ops;
134 P_DEBUG(" -= Work Queue =-\n");
136 spin_lock(&appldata_ops_lock);
137 list_for_each(lh, &appldata_ops_list) {
138 ops = list_entry(lh, struct appldata_ops, list);
139 P_DEBUG("list_for_each loop: %i) active = %u, name = %s\n",
140 ++i, ops->active, ops->name);
141 if (ops->active == 1) {
142 ops->callback(ops->data);
145 spin_unlock(&appldata_ops_lock);
151 * prepare parameter list, issue DIAG 0xDC
153 int appldata_diag(char record_nr, u16 function, unsigned long buffer,
154 u16 length, char *mod_lvl)
156 struct appldata_product_id id = {
157 .prod_nr = {0xD3, 0xC9, 0xD5, 0xE4,
158 0xE7, 0xD2, 0xD9}, /* "LINUXKR" */
159 .prod_fn = 0xD5D3, /* "NL" */
160 .version_nr = 0xF2F6, /* "26" */
161 .release_nr = 0xF0F1, /* "01" */
164 id.record_nr = record_nr;
165 id.mod_lvl = (mod_lvl[0]) << 8 | mod_lvl[1];
166 return appldata_asm(&id, function, (void *) buffer, length);
168 /************************ timer, work, DIAG <END> ****************************/
171 /****************************** /proc stuff **********************************/
174 * appldata_mod_vtimer_wrap()
176 * wrapper function for mod_virt_timer(), because smp_call_function_on()
177 * accepts only one parameter.
179 static void __appldata_mod_vtimer_wrap(void *p) {
181 struct vtimer_list *timer;
184 mod_virt_timer(args->timer, args->expires);
187 #define APPLDATA_ADD_TIMER 0
188 #define APPLDATA_DEL_TIMER 1
189 #define APPLDATA_MOD_TIMER 2
192 * __appldata_vtimer_setup()
194 * Add, delete or modify virtual timers on all online cpus.
195 * The caller needs to get the appldata_timer_lock spinlock.
198 __appldata_vtimer_setup(int cmd)
200 u64 per_cpu_interval;
204 case APPLDATA_ADD_TIMER:
205 if (appldata_timer_active)
207 per_cpu_interval = (u64) (appldata_interval*1000 /
208 num_online_cpus()) * TOD_MICRO;
209 for_each_online_cpu(i) {
210 per_cpu(appldata_timer, i).expires = per_cpu_interval;
211 smp_call_function_on(add_virt_timer_periodic,
212 &per_cpu(appldata_timer, i),
215 appldata_timer_active = 1;
216 P_INFO("Monitoring timer started.\n");
218 case APPLDATA_DEL_TIMER:
219 for_each_online_cpu(i)
220 del_virt_timer(&per_cpu(appldata_timer, i));
221 if (!appldata_timer_active)
223 appldata_timer_active = 0;
224 atomic_set(&appldata_expire_count, num_online_cpus());
225 P_INFO("Monitoring timer stopped.\n");
227 case APPLDATA_MOD_TIMER:
228 per_cpu_interval = (u64) (appldata_interval*1000 /
229 num_online_cpus()) * TOD_MICRO;
230 if (!appldata_timer_active)
232 for_each_online_cpu(i) {
234 struct vtimer_list *timer;
237 args.timer = &per_cpu(appldata_timer, i);
238 args.expires = per_cpu_interval;
239 smp_call_function_on(__appldata_mod_vtimer_wrap,
246 * appldata_timer_handler()
248 * Start/Stop timer, show status of timer (0 = not active, 1 = active)
251 appldata_timer_handler(ctl_table *ctl, int write, struct file *filp,
252 void __user *buffer, size_t *lenp, loff_t *ppos)
257 if (!*lenp || *ppos) {
262 len = sprintf(buf, appldata_timer_active ? "1\n" : "0\n");
265 if (copy_to_user(buffer, buf, len))
270 if (copy_from_user(buf, buffer, len > sizeof(buf) ? sizeof(buf) : len))
272 spin_lock(&appldata_timer_lock);
274 __appldata_vtimer_setup(APPLDATA_ADD_TIMER);
275 else if (buf[0] == '0')
276 __appldata_vtimer_setup(APPLDATA_DEL_TIMER);
277 spin_unlock(&appldata_timer_lock);
285 * appldata_interval_handler()
287 * Set (CPU) timer interval for collection of data (in milliseconds), show
288 * current timer interval.
291 appldata_interval_handler(ctl_table *ctl, int write, struct file *filp,
292 void __user *buffer, size_t *lenp, loff_t *ppos)
297 if (!*lenp || *ppos) {
302 len = sprintf(buf, "%i\n", appldata_interval);
305 if (copy_to_user(buffer, buf, len))
310 if (copy_from_user(buf, buffer, len > sizeof(buf) ? sizeof(buf) : len)) {
314 sscanf(buf, "%i", &interval);
316 P_ERROR("Timer CPU interval has to be > 0!\n");
320 spin_lock(&appldata_timer_lock);
321 appldata_interval = interval;
322 __appldata_vtimer_setup(APPLDATA_MOD_TIMER);
323 spin_unlock(&appldata_timer_lock);
325 P_INFO("Monitoring CPU interval set to %u milliseconds.\n",
334 * appldata_generic_handler()
336 * Generic start/stop monitoring and DIAG, show status of
337 * monitoring (0 = not in process, 1 = in process)
340 appldata_generic_handler(ctl_table *ctl, int write, struct file *filp,
341 void __user *buffer, size_t *lenp, loff_t *ppos)
343 struct appldata_ops *ops = NULL, *tmp_ops;
346 struct list_head *lh;
349 spin_lock(&appldata_ops_lock);
350 list_for_each(lh, &appldata_ops_list) {
351 tmp_ops = list_entry(lh, struct appldata_ops, list);
352 if (&tmp_ops->ctl_table[2] == ctl) {
357 spin_unlock(&appldata_ops_lock);
361 if (!try_module_get(ops->owner)) { // protect this function
362 spin_unlock(&appldata_ops_lock);
365 spin_unlock(&appldata_ops_lock);
367 if (!*lenp || *ppos) {
369 module_put(ops->owner);
373 len = sprintf(buf, ops->active ? "1\n" : "0\n");
376 if (copy_to_user(buffer, buf, len)) {
377 module_put(ops->owner);
383 if (copy_from_user(buf, buffer,
384 len > sizeof(buf) ? sizeof(buf) : len)) {
385 module_put(ops->owner);
389 spin_lock(&appldata_ops_lock);
390 if ((buf[0] == '1') && (ops->active == 0)) {
391 // protect work queue callback
392 if (!try_module_get(ops->owner)) {
393 spin_unlock(&appldata_ops_lock);
394 module_put(ops->owner);
397 ops->callback(ops->data); // init record
398 rc = appldata_diag(ops->record_nr,
399 APPLDATA_START_INTERVAL_REC,
400 (unsigned long) ops->data, ops->size,
403 P_ERROR("START DIAG 0xDC for %s failed, "
404 "return code: %d\n", ops->name, rc);
405 module_put(ops->owner);
407 P_INFO("Monitoring %s data enabled, "
408 "DIAG 0xDC started.\n", ops->name);
411 } else if ((buf[0] == '0') && (ops->active == 1)) {
413 rc = appldata_diag(ops->record_nr, APPLDATA_STOP_REC,
414 (unsigned long) ops->data, ops->size,
417 P_ERROR("STOP DIAG 0xDC for %s failed, "
418 "return code: %d\n", ops->name, rc);
420 P_INFO("Monitoring %s data disabled, "
421 "DIAG 0xDC stopped.\n", ops->name);
423 module_put(ops->owner);
425 spin_unlock(&appldata_ops_lock);
429 module_put(ops->owner);
433 /*************************** /proc stuff <END> *******************************/
436 /************************* module-ops management *****************************/
438 * appldata_register_ops()
440 * update ops list, register /proc/sys entries
442 int appldata_register_ops(struct appldata_ops *ops)
444 struct list_head *lh;
445 struct appldata_ops *tmp_ops;
450 if ((ops->size > APPLDATA_MAX_REC_SIZE) ||
452 P_ERROR("Invalid size of %s record = %i, maximum = %i!\n",
453 ops->name, ops->size, APPLDATA_MAX_REC_SIZE);
456 if ((ops->ctl_nr == CTL_APPLDATA) ||
457 (ops->ctl_nr == CTL_APPLDATA_TIMER) ||
458 (ops->ctl_nr == CTL_APPLDATA_INTERVAL)) {
459 P_ERROR("ctl_nr %i already in use!\n", ops->ctl_nr);
462 ops->ctl_table = kzalloc(4*sizeof(struct ctl_table), GFP_KERNEL);
463 if (ops->ctl_table == NULL) {
464 P_ERROR("Not enough memory for %s ctl_table!\n", ops->name);
468 spin_lock(&appldata_ops_lock);
469 list_for_each(lh, &appldata_ops_list) {
470 tmp_ops = list_entry(lh, struct appldata_ops, list);
471 P_DEBUG("register_ops loop: %i) name = %s, ctl = %i\n",
472 ++i, tmp_ops->name, tmp_ops->ctl_nr);
473 P_DEBUG("Comparing %s (ctl %i) with %s (ctl %i)\n",
474 tmp_ops->name, tmp_ops->ctl_nr, ops->name,
476 if (strncmp(tmp_ops->name, ops->name,
477 APPLDATA_PROC_NAME_LENGTH) == 0) {
478 P_ERROR("Name \"%s\" already registered!\n", ops->name);
479 kfree(ops->ctl_table);
480 spin_unlock(&appldata_ops_lock);
483 if (tmp_ops->ctl_nr == ops->ctl_nr) {
484 P_ERROR("ctl_nr %i already registered!\n", ops->ctl_nr);
485 kfree(ops->ctl_table);
486 spin_unlock(&appldata_ops_lock);
490 list_add(&ops->list, &appldata_ops_list);
491 spin_unlock(&appldata_ops_lock);
493 ops->ctl_table[0].ctl_name = CTL_APPLDATA;
494 ops->ctl_table[0].procname = appldata_proc_name;
495 ops->ctl_table[0].maxlen = 0;
496 ops->ctl_table[0].mode = S_IRUGO | S_IXUGO;
497 ops->ctl_table[0].child = &ops->ctl_table[2];
499 ops->ctl_table[1].ctl_name = 0;
501 ops->ctl_table[2].ctl_name = ops->ctl_nr;
502 ops->ctl_table[2].procname = ops->name;
503 ops->ctl_table[2].mode = S_IRUGO | S_IWUSR;
504 ops->ctl_table[2].proc_handler = appldata_generic_handler;
505 ops->ctl_table[2].data = ops;
507 ops->ctl_table[3].ctl_name = 0;
509 ops->sysctl_header = register_sysctl_table(ops->ctl_table);
511 P_INFO("%s-ops registered!\n", ops->name);
516 * appldata_unregister_ops()
518 * update ops list, unregister /proc entries, stop DIAG if necessary
520 void appldata_unregister_ops(struct appldata_ops *ops)
523 spin_lock(&appldata_ops_lock);
524 list_del(&ops->list);
525 /* at that point any incoming access will fail */
526 table = ops->ctl_table;
527 ops->ctl_table = NULL;
528 spin_unlock(&appldata_ops_lock);
529 unregister_sysctl_table(ops->sysctl_header);
531 P_INFO("%s-ops unregistered!\n", ops->name);
533 /********************** module-ops management <END> **************************/
536 /******************************* init / exit *********************************/
539 appldata_online_cpu(int cpu)
541 init_virt_timer(&per_cpu(appldata_timer, cpu));
542 per_cpu(appldata_timer, cpu).function = appldata_timer_function;
543 per_cpu(appldata_timer, cpu).data = (unsigned long)
545 atomic_inc(&appldata_expire_count);
546 spin_lock(&appldata_timer_lock);
547 __appldata_vtimer_setup(APPLDATA_MOD_TIMER);
548 spin_unlock(&appldata_timer_lock);
552 appldata_offline_cpu(int cpu)
554 del_virt_timer(&per_cpu(appldata_timer, cpu));
555 if (atomic_dec_and_test(&appldata_expire_count)) {
556 atomic_set(&appldata_expire_count, num_online_cpus());
557 queue_work(appldata_wq, &appldata_work);
559 spin_lock(&appldata_timer_lock);
560 __appldata_vtimer_setup(APPLDATA_MOD_TIMER);
561 spin_unlock(&appldata_timer_lock);
565 appldata_cpu_notify(struct notifier_block *self,
566 unsigned long action, void *hcpu)
570 case CPU_ONLINE_FROZEN:
571 appldata_online_cpu((long) hcpu);
574 case CPU_DEAD_FROZEN:
575 appldata_offline_cpu((long) hcpu);
583 static struct notifier_block appldata_nb = {
584 .notifier_call = appldata_cpu_notify,
590 * init timer, register /proc entries
592 static int __init appldata_init(void)
596 P_DEBUG("sizeof(parameter_list) = %lu\n",
597 sizeof(struct appldata_parameter_list));
599 appldata_wq = create_singlethread_workqueue("appldata");
601 P_ERROR("Could not create work queue\n");
605 for_each_online_cpu(i)
606 appldata_online_cpu(i);
608 /* Register cpu hotplug notifier */
609 register_hotcpu_notifier(&appldata_nb);
611 appldata_sysctl_header = register_sysctl_table(appldata_dir_table);
613 appldata_dir_table[0].de->owner = THIS_MODULE;
614 appldata_table[0].de->owner = THIS_MODULE;
615 appldata_table[1].de->owner = THIS_MODULE;
618 P_DEBUG("Base interface initialized.\n");
625 * stop timer, unregister /proc entries
627 static void __exit appldata_exit(void)
629 struct list_head *lh;
630 struct appldata_ops *ops;
633 P_DEBUG("Unloading module ...\n");
635 * ops list should be empty, but just in case something went wrong...
637 spin_lock(&appldata_ops_lock);
638 list_for_each(lh, &appldata_ops_list) {
639 ops = list_entry(lh, struct appldata_ops, list);
640 rc = appldata_diag(ops->record_nr, APPLDATA_STOP_REC,
641 (unsigned long) ops->data, ops->size,
644 P_ERROR("STOP DIAG 0xDC for %s failed, "
645 "return code: %d\n", ops->name, rc);
648 spin_unlock(&appldata_ops_lock);
650 for_each_online_cpu(i)
651 appldata_offline_cpu(i);
653 appldata_timer_active = 0;
655 unregister_sysctl_table(appldata_sysctl_header);
657 destroy_workqueue(appldata_wq);
658 P_DEBUG("... module unloaded!\n");
660 /**************************** init / exit <END> ******************************/
663 module_init(appldata_init);
664 module_exit(appldata_exit);
665 MODULE_LICENSE("GPL");
666 MODULE_AUTHOR("Gerald Schaefer");
667 MODULE_DESCRIPTION("Linux-VM Monitor Stream, base infrastructure");
669 EXPORT_SYMBOL_GPL(appldata_register_ops);
670 EXPORT_SYMBOL_GPL(appldata_unregister_ops);
671 EXPORT_SYMBOL_GPL(appldata_diag);
673 EXPORT_SYMBOL_GPL(si_swapinfo);
674 EXPORT_SYMBOL_GPL(nr_threads);
675 EXPORT_SYMBOL_GPL(nr_running);
676 EXPORT_SYMBOL_GPL(nr_iowait);