appldata_base.c 14 KB

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