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