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 IBM Corp. 2003, 2008
10 * Author: Gerald Schaefer <gerald.schaefer@de.ibm.com>
13 #define KMSG_COMPONENT "appldata"
14 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
16 #include <linux/module.h>
17 #include <linux/init.h>
18 #include <linux/slab.h>
19 #include <linux/errno.h>
20 #include <linux/interrupt.h>
21 #include <linux/proc_fs.h>
23 #include <linux/swap.h>
24 #include <linux/pagemap.h>
25 #include <linux/sysctl.h>
26 #include <linux/notifier.h>
27 #include <linux/cpu.h>
28 #include <linux/workqueue.h>
29 #include <asm/appldata.h>
30 #include <asm/timer.h>
31 #include <asm/uaccess.h>
38 #define APPLDATA_CPU_INTERVAL 10000 /* default (CPU) time for
42 #define TOD_MICRO 0x01000 /* nr. of TOD clock units
45 * /proc entries (sysctl)
47 static const char appldata_proc_name[APPLDATA_PROC_NAME_LENGTH] = "appldata";
48 static int appldata_timer_handler(ctl_table *ctl, int write, struct file *filp,
49 void __user *buffer, size_t *lenp, loff_t *ppos);
50 static int appldata_interval_handler(ctl_table *ctl, int write,
53 size_t *lenp, loff_t *ppos);
55 static struct ctl_table_header *appldata_sysctl_header;
56 static struct ctl_table appldata_table[] = {
59 .mode = S_IRUGO | S_IWUSR,
60 .proc_handler = &appldata_timer_handler,
63 .procname = "interval",
64 .mode = S_IRUGO | S_IWUSR,
65 .proc_handler = &appldata_interval_handler,
70 static struct ctl_table appldata_dir_table[] = {
72 .procname = appldata_proc_name,
74 .mode = S_IRUGO | S_IXUGO,
75 .child = appldata_table,
83 static DEFINE_PER_CPU(struct vtimer_list, appldata_timer);
84 static atomic_t appldata_expire_count = ATOMIC_INIT(0);
86 static DEFINE_SPINLOCK(appldata_timer_lock);
87 static int appldata_interval = APPLDATA_CPU_INTERVAL;
88 static int appldata_timer_active;
93 static struct workqueue_struct *appldata_wq;
94 static void appldata_work_fn(struct work_struct *work);
95 static DECLARE_WORK(appldata_work, appldata_work_fn);
101 static DEFINE_SPINLOCK(appldata_ops_lock);
102 static LIST_HEAD(appldata_ops_list);
105 /*************************** timer, work, DIAG *******************************/
107 * appldata_timer_function()
109 * schedule work and reschedule timer
111 static void appldata_timer_function(unsigned long data)
113 if (atomic_dec_and_test(&appldata_expire_count)) {
114 atomic_set(&appldata_expire_count, num_online_cpus());
115 queue_work(appldata_wq, (struct work_struct *) data);
122 * call data gathering function for each (active) module
124 static void appldata_work_fn(struct work_struct *work)
126 struct list_head *lh;
127 struct appldata_ops *ops;
132 spin_lock(&appldata_ops_lock);
133 list_for_each(lh, &appldata_ops_list) {
134 ops = list_entry(lh, struct appldata_ops, list);
135 if (ops->active == 1) {
136 ops->callback(ops->data);
139 spin_unlock(&appldata_ops_lock);
146 * prepare parameter list, issue DIAG 0xDC
148 int appldata_diag(char record_nr, u16 function, unsigned long buffer,
149 u16 length, char *mod_lvl)
151 struct appldata_product_id id = {
152 .prod_nr = {0xD3, 0xC9, 0xD5, 0xE4,
153 0xE7, 0xD2, 0xD9}, /* "LINUXKR" */
154 .prod_fn = 0xD5D3, /* "NL" */
155 .version_nr = 0xF2F6, /* "26" */
156 .release_nr = 0xF0F1, /* "01" */
159 id.record_nr = record_nr;
160 id.mod_lvl = (mod_lvl[0]) << 8 | mod_lvl[1];
161 return appldata_asm(&id, function, (void *) buffer, length);
163 /************************ timer, work, DIAG <END> ****************************/
166 /****************************** /proc stuff **********************************/
169 * appldata_mod_vtimer_wrap()
171 * wrapper function for mod_virt_timer(), because smp_call_function_single()
172 * accepts only one parameter.
174 static void __appldata_mod_vtimer_wrap(void *p) {
176 struct vtimer_list *timer;
179 mod_virt_timer_periodic(args->timer, args->expires);
182 #define APPLDATA_ADD_TIMER 0
183 #define APPLDATA_DEL_TIMER 1
184 #define APPLDATA_MOD_TIMER 2
187 * __appldata_vtimer_setup()
189 * Add, delete or modify virtual timers on all online cpus.
190 * The caller needs to get the appldata_timer_lock spinlock.
193 __appldata_vtimer_setup(int cmd)
195 u64 per_cpu_interval;
199 case APPLDATA_ADD_TIMER:
200 if (appldata_timer_active)
202 per_cpu_interval = (u64) (appldata_interval*1000 /
203 num_online_cpus()) * TOD_MICRO;
204 for_each_online_cpu(i) {
205 per_cpu(appldata_timer, i).expires = per_cpu_interval;
206 smp_call_function_single(i, add_virt_timer_periodic,
207 &per_cpu(appldata_timer, i),
210 appldata_timer_active = 1;
212 case APPLDATA_DEL_TIMER:
213 for_each_online_cpu(i)
214 del_virt_timer(&per_cpu(appldata_timer, i));
215 if (!appldata_timer_active)
217 appldata_timer_active = 0;
218 atomic_set(&appldata_expire_count, num_online_cpus());
220 case APPLDATA_MOD_TIMER:
221 per_cpu_interval = (u64) (appldata_interval*1000 /
222 num_online_cpus()) * TOD_MICRO;
223 if (!appldata_timer_active)
225 for_each_online_cpu(i) {
227 struct vtimer_list *timer;
230 args.timer = &per_cpu(appldata_timer, i);
231 args.expires = per_cpu_interval;
232 smp_call_function_single(i, __appldata_mod_vtimer_wrap,
239 * appldata_timer_handler()
241 * Start/Stop timer, show status of timer (0 = not active, 1 = active)
244 appldata_timer_handler(ctl_table *ctl, int write, struct file *filp,
245 void __user *buffer, size_t *lenp, loff_t *ppos)
250 if (!*lenp || *ppos) {
255 len = sprintf(buf, appldata_timer_active ? "1\n" : "0\n");
258 if (copy_to_user(buffer, buf, len))
263 if (copy_from_user(buf, buffer, len > sizeof(buf) ? sizeof(buf) : len))
266 spin_lock(&appldata_timer_lock);
268 __appldata_vtimer_setup(APPLDATA_ADD_TIMER);
269 else if (buf[0] == '0')
270 __appldata_vtimer_setup(APPLDATA_DEL_TIMER);
271 spin_unlock(&appldata_timer_lock);
280 * appldata_interval_handler()
282 * Set (CPU) timer interval for collection of data (in milliseconds), show
283 * current timer interval.
286 appldata_interval_handler(ctl_table *ctl, int write, struct file *filp,
287 void __user *buffer, size_t *lenp, loff_t *ppos)
292 if (!*lenp || *ppos) {
297 len = sprintf(buf, "%i\n", appldata_interval);
300 if (copy_to_user(buffer, buf, len))
305 if (copy_from_user(buf, buffer, len > sizeof(buf) ? sizeof(buf) : len)) {
309 sscanf(buf, "%i", &interval);
314 spin_lock(&appldata_timer_lock);
315 appldata_interval = interval;
316 __appldata_vtimer_setup(APPLDATA_MOD_TIMER);
317 spin_unlock(&appldata_timer_lock);
326 * appldata_generic_handler()
328 * Generic start/stop monitoring and DIAG, show status of
329 * monitoring (0 = not in process, 1 = in process)
332 appldata_generic_handler(ctl_table *ctl, int write, struct file *filp,
333 void __user *buffer, size_t *lenp, loff_t *ppos)
335 struct appldata_ops *ops = NULL, *tmp_ops;
338 struct list_head *lh;
341 spin_lock(&appldata_ops_lock);
342 list_for_each(lh, &appldata_ops_list) {
343 tmp_ops = list_entry(lh, struct appldata_ops, list);
344 if (&tmp_ops->ctl_table[2] == ctl) {
349 spin_unlock(&appldata_ops_lock);
353 if (!try_module_get(ops->owner)) { // protect this function
354 spin_unlock(&appldata_ops_lock);
357 spin_unlock(&appldata_ops_lock);
359 if (!*lenp || *ppos) {
361 module_put(ops->owner);
365 len = sprintf(buf, ops->active ? "1\n" : "0\n");
368 if (copy_to_user(buffer, buf, len)) {
369 module_put(ops->owner);
375 if (copy_from_user(buf, buffer,
376 len > sizeof(buf) ? sizeof(buf) : len)) {
377 module_put(ops->owner);
381 spin_lock(&appldata_ops_lock);
382 if ((buf[0] == '1') && (ops->active == 0)) {
383 // protect work queue callback
384 if (!try_module_get(ops->owner)) {
385 spin_unlock(&appldata_ops_lock);
386 module_put(ops->owner);
389 ops->callback(ops->data); // init record
390 rc = appldata_diag(ops->record_nr,
391 APPLDATA_START_INTERVAL_REC,
392 (unsigned long) ops->data, ops->size,
395 pr_err("Starting the data collection for %s "
396 "failed with rc=%d\n", ops->name, rc);
397 module_put(ops->owner);
400 } else if ((buf[0] == '0') && (ops->active == 1)) {
402 rc = appldata_diag(ops->record_nr, APPLDATA_STOP_REC,
403 (unsigned long) ops->data, ops->size,
406 pr_err("Stopping the data collection for %s "
407 "failed with rc=%d\n", ops->name, rc);
408 module_put(ops->owner);
410 spin_unlock(&appldata_ops_lock);
414 module_put(ops->owner);
418 /*************************** /proc stuff <END> *******************************/
421 /************************* module-ops management *****************************/
423 * appldata_register_ops()
425 * update ops list, register /proc/sys entries
427 int appldata_register_ops(struct appldata_ops *ops)
429 if (ops->size > APPLDATA_MAX_REC_SIZE)
432 ops->ctl_table = kzalloc(4 * sizeof(struct ctl_table), GFP_KERNEL);
436 spin_lock(&appldata_ops_lock);
437 list_add(&ops->list, &appldata_ops_list);
438 spin_unlock(&appldata_ops_lock);
440 ops->ctl_table[0].procname = appldata_proc_name;
441 ops->ctl_table[0].maxlen = 0;
442 ops->ctl_table[0].mode = S_IRUGO | S_IXUGO;
443 ops->ctl_table[0].child = &ops->ctl_table[2];
445 ops->ctl_table[2].procname = ops->name;
446 ops->ctl_table[2].mode = S_IRUGO | S_IWUSR;
447 ops->ctl_table[2].proc_handler = appldata_generic_handler;
448 ops->ctl_table[2].data = ops;
450 ops->sysctl_header = register_sysctl_table(ops->ctl_table);
451 if (!ops->sysctl_header)
455 spin_lock(&appldata_ops_lock);
456 list_del(&ops->list);
457 spin_unlock(&appldata_ops_lock);
458 kfree(ops->ctl_table);
463 * appldata_unregister_ops()
465 * update ops list, unregister /proc entries, stop DIAG if necessary
467 void appldata_unregister_ops(struct appldata_ops *ops)
469 spin_lock(&appldata_ops_lock);
470 list_del(&ops->list);
471 spin_unlock(&appldata_ops_lock);
472 unregister_sysctl_table(ops->sysctl_header);
473 kfree(ops->ctl_table);
475 /********************** module-ops management <END> **************************/
478 /******************************* init / exit *********************************/
480 static void __cpuinit appldata_online_cpu(int cpu)
482 init_virt_timer(&per_cpu(appldata_timer, cpu));
483 per_cpu(appldata_timer, cpu).function = appldata_timer_function;
484 per_cpu(appldata_timer, cpu).data = (unsigned long)
486 atomic_inc(&appldata_expire_count);
487 spin_lock(&appldata_timer_lock);
488 __appldata_vtimer_setup(APPLDATA_MOD_TIMER);
489 spin_unlock(&appldata_timer_lock);
492 static void __cpuinit appldata_offline_cpu(int cpu)
494 del_virt_timer(&per_cpu(appldata_timer, cpu));
495 if (atomic_dec_and_test(&appldata_expire_count)) {
496 atomic_set(&appldata_expire_count, num_online_cpus());
497 queue_work(appldata_wq, &appldata_work);
499 spin_lock(&appldata_timer_lock);
500 __appldata_vtimer_setup(APPLDATA_MOD_TIMER);
501 spin_unlock(&appldata_timer_lock);
504 static int __cpuinit appldata_cpu_notify(struct notifier_block *self,
505 unsigned long action,
510 case CPU_ONLINE_FROZEN:
511 appldata_online_cpu((long) hcpu);
514 case CPU_DEAD_FROZEN:
515 appldata_offline_cpu((long) hcpu);
523 static struct notifier_block __cpuinitdata appldata_nb = {
524 .notifier_call = appldata_cpu_notify,
530 * init timer, register /proc entries
532 static int __init appldata_init(void)
536 appldata_wq = create_singlethread_workqueue("appldata");
541 for_each_online_cpu(i)
542 appldata_online_cpu(i);
545 /* Register cpu hotplug notifier */
546 register_hotcpu_notifier(&appldata_nb);
548 appldata_sysctl_header = register_sysctl_table(appldata_dir_table);
552 __initcall(appldata_init);
554 /**************************** init / exit <END> ******************************/
556 EXPORT_SYMBOL_GPL(appldata_register_ops);
557 EXPORT_SYMBOL_GPL(appldata_unregister_ops);
558 EXPORT_SYMBOL_GPL(appldata_diag);
561 EXPORT_SYMBOL_GPL(si_swapinfo);
563 EXPORT_SYMBOL_GPL(nr_threads);
564 EXPORT_SYMBOL_GPL(nr_running);
565 EXPORT_SYMBOL_GPL(nr_iowait);