appldata_base.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563
  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 IBM Corp. 2003, 2008
  9. *
  10. * Author: Gerald Schaefer <gerald.schaefer@de.ibm.com>
  11. */
  12. #include <linux/module.h>
  13. #include <linux/init.h>
  14. #include <linux/slab.h>
  15. #include <linux/errno.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/proc_fs.h>
  18. #include <linux/mm.h>
  19. #include <linux/swap.h>
  20. #include <linux/pagemap.h>
  21. #include <linux/sysctl.h>
  22. #include <linux/notifier.h>
  23. #include <linux/cpu.h>
  24. #include <linux/workqueue.h>
  25. #include <asm/appldata.h>
  26. #include <asm/timer.h>
  27. #include <asm/uaccess.h>
  28. #include <asm/io.h>
  29. #include <asm/smp.h>
  30. #include "appldata.h"
  31. #define MY_PRINT_NAME "appldata" /* for debug messages, etc. */
  32. #define APPLDATA_CPU_INTERVAL 10000 /* default (CPU) time for
  33. sampling interval in
  34. milliseconds */
  35. #define TOD_MICRO 0x01000 /* nr. of TOD clock units
  36. for 1 microsecond */
  37. /*
  38. * /proc entries (sysctl)
  39. */
  40. static const char appldata_proc_name[APPLDATA_PROC_NAME_LENGTH] = "appldata";
  41. static int appldata_timer_handler(ctl_table *ctl, int write, struct file *filp,
  42. void __user *buffer, size_t *lenp, loff_t *ppos);
  43. static int appldata_interval_handler(ctl_table *ctl, int write,
  44. struct file *filp,
  45. void __user *buffer,
  46. size_t *lenp, loff_t *ppos);
  47. static struct ctl_table_header *appldata_sysctl_header;
  48. static struct ctl_table appldata_table[] = {
  49. {
  50. .procname = "timer",
  51. .mode = S_IRUGO | S_IWUSR,
  52. .proc_handler = &appldata_timer_handler,
  53. },
  54. {
  55. .procname = "interval",
  56. .mode = S_IRUGO | S_IWUSR,
  57. .proc_handler = &appldata_interval_handler,
  58. },
  59. { },
  60. };
  61. static struct ctl_table appldata_dir_table[] = {
  62. {
  63. .procname = appldata_proc_name,
  64. .maxlen = 0,
  65. .mode = S_IRUGO | S_IXUGO,
  66. .child = appldata_table,
  67. },
  68. { },
  69. };
  70. /*
  71. * Timer
  72. */
  73. static DEFINE_PER_CPU(struct vtimer_list, appldata_timer);
  74. static atomic_t appldata_expire_count = ATOMIC_INIT(0);
  75. static DEFINE_SPINLOCK(appldata_timer_lock);
  76. static int appldata_interval = APPLDATA_CPU_INTERVAL;
  77. static int appldata_timer_active;
  78. /*
  79. * Work queue
  80. */
  81. static struct workqueue_struct *appldata_wq;
  82. static void appldata_work_fn(struct work_struct *work);
  83. static DECLARE_WORK(appldata_work, appldata_work_fn);
  84. /*
  85. * Ops list
  86. */
  87. static DEFINE_SPINLOCK(appldata_ops_lock);
  88. static LIST_HEAD(appldata_ops_list);
  89. /*************************** timer, work, DIAG *******************************/
  90. /*
  91. * appldata_timer_function()
  92. *
  93. * schedule work and reschedule timer
  94. */
  95. static void appldata_timer_function(unsigned long data)
  96. {
  97. if (atomic_dec_and_test(&appldata_expire_count)) {
  98. atomic_set(&appldata_expire_count, num_online_cpus());
  99. queue_work(appldata_wq, (struct work_struct *) data);
  100. }
  101. }
  102. /*
  103. * appldata_work_fn()
  104. *
  105. * call data gathering function for each (active) module
  106. */
  107. static void appldata_work_fn(struct work_struct *work)
  108. {
  109. struct list_head *lh;
  110. struct appldata_ops *ops;
  111. int i;
  112. i = 0;
  113. get_online_cpus();
  114. spin_lock(&appldata_ops_lock);
  115. list_for_each(lh, &appldata_ops_list) {
  116. ops = list_entry(lh, struct appldata_ops, list);
  117. if (ops->active == 1) {
  118. ops->callback(ops->data);
  119. }
  120. }
  121. spin_unlock(&appldata_ops_lock);
  122. put_online_cpus();
  123. }
  124. /*
  125. * appldata_diag()
  126. *
  127. * prepare parameter list, issue DIAG 0xDC
  128. */
  129. int appldata_diag(char record_nr, u16 function, unsigned long buffer,
  130. u16 length, char *mod_lvl)
  131. {
  132. struct appldata_product_id id = {
  133. .prod_nr = {0xD3, 0xC9, 0xD5, 0xE4,
  134. 0xE7, 0xD2, 0xD9}, /* "LINUXKR" */
  135. .prod_fn = 0xD5D3, /* "NL" */
  136. .version_nr = 0xF2F6, /* "26" */
  137. .release_nr = 0xF0F1, /* "01" */
  138. };
  139. id.record_nr = record_nr;
  140. id.mod_lvl = (mod_lvl[0]) << 8 | mod_lvl[1];
  141. return appldata_asm(&id, function, (void *) buffer, length);
  142. }
  143. /************************ timer, work, DIAG <END> ****************************/
  144. /****************************** /proc stuff **********************************/
  145. /*
  146. * appldata_mod_vtimer_wrap()
  147. *
  148. * wrapper function for mod_virt_timer(), because smp_call_function_single()
  149. * accepts only one parameter.
  150. */
  151. static void __appldata_mod_vtimer_wrap(void *p) {
  152. struct {
  153. struct vtimer_list *timer;
  154. u64 expires;
  155. } *args = p;
  156. mod_virt_timer(args->timer, args->expires);
  157. }
  158. #define APPLDATA_ADD_TIMER 0
  159. #define APPLDATA_DEL_TIMER 1
  160. #define APPLDATA_MOD_TIMER 2
  161. /*
  162. * __appldata_vtimer_setup()
  163. *
  164. * Add, delete or modify virtual timers on all online cpus.
  165. * The caller needs to get the appldata_timer_lock spinlock.
  166. */
  167. static void
  168. __appldata_vtimer_setup(int cmd)
  169. {
  170. u64 per_cpu_interval;
  171. int i;
  172. switch (cmd) {
  173. case APPLDATA_ADD_TIMER:
  174. if (appldata_timer_active)
  175. break;
  176. per_cpu_interval = (u64) (appldata_interval*1000 /
  177. num_online_cpus()) * TOD_MICRO;
  178. for_each_online_cpu(i) {
  179. per_cpu(appldata_timer, i).expires = per_cpu_interval;
  180. smp_call_function_single(i, add_virt_timer_periodic,
  181. &per_cpu(appldata_timer, i),
  182. 1);
  183. }
  184. appldata_timer_active = 1;
  185. break;
  186. case APPLDATA_DEL_TIMER:
  187. for_each_online_cpu(i)
  188. del_virt_timer(&per_cpu(appldata_timer, i));
  189. if (!appldata_timer_active)
  190. break;
  191. appldata_timer_active = 0;
  192. atomic_set(&appldata_expire_count, num_online_cpus());
  193. break;
  194. case APPLDATA_MOD_TIMER:
  195. per_cpu_interval = (u64) (appldata_interval*1000 /
  196. num_online_cpus()) * TOD_MICRO;
  197. if (!appldata_timer_active)
  198. break;
  199. for_each_online_cpu(i) {
  200. struct {
  201. struct vtimer_list *timer;
  202. u64 expires;
  203. } args;
  204. args.timer = &per_cpu(appldata_timer, i);
  205. args.expires = per_cpu_interval;
  206. smp_call_function_single(i, __appldata_mod_vtimer_wrap,
  207. &args, 1);
  208. }
  209. }
  210. }
  211. /*
  212. * appldata_timer_handler()
  213. *
  214. * Start/Stop timer, show status of timer (0 = not active, 1 = active)
  215. */
  216. static int
  217. appldata_timer_handler(ctl_table *ctl, int write, struct file *filp,
  218. void __user *buffer, size_t *lenp, loff_t *ppos)
  219. {
  220. int len;
  221. char buf[2];
  222. if (!*lenp || *ppos) {
  223. *lenp = 0;
  224. return 0;
  225. }
  226. if (!write) {
  227. len = sprintf(buf, appldata_timer_active ? "1\n" : "0\n");
  228. if (len > *lenp)
  229. len = *lenp;
  230. if (copy_to_user(buffer, buf, len))
  231. return -EFAULT;
  232. goto out;
  233. }
  234. len = *lenp;
  235. if (copy_from_user(buf, buffer, len > sizeof(buf) ? sizeof(buf) : len))
  236. return -EFAULT;
  237. get_online_cpus();
  238. spin_lock(&appldata_timer_lock);
  239. if (buf[0] == '1')
  240. __appldata_vtimer_setup(APPLDATA_ADD_TIMER);
  241. else if (buf[0] == '0')
  242. __appldata_vtimer_setup(APPLDATA_DEL_TIMER);
  243. spin_unlock(&appldata_timer_lock);
  244. put_online_cpus();
  245. out:
  246. *lenp = len;
  247. *ppos += len;
  248. return 0;
  249. }
  250. /*
  251. * appldata_interval_handler()
  252. *
  253. * Set (CPU) timer interval for collection of data (in milliseconds), show
  254. * current timer interval.
  255. */
  256. static int
  257. appldata_interval_handler(ctl_table *ctl, int write, struct file *filp,
  258. void __user *buffer, size_t *lenp, loff_t *ppos)
  259. {
  260. int len, interval;
  261. char buf[16];
  262. if (!*lenp || *ppos) {
  263. *lenp = 0;
  264. return 0;
  265. }
  266. if (!write) {
  267. len = sprintf(buf, "%i\n", appldata_interval);
  268. if (len > *lenp)
  269. len = *lenp;
  270. if (copy_to_user(buffer, buf, len))
  271. return -EFAULT;
  272. goto out;
  273. }
  274. len = *lenp;
  275. if (copy_from_user(buf, buffer, len > sizeof(buf) ? sizeof(buf) : len)) {
  276. return -EFAULT;
  277. }
  278. interval = 0;
  279. sscanf(buf, "%i", &interval);
  280. if (interval <= 0)
  281. return -EINVAL;
  282. get_online_cpus();
  283. spin_lock(&appldata_timer_lock);
  284. appldata_interval = interval;
  285. __appldata_vtimer_setup(APPLDATA_MOD_TIMER);
  286. spin_unlock(&appldata_timer_lock);
  287. put_online_cpus();
  288. out:
  289. *lenp = len;
  290. *ppos += len;
  291. return 0;
  292. }
  293. /*
  294. * appldata_generic_handler()
  295. *
  296. * Generic start/stop monitoring and DIAG, show status of
  297. * monitoring (0 = not in process, 1 = in process)
  298. */
  299. static int
  300. appldata_generic_handler(ctl_table *ctl, int write, struct file *filp,
  301. void __user *buffer, size_t *lenp, loff_t *ppos)
  302. {
  303. struct appldata_ops *ops = NULL, *tmp_ops;
  304. int rc, len, found;
  305. char buf[2];
  306. struct list_head *lh;
  307. found = 0;
  308. spin_lock(&appldata_ops_lock);
  309. list_for_each(lh, &appldata_ops_list) {
  310. tmp_ops = list_entry(lh, struct appldata_ops, list);
  311. if (&tmp_ops->ctl_table[2] == ctl) {
  312. found = 1;
  313. }
  314. }
  315. if (!found) {
  316. spin_unlock(&appldata_ops_lock);
  317. return -ENODEV;
  318. }
  319. ops = ctl->data;
  320. if (!try_module_get(ops->owner)) { // protect this function
  321. spin_unlock(&appldata_ops_lock);
  322. return -ENODEV;
  323. }
  324. spin_unlock(&appldata_ops_lock);
  325. if (!*lenp || *ppos) {
  326. *lenp = 0;
  327. module_put(ops->owner);
  328. return 0;
  329. }
  330. if (!write) {
  331. len = sprintf(buf, ops->active ? "1\n" : "0\n");
  332. if (len > *lenp)
  333. len = *lenp;
  334. if (copy_to_user(buffer, buf, len)) {
  335. module_put(ops->owner);
  336. return -EFAULT;
  337. }
  338. goto out;
  339. }
  340. len = *lenp;
  341. if (copy_from_user(buf, buffer,
  342. len > sizeof(buf) ? sizeof(buf) : len)) {
  343. module_put(ops->owner);
  344. return -EFAULT;
  345. }
  346. spin_lock(&appldata_ops_lock);
  347. if ((buf[0] == '1') && (ops->active == 0)) {
  348. // protect work queue callback
  349. if (!try_module_get(ops->owner)) {
  350. spin_unlock(&appldata_ops_lock);
  351. module_put(ops->owner);
  352. return -ENODEV;
  353. }
  354. ops->callback(ops->data); // init record
  355. rc = appldata_diag(ops->record_nr,
  356. APPLDATA_START_INTERVAL_REC,
  357. (unsigned long) ops->data, ops->size,
  358. ops->mod_lvl);
  359. if (rc != 0) {
  360. P_ERROR("START DIAG 0xDC for %s failed, "
  361. "return code: %d\n", ops->name, rc);
  362. module_put(ops->owner);
  363. } else
  364. ops->active = 1;
  365. } else if ((buf[0] == '0') && (ops->active == 1)) {
  366. ops->active = 0;
  367. rc = appldata_diag(ops->record_nr, APPLDATA_STOP_REC,
  368. (unsigned long) ops->data, ops->size,
  369. ops->mod_lvl);
  370. if (rc != 0)
  371. P_ERROR("STOP DIAG 0xDC for %s failed, "
  372. "return code: %d\n", ops->name, rc);
  373. module_put(ops->owner);
  374. }
  375. spin_unlock(&appldata_ops_lock);
  376. out:
  377. *lenp = len;
  378. *ppos += len;
  379. module_put(ops->owner);
  380. return 0;
  381. }
  382. /*************************** /proc stuff <END> *******************************/
  383. /************************* module-ops management *****************************/
  384. /*
  385. * appldata_register_ops()
  386. *
  387. * update ops list, register /proc/sys entries
  388. */
  389. int appldata_register_ops(struct appldata_ops *ops)
  390. {
  391. if ((ops->size > APPLDATA_MAX_REC_SIZE) || (ops->size < 0))
  392. return -EINVAL;
  393. ops->ctl_table = kzalloc(4 * sizeof(struct ctl_table), GFP_KERNEL);
  394. if (!ops->ctl_table)
  395. return -ENOMEM;
  396. spin_lock(&appldata_ops_lock);
  397. list_add(&ops->list, &appldata_ops_list);
  398. spin_unlock(&appldata_ops_lock);
  399. ops->ctl_table[0].procname = appldata_proc_name;
  400. ops->ctl_table[0].maxlen = 0;
  401. ops->ctl_table[0].mode = S_IRUGO | S_IXUGO;
  402. ops->ctl_table[0].child = &ops->ctl_table[2];
  403. ops->ctl_table[2].procname = ops->name;
  404. ops->ctl_table[2].mode = S_IRUGO | S_IWUSR;
  405. ops->ctl_table[2].proc_handler = appldata_generic_handler;
  406. ops->ctl_table[2].data = ops;
  407. ops->sysctl_header = register_sysctl_table(ops->ctl_table);
  408. if (!ops->sysctl_header)
  409. goto out;
  410. return 0;
  411. out:
  412. spin_lock(&appldata_ops_lock);
  413. list_del(&ops->list);
  414. spin_unlock(&appldata_ops_lock);
  415. kfree(ops->ctl_table);
  416. return -ENOMEM;
  417. }
  418. /*
  419. * appldata_unregister_ops()
  420. *
  421. * update ops list, unregister /proc entries, stop DIAG if necessary
  422. */
  423. void appldata_unregister_ops(struct appldata_ops *ops)
  424. {
  425. spin_lock(&appldata_ops_lock);
  426. list_del(&ops->list);
  427. spin_unlock(&appldata_ops_lock);
  428. unregister_sysctl_table(ops->sysctl_header);
  429. kfree(ops->ctl_table);
  430. }
  431. /********************** module-ops management <END> **************************/
  432. /******************************* init / exit *********************************/
  433. static void __cpuinit appldata_online_cpu(int cpu)
  434. {
  435. init_virt_timer(&per_cpu(appldata_timer, cpu));
  436. per_cpu(appldata_timer, cpu).function = appldata_timer_function;
  437. per_cpu(appldata_timer, cpu).data = (unsigned long)
  438. &appldata_work;
  439. atomic_inc(&appldata_expire_count);
  440. spin_lock(&appldata_timer_lock);
  441. __appldata_vtimer_setup(APPLDATA_MOD_TIMER);
  442. spin_unlock(&appldata_timer_lock);
  443. }
  444. static void __cpuinit appldata_offline_cpu(int cpu)
  445. {
  446. del_virt_timer(&per_cpu(appldata_timer, cpu));
  447. if (atomic_dec_and_test(&appldata_expire_count)) {
  448. atomic_set(&appldata_expire_count, num_online_cpus());
  449. queue_work(appldata_wq, &appldata_work);
  450. }
  451. spin_lock(&appldata_timer_lock);
  452. __appldata_vtimer_setup(APPLDATA_MOD_TIMER);
  453. spin_unlock(&appldata_timer_lock);
  454. }
  455. static int __cpuinit appldata_cpu_notify(struct notifier_block *self,
  456. unsigned long action,
  457. void *hcpu)
  458. {
  459. switch (action) {
  460. case CPU_ONLINE:
  461. case CPU_ONLINE_FROZEN:
  462. appldata_online_cpu((long) hcpu);
  463. break;
  464. case CPU_DEAD:
  465. case CPU_DEAD_FROZEN:
  466. appldata_offline_cpu((long) hcpu);
  467. break;
  468. default:
  469. break;
  470. }
  471. return NOTIFY_OK;
  472. }
  473. static struct notifier_block __cpuinitdata appldata_nb = {
  474. .notifier_call = appldata_cpu_notify,
  475. };
  476. /*
  477. * appldata_init()
  478. *
  479. * init timer, register /proc entries
  480. */
  481. static int __init appldata_init(void)
  482. {
  483. int i;
  484. appldata_wq = create_singlethread_workqueue("appldata");
  485. if (!appldata_wq)
  486. return -ENOMEM;
  487. get_online_cpus();
  488. for_each_online_cpu(i)
  489. appldata_online_cpu(i);
  490. put_online_cpus();
  491. /* Register cpu hotplug notifier */
  492. register_hotcpu_notifier(&appldata_nb);
  493. appldata_sysctl_header = register_sysctl_table(appldata_dir_table);
  494. return 0;
  495. }
  496. __initcall(appldata_init);
  497. /**************************** init / exit <END> ******************************/
  498. EXPORT_SYMBOL_GPL(appldata_register_ops);
  499. EXPORT_SYMBOL_GPL(appldata_unregister_ops);
  500. EXPORT_SYMBOL_GPL(appldata_diag);
  501. #ifdef CONFIG_SWAP
  502. EXPORT_SYMBOL_GPL(si_swapinfo);
  503. #endif
  504. EXPORT_SYMBOL_GPL(nr_threads);
  505. EXPORT_SYMBOL_GPL(nr_running);
  506. EXPORT_SYMBOL_GPL(nr_iowait);