appldata_os.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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. .ctl_nr = CTL_APPLDATA_OS,
  72. .name = "os",
  73. .record_nr = APPLDATA_RECORD_OS_ID,
  74. .owner = THIS_MODULE,
  75. .mod_lvl = {0xF0, 0xF1}, /* EBCDIC "01" */
  76. };
  77. static inline void appldata_print_debug(struct appldata_os_data *os_data)
  78. {
  79. int a0, a1, a2, i;
  80. P_DEBUG("--- OS - RECORD ---\n");
  81. P_DEBUG("nr_threads = %u\n", os_data->nr_threads);
  82. P_DEBUG("nr_running = %u\n", os_data->nr_running);
  83. P_DEBUG("nr_iowait = %u\n", os_data->nr_iowait);
  84. P_DEBUG("avenrun(int) = %8x / %8x / %8x\n", os_data->avenrun[0],
  85. os_data->avenrun[1], os_data->avenrun[2]);
  86. a0 = os_data->avenrun[0];
  87. a1 = os_data->avenrun[1];
  88. a2 = os_data->avenrun[2];
  89. P_DEBUG("avenrun(float) = %d.%02d / %d.%02d / %d.%02d\n",
  90. LOAD_INT(a0), LOAD_FRAC(a0), LOAD_INT(a1), LOAD_FRAC(a1),
  91. LOAD_INT(a2), LOAD_FRAC(a2));
  92. P_DEBUG("nr_cpus = %u\n", os_data->nr_cpus);
  93. for (i = 0; i < os_data->nr_cpus; i++) {
  94. P_DEBUG("cpu%u : user = %u, nice = %u, system = %u, "
  95. "idle = %u, irq = %u, softirq = %u, iowait = %u, "
  96. "steal = %u\n",
  97. os_data->os_cpu[i].cpu_id,
  98. os_data->os_cpu[i].per_cpu_user,
  99. os_data->os_cpu[i].per_cpu_nice,
  100. os_data->os_cpu[i].per_cpu_system,
  101. os_data->os_cpu[i].per_cpu_idle,
  102. os_data->os_cpu[i].per_cpu_irq,
  103. os_data->os_cpu[i].per_cpu_softirq,
  104. os_data->os_cpu[i].per_cpu_iowait,
  105. os_data->os_cpu[i].per_cpu_steal);
  106. }
  107. P_DEBUG("sync_count_1 = %u\n", os_data->sync_count_1);
  108. P_DEBUG("sync_count_2 = %u\n", os_data->sync_count_2);
  109. P_DEBUG("timestamp = %lX\n", os_data->timestamp);
  110. }
  111. /*
  112. * appldata_get_os_data()
  113. *
  114. * gather OS data
  115. */
  116. static void appldata_get_os_data(void *data)
  117. {
  118. int i, j, rc;
  119. struct appldata_os_data *os_data;
  120. unsigned int new_size;
  121. os_data = data;
  122. os_data->sync_count_1++;
  123. os_data->nr_threads = nr_threads;
  124. os_data->nr_running = nr_running();
  125. os_data->nr_iowait = nr_iowait();
  126. os_data->avenrun[0] = avenrun[0] + (FIXED_1/200);
  127. os_data->avenrun[1] = avenrun[1] + (FIXED_1/200);
  128. os_data->avenrun[2] = avenrun[2] + (FIXED_1/200);
  129. j = 0;
  130. for_each_online_cpu(i) {
  131. os_data->os_cpu[j].per_cpu_user =
  132. cputime_to_jiffies(kstat_cpu(i).cpustat.user);
  133. os_data->os_cpu[j].per_cpu_nice =
  134. cputime_to_jiffies(kstat_cpu(i).cpustat.nice);
  135. os_data->os_cpu[j].per_cpu_system =
  136. cputime_to_jiffies(kstat_cpu(i).cpustat.system);
  137. os_data->os_cpu[j].per_cpu_idle =
  138. cputime_to_jiffies(kstat_cpu(i).cpustat.idle);
  139. os_data->os_cpu[j].per_cpu_irq =
  140. cputime_to_jiffies(kstat_cpu(i).cpustat.irq);
  141. os_data->os_cpu[j].per_cpu_softirq =
  142. cputime_to_jiffies(kstat_cpu(i).cpustat.softirq);
  143. os_data->os_cpu[j].per_cpu_iowait =
  144. cputime_to_jiffies(kstat_cpu(i).cpustat.iowait);
  145. os_data->os_cpu[j].per_cpu_steal =
  146. cputime_to_jiffies(kstat_cpu(i).cpustat.steal);
  147. os_data->os_cpu[j].cpu_id = i;
  148. j++;
  149. }
  150. os_data->nr_cpus = j;
  151. new_size = sizeof(struct appldata_os_data) +
  152. (os_data->nr_cpus * sizeof(struct appldata_os_per_cpu));
  153. if (ops.size != new_size) {
  154. if (ops.active) {
  155. rc = appldata_diag(APPLDATA_RECORD_OS_ID,
  156. APPLDATA_START_INTERVAL_REC,
  157. (unsigned long) ops.data, new_size,
  158. ops.mod_lvl);
  159. if (rc != 0) {
  160. P_ERROR("os: START NEW DIAG 0xDC failed, "
  161. "return code: %d, new size = %i\n", rc,
  162. new_size);
  163. P_INFO("os: stopping old record now\n");
  164. } else
  165. P_INFO("os: new record size = %i\n", new_size);
  166. rc = appldata_diag(APPLDATA_RECORD_OS_ID,
  167. APPLDATA_STOP_REC,
  168. (unsigned long) ops.data, ops.size,
  169. ops.mod_lvl);
  170. if (rc != 0)
  171. P_ERROR("os: STOP OLD DIAG 0xDC failed, "
  172. "return code: %d, old size = %i\n", rc,
  173. ops.size);
  174. else
  175. P_INFO("os: old record size = %i stopped\n",
  176. ops.size);
  177. }
  178. ops.size = new_size;
  179. }
  180. os_data->timestamp = get_clock();
  181. os_data->sync_count_2++;
  182. #ifdef APPLDATA_DEBUG
  183. appldata_print_debug(os_data);
  184. #endif
  185. }
  186. /*
  187. * appldata_os_init()
  188. *
  189. * init data, register ops
  190. */
  191. static int __init appldata_os_init(void)
  192. {
  193. int rc, max_size;
  194. max_size = sizeof(struct appldata_os_data) +
  195. (NR_CPUS * sizeof(struct appldata_os_per_cpu));
  196. if (max_size > APPLDATA_MAX_REC_SIZE) {
  197. P_ERROR("Max. size of OS record = %i, bigger than maximum "
  198. "record size (%i)\n", max_size, APPLDATA_MAX_REC_SIZE);
  199. rc = -ENOMEM;
  200. goto out;
  201. }
  202. P_DEBUG("max. sizeof(os) = %i, sizeof(os_cpu) = %lu\n", max_size,
  203. sizeof(struct appldata_os_per_cpu));
  204. appldata_os_data = kzalloc(max_size, GFP_DMA);
  205. if (appldata_os_data == NULL) {
  206. P_ERROR("No memory for %s!\n", ops.name);
  207. rc = -ENOMEM;
  208. goto out;
  209. }
  210. appldata_os_data->per_cpu_size = sizeof(struct appldata_os_per_cpu);
  211. appldata_os_data->cpu_offset = offsetof(struct appldata_os_data,
  212. os_cpu);
  213. P_DEBUG("cpu offset = %u\n", appldata_os_data->cpu_offset);
  214. ops.data = appldata_os_data;
  215. ops.callback = &appldata_get_os_data;
  216. rc = appldata_register_ops(&ops);
  217. if (rc != 0) {
  218. P_ERROR("Error registering ops, rc = %i\n", rc);
  219. kfree(appldata_os_data);
  220. } else {
  221. P_DEBUG("%s-ops registered!\n", ops.name);
  222. }
  223. out:
  224. return rc;
  225. }
  226. /*
  227. * appldata_os_exit()
  228. *
  229. * unregister ops
  230. */
  231. static void __exit appldata_os_exit(void)
  232. {
  233. appldata_unregister_ops(&ops);
  234. kfree(appldata_os_data);
  235. P_DEBUG("%s-ops unregistered!\n", ops.name);
  236. }
  237. module_init(appldata_os_init);
  238. module_exit(appldata_os_exit);
  239. MODULE_LICENSE("GPL");
  240. MODULE_AUTHOR("Gerald Schaefer");
  241. MODULE_DESCRIPTION("Linux-VM Monitor Stream, OS statistics");