appldata_os.c 6.0 KB

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