appldata_base.c 13 KB

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