appldata_os.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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 IBM Corporation, IBM Deutschland Entwicklung GmbH.
  8. *
  9. * Author: Gerald Schaefer <geraldsc@de.ibm.com>
  10. */
  11. #include <linux/config.h>
  12. #include <linux/module.h>
  13. #include <linux/init.h>
  14. #include <linux/slab.h>
  15. #include <linux/errno.h>
  16. #include <linux/kernel_stat.h>
  17. #include <linux/netdevice.h>
  18. #include <linux/sched.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 2.6
  46. } __attribute__((packed));
  47. struct appldata_os_data {
  48. u64 timestamp;
  49. u32 sync_count_1; /* after VM collected the record data, */
  50. u32 sync_count_2; /* sync_count_1 and sync_count_2 should be the
  51. same. If not, the record has been updated on
  52. the Linux side while VM was collecting the
  53. (possibly corrupt) data */
  54. u32 nr_cpus; /* number of (virtual) CPUs */
  55. u32 per_cpu_size; /* size of the per-cpu data struct */
  56. u32 cpu_offset; /* offset of the first per-cpu data struct */
  57. u32 nr_running; /* number of runnable threads */
  58. u32 nr_threads; /* number of threads */
  59. u32 avenrun[3]; /* average nr. of running processes during */
  60. /* the last 1, 5 and 15 minutes */
  61. // New in 2.6 -->
  62. u32 nr_iowait; /* number of blocked threads
  63. (waiting for I/O) */
  64. // <-- New in 2.6
  65. /* per cpu data */
  66. struct appldata_os_per_cpu os_cpu[0];
  67. } __attribute__((packed));
  68. static struct appldata_os_data *appldata_os_data;
  69. static inline void appldata_print_debug(struct appldata_os_data *os_data)
  70. {
  71. int a0, a1, a2, i;
  72. P_DEBUG("--- OS - RECORD ---\n");
  73. P_DEBUG("nr_threads = %u\n", os_data->nr_threads);
  74. P_DEBUG("nr_running = %u\n", os_data->nr_running);
  75. P_DEBUG("nr_iowait = %u\n", os_data->nr_iowait);
  76. P_DEBUG("avenrun(int) = %8x / %8x / %8x\n", os_data->avenrun[0],
  77. os_data->avenrun[1], os_data->avenrun[2]);
  78. a0 = os_data->avenrun[0];
  79. a1 = os_data->avenrun[1];
  80. a2 = os_data->avenrun[2];
  81. P_DEBUG("avenrun(float) = %d.%02d / %d.%02d / %d.%02d\n",
  82. LOAD_INT(a0), LOAD_FRAC(a0), LOAD_INT(a1), LOAD_FRAC(a1),
  83. LOAD_INT(a2), LOAD_FRAC(a2));
  84. P_DEBUG("nr_cpus = %u\n", os_data->nr_cpus);
  85. for (i = 0; i < os_data->nr_cpus; i++) {
  86. P_DEBUG("cpu%u : user = %u, nice = %u, system = %u, "
  87. "idle = %u, irq = %u, softirq = %u, iowait = %u\n",
  88. i,
  89. os_data->os_cpu[i].per_cpu_user,
  90. os_data->os_cpu[i].per_cpu_nice,
  91. os_data->os_cpu[i].per_cpu_system,
  92. os_data->os_cpu[i].per_cpu_idle,
  93. os_data->os_cpu[i].per_cpu_irq,
  94. os_data->os_cpu[i].per_cpu_softirq,
  95. os_data->os_cpu[i].per_cpu_iowait);
  96. }
  97. P_DEBUG("sync_count_1 = %u\n", os_data->sync_count_1);
  98. P_DEBUG("sync_count_2 = %u\n", os_data->sync_count_2);
  99. P_DEBUG("timestamp = %lX\n", os_data->timestamp);
  100. }
  101. /*
  102. * appldata_get_os_data()
  103. *
  104. * gather OS data
  105. */
  106. static void appldata_get_os_data(void *data)
  107. {
  108. int i, j;
  109. struct appldata_os_data *os_data;
  110. os_data = data;
  111. os_data->sync_count_1++;
  112. os_data->nr_cpus = num_online_cpus();
  113. os_data->nr_threads = nr_threads;
  114. os_data->nr_running = nr_running();
  115. os_data->nr_iowait = nr_iowait();
  116. os_data->avenrun[0] = avenrun[0] + (FIXED_1/200);
  117. os_data->avenrun[1] = avenrun[1] + (FIXED_1/200);
  118. os_data->avenrun[2] = avenrun[2] + (FIXED_1/200);
  119. j = 0;
  120. for_each_online_cpu(i) {
  121. os_data->os_cpu[j].per_cpu_user =
  122. cputime_to_jiffies(kstat_cpu(i).cpustat.user);
  123. os_data->os_cpu[j].per_cpu_nice =
  124. cputime_to_jiffies(kstat_cpu(i).cpustat.nice);
  125. os_data->os_cpu[j].per_cpu_system =
  126. cputime_to_jiffies(kstat_cpu(i).cpustat.system);
  127. os_data->os_cpu[j].per_cpu_idle =
  128. cputime_to_jiffies(kstat_cpu(i).cpustat.idle);
  129. os_data->os_cpu[j].per_cpu_irq =
  130. cputime_to_jiffies(kstat_cpu(i).cpustat.irq);
  131. os_data->os_cpu[j].per_cpu_softirq =
  132. cputime_to_jiffies(kstat_cpu(i).cpustat.softirq);
  133. os_data->os_cpu[j].per_cpu_iowait =
  134. cputime_to_jiffies(kstat_cpu(i).cpustat.iowait);
  135. j++;
  136. }
  137. os_data->timestamp = get_clock();
  138. os_data->sync_count_2++;
  139. #ifdef APPLDATA_DEBUG
  140. appldata_print_debug(os_data);
  141. #endif
  142. }
  143. static struct appldata_ops ops = {
  144. .ctl_nr = CTL_APPLDATA_OS,
  145. .name = "os",
  146. .record_nr = APPLDATA_RECORD_OS_ID,
  147. .callback = &appldata_get_os_data,
  148. .owner = THIS_MODULE,
  149. };
  150. /*
  151. * appldata_os_init()
  152. *
  153. * init data, register ops
  154. */
  155. static int __init appldata_os_init(void)
  156. {
  157. int rc, size;
  158. size = sizeof(struct appldata_os_data) +
  159. (NR_CPUS * sizeof(struct appldata_os_per_cpu));
  160. if (size > APPLDATA_MAX_REC_SIZE) {
  161. P_ERROR("Size of record = %i, bigger than maximum (%i)!\n",
  162. size, APPLDATA_MAX_REC_SIZE);
  163. rc = -ENOMEM;
  164. goto out;
  165. }
  166. P_DEBUG("sizeof(os) = %i, sizeof(os_cpu) = %lu\n", size,
  167. sizeof(struct appldata_os_per_cpu));
  168. appldata_os_data = kmalloc(size, GFP_DMA);
  169. if (appldata_os_data == NULL) {
  170. P_ERROR("No memory for %s!\n", ops.name);
  171. rc = -ENOMEM;
  172. goto out;
  173. }
  174. memset(appldata_os_data, 0, size);
  175. appldata_os_data->per_cpu_size = sizeof(struct appldata_os_per_cpu);
  176. appldata_os_data->cpu_offset = offsetof(struct appldata_os_data,
  177. os_cpu);
  178. P_DEBUG("cpu offset = %u\n", appldata_os_data->cpu_offset);
  179. ops.data = appldata_os_data;
  180. ops.size = size;
  181. rc = appldata_register_ops(&ops);
  182. if (rc != 0) {
  183. P_ERROR("Error registering ops, rc = %i\n", rc);
  184. kfree(appldata_os_data);
  185. } else {
  186. P_DEBUG("%s-ops registered!\n", ops.name);
  187. }
  188. out:
  189. return rc;
  190. }
  191. /*
  192. * appldata_os_exit()
  193. *
  194. * unregister ops
  195. */
  196. static void __exit appldata_os_exit(void)
  197. {
  198. appldata_unregister_ops(&ops);
  199. kfree(appldata_os_data);
  200. P_DEBUG("%s-ops unregistered!\n", ops.name);
  201. }
  202. module_init(appldata_os_init);
  203. module_exit(appldata_os_exit);
  204. MODULE_LICENSE("GPL");
  205. MODULE_AUTHOR("Gerald Schaefer");
  206. MODULE_DESCRIPTION("Linux-VM Monitor Stream, OS statistics");