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 11 #define pr_fmt(fmt) "appldata: " fmt 12 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 <linux/sched/loadavg.h> 21 #include <linux/sched/stat.h> 22 #include <asm/appldata.h> 23 #include <asm/smp.h> 24 25 #include "appldata.h" 26 27 /* 28 * OS data 29 * 30 * This is accessed as binary data by z/VM. If changes to it can't be avoided, 31 * the structure version (product ID, see appldata_base.c) needs to be changed 32 * as well and all documentation and z/VM applications using it must be 33 * updated. 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 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 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 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 59 u32 nr_cpus; /* number of (virtual) CPUs */ 60 u32 per_cpu_size; /* size of the per-cpu data struct */ 61 u32 cpu_offset; /* offset of the first per-cpu data struct */ 62 63 u32 nr_running; /* number of runnable threads */ 64 u32 nr_threads; /* number of threads */ 65 u32 avenrun[3]; /* average nr. of running processes during */ 66 /* the last 1, 5 and 15 minutes */ 67 68 /* New in 2.6 */ 69 u32 nr_iowait; /* number of blocked threads 70 (waiting for I/O) */ 71 72 /* per cpu data */ 73 struct appldata_os_per_cpu os_cpu[]; 74 } __attribute__((packed)); 75 76 static struct appldata_os_data *appldata_os_data; 77 78 static struct appldata_ops ops = { 79 .name = "os", 80 .record_nr = APPLDATA_RECORD_OS_ID, 81 .owner = THIS_MODULE, 82 .mod_lvl = {0xF0, 0xF1}, /* EBCDIC "01" */ 83 }; 84 85 86 /* 87 * appldata_get_os_data() 88 * 89 * gather OS data 90 */ 91 static void appldata_get_os_data(void *data) 92 { 93 int i, j, rc; 94 struct appldata_os_data *os_data; 95 unsigned int new_size; 96 97 os_data = data; 98 os_data->sync_count_1++; 99 100 os_data->nr_threads = nr_threads; 101 os_data->nr_running = nr_running(); 102 os_data->nr_iowait = nr_iowait(); 103 os_data->avenrun[0] = avenrun[0] + (FIXED_1/200); 104 os_data->avenrun[1] = avenrun[1] + (FIXED_1/200); 105 os_data->avenrun[2] = avenrun[2] + (FIXED_1/200); 106 107 j = 0; 108 for_each_online_cpu(i) { 109 os_data->os_cpu[j].per_cpu_user = 110 nsecs_to_jiffies(kcpustat_cpu(i).cpustat[CPUTIME_USER]); 111 os_data->os_cpu[j].per_cpu_nice = 112 nsecs_to_jiffies(kcpustat_cpu(i).cpustat[CPUTIME_NICE]); 113 os_data->os_cpu[j].per_cpu_system = 114 nsecs_to_jiffies(kcpustat_cpu(i).cpustat[CPUTIME_SYSTEM]); 115 os_data->os_cpu[j].per_cpu_idle = 116 nsecs_to_jiffies(kcpustat_cpu(i).cpustat[CPUTIME_IDLE]); 117 os_data->os_cpu[j].per_cpu_irq = 118 nsecs_to_jiffies(kcpustat_cpu(i).cpustat[CPUTIME_IRQ]); 119 os_data->os_cpu[j].per_cpu_softirq = 120 nsecs_to_jiffies(kcpustat_cpu(i).cpustat[CPUTIME_SOFTIRQ]); 121 os_data->os_cpu[j].per_cpu_iowait = 122 nsecs_to_jiffies(kcpustat_cpu(i).cpustat[CPUTIME_IOWAIT]); 123 os_data->os_cpu[j].per_cpu_steal = 124 nsecs_to_jiffies(kcpustat_cpu(i).cpustat[CPUTIME_STEAL]); 125 os_data->os_cpu[j].cpu_id = i; 126 j++; 127 } 128 129 os_data->nr_cpus = j; 130 131 new_size = struct_size(os_data, os_cpu, os_data->nr_cpus); 132 if (ops.size != new_size) { 133 if (ops.active) { 134 rc = appldata_diag(APPLDATA_RECORD_OS_ID, 135 APPLDATA_START_INTERVAL_REC, 136 (unsigned long) ops.data, new_size, 137 ops.mod_lvl); 138 if (rc != 0) 139 pr_err("Starting a new OS data collection " 140 "failed with rc=%d\n", rc); 141 142 rc = appldata_diag(APPLDATA_RECORD_OS_ID, 143 APPLDATA_STOP_REC, 144 (unsigned long) ops.data, ops.size, 145 ops.mod_lvl); 146 if (rc != 0) 147 pr_err("Stopping a faulty OS data " 148 "collection failed with rc=%d\n", rc); 149 } 150 ops.size = new_size; 151 } 152 os_data->timestamp = get_tod_clock(); 153 os_data->sync_count_2++; 154 } 155 156 157 /* 158 * appldata_os_init() 159 * 160 * init data, register ops 161 */ 162 static int __init appldata_os_init(void) 163 { 164 int rc, max_size; 165 166 max_size = struct_size(appldata_os_data, os_cpu, num_possible_cpus()); 167 if (max_size > APPLDATA_MAX_REC_SIZE) { 168 pr_err("Maximum OS record size %i exceeds the maximum " 169 "record size %i\n", max_size, APPLDATA_MAX_REC_SIZE); 170 rc = -ENOMEM; 171 goto out; 172 } 173 174 appldata_os_data = kzalloc(max_size, GFP_KERNEL | GFP_DMA); 175 if (appldata_os_data == NULL) { 176 rc = -ENOMEM; 177 goto out; 178 } 179 180 appldata_os_data->per_cpu_size = sizeof(struct appldata_os_per_cpu); 181 appldata_os_data->cpu_offset = offsetof(struct appldata_os_data, 182 os_cpu); 183 184 ops.data = appldata_os_data; 185 ops.callback = &appldata_get_os_data; 186 rc = appldata_register_ops(&ops); 187 if (rc != 0) 188 kfree(appldata_os_data); 189 out: 190 return rc; 191 } 192 193 /* 194 * appldata_os_exit() 195 * 196 * unregister ops 197 */ 198 static void __exit appldata_os_exit(void) 199 { 200 appldata_unregister_ops(&ops); 201 kfree(appldata_os_data); 202 } 203 204 205 module_init(appldata_os_init); 206 module_exit(appldata_os_exit); 207 208 MODULE_LICENSE("GPL"); 209 MODULE_AUTHOR("Gerald Schaefer"); 210 MODULE_DESCRIPTION("Linux-VM Monitor Stream, OS statistics"); 211