appldata_base.c 14 KB

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