appldata_os.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /*
  2. * Data gathering module for Linux-VM Monitor Stream, Stage 1.
  3. * Collects misc. OS related data (CPU utilization, running processes).
  4. *
  5. * Copyright IBM Corp. 2003, 2006
  6. *
  7. * Author: Gerald Schaefer <gerald.schaefer@de.ibm.com>
  8. */
  9. #define KMSG_COMPONENT "appldata"
  10. #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
  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 LOAD_INT(x) ((x) >> FSHIFT)
  22. #define LOAD_FRAC(x) LOAD_INT(((x) & (FIXED_1-1)) * 100)
  23. /*
  24. * OS data
  25. *
  26. * This is accessed as binary data by z/VM. If changes to it can't be avoided,
  27. * the structure version (product ID, see appldata_base.c) needs to be changed
  28. * as well and all documentation and z/VM applications using it must be
  29. * updated.
  30. *
  31. * The record layout is documented in the Linux for zSeries Device Drivers
  32. * book:
  33. * http://oss.software.ibm.com/developerworks/opensource/linux390/index.shtml
  34. */
  35. struct appldata_os_per_cpu {
  36. u32 per_cpu_user; /* timer ticks spent in user mode */
  37. u32 per_cpu_nice; /* ... spent with modified priority */
  38. u32 per_cpu_system; /* ... spent in kernel mode */
  39. u32 per_cpu_idle; /* ... spent in idle mode */
  40. /* New in 2.6 */
  41. u32 per_cpu_irq; /* ... spent in interrupts */
  42. u32 per_cpu_softirq; /* ... spent in softirqs */
  43. u32 per_cpu_iowait; /* ... spent while waiting for I/O */
  44. /* New in modification level 01 */
  45. u32 per_cpu_steal; /* ... stolen by hypervisor */
  46. u32 cpu_id; /* number of this CPU */
  47. } __attribute__((packed));
  48. struct appldata_os_data {
  49. u64 timestamp;
  50. u32 sync_count_1; /* after VM collected the record data, */
  51. u32 sync_count_2; /* sync_count_1 and sync_count_2 should be the
  52. same. If not, the record has been updated on
  53. the Linux side while VM was collecting the
  54. (possibly corrupt) data */
  55. u32 nr_cpus; /* number of (virtual) CPUs */
  56. u32 per_cpu_size; /* size of the per-cpu data struct */
  57. u32 cpu_offset; /* offset of the first per-cpu data struct */
  58. u32 nr_running; /* number of runnable threads */
  59. u32 nr_threads; /* number of threads */
  60. u32 avenrun[3]; /* average nr. of running processes during */
  61. /* the last 1, 5 and 15 minutes */
  62. /* New in 2.6 */
  63. u32 nr_iowait; /* number of blocked threads
  64. (waiting for I/O) */
  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 struct appldata_ops ops = {
  70. .name = "os",
  71. .record_nr = APPLDATA_RECORD_OS_ID,
  72. .owner = THIS_MODULE,
  73. .mod_lvl = {0xF0, 0xF1}, /* EBCDIC "01" */
  74. };
  75. /*
  76. * appldata_get_os_data()
  77. *
  78. * gather OS data
  79. */
  80. static void appldata_get_os_data(void *data)
  81. {
  82. int i, j, rc;
  83. struct appldata_os_data *os_data;
  84. unsigned int new_size;
  85. os_data = data;
  86. os_data->sync_count_1++;
  87. os_data->nr_threads = nr_threads;
  88. os_data->nr_running = nr_running();
  89. os_data->nr_iowait = nr_iowait();
  90. os_data->avenrun[0] = avenrun[0] + (FIXED_1/200);
  91. os_data->avenrun[1] = avenrun[1] + (FIXED_1/200);
  92. os_data->avenrun[2] = avenrun[2] + (FIXED_1/200);
  93. j = 0;
  94. for_each_online_cpu(i) {
  95. os_data->os_cpu[j].per_cpu_user =
  96. cputime_to_jiffies(kcpustat_cpu(i).cpustat[CPUTIME_USER]);
  97. os_data->os_cpu[j].per_cpu_nice =
  98. cputime_to_jiffies(kcpustat_cpu(i).cpustat[CPUTIME_NICE]);
  99. os_data->os_cpu[j].per_cpu_system =
  100. cputime_to_jiffies(kcpustat_cpu(i).cpustat[CPUTIME_SYSTEM]);
  101. os_data->os_cpu[j].per_cpu_idle =
  102. cputime_to_jiffies(kcpustat_cpu(i).cpustat[CPUTIME_IDLE]);
  103. os_data->os_cpu[j].per_cpu_irq =
  104. cputime_to_jiffies(kcpustat_cpu(i).cpustat[CPUTIME_IRQ]);
  105. os_data->os_cpu[j].per_cpu_softirq =
  106. cputime_to_jiffies(kcpustat_cpu(i).cpustat[CPUTIME_SOFTIRQ]);
  107. os_data->os_cpu[j].per_cpu_iowait =
  108. cputime_to_jiffies(kcpustat_cpu(i).cpustat[CPUTIME_IOWAIT]);
  109. os_data->os_cpu[j].per_cpu_steal =
  110. cputime_to_jiffies(kcpustat_cpu(i).cpustat[CPUTIME_STEAL]);
  111. os_data->os_cpu[j].cpu_id = i;
  112. j++;
  113. }
  114. os_data->nr_cpus = j;
  115. new_size = sizeof(struct appldata_os_data) +
  116. (os_data->nr_cpus * sizeof(struct appldata_os_per_cpu));
  117. if (ops.size != new_size) {
  118. if (ops.active) {
  119. rc = appldata_diag(APPLDATA_RECORD_OS_ID,
  120. APPLDATA_START_INTERVAL_REC,
  121. (unsigned long) ops.data, new_size,
  122. ops.mod_lvl);
  123. if (rc != 0)
  124. pr_err("Starting a new OS data collection "
  125. "failed with rc=%d\n", rc);
  126. rc = appldata_diag(APPLDATA_RECORD_OS_ID,
  127. APPLDATA_STOP_REC,
  128. (unsigned long) ops.data, ops.size,
  129. ops.mod_lvl);
  130. if (rc != 0)
  131. pr_err("Stopping a faulty OS data "
  132. "collection failed with rc=%d\n", rc);
  133. }
  134. ops.size = new_size;
  135. }
  136. os_data->timestamp = get_tod_clock();
  137. os_data->sync_count_2++;
  138. }
  139. /*
  140. * appldata_os_init()
  141. *
  142. * init data, register ops
  143. */
  144. static int __init appldata_os_init(void)
  145. {
  146. int rc, max_size;
  147. max_size = sizeof(struct appldata_os_data) +
  148. (num_possible_cpus() * sizeof(struct appldata_os_per_cpu));
  149. if (max_size > APPLDATA_MAX_REC_SIZE) {
  150. pr_err("Maximum OS record size %i exceeds the maximum "
  151. "record size %i\n", max_size, APPLDATA_MAX_REC_SIZE);
  152. rc = -ENOMEM;
  153. goto out;
  154. }
  155. appldata_os_data = kzalloc(max_size, GFP_KERNEL | GFP_DMA);
  156. if (appldata_os_data == NULL) {
  157. rc = -ENOMEM;
  158. goto out;
  159. }
  160. appldata_os_data->per_cpu_size = sizeof(struct appldata_os_per_cpu);
  161. appldata_os_data->cpu_offset = offsetof(struct appldata_os_data,
  162. os_cpu);
  163. ops.data = appldata_os_data;
  164. ops.callback = &appldata_get_os_data;
  165. rc = appldata_register_ops(&ops);
  166. if (rc != 0)
  167. kfree(appldata_os_data);
  168. out:
  169. return rc;
  170. }
  171. /*
  172. * appldata_os_exit()
  173. *
  174. * unregister ops
  175. */
  176. static void __exit appldata_os_exit(void)
  177. {
  178. appldata_unregister_ops(&ops);
  179. kfree(appldata_os_data);
  180. }
  181. module_init(appldata_os_init);
  182. module_exit(appldata_os_exit);
  183. MODULE_LICENSE("GPL");
  184. MODULE_AUTHOR("Gerald Schaefer");
  185. MODULE_DESCRIPTION("Linux-VM Monitor Stream, OS statistics");