blob: a0a94e0ef8d121b38542c965def18842cfc845f2 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * arch/s390/appldata/appldata_base.c
3 *
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.
7 *
Gerald Schaefer5b5dd212006-06-29 15:08:35 +02008 * Copyright (C) 2003,2006 IBM Corporation, IBM Deutschland Entwicklung GmbH.
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 *
Gerald Schaefer5b5dd212006-06-29 15:08:35 +020010 * Author: Gerald Schaefer <gerald.schaefer@de.ibm.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 */
12
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/module.h>
14#include <linux/init.h>
15#include <linux/slab.h>
16#include <linux/errno.h>
17#include <asm/uaccess.h>
18#include <asm/io.h>
19#include <asm/smp.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>
Gerald Schaeferf26d5832005-06-04 15:43:33 -070030#include <linux/workqueue.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
32#include "appldata.h"
33
34
35#define MY_PRINT_NAME "appldata" /* for debug messages, etc. */
36#define APPLDATA_CPU_INTERVAL 10000 /* default (CPU) time for
37 sampling interval in
38 milliseconds */
39
40#define TOD_MICRO 0x01000 /* nr. of TOD clock units
41 for 1 microsecond */
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
43/*
44 * Parameter list for DIAGNOSE X'DC'
45 */
Martin Schwidefsky347a8dc2006-01-06 00:19:28 -080046#ifndef CONFIG_64BIT
Linus Torvalds1da177e2005-04-16 15:20:36 -070047struct 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 */
52 u16 reserved;
53 u16 buffer_length; /* Length of the application data buffer */
54 u32 buffer_addr; /* Address of the application data buffer */
55};
56#else
57struct appldata_parameter_list {
58 u16 diag;
59 u8 function;
60 u8 parlist_length;
61 u32 unused01;
62 u16 reserved;
63 u16 buffer_length;
64 u32 unused02;
65 u64 product_id_addr;
66 u64 buffer_addr;
67};
Martin Schwidefsky347a8dc2006-01-06 00:19:28 -080068#endif /* CONFIG_64BIT */
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
70/*
71 * /proc entries (sysctl)
72 */
73static const char appldata_proc_name[APPLDATA_PROC_NAME_LENGTH] = "appldata";
74static int appldata_timer_handler(ctl_table *ctl, int write, struct file *filp,
75 void __user *buffer, size_t *lenp, loff_t *ppos);
76static int appldata_interval_handler(ctl_table *ctl, int write,
77 struct file *filp,
78 void __user *buffer,
79 size_t *lenp, loff_t *ppos);
80
81static struct ctl_table_header *appldata_sysctl_header;
82static struct ctl_table appldata_table[] = {
83 {
84 .ctl_name = CTL_APPLDATA_TIMER,
85 .procname = "timer",
86 .mode = S_IRUGO | S_IWUSR,
87 .proc_handler = &appldata_timer_handler,
88 },
89 {
90 .ctl_name = CTL_APPLDATA_INTERVAL,
91 .procname = "interval",
92 .mode = S_IRUGO | S_IWUSR,
93 .proc_handler = &appldata_interval_handler,
94 },
95 { .ctl_name = 0 }
96};
97
98static struct ctl_table appldata_dir_table[] = {
99 {
100 .ctl_name = CTL_APPLDATA,
101 .procname = appldata_proc_name,
102 .maxlen = 0,
103 .mode = S_IRUGO | S_IXUGO,
104 .child = appldata_table,
105 },
106 { .ctl_name = 0 }
107};
108
109/*
110 * Timer
111 */
112DEFINE_PER_CPU(struct vtimer_list, appldata_timer);
113static atomic_t appldata_expire_count = ATOMIC_INIT(0);
114
115static DEFINE_SPINLOCK(appldata_timer_lock);
116static int appldata_interval = APPLDATA_CPU_INTERVAL;
117static int appldata_timer_active;
118
119/*
Gerald Schaeferf26d5832005-06-04 15:43:33 -0700120 * Work queue
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 */
Gerald Schaeferf26d5832005-06-04 15:43:33 -0700122static struct workqueue_struct *appldata_wq;
123static void appldata_work_fn(void *data);
124static DECLARE_WORK(appldata_work, appldata_work_fn, NULL);
125
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
127/*
128 * Ops list
129 */
130static DEFINE_SPINLOCK(appldata_ops_lock);
131static LIST_HEAD(appldata_ops_list);
132
133
Gerald Schaeferf26d5832005-06-04 15:43:33 -0700134/*************************** timer, work, DIAG *******************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135/*
136 * appldata_timer_function()
137 *
Gerald Schaeferf26d5832005-06-04 15:43:33 -0700138 * schedule work and reschedule timer
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 */
140static void appldata_timer_function(unsigned long data, struct pt_regs *regs)
141{
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());
Gerald Schaeferf26d5832005-06-04 15:43:33 -0700147 queue_work(appldata_wq, (struct work_struct *) data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 }
149}
150
151/*
Gerald Schaeferf26d5832005-06-04 15:43:33 -0700152 * appldata_work_fn()
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 *
154 * call data gathering function for each (active) module
155 */
Gerald Schaeferf26d5832005-06-04 15:43:33 -0700156static void appldata_work_fn(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157{
158 struct list_head *lh;
159 struct appldata_ops *ops;
160 int i;
161
Gerald Schaeferf26d5832005-06-04 15:43:33 -0700162 P_DEBUG(" -= Work Queue =-\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 i = 0;
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);
171 }
172 }
173 spin_unlock(&appldata_ops_lock);
174}
175
176/*
177 * appldata_diag()
178 *
179 * prepare parameter list, issue DIAG 0xDC
180 */
Gerald Schaefer5b5dd212006-06-29 15:08:35 +0200181int appldata_diag(char record_nr, u16 function, unsigned long buffer,
182 u16 length, char *mod_lvl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183{
184 unsigned long ry;
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" */
Gerald Schaefer5b5dd212006-06-29 15:08:35 +0200200 .mod_lvl = {mod_lvl[0], mod_lvl[1]},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 };
202 struct appldata_parameter_list appldata_parameter_list = {
203 .diag = 0xDC,
204 .function = function,
205 .parlist_length =
206 sizeof(appldata_parameter_list),
207 .buffer_length = length,
208 .product_id_addr =
209 (unsigned long) &appldata_product_id,
210 .buffer_addr = virt_to_phys((void *) buffer)
211 };
212
213 if (!MACHINE_IS_VM)
214 return -ENOSYS;
215 ry = -1;
216 asm volatile(
217 "diag %1,%0,0xDC\n\t"
Gerald Schaeferb87a1e52005-07-29 14:03:38 -0700218 : "=d" (ry)
219 : "d" (&appldata_parameter_list),
220 "m" (appldata_parameter_list),
221 "m" (appldata_product_id)
222 : "cc");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 return (int) ry;
224}
Gerald Schaeferf26d5832005-06-04 15:43:33 -0700225/************************ timer, work, DIAG <END> ****************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226
227
228/****************************** /proc stuff **********************************/
229
230/*
231 * appldata_mod_vtimer_wrap()
232 *
233 * wrapper function for mod_virt_timer(), because smp_call_function_on()
234 * accepts only one parameter.
235 */
236static void __appldata_mod_vtimer_wrap(void *p) {
237 struct {
238 struct vtimer_list *timer;
239 u64 expires;
240 } *args = p;
241 mod_virt_timer(args->timer, args->expires);
242}
243
244#define APPLDATA_ADD_TIMER 0
245#define APPLDATA_DEL_TIMER 1
246#define APPLDATA_MOD_TIMER 2
247
248/*
249 * __appldata_vtimer_setup()
250 *
251 * Add, delete or modify virtual timers on all online cpus.
252 * The caller needs to get the appldata_timer_lock spinlock.
253 */
254static void
255__appldata_vtimer_setup(int cmd)
256{
257 u64 per_cpu_interval;
258 int i;
259
260 switch (cmd) {
261 case APPLDATA_ADD_TIMER:
262 if (appldata_timer_active)
263 break;
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),
270 0, 1, i);
271 }
272 appldata_timer_active = 1;
273 P_INFO("Monitoring timer started.\n");
274 break;
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)
279 break;
280 appldata_timer_active = 0;
281 atomic_set(&appldata_expire_count, num_online_cpus());
282 P_INFO("Monitoring timer stopped.\n");
283 break;
284 case APPLDATA_MOD_TIMER:
285 per_cpu_interval = (u64) (appldata_interval*1000 /
286 num_online_cpus()) * TOD_MICRO;
287 if (!appldata_timer_active)
288 break;
289 for_each_online_cpu(i) {
290 struct {
291 struct vtimer_list *timer;
292 u64 expires;
293 } args;
294 args.timer = &per_cpu(appldata_timer, i);
295 args.expires = per_cpu_interval;
296 smp_call_function_on(__appldata_mod_vtimer_wrap,
297 &args, 0, 1, i);
298 }
299 }
300}
301
302/*
303 * appldata_timer_handler()
304 *
305 * Start/Stop timer, show status of timer (0 = not active, 1 = active)
306 */
307static int
308appldata_timer_handler(ctl_table *ctl, int write, struct file *filp,
309 void __user *buffer, size_t *lenp, loff_t *ppos)
310{
311 int len;
312 char buf[2];
313
314 if (!*lenp || *ppos) {
315 *lenp = 0;
316 return 0;
317 }
318 if (!write) {
319 len = sprintf(buf, appldata_timer_active ? "1\n" : "0\n");
320 if (len > *lenp)
321 len = *lenp;
322 if (copy_to_user(buffer, buf, len))
323 return -EFAULT;
324 goto out;
325 }
326 len = *lenp;
327 if (copy_from_user(buf, buffer, len > sizeof(buf) ? sizeof(buf) : len))
328 return -EFAULT;
329 spin_lock(&appldata_timer_lock);
330 if (buf[0] == '1')
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);
335out:
336 *lenp = len;
337 *ppos += len;
338 return 0;
339}
340
341/*
342 * appldata_interval_handler()
343 *
344 * Set (CPU) timer interval for collection of data (in milliseconds), show
345 * current timer interval.
346 */
347static int
348appldata_interval_handler(ctl_table *ctl, int write, struct file *filp,
349 void __user *buffer, size_t *lenp, loff_t *ppos)
350{
351 int len, interval;
352 char buf[16];
353
354 if (!*lenp || *ppos) {
355 *lenp = 0;
356 return 0;
357 }
358 if (!write) {
359 len = sprintf(buf, "%i\n", appldata_interval);
360 if (len > *lenp)
361 len = *lenp;
362 if (copy_to_user(buffer, buf, len))
363 return -EFAULT;
364 goto out;
365 }
366 len = *lenp;
367 if (copy_from_user(buf, buffer, len > sizeof(buf) ? sizeof(buf) : len)) {
368 return -EFAULT;
369 }
370 sscanf(buf, "%i", &interval);
371 if (interval <= 0) {
372 P_ERROR("Timer CPU interval has to be > 0!\n");
373 return -EINVAL;
374 }
375
376 spin_lock(&appldata_timer_lock);
377 appldata_interval = interval;
378 __appldata_vtimer_setup(APPLDATA_MOD_TIMER);
379 spin_unlock(&appldata_timer_lock);
380
381 P_INFO("Monitoring CPU interval set to %u milliseconds.\n",
382 interval);
383out:
384 *lenp = len;
385 *ppos += len;
386 return 0;
387}
388
389/*
390 * appldata_generic_handler()
391 *
392 * Generic start/stop monitoring and DIAG, show status of
393 * monitoring (0 = not in process, 1 = in process)
394 */
395static int
396appldata_generic_handler(ctl_table *ctl, int write, struct file *filp,
397 void __user *buffer, size_t *lenp, loff_t *ppos)
398{
399 struct appldata_ops *ops = NULL, *tmp_ops;
400 int rc, len, found;
401 char buf[2];
402 struct list_head *lh;
403
404 found = 0;
Gerald Schaeferf26d5832005-06-04 15:43:33 -0700405 spin_lock(&appldata_ops_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 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) {
409 found = 1;
410 }
411 }
412 if (!found) {
Gerald Schaeferf26d5832005-06-04 15:43:33 -0700413 spin_unlock(&appldata_ops_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 return -ENODEV;
415 }
416 ops = ctl->data;
417 if (!try_module_get(ops->owner)) { // protect this function
Gerald Schaeferf26d5832005-06-04 15:43:33 -0700418 spin_unlock(&appldata_ops_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 return -ENODEV;
420 }
Gerald Schaeferf26d5832005-06-04 15:43:33 -0700421 spin_unlock(&appldata_ops_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422
423 if (!*lenp || *ppos) {
424 *lenp = 0;
425 module_put(ops->owner);
426 return 0;
427 }
428 if (!write) {
429 len = sprintf(buf, ops->active ? "1\n" : "0\n");
430 if (len > *lenp)
431 len = *lenp;
432 if (copy_to_user(buffer, buf, len)) {
433 module_put(ops->owner);
434 return -EFAULT;
435 }
436 goto out;
437 }
438 len = *lenp;
439 if (copy_from_user(buf, buffer,
440 len > sizeof(buf) ? sizeof(buf) : len)) {
441 module_put(ops->owner);
442 return -EFAULT;
443 }
444
Gerald Schaeferf26d5832005-06-04 15:43:33 -0700445 spin_lock(&appldata_ops_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 if ((buf[0] == '1') && (ops->active == 0)) {
Gerald Schaeferf26d5832005-06-04 15:43:33 -0700447 // protect work queue callback
448 if (!try_module_get(ops->owner)) {
449 spin_unlock(&appldata_ops_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 module_put(ops->owner);
451 return -ENODEV;
452 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 ops->callback(ops->data); // init record
454 rc = appldata_diag(ops->record_nr,
455 APPLDATA_START_INTERVAL_REC,
Gerald Schaefer5b5dd212006-06-29 15:08:35 +0200456 (unsigned long) ops->data, ops->size,
457 ops->mod_lvl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 if (rc != 0) {
459 P_ERROR("START DIAG 0xDC for %s failed, "
460 "return code: %d\n", ops->name, rc);
461 module_put(ops->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 } else {
463 P_INFO("Monitoring %s data enabled, "
464 "DIAG 0xDC started.\n", ops->name);
Gerald Schaefer5b5dd212006-06-29 15:08:35 +0200465 ops->active = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 }
467 } else if ((buf[0] == '0') && (ops->active == 1)) {
468 ops->active = 0;
469 rc = appldata_diag(ops->record_nr, APPLDATA_STOP_REC,
Gerald Schaefer5b5dd212006-06-29 15:08:35 +0200470 (unsigned long) ops->data, ops->size,
471 ops->mod_lvl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 if (rc != 0) {
473 P_ERROR("STOP DIAG 0xDC for %s failed, "
474 "return code: %d\n", ops->name, rc);
475 } else {
476 P_INFO("Monitoring %s data disabled, "
477 "DIAG 0xDC stopped.\n", ops->name);
478 }
479 module_put(ops->owner);
480 }
Gerald Schaeferf26d5832005-06-04 15:43:33 -0700481 spin_unlock(&appldata_ops_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482out:
483 *lenp = len;
484 *ppos += len;
485 module_put(ops->owner);
486 return 0;
487}
488
489/*************************** /proc stuff <END> *******************************/
490
491
492/************************* module-ops management *****************************/
493/*
494 * appldata_register_ops()
495 *
496 * update ops list, register /proc/sys entries
497 */
498int appldata_register_ops(struct appldata_ops *ops)
499{
500 struct list_head *lh;
501 struct appldata_ops *tmp_ops;
502 int i;
503
504 i = 0;
505
506 if ((ops->size > APPLDATA_MAX_REC_SIZE) ||
507 (ops->size < 0)){
508 P_ERROR("Invalid size of %s record = %i, maximum = %i!\n",
509 ops->name, ops->size, APPLDATA_MAX_REC_SIZE);
510 return -ENOMEM;
511 }
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);
516 return -EBUSY;
517 }
Eric Sesterhennfb630512006-03-24 03:15:31 -0800518 ops->ctl_table = kzalloc(4*sizeof(struct ctl_table), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 if (ops->ctl_table == NULL) {
520 P_ERROR("Not enough memory for %s ctl_table!\n", ops->name);
521 return -ENOMEM;
522 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523
Gerald Schaeferf26d5832005-06-04 15:43:33 -0700524 spin_lock(&appldata_ops_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 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,
531 ops->ctl_nr);
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);
Gerald Schaeferf26d5832005-06-04 15:43:33 -0700536 spin_unlock(&appldata_ops_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 return -EBUSY;
538 }
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);
Gerald Schaeferf26d5832005-06-04 15:43:33 -0700542 spin_unlock(&appldata_ops_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 return -EBUSY;
544 }
545 }
546 list_add(&ops->list, &appldata_ops_list);
Gerald Schaeferf26d5832005-06-04 15:43:33 -0700547 spin_unlock(&appldata_ops_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548
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];
554
555 ops->ctl_table[1].ctl_name = 0;
556
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;
562
563 ops->ctl_table[3].ctl_name = 0;
564
565 ops->sysctl_header = register_sysctl_table(ops->ctl_table,1);
566
567 P_INFO("%s-ops registered!\n", ops->name);
568 return 0;
569}
570
571/*
572 * appldata_unregister_ops()
573 *
574 * update ops list, unregister /proc entries, stop DIAG if necessary
575 */
576void appldata_unregister_ops(struct appldata_ops *ops)
577{
Al Viro330d57f2005-11-04 10:18:40 +0000578 void *table;
Gerald Schaeferf26d5832005-06-04 15:43:33 -0700579 spin_lock(&appldata_ops_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 list_del(&ops->list);
Al Viro330d57f2005-11-04 10:18:40 +0000581 /* at that point any incoming access will fail */
582 table = ops->ctl_table;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 ops->ctl_table = NULL;
Gerald Schaeferf26d5832005-06-04 15:43:33 -0700584 spin_unlock(&appldata_ops_lock);
Al Viro330d57f2005-11-04 10:18:40 +0000585 unregister_sysctl_table(ops->sysctl_header);
586 kfree(table);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 P_INFO("%s-ops unregistered!\n", ops->name);
588}
589/********************** module-ops management <END> **************************/
590
591
592/******************************* init / exit *********************************/
593
594static void
595appldata_online_cpu(int cpu)
596{
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)
Gerald Schaeferf26d5832005-06-04 15:43:33 -0700600 &appldata_work;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 atomic_inc(&appldata_expire_count);
602 spin_lock(&appldata_timer_lock);
603 __appldata_vtimer_setup(APPLDATA_MOD_TIMER);
604 spin_unlock(&appldata_timer_lock);
605}
606
607static void
608appldata_offline_cpu(int cpu)
609{
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());
Gerald Schaeferf26d5832005-06-04 15:43:33 -0700613 queue_work(appldata_wq, &appldata_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 }
615 spin_lock(&appldata_timer_lock);
616 __appldata_vtimer_setup(APPLDATA_MOD_TIMER);
617 spin_unlock(&appldata_timer_lock);
618}
619
Chandra Seetharamanbe6b5a32006-07-30 03:03:37 -0700620#ifdef CONFIG_HOTPLUG_CPU
Gerald Schaefer5cb900a2006-08-07 18:13:09 +0200621static int __cpuinit
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622appldata_cpu_notify(struct notifier_block *self,
623 unsigned long action, void *hcpu)
624{
625 switch (action) {
626 case CPU_ONLINE:
627 appldata_online_cpu((long) hcpu);
628 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 case CPU_DEAD:
630 appldata_offline_cpu((long) hcpu);
631 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 default:
633 break;
634 }
635 return NOTIFY_OK;
636}
637
Chandra Seetharamanbe6b5a32006-07-30 03:03:37 -0700638static struct notifier_block appldata_nb = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 .notifier_call = appldata_cpu_notify,
640};
Chandra Seetharamanbe6b5a32006-07-30 03:03:37 -0700641#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642
643/*
644 * appldata_init()
645 *
Gerald Schaeferf26d5832005-06-04 15:43:33 -0700646 * init timer, register /proc entries
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 */
648static int __init appldata_init(void)
649{
650 int i;
651
652 P_DEBUG("sizeof(parameter_list) = %lu\n",
653 sizeof(struct appldata_parameter_list));
654
Gerald Schaeferf26d5832005-06-04 15:43:33 -0700655 appldata_wq = create_singlethread_workqueue("appldata");
656 if (!appldata_wq) {
657 P_ERROR("Could not create work queue\n");
658 return -ENOMEM;
659 }
660
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 for_each_online_cpu(i)
662 appldata_online_cpu(i);
663
664 /* Register cpu hotplug notifier */
Chandra Seetharamanbe6b5a32006-07-30 03:03:37 -0700665 register_hotcpu_notifier(&appldata_nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666
667 appldata_sysctl_header = register_sysctl_table(appldata_dir_table, 1);
668#ifdef MODULE
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;
672#endif
673
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 P_DEBUG("Base interface initialized.\n");
675 return 0;
676}
677
678/*
679 * appldata_exit()
680 *
Gerald Schaeferf26d5832005-06-04 15:43:33 -0700681 * stop timer, unregister /proc entries
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 */
683static void __exit appldata_exit(void)
684{
685 struct list_head *lh;
686 struct appldata_ops *ops;
687 int rc, i;
688
689 P_DEBUG("Unloading module ...\n");
690 /*
691 * ops list should be empty, but just in case something went wrong...
692 */
Gerald Schaeferf26d5832005-06-04 15:43:33 -0700693 spin_lock(&appldata_ops_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 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,
Gerald Schaefer5b5dd212006-06-29 15:08:35 +0200697 (unsigned long) ops->data, ops->size,
698 ops->mod_lvl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 if (rc != 0) {
700 P_ERROR("STOP DIAG 0xDC for %s failed, "
701 "return code: %d\n", ops->name, rc);
702 }
703 }
Gerald Schaeferf26d5832005-06-04 15:43:33 -0700704 spin_unlock(&appldata_ops_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705
706 for_each_online_cpu(i)
707 appldata_offline_cpu(i);
708
709 appldata_timer_active = 0;
710
711 unregister_sysctl_table(appldata_sysctl_header);
712
Gerald Schaeferf26d5832005-06-04 15:43:33 -0700713 destroy_workqueue(appldata_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 P_DEBUG("... module unloaded!\n");
715}
716/**************************** init / exit <END> ******************************/
717
718
719module_init(appldata_init);
720module_exit(appldata_exit);
721MODULE_LICENSE("GPL");
722MODULE_AUTHOR("Gerald Schaefer");
723MODULE_DESCRIPTION("Linux-VM Monitor Stream, base infrastructure");
724
725EXPORT_SYMBOL_GPL(appldata_register_ops);
726EXPORT_SYMBOL_GPL(appldata_unregister_ops);
Gerald Schaefer5b5dd212006-06-29 15:08:35 +0200727EXPORT_SYMBOL_GPL(appldata_diag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728
729#ifdef MODULE
730/*
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
734 * those.
735 */
736void si_swapinfo(struct sysinfo *val)
737{
738 val->freeswap = -1ul;
739 val->totalswap = -1ul;
740}
741
742unsigned long avenrun[3] = {-1 - FIXED_1/200, -1 - FIXED_1/200,
743 -1 - FIXED_1/200};
744int nr_threads = -1;
745
746void get_full_page_state(struct page_state *ps)
747{
748 memset(ps, -1, sizeof(struct page_state));
749}
750
751unsigned long nr_running(void)
752{
753 return -1;
754}
755
756unsigned long nr_iowait(void)
757{
758 return -1;
759}
760
761/*unsigned long nr_context_switches(void)
762{
763 return -1;
764}*/
765#endif /* MODULE */
766EXPORT_SYMBOL_GPL(si_swapinfo);
767EXPORT_SYMBOL_GPL(nr_threads);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768EXPORT_SYMBOL_GPL(nr_running);
769EXPORT_SYMBOL_GPL(nr_iowait);
770//EXPORT_SYMBOL_GPL(nr_context_switches);