appldata_base.c 14 KB

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