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[] = {
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 P_DEBUG(" -= Timer =-\n");
112 P_DEBUG("CPU: %i, expire_count: %i\n", smp_processor_id(),
113 atomic_read(&appldata_expire_count));
114 if (atomic_dec_and_test(&appldata_expire_count)) {
115 atomic_set(&appldata_expire_count, num_online_cpus());
116 queue_work(appldata_wq, (struct work_struct *) data);
123 * call data gathering function for each (active) module
125 static void appldata_work_fn(struct work_struct *work)
127 struct list_head *lh;
128 struct appldata_ops *ops;
131 P_DEBUG(" -= Work Queue =-\n");
134 spin_lock(&appldata_ops_lock);
135 list_for_each(lh, &appldata_ops_list) {
136 ops = list_entry(lh, struct appldata_ops, list);
137 P_DEBUG("list_for_each loop: %i) active = %u, name = %s\n",
138 ++i, ops->active, ops->name);
139 if (ops->active == 1) {
140 ops->callback(ops->data);
143 spin_unlock(&appldata_ops_lock);
150 * prepare parameter list, issue DIAG 0xDC
152 int appldata_diag(char record_nr, u16 function, unsigned long buffer,
153 u16 length, char *mod_lvl)
155 struct appldata_product_id id = {
156 .prod_nr = {0xD3, 0xC9, 0xD5, 0xE4,
157 0xE7, 0xD2, 0xD9}, /* "LINUXKR" */
158 .prod_fn = 0xD5D3, /* "NL" */
159 .version_nr = 0xF2F6, /* "26" */
160 .release_nr = 0xF0F1, /* "01" */
163 id.record_nr = record_nr;
164 id.mod_lvl = (mod_lvl[0]) << 8 | mod_lvl[1];
165 return appldata_asm(&id, function, (void *) buffer, length);
167 /************************ timer, work, DIAG <END> ****************************/
170 /****************************** /proc stuff **********************************/
173 * appldata_mod_vtimer_wrap()
175 * wrapper function for mod_virt_timer(), because smp_call_function_single()
176 * accepts only one parameter.
178 static void __appldata_mod_vtimer_wrap(void *p) {
180 struct vtimer_list *timer;
183 mod_virt_timer(args->timer, args->expires);
186 #define APPLDATA_ADD_TIMER 0
187 #define APPLDATA_DEL_TIMER 1
188 #define APPLDATA_MOD_TIMER 2
191 * __appldata_vtimer_setup()
193 * Add, delete or modify virtual timers on all online cpus.
194 * The caller needs to get the appldata_timer_lock spinlock.
197 __appldata_vtimer_setup(int cmd)
199 u64 per_cpu_interval;
203 case APPLDATA_ADD_TIMER:
204 if (appldata_timer_active)
206 per_cpu_interval = (u64) (appldata_interval*1000 /
207 num_online_cpus()) * TOD_MICRO;
208 for_each_online_cpu(i) {
209 per_cpu(appldata_timer, i).expires = per_cpu_interval;
210 smp_call_function_single(i, add_virt_timer_periodic,
211 &per_cpu(appldata_timer, i),
214 appldata_timer_active = 1;
215 P_INFO("Monitoring timer started.\n");
217 case APPLDATA_DEL_TIMER:
218 for_each_online_cpu(i)
219 del_virt_timer(&per_cpu(appldata_timer, i));
220 if (!appldata_timer_active)
222 appldata_timer_active = 0;
223 atomic_set(&appldata_expire_count, num_online_cpus());
224 P_INFO("Monitoring timer stopped.\n");
226 case APPLDATA_MOD_TIMER:
227 per_cpu_interval = (u64) (appldata_interval*1000 /
228 num_online_cpus()) * TOD_MICRO;
229 if (!appldata_timer_active)
231 for_each_online_cpu(i) {
233 struct vtimer_list *timer;
236 args.timer = &per_cpu(appldata_timer, i);
237 args.expires = per_cpu_interval;
238 smp_call_function_single(i, __appldata_mod_vtimer_wrap,
245 * appldata_timer_handler()
247 * Start/Stop timer, show status of timer (0 = not active, 1 = active)
250 appldata_timer_handler(ctl_table *ctl, int write, struct file *filp,
251 void __user *buffer, size_t *lenp, loff_t *ppos)
256 if (!*lenp || *ppos) {
261 len = sprintf(buf, appldata_timer_active ? "1\n" : "0\n");
264 if (copy_to_user(buffer, buf, len))
269 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);
286 * appldata_interval_handler()
288 * Set (CPU) timer interval for collection of data (in milliseconds), show
289 * current timer interval.
292 appldata_interval_handler(ctl_table *ctl, int write, struct file *filp,
293 void __user *buffer, size_t *lenp, loff_t *ppos)
298 if (!*lenp || *ppos) {
303 len = sprintf(buf, "%i\n", appldata_interval);
306 if (copy_to_user(buffer, buf, len))
311 if (copy_from_user(buf, buffer, len > sizeof(buf) ? sizeof(buf) : len)) {
315 sscanf(buf, "%i", &interval);
317 P_ERROR("Timer CPU interval has to be > 0!\n");
322 spin_lock(&appldata_timer_lock);
323 appldata_interval = interval;
324 __appldata_vtimer_setup(APPLDATA_MOD_TIMER);
325 spin_unlock(&appldata_timer_lock);
328 P_INFO("Monitoring CPU interval set to %u milliseconds.\n",
337 * appldata_generic_handler()
339 * Generic start/stop monitoring and DIAG, show status of
340 * monitoring (0 = not in process, 1 = in process)
343 appldata_generic_handler(ctl_table *ctl, int write, struct file *filp,
344 void __user *buffer, size_t *lenp, loff_t *ppos)
346 struct appldata_ops *ops = NULL, *tmp_ops;
349 struct list_head *lh;
352 spin_lock(&appldata_ops_lock);
353 list_for_each(lh, &appldata_ops_list) {
354 tmp_ops = list_entry(lh, struct appldata_ops, list);
355 if (&tmp_ops->ctl_table[2] == ctl) {
360 spin_unlock(&appldata_ops_lock);
364 if (!try_module_get(ops->owner)) { // protect this function
365 spin_unlock(&appldata_ops_lock);
368 spin_unlock(&appldata_ops_lock);
370 if (!*lenp || *ppos) {
372 module_put(ops->owner);
376 len = sprintf(buf, ops->active ? "1\n" : "0\n");
379 if (copy_to_user(buffer, buf, len)) {
380 module_put(ops->owner);
386 if (copy_from_user(buf, buffer,
387 len > sizeof(buf) ? sizeof(buf) : len)) {
388 module_put(ops->owner);
392 spin_lock(&appldata_ops_lock);
393 if ((buf[0] == '1') && (ops->active == 0)) {
394 // protect work queue callback
395 if (!try_module_get(ops->owner)) {
396 spin_unlock(&appldata_ops_lock);
397 module_put(ops->owner);
400 ops->callback(ops->data); // init record
401 rc = appldata_diag(ops->record_nr,
402 APPLDATA_START_INTERVAL_REC,
403 (unsigned long) ops->data, ops->size,
406 P_ERROR("START DIAG 0xDC for %s failed, "
407 "return code: %d\n", ops->name, rc);
408 module_put(ops->owner);
410 P_INFO("Monitoring %s data enabled, "
411 "DIAG 0xDC started.\n", ops->name);
414 } else if ((buf[0] == '0') && (ops->active == 1)) {
416 rc = appldata_diag(ops->record_nr, APPLDATA_STOP_REC,
417 (unsigned long) ops->data, ops->size,
420 P_ERROR("STOP DIAG 0xDC for %s failed, "
421 "return code: %d\n", ops->name, rc);
423 P_INFO("Monitoring %s data disabled, "
424 "DIAG 0xDC stopped.\n", ops->name);
426 module_put(ops->owner);
428 spin_unlock(&appldata_ops_lock);
432 module_put(ops->owner);
436 /*************************** /proc stuff <END> *******************************/
439 /************************* module-ops management *****************************/
441 * appldata_register_ops()
443 * update ops list, register /proc/sys entries
445 int appldata_register_ops(struct appldata_ops *ops)
447 if ((ops->size > APPLDATA_MAX_REC_SIZE) || (ops->size < 0))
450 ops->ctl_table = kzalloc(4 * sizeof(struct ctl_table), GFP_KERNEL);
454 spin_lock(&appldata_ops_lock);
455 list_add(&ops->list, &appldata_ops_list);
456 spin_unlock(&appldata_ops_lock);
458 ops->ctl_table[0].procname = appldata_proc_name;
459 ops->ctl_table[0].maxlen = 0;
460 ops->ctl_table[0].mode = S_IRUGO | S_IXUGO;
461 ops->ctl_table[0].child = &ops->ctl_table[2];
463 ops->ctl_table[2].procname = ops->name;
464 ops->ctl_table[2].mode = S_IRUGO | S_IWUSR;
465 ops->ctl_table[2].proc_handler = appldata_generic_handler;
466 ops->ctl_table[2].data = ops;
468 ops->sysctl_header = register_sysctl_table(ops->ctl_table);
469 if (!ops->sysctl_header)
471 P_INFO("%s-ops registered!\n", ops->name);
474 spin_lock(&appldata_ops_lock);
475 list_del(&ops->list);
476 spin_unlock(&appldata_ops_lock);
477 kfree(ops->ctl_table);
482 * appldata_unregister_ops()
484 * update ops list, unregister /proc entries, stop DIAG if necessary
486 void appldata_unregister_ops(struct appldata_ops *ops)
488 spin_lock(&appldata_ops_lock);
489 list_del(&ops->list);
490 spin_unlock(&appldata_ops_lock);
491 unregister_sysctl_table(ops->sysctl_header);
492 kfree(ops->ctl_table);
493 P_INFO("%s-ops unregistered!\n", ops->name);
495 /********************** module-ops management <END> **************************/
498 /******************************* init / exit *********************************/
500 static void __cpuinit appldata_online_cpu(int cpu)
502 init_virt_timer(&per_cpu(appldata_timer, cpu));
503 per_cpu(appldata_timer, cpu).function = appldata_timer_function;
504 per_cpu(appldata_timer, cpu).data = (unsigned long)
506 atomic_inc(&appldata_expire_count);
507 spin_lock(&appldata_timer_lock);
508 __appldata_vtimer_setup(APPLDATA_MOD_TIMER);
509 spin_unlock(&appldata_timer_lock);
512 static void __cpuinit appldata_offline_cpu(int cpu)
514 del_virt_timer(&per_cpu(appldata_timer, cpu));
515 if (atomic_dec_and_test(&appldata_expire_count)) {
516 atomic_set(&appldata_expire_count, num_online_cpus());
517 queue_work(appldata_wq, &appldata_work);
519 spin_lock(&appldata_timer_lock);
520 __appldata_vtimer_setup(APPLDATA_MOD_TIMER);
521 spin_unlock(&appldata_timer_lock);
524 static int __cpuinit appldata_cpu_notify(struct notifier_block *self,
525 unsigned long action,
530 case CPU_ONLINE_FROZEN:
531 appldata_online_cpu((long) hcpu);
534 case CPU_DEAD_FROZEN:
535 appldata_offline_cpu((long) hcpu);
543 static struct notifier_block __cpuinitdata appldata_nb = {
544 .notifier_call = appldata_cpu_notify,
550 * init timer, register /proc entries
552 static int __init appldata_init(void)
556 P_DEBUG("sizeof(parameter_list) = %lu\n",
557 sizeof(struct appldata_parameter_list));
559 appldata_wq = create_singlethread_workqueue("appldata");
561 P_ERROR("Could not create work queue\n");
566 for_each_online_cpu(i)
567 appldata_online_cpu(i);
570 /* Register cpu hotplug notifier */
571 register_hotcpu_notifier(&appldata_nb);
573 appldata_sysctl_header = register_sysctl_table(appldata_dir_table);
575 P_DEBUG("Base interface initialized.\n");
579 __initcall(appldata_init);
581 /**************************** init / exit <END> ******************************/
583 EXPORT_SYMBOL_GPL(appldata_register_ops);
584 EXPORT_SYMBOL_GPL(appldata_unregister_ops);
585 EXPORT_SYMBOL_GPL(appldata_diag);
587 EXPORT_SYMBOL_GPL(si_swapinfo);
588 EXPORT_SYMBOL_GPL(nr_threads);
589 EXPORT_SYMBOL_GPL(nr_running);
590 EXPORT_SYMBOL_GPL(nr_iowait);