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>
32 #define MY_PRINT_NAME "appldata" /* for debug messages, etc. */
33 #define APPLDATA_CPU_INTERVAL 10000 /* default (CPU) time for
37 #define TOD_MICRO 0x01000 /* nr. of TOD clock units
39 #ifndef CONFIG_ARCH_S390X
41 #define APPLDATA_START_INTERVAL_REC 0x00 /* Function codes for */
42 #define APPLDATA_STOP_REC 0x01 /* DIAG 0xDC */
43 #define APPLDATA_GEN_EVENT_RECORD 0x02
44 #define APPLDATA_START_CONFIG_REC 0x03
48 #define APPLDATA_START_INTERVAL_REC 0x80
49 #define APPLDATA_STOP_REC 0x81
50 #define APPLDATA_GEN_EVENT_RECORD 0x82
51 #define APPLDATA_START_CONFIG_REC 0x83
53 #endif /* CONFIG_ARCH_S390X */
57 * Parameter list for DIAGNOSE X'DC'
59 #ifndef CONFIG_ARCH_S390X
60 struct appldata_parameter_list {
61 u16 diag; /* The DIAGNOSE code X'00DC' */
62 u8 function; /* The function code for the DIAGNOSE */
63 u8 parlist_length; /* Length of the parameter list */
64 u32 product_id_addr; /* Address of the 16-byte product ID */
66 u16 buffer_length; /* Length of the application data buffer */
67 u32 buffer_addr; /* Address of the application data buffer */
70 struct appldata_parameter_list {
81 #endif /* CONFIG_ARCH_S390X */
84 * /proc entries (sysctl)
86 static const char appldata_proc_name[APPLDATA_PROC_NAME_LENGTH] = "appldata";
87 static int appldata_timer_handler(ctl_table *ctl, int write, struct file *filp,
88 void *buffer, size_t *lenp);
89 static int appldata_interval_handler(ctl_table *ctl, int write,
90 struct file *filp, void *buffer,
93 static struct ctl_table_header *appldata_sysctl_header;
94 static struct ctl_table appldata_table[] = {
96 .ctl_name = CTL_APPLDATA_TIMER,
98 .mode = S_IRUGO | S_IWUSR,
99 .proc_handler = &appldata_timer_handler,
102 .ctl_name = CTL_APPLDATA_INTERVAL,
103 .procname = "interval",
104 .mode = S_IRUGO | S_IWUSR,
105 .proc_handler = &appldata_interval_handler,
110 static struct ctl_table appldata_dir_table[] = {
112 .ctl_name = CTL_APPLDATA,
113 .procname = appldata_proc_name,
115 .mode = S_IRUGO | S_IXUGO,
116 .child = appldata_table,
124 DEFINE_PER_CPU(struct vtimer_list, appldata_timer);
125 static atomic_t appldata_expire_count = ATOMIC_INIT(0);
126 static struct appldata_mod_vtimer_args {
127 struct vtimer_list *timer;
129 } appldata_mod_vtimer_args;
131 static spinlock_t appldata_timer_lock = SPIN_LOCK_UNLOCKED;
132 static int appldata_interval = APPLDATA_CPU_INTERVAL;
133 static int appldata_timer_active;
138 static struct tasklet_struct appldata_tasklet_struct;
143 static spinlock_t appldata_ops_lock = SPIN_LOCK_UNLOCKED;
144 static LIST_HEAD(appldata_ops_list);
147 /************************* timer, tasklet, DIAG ******************************/
149 * appldata_timer_function()
151 * schedule tasklet and reschedule timer
153 static void appldata_timer_function(unsigned long data, struct pt_regs *regs)
155 P_DEBUG(" -= Timer =-\n");
156 P_DEBUG("CPU: %i, expire: %i\n", smp_processor_id(),
157 atomic_read(&appldata_expire_count));
158 if (atomic_dec_and_test(&appldata_expire_count)) {
159 atomic_set(&appldata_expire_count, num_online_cpus());
160 tasklet_schedule((struct tasklet_struct *) data);
165 * appldata_tasklet_function()
167 * call data gathering function for each (active) module
169 static void appldata_tasklet_function(unsigned long data)
171 struct list_head *lh;
172 struct appldata_ops *ops;
175 P_DEBUG(" -= Tasklet =-\n");
177 spin_lock(&appldata_ops_lock);
178 list_for_each(lh, &appldata_ops_list) {
179 ops = list_entry(lh, struct appldata_ops, list);
180 P_DEBUG("list_for_each loop: %i) active = %u, name = %s\n",
181 ++i, ops->active, ops->name);
182 if (ops->active == 1) {
183 ops->callback(ops->data);
186 spin_unlock(&appldata_ops_lock);
190 * appldata_mod_vtimer_wrap()
192 * wrapper function for mod_virt_timer(), because smp_call_function_on()
193 * accepts only one parameter.
195 static void appldata_mod_vtimer_wrap(struct appldata_mod_vtimer_args *args) {
196 mod_virt_timer(args->timer, args->expires);
202 * prepare parameter list, issue DIAG 0xDC
204 static int appldata_diag(char record_nr, u16 function, unsigned long buffer,
208 struct appldata_product_id {
209 char prod_nr[7]; /* product nr. */
210 char prod_fn[2]; /* product function */
211 char record_nr; /* record nr. */
212 char version_nr[2]; /* version */
213 char release_nr[2]; /* release */
214 char mod_lvl[2]; /* modification lvl. */
215 } appldata_product_id = {
216 /* all strings are EBCDIC, record_nr is byte */
217 .prod_nr = {0xD3, 0xC9, 0xD5, 0xE4,
218 0xE7, 0xD2, 0xD9}, /* "LINUXKR" */
219 .prod_fn = {0xD5, 0xD3}, /* "NL" */
220 .record_nr = record_nr,
221 .version_nr = {0xF2, 0xF6}, /* "26" */
222 .release_nr = {0xF0, 0xF1}, /* "01" */
223 .mod_lvl = {0xF0, 0xF0}, /* "00" */
225 struct appldata_parameter_list appldata_parameter_list = {
227 .function = function,
229 sizeof(appldata_parameter_list),
230 .buffer_length = length,
232 (unsigned long) &appldata_product_id,
233 .buffer_addr = virt_to_phys((void *) buffer)
240 "diag %1,%0,0xDC\n\t"
241 : "=d" (ry) : "d" (&(appldata_parameter_list)) : "cc");
244 /********************** timer, tasklet, DIAG <END> ***************************/
247 /****************************** /proc stuff **********************************/
249 * appldata_timer_handler()
251 * Start/Stop timer, show status of timer (0 = not active, 1 = active)
254 appldata_timer_handler(ctl_table *ctl, int write, struct file *filp,
255 void *buffer, size_t *lenp)
259 u64 per_cpu_interval;
261 if (!*lenp || filp->f_pos) {
266 len = sprintf(buf, appldata_timer_active ? "1\n" : "0\n");
269 if (copy_to_user(buffer, buf, len))
273 per_cpu_interval = (u64) (appldata_interval*1000 /
274 num_online_cpus()) * TOD_MICRO;
276 if (copy_from_user(buf, buffer, len > sizeof(buf) ? sizeof(buf) : len))
278 spin_lock(&appldata_timer_lock);
279 per_cpu_interval = (u64) (appldata_interval*1000 /
280 num_online_cpus()) * TOD_MICRO;
281 if ((buf[0] == '1') && (!appldata_timer_active)) {
282 for (i = 0; i < num_online_cpus(); i++) {
283 per_cpu(appldata_timer, i).expires = per_cpu_interval;
284 smp_call_function_on(add_virt_timer_periodic,
285 &per_cpu(appldata_timer, i),
288 appldata_timer_active = 1;
289 P_INFO("Monitoring timer started.\n");
290 } else if ((buf[0] == '0') && (appldata_timer_active)) {
291 for (i = 0; i < num_online_cpus(); i++) {
292 del_virt_timer(&per_cpu(appldata_timer, i));
294 appldata_timer_active = 0;
295 P_INFO("Monitoring timer stopped.\n");
297 spin_unlock(&appldata_timer_lock);
305 * appldata_interval_handler()
307 * Set (CPU) timer interval for collection of data (in milliseconds), show
308 * current timer interval.
311 appldata_interval_handler(ctl_table *ctl, int write, struct file *filp,
312 void *buffer, size_t *lenp)
314 int len, i, interval;
316 u64 per_cpu_interval;
318 if (!*lenp || filp->f_pos) {
323 len = sprintf(buf, "%i\n", appldata_interval);
326 if (copy_to_user(buffer, buf, len))
331 if (copy_from_user(buf, buffer, len > sizeof(buf) ? sizeof(buf) : len)) {
334 sscanf(buf, "%i", &interval);
336 P_ERROR("Timer CPU interval has to be > 0!\n");
340 spin_lock(&appldata_timer_lock);
341 per_cpu_interval = (u64) (interval*1000 / num_online_cpus()) * TOD_MICRO;
342 appldata_interval = interval;
343 if (appldata_timer_active) {
344 for (i = 0; i < num_online_cpus(); i++) {
345 appldata_mod_vtimer_args.timer =
346 &per_cpu(appldata_timer, i);
347 appldata_mod_vtimer_args.expires =
349 smp_call_function_on(
350 (void *) appldata_mod_vtimer_wrap,
351 &appldata_mod_vtimer_args,
355 spin_unlock(&appldata_timer_lock);
357 P_INFO("Monitoring CPU interval set to %u milliseconds.\n",
366 * appldata_generic_handler()
368 * Generic start/stop monitoring and DIAG, show status of
369 * monitoring (0 = not in process, 1 = in process)
372 appldata_generic_handler(ctl_table *ctl, int write, struct file *filp,
373 void *buffer, size_t *lenp)
375 struct appldata_ops *ops = NULL, *tmp_ops;
378 struct list_head *lh;
381 spin_lock_bh(&appldata_ops_lock);
382 list_for_each(lh, &appldata_ops_list) {
383 tmp_ops = list_entry(lh, struct appldata_ops, list);
384 if (&tmp_ops->ctl_table[2] == ctl) {
389 spin_unlock_bh(&appldata_ops_lock);
393 if (!try_module_get(ops->owner)) { // protect this function
394 spin_unlock_bh(&appldata_ops_lock);
397 spin_unlock_bh(&appldata_ops_lock);
399 if (!*lenp || filp->f_pos) {
401 module_put(ops->owner);
405 len = sprintf(buf, ops->active ? "1\n" : "0\n");
408 if (copy_to_user(buffer, buf, len)) {
409 module_put(ops->owner);
415 if (copy_from_user(buf, buffer,
416 len > sizeof(buf) ? sizeof(buf) : len)) {
417 module_put(ops->owner);
421 spin_lock_bh(&appldata_ops_lock);
422 if ((buf[0] == '1') && (ops->active == 0)) {
423 if (!try_module_get(ops->owner)) { // protect tasklet
424 spin_unlock_bh(&appldata_ops_lock);
425 module_put(ops->owner);
429 ops->callback(ops->data); // init record
430 rc = appldata_diag(ops->record_nr,
431 APPLDATA_START_INTERVAL_REC,
432 (unsigned long) ops->data, ops->size);
434 P_ERROR("START DIAG 0xDC for %s failed, "
435 "return code: %d\n", ops->name, rc);
436 module_put(ops->owner);
439 P_INFO("Monitoring %s data enabled, "
440 "DIAG 0xDC started.\n", ops->name);
442 } else if ((buf[0] == '0') && (ops->active == 1)) {
444 rc = appldata_diag(ops->record_nr, APPLDATA_STOP_REC,
445 (unsigned long) ops->data, ops->size);
447 P_ERROR("STOP DIAG 0xDC for %s failed, "
448 "return code: %d\n", ops->name, rc);
450 P_INFO("Monitoring %s data disabled, "
451 "DIAG 0xDC stopped.\n", ops->name);
453 module_put(ops->owner);
455 spin_unlock_bh(&appldata_ops_lock);
459 module_put(ops->owner);
463 /*************************** /proc stuff <END> *******************************/
466 /************************* module-ops management *****************************/
468 * appldata_register_ops()
470 * update ops list, register /proc/sys entries
472 int appldata_register_ops(struct appldata_ops *ops)
474 struct list_head *lh;
475 struct appldata_ops *tmp_ops;
480 if ((ops->size > APPLDATA_MAX_REC_SIZE) ||
482 P_ERROR("Invalid size of %s record = %i, maximum = %i!\n",
483 ops->name, ops->size, APPLDATA_MAX_REC_SIZE);
486 if ((ops->ctl_nr == CTL_APPLDATA) ||
487 (ops->ctl_nr == CTL_APPLDATA_TIMER) ||
488 (ops->ctl_nr == CTL_APPLDATA_INTERVAL)) {
489 P_ERROR("ctl_nr %i already in use!\n", ops->ctl_nr);
492 ops->ctl_table = kmalloc(4*sizeof(struct ctl_table), GFP_KERNEL);
493 if (ops->ctl_table == NULL) {
494 P_ERROR("Not enough memory for %s ctl_table!\n", ops->name);
497 memset(ops->ctl_table, 0, 4*sizeof(struct ctl_table));
499 spin_lock_bh(&appldata_ops_lock);
500 list_for_each(lh, &appldata_ops_list) {
501 tmp_ops = list_entry(lh, struct appldata_ops, list);
502 P_DEBUG("register_ops loop: %i) name = %s, ctl = %i\n",
503 ++i, tmp_ops->name, tmp_ops->ctl_nr);
504 P_DEBUG("Comparing %s (ctl %i) with %s (ctl %i)\n",
505 tmp_ops->name, tmp_ops->ctl_nr, ops->name,
507 if (strncmp(tmp_ops->name, ops->name,
508 APPLDATA_PROC_NAME_LENGTH) == 0) {
509 P_ERROR("Name \"%s\" already registered!\n", ops->name);
510 kfree(ops->ctl_table);
511 spin_unlock_bh(&appldata_ops_lock);
514 if (tmp_ops->ctl_nr == ops->ctl_nr) {
515 P_ERROR("ctl_nr %i already registered!\n", ops->ctl_nr);
516 kfree(ops->ctl_table);
517 spin_unlock_bh(&appldata_ops_lock);
521 list_add(&ops->list, &appldata_ops_list);
522 spin_unlock_bh(&appldata_ops_lock);
524 ops->ctl_table[0].ctl_name = CTL_APPLDATA;
525 ops->ctl_table[0].procname = appldata_proc_name;
526 ops->ctl_table[0].maxlen = 0;
527 ops->ctl_table[0].mode = S_IRUGO | S_IXUGO;
528 ops->ctl_table[0].child = &ops->ctl_table[2];
530 ops->ctl_table[1].ctl_name = 0;
532 ops->ctl_table[2].ctl_name = ops->ctl_nr;
533 ops->ctl_table[2].procname = ops->name;
534 ops->ctl_table[2].mode = S_IRUGO | S_IWUSR;
535 ops->ctl_table[2].proc_handler = appldata_generic_handler;
536 ops->ctl_table[2].data = ops;
538 ops->ctl_table[3].ctl_name = 0;
540 ops->sysctl_header = register_sysctl_table(ops->ctl_table,1);
542 P_INFO("%s-ops registered!\n", ops->name);
547 * appldata_unregister_ops()
549 * update ops list, unregister /proc entries, stop DIAG if necessary
551 void appldata_unregister_ops(struct appldata_ops *ops)
553 spin_lock_bh(&appldata_ops_lock);
554 unregister_sysctl_table(ops->sysctl_header);
555 list_del(&ops->list);
556 kfree(ops->ctl_table);
557 ops->ctl_table = NULL;
558 spin_unlock_bh(&appldata_ops_lock);
559 P_INFO("%s-ops unregistered!\n", ops->name);
561 /********************** module-ops management <END> **************************/
564 /******************************* init / exit *********************************/
568 * init timer and tasklet, register /proc entries
570 static int __init appldata_init(void)
574 P_DEBUG("sizeof(parameter_list) = %lu\n",
575 sizeof(struct appldata_parameter_list));
577 for (i = 0; i < num_online_cpus(); i++) {
578 init_virt_timer(&per_cpu(appldata_timer, i));
579 per_cpu(appldata_timer, i).function = appldata_timer_function;
580 per_cpu(appldata_timer, i).data = (unsigned long)
581 &appldata_tasklet_struct;
583 atomic_set(&appldata_expire_count, num_online_cpus());
585 appldata_sysctl_header = register_sysctl_table(appldata_dir_table, 1);
587 appldata_dir_table[0].de->owner = THIS_MODULE;
588 appldata_table[0].de->owner = THIS_MODULE;
589 appldata_table[1].de->owner = THIS_MODULE;
592 tasklet_init(&appldata_tasklet_struct, appldata_tasklet_function, 0);
593 P_DEBUG("Base interface initialized.\n");
600 * stop timer and tasklet, unregister /proc entries
602 static void __exit appldata_exit(void)
604 struct list_head *lh;
605 struct appldata_ops *ops;
608 P_DEBUG("Unloading module ...\n");
610 * ops list should be empty, but just in case something went wrong...
612 spin_lock_bh(&appldata_ops_lock);
613 list_for_each(lh, &appldata_ops_list) {
614 ops = list_entry(lh, struct appldata_ops, list);
615 rc = appldata_diag(ops->record_nr, APPLDATA_STOP_REC,
616 (unsigned long) ops->data, ops->size);
618 P_ERROR("STOP DIAG 0xDC for %s failed, "
619 "return code: %d\n", ops->name, rc);
622 spin_unlock_bh(&appldata_ops_lock);
624 for (i = 0; i < num_online_cpus(); i++) {
625 del_virt_timer(&per_cpu(appldata_timer, i));
627 appldata_timer_active = 0;
629 unregister_sysctl_table(appldata_sysctl_header);
631 tasklet_kill(&appldata_tasklet_struct);
632 P_DEBUG("... module unloaded!\n");
634 /**************************** init / exit <END> ******************************/
637 module_init(appldata_init);
638 module_exit(appldata_exit);
639 MODULE_LICENSE("GPL");
640 MODULE_AUTHOR("Gerald Schaefer");
641 MODULE_DESCRIPTION("Linux-VM Monitor Stream, base infrastructure");
643 EXPORT_SYMBOL_GPL(appldata_register_ops);
644 EXPORT_SYMBOL_GPL(appldata_unregister_ops);
648 * Kernel symbols needed by appldata_mem and appldata_os modules.
649 * However, if this file is compiled as a module (for testing only), these
650 * symbols are not exported. In this case, we define them locally and export
653 void si_swapinfo(struct sysinfo *val)
655 val->freeswap = -1ul;
656 val->totalswap = -1ul;
659 unsigned long avenrun[3] = {-1 - FIXED_1/200, -1 - FIXED_1/200,
663 void get_full_page_state(struct page_state *ps)
665 memset(ps, -1, sizeof(struct page_state));
668 unsigned long nr_running(void)
673 unsigned long nr_iowait(void)
678 /*unsigned long nr_context_switches(void)
683 EXPORT_SYMBOL_GPL(si_swapinfo);
684 EXPORT_SYMBOL_GPL(nr_threads);
685 EXPORT_SYMBOL_GPL(avenrun);
686 EXPORT_SYMBOL_GPL(get_full_page_state);
687 EXPORT_SYMBOL_GPL(nr_running);
688 EXPORT_SYMBOL_GPL(nr_iowait);
689 //EXPORT_SYMBOL_GPL(nr_context_switches);