appldata_os.c 6.1 KB

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