appldata_os.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. /*
  2. * arch/s390/appldata/appldata_os.c
  3. *
  4. * Data gathering module for Linux-VM Monitor Stream, Stage 1.
  5. * Collects misc. OS related data (CPU utilization, running processes).
  6. *
  7. * Copyright (C) 2003,2006 IBM Corporation, IBM Deutschland Entwicklung GmbH.
  8. *
  9. * Author: Gerald Schaefer <gerald.schaefer@de.ibm.com>
  10. */
  11. #include <linux/module.h>
  12. #include <linux/init.h>
  13. #include <linux/slab.h>
  14. #include <linux/errno.h>
  15. #include <linux/kernel_stat.h>
  16. #include <linux/netdevice.h>
  17. #include <linux/sched.h>
  18. #include <asm/appldata.h>
  19. #include <asm/smp.h>
  20. #include "appldata.h"
  21. #define MY_PRINT_NAME "appldata_os" /* for debug messages, etc. */
  22. #define LOAD_INT(x) ((x) >> FSHIFT)
  23. #define LOAD_FRAC(x) LOAD_INT(((x) & (FIXED_1-1)) * 100)
  24. /*
  25. * OS data
  26. *
  27. * This is accessed as binary data by z/VM. If changes to it can't be avoided,
  28. * the structure version (product ID, see appldata_base.c) needs to be changed
  29. * as well and all documentation and z/VM applications using it must be
  30. * updated.
  31. *
  32. * The record layout is documented in the Linux for zSeries Device Drivers
  33. * book:
  34. * http://oss.software.ibm.com/developerworks/opensource/linux390/index.shtml
  35. */
  36. struct appldata_os_per_cpu {
  37. u32 per_cpu_user; /* timer ticks spent in user mode */
  38. u32 per_cpu_nice; /* ... spent with modified priority */
  39. u32 per_cpu_system; /* ... spent in kernel mode */
  40. u32 per_cpu_idle; /* ... spent in idle mode */
  41. /* New in 2.6 */
  42. u32 per_cpu_irq; /* ... spent in interrupts */
  43. u32 per_cpu_softirq; /* ... spent in softirqs */
  44. u32 per_cpu_iowait; /* ... spent while waiting for I/O */
  45. /* New in modification level 01 */
  46. u32 per_cpu_steal; /* ... stolen by hypervisor */
  47. u32 cpu_id; /* number of this CPU */
  48. } __attribute__((packed));
  49. struct appldata_os_data {
  50. u64 timestamp;
  51. u32 sync_count_1; /* after VM collected the record data, */
  52. u32 sync_count_2; /* sync_count_1 and sync_count_2 should be the
  53. same. If not, the record has been updated on
  54. the Linux side while VM was collecting the
  55. (possibly corrupt) data */
  56. u32 nr_cpus; /* number of (virtual) CPUs */
  57. u32 per_cpu_size; /* size of the per-cpu data struct */
  58. u32 cpu_offset; /* offset of the first per-cpu data struct */
  59. u32 nr_running; /* number of runnable threads */
  60. u32 nr_threads; /* number of threads */
  61. u32 avenrun[3]; /* average nr. of running processes during */
  62. /* the last 1, 5 and 15 minutes */
  63. /* New in 2.6 */
  64. u32 nr_iowait; /* number of blocked threads
  65. (waiting for I/O) */
  66. /* per cpu data */
  67. struct appldata_os_per_cpu os_cpu[0];
  68. } __attribute__((packed));
  69. static struct appldata_os_data *appldata_os_data;
  70. static struct appldata_ops ops = {
  71. .name = "os",
  72. .record_nr = APPLDATA_RECORD_OS_ID,
  73. .owner = THIS_MODULE,
  74. .mod_lvl = {0xF0, 0xF1}, /* EBCDIC "01" */
  75. };
  76. static inline void appldata_print_debug(struct appldata_os_data *os_data)
  77. {
  78. int a0, a1, a2, i;
  79. P_DEBUG("--- OS - RECORD ---\n");
  80. P_DEBUG("nr_threads = %u\n", os_data->nr_threads);
  81. P_DEBUG("nr_running = %u\n", os_data->nr_running);
  82. P_DEBUG("nr_iowait = %u\n", os_data->nr_iowait);
  83. P_DEBUG("avenrun(int) = %8x / %8x / %8x\n", os_data->avenrun[0],
  84. os_data->avenrun[1], os_data->avenrun[2]);
  85. a0 = os_data->avenrun[0];
  86. a1 = os_data->avenrun[1];
  87. a2 = os_data->avenrun[2];
  88. P_DEBUG("avenrun(float) = %d.%02d / %d.%02d / %d.%02d\n",
  89. LOAD_INT(a0), LOAD_FRAC(a0), LOAD_INT(a1), LOAD_FRAC(a1),
  90. LOAD_INT(a2), LOAD_FRAC(a2));
  91. P_DEBUG("nr_cpus = %u\n", os_data->nr_cpus);
  92. for (i = 0; i < os_data->nr_cpus; i++) {
  93. P_DEBUG("cpu%u : user = %u, nice = %u, system = %u, "
  94. "idle = %u, irq = %u, softirq = %u, iowait = %u, "
  95. "steal = %u\n",
  96. os_data->os_cpu[i].cpu_id,
  97. os_data->os_cpu[i].per_cpu_user,
  98. os_data->os_cpu[i].per_cpu_nice,
  99. os_data->os_cpu[i].per_cpu_system,
  100. os_data->os_cpu[i].per_cpu_idle,
  101. os_data->os_cpu[i].per_cpu_irq,
  102. os_data->os_cpu[i].per_cpu_softirq,
  103. os_data->os_cpu[i].per_cpu_iowait,
  104. os_data->os_cpu[i].per_cpu_steal);
  105. }
  106. P_DEBUG("sync_count_1 = %u\n", os_data->sync_count_1);
  107. P_DEBUG("sync_count_2 = %u\n", os_data->sync_count_2);
  108. P_DEBUG("timestamp = %lX\n", os_data->timestamp);
  109. }
  110. /*
  111. * appldata_get_os_data()
  112. *
  113. * gather OS data
  114. */
  115. static void appldata_get_os_data(void *data)
  116. {
  117. int i, j, rc;
  118. struct appldata_os_data *os_data;
  119. unsigned int new_size;
  120. os_data = data;
  121. os_data->sync_count_1++;
  122. os_data->nr_threads = nr_threads;
  123. os_data->nr_running = nr_running();
  124. os_data->nr_iowait = nr_iowait();
  125. os_data->avenrun[0] = avenrun[0] + (FIXED_1/200);
  126. os_data->avenrun[1] = avenrun[1] + (FIXED_1/200);
  127. os_data->avenrun[2] = avenrun[2] + (FIXED_1/200);
  128. j = 0;
  129. for_each_online_cpu(i) {
  130. os_data->os_cpu[j].per_cpu_user =
  131. cputime_to_jiffies(kstat_cpu(i).cpustat.user);
  132. os_data->os_cpu[j].per_cpu_nice =
  133. cputime_to_jiffies(kstat_cpu(i).cpustat.nice);
  134. os_data->os_cpu[j].per_cpu_system =
  135. cputime_to_jiffies(kstat_cpu(i).cpustat.system);
  136. os_data->os_cpu[j].per_cpu_idle =
  137. cputime_to_jiffies(kstat_cpu(i).cpustat.idle);
  138. os_data->os_cpu[j].per_cpu_irq =
  139. cputime_to_jiffies(kstat_cpu(i).cpustat.irq);
  140. os_data->os_cpu[j].per_cpu_softirq =
  141. cputime_to_jiffies(kstat_cpu(i).cpustat.softirq);
  142. os_data->os_cpu[j].per_cpu_iowait =
  143. cputime_to_jiffies(kstat_cpu(i).cpustat.iowait);
  144. os_data->os_cpu[j].per_cpu_steal =
  145. cputime_to_jiffies(kstat_cpu(i).cpustat.steal);
  146. os_data->os_cpu[j].cpu_id = i;
  147. j++;
  148. }
  149. os_data->nr_cpus = j;
  150. new_size = sizeof(struct appldata_os_data) +
  151. (os_data->nr_cpus * sizeof(struct appldata_os_per_cpu));
  152. if (ops.size != new_size) {
  153. if (ops.active) {
  154. rc = appldata_diag(APPLDATA_RECORD_OS_ID,
  155. APPLDATA_START_INTERVAL_REC,
  156. (unsigned long) ops.data, new_size,
  157. ops.mod_lvl);
  158. if (rc != 0) {
  159. P_ERROR("os: START NEW DIAG 0xDC failed, "
  160. "return code: %d, new size = %i\n", rc,
  161. new_size);
  162. P_INFO("os: stopping old record now\n");
  163. } else
  164. P_INFO("os: new record size = %i\n", new_size);
  165. rc = appldata_diag(APPLDATA_RECORD_OS_ID,
  166. APPLDATA_STOP_REC,
  167. (unsigned long) ops.data, ops.size,
  168. ops.mod_lvl);
  169. if (rc != 0)
  170. P_ERROR("os: STOP OLD DIAG 0xDC failed, "
  171. "return code: %d, old size = %i\n", rc,
  172. ops.size);
  173. else
  174. P_INFO("os: old record size = %i stopped\n",
  175. ops.size);
  176. }
  177. ops.size = new_size;
  178. }
  179. os_data->timestamp = get_clock();
  180. os_data->sync_count_2++;
  181. #ifdef APPLDATA_DEBUG
  182. appldata_print_debug(os_data);
  183. #endif
  184. }
  185. /*
  186. * appldata_os_init()
  187. *
  188. * init data, register ops
  189. */
  190. static int __init appldata_os_init(void)
  191. {
  192. int rc, max_size;
  193. max_size = sizeof(struct appldata_os_data) +
  194. (NR_CPUS * sizeof(struct appldata_os_per_cpu));
  195. if (max_size > APPLDATA_MAX_REC_SIZE) {
  196. P_ERROR("Max. size of OS record = %i, bigger than maximum "
  197. "record size (%i)\n", max_size, APPLDATA_MAX_REC_SIZE);
  198. rc = -ENOMEM;
  199. goto out;
  200. }
  201. P_DEBUG("max. sizeof(os) = %i, sizeof(os_cpu) = %lu\n", max_size,
  202. sizeof(struct appldata_os_per_cpu));
  203. appldata_os_data = kzalloc(max_size, GFP_DMA);
  204. if (appldata_os_data == NULL) {
  205. P_ERROR("No memory for %s!\n", ops.name);
  206. rc = -ENOMEM;
  207. goto out;
  208. }
  209. appldata_os_data->per_cpu_size = sizeof(struct appldata_os_per_cpu);
  210. appldata_os_data->cpu_offset = offsetof(struct appldata_os_data,
  211. os_cpu);
  212. P_DEBUG("cpu offset = %u\n", appldata_os_data->cpu_offset);
  213. ops.data = appldata_os_data;
  214. ops.callback = &appldata_get_os_data;
  215. rc = appldata_register_ops(&ops);
  216. if (rc != 0) {
  217. P_ERROR("Error registering ops, rc = %i\n", rc);
  218. kfree(appldata_os_data);
  219. } else {
  220. P_DEBUG("%s-ops registered!\n", ops.name);
  221. }
  222. out:
  223. return rc;
  224. }
  225. /*
  226. * appldata_os_exit()
  227. *
  228. * unregister ops
  229. */
  230. static void __exit appldata_os_exit(void)
  231. {
  232. appldata_unregister_ops(&ops);
  233. kfree(appldata_os_data);
  234. P_DEBUG("%s-ops unregistered!\n", ops.name);
  235. }
  236. module_init(appldata_os_init);
  237. module_exit(appldata_os_exit);
  238. MODULE_LICENSE("GPL");
  239. MODULE_AUTHOR("Gerald Schaefer");
  240. MODULE_DESCRIPTION("Linux-VM Monitor Stream, OS statistics");