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 #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[] = {
57 .mode = S_IRUGO | S_IWUSR,
58 .proc_handler = &appldata_timer_handler,
61 .procname = "interval",
62 .mode = S_IRUGO | S_IWUSR,
63 .proc_handler = &appldata_interval_handler,
68 static struct ctl_table appldata_dir_table[] = {
70 .procname = appldata_proc_name,
72 .mode = S_IRUGO | S_IXUGO,
73 .child = appldata_table,
81 static DEFINE_PER_CPU(struct vtimer_list, appldata_timer);
82 static atomic_t appldata_expire_count = ATOMIC_INIT(0);
84 static DEFINE_SPINLOCK(appldata_timer_lock);
85 static int appldata_interval = APPLDATA_CPU_INTERVAL;
86 static int appldata_timer_active;
91 static struct workqueue_struct *appldata_wq;
92 static void appldata_work_fn(struct work_struct *work);
93 static DECLARE_WORK(appldata_work, appldata_work_fn);
99 static DEFINE_SPINLOCK(appldata_ops_lock);
100 static LIST_HEAD(appldata_ops_list);
103 /*************************** timer, work, DIAG *******************************/
105 * appldata_timer_function()
107 * schedule work and reschedule timer
109 static void appldata_timer_function(unsigned long data)
111 if (atomic_dec_and_test(&appldata_expire_count)) {
112 atomic_set(&appldata_expire_count, num_online_cpus());
113 queue_work(appldata_wq, (struct work_struct *) data);
120 * call data gathering function for each (active) module
122 static void appldata_work_fn(struct work_struct *work)
124 struct list_head *lh;
125 struct appldata_ops *ops;
130 spin_lock(&appldata_ops_lock);
131 list_for_each(lh, &appldata_ops_list) {
132 ops = list_entry(lh, struct appldata_ops, list);
133 if (ops->active == 1) {
134 ops->callback(ops->data);
137 spin_unlock(&appldata_ops_lock);
144 * prepare parameter list, issue DIAG 0xDC
146 int appldata_diag(char record_nr, u16 function, unsigned long buffer,
147 u16 length, char *mod_lvl)
149 struct appldata_product_id id = {
150 .prod_nr = {0xD3, 0xC9, 0xD5, 0xE4,
151 0xE7, 0xD2, 0xD9}, /* "LINUXKR" */
152 .prod_fn = 0xD5D3, /* "NL" */
153 .version_nr = 0xF2F6, /* "26" */
154 .release_nr = 0xF0F1, /* "01" */
157 id.record_nr = record_nr;
158 id.mod_lvl = (mod_lvl[0]) << 8 | mod_lvl[1];
159 return appldata_asm(&id, function, (void *) buffer, length);
161 /************************ timer, work, DIAG <END> ****************************/
164 /****************************** /proc stuff **********************************/
167 * appldata_mod_vtimer_wrap()
169 * wrapper function for mod_virt_timer(), because smp_call_function_single()
170 * accepts only one parameter.
172 static void __appldata_mod_vtimer_wrap(void *p) {
174 struct vtimer_list *timer;
177 mod_virt_timer(args->timer, args->expires);
180 #define APPLDATA_ADD_TIMER 0
181 #define APPLDATA_DEL_TIMER 1
182 #define APPLDATA_MOD_TIMER 2
185 * __appldata_vtimer_setup()
187 * Add, delete or modify virtual timers on all online cpus.
188 * The caller needs to get the appldata_timer_lock spinlock.
191 __appldata_vtimer_setup(int cmd)
193 u64 per_cpu_interval;
197 case APPLDATA_ADD_TIMER:
198 if (appldata_timer_active)
200 per_cpu_interval = (u64) (appldata_interval*1000 /
201 num_online_cpus()) * TOD_MICRO;
202 for_each_online_cpu(i) {
203 per_cpu(appldata_timer, i).expires = per_cpu_interval;
204 smp_call_function_single(i, add_virt_timer_periodic,
205 &per_cpu(appldata_timer, i),
208 appldata_timer_active = 1;
210 case APPLDATA_DEL_TIMER:
211 for_each_online_cpu(i)
212 del_virt_timer(&per_cpu(appldata_timer, i));
213 if (!appldata_timer_active)
215 appldata_timer_active = 0;
216 atomic_set(&appldata_expire_count, num_online_cpus());
218 case APPLDATA_MOD_TIMER:
219 per_cpu_interval = (u64) (appldata_interval*1000 /
220 num_online_cpus()) * TOD_MICRO;
221 if (!appldata_timer_active)
223 for_each_online_cpu(i) {
225 struct vtimer_list *timer;
228 args.timer = &per_cpu(appldata_timer, i);
229 args.expires = per_cpu_interval;
230 smp_call_function_single(i, __appldata_mod_vtimer_wrap,
237 * appldata_timer_handler()
239 * Start/Stop timer, show status of timer (0 = not active, 1 = active)
242 appldata_timer_handler(ctl_table *ctl, int write, struct file *filp,
243 void __user *buffer, size_t *lenp, loff_t *ppos)
248 if (!*lenp || *ppos) {
253 len = sprintf(buf, appldata_timer_active ? "1\n" : "0\n");
256 if (copy_to_user(buffer, buf, len))
261 if (copy_from_user(buf, buffer, len > sizeof(buf) ? sizeof(buf) : len))
264 spin_lock(&appldata_timer_lock);
266 __appldata_vtimer_setup(APPLDATA_ADD_TIMER);
267 else if (buf[0] == '0')
268 __appldata_vtimer_setup(APPLDATA_DEL_TIMER);
269 spin_unlock(&appldata_timer_lock);
278 * appldata_interval_handler()
280 * Set (CPU) timer interval for collection of data (in milliseconds), show
281 * current timer interval.
284 appldata_interval_handler(ctl_table *ctl, int write, struct file *filp,
285 void __user *buffer, size_t *lenp, loff_t *ppos)
290 if (!*lenp || *ppos) {
295 len = sprintf(buf, "%i\n", appldata_interval);
298 if (copy_to_user(buffer, buf, len))
303 if (copy_from_user(buf, buffer, len > sizeof(buf) ? sizeof(buf) : len)) {
307 sscanf(buf, "%i", &interval);
312 spin_lock(&appldata_timer_lock);
313 appldata_interval = interval;
314 __appldata_vtimer_setup(APPLDATA_MOD_TIMER);
315 spin_unlock(&appldata_timer_lock);
324 * appldata_generic_handler()
326 * Generic start/stop monitoring and DIAG, show status of
327 * monitoring (0 = not in process, 1 = in process)
330 appldata_generic_handler(ctl_table *ctl, int write, struct file *filp,
331 void __user *buffer, size_t *lenp, loff_t *ppos)
333 struct appldata_ops *ops = NULL, *tmp_ops;
336 struct list_head *lh;
339 spin_lock(&appldata_ops_lock);
340 list_for_each(lh, &appldata_ops_list) {
341 tmp_ops = list_entry(lh, struct appldata_ops, list);
342 if (&tmp_ops->ctl_table[2] == ctl) {
347 spin_unlock(&appldata_ops_lock);
351 if (!try_module_get(ops->owner)) { // protect this function
352 spin_unlock(&appldata_ops_lock);
355 spin_unlock(&appldata_ops_lock);
357 if (!*lenp || *ppos) {
359 module_put(ops->owner);
363 len = sprintf(buf, ops->active ? "1\n" : "0\n");
366 if (copy_to_user(buffer, buf, len)) {
367 module_put(ops->owner);
373 if (copy_from_user(buf, buffer,
374 len > sizeof(buf) ? sizeof(buf) : len)) {
375 module_put(ops->owner);
379 spin_lock(&appldata_ops_lock);
380 if ((buf[0] == '1') && (ops->active == 0)) {
381 // protect work queue callback
382 if (!try_module_get(ops->owner)) {
383 spin_unlock(&appldata_ops_lock);
384 module_put(ops->owner);
387 ops->callback(ops->data); // init record
388 rc = appldata_diag(ops->record_nr,
389 APPLDATA_START_INTERVAL_REC,
390 (unsigned long) ops->data, ops->size,
393 P_ERROR("START DIAG 0xDC for %s failed, "
394 "return code: %d\n", ops->name, rc);
395 module_put(ops->owner);
398 } else if ((buf[0] == '0') && (ops->active == 1)) {
400 rc = appldata_diag(ops->record_nr, APPLDATA_STOP_REC,
401 (unsigned long) ops->data, ops->size,
404 P_ERROR("STOP DIAG 0xDC for %s failed, "
405 "return code: %d\n", ops->name, rc);
406 module_put(ops->owner);
408 spin_unlock(&appldata_ops_lock);
412 module_put(ops->owner);
416 /*************************** /proc stuff <END> *******************************/
419 /************************* module-ops management *****************************/
421 * appldata_register_ops()
423 * update ops list, register /proc/sys entries
425 int appldata_register_ops(struct appldata_ops *ops)
427 if (ops->size > APPLDATA_MAX_REC_SIZE)
430 ops->ctl_table = kzalloc(4 * sizeof(struct ctl_table), GFP_KERNEL);
434 spin_lock(&appldata_ops_lock);
435 list_add(&ops->list, &appldata_ops_list);
436 spin_unlock(&appldata_ops_lock);
438 ops->ctl_table[0].procname = appldata_proc_name;
439 ops->ctl_table[0].maxlen = 0;
440 ops->ctl_table[0].mode = S_IRUGO | S_IXUGO;
441 ops->ctl_table[0].child = &ops->ctl_table[2];
443 ops->ctl_table[2].procname = ops->name;
444 ops->ctl_table[2].mode = S_IRUGO | S_IWUSR;
445 ops->ctl_table[2].proc_handler = appldata_generic_handler;
446 ops->ctl_table[2].data = ops;
448 ops->sysctl_header = register_sysctl_table(ops->ctl_table);
449 if (!ops->sysctl_header)
453 spin_lock(&appldata_ops_lock);
454 list_del(&ops->list);
455 spin_unlock(&appldata_ops_lock);
456 kfree(ops->ctl_table);
461 * appldata_unregister_ops()
463 * update ops list, unregister /proc entries, stop DIAG if necessary
465 void appldata_unregister_ops(struct appldata_ops *ops)
467 spin_lock(&appldata_ops_lock);
468 list_del(&ops->list);
469 spin_unlock(&appldata_ops_lock);
470 unregister_sysctl_table(ops->sysctl_header);
471 kfree(ops->ctl_table);
473 /********************** module-ops management <END> **************************/
476 /******************************* init / exit *********************************/
478 static void __cpuinit appldata_online_cpu(int cpu)
480 init_virt_timer(&per_cpu(appldata_timer, cpu));
481 per_cpu(appldata_timer, cpu).function = appldata_timer_function;
482 per_cpu(appldata_timer, cpu).data = (unsigned long)
484 atomic_inc(&appldata_expire_count);
485 spin_lock(&appldata_timer_lock);
486 __appldata_vtimer_setup(APPLDATA_MOD_TIMER);
487 spin_unlock(&appldata_timer_lock);
490 static void __cpuinit appldata_offline_cpu(int cpu)
492 del_virt_timer(&per_cpu(appldata_timer, cpu));
493 if (atomic_dec_and_test(&appldata_expire_count)) {
494 atomic_set(&appldata_expire_count, num_online_cpus());
495 queue_work(appldata_wq, &appldata_work);
497 spin_lock(&appldata_timer_lock);
498 __appldata_vtimer_setup(APPLDATA_MOD_TIMER);
499 spin_unlock(&appldata_timer_lock);
502 static int __cpuinit appldata_cpu_notify(struct notifier_block *self,
503 unsigned long action,
508 case CPU_ONLINE_FROZEN:
509 appldata_online_cpu((long) hcpu);
512 case CPU_DEAD_FROZEN:
513 appldata_offline_cpu((long) hcpu);
521 static struct notifier_block __cpuinitdata appldata_nb = {
522 .notifier_call = appldata_cpu_notify,
528 * init timer, register /proc entries
530 static int __init appldata_init(void)
534 appldata_wq = create_singlethread_workqueue("appldata");
539 for_each_online_cpu(i)
540 appldata_online_cpu(i);
543 /* Register cpu hotplug notifier */
544 register_hotcpu_notifier(&appldata_nb);
546 appldata_sysctl_header = register_sysctl_table(appldata_dir_table);
550 __initcall(appldata_init);
552 /**************************** init / exit <END> ******************************/
554 EXPORT_SYMBOL_GPL(appldata_register_ops);
555 EXPORT_SYMBOL_GPL(appldata_unregister_ops);
556 EXPORT_SYMBOL_GPL(appldata_diag);
559 EXPORT_SYMBOL_GPL(si_swapinfo);
561 EXPORT_SYMBOL_GPL(nr_threads);
562 EXPORT_SYMBOL_GPL(nr_running);
563 EXPORT_SYMBOL_GPL(nr_iowait);