appldata_base.c 13 KB

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