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/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>
31 #include <linux/workqueue.h>
36 #define MY_PRINT_NAME "appldata" /* for debug messages, etc. */
37 #define APPLDATA_CPU_INTERVAL 10000 /* default (CPU) time for
41 #define TOD_MICRO 0x01000 /* nr. of TOD clock units
45 * Parameter list for DIAGNOSE X'DC'
48 struct appldata_parameter_list
{
49 u16 diag
; /* The DIAGNOSE code X'00DC' */
50 u8 function
; /* The function code for the DIAGNOSE */
51 u8 parlist_length
; /* Length of the parameter list */
52 u32 product_id_addr
; /* Address of the 16-byte product ID */
54 u16 buffer_length
; /* Length of the application data buffer */
55 u32 buffer_addr
; /* Address of the application data buffer */
58 struct appldata_parameter_list
{
69 #endif /* CONFIG_64BIT */
72 * /proc entries (sysctl)
74 static const char appldata_proc_name
[APPLDATA_PROC_NAME_LENGTH
] = "appldata";
75 static int appldata_timer_handler(ctl_table
*ctl
, int write
, struct file
*filp
,
76 void __user
*buffer
, size_t *lenp
, loff_t
*ppos
);
77 static int appldata_interval_handler(ctl_table
*ctl
, int write
,
80 size_t *lenp
, loff_t
*ppos
);
82 static struct ctl_table_header
*appldata_sysctl_header
;
83 static struct ctl_table appldata_table
[] = {
85 .ctl_name
= CTL_APPLDATA_TIMER
,
87 .mode
= S_IRUGO
| S_IWUSR
,
88 .proc_handler
= &appldata_timer_handler
,
91 .ctl_name
= CTL_APPLDATA_INTERVAL
,
92 .procname
= "interval",
93 .mode
= S_IRUGO
| S_IWUSR
,
94 .proc_handler
= &appldata_interval_handler
,
99 static struct ctl_table appldata_dir_table
[] = {
101 .ctl_name
= CTL_APPLDATA
,
102 .procname
= appldata_proc_name
,
104 .mode
= S_IRUGO
| S_IXUGO
,
105 .child
= appldata_table
,
113 DEFINE_PER_CPU(struct vtimer_list
, appldata_timer
);
114 static atomic_t appldata_expire_count
= ATOMIC_INIT(0);
116 static DEFINE_SPINLOCK(appldata_timer_lock
);
117 static int appldata_interval
= APPLDATA_CPU_INTERVAL
;
118 static int appldata_timer_active
;
123 static struct workqueue_struct
*appldata_wq
;
124 static void appldata_work_fn(void *data
);
125 static DECLARE_WORK(appldata_work
, appldata_work_fn
, NULL
);
131 static DEFINE_SPINLOCK(appldata_ops_lock
);
132 static LIST_HEAD(appldata_ops_list
);
135 /*************************** timer, work, DIAG *******************************/
137 * appldata_timer_function()
139 * schedule work and reschedule timer
141 static void appldata_timer_function(unsigned long data
, struct pt_regs
*regs
)
143 P_DEBUG(" -= Timer =-\n");
144 P_DEBUG("CPU: %i, expire_count: %i\n", smp_processor_id(),
145 atomic_read(&appldata_expire_count
));
146 if (atomic_dec_and_test(&appldata_expire_count
)) {
147 atomic_set(&appldata_expire_count
, num_online_cpus());
148 queue_work(appldata_wq
, (struct work_struct
*) data
);
155 * call data gathering function for each (active) module
157 static void appldata_work_fn(void *data
)
159 struct list_head
*lh
;
160 struct appldata_ops
*ops
;
163 P_DEBUG(" -= Work Queue =-\n");
165 spin_lock(&appldata_ops_lock
);
166 list_for_each(lh
, &appldata_ops_list
) {
167 ops
= list_entry(lh
, struct appldata_ops
, list
);
168 P_DEBUG("list_for_each loop: %i) active = %u, name = %s\n",
169 ++i
, ops
->active
, ops
->name
);
170 if (ops
->active
== 1) {
171 ops
->callback(ops
->data
);
174 spin_unlock(&appldata_ops_lock
);
180 * prepare parameter list, issue DIAG 0xDC
182 int appldata_diag(char record_nr
, u16 function
, unsigned long buffer
,
183 u16 length
, char *mod_lvl
)
186 struct appldata_product_id
{
187 char prod_nr
[7]; /* product nr. */
188 char prod_fn
[2]; /* product function */
189 char record_nr
; /* record nr. */
190 char version_nr
[2]; /* version */
191 char release_nr
[2]; /* release */
192 char mod_lvl
[2]; /* modification lvl. */
193 } appldata_product_id
= {
194 /* all strings are EBCDIC, record_nr is byte */
195 .prod_nr
= {0xD3, 0xC9, 0xD5, 0xE4,
196 0xE7, 0xD2, 0xD9}, /* "LINUXKR" */
197 .prod_fn
= {0xD5, 0xD3}, /* "NL" */
198 .record_nr
= record_nr
,
199 .version_nr
= {0xF2, 0xF6}, /* "26" */
200 .release_nr
= {0xF0, 0xF1}, /* "01" */
201 .mod_lvl
= {mod_lvl
[0], mod_lvl
[1]},
203 struct appldata_parameter_list appldata_parameter_list
= {
205 .function
= function
,
207 sizeof(appldata_parameter_list
),
208 .buffer_length
= length
,
210 (unsigned long) &appldata_product_id
,
211 .buffer_addr
= virt_to_phys((void *) buffer
)
218 "diag %1,%0,0xDC\n\t"
220 : "d" (&appldata_parameter_list
),
221 "m" (appldata_parameter_list
),
222 "m" (appldata_product_id
)
226 /************************ timer, work, DIAG <END> ****************************/
229 /****************************** /proc stuff **********************************/
232 * appldata_mod_vtimer_wrap()
234 * wrapper function for mod_virt_timer(), because smp_call_function_on()
235 * accepts only one parameter.
237 static void __appldata_mod_vtimer_wrap(void *p
) {
239 struct vtimer_list
*timer
;
242 mod_virt_timer(args
->timer
, args
->expires
);
245 #define APPLDATA_ADD_TIMER 0
246 #define APPLDATA_DEL_TIMER 1
247 #define APPLDATA_MOD_TIMER 2
250 * __appldata_vtimer_setup()
252 * Add, delete or modify virtual timers on all online cpus.
253 * The caller needs to get the appldata_timer_lock spinlock.
256 __appldata_vtimer_setup(int cmd
)
258 u64 per_cpu_interval
;
262 case APPLDATA_ADD_TIMER
:
263 if (appldata_timer_active
)
265 per_cpu_interval
= (u64
) (appldata_interval
*1000 /
266 num_online_cpus()) * TOD_MICRO
;
267 for_each_online_cpu(i
) {
268 per_cpu(appldata_timer
, i
).expires
= per_cpu_interval
;
269 smp_call_function_on(add_virt_timer_periodic
,
270 &per_cpu(appldata_timer
, i
),
273 appldata_timer_active
= 1;
274 P_INFO("Monitoring timer started.\n");
276 case APPLDATA_DEL_TIMER
:
277 for_each_online_cpu(i
)
278 del_virt_timer(&per_cpu(appldata_timer
, i
));
279 if (!appldata_timer_active
)
281 appldata_timer_active
= 0;
282 atomic_set(&appldata_expire_count
, num_online_cpus());
283 P_INFO("Monitoring timer stopped.\n");
285 case APPLDATA_MOD_TIMER
:
286 per_cpu_interval
= (u64
) (appldata_interval
*1000 /
287 num_online_cpus()) * TOD_MICRO
;
288 if (!appldata_timer_active
)
290 for_each_online_cpu(i
) {
292 struct vtimer_list
*timer
;
295 args
.timer
= &per_cpu(appldata_timer
, i
);
296 args
.expires
= per_cpu_interval
;
297 smp_call_function_on(__appldata_mod_vtimer_wrap
,
304 * appldata_timer_handler()
306 * Start/Stop timer, show status of timer (0 = not active, 1 = active)
309 appldata_timer_handler(ctl_table
*ctl
, int write
, struct file
*filp
,
310 void __user
*buffer
, size_t *lenp
, loff_t
*ppos
)
315 if (!*lenp
|| *ppos
) {
320 len
= sprintf(buf
, appldata_timer_active
? "1\n" : "0\n");
323 if (copy_to_user(buffer
, buf
, len
))
328 if (copy_from_user(buf
, buffer
, len
> sizeof(buf
) ? sizeof(buf
) : len
))
330 spin_lock(&appldata_timer_lock
);
332 __appldata_vtimer_setup(APPLDATA_ADD_TIMER
);
333 else if (buf
[0] == '0')
334 __appldata_vtimer_setup(APPLDATA_DEL_TIMER
);
335 spin_unlock(&appldata_timer_lock
);
343 * appldata_interval_handler()
345 * Set (CPU) timer interval for collection of data (in milliseconds), show
346 * current timer interval.
349 appldata_interval_handler(ctl_table
*ctl
, int write
, struct file
*filp
,
350 void __user
*buffer
, size_t *lenp
, loff_t
*ppos
)
355 if (!*lenp
|| *ppos
) {
360 len
= sprintf(buf
, "%i\n", appldata_interval
);
363 if (copy_to_user(buffer
, buf
, len
))
368 if (copy_from_user(buf
, buffer
, len
> sizeof(buf
) ? sizeof(buf
) : len
)) {
371 sscanf(buf
, "%i", &interval
);
373 P_ERROR("Timer CPU interval has to be > 0!\n");
377 spin_lock(&appldata_timer_lock
);
378 appldata_interval
= interval
;
379 __appldata_vtimer_setup(APPLDATA_MOD_TIMER
);
380 spin_unlock(&appldata_timer_lock
);
382 P_INFO("Monitoring CPU interval set to %u milliseconds.\n",
391 * appldata_generic_handler()
393 * Generic start/stop monitoring and DIAG, show status of
394 * monitoring (0 = not in process, 1 = in process)
397 appldata_generic_handler(ctl_table
*ctl
, int write
, struct file
*filp
,
398 void __user
*buffer
, size_t *lenp
, loff_t
*ppos
)
400 struct appldata_ops
*ops
= NULL
, *tmp_ops
;
403 struct list_head
*lh
;
406 spin_lock(&appldata_ops_lock
);
407 list_for_each(lh
, &appldata_ops_list
) {
408 tmp_ops
= list_entry(lh
, struct appldata_ops
, list
);
409 if (&tmp_ops
->ctl_table
[2] == ctl
) {
414 spin_unlock(&appldata_ops_lock
);
418 if (!try_module_get(ops
->owner
)) { // protect this function
419 spin_unlock(&appldata_ops_lock
);
422 spin_unlock(&appldata_ops_lock
);
424 if (!*lenp
|| *ppos
) {
426 module_put(ops
->owner
);
430 len
= sprintf(buf
, ops
->active
? "1\n" : "0\n");
433 if (copy_to_user(buffer
, buf
, len
)) {
434 module_put(ops
->owner
);
440 if (copy_from_user(buf
, buffer
,
441 len
> sizeof(buf
) ? sizeof(buf
) : len
)) {
442 module_put(ops
->owner
);
446 spin_lock(&appldata_ops_lock
);
447 if ((buf
[0] == '1') && (ops
->active
== 0)) {
448 // protect work queue callback
449 if (!try_module_get(ops
->owner
)) {
450 spin_unlock(&appldata_ops_lock
);
451 module_put(ops
->owner
);
454 ops
->callback(ops
->data
); // init record
455 rc
= appldata_diag(ops
->record_nr
,
456 APPLDATA_START_INTERVAL_REC
,
457 (unsigned long) ops
->data
, ops
->size
,
460 P_ERROR("START DIAG 0xDC for %s failed, "
461 "return code: %d\n", ops
->name
, rc
);
462 module_put(ops
->owner
);
464 P_INFO("Monitoring %s data enabled, "
465 "DIAG 0xDC started.\n", ops
->name
);
468 } else if ((buf
[0] == '0') && (ops
->active
== 1)) {
470 rc
= appldata_diag(ops
->record_nr
, APPLDATA_STOP_REC
,
471 (unsigned long) ops
->data
, ops
->size
,
474 P_ERROR("STOP DIAG 0xDC for %s failed, "
475 "return code: %d\n", ops
->name
, rc
);
477 P_INFO("Monitoring %s data disabled, "
478 "DIAG 0xDC stopped.\n", ops
->name
);
480 module_put(ops
->owner
);
482 spin_unlock(&appldata_ops_lock
);
486 module_put(ops
->owner
);
490 /*************************** /proc stuff <END> *******************************/
493 /************************* module-ops management *****************************/
495 * appldata_register_ops()
497 * update ops list, register /proc/sys entries
499 int appldata_register_ops(struct appldata_ops
*ops
)
501 struct list_head
*lh
;
502 struct appldata_ops
*tmp_ops
;
507 if ((ops
->size
> APPLDATA_MAX_REC_SIZE
) ||
509 P_ERROR("Invalid size of %s record = %i, maximum = %i!\n",
510 ops
->name
, ops
->size
, APPLDATA_MAX_REC_SIZE
);
513 if ((ops
->ctl_nr
== CTL_APPLDATA
) ||
514 (ops
->ctl_nr
== CTL_APPLDATA_TIMER
) ||
515 (ops
->ctl_nr
== CTL_APPLDATA_INTERVAL
)) {
516 P_ERROR("ctl_nr %i already in use!\n", ops
->ctl_nr
);
519 ops
->ctl_table
= kzalloc(4*sizeof(struct ctl_table
), GFP_KERNEL
);
520 if (ops
->ctl_table
== NULL
) {
521 P_ERROR("Not enough memory for %s ctl_table!\n", ops
->name
);
525 spin_lock(&appldata_ops_lock
);
526 list_for_each(lh
, &appldata_ops_list
) {
527 tmp_ops
= list_entry(lh
, struct appldata_ops
, list
);
528 P_DEBUG("register_ops loop: %i) name = %s, ctl = %i\n",
529 ++i
, tmp_ops
->name
, tmp_ops
->ctl_nr
);
530 P_DEBUG("Comparing %s (ctl %i) with %s (ctl %i)\n",
531 tmp_ops
->name
, tmp_ops
->ctl_nr
, ops
->name
,
533 if (strncmp(tmp_ops
->name
, ops
->name
,
534 APPLDATA_PROC_NAME_LENGTH
) == 0) {
535 P_ERROR("Name \"%s\" already registered!\n", ops
->name
);
536 kfree(ops
->ctl_table
);
537 spin_unlock(&appldata_ops_lock
);
540 if (tmp_ops
->ctl_nr
== ops
->ctl_nr
) {
541 P_ERROR("ctl_nr %i already registered!\n", ops
->ctl_nr
);
542 kfree(ops
->ctl_table
);
543 spin_unlock(&appldata_ops_lock
);
547 list_add(&ops
->list
, &appldata_ops_list
);
548 spin_unlock(&appldata_ops_lock
);
550 ops
->ctl_table
[0].ctl_name
= CTL_APPLDATA
;
551 ops
->ctl_table
[0].procname
= appldata_proc_name
;
552 ops
->ctl_table
[0].maxlen
= 0;
553 ops
->ctl_table
[0].mode
= S_IRUGO
| S_IXUGO
;
554 ops
->ctl_table
[0].child
= &ops
->ctl_table
[2];
556 ops
->ctl_table
[1].ctl_name
= 0;
558 ops
->ctl_table
[2].ctl_name
= ops
->ctl_nr
;
559 ops
->ctl_table
[2].procname
= ops
->name
;
560 ops
->ctl_table
[2].mode
= S_IRUGO
| S_IWUSR
;
561 ops
->ctl_table
[2].proc_handler
= appldata_generic_handler
;
562 ops
->ctl_table
[2].data
= ops
;
564 ops
->ctl_table
[3].ctl_name
= 0;
566 ops
->sysctl_header
= register_sysctl_table(ops
->ctl_table
,1);
568 P_INFO("%s-ops registered!\n", ops
->name
);
573 * appldata_unregister_ops()
575 * update ops list, unregister /proc entries, stop DIAG if necessary
577 void appldata_unregister_ops(struct appldata_ops
*ops
)
580 spin_lock(&appldata_ops_lock
);
581 list_del(&ops
->list
);
582 /* at that point any incoming access will fail */
583 table
= ops
->ctl_table
;
584 ops
->ctl_table
= NULL
;
585 spin_unlock(&appldata_ops_lock
);
586 unregister_sysctl_table(ops
->sysctl_header
);
588 P_INFO("%s-ops unregistered!\n", ops
->name
);
590 /********************** module-ops management <END> **************************/
593 /******************************* init / exit *********************************/
596 appldata_online_cpu(int cpu
)
598 init_virt_timer(&per_cpu(appldata_timer
, cpu
));
599 per_cpu(appldata_timer
, cpu
).function
= appldata_timer_function
;
600 per_cpu(appldata_timer
, cpu
).data
= (unsigned long)
602 atomic_inc(&appldata_expire_count
);
603 spin_lock(&appldata_timer_lock
);
604 __appldata_vtimer_setup(APPLDATA_MOD_TIMER
);
605 spin_unlock(&appldata_timer_lock
);
609 appldata_offline_cpu(int cpu
)
611 del_virt_timer(&per_cpu(appldata_timer
, cpu
));
612 if (atomic_dec_and_test(&appldata_expire_count
)) {
613 atomic_set(&appldata_expire_count
, num_online_cpus());
614 queue_work(appldata_wq
, &appldata_work
);
616 spin_lock(&appldata_timer_lock
);
617 __appldata_vtimer_setup(APPLDATA_MOD_TIMER
);
618 spin_unlock(&appldata_timer_lock
);
622 appldata_cpu_notify(struct notifier_block
*self
,
623 unsigned long action
, void *hcpu
)
627 appldata_online_cpu((long) hcpu
);
629 #ifdef CONFIG_HOTPLUG_CPU
631 appldata_offline_cpu((long) hcpu
);
640 static struct notifier_block __devinitdata appldata_nb
= {
641 .notifier_call
= appldata_cpu_notify
,
647 * init timer, register /proc entries
649 static int __init
appldata_init(void)
653 P_DEBUG("sizeof(parameter_list) = %lu\n",
654 sizeof(struct appldata_parameter_list
));
656 appldata_wq
= create_singlethread_workqueue("appldata");
658 P_ERROR("Could not create work queue\n");
662 for_each_online_cpu(i
)
663 appldata_online_cpu(i
);
665 /* Register cpu hotplug notifier */
666 register_cpu_notifier(&appldata_nb
);
668 appldata_sysctl_header
= register_sysctl_table(appldata_dir_table
, 1);
670 appldata_dir_table
[0].de
->owner
= THIS_MODULE
;
671 appldata_table
[0].de
->owner
= THIS_MODULE
;
672 appldata_table
[1].de
->owner
= THIS_MODULE
;
675 P_DEBUG("Base interface initialized.\n");
682 * stop timer, unregister /proc entries
684 static void __exit
appldata_exit(void)
686 struct list_head
*lh
;
687 struct appldata_ops
*ops
;
690 P_DEBUG("Unloading module ...\n");
692 * ops list should be empty, but just in case something went wrong...
694 spin_lock(&appldata_ops_lock
);
695 list_for_each(lh
, &appldata_ops_list
) {
696 ops
= list_entry(lh
, struct appldata_ops
, list
);
697 rc
= appldata_diag(ops
->record_nr
, APPLDATA_STOP_REC
,
698 (unsigned long) ops
->data
, ops
->size
,
701 P_ERROR("STOP DIAG 0xDC for %s failed, "
702 "return code: %d\n", ops
->name
, rc
);
705 spin_unlock(&appldata_ops_lock
);
707 for_each_online_cpu(i
)
708 appldata_offline_cpu(i
);
710 appldata_timer_active
= 0;
712 unregister_sysctl_table(appldata_sysctl_header
);
714 destroy_workqueue(appldata_wq
);
715 P_DEBUG("... module unloaded!\n");
717 /**************************** init / exit <END> ******************************/
720 module_init(appldata_init
);
721 module_exit(appldata_exit
);
722 MODULE_LICENSE("GPL");
723 MODULE_AUTHOR("Gerald Schaefer");
724 MODULE_DESCRIPTION("Linux-VM Monitor Stream, base infrastructure");
726 EXPORT_SYMBOL_GPL(appldata_register_ops
);
727 EXPORT_SYMBOL_GPL(appldata_unregister_ops
);
728 EXPORT_SYMBOL_GPL(appldata_diag
);
732 * Kernel symbols needed by appldata_mem and appldata_os modules.
733 * However, if this file is compiled as a module (for testing only), these
734 * symbols are not exported. In this case, we define them locally and export
737 void si_swapinfo(struct sysinfo
*val
)
739 val
->freeswap
= -1ul;
740 val
->totalswap
= -1ul;
743 unsigned long avenrun
[3] = {-1 - FIXED_1
/200, -1 - FIXED_1
/200,
747 void get_full_page_state(struct page_state
*ps
)
749 memset(ps
, -1, sizeof(struct page_state
));
752 unsigned long nr_running(void)
757 unsigned long nr_iowait(void)
762 /*unsigned long nr_context_switches(void)
767 EXPORT_SYMBOL_GPL(si_swapinfo
);
768 EXPORT_SYMBOL_GPL(nr_threads
);
769 EXPORT_SYMBOL_GPL(nr_running
);
770 EXPORT_SYMBOL_GPL(nr_iowait
);
771 //EXPORT_SYMBOL_GPL(nr_context_switches);