2 * Base infrastructure for Linux-z/VM Monitor Stream, Stage 1.
3 * Exports appldata_register_ops() and appldata_unregister_ops() for the
4 * data gathering modules.
6 * Copyright IBM Corp. 2003, 2009
8 * Author: Gerald Schaefer <gerald.schaefer@de.ibm.com>
11 #define KMSG_COMPONENT "appldata"
12 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
14 #include <linux/module.h>
15 #include <linux/init.h>
16 #include <linux/slab.h>
17 #include <linux/errno.h>
18 #include <linux/interrupt.h>
19 #include <linux/proc_fs.h>
21 #include <linux/swap.h>
22 #include <linux/pagemap.h>
23 #include <linux/sysctl.h>
24 #include <linux/notifier.h>
25 #include <linux/cpu.h>
26 #include <linux/workqueue.h>
27 #include <linux/suspend.h>
28 #include <linux/platform_device.h>
29 #include <asm/appldata.h>
30 #include <asm/vtimer.h>
31 #include <asm/uaccess.h>
38 #define APPLDATA_CPU_INTERVAL 10000 /* default (CPU) time for
42 #define TOD_MICRO 0x01000 /* nr. of TOD clock units
45 static struct platform_device
*appldata_pdev
;
48 * /proc entries (sysctl)
50 static const char appldata_proc_name
[APPLDATA_PROC_NAME_LENGTH
] = "appldata";
51 static int appldata_timer_handler(struct ctl_table
*ctl
, int write
,
52 void __user
*buffer
, size_t *lenp
, loff_t
*ppos
);
53 static int appldata_interval_handler(struct ctl_table
*ctl
, int write
,
55 size_t *lenp
, loff_t
*ppos
);
57 static struct ctl_table_header
*appldata_sysctl_header
;
58 static struct ctl_table appldata_table
[] = {
61 .mode
= S_IRUGO
| S_IWUSR
,
62 .proc_handler
= appldata_timer_handler
,
65 .procname
= "interval",
66 .mode
= S_IRUGO
| S_IWUSR
,
67 .proc_handler
= appldata_interval_handler
,
72 static struct ctl_table appldata_dir_table
[] = {
74 .procname
= appldata_proc_name
,
76 .mode
= S_IRUGO
| S_IXUGO
,
77 .child
= appldata_table
,
85 static struct vtimer_list appldata_timer
;
87 static DEFINE_SPINLOCK(appldata_timer_lock
);
88 static int appldata_interval
= APPLDATA_CPU_INTERVAL
;
89 static int appldata_timer_active
;
90 static int appldata_timer_suspended
= 0;
95 static struct workqueue_struct
*appldata_wq
;
96 static void appldata_work_fn(struct work_struct
*work
);
97 static DECLARE_WORK(appldata_work
, appldata_work_fn
);
103 static DEFINE_MUTEX(appldata_ops_mutex
);
104 static LIST_HEAD(appldata_ops_list
);
107 /*************************** timer, work, DIAG *******************************/
109 * appldata_timer_function()
111 * schedule work and reschedule timer
113 static void appldata_timer_function(unsigned long data
)
115 queue_work(appldata_wq
, (struct work_struct
*) data
);
121 * call data gathering function for each (active) module
123 static void appldata_work_fn(struct work_struct
*work
)
125 struct list_head
*lh
;
126 struct appldata_ops
*ops
;
128 mutex_lock(&appldata_ops_mutex
);
129 list_for_each(lh
, &appldata_ops_list
) {
130 ops
= list_entry(lh
, struct appldata_ops
, list
);
131 if (ops
->active
== 1) {
132 ops
->callback(ops
->data
);
135 mutex_unlock(&appldata_ops_mutex
);
141 * prepare parameter list, issue DIAG 0xDC
143 int appldata_diag(char record_nr
, u16 function
, unsigned long buffer
,
144 u16 length
, char *mod_lvl
)
146 struct appldata_product_id id
= {
147 .prod_nr
= {0xD3, 0xC9, 0xD5, 0xE4,
148 0xE7, 0xD2, 0xD9}, /* "LINUXKR" */
149 .prod_fn
= 0xD5D3, /* "NL" */
150 .version_nr
= 0xF2F6, /* "26" */
151 .release_nr
= 0xF0F1, /* "01" */
154 id
.record_nr
= record_nr
;
155 id
.mod_lvl
= (mod_lvl
[0]) << 8 | mod_lvl
[1];
156 return appldata_asm(&id
, function
, (void *) buffer
, length
);
158 /************************ timer, work, DIAG <END> ****************************/
161 /****************************** /proc stuff **********************************/
163 #define APPLDATA_ADD_TIMER 0
164 #define APPLDATA_DEL_TIMER 1
165 #define APPLDATA_MOD_TIMER 2
168 * __appldata_vtimer_setup()
170 * Add, delete or modify virtual timers on all online cpus.
171 * The caller needs to get the appldata_timer_lock spinlock.
173 static void __appldata_vtimer_setup(int cmd
)
175 u64 timer_interval
= (u64
) appldata_interval
* 1000 * TOD_MICRO
;
178 case APPLDATA_ADD_TIMER
:
179 if (appldata_timer_active
)
181 appldata_timer
.expires
= timer_interval
;
182 add_virt_timer_periodic(&appldata_timer
);
183 appldata_timer_active
= 1;
185 case APPLDATA_DEL_TIMER
:
186 del_virt_timer(&appldata_timer
);
187 if (!appldata_timer_active
)
189 appldata_timer_active
= 0;
191 case APPLDATA_MOD_TIMER
:
192 if (!appldata_timer_active
)
194 mod_virt_timer_periodic(&appldata_timer
, timer_interval
);
199 * appldata_timer_handler()
201 * Start/Stop timer, show status of timer (0 = not active, 1 = active)
204 appldata_timer_handler(struct ctl_table
*ctl
, int write
,
205 void __user
*buffer
, size_t *lenp
, loff_t
*ppos
)
210 if (!*lenp
|| *ppos
) {
215 strncpy(buf
, appldata_timer_active
? "1\n" : "0\n",
217 len
= strnlen(buf
, ARRAY_SIZE(buf
));
220 if (copy_to_user(buffer
, buf
, len
))
225 if (copy_from_user(buf
, buffer
, len
> sizeof(buf
) ? sizeof(buf
) : len
))
227 spin_lock(&appldata_timer_lock
);
229 __appldata_vtimer_setup(APPLDATA_ADD_TIMER
);
230 else if (buf
[0] == '0')
231 __appldata_vtimer_setup(APPLDATA_DEL_TIMER
);
232 spin_unlock(&appldata_timer_lock
);
240 * appldata_interval_handler()
242 * Set (CPU) timer interval for collection of data (in milliseconds), show
243 * current timer interval.
246 appldata_interval_handler(struct ctl_table
*ctl
, int write
,
247 void __user
*buffer
, size_t *lenp
, loff_t
*ppos
)
253 if (!*lenp
|| *ppos
) {
258 len
= sprintf(buf
, "%i\n", appldata_interval
);
261 if (copy_to_user(buffer
, buf
, len
))
266 if (copy_from_user(buf
, buffer
, len
> sizeof(buf
) ? sizeof(buf
) : len
))
269 sscanf(buf
, "%i", &interval
);
273 spin_lock(&appldata_timer_lock
);
274 appldata_interval
= interval
;
275 __appldata_vtimer_setup(APPLDATA_MOD_TIMER
);
276 spin_unlock(&appldata_timer_lock
);
284 * appldata_generic_handler()
286 * Generic start/stop monitoring and DIAG, show status of
287 * monitoring (0 = not in process, 1 = in process)
290 appldata_generic_handler(struct ctl_table
*ctl
, int write
,
291 void __user
*buffer
, size_t *lenp
, loff_t
*ppos
)
293 struct appldata_ops
*ops
= NULL
, *tmp_ops
;
297 struct list_head
*lh
;
300 mutex_lock(&appldata_ops_mutex
);
301 list_for_each(lh
, &appldata_ops_list
) {
302 tmp_ops
= list_entry(lh
, struct appldata_ops
, list
);
303 if (&tmp_ops
->ctl_table
[2] == ctl
) {
308 mutex_unlock(&appldata_ops_mutex
);
312 if (!try_module_get(ops
->owner
)) { // protect this function
313 mutex_unlock(&appldata_ops_mutex
);
316 mutex_unlock(&appldata_ops_mutex
);
318 if (!*lenp
|| *ppos
) {
320 module_put(ops
->owner
);
324 strncpy(buf
, ops
->active
? "1\n" : "0\n", ARRAY_SIZE(buf
));
325 len
= strnlen(buf
, ARRAY_SIZE(buf
));
328 if (copy_to_user(buffer
, buf
, len
)) {
329 module_put(ops
->owner
);
335 if (copy_from_user(buf
, buffer
,
336 len
> sizeof(buf
) ? sizeof(buf
) : len
)) {
337 module_put(ops
->owner
);
341 mutex_lock(&appldata_ops_mutex
);
342 if ((buf
[0] == '1') && (ops
->active
== 0)) {
343 // protect work queue callback
344 if (!try_module_get(ops
->owner
)) {
345 mutex_unlock(&appldata_ops_mutex
);
346 module_put(ops
->owner
);
349 ops
->callback(ops
->data
); // init record
350 rc
= appldata_diag(ops
->record_nr
,
351 APPLDATA_START_INTERVAL_REC
,
352 (unsigned long) ops
->data
, ops
->size
,
355 pr_err("Starting the data collection for %s "
356 "failed with rc=%d\n", ops
->name
, rc
);
357 module_put(ops
->owner
);
360 } else if ((buf
[0] == '0') && (ops
->active
== 1)) {
362 rc
= appldata_diag(ops
->record_nr
, APPLDATA_STOP_REC
,
363 (unsigned long) ops
->data
, ops
->size
,
366 pr_err("Stopping the data collection for %s "
367 "failed with rc=%d\n", ops
->name
, rc
);
368 module_put(ops
->owner
);
370 mutex_unlock(&appldata_ops_mutex
);
374 module_put(ops
->owner
);
378 /*************************** /proc stuff <END> *******************************/
381 /************************* module-ops management *****************************/
383 * appldata_register_ops()
385 * update ops list, register /proc/sys entries
387 int appldata_register_ops(struct appldata_ops
*ops
)
389 if (ops
->size
> APPLDATA_MAX_REC_SIZE
)
392 ops
->ctl_table
= kzalloc(4 * sizeof(struct ctl_table
), GFP_KERNEL
);
396 mutex_lock(&appldata_ops_mutex
);
397 list_add(&ops
->list
, &appldata_ops_list
);
398 mutex_unlock(&appldata_ops_mutex
);
400 ops
->ctl_table
[0].procname
= appldata_proc_name
;
401 ops
->ctl_table
[0].maxlen
= 0;
402 ops
->ctl_table
[0].mode
= S_IRUGO
| S_IXUGO
;
403 ops
->ctl_table
[0].child
= &ops
->ctl_table
[2];
405 ops
->ctl_table
[2].procname
= ops
->name
;
406 ops
->ctl_table
[2].mode
= S_IRUGO
| S_IWUSR
;
407 ops
->ctl_table
[2].proc_handler
= appldata_generic_handler
;
408 ops
->ctl_table
[2].data
= ops
;
410 ops
->sysctl_header
= register_sysctl_table(ops
->ctl_table
);
411 if (!ops
->sysctl_header
)
415 mutex_lock(&appldata_ops_mutex
);
416 list_del(&ops
->list
);
417 mutex_unlock(&appldata_ops_mutex
);
418 kfree(ops
->ctl_table
);
423 * appldata_unregister_ops()
425 * update ops list, unregister /proc entries, stop DIAG if necessary
427 void appldata_unregister_ops(struct appldata_ops
*ops
)
429 mutex_lock(&appldata_ops_mutex
);
430 list_del(&ops
->list
);
431 mutex_unlock(&appldata_ops_mutex
);
432 unregister_sysctl_table(ops
->sysctl_header
);
433 kfree(ops
->ctl_table
);
435 /********************** module-ops management <END> **************************/
438 /**************************** suspend / resume *******************************/
439 static int appldata_freeze(struct device
*dev
)
441 struct appldata_ops
*ops
;
443 struct list_head
*lh
;
445 spin_lock(&appldata_timer_lock
);
446 if (appldata_timer_active
) {
447 __appldata_vtimer_setup(APPLDATA_DEL_TIMER
);
448 appldata_timer_suspended
= 1;
450 spin_unlock(&appldata_timer_lock
);
452 mutex_lock(&appldata_ops_mutex
);
453 list_for_each(lh
, &appldata_ops_list
) {
454 ops
= list_entry(lh
, struct appldata_ops
, list
);
455 if (ops
->active
== 1) {
456 rc
= appldata_diag(ops
->record_nr
, APPLDATA_STOP_REC
,
457 (unsigned long) ops
->data
, ops
->size
,
460 pr_err("Stopping the data collection for %s "
461 "failed with rc=%d\n", ops
->name
, rc
);
464 mutex_unlock(&appldata_ops_mutex
);
468 static int appldata_restore(struct device
*dev
)
470 struct appldata_ops
*ops
;
472 struct list_head
*lh
;
474 spin_lock(&appldata_timer_lock
);
475 if (appldata_timer_suspended
) {
476 __appldata_vtimer_setup(APPLDATA_ADD_TIMER
);
477 appldata_timer_suspended
= 0;
479 spin_unlock(&appldata_timer_lock
);
481 mutex_lock(&appldata_ops_mutex
);
482 list_for_each(lh
, &appldata_ops_list
) {
483 ops
= list_entry(lh
, struct appldata_ops
, list
);
484 if (ops
->active
== 1) {
485 ops
->callback(ops
->data
); // init record
486 rc
= appldata_diag(ops
->record_nr
,
487 APPLDATA_START_INTERVAL_REC
,
488 (unsigned long) ops
->data
, ops
->size
,
491 pr_err("Starting the data collection for %s "
492 "failed with rc=%d\n", ops
->name
, rc
);
496 mutex_unlock(&appldata_ops_mutex
);
500 static int appldata_thaw(struct device
*dev
)
502 return appldata_restore(dev
);
505 static const struct dev_pm_ops appldata_pm_ops
= {
506 .freeze
= appldata_freeze
,
507 .thaw
= appldata_thaw
,
508 .restore
= appldata_restore
,
511 static struct platform_driver appldata_pdrv
= {
514 .owner
= THIS_MODULE
,
515 .pm
= &appldata_pm_ops
,
518 /************************* suspend / resume <END> ****************************/
521 /******************************* init / exit *********************************/
526 * init timer, register /proc entries
528 static int __init
appldata_init(void)
532 init_virt_timer(&appldata_timer
);
533 appldata_timer
.function
= appldata_timer_function
;
534 appldata_timer
.data
= (unsigned long) &appldata_work
;
536 rc
= platform_driver_register(&appldata_pdrv
);
540 appldata_pdev
= platform_device_register_simple("appldata", -1, NULL
,
542 if (IS_ERR(appldata_pdev
)) {
543 rc
= PTR_ERR(appldata_pdev
);
546 appldata_wq
= create_singlethread_workqueue("appldata");
552 appldata_sysctl_header
= register_sysctl_table(appldata_dir_table
);
556 platform_device_unregister(appldata_pdev
);
558 platform_driver_unregister(&appldata_pdrv
);
562 __initcall(appldata_init
);
564 /**************************** init / exit <END> ******************************/
566 EXPORT_SYMBOL_GPL(appldata_register_ops
);
567 EXPORT_SYMBOL_GPL(appldata_unregister_ops
);
568 EXPORT_SYMBOL_GPL(appldata_diag
);
571 EXPORT_SYMBOL_GPL(si_swapinfo
);
573 EXPORT_SYMBOL_GPL(nr_threads
);
574 EXPORT_SYMBOL_GPL(nr_running
);
575 EXPORT_SYMBOL_GPL(nr_iowait
);