Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | * Data gathering module for Linux-VM Monitor Stream, Stage 1. |
| 3 | * Collects misc. OS related data (CPU utilization, running processes). |
| 4 | * |
Heiko Carstens | a53c8fa | 2012-07-20 11:15:04 +0200 | [diff] [blame] | 5 | * Copyright IBM Corp. 2003, 2006 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6 | * |
Gerald Schaefer | 5b5dd21 | 2006-06-29 15:08:35 +0200 | [diff] [blame] | 7 | * Author: Gerald Schaefer <gerald.schaefer@de.ibm.com> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | */ |
| 9 | |
Gerald Schaefer | e7534b0 | 2008-12-25 13:39:41 +0100 | [diff] [blame] | 10 | #define KMSG_COMPONENT "appldata" |
| 11 | #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt |
| 12 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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> |
Ingo Molnar | 4f17722 | 2017-02-08 08:45:17 +0100 | [diff] [blame] | 20 | #include <linux/sched/loadavg.h> |
Ingo Molnar | 03441a3 | 2017-02-08 18:51:35 +0100 | [diff] [blame] | 21 | #include <linux/sched/stat.h> |
Gerald Schaefer | 1f38d61 | 2006-09-20 15:59:26 +0200 | [diff] [blame] | 22 | #include <asm/appldata.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | #include <asm/smp.h> |
| 24 | |
| 25 | #include "appldata.h" |
| 26 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | * The record layout is documented in the Linux for zSeries Device Drivers |
| 36 | * book: |
| 37 | * http://oss.software.ibm.com/developerworks/opensource/linux390/index.shtml |
| 38 | */ |
| 39 | struct appldata_os_per_cpu { |
| 40 | u32 per_cpu_user; /* timer ticks spent in user mode */ |
| 41 | u32 per_cpu_nice; /* ... spent with modified priority */ |
| 42 | u32 per_cpu_system; /* ... spent in kernel mode */ |
| 43 | u32 per_cpu_idle; /* ... spent in idle mode */ |
| 44 | |
Gerald Schaefer | 5b5dd21 | 2006-06-29 15:08:35 +0200 | [diff] [blame] | 45 | /* New in 2.6 */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | u32 per_cpu_irq; /* ... spent in interrupts */ |
| 47 | u32 per_cpu_softirq; /* ... spent in softirqs */ |
| 48 | u32 per_cpu_iowait; /* ... spent while waiting for I/O */ |
Gerald Schaefer | 5b5dd21 | 2006-06-29 15:08:35 +0200 | [diff] [blame] | 49 | |
| 50 | /* New in modification level 01 */ |
| 51 | u32 per_cpu_steal; /* ... stolen by hypervisor */ |
| 52 | u32 cpu_id; /* number of this CPU */ |
Gerald Schaefer | f26d583 | 2005-06-04 15:43:33 -0700 | [diff] [blame] | 53 | } __attribute__((packed)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | |
| 55 | struct appldata_os_data { |
| 56 | u64 timestamp; |
| 57 | u32 sync_count_1; /* after VM collected the record data, */ |
| 58 | u32 sync_count_2; /* sync_count_1 and sync_count_2 should be the |
| 59 | same. If not, the record has been updated on |
| 60 | the Linux side while VM was collecting the |
| 61 | (possibly corrupt) data */ |
| 62 | |
| 63 | u32 nr_cpus; /* number of (virtual) CPUs */ |
| 64 | u32 per_cpu_size; /* size of the per-cpu data struct */ |
| 65 | u32 cpu_offset; /* offset of the first per-cpu data struct */ |
| 66 | |
| 67 | u32 nr_running; /* number of runnable threads */ |
| 68 | u32 nr_threads; /* number of threads */ |
| 69 | u32 avenrun[3]; /* average nr. of running processes during */ |
| 70 | /* the last 1, 5 and 15 minutes */ |
| 71 | |
Gerald Schaefer | 5b5dd21 | 2006-06-29 15:08:35 +0200 | [diff] [blame] | 72 | /* New in 2.6 */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | u32 nr_iowait; /* number of blocked threads |
| 74 | (waiting for I/O) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | |
| 76 | /* per cpu data */ |
| 77 | struct appldata_os_per_cpu os_cpu[0]; |
Gerald Schaefer | f26d583 | 2005-06-04 15:43:33 -0700 | [diff] [blame] | 78 | } __attribute__((packed)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | |
| 80 | static struct appldata_os_data *appldata_os_data; |
| 81 | |
Gerald Schaefer | 5b5dd21 | 2006-06-29 15:08:35 +0200 | [diff] [blame] | 82 | static struct appldata_ops ops = { |
Gerald Schaefer | 5b5dd21 | 2006-06-29 15:08:35 +0200 | [diff] [blame] | 83 | .name = "os", |
| 84 | .record_nr = APPLDATA_RECORD_OS_ID, |
| 85 | .owner = THIS_MODULE, |
| 86 | .mod_lvl = {0xF0, 0xF1}, /* EBCDIC "01" */ |
| 87 | }; |
| 88 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | /* |
| 91 | * appldata_get_os_data() |
| 92 | * |
| 93 | * gather OS data |
| 94 | */ |
| 95 | static void appldata_get_os_data(void *data) |
| 96 | { |
Gerald Schaefer | 5b5dd21 | 2006-06-29 15:08:35 +0200 | [diff] [blame] | 97 | int i, j, rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | struct appldata_os_data *os_data; |
Gerald Schaefer | 5b5dd21 | 2006-06-29 15:08:35 +0200 | [diff] [blame] | 99 | unsigned int new_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | |
| 101 | os_data = data; |
| 102 | os_data->sync_count_1++; |
| 103 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | os_data->nr_threads = nr_threads; |
| 105 | os_data->nr_running = nr_running(); |
| 106 | os_data->nr_iowait = nr_iowait(); |
| 107 | os_data->avenrun[0] = avenrun[0] + (FIXED_1/200); |
| 108 | os_data->avenrun[1] = avenrun[1] + (FIXED_1/200); |
| 109 | os_data->avenrun[2] = avenrun[2] + (FIXED_1/200); |
| 110 | |
| 111 | j = 0; |
| 112 | for_each_online_cpu(i) { |
| 113 | os_data->os_cpu[j].per_cpu_user = |
Frederic Weisbecker | 7fb1327 | 2017-01-31 04:09:19 +0100 | [diff] [blame] | 114 | nsecs_to_jiffies(kcpustat_cpu(i).cpustat[CPUTIME_USER]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | os_data->os_cpu[j].per_cpu_nice = |
Frederic Weisbecker | 7fb1327 | 2017-01-31 04:09:19 +0100 | [diff] [blame] | 116 | nsecs_to_jiffies(kcpustat_cpu(i).cpustat[CPUTIME_NICE]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | os_data->os_cpu[j].per_cpu_system = |
Frederic Weisbecker | 7fb1327 | 2017-01-31 04:09:19 +0100 | [diff] [blame] | 118 | nsecs_to_jiffies(kcpustat_cpu(i).cpustat[CPUTIME_SYSTEM]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | os_data->os_cpu[j].per_cpu_idle = |
Frederic Weisbecker | 7fb1327 | 2017-01-31 04:09:19 +0100 | [diff] [blame] | 120 | nsecs_to_jiffies(kcpustat_cpu(i).cpustat[CPUTIME_IDLE]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | os_data->os_cpu[j].per_cpu_irq = |
Frederic Weisbecker | 7fb1327 | 2017-01-31 04:09:19 +0100 | [diff] [blame] | 122 | nsecs_to_jiffies(kcpustat_cpu(i).cpustat[CPUTIME_IRQ]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | os_data->os_cpu[j].per_cpu_softirq = |
Frederic Weisbecker | 7fb1327 | 2017-01-31 04:09:19 +0100 | [diff] [blame] | 124 | nsecs_to_jiffies(kcpustat_cpu(i).cpustat[CPUTIME_SOFTIRQ]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | os_data->os_cpu[j].per_cpu_iowait = |
Frederic Weisbecker | 7fb1327 | 2017-01-31 04:09:19 +0100 | [diff] [blame] | 126 | nsecs_to_jiffies(kcpustat_cpu(i).cpustat[CPUTIME_IOWAIT]); |
Gerald Schaefer | 5b5dd21 | 2006-06-29 15:08:35 +0200 | [diff] [blame] | 127 | os_data->os_cpu[j].per_cpu_steal = |
Frederic Weisbecker | 7fb1327 | 2017-01-31 04:09:19 +0100 | [diff] [blame] | 128 | nsecs_to_jiffies(kcpustat_cpu(i).cpustat[CPUTIME_STEAL]); |
Gerald Schaefer | 5b5dd21 | 2006-06-29 15:08:35 +0200 | [diff] [blame] | 129 | os_data->os_cpu[j].cpu_id = i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | j++; |
| 131 | } |
| 132 | |
Gerald Schaefer | 5b5dd21 | 2006-06-29 15:08:35 +0200 | [diff] [blame] | 133 | os_data->nr_cpus = j; |
| 134 | |
| 135 | new_size = sizeof(struct appldata_os_data) + |
| 136 | (os_data->nr_cpus * sizeof(struct appldata_os_per_cpu)); |
| 137 | if (ops.size != new_size) { |
| 138 | if (ops.active) { |
| 139 | rc = appldata_diag(APPLDATA_RECORD_OS_ID, |
| 140 | APPLDATA_START_INTERVAL_REC, |
| 141 | (unsigned long) ops.data, new_size, |
| 142 | ops.mod_lvl); |
Gerald Schaefer | d3ae942 | 2008-07-14 09:59:34 +0200 | [diff] [blame] | 143 | if (rc != 0) |
Gerald Schaefer | e7534b0 | 2008-12-25 13:39:41 +0100 | [diff] [blame] | 144 | pr_err("Starting a new OS data collection " |
| 145 | "failed with rc=%d\n", rc); |
Gerald Schaefer | 5b5dd21 | 2006-06-29 15:08:35 +0200 | [diff] [blame] | 146 | |
| 147 | rc = appldata_diag(APPLDATA_RECORD_OS_ID, |
| 148 | APPLDATA_STOP_REC, |
| 149 | (unsigned long) ops.data, ops.size, |
| 150 | ops.mod_lvl); |
| 151 | if (rc != 0) |
Gerald Schaefer | e7534b0 | 2008-12-25 13:39:41 +0100 | [diff] [blame] | 152 | pr_err("Stopping a faulty OS data " |
| 153 | "collection failed with rc=%d\n", rc); |
Gerald Schaefer | 5b5dd21 | 2006-06-29 15:08:35 +0200 | [diff] [blame] | 154 | } |
| 155 | ops.size = new_size; |
| 156 | } |
Heiko Carstens | 1aae056 | 2013-01-30 09:49:40 +0100 | [diff] [blame] | 157 | os_data->timestamp = get_tod_clock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | os_data->sync_count_2++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 162 | /* |
| 163 | * appldata_os_init() |
| 164 | * |
| 165 | * init data, register ops |
| 166 | */ |
| 167 | static int __init appldata_os_init(void) |
| 168 | { |
Gerald Schaefer | 5b5dd21 | 2006-06-29 15:08:35 +0200 | [diff] [blame] | 169 | int rc, max_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | |
Gerald Schaefer | 5b5dd21 | 2006-06-29 15:08:35 +0200 | [diff] [blame] | 171 | max_size = sizeof(struct appldata_os_data) + |
Gerald Schaefer | 6967037 | 2014-02-28 17:35:52 +0100 | [diff] [blame] | 172 | (num_possible_cpus() * sizeof(struct appldata_os_per_cpu)); |
Gerald Schaefer | 5b5dd21 | 2006-06-29 15:08:35 +0200 | [diff] [blame] | 173 | if (max_size > APPLDATA_MAX_REC_SIZE) { |
Gerald Schaefer | e7534b0 | 2008-12-25 13:39:41 +0100 | [diff] [blame] | 174 | pr_err("Maximum OS record size %i exceeds the maximum " |
| 175 | "record size %i\n", max_size, APPLDATA_MAX_REC_SIZE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | rc = -ENOMEM; |
| 177 | goto out; |
| 178 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 179 | |
Heiko Carstens | c2f0e8c | 2010-06-08 18:58:09 +0200 | [diff] [blame] | 180 | appldata_os_data = kzalloc(max_size, GFP_KERNEL | GFP_DMA); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 181 | if (appldata_os_data == NULL) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 182 | rc = -ENOMEM; |
| 183 | goto out; |
| 184 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | |
| 186 | appldata_os_data->per_cpu_size = sizeof(struct appldata_os_per_cpu); |
| 187 | appldata_os_data->cpu_offset = offsetof(struct appldata_os_data, |
| 188 | os_cpu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 189 | |
| 190 | ops.data = appldata_os_data; |
Gerald Schaefer | 5b5dd21 | 2006-06-29 15:08:35 +0200 | [diff] [blame] | 191 | ops.callback = &appldata_get_os_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 192 | rc = appldata_register_ops(&ops); |
Gerald Schaefer | d3ae942 | 2008-07-14 09:59:34 +0200 | [diff] [blame] | 193 | if (rc != 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 194 | kfree(appldata_os_data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 195 | out: |
| 196 | return rc; |
| 197 | } |
| 198 | |
| 199 | /* |
| 200 | * appldata_os_exit() |
| 201 | * |
| 202 | * unregister ops |
| 203 | */ |
| 204 | static void __exit appldata_os_exit(void) |
| 205 | { |
| 206 | appldata_unregister_ops(&ops); |
| 207 | kfree(appldata_os_data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | |
| 211 | module_init(appldata_os_init); |
| 212 | module_exit(appldata_os_exit); |
| 213 | |
| 214 | MODULE_LICENSE("GPL"); |
| 215 | MODULE_AUTHOR("Gerald Schaefer"); |
| 216 | MODULE_DESCRIPTION("Linux-VM Monitor Stream, OS statistics"); |