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>
21 #include <linux/interrupt.h>
22 #include <linux/proc_fs.h>
23 #include <linux/page-flags.h>
24 #include <linux/swap.h>
25 #include <linux/pagemap.h>
26 #include <linux/sysctl.h>
27 #include <asm/timer.h>
28 //#include <linux/kernel_stat.h>
29 #include <linux/notifier.h>
30 #include <linux/cpu.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
42 #ifndef CONFIG_ARCH_S390X
44 #define APPLDATA_START_INTERVAL_REC 0x00 /* Function codes for */
45 #define APPLDATA_STOP_REC 0x01 /* DIAG 0xDC */
46 #define APPLDATA_GEN_EVENT_RECORD 0x02
47 #define APPLDATA_START_CONFIG_REC 0x03
51 #define APPLDATA_START_INTERVAL_REC 0x80
52 #define APPLDATA_STOP_REC 0x81
53 #define APPLDATA_GEN_EVENT_RECORD 0x82
54 #define APPLDATA_START_CONFIG_REC 0x83
56 #endif /* CONFIG_ARCH_S390X */
60 * Parameter list for DIAGNOSE X'DC'
62 #ifndef CONFIG_ARCH_S390X
63 struct appldata_parameter_list
{
64 u16 diag
; /* The DIAGNOSE code X'00DC' */
65 u8 function
; /* The function code for the DIAGNOSE */
66 u8 parlist_length
; /* Length of the parameter list */
67 u32 product_id_addr
; /* Address of the 16-byte product ID */
69 u16 buffer_length
; /* Length of the application data buffer */
70 u32 buffer_addr
; /* Address of the application data buffer */
73 struct appldata_parameter_list
{
84 #endif /* CONFIG_ARCH_S390X */
87 * /proc entries (sysctl)
89 static const char appldata_proc_name
[APPLDATA_PROC_NAME_LENGTH
] = "appldata";
90 static int appldata_timer_handler(ctl_table
*ctl
, int write
, struct file
*filp
,
91 void __user
*buffer
, size_t *lenp
, loff_t
*ppos
);
92 static int appldata_interval_handler(ctl_table
*ctl
, int write
,
95 size_t *lenp
, loff_t
*ppos
);
97 static struct ctl_table_header
*appldata_sysctl_header
;
98 static struct ctl_table appldata_table
[] = {
100 .ctl_name
= CTL_APPLDATA_TIMER
,
102 .mode
= S_IRUGO
| S_IWUSR
,
103 .proc_handler
= &appldata_timer_handler
,
106 .ctl_name
= CTL_APPLDATA_INTERVAL
,
107 .procname
= "interval",
108 .mode
= S_IRUGO
| S_IWUSR
,
109 .proc_handler
= &appldata_interval_handler
,
114 static struct ctl_table appldata_dir_table
[] = {
116 .ctl_name
= CTL_APPLDATA
,
117 .procname
= appldata_proc_name
,
119 .mode
= S_IRUGO
| S_IXUGO
,
120 .child
= appldata_table
,
128 DEFINE_PER_CPU(struct vtimer_list
, appldata_timer
);
129 static atomic_t appldata_expire_count
= ATOMIC_INIT(0);
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_count: %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
);
192 * prepare parameter list, issue DIAG 0xDC
194 static int appldata_diag(char record_nr
, u16 function
, unsigned long buffer
,
198 struct appldata_product_id
{
199 char prod_nr
[7]; /* product nr. */
200 char prod_fn
[2]; /* product function */
201 char record_nr
; /* record nr. */
202 char version_nr
[2]; /* version */
203 char release_nr
[2]; /* release */
204 char mod_lvl
[2]; /* modification lvl. */
205 } appldata_product_id
= {
206 /* all strings are EBCDIC, record_nr is byte */
207 .prod_nr
= {0xD3, 0xC9, 0xD5, 0xE4,
208 0xE7, 0xD2, 0xD9}, /* "LINUXKR" */
209 .prod_fn
= {0xD5, 0xD3}, /* "NL" */
210 .record_nr
= record_nr
,
211 .version_nr
= {0xF2, 0xF6}, /* "26" */
212 .release_nr
= {0xF0, 0xF1}, /* "01" */
213 .mod_lvl
= {0xF0, 0xF0}, /* "00" */
215 struct appldata_parameter_list appldata_parameter_list
= {
217 .function
= function
,
219 sizeof(appldata_parameter_list
),
220 .buffer_length
= length
,
222 (unsigned long) &appldata_product_id
,
223 .buffer_addr
= virt_to_phys((void *) buffer
)
230 "diag %1,%0,0xDC\n\t"
231 : "=d" (ry
) : "d" (&(appldata_parameter_list
)) : "cc");
234 /********************** timer, tasklet, DIAG <END> ***************************/
237 /****************************** /proc stuff **********************************/
240 * appldata_mod_vtimer_wrap()
242 * wrapper function for mod_virt_timer(), because smp_call_function_on()
243 * accepts only one parameter.
245 static void __appldata_mod_vtimer_wrap(void *p
) {
247 struct vtimer_list
*timer
;
250 mod_virt_timer(args
->timer
, args
->expires
);
253 #define APPLDATA_ADD_TIMER 0
254 #define APPLDATA_DEL_TIMER 1
255 #define APPLDATA_MOD_TIMER 2
258 * __appldata_vtimer_setup()
260 * Add, delete or modify virtual timers on all online cpus.
261 * The caller needs to get the appldata_timer_lock spinlock.
264 __appldata_vtimer_setup(int cmd
)
266 u64 per_cpu_interval
;
270 case APPLDATA_ADD_TIMER
:
271 if (appldata_timer_active
)
273 per_cpu_interval
= (u64
) (appldata_interval
*1000 /
274 num_online_cpus()) * TOD_MICRO
;
275 for_each_online_cpu(i
) {
276 per_cpu(appldata_timer
, i
).expires
= per_cpu_interval
;
277 smp_call_function_on(add_virt_timer_periodic
,
278 &per_cpu(appldata_timer
, i
),
281 appldata_timer_active
= 1;
282 P_INFO("Monitoring timer started.\n");
284 case APPLDATA_DEL_TIMER
:
285 for_each_online_cpu(i
)
286 del_virt_timer(&per_cpu(appldata_timer
, i
));
287 if (!appldata_timer_active
)
289 appldata_timer_active
= 0;
290 atomic_set(&appldata_expire_count
, num_online_cpus());
291 P_INFO("Monitoring timer stopped.\n");
293 case APPLDATA_MOD_TIMER
:
294 per_cpu_interval
= (u64
) (appldata_interval
*1000 /
295 num_online_cpus()) * TOD_MICRO
;
296 if (!appldata_timer_active
)
298 for_each_online_cpu(i
) {
300 struct vtimer_list
*timer
;
303 args
.timer
= &per_cpu(appldata_timer
, i
);
304 args
.expires
= per_cpu_interval
;
305 smp_call_function_on(__appldata_mod_vtimer_wrap
,
312 * appldata_timer_handler()
314 * Start/Stop timer, show status of timer (0 = not active, 1 = active)
317 appldata_timer_handler(ctl_table
*ctl
, int write
, struct file
*filp
,
318 void __user
*buffer
, size_t *lenp
, loff_t
*ppos
)
323 if (!*lenp
|| *ppos
) {
328 len
= sprintf(buf
, appldata_timer_active
? "1\n" : "0\n");
331 if (copy_to_user(buffer
, buf
, len
))
336 if (copy_from_user(buf
, buffer
, len
> sizeof(buf
) ? sizeof(buf
) : len
))
338 spin_lock(&appldata_timer_lock
);
340 __appldata_vtimer_setup(APPLDATA_ADD_TIMER
);
341 else if (buf
[0] == '0')
342 __appldata_vtimer_setup(APPLDATA_DEL_TIMER
);
343 spin_unlock(&appldata_timer_lock
);
351 * appldata_interval_handler()
353 * Set (CPU) timer interval for collection of data (in milliseconds), show
354 * current timer interval.
357 appldata_interval_handler(ctl_table
*ctl
, int write
, struct file
*filp
,
358 void __user
*buffer
, size_t *lenp
, loff_t
*ppos
)
363 if (!*lenp
|| *ppos
) {
368 len
= sprintf(buf
, "%i\n", appldata_interval
);
371 if (copy_to_user(buffer
, buf
, len
))
376 if (copy_from_user(buf
, buffer
, len
> sizeof(buf
) ? sizeof(buf
) : len
)) {
379 sscanf(buf
, "%i", &interval
);
381 P_ERROR("Timer CPU interval has to be > 0!\n");
385 spin_lock(&appldata_timer_lock
);
386 appldata_interval
= interval
;
387 __appldata_vtimer_setup(APPLDATA_MOD_TIMER
);
388 spin_unlock(&appldata_timer_lock
);
390 P_INFO("Monitoring CPU interval set to %u milliseconds.\n",
399 * appldata_generic_handler()
401 * Generic start/stop monitoring and DIAG, show status of
402 * monitoring (0 = not in process, 1 = in process)
405 appldata_generic_handler(ctl_table
*ctl
, int write
, struct file
*filp
,
406 void __user
*buffer
, size_t *lenp
, loff_t
*ppos
)
408 struct appldata_ops
*ops
= NULL
, *tmp_ops
;
411 struct list_head
*lh
;
414 spin_lock_bh(&appldata_ops_lock
);
415 list_for_each(lh
, &appldata_ops_list
) {
416 tmp_ops
= list_entry(lh
, struct appldata_ops
, list
);
417 if (&tmp_ops
->ctl_table
[2] == ctl
) {
422 spin_unlock_bh(&appldata_ops_lock
);
426 if (!try_module_get(ops
->owner
)) { // protect this function
427 spin_unlock_bh(&appldata_ops_lock
);
430 spin_unlock_bh(&appldata_ops_lock
);
432 if (!*lenp
|| *ppos
) {
434 module_put(ops
->owner
);
438 len
= sprintf(buf
, ops
->active
? "1\n" : "0\n");
441 if (copy_to_user(buffer
, buf
, len
)) {
442 module_put(ops
->owner
);
448 if (copy_from_user(buf
, buffer
,
449 len
> sizeof(buf
) ? sizeof(buf
) : len
)) {
450 module_put(ops
->owner
);
454 spin_lock_bh(&appldata_ops_lock
);
455 if ((buf
[0] == '1') && (ops
->active
== 0)) {
456 if (!try_module_get(ops
->owner
)) { // protect tasklet
457 spin_unlock_bh(&appldata_ops_lock
);
458 module_put(ops
->owner
);
462 ops
->callback(ops
->data
); // init record
463 rc
= appldata_diag(ops
->record_nr
,
464 APPLDATA_START_INTERVAL_REC
,
465 (unsigned long) ops
->data
, ops
->size
);
467 P_ERROR("START DIAG 0xDC for %s failed, "
468 "return code: %d\n", ops
->name
, rc
);
469 module_put(ops
->owner
);
472 P_INFO("Monitoring %s data enabled, "
473 "DIAG 0xDC started.\n", ops
->name
);
475 } else if ((buf
[0] == '0') && (ops
->active
== 1)) {
477 rc
= appldata_diag(ops
->record_nr
, APPLDATA_STOP_REC
,
478 (unsigned long) ops
->data
, ops
->size
);
480 P_ERROR("STOP DIAG 0xDC for %s failed, "
481 "return code: %d\n", ops
->name
, rc
);
483 P_INFO("Monitoring %s data disabled, "
484 "DIAG 0xDC stopped.\n", ops
->name
);
486 module_put(ops
->owner
);
488 spin_unlock_bh(&appldata_ops_lock
);
492 module_put(ops
->owner
);
496 /*************************** /proc stuff <END> *******************************/
499 /************************* module-ops management *****************************/
501 * appldata_register_ops()
503 * update ops list, register /proc/sys entries
505 int appldata_register_ops(struct appldata_ops
*ops
)
507 struct list_head
*lh
;
508 struct appldata_ops
*tmp_ops
;
513 if ((ops
->size
> APPLDATA_MAX_REC_SIZE
) ||
515 P_ERROR("Invalid size of %s record = %i, maximum = %i!\n",
516 ops
->name
, ops
->size
, APPLDATA_MAX_REC_SIZE
);
519 if ((ops
->ctl_nr
== CTL_APPLDATA
) ||
520 (ops
->ctl_nr
== CTL_APPLDATA_TIMER
) ||
521 (ops
->ctl_nr
== CTL_APPLDATA_INTERVAL
)) {
522 P_ERROR("ctl_nr %i already in use!\n", ops
->ctl_nr
);
525 ops
->ctl_table
= kmalloc(4*sizeof(struct ctl_table
), GFP_KERNEL
);
526 if (ops
->ctl_table
== NULL
) {
527 P_ERROR("Not enough memory for %s ctl_table!\n", ops
->name
);
530 memset(ops
->ctl_table
, 0, 4*sizeof(struct ctl_table
));
532 spin_lock_bh(&appldata_ops_lock
);
533 list_for_each(lh
, &appldata_ops_list
) {
534 tmp_ops
= list_entry(lh
, struct appldata_ops
, list
);
535 P_DEBUG("register_ops loop: %i) name = %s, ctl = %i\n",
536 ++i
, tmp_ops
->name
, tmp_ops
->ctl_nr
);
537 P_DEBUG("Comparing %s (ctl %i) with %s (ctl %i)\n",
538 tmp_ops
->name
, tmp_ops
->ctl_nr
, ops
->name
,
540 if (strncmp(tmp_ops
->name
, ops
->name
,
541 APPLDATA_PROC_NAME_LENGTH
) == 0) {
542 P_ERROR("Name \"%s\" already registered!\n", ops
->name
);
543 kfree(ops
->ctl_table
);
544 spin_unlock_bh(&appldata_ops_lock
);
547 if (tmp_ops
->ctl_nr
== ops
->ctl_nr
) {
548 P_ERROR("ctl_nr %i already registered!\n", ops
->ctl_nr
);
549 kfree(ops
->ctl_table
);
550 spin_unlock_bh(&appldata_ops_lock
);
554 list_add(&ops
->list
, &appldata_ops_list
);
555 spin_unlock_bh(&appldata_ops_lock
);
557 ops
->ctl_table
[0].ctl_name
= CTL_APPLDATA
;
558 ops
->ctl_table
[0].procname
= appldata_proc_name
;
559 ops
->ctl_table
[0].maxlen
= 0;
560 ops
->ctl_table
[0].mode
= S_IRUGO
| S_IXUGO
;
561 ops
->ctl_table
[0].child
= &ops
->ctl_table
[2];
563 ops
->ctl_table
[1].ctl_name
= 0;
565 ops
->ctl_table
[2].ctl_name
= ops
->ctl_nr
;
566 ops
->ctl_table
[2].procname
= ops
->name
;
567 ops
->ctl_table
[2].mode
= S_IRUGO
| S_IWUSR
;
568 ops
->ctl_table
[2].proc_handler
= appldata_generic_handler
;
569 ops
->ctl_table
[2].data
= ops
;
571 ops
->ctl_table
[3].ctl_name
= 0;
573 ops
->sysctl_header
= register_sysctl_table(ops
->ctl_table
,1);
575 P_INFO("%s-ops registered!\n", ops
->name
);
580 * appldata_unregister_ops()
582 * update ops list, unregister /proc entries, stop DIAG if necessary
584 void appldata_unregister_ops(struct appldata_ops
*ops
)
586 spin_lock_bh(&appldata_ops_lock
);
587 unregister_sysctl_table(ops
->sysctl_header
);
588 list_del(&ops
->list
);
589 kfree(ops
->ctl_table
);
590 ops
->ctl_table
= NULL
;
591 spin_unlock_bh(&appldata_ops_lock
);
592 P_INFO("%s-ops unregistered!\n", ops
->name
);
594 /********************** module-ops management <END> **************************/
597 /******************************* init / exit *********************************/
600 appldata_online_cpu(int cpu
)
602 init_virt_timer(&per_cpu(appldata_timer
, cpu
));
603 per_cpu(appldata_timer
, cpu
).function
= appldata_timer_function
;
604 per_cpu(appldata_timer
, cpu
).data
= (unsigned long)
605 &appldata_tasklet_struct
;
606 atomic_inc(&appldata_expire_count
);
607 spin_lock(&appldata_timer_lock
);
608 __appldata_vtimer_setup(APPLDATA_MOD_TIMER
);
609 spin_unlock(&appldata_timer_lock
);
613 appldata_offline_cpu(int cpu
)
615 del_virt_timer(&per_cpu(appldata_timer
, cpu
));
616 if (atomic_dec_and_test(&appldata_expire_count
)) {
617 atomic_set(&appldata_expire_count
, num_online_cpus());
618 tasklet_schedule(&appldata_tasklet_struct
);
620 spin_lock(&appldata_timer_lock
);
621 __appldata_vtimer_setup(APPLDATA_MOD_TIMER
);
622 spin_unlock(&appldata_timer_lock
);
626 appldata_cpu_notify(struct notifier_block
*self
,
627 unsigned long action
, void *hcpu
)
631 appldata_online_cpu((long) hcpu
);
633 #ifdef CONFIG_HOTPLUG_CPU
635 appldata_offline_cpu((long) hcpu
);
644 static struct notifier_block __devinitdata appldata_nb
= {
645 .notifier_call
= appldata_cpu_notify
,
651 * init timer and tasklet, register /proc entries
653 static int __init
appldata_init(void)
657 P_DEBUG("sizeof(parameter_list) = %lu\n",
658 sizeof(struct appldata_parameter_list
));
660 for_each_online_cpu(i
)
661 appldata_online_cpu(i
);
663 /* Register cpu hotplug notifier */
664 register_cpu_notifier(&appldata_nb
);
666 appldata_sysctl_header
= register_sysctl_table(appldata_dir_table
, 1);
668 appldata_dir_table
[0].de
->owner
= THIS_MODULE
;
669 appldata_table
[0].de
->owner
= THIS_MODULE
;
670 appldata_table
[1].de
->owner
= THIS_MODULE
;
673 tasklet_init(&appldata_tasklet_struct
, appldata_tasklet_function
, 0);
674 P_DEBUG("Base interface initialized.\n");
681 * stop timer and tasklet, unregister /proc entries
683 static void __exit
appldata_exit(void)
685 struct list_head
*lh
;
686 struct appldata_ops
*ops
;
689 P_DEBUG("Unloading module ...\n");
691 * ops list should be empty, but just in case something went wrong...
693 spin_lock_bh(&appldata_ops_lock
);
694 list_for_each(lh
, &appldata_ops_list
) {
695 ops
= list_entry(lh
, struct appldata_ops
, list
);
696 rc
= appldata_diag(ops
->record_nr
, APPLDATA_STOP_REC
,
697 (unsigned long) ops
->data
, ops
->size
);
699 P_ERROR("STOP DIAG 0xDC for %s failed, "
700 "return code: %d\n", ops
->name
, rc
);
703 spin_unlock_bh(&appldata_ops_lock
);
705 for_each_online_cpu(i
)
706 appldata_offline_cpu(i
);
708 appldata_timer_active
= 0;
710 unregister_sysctl_table(appldata_sysctl_header
);
712 tasklet_kill(&appldata_tasklet_struct
);
713 P_DEBUG("... module unloaded!\n");
715 /**************************** init / exit <END> ******************************/
718 module_init(appldata_init
);
719 module_exit(appldata_exit
);
720 MODULE_LICENSE("GPL");
721 MODULE_AUTHOR("Gerald Schaefer");
722 MODULE_DESCRIPTION("Linux-VM Monitor Stream, base infrastructure");
724 EXPORT_SYMBOL_GPL(appldata_register_ops
);
725 EXPORT_SYMBOL_GPL(appldata_unregister_ops
);
729 * Kernel symbols needed by appldata_mem and appldata_os modules.
730 * However, if this file is compiled as a module (for testing only), these
731 * symbols are not exported. In this case, we define them locally and export
734 void si_swapinfo(struct sysinfo
*val
)
736 val
->freeswap
= -1ul;
737 val
->totalswap
= -1ul;
740 unsigned long avenrun
[3] = {-1 - FIXED_1
/200, -1 - FIXED_1
/200,
744 void get_full_page_state(struct page_state
*ps
)
746 memset(ps
, -1, sizeof(struct page_state
));
749 unsigned long nr_running(void)
754 unsigned long nr_iowait(void)
759 /*unsigned long nr_context_switches(void)
764 EXPORT_SYMBOL_GPL(si_swapinfo
);
765 EXPORT_SYMBOL_GPL(nr_threads
);
766 EXPORT_SYMBOL_GPL(avenrun
);
767 EXPORT_SYMBOL_GPL(get_full_page_state
);
768 EXPORT_SYMBOL_GPL(nr_running
);
769 EXPORT_SYMBOL_GPL(nr_iowait
);
770 //EXPORT_SYMBOL_GPL(nr_context_switches);