appldata_base.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572
  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(ctl_table *ctl, int write,
  44. void __user *buffer, size_t *lenp, loff_t *ppos);
  45. static int appldata_interval_handler(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(ctl_table *ctl, int write,
  178. void __user *buffer, size_t *lenp, loff_t *ppos)
  179. {
  180. 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(ctl_table *ctl, int write,
  218. void __user *buffer, size_t *lenp, loff_t *ppos)
  219. {
  220. int len, interval;
  221. char buf[16];
  222. if (!*lenp || *ppos) {
  223. *lenp = 0;
  224. return 0;
  225. }
  226. if (!write) {
  227. len = sprintf(buf, "%i\n", appldata_interval);
  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. interval = 0;
  238. sscanf(buf, "%i", &interval);
  239. if (interval <= 0)
  240. return -EINVAL;
  241. spin_lock(&appldata_timer_lock);
  242. appldata_interval = interval;
  243. __appldata_vtimer_setup(APPLDATA_MOD_TIMER);
  244. spin_unlock(&appldata_timer_lock);
  245. out:
  246. *lenp = len;
  247. *ppos += len;
  248. return 0;
  249. }
  250. /*
  251. * appldata_generic_handler()
  252. *
  253. * Generic start/stop monitoring and DIAG, show status of
  254. * monitoring (0 = not in process, 1 = in process)
  255. */
  256. static int
  257. appldata_generic_handler(ctl_table *ctl, int write,
  258. void __user *buffer, size_t *lenp, loff_t *ppos)
  259. {
  260. struct appldata_ops *ops = NULL, *tmp_ops;
  261. int rc, len, found;
  262. char buf[2];
  263. struct list_head *lh;
  264. found = 0;
  265. mutex_lock(&appldata_ops_mutex);
  266. list_for_each(lh, &appldata_ops_list) {
  267. tmp_ops = list_entry(lh, struct appldata_ops, list);
  268. if (&tmp_ops->ctl_table[2] == ctl) {
  269. found = 1;
  270. }
  271. }
  272. if (!found) {
  273. mutex_unlock(&appldata_ops_mutex);
  274. return -ENODEV;
  275. }
  276. ops = ctl->data;
  277. if (!try_module_get(ops->owner)) { // protect this function
  278. mutex_unlock(&appldata_ops_mutex);
  279. return -ENODEV;
  280. }
  281. mutex_unlock(&appldata_ops_mutex);
  282. if (!*lenp || *ppos) {
  283. *lenp = 0;
  284. module_put(ops->owner);
  285. return 0;
  286. }
  287. if (!write) {
  288. strncpy(buf, ops->active ? "1\n" : "0\n", ARRAY_SIZE(buf));
  289. len = strnlen(buf, ARRAY_SIZE(buf));
  290. if (len > *lenp)
  291. len = *lenp;
  292. if (copy_to_user(buffer, buf, len)) {
  293. module_put(ops->owner);
  294. return -EFAULT;
  295. }
  296. goto out;
  297. }
  298. len = *lenp;
  299. if (copy_from_user(buf, buffer,
  300. len > sizeof(buf) ? sizeof(buf) : len)) {
  301. module_put(ops->owner);
  302. return -EFAULT;
  303. }
  304. mutex_lock(&appldata_ops_mutex);
  305. if ((buf[0] == '1') && (ops->active == 0)) {
  306. // protect work queue callback
  307. if (!try_module_get(ops->owner)) {
  308. mutex_unlock(&appldata_ops_mutex);
  309. module_put(ops->owner);
  310. return -ENODEV;
  311. }
  312. ops->callback(ops->data); // init record
  313. rc = appldata_diag(ops->record_nr,
  314. APPLDATA_START_INTERVAL_REC,
  315. (unsigned long) ops->data, ops->size,
  316. ops->mod_lvl);
  317. if (rc != 0) {
  318. pr_err("Starting the data collection for %s "
  319. "failed with rc=%d\n", ops->name, rc);
  320. module_put(ops->owner);
  321. } else
  322. ops->active = 1;
  323. } else if ((buf[0] == '0') && (ops->active == 1)) {
  324. ops->active = 0;
  325. rc = appldata_diag(ops->record_nr, APPLDATA_STOP_REC,
  326. (unsigned long) ops->data, ops->size,
  327. ops->mod_lvl);
  328. if (rc != 0)
  329. pr_err("Stopping the data collection for %s "
  330. "failed with rc=%d\n", ops->name, rc);
  331. module_put(ops->owner);
  332. }
  333. mutex_unlock(&appldata_ops_mutex);
  334. out:
  335. *lenp = len;
  336. *ppos += len;
  337. module_put(ops->owner);
  338. return 0;
  339. }
  340. /*************************** /proc stuff <END> *******************************/
  341. /************************* module-ops management *****************************/
  342. /*
  343. * appldata_register_ops()
  344. *
  345. * update ops list, register /proc/sys entries
  346. */
  347. int appldata_register_ops(struct appldata_ops *ops)
  348. {
  349. if (ops->size > APPLDATA_MAX_REC_SIZE)
  350. return -EINVAL;
  351. ops->ctl_table = kzalloc(4 * sizeof(struct ctl_table), GFP_KERNEL);
  352. if (!ops->ctl_table)
  353. return -ENOMEM;
  354. mutex_lock(&appldata_ops_mutex);
  355. list_add(&ops->list, &appldata_ops_list);
  356. mutex_unlock(&appldata_ops_mutex);
  357. ops->ctl_table[0].procname = appldata_proc_name;
  358. ops->ctl_table[0].maxlen = 0;
  359. ops->ctl_table[0].mode = S_IRUGO | S_IXUGO;
  360. ops->ctl_table[0].child = &ops->ctl_table[2];
  361. ops->ctl_table[2].procname = ops->name;
  362. ops->ctl_table[2].mode = S_IRUGO | S_IWUSR;
  363. ops->ctl_table[2].proc_handler = appldata_generic_handler;
  364. ops->ctl_table[2].data = ops;
  365. ops->sysctl_header = register_sysctl_table(ops->ctl_table);
  366. if (!ops->sysctl_header)
  367. goto out;
  368. return 0;
  369. out:
  370. mutex_lock(&appldata_ops_mutex);
  371. list_del(&ops->list);
  372. mutex_unlock(&appldata_ops_mutex);
  373. kfree(ops->ctl_table);
  374. return -ENOMEM;
  375. }
  376. /*
  377. * appldata_unregister_ops()
  378. *
  379. * update ops list, unregister /proc entries, stop DIAG if necessary
  380. */
  381. void appldata_unregister_ops(struct appldata_ops *ops)
  382. {
  383. mutex_lock(&appldata_ops_mutex);
  384. list_del(&ops->list);
  385. mutex_unlock(&appldata_ops_mutex);
  386. unregister_sysctl_table(ops->sysctl_header);
  387. kfree(ops->ctl_table);
  388. }
  389. /********************** module-ops management <END> **************************/
  390. /**************************** suspend / resume *******************************/
  391. static int appldata_freeze(struct device *dev)
  392. {
  393. struct appldata_ops *ops;
  394. int rc;
  395. struct list_head *lh;
  396. spin_lock(&appldata_timer_lock);
  397. if (appldata_timer_active) {
  398. __appldata_vtimer_setup(APPLDATA_DEL_TIMER);
  399. appldata_timer_suspended = 1;
  400. }
  401. spin_unlock(&appldata_timer_lock);
  402. mutex_lock(&appldata_ops_mutex);
  403. list_for_each(lh, &appldata_ops_list) {
  404. ops = list_entry(lh, struct appldata_ops, list);
  405. if (ops->active == 1) {
  406. rc = appldata_diag(ops->record_nr, APPLDATA_STOP_REC,
  407. (unsigned long) ops->data, ops->size,
  408. ops->mod_lvl);
  409. if (rc != 0)
  410. pr_err("Stopping the data collection for %s "
  411. "failed with rc=%d\n", ops->name, rc);
  412. }
  413. }
  414. mutex_unlock(&appldata_ops_mutex);
  415. return 0;
  416. }
  417. static int appldata_restore(struct device *dev)
  418. {
  419. struct appldata_ops *ops;
  420. int rc;
  421. struct list_head *lh;
  422. spin_lock(&appldata_timer_lock);
  423. if (appldata_timer_suspended) {
  424. __appldata_vtimer_setup(APPLDATA_ADD_TIMER);
  425. appldata_timer_suspended = 0;
  426. }
  427. spin_unlock(&appldata_timer_lock);
  428. mutex_lock(&appldata_ops_mutex);
  429. list_for_each(lh, &appldata_ops_list) {
  430. ops = list_entry(lh, struct appldata_ops, list);
  431. if (ops->active == 1) {
  432. ops->callback(ops->data); // init record
  433. rc = appldata_diag(ops->record_nr,
  434. APPLDATA_START_INTERVAL_REC,
  435. (unsigned long) ops->data, ops->size,
  436. ops->mod_lvl);
  437. if (rc != 0) {
  438. pr_err("Starting the data collection for %s "
  439. "failed with rc=%d\n", ops->name, rc);
  440. }
  441. }
  442. }
  443. mutex_unlock(&appldata_ops_mutex);
  444. return 0;
  445. }
  446. static int appldata_thaw(struct device *dev)
  447. {
  448. return appldata_restore(dev);
  449. }
  450. static const struct dev_pm_ops appldata_pm_ops = {
  451. .freeze = appldata_freeze,
  452. .thaw = appldata_thaw,
  453. .restore = appldata_restore,
  454. };
  455. static struct platform_driver appldata_pdrv = {
  456. .driver = {
  457. .name = "appldata",
  458. .owner = THIS_MODULE,
  459. .pm = &appldata_pm_ops,
  460. },
  461. };
  462. /************************* suspend / resume <END> ****************************/
  463. /******************************* init / exit *********************************/
  464. /*
  465. * appldata_init()
  466. *
  467. * init timer, register /proc entries
  468. */
  469. static int __init appldata_init(void)
  470. {
  471. int rc;
  472. appldata_timer.function = appldata_timer_function;
  473. appldata_timer.data = (unsigned long) &appldata_work;
  474. rc = platform_driver_register(&appldata_pdrv);
  475. if (rc)
  476. return rc;
  477. appldata_pdev = platform_device_register_simple("appldata", -1, NULL,
  478. 0);
  479. if (IS_ERR(appldata_pdev)) {
  480. rc = PTR_ERR(appldata_pdev);
  481. goto out_driver;
  482. }
  483. appldata_wq = create_singlethread_workqueue("appldata");
  484. if (!appldata_wq) {
  485. rc = -ENOMEM;
  486. goto out_device;
  487. }
  488. appldata_sysctl_header = register_sysctl_table(appldata_dir_table);
  489. return 0;
  490. out_device:
  491. platform_device_unregister(appldata_pdev);
  492. out_driver:
  493. platform_driver_unregister(&appldata_pdrv);
  494. return rc;
  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);