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 <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>
28 #include <linux/notifier.h>
29 #include <linux/cpu.h>
30 #include <linux/workqueue.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
44 * Parameter list for DIAGNOSE X'DC'
47 struct appldata_parameter_list
{
48 u16 diag
; /* The DIAGNOSE code X'00DC' */
49 u8 function
; /* The function code for the DIAGNOSE */
50 u8 parlist_length
; /* Length of the parameter list */
51 u32 product_id_addr
; /* Address of the 16-byte product ID */
53 u16 buffer_length
; /* Length of the application data buffer */
54 u32 buffer_addr
; /* Address of the application data buffer */
57 struct appldata_parameter_list
{
68 #endif /* CONFIG_64BIT */
71 * /proc entries (sysctl)
73 static const char appldata_proc_name
[APPLDATA_PROC_NAME_LENGTH
] = "appldata";
74 static int appldata_timer_handler(ctl_table
*ctl
, int write
, struct file
*filp
,
75 void __user
*buffer
, size_t *lenp
, loff_t
*ppos
);
76 static int appldata_interval_handler(ctl_table
*ctl
, int write
,
79 size_t *lenp
, loff_t
*ppos
);
81 static struct ctl_table_header
*appldata_sysctl_header
;
82 static struct ctl_table appldata_table
[] = {
84 .ctl_name
= CTL_APPLDATA_TIMER
,
86 .mode
= S_IRUGO
| S_IWUSR
,
87 .proc_handler
= &appldata_timer_handler
,
90 .ctl_name
= CTL_APPLDATA_INTERVAL
,
91 .procname
= "interval",
92 .mode
= S_IRUGO
| S_IWUSR
,
93 .proc_handler
= &appldata_interval_handler
,
98 static struct ctl_table appldata_dir_table
[] = {
100 .ctl_name
= CTL_APPLDATA
,
101 .procname
= appldata_proc_name
,
103 .mode
= S_IRUGO
| S_IXUGO
,
104 .child
= appldata_table
,
112 DEFINE_PER_CPU(struct vtimer_list
, appldata_timer
);
113 static atomic_t appldata_expire_count
= ATOMIC_INIT(0);
115 static DEFINE_SPINLOCK(appldata_timer_lock
);
116 static int appldata_interval
= APPLDATA_CPU_INTERVAL
;
117 static int appldata_timer_active
;
122 static struct workqueue_struct
*appldata_wq
;
123 static void appldata_work_fn(void *data
);
124 static DECLARE_WORK(appldata_work
, appldata_work_fn
, NULL
);
130 static DEFINE_SPINLOCK(appldata_ops_lock
);
131 static LIST_HEAD(appldata_ops_list
);
134 /*************************** timer, work, DIAG *******************************/
136 * appldata_timer_function()
138 * schedule work and reschedule timer
140 static void appldata_timer_function(unsigned long data
, struct pt_regs
*regs
)
142 P_DEBUG(" -= Timer =-\n");
143 P_DEBUG("CPU: %i, expire_count: %i\n", smp_processor_id(),
144 atomic_read(&appldata_expire_count
));
145 if (atomic_dec_and_test(&appldata_expire_count
)) {
146 atomic_set(&appldata_expire_count
, num_online_cpus());
147 queue_work(appldata_wq
, (struct work_struct
*) data
);
154 * call data gathering function for each (active) module
156 static void appldata_work_fn(void *data
)
158 struct list_head
*lh
;
159 struct appldata_ops
*ops
;
162 P_DEBUG(" -= Work Queue =-\n");
164 spin_lock(&appldata_ops_lock
);
165 list_for_each(lh
, &appldata_ops_list
) {
166 ops
= list_entry(lh
, struct appldata_ops
, list
);
167 P_DEBUG("list_for_each loop: %i) active = %u, name = %s\n",
168 ++i
, ops
->active
, ops
->name
);
169 if (ops
->active
== 1) {
170 ops
->callback(ops
->data
);
173 spin_unlock(&appldata_ops_lock
);
179 * prepare parameter list, issue DIAG 0xDC
181 int appldata_diag(char record_nr
, u16 function
, unsigned long buffer
,
182 u16 length
, char *mod_lvl
)
185 struct appldata_product_id
{
186 char prod_nr
[7]; /* product nr. */
187 char prod_fn
[2]; /* product function */
188 char record_nr
; /* record nr. */
189 char version_nr
[2]; /* version */
190 char release_nr
[2]; /* release */
191 char mod_lvl
[2]; /* modification lvl. */
192 } appldata_product_id
= {
193 /* all strings are EBCDIC, record_nr is byte */
194 .prod_nr
= {0xD3, 0xC9, 0xD5, 0xE4,
195 0xE7, 0xD2, 0xD9}, /* "LINUXKR" */
196 .prod_fn
= {0xD5, 0xD3}, /* "NL" */
197 .record_nr
= record_nr
,
198 .version_nr
= {0xF2, 0xF6}, /* "26" */
199 .release_nr
= {0xF0, 0xF1}, /* "01" */
200 .mod_lvl
= {mod_lvl
[0], mod_lvl
[1]},
202 struct appldata_parameter_list appldata_parameter_list
= {
204 .function
= function
,
206 sizeof(appldata_parameter_list
),
207 .buffer_length
= length
,
209 (unsigned long) &appldata_product_id
,
210 .buffer_addr
= virt_to_phys((void *) buffer
)
217 "diag %1,%0,0xDC\n\t"
219 : "d" (&appldata_parameter_list
),
220 "m" (appldata_parameter_list
),
221 "m" (appldata_product_id
)
225 /************************ timer, work, DIAG <END> ****************************/
228 /****************************** /proc stuff **********************************/
231 * appldata_mod_vtimer_wrap()
233 * wrapper function for mod_virt_timer(), because smp_call_function_on()
234 * accepts only one parameter.
236 static void __appldata_mod_vtimer_wrap(void *p
) {
238 struct vtimer_list
*timer
;
241 mod_virt_timer(args
->timer
, args
->expires
);
244 #define APPLDATA_ADD_TIMER 0
245 #define APPLDATA_DEL_TIMER 1
246 #define APPLDATA_MOD_TIMER 2
249 * __appldata_vtimer_setup()
251 * Add, delete or modify virtual timers on all online cpus.
252 * The caller needs to get the appldata_timer_lock spinlock.
255 __appldata_vtimer_setup(int cmd
)
257 u64 per_cpu_interval
;
261 case APPLDATA_ADD_TIMER
:
262 if (appldata_timer_active
)
264 per_cpu_interval
= (u64
) (appldata_interval
*1000 /
265 num_online_cpus()) * TOD_MICRO
;
266 for_each_online_cpu(i
) {
267 per_cpu(appldata_timer
, i
).expires
= per_cpu_interval
;
268 smp_call_function_on(add_virt_timer_periodic
,
269 &per_cpu(appldata_timer
, i
),
272 appldata_timer_active
= 1;
273 P_INFO("Monitoring timer started.\n");
275 case APPLDATA_DEL_TIMER
:
276 for_each_online_cpu(i
)
277 del_virt_timer(&per_cpu(appldata_timer
, i
));
278 if (!appldata_timer_active
)
280 appldata_timer_active
= 0;
281 atomic_set(&appldata_expire_count
, num_online_cpus());
282 P_INFO("Monitoring timer stopped.\n");
284 case APPLDATA_MOD_TIMER
:
285 per_cpu_interval
= (u64
) (appldata_interval
*1000 /
286 num_online_cpus()) * TOD_MICRO
;
287 if (!appldata_timer_active
)
289 for_each_online_cpu(i
) {
291 struct vtimer_list
*timer
;
294 args
.timer
= &per_cpu(appldata_timer
, i
);
295 args
.expires
= per_cpu_interval
;
296 smp_call_function_on(__appldata_mod_vtimer_wrap
,
303 * appldata_timer_handler()
305 * Start/Stop timer, show status of timer (0 = not active, 1 = active)
308 appldata_timer_handler(ctl_table
*ctl
, int write
, struct file
*filp
,
309 void __user
*buffer
, size_t *lenp
, loff_t
*ppos
)
314 if (!*lenp
|| *ppos
) {
319 len
= sprintf(buf
, appldata_timer_active
? "1\n" : "0\n");
322 if (copy_to_user(buffer
, buf
, len
))
327 if (copy_from_user(buf
, buffer
, len
> sizeof(buf
) ? sizeof(buf
) : len
))
329 spin_lock(&appldata_timer_lock
);
331 __appldata_vtimer_setup(APPLDATA_ADD_TIMER
);
332 else if (buf
[0] == '0')
333 __appldata_vtimer_setup(APPLDATA_DEL_TIMER
);
334 spin_unlock(&appldata_timer_lock
);
342 * appldata_interval_handler()
344 * Set (CPU) timer interval for collection of data (in milliseconds), show
345 * current timer interval.
348 appldata_interval_handler(ctl_table
*ctl
, int write
, struct file
*filp
,
349 void __user
*buffer
, size_t *lenp
, loff_t
*ppos
)
354 if (!*lenp
|| *ppos
) {
359 len
= sprintf(buf
, "%i\n", appldata_interval
);
362 if (copy_to_user(buffer
, buf
, len
))
367 if (copy_from_user(buf
, buffer
, len
> sizeof(buf
) ? sizeof(buf
) : len
)) {
370 sscanf(buf
, "%i", &interval
);
372 P_ERROR("Timer CPU interval has to be > 0!\n");
376 spin_lock(&appldata_timer_lock
);
377 appldata_interval
= interval
;
378 __appldata_vtimer_setup(APPLDATA_MOD_TIMER
);
379 spin_unlock(&appldata_timer_lock
);
381 P_INFO("Monitoring CPU interval set to %u milliseconds.\n",
390 * appldata_generic_handler()
392 * Generic start/stop monitoring and DIAG, show status of
393 * monitoring (0 = not in process, 1 = in process)
396 appldata_generic_handler(ctl_table
*ctl
, int write
, struct file
*filp
,
397 void __user
*buffer
, size_t *lenp
, loff_t
*ppos
)
399 struct appldata_ops
*ops
= NULL
, *tmp_ops
;
402 struct list_head
*lh
;
405 spin_lock(&appldata_ops_lock
);
406 list_for_each(lh
, &appldata_ops_list
) {
407 tmp_ops
= list_entry(lh
, struct appldata_ops
, list
);
408 if (&tmp_ops
->ctl_table
[2] == ctl
) {
413 spin_unlock(&appldata_ops_lock
);
417 if (!try_module_get(ops
->owner
)) { // protect this function
418 spin_unlock(&appldata_ops_lock
);
421 spin_unlock(&appldata_ops_lock
);
423 if (!*lenp
|| *ppos
) {
425 module_put(ops
->owner
);
429 len
= sprintf(buf
, ops
->active
? "1\n" : "0\n");
432 if (copy_to_user(buffer
, buf
, len
)) {
433 module_put(ops
->owner
);
439 if (copy_from_user(buf
, buffer
,
440 len
> sizeof(buf
) ? sizeof(buf
) : len
)) {
441 module_put(ops
->owner
);
445 spin_lock(&appldata_ops_lock
);
446 if ((buf
[0] == '1') && (ops
->active
== 0)) {
447 // protect work queue callback
448 if (!try_module_get(ops
->owner
)) {
449 spin_unlock(&appldata_ops_lock
);
450 module_put(ops
->owner
);
453 ops
->callback(ops
->data
); // init record
454 rc
= appldata_diag(ops
->record_nr
,
455 APPLDATA_START_INTERVAL_REC
,
456 (unsigned long) ops
->data
, ops
->size
,
459 P_ERROR("START DIAG 0xDC for %s failed, "
460 "return code: %d\n", ops
->name
, rc
);
461 module_put(ops
->owner
);
463 P_INFO("Monitoring %s data enabled, "
464 "DIAG 0xDC started.\n", ops
->name
);
467 } else if ((buf
[0] == '0') && (ops
->active
== 1)) {
469 rc
= appldata_diag(ops
->record_nr
, APPLDATA_STOP_REC
,
470 (unsigned long) ops
->data
, ops
->size
,
473 P_ERROR("STOP DIAG 0xDC for %s failed, "
474 "return code: %d\n", ops
->name
, rc
);
476 P_INFO("Monitoring %s data disabled, "
477 "DIAG 0xDC stopped.\n", ops
->name
);
479 module_put(ops
->owner
);
481 spin_unlock(&appldata_ops_lock
);
485 module_put(ops
->owner
);
489 /*************************** /proc stuff <END> *******************************/
492 /************************* module-ops management *****************************/
494 * appldata_register_ops()
496 * update ops list, register /proc/sys entries
498 int appldata_register_ops(struct appldata_ops
*ops
)
500 struct list_head
*lh
;
501 struct appldata_ops
*tmp_ops
;
506 if ((ops
->size
> APPLDATA_MAX_REC_SIZE
) ||
508 P_ERROR("Invalid size of %s record = %i, maximum = %i!\n",
509 ops
->name
, ops
->size
, APPLDATA_MAX_REC_SIZE
);
512 if ((ops
->ctl_nr
== CTL_APPLDATA
) ||
513 (ops
->ctl_nr
== CTL_APPLDATA_TIMER
) ||
514 (ops
->ctl_nr
== CTL_APPLDATA_INTERVAL
)) {
515 P_ERROR("ctl_nr %i already in use!\n", ops
->ctl_nr
);
518 ops
->ctl_table
= kzalloc(4*sizeof(struct ctl_table
), GFP_KERNEL
);
519 if (ops
->ctl_table
== NULL
) {
520 P_ERROR("Not enough memory for %s ctl_table!\n", ops
->name
);
524 spin_lock(&appldata_ops_lock
);
525 list_for_each(lh
, &appldata_ops_list
) {
526 tmp_ops
= list_entry(lh
, struct appldata_ops
, list
);
527 P_DEBUG("register_ops loop: %i) name = %s, ctl = %i\n",
528 ++i
, tmp_ops
->name
, tmp_ops
->ctl_nr
);
529 P_DEBUG("Comparing %s (ctl %i) with %s (ctl %i)\n",
530 tmp_ops
->name
, tmp_ops
->ctl_nr
, ops
->name
,
532 if (strncmp(tmp_ops
->name
, ops
->name
,
533 APPLDATA_PROC_NAME_LENGTH
) == 0) {
534 P_ERROR("Name \"%s\" already registered!\n", ops
->name
);
535 kfree(ops
->ctl_table
);
536 spin_unlock(&appldata_ops_lock
);
539 if (tmp_ops
->ctl_nr
== ops
->ctl_nr
) {
540 P_ERROR("ctl_nr %i already registered!\n", ops
->ctl_nr
);
541 kfree(ops
->ctl_table
);
542 spin_unlock(&appldata_ops_lock
);
546 list_add(&ops
->list
, &appldata_ops_list
);
547 spin_unlock(&appldata_ops_lock
);
549 ops
->ctl_table
[0].ctl_name
= CTL_APPLDATA
;
550 ops
->ctl_table
[0].procname
= appldata_proc_name
;
551 ops
->ctl_table
[0].maxlen
= 0;
552 ops
->ctl_table
[0].mode
= S_IRUGO
| S_IXUGO
;
553 ops
->ctl_table
[0].child
= &ops
->ctl_table
[2];
555 ops
->ctl_table
[1].ctl_name
= 0;
557 ops
->ctl_table
[2].ctl_name
= ops
->ctl_nr
;
558 ops
->ctl_table
[2].procname
= ops
->name
;
559 ops
->ctl_table
[2].mode
= S_IRUGO
| S_IWUSR
;
560 ops
->ctl_table
[2].proc_handler
= appldata_generic_handler
;
561 ops
->ctl_table
[2].data
= ops
;
563 ops
->ctl_table
[3].ctl_name
= 0;
565 ops
->sysctl_header
= register_sysctl_table(ops
->ctl_table
,1);
567 P_INFO("%s-ops registered!\n", ops
->name
);
572 * appldata_unregister_ops()
574 * update ops list, unregister /proc entries, stop DIAG if necessary
576 void appldata_unregister_ops(struct appldata_ops
*ops
)
579 spin_lock(&appldata_ops_lock
);
580 list_del(&ops
->list
);
581 /* at that point any incoming access will fail */
582 table
= ops
->ctl_table
;
583 ops
->ctl_table
= NULL
;
584 spin_unlock(&appldata_ops_lock
);
585 unregister_sysctl_table(ops
->sysctl_header
);
587 P_INFO("%s-ops unregistered!\n", ops
->name
);
589 /********************** module-ops management <END> **************************/
592 /******************************* init / exit *********************************/
595 appldata_online_cpu(int cpu
)
597 init_virt_timer(&per_cpu(appldata_timer
, cpu
));
598 per_cpu(appldata_timer
, cpu
).function
= appldata_timer_function
;
599 per_cpu(appldata_timer
, cpu
).data
= (unsigned long)
601 atomic_inc(&appldata_expire_count
);
602 spin_lock(&appldata_timer_lock
);
603 __appldata_vtimer_setup(APPLDATA_MOD_TIMER
);
604 spin_unlock(&appldata_timer_lock
);
608 appldata_offline_cpu(int cpu
)
610 del_virt_timer(&per_cpu(appldata_timer
, cpu
));
611 if (atomic_dec_and_test(&appldata_expire_count
)) {
612 atomic_set(&appldata_expire_count
, num_online_cpus());
613 queue_work(appldata_wq
, &appldata_work
);
615 spin_lock(&appldata_timer_lock
);
616 __appldata_vtimer_setup(APPLDATA_MOD_TIMER
);
617 spin_unlock(&appldata_timer_lock
);
621 appldata_cpu_notify(struct notifier_block
*self
,
622 unsigned long action
, void *hcpu
)
626 appldata_online_cpu((long) hcpu
);
628 #ifdef CONFIG_HOTPLUG_CPU
630 appldata_offline_cpu((long) hcpu
);
639 static struct notifier_block __devinitdata appldata_nb
= {
640 .notifier_call
= appldata_cpu_notify
,
646 * init timer, register /proc entries
648 static int __init
appldata_init(void)
652 P_DEBUG("sizeof(parameter_list) = %lu\n",
653 sizeof(struct appldata_parameter_list
));
655 appldata_wq
= create_singlethread_workqueue("appldata");
657 P_ERROR("Could not create work queue\n");
661 for_each_online_cpu(i
)
662 appldata_online_cpu(i
);
664 /* Register cpu hotplug notifier */
665 register_cpu_notifier(&appldata_nb
);
667 appldata_sysctl_header
= register_sysctl_table(appldata_dir_table
, 1);
669 appldata_dir_table
[0].de
->owner
= THIS_MODULE
;
670 appldata_table
[0].de
->owner
= THIS_MODULE
;
671 appldata_table
[1].de
->owner
= THIS_MODULE
;
674 P_DEBUG("Base interface initialized.\n");
681 * stop timer, 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(&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
,
700 P_ERROR("STOP DIAG 0xDC for %s failed, "
701 "return code: %d\n", ops
->name
, rc
);
704 spin_unlock(&appldata_ops_lock
);
706 for_each_online_cpu(i
)
707 appldata_offline_cpu(i
);
709 appldata_timer_active
= 0;
711 unregister_sysctl_table(appldata_sysctl_header
);
713 destroy_workqueue(appldata_wq
);
714 P_DEBUG("... module unloaded!\n");
716 /**************************** init / exit <END> ******************************/
719 module_init(appldata_init
);
720 module_exit(appldata_exit
);
721 MODULE_LICENSE("GPL");
722 MODULE_AUTHOR("Gerald Schaefer");
723 MODULE_DESCRIPTION("Linux-VM Monitor Stream, base infrastructure");
725 EXPORT_SYMBOL_GPL(appldata_register_ops
);
726 EXPORT_SYMBOL_GPL(appldata_unregister_ops
);
727 EXPORT_SYMBOL_GPL(appldata_diag
);
731 * Kernel symbols needed by appldata_mem and appldata_os modules.
732 * However, if this file is compiled as a module (for testing only), these
733 * symbols are not exported. In this case, we define them locally and export
736 void si_swapinfo(struct sysinfo
*val
)
738 val
->freeswap
= -1ul;
739 val
->totalswap
= -1ul;
742 unsigned long avenrun
[3] = {-1 - FIXED_1
/200, -1 - FIXED_1
/200,
746 void get_full_page_state(struct page_state
*ps
)
748 memset(ps
, -1, sizeof(struct page_state
));
751 unsigned long nr_running(void)
756 unsigned long nr_iowait(void)
761 /*unsigned long nr_context_switches(void)
766 EXPORT_SYMBOL_GPL(si_swapinfo
);
767 EXPORT_SYMBOL_GPL(nr_threads
);
768 EXPORT_SYMBOL_GPL(nr_running
);
769 EXPORT_SYMBOL_GPL(nr_iowait
);
770 //EXPORT_SYMBOL_GPL(nr_context_switches);