appldata_os.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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 <[email protected]>
  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. /*
  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. struct appldata_os_per_cpu {
  33. u32 per_cpu_user; /* timer ticks spent in user mode */
  34. u32 per_cpu_nice; /* ... spent with modified priority */
  35. u32 per_cpu_system; /* ... spent in kernel mode */
  36. u32 per_cpu_idle; /* ... spent in idle mode */
  37. /* New in 2.6 */
  38. u32 per_cpu_irq; /* ... spent in interrupts */
  39. u32 per_cpu_softirq; /* ... spent in softirqs */
  40. u32 per_cpu_iowait; /* ... spent while waiting for I/O */
  41. /* New in modification level 01 */
  42. u32 per_cpu_steal; /* ... stolen by hypervisor */
  43. u32 cpu_id; /* number of this CPU */
  44. } __attribute__((packed));
  45. struct appldata_os_data {
  46. u64 timestamp;
  47. u32 sync_count_1; /* after VM collected the record data, */
  48. u32 sync_count_2; /* sync_count_1 and sync_count_2 should be the
  49. same. If not, the record has been updated on
  50. the Linux side while VM was collecting the
  51. (possibly corrupt) data */
  52. u32 nr_cpus; /* number of (virtual) CPUs */
  53. u32 per_cpu_size; /* size of the per-cpu data struct */
  54. u32 cpu_offset; /* offset of the first per-cpu data struct */
  55. u32 nr_running; /* number of runnable threads */
  56. u32 nr_threads; /* number of threads */
  57. u32 avenrun[3]; /* average nr. of running processes during */
  58. /* the last 1, 5 and 15 minutes */
  59. /* New in 2.6 */
  60. u32 nr_iowait; /* number of blocked threads
  61. (waiting for I/O) */
  62. /* per cpu data */
  63. struct appldata_os_per_cpu os_cpu[];
  64. } __attribute__((packed));
  65. static struct appldata_os_data *appldata_os_data;
  66. static struct appldata_ops ops = {
  67. .name = "os",
  68. .record_nr = APPLDATA_RECORD_OS_ID,
  69. .owner = THIS_MODULE,
  70. .mod_lvl = {0xF0, 0xF1}, /* EBCDIC "01" */
  71. };
  72. /*
  73. * appldata_get_os_data()
  74. *
  75. * gather OS data
  76. */
  77. static void appldata_get_os_data(void *data)
  78. {
  79. int i, j, rc;
  80. struct appldata_os_data *os_data;
  81. unsigned int new_size;
  82. os_data = data;
  83. os_data->sync_count_1++;
  84. os_data->nr_threads = nr_threads;
  85. os_data->nr_running = nr_running();
  86. os_data->nr_iowait = nr_iowait();
  87. os_data->avenrun[0] = avenrun[0] + (FIXED_1/200);
  88. os_data->avenrun[1] = avenrun[1] + (FIXED_1/200);
  89. os_data->avenrun[2] = avenrun[2] + (FIXED_1/200);
  90. j = 0;
  91. for_each_online_cpu(i) {
  92. os_data->os_cpu[j].per_cpu_user =
  93. nsecs_to_jiffies(kcpustat_cpu(i).cpustat[CPUTIME_USER]);
  94. os_data->os_cpu[j].per_cpu_nice =
  95. nsecs_to_jiffies(kcpustat_cpu(i).cpustat[CPUTIME_NICE]);
  96. os_data->os_cpu[j].per_cpu_system =
  97. nsecs_to_jiffies(kcpustat_cpu(i).cpustat[CPUTIME_SYSTEM]);
  98. os_data->os_cpu[j].per_cpu_idle =
  99. nsecs_to_jiffies(kcpustat_cpu(i).cpustat[CPUTIME_IDLE]);
  100. os_data->os_cpu[j].per_cpu_irq =
  101. nsecs_to_jiffies(kcpustat_cpu(i).cpustat[CPUTIME_IRQ]);
  102. os_data->os_cpu[j].per_cpu_softirq =
  103. nsecs_to_jiffies(kcpustat_cpu(i).cpustat[CPUTIME_SOFTIRQ]);
  104. os_data->os_cpu[j].per_cpu_iowait =
  105. nsecs_to_jiffies(kcpustat_cpu(i).cpustat[CPUTIME_IOWAIT]);
  106. os_data->os_cpu[j].per_cpu_steal =
  107. nsecs_to_jiffies(kcpustat_cpu(i).cpustat[CPUTIME_STEAL]);
  108. os_data->os_cpu[j].cpu_id = i;
  109. j++;
  110. }
  111. os_data->nr_cpus = j;
  112. new_size = struct_size(os_data, os_cpu, os_data->nr_cpus);
  113. if (ops.size != new_size) {
  114. if (ops.active) {
  115. rc = appldata_diag(APPLDATA_RECORD_OS_ID,
  116. APPLDATA_START_INTERVAL_REC,
  117. (unsigned long) ops.data, new_size,
  118. ops.mod_lvl);
  119. if (rc != 0)
  120. pr_err("Starting a new OS data collection "
  121. "failed with rc=%d\n", rc);
  122. rc = appldata_diag(APPLDATA_RECORD_OS_ID,
  123. APPLDATA_STOP_REC,
  124. (unsigned long) ops.data, ops.size,
  125. ops.mod_lvl);
  126. if (rc != 0)
  127. pr_err("Stopping a faulty OS data "
  128. "collection failed with rc=%d\n", rc);
  129. }
  130. ops.size = new_size;
  131. }
  132. os_data->timestamp = get_tod_clock();
  133. os_data->sync_count_2++;
  134. }
  135. /*
  136. * appldata_os_init()
  137. *
  138. * init data, register ops
  139. */
  140. static int __init appldata_os_init(void)
  141. {
  142. int rc, max_size;
  143. max_size = struct_size(appldata_os_data, os_cpu, num_possible_cpus());
  144. if (max_size > APPLDATA_MAX_REC_SIZE) {
  145. pr_err("Maximum OS record size %i exceeds the maximum "
  146. "record size %i\n", max_size, APPLDATA_MAX_REC_SIZE);
  147. rc = -ENOMEM;
  148. goto out;
  149. }
  150. appldata_os_data = kzalloc(max_size, GFP_KERNEL | GFP_DMA);
  151. if (appldata_os_data == NULL) {
  152. rc = -ENOMEM;
  153. goto out;
  154. }
  155. appldata_os_data->per_cpu_size = sizeof(struct appldata_os_per_cpu);
  156. appldata_os_data->cpu_offset = offsetof(struct appldata_os_data,
  157. os_cpu);
  158. ops.data = appldata_os_data;
  159. ops.callback = &appldata_get_os_data;
  160. rc = appldata_register_ops(&ops);
  161. if (rc != 0)
  162. kfree(appldata_os_data);
  163. out:
  164. return rc;
  165. }
  166. /*
  167. * appldata_os_exit()
  168. *
  169. * unregister ops
  170. */
  171. static void __exit appldata_os_exit(void)
  172. {
  173. appldata_unregister_ops(&ops);
  174. kfree(appldata_os_data);
  175. }
  176. module_init(appldata_os_init);
  177. module_exit(appldata_os_exit);
  178. MODULE_LICENSE("GPL");
  179. MODULE_AUTHOR("Gerald Schaefer");
  180. MODULE_DESCRIPTION("Linux-VM Monitor Stream, OS statistics");