Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 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 | * |
| 8 | * Copyright (C) 2003 IBM Corporation, IBM Deutschland Entwicklung GmbH. |
| 9 | * |
| 10 | * Author: Gerald Schaefer <geraldsc@de.ibm.com> |
| 11 | */ |
| 12 | |
| 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> |
| 19 | #include <asm/io.h> |
| 20 | #include <asm/smp.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> |
Gerald Schaefer | f26d583 | 2005-06-04 15:43:33 -0700 | [diff] [blame] | 31 | #include <linux/workqueue.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | |
| 33 | #include "appldata.h" |
| 34 | |
| 35 | |
| 36 | #define MY_PRINT_NAME "appldata" /* for debug messages, etc. */ |
| 37 | #define APPLDATA_CPU_INTERVAL 10000 /* default (CPU) time for |
| 38 | sampling interval in |
| 39 | milliseconds */ |
| 40 | |
| 41 | #define TOD_MICRO 0x01000 /* nr. of TOD clock units |
| 42 | for 1 microsecond */ |
| 43 | #ifndef CONFIG_ARCH_S390X |
| 44 | |
| 45 | #define APPLDATA_START_INTERVAL_REC 0x00 /* Function codes for */ |
| 46 | #define APPLDATA_STOP_REC 0x01 /* DIAG 0xDC */ |
| 47 | #define APPLDATA_GEN_EVENT_RECORD 0x02 |
| 48 | #define APPLDATA_START_CONFIG_REC 0x03 |
| 49 | |
| 50 | #else |
| 51 | |
| 52 | #define APPLDATA_START_INTERVAL_REC 0x80 |
| 53 | #define APPLDATA_STOP_REC 0x81 |
| 54 | #define APPLDATA_GEN_EVENT_RECORD 0x82 |
| 55 | #define APPLDATA_START_CONFIG_REC 0x83 |
| 56 | |
| 57 | #endif /* CONFIG_ARCH_S390X */ |
| 58 | |
| 59 | |
| 60 | /* |
| 61 | * Parameter list for DIAGNOSE X'DC' |
| 62 | */ |
| 63 | #ifndef CONFIG_ARCH_S390X |
| 64 | struct appldata_parameter_list { |
| 65 | u16 diag; /* The DIAGNOSE code X'00DC' */ |
| 66 | u8 function; /* The function code for the DIAGNOSE */ |
| 67 | u8 parlist_length; /* Length of the parameter list */ |
| 68 | u32 product_id_addr; /* Address of the 16-byte product ID */ |
| 69 | u16 reserved; |
| 70 | u16 buffer_length; /* Length of the application data buffer */ |
| 71 | u32 buffer_addr; /* Address of the application data buffer */ |
| 72 | }; |
| 73 | #else |
| 74 | struct appldata_parameter_list { |
| 75 | u16 diag; |
| 76 | u8 function; |
| 77 | u8 parlist_length; |
| 78 | u32 unused01; |
| 79 | u16 reserved; |
| 80 | u16 buffer_length; |
| 81 | u32 unused02; |
| 82 | u64 product_id_addr; |
| 83 | u64 buffer_addr; |
| 84 | }; |
| 85 | #endif /* CONFIG_ARCH_S390X */ |
| 86 | |
| 87 | /* |
| 88 | * /proc entries (sysctl) |
| 89 | */ |
| 90 | static const char appldata_proc_name[APPLDATA_PROC_NAME_LENGTH] = "appldata"; |
| 91 | static int appldata_timer_handler(ctl_table *ctl, int write, struct file *filp, |
| 92 | void __user *buffer, size_t *lenp, loff_t *ppos); |
| 93 | static int appldata_interval_handler(ctl_table *ctl, int write, |
| 94 | struct file *filp, |
| 95 | void __user *buffer, |
| 96 | size_t *lenp, loff_t *ppos); |
| 97 | |
| 98 | static struct ctl_table_header *appldata_sysctl_header; |
| 99 | static struct ctl_table appldata_table[] = { |
| 100 | { |
| 101 | .ctl_name = CTL_APPLDATA_TIMER, |
| 102 | .procname = "timer", |
| 103 | .mode = S_IRUGO | S_IWUSR, |
| 104 | .proc_handler = &appldata_timer_handler, |
| 105 | }, |
| 106 | { |
| 107 | .ctl_name = CTL_APPLDATA_INTERVAL, |
| 108 | .procname = "interval", |
| 109 | .mode = S_IRUGO | S_IWUSR, |
| 110 | .proc_handler = &appldata_interval_handler, |
| 111 | }, |
| 112 | { .ctl_name = 0 } |
| 113 | }; |
| 114 | |
| 115 | static struct ctl_table appldata_dir_table[] = { |
| 116 | { |
| 117 | .ctl_name = CTL_APPLDATA, |
| 118 | .procname = appldata_proc_name, |
| 119 | .maxlen = 0, |
| 120 | .mode = S_IRUGO | S_IXUGO, |
| 121 | .child = appldata_table, |
| 122 | }, |
| 123 | { .ctl_name = 0 } |
| 124 | }; |
| 125 | |
| 126 | /* |
| 127 | * Timer |
| 128 | */ |
| 129 | DEFINE_PER_CPU(struct vtimer_list, appldata_timer); |
| 130 | static atomic_t appldata_expire_count = ATOMIC_INIT(0); |
| 131 | |
| 132 | static DEFINE_SPINLOCK(appldata_timer_lock); |
| 133 | static int appldata_interval = APPLDATA_CPU_INTERVAL; |
| 134 | static int appldata_timer_active; |
| 135 | |
| 136 | /* |
Gerald Schaefer | f26d583 | 2005-06-04 15:43:33 -0700 | [diff] [blame] | 137 | * Work queue |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | */ |
Gerald Schaefer | f26d583 | 2005-06-04 15:43:33 -0700 | [diff] [blame] | 139 | static struct workqueue_struct *appldata_wq; |
| 140 | static void appldata_work_fn(void *data); |
| 141 | static DECLARE_WORK(appldata_work, appldata_work_fn, NULL); |
| 142 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | |
| 144 | /* |
| 145 | * Ops list |
| 146 | */ |
| 147 | static DEFINE_SPINLOCK(appldata_ops_lock); |
| 148 | static LIST_HEAD(appldata_ops_list); |
| 149 | |
| 150 | |
Gerald Schaefer | f26d583 | 2005-06-04 15:43:33 -0700 | [diff] [blame] | 151 | /*************************** timer, work, DIAG *******************************/ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | /* |
| 153 | * appldata_timer_function() |
| 154 | * |
Gerald Schaefer | f26d583 | 2005-06-04 15:43:33 -0700 | [diff] [blame] | 155 | * schedule work and reschedule timer |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | */ |
| 157 | static void appldata_timer_function(unsigned long data, struct pt_regs *regs) |
| 158 | { |
| 159 | P_DEBUG(" -= Timer =-\n"); |
| 160 | P_DEBUG("CPU: %i, expire_count: %i\n", smp_processor_id(), |
| 161 | atomic_read(&appldata_expire_count)); |
| 162 | if (atomic_dec_and_test(&appldata_expire_count)) { |
| 163 | atomic_set(&appldata_expire_count, num_online_cpus()); |
Gerald Schaefer | f26d583 | 2005-06-04 15:43:33 -0700 | [diff] [blame] | 164 | queue_work(appldata_wq, (struct work_struct *) data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 | } |
| 166 | } |
| 167 | |
| 168 | /* |
Gerald Schaefer | f26d583 | 2005-06-04 15:43:33 -0700 | [diff] [blame] | 169 | * appldata_work_fn() |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | * |
| 171 | * call data gathering function for each (active) module |
| 172 | */ |
Gerald Schaefer | f26d583 | 2005-06-04 15:43:33 -0700 | [diff] [blame] | 173 | static void appldata_work_fn(void *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 174 | { |
| 175 | struct list_head *lh; |
| 176 | struct appldata_ops *ops; |
| 177 | int i; |
| 178 | |
Gerald Schaefer | f26d583 | 2005-06-04 15:43:33 -0700 | [diff] [blame] | 179 | P_DEBUG(" -= Work Queue =-\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | i = 0; |
| 181 | spin_lock(&appldata_ops_lock); |
| 182 | list_for_each(lh, &appldata_ops_list) { |
| 183 | ops = list_entry(lh, struct appldata_ops, list); |
| 184 | P_DEBUG("list_for_each loop: %i) active = %u, name = %s\n", |
| 185 | ++i, ops->active, ops->name); |
| 186 | if (ops->active == 1) { |
| 187 | ops->callback(ops->data); |
| 188 | } |
| 189 | } |
| 190 | spin_unlock(&appldata_ops_lock); |
| 191 | } |
| 192 | |
| 193 | /* |
| 194 | * appldata_diag() |
| 195 | * |
| 196 | * prepare parameter list, issue DIAG 0xDC |
| 197 | */ |
| 198 | static int appldata_diag(char record_nr, u16 function, unsigned long buffer, |
| 199 | u16 length) |
| 200 | { |
| 201 | unsigned long ry; |
| 202 | struct appldata_product_id { |
| 203 | char prod_nr[7]; /* product nr. */ |
| 204 | char prod_fn[2]; /* product function */ |
| 205 | char record_nr; /* record nr. */ |
| 206 | char version_nr[2]; /* version */ |
| 207 | char release_nr[2]; /* release */ |
| 208 | char mod_lvl[2]; /* modification lvl. */ |
| 209 | } appldata_product_id = { |
| 210 | /* all strings are EBCDIC, record_nr is byte */ |
| 211 | .prod_nr = {0xD3, 0xC9, 0xD5, 0xE4, |
| 212 | 0xE7, 0xD2, 0xD9}, /* "LINUXKR" */ |
| 213 | .prod_fn = {0xD5, 0xD3}, /* "NL" */ |
| 214 | .record_nr = record_nr, |
| 215 | .version_nr = {0xF2, 0xF6}, /* "26" */ |
| 216 | .release_nr = {0xF0, 0xF1}, /* "01" */ |
| 217 | .mod_lvl = {0xF0, 0xF0}, /* "00" */ |
| 218 | }; |
| 219 | struct appldata_parameter_list appldata_parameter_list = { |
| 220 | .diag = 0xDC, |
| 221 | .function = function, |
| 222 | .parlist_length = |
| 223 | sizeof(appldata_parameter_list), |
| 224 | .buffer_length = length, |
| 225 | .product_id_addr = |
| 226 | (unsigned long) &appldata_product_id, |
| 227 | .buffer_addr = virt_to_phys((void *) buffer) |
| 228 | }; |
| 229 | |
| 230 | if (!MACHINE_IS_VM) |
| 231 | return -ENOSYS; |
| 232 | ry = -1; |
| 233 | asm volatile( |
| 234 | "diag %1,%0,0xDC\n\t" |
Gerald Schaefer | b87a1e5 | 2005-07-29 14:03:38 -0700 | [diff] [blame] | 235 | : "=d" (ry) |
| 236 | : "d" (&appldata_parameter_list), |
| 237 | "m" (appldata_parameter_list), |
| 238 | "m" (appldata_product_id) |
| 239 | : "cc"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 240 | return (int) ry; |
| 241 | } |
Gerald Schaefer | f26d583 | 2005-06-04 15:43:33 -0700 | [diff] [blame] | 242 | /************************ timer, work, DIAG <END> ****************************/ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 243 | |
| 244 | |
| 245 | /****************************** /proc stuff **********************************/ |
| 246 | |
| 247 | /* |
| 248 | * appldata_mod_vtimer_wrap() |
| 249 | * |
| 250 | * wrapper function for mod_virt_timer(), because smp_call_function_on() |
| 251 | * accepts only one parameter. |
| 252 | */ |
| 253 | static void __appldata_mod_vtimer_wrap(void *p) { |
| 254 | struct { |
| 255 | struct vtimer_list *timer; |
| 256 | u64 expires; |
| 257 | } *args = p; |
| 258 | mod_virt_timer(args->timer, args->expires); |
| 259 | } |
| 260 | |
| 261 | #define APPLDATA_ADD_TIMER 0 |
| 262 | #define APPLDATA_DEL_TIMER 1 |
| 263 | #define APPLDATA_MOD_TIMER 2 |
| 264 | |
| 265 | /* |
| 266 | * __appldata_vtimer_setup() |
| 267 | * |
| 268 | * Add, delete or modify virtual timers on all online cpus. |
| 269 | * The caller needs to get the appldata_timer_lock spinlock. |
| 270 | */ |
| 271 | static void |
| 272 | __appldata_vtimer_setup(int cmd) |
| 273 | { |
| 274 | u64 per_cpu_interval; |
| 275 | int i; |
| 276 | |
| 277 | switch (cmd) { |
| 278 | case APPLDATA_ADD_TIMER: |
| 279 | if (appldata_timer_active) |
| 280 | break; |
| 281 | per_cpu_interval = (u64) (appldata_interval*1000 / |
| 282 | num_online_cpus()) * TOD_MICRO; |
| 283 | for_each_online_cpu(i) { |
| 284 | per_cpu(appldata_timer, i).expires = per_cpu_interval; |
| 285 | smp_call_function_on(add_virt_timer_periodic, |
| 286 | &per_cpu(appldata_timer, i), |
| 287 | 0, 1, i); |
| 288 | } |
| 289 | appldata_timer_active = 1; |
| 290 | P_INFO("Monitoring timer started.\n"); |
| 291 | break; |
| 292 | case APPLDATA_DEL_TIMER: |
| 293 | for_each_online_cpu(i) |
| 294 | del_virt_timer(&per_cpu(appldata_timer, i)); |
| 295 | if (!appldata_timer_active) |
| 296 | break; |
| 297 | appldata_timer_active = 0; |
| 298 | atomic_set(&appldata_expire_count, num_online_cpus()); |
| 299 | P_INFO("Monitoring timer stopped.\n"); |
| 300 | break; |
| 301 | case APPLDATA_MOD_TIMER: |
| 302 | per_cpu_interval = (u64) (appldata_interval*1000 / |
| 303 | num_online_cpus()) * TOD_MICRO; |
| 304 | if (!appldata_timer_active) |
| 305 | break; |
| 306 | for_each_online_cpu(i) { |
| 307 | struct { |
| 308 | struct vtimer_list *timer; |
| 309 | u64 expires; |
| 310 | } args; |
| 311 | args.timer = &per_cpu(appldata_timer, i); |
| 312 | args.expires = per_cpu_interval; |
| 313 | smp_call_function_on(__appldata_mod_vtimer_wrap, |
| 314 | &args, 0, 1, i); |
| 315 | } |
| 316 | } |
| 317 | } |
| 318 | |
| 319 | /* |
| 320 | * appldata_timer_handler() |
| 321 | * |
| 322 | * Start/Stop timer, show status of timer (0 = not active, 1 = active) |
| 323 | */ |
| 324 | static int |
| 325 | appldata_timer_handler(ctl_table *ctl, int write, struct file *filp, |
| 326 | void __user *buffer, size_t *lenp, loff_t *ppos) |
| 327 | { |
| 328 | int len; |
| 329 | char buf[2]; |
| 330 | |
| 331 | if (!*lenp || *ppos) { |
| 332 | *lenp = 0; |
| 333 | return 0; |
| 334 | } |
| 335 | if (!write) { |
| 336 | len = sprintf(buf, appldata_timer_active ? "1\n" : "0\n"); |
| 337 | if (len > *lenp) |
| 338 | len = *lenp; |
| 339 | if (copy_to_user(buffer, buf, len)) |
| 340 | return -EFAULT; |
| 341 | goto out; |
| 342 | } |
| 343 | len = *lenp; |
| 344 | if (copy_from_user(buf, buffer, len > sizeof(buf) ? sizeof(buf) : len)) |
| 345 | return -EFAULT; |
| 346 | spin_lock(&appldata_timer_lock); |
| 347 | if (buf[0] == '1') |
| 348 | __appldata_vtimer_setup(APPLDATA_ADD_TIMER); |
| 349 | else if (buf[0] == '0') |
| 350 | __appldata_vtimer_setup(APPLDATA_DEL_TIMER); |
| 351 | spin_unlock(&appldata_timer_lock); |
| 352 | out: |
| 353 | *lenp = len; |
| 354 | *ppos += len; |
| 355 | return 0; |
| 356 | } |
| 357 | |
| 358 | /* |
| 359 | * appldata_interval_handler() |
| 360 | * |
| 361 | * Set (CPU) timer interval for collection of data (in milliseconds), show |
| 362 | * current timer interval. |
| 363 | */ |
| 364 | static int |
| 365 | appldata_interval_handler(ctl_table *ctl, int write, struct file *filp, |
| 366 | void __user *buffer, size_t *lenp, loff_t *ppos) |
| 367 | { |
| 368 | int len, interval; |
| 369 | char buf[16]; |
| 370 | |
| 371 | if (!*lenp || *ppos) { |
| 372 | *lenp = 0; |
| 373 | return 0; |
| 374 | } |
| 375 | if (!write) { |
| 376 | len = sprintf(buf, "%i\n", appldata_interval); |
| 377 | if (len > *lenp) |
| 378 | len = *lenp; |
| 379 | if (copy_to_user(buffer, buf, len)) |
| 380 | return -EFAULT; |
| 381 | goto out; |
| 382 | } |
| 383 | len = *lenp; |
| 384 | if (copy_from_user(buf, buffer, len > sizeof(buf) ? sizeof(buf) : len)) { |
| 385 | return -EFAULT; |
| 386 | } |
| 387 | sscanf(buf, "%i", &interval); |
| 388 | if (interval <= 0) { |
| 389 | P_ERROR("Timer CPU interval has to be > 0!\n"); |
| 390 | return -EINVAL; |
| 391 | } |
| 392 | |
| 393 | spin_lock(&appldata_timer_lock); |
| 394 | appldata_interval = interval; |
| 395 | __appldata_vtimer_setup(APPLDATA_MOD_TIMER); |
| 396 | spin_unlock(&appldata_timer_lock); |
| 397 | |
| 398 | P_INFO("Monitoring CPU interval set to %u milliseconds.\n", |
| 399 | interval); |
| 400 | out: |
| 401 | *lenp = len; |
| 402 | *ppos += len; |
| 403 | return 0; |
| 404 | } |
| 405 | |
| 406 | /* |
| 407 | * appldata_generic_handler() |
| 408 | * |
| 409 | * Generic start/stop monitoring and DIAG, show status of |
| 410 | * monitoring (0 = not in process, 1 = in process) |
| 411 | */ |
| 412 | static int |
| 413 | appldata_generic_handler(ctl_table *ctl, int write, struct file *filp, |
| 414 | void __user *buffer, size_t *lenp, loff_t *ppos) |
| 415 | { |
| 416 | struct appldata_ops *ops = NULL, *tmp_ops; |
| 417 | int rc, len, found; |
| 418 | char buf[2]; |
| 419 | struct list_head *lh; |
| 420 | |
| 421 | found = 0; |
Gerald Schaefer | f26d583 | 2005-06-04 15:43:33 -0700 | [diff] [blame] | 422 | spin_lock(&appldata_ops_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 423 | list_for_each(lh, &appldata_ops_list) { |
| 424 | tmp_ops = list_entry(lh, struct appldata_ops, list); |
| 425 | if (&tmp_ops->ctl_table[2] == ctl) { |
| 426 | found = 1; |
| 427 | } |
| 428 | } |
| 429 | if (!found) { |
Gerald Schaefer | f26d583 | 2005-06-04 15:43:33 -0700 | [diff] [blame] | 430 | spin_unlock(&appldata_ops_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 431 | return -ENODEV; |
| 432 | } |
| 433 | ops = ctl->data; |
| 434 | if (!try_module_get(ops->owner)) { // protect this function |
Gerald Schaefer | f26d583 | 2005-06-04 15:43:33 -0700 | [diff] [blame] | 435 | spin_unlock(&appldata_ops_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 436 | return -ENODEV; |
| 437 | } |
Gerald Schaefer | f26d583 | 2005-06-04 15:43:33 -0700 | [diff] [blame] | 438 | spin_unlock(&appldata_ops_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 439 | |
| 440 | if (!*lenp || *ppos) { |
| 441 | *lenp = 0; |
| 442 | module_put(ops->owner); |
| 443 | return 0; |
| 444 | } |
| 445 | if (!write) { |
| 446 | len = sprintf(buf, ops->active ? "1\n" : "0\n"); |
| 447 | if (len > *lenp) |
| 448 | len = *lenp; |
| 449 | if (copy_to_user(buffer, buf, len)) { |
| 450 | module_put(ops->owner); |
| 451 | return -EFAULT; |
| 452 | } |
| 453 | goto out; |
| 454 | } |
| 455 | len = *lenp; |
| 456 | if (copy_from_user(buf, buffer, |
| 457 | len > sizeof(buf) ? sizeof(buf) : len)) { |
| 458 | module_put(ops->owner); |
| 459 | return -EFAULT; |
| 460 | } |
| 461 | |
Gerald Schaefer | f26d583 | 2005-06-04 15:43:33 -0700 | [diff] [blame] | 462 | spin_lock(&appldata_ops_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 463 | if ((buf[0] == '1') && (ops->active == 0)) { |
Gerald Schaefer | f26d583 | 2005-06-04 15:43:33 -0700 | [diff] [blame] | 464 | // protect work queue callback |
| 465 | if (!try_module_get(ops->owner)) { |
| 466 | spin_unlock(&appldata_ops_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 467 | module_put(ops->owner); |
| 468 | return -ENODEV; |
| 469 | } |
| 470 | ops->active = 1; |
| 471 | ops->callback(ops->data); // init record |
| 472 | rc = appldata_diag(ops->record_nr, |
| 473 | APPLDATA_START_INTERVAL_REC, |
| 474 | (unsigned long) ops->data, ops->size); |
| 475 | if (rc != 0) { |
| 476 | P_ERROR("START DIAG 0xDC for %s failed, " |
| 477 | "return code: %d\n", ops->name, rc); |
| 478 | module_put(ops->owner); |
| 479 | ops->active = 0; |
| 480 | } else { |
| 481 | P_INFO("Monitoring %s data enabled, " |
| 482 | "DIAG 0xDC started.\n", ops->name); |
| 483 | } |
| 484 | } else if ((buf[0] == '0') && (ops->active == 1)) { |
| 485 | ops->active = 0; |
| 486 | rc = appldata_diag(ops->record_nr, APPLDATA_STOP_REC, |
| 487 | (unsigned long) ops->data, ops->size); |
| 488 | if (rc != 0) { |
| 489 | P_ERROR("STOP DIAG 0xDC for %s failed, " |
| 490 | "return code: %d\n", ops->name, rc); |
| 491 | } else { |
| 492 | P_INFO("Monitoring %s data disabled, " |
| 493 | "DIAG 0xDC stopped.\n", ops->name); |
| 494 | } |
| 495 | module_put(ops->owner); |
| 496 | } |
Gerald Schaefer | f26d583 | 2005-06-04 15:43:33 -0700 | [diff] [blame] | 497 | spin_unlock(&appldata_ops_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 498 | out: |
| 499 | *lenp = len; |
| 500 | *ppos += len; |
| 501 | module_put(ops->owner); |
| 502 | return 0; |
| 503 | } |
| 504 | |
| 505 | /*************************** /proc stuff <END> *******************************/ |
| 506 | |
| 507 | |
| 508 | /************************* module-ops management *****************************/ |
| 509 | /* |
| 510 | * appldata_register_ops() |
| 511 | * |
| 512 | * update ops list, register /proc/sys entries |
| 513 | */ |
| 514 | int appldata_register_ops(struct appldata_ops *ops) |
| 515 | { |
| 516 | struct list_head *lh; |
| 517 | struct appldata_ops *tmp_ops; |
| 518 | int i; |
| 519 | |
| 520 | i = 0; |
| 521 | |
| 522 | if ((ops->size > APPLDATA_MAX_REC_SIZE) || |
| 523 | (ops->size < 0)){ |
| 524 | P_ERROR("Invalid size of %s record = %i, maximum = %i!\n", |
| 525 | ops->name, ops->size, APPLDATA_MAX_REC_SIZE); |
| 526 | return -ENOMEM; |
| 527 | } |
| 528 | if ((ops->ctl_nr == CTL_APPLDATA) || |
| 529 | (ops->ctl_nr == CTL_APPLDATA_TIMER) || |
| 530 | (ops->ctl_nr == CTL_APPLDATA_INTERVAL)) { |
| 531 | P_ERROR("ctl_nr %i already in use!\n", ops->ctl_nr); |
| 532 | return -EBUSY; |
| 533 | } |
| 534 | ops->ctl_table = kmalloc(4*sizeof(struct ctl_table), GFP_KERNEL); |
| 535 | if (ops->ctl_table == NULL) { |
| 536 | P_ERROR("Not enough memory for %s ctl_table!\n", ops->name); |
| 537 | return -ENOMEM; |
| 538 | } |
| 539 | memset(ops->ctl_table, 0, 4*sizeof(struct ctl_table)); |
| 540 | |
Gerald Schaefer | f26d583 | 2005-06-04 15:43:33 -0700 | [diff] [blame] | 541 | spin_lock(&appldata_ops_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 542 | list_for_each(lh, &appldata_ops_list) { |
| 543 | tmp_ops = list_entry(lh, struct appldata_ops, list); |
| 544 | P_DEBUG("register_ops loop: %i) name = %s, ctl = %i\n", |
| 545 | ++i, tmp_ops->name, tmp_ops->ctl_nr); |
| 546 | P_DEBUG("Comparing %s (ctl %i) with %s (ctl %i)\n", |
| 547 | tmp_ops->name, tmp_ops->ctl_nr, ops->name, |
| 548 | ops->ctl_nr); |
| 549 | if (strncmp(tmp_ops->name, ops->name, |
| 550 | APPLDATA_PROC_NAME_LENGTH) == 0) { |
| 551 | P_ERROR("Name \"%s\" already registered!\n", ops->name); |
| 552 | kfree(ops->ctl_table); |
Gerald Schaefer | f26d583 | 2005-06-04 15:43:33 -0700 | [diff] [blame] | 553 | spin_unlock(&appldata_ops_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 554 | return -EBUSY; |
| 555 | } |
| 556 | if (tmp_ops->ctl_nr == ops->ctl_nr) { |
| 557 | P_ERROR("ctl_nr %i already registered!\n", ops->ctl_nr); |
| 558 | kfree(ops->ctl_table); |
Gerald Schaefer | f26d583 | 2005-06-04 15:43:33 -0700 | [diff] [blame] | 559 | spin_unlock(&appldata_ops_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 560 | return -EBUSY; |
| 561 | } |
| 562 | } |
| 563 | list_add(&ops->list, &appldata_ops_list); |
Gerald Schaefer | f26d583 | 2005-06-04 15:43:33 -0700 | [diff] [blame] | 564 | spin_unlock(&appldata_ops_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 565 | |
| 566 | ops->ctl_table[0].ctl_name = CTL_APPLDATA; |
| 567 | ops->ctl_table[0].procname = appldata_proc_name; |
| 568 | ops->ctl_table[0].maxlen = 0; |
| 569 | ops->ctl_table[0].mode = S_IRUGO | S_IXUGO; |
| 570 | ops->ctl_table[0].child = &ops->ctl_table[2]; |
| 571 | |
| 572 | ops->ctl_table[1].ctl_name = 0; |
| 573 | |
| 574 | ops->ctl_table[2].ctl_name = ops->ctl_nr; |
| 575 | ops->ctl_table[2].procname = ops->name; |
| 576 | ops->ctl_table[2].mode = S_IRUGO | S_IWUSR; |
| 577 | ops->ctl_table[2].proc_handler = appldata_generic_handler; |
| 578 | ops->ctl_table[2].data = ops; |
| 579 | |
| 580 | ops->ctl_table[3].ctl_name = 0; |
| 581 | |
| 582 | ops->sysctl_header = register_sysctl_table(ops->ctl_table,1); |
| 583 | |
| 584 | P_INFO("%s-ops registered!\n", ops->name); |
| 585 | return 0; |
| 586 | } |
| 587 | |
| 588 | /* |
| 589 | * appldata_unregister_ops() |
| 590 | * |
| 591 | * update ops list, unregister /proc entries, stop DIAG if necessary |
| 592 | */ |
| 593 | void appldata_unregister_ops(struct appldata_ops *ops) |
| 594 | { |
Gerald Schaefer | f26d583 | 2005-06-04 15:43:33 -0700 | [diff] [blame] | 595 | spin_lock(&appldata_ops_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 596 | unregister_sysctl_table(ops->sysctl_header); |
| 597 | list_del(&ops->list); |
| 598 | kfree(ops->ctl_table); |
| 599 | ops->ctl_table = NULL; |
Gerald Schaefer | f26d583 | 2005-06-04 15:43:33 -0700 | [diff] [blame] | 600 | spin_unlock(&appldata_ops_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 601 | P_INFO("%s-ops unregistered!\n", ops->name); |
| 602 | } |
| 603 | /********************** module-ops management <END> **************************/ |
| 604 | |
| 605 | |
| 606 | /******************************* init / exit *********************************/ |
| 607 | |
| 608 | static void |
| 609 | appldata_online_cpu(int cpu) |
| 610 | { |
| 611 | init_virt_timer(&per_cpu(appldata_timer, cpu)); |
| 612 | per_cpu(appldata_timer, cpu).function = appldata_timer_function; |
| 613 | per_cpu(appldata_timer, cpu).data = (unsigned long) |
Gerald Schaefer | f26d583 | 2005-06-04 15:43:33 -0700 | [diff] [blame] | 614 | &appldata_work; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 615 | atomic_inc(&appldata_expire_count); |
| 616 | spin_lock(&appldata_timer_lock); |
| 617 | __appldata_vtimer_setup(APPLDATA_MOD_TIMER); |
| 618 | spin_unlock(&appldata_timer_lock); |
| 619 | } |
| 620 | |
| 621 | static void |
| 622 | appldata_offline_cpu(int cpu) |
| 623 | { |
| 624 | del_virt_timer(&per_cpu(appldata_timer, cpu)); |
| 625 | if (atomic_dec_and_test(&appldata_expire_count)) { |
| 626 | atomic_set(&appldata_expire_count, num_online_cpus()); |
Gerald Schaefer | f26d583 | 2005-06-04 15:43:33 -0700 | [diff] [blame] | 627 | queue_work(appldata_wq, &appldata_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 628 | } |
| 629 | spin_lock(&appldata_timer_lock); |
| 630 | __appldata_vtimer_setup(APPLDATA_MOD_TIMER); |
| 631 | spin_unlock(&appldata_timer_lock); |
| 632 | } |
| 633 | |
| 634 | static int |
| 635 | appldata_cpu_notify(struct notifier_block *self, |
| 636 | unsigned long action, void *hcpu) |
| 637 | { |
| 638 | switch (action) { |
| 639 | case CPU_ONLINE: |
| 640 | appldata_online_cpu((long) hcpu); |
| 641 | break; |
| 642 | #ifdef CONFIG_HOTPLUG_CPU |
| 643 | case CPU_DEAD: |
| 644 | appldata_offline_cpu((long) hcpu); |
| 645 | break; |
| 646 | #endif |
| 647 | default: |
| 648 | break; |
| 649 | } |
| 650 | return NOTIFY_OK; |
| 651 | } |
| 652 | |
| 653 | static struct notifier_block __devinitdata appldata_nb = { |
| 654 | .notifier_call = appldata_cpu_notify, |
| 655 | }; |
| 656 | |
| 657 | /* |
| 658 | * appldata_init() |
| 659 | * |
Gerald Schaefer | f26d583 | 2005-06-04 15:43:33 -0700 | [diff] [blame] | 660 | * init timer, register /proc entries |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 661 | */ |
| 662 | static int __init appldata_init(void) |
| 663 | { |
| 664 | int i; |
| 665 | |
| 666 | P_DEBUG("sizeof(parameter_list) = %lu\n", |
| 667 | sizeof(struct appldata_parameter_list)); |
| 668 | |
Gerald Schaefer | f26d583 | 2005-06-04 15:43:33 -0700 | [diff] [blame] | 669 | appldata_wq = create_singlethread_workqueue("appldata"); |
| 670 | if (!appldata_wq) { |
| 671 | P_ERROR("Could not create work queue\n"); |
| 672 | return -ENOMEM; |
| 673 | } |
| 674 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 675 | for_each_online_cpu(i) |
| 676 | appldata_online_cpu(i); |
| 677 | |
| 678 | /* Register cpu hotplug notifier */ |
| 679 | register_cpu_notifier(&appldata_nb); |
| 680 | |
| 681 | appldata_sysctl_header = register_sysctl_table(appldata_dir_table, 1); |
| 682 | #ifdef MODULE |
| 683 | appldata_dir_table[0].de->owner = THIS_MODULE; |
| 684 | appldata_table[0].de->owner = THIS_MODULE; |
| 685 | appldata_table[1].de->owner = THIS_MODULE; |
| 686 | #endif |
| 687 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 688 | P_DEBUG("Base interface initialized.\n"); |
| 689 | return 0; |
| 690 | } |
| 691 | |
| 692 | /* |
| 693 | * appldata_exit() |
| 694 | * |
Gerald Schaefer | f26d583 | 2005-06-04 15:43:33 -0700 | [diff] [blame] | 695 | * stop timer, unregister /proc entries |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 696 | */ |
| 697 | static void __exit appldata_exit(void) |
| 698 | { |
| 699 | struct list_head *lh; |
| 700 | struct appldata_ops *ops; |
| 701 | int rc, i; |
| 702 | |
| 703 | P_DEBUG("Unloading module ...\n"); |
| 704 | /* |
| 705 | * ops list should be empty, but just in case something went wrong... |
| 706 | */ |
Gerald Schaefer | f26d583 | 2005-06-04 15:43:33 -0700 | [diff] [blame] | 707 | spin_lock(&appldata_ops_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 708 | list_for_each(lh, &appldata_ops_list) { |
| 709 | ops = list_entry(lh, struct appldata_ops, list); |
| 710 | rc = appldata_diag(ops->record_nr, APPLDATA_STOP_REC, |
| 711 | (unsigned long) ops->data, ops->size); |
| 712 | if (rc != 0) { |
| 713 | P_ERROR("STOP DIAG 0xDC for %s failed, " |
| 714 | "return code: %d\n", ops->name, rc); |
| 715 | } |
| 716 | } |
Gerald Schaefer | f26d583 | 2005-06-04 15:43:33 -0700 | [diff] [blame] | 717 | spin_unlock(&appldata_ops_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 718 | |
| 719 | for_each_online_cpu(i) |
| 720 | appldata_offline_cpu(i); |
| 721 | |
| 722 | appldata_timer_active = 0; |
| 723 | |
| 724 | unregister_sysctl_table(appldata_sysctl_header); |
| 725 | |
Gerald Schaefer | f26d583 | 2005-06-04 15:43:33 -0700 | [diff] [blame] | 726 | destroy_workqueue(appldata_wq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 727 | P_DEBUG("... module unloaded!\n"); |
| 728 | } |
| 729 | /**************************** init / exit <END> ******************************/ |
| 730 | |
| 731 | |
| 732 | module_init(appldata_init); |
| 733 | module_exit(appldata_exit); |
| 734 | MODULE_LICENSE("GPL"); |
| 735 | MODULE_AUTHOR("Gerald Schaefer"); |
| 736 | MODULE_DESCRIPTION("Linux-VM Monitor Stream, base infrastructure"); |
| 737 | |
| 738 | EXPORT_SYMBOL_GPL(appldata_register_ops); |
| 739 | EXPORT_SYMBOL_GPL(appldata_unregister_ops); |
| 740 | |
| 741 | #ifdef MODULE |
| 742 | /* |
| 743 | * Kernel symbols needed by appldata_mem and appldata_os modules. |
| 744 | * However, if this file is compiled as a module (for testing only), these |
| 745 | * symbols are not exported. In this case, we define them locally and export |
| 746 | * those. |
| 747 | */ |
| 748 | void si_swapinfo(struct sysinfo *val) |
| 749 | { |
| 750 | val->freeswap = -1ul; |
| 751 | val->totalswap = -1ul; |
| 752 | } |
| 753 | |
| 754 | unsigned long avenrun[3] = {-1 - FIXED_1/200, -1 - FIXED_1/200, |
| 755 | -1 - FIXED_1/200}; |
| 756 | int nr_threads = -1; |
| 757 | |
| 758 | void get_full_page_state(struct page_state *ps) |
| 759 | { |
| 760 | memset(ps, -1, sizeof(struct page_state)); |
| 761 | } |
| 762 | |
| 763 | unsigned long nr_running(void) |
| 764 | { |
| 765 | return -1; |
| 766 | } |
| 767 | |
| 768 | unsigned long nr_iowait(void) |
| 769 | { |
| 770 | return -1; |
| 771 | } |
| 772 | |
| 773 | /*unsigned long nr_context_switches(void) |
| 774 | { |
| 775 | return -1; |
| 776 | }*/ |
| 777 | #endif /* MODULE */ |
| 778 | EXPORT_SYMBOL_GPL(si_swapinfo); |
| 779 | EXPORT_SYMBOL_GPL(nr_threads); |
| 780 | EXPORT_SYMBOL_GPL(avenrun); |
| 781 | EXPORT_SYMBOL_GPL(get_full_page_state); |
| 782 | EXPORT_SYMBOL_GPL(nr_running); |
| 783 | EXPORT_SYMBOL_GPL(nr_iowait); |
| 784 | //EXPORT_SYMBOL_GPL(nr_context_switches); |