Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | * |
Gerald Schaefer | 5b5dd21 | 2006-06-29 15:08:35 +0200 | [diff] [blame^] | 7 | * Copyright (C) 2003,2006 IBM Corporation, IBM Deutschland Entwicklung GmbH. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | * |
Gerald Schaefer | 5b5dd21 | 2006-06-29 15:08:35 +0200 | [diff] [blame^] | 9 | * Author: Gerald Schaefer <gerald.schaefer@de.ibm.com> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 10 | */ |
| 11 | |
| 12 | #include <linux/config.h> |
| 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/smp.h> |
| 21 | |
| 22 | #include "appldata.h" |
| 23 | |
| 24 | |
| 25 | #define MY_PRINT_NAME "appldata_os" /* for debug messages, etc. */ |
| 26 | #define LOAD_INT(x) ((x) >> FSHIFT) |
| 27 | #define LOAD_FRAC(x) LOAD_INT(((x) & (FIXED_1-1)) * 100) |
| 28 | |
| 29 | /* |
| 30 | * OS data |
| 31 | * |
| 32 | * This is accessed as binary data by z/VM. If changes to it can't be avoided, |
| 33 | * the structure version (product ID, see appldata_base.c) needs to be changed |
| 34 | * as well and all documentation and z/VM applications using it must be |
| 35 | * updated. |
| 36 | * |
| 37 | * The record layout is documented in the Linux for zSeries Device Drivers |
| 38 | * book: |
| 39 | * http://oss.software.ibm.com/developerworks/opensource/linux390/index.shtml |
| 40 | */ |
| 41 | struct appldata_os_per_cpu { |
| 42 | u32 per_cpu_user; /* timer ticks spent in user mode */ |
| 43 | u32 per_cpu_nice; /* ... spent with modified priority */ |
| 44 | u32 per_cpu_system; /* ... spent in kernel mode */ |
| 45 | u32 per_cpu_idle; /* ... spent in idle mode */ |
| 46 | |
Gerald Schaefer | 5b5dd21 | 2006-06-29 15:08:35 +0200 | [diff] [blame^] | 47 | /* New in 2.6 */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | u32 per_cpu_irq; /* ... spent in interrupts */ |
| 49 | u32 per_cpu_softirq; /* ... spent in softirqs */ |
| 50 | u32 per_cpu_iowait; /* ... spent while waiting for I/O */ |
Gerald Schaefer | 5b5dd21 | 2006-06-29 15:08:35 +0200 | [diff] [blame^] | 51 | |
| 52 | /* New in modification level 01 */ |
| 53 | u32 per_cpu_steal; /* ... stolen by hypervisor */ |
| 54 | u32 cpu_id; /* number of this CPU */ |
Gerald Schaefer | f26d583 | 2005-06-04 15:43:33 -0700 | [diff] [blame] | 55 | } __attribute__((packed)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | |
| 57 | struct appldata_os_data { |
| 58 | u64 timestamp; |
| 59 | u32 sync_count_1; /* after VM collected the record data, */ |
| 60 | u32 sync_count_2; /* sync_count_1 and sync_count_2 should be the |
| 61 | same. If not, the record has been updated on |
| 62 | the Linux side while VM was collecting the |
| 63 | (possibly corrupt) data */ |
| 64 | |
| 65 | u32 nr_cpus; /* number of (virtual) CPUs */ |
| 66 | u32 per_cpu_size; /* size of the per-cpu data struct */ |
| 67 | u32 cpu_offset; /* offset of the first per-cpu data struct */ |
| 68 | |
| 69 | u32 nr_running; /* number of runnable threads */ |
| 70 | u32 nr_threads; /* number of threads */ |
| 71 | u32 avenrun[3]; /* average nr. of running processes during */ |
| 72 | /* the last 1, 5 and 15 minutes */ |
| 73 | |
Gerald Schaefer | 5b5dd21 | 2006-06-29 15:08:35 +0200 | [diff] [blame^] | 74 | /* New in 2.6 */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | u32 nr_iowait; /* number of blocked threads |
| 76 | (waiting for I/O) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | |
| 78 | /* per cpu data */ |
| 79 | struct appldata_os_per_cpu os_cpu[0]; |
Gerald Schaefer | f26d583 | 2005-06-04 15:43:33 -0700 | [diff] [blame] | 80 | } __attribute__((packed)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | |
| 82 | static struct appldata_os_data *appldata_os_data; |
| 83 | |
Gerald Schaefer | 5b5dd21 | 2006-06-29 15:08:35 +0200 | [diff] [blame^] | 84 | static struct appldata_ops ops = { |
| 85 | .ctl_nr = CTL_APPLDATA_OS, |
| 86 | .name = "os", |
| 87 | .record_nr = APPLDATA_RECORD_OS_ID, |
| 88 | .owner = THIS_MODULE, |
| 89 | .mod_lvl = {0xF0, 0xF1}, /* EBCDIC "01" */ |
| 90 | }; |
| 91 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | |
| 93 | static inline void appldata_print_debug(struct appldata_os_data *os_data) |
| 94 | { |
| 95 | int a0, a1, a2, i; |
| 96 | |
| 97 | P_DEBUG("--- OS - RECORD ---\n"); |
| 98 | P_DEBUG("nr_threads = %u\n", os_data->nr_threads); |
| 99 | P_DEBUG("nr_running = %u\n", os_data->nr_running); |
| 100 | P_DEBUG("nr_iowait = %u\n", os_data->nr_iowait); |
| 101 | P_DEBUG("avenrun(int) = %8x / %8x / %8x\n", os_data->avenrun[0], |
| 102 | os_data->avenrun[1], os_data->avenrun[2]); |
| 103 | a0 = os_data->avenrun[0]; |
| 104 | a1 = os_data->avenrun[1]; |
| 105 | a2 = os_data->avenrun[2]; |
| 106 | P_DEBUG("avenrun(float) = %d.%02d / %d.%02d / %d.%02d\n", |
| 107 | LOAD_INT(a0), LOAD_FRAC(a0), LOAD_INT(a1), LOAD_FRAC(a1), |
| 108 | LOAD_INT(a2), LOAD_FRAC(a2)); |
| 109 | |
| 110 | P_DEBUG("nr_cpus = %u\n", os_data->nr_cpus); |
| 111 | for (i = 0; i < os_data->nr_cpus; i++) { |
| 112 | P_DEBUG("cpu%u : user = %u, nice = %u, system = %u, " |
Gerald Schaefer | 5b5dd21 | 2006-06-29 15:08:35 +0200 | [diff] [blame^] | 113 | "idle = %u, irq = %u, softirq = %u, iowait = %u, " |
| 114 | "steal = %u\n", |
| 115 | os_data->os_cpu[i].cpu_id, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | os_data->os_cpu[i].per_cpu_user, |
| 117 | os_data->os_cpu[i].per_cpu_nice, |
| 118 | os_data->os_cpu[i].per_cpu_system, |
| 119 | os_data->os_cpu[i].per_cpu_idle, |
| 120 | os_data->os_cpu[i].per_cpu_irq, |
| 121 | os_data->os_cpu[i].per_cpu_softirq, |
Gerald Schaefer | 5b5dd21 | 2006-06-29 15:08:35 +0200 | [diff] [blame^] | 122 | os_data->os_cpu[i].per_cpu_iowait, |
| 123 | os_data->os_cpu[i].per_cpu_steal); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | P_DEBUG("sync_count_1 = %u\n", os_data->sync_count_1); |
| 127 | P_DEBUG("sync_count_2 = %u\n", os_data->sync_count_2); |
| 128 | P_DEBUG("timestamp = %lX\n", os_data->timestamp); |
| 129 | } |
| 130 | |
| 131 | /* |
| 132 | * appldata_get_os_data() |
| 133 | * |
| 134 | * gather OS data |
| 135 | */ |
| 136 | static void appldata_get_os_data(void *data) |
| 137 | { |
Gerald Schaefer | 5b5dd21 | 2006-06-29 15:08:35 +0200 | [diff] [blame^] | 138 | int i, j, rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | struct appldata_os_data *os_data; |
Gerald Schaefer | 5b5dd21 | 2006-06-29 15:08:35 +0200 | [diff] [blame^] | 140 | unsigned int new_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | |
| 142 | os_data = data; |
| 143 | os_data->sync_count_1++; |
| 144 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | os_data->nr_threads = nr_threads; |
| 146 | os_data->nr_running = nr_running(); |
| 147 | os_data->nr_iowait = nr_iowait(); |
| 148 | os_data->avenrun[0] = avenrun[0] + (FIXED_1/200); |
| 149 | os_data->avenrun[1] = avenrun[1] + (FIXED_1/200); |
| 150 | os_data->avenrun[2] = avenrun[2] + (FIXED_1/200); |
| 151 | |
| 152 | j = 0; |
| 153 | for_each_online_cpu(i) { |
| 154 | os_data->os_cpu[j].per_cpu_user = |
Martin Schwidefsky | 089545f | 2006-01-06 00:19:12 -0800 | [diff] [blame] | 155 | cputime_to_jiffies(kstat_cpu(i).cpustat.user); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | os_data->os_cpu[j].per_cpu_nice = |
Martin Schwidefsky | 089545f | 2006-01-06 00:19:12 -0800 | [diff] [blame] | 157 | cputime_to_jiffies(kstat_cpu(i).cpustat.nice); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | os_data->os_cpu[j].per_cpu_system = |
Martin Schwidefsky | 089545f | 2006-01-06 00:19:12 -0800 | [diff] [blame] | 159 | cputime_to_jiffies(kstat_cpu(i).cpustat.system); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 | os_data->os_cpu[j].per_cpu_idle = |
Martin Schwidefsky | 089545f | 2006-01-06 00:19:12 -0800 | [diff] [blame] | 161 | cputime_to_jiffies(kstat_cpu(i).cpustat.idle); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 162 | os_data->os_cpu[j].per_cpu_irq = |
Martin Schwidefsky | 089545f | 2006-01-06 00:19:12 -0800 | [diff] [blame] | 163 | cputime_to_jiffies(kstat_cpu(i).cpustat.irq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | os_data->os_cpu[j].per_cpu_softirq = |
Martin Schwidefsky | 089545f | 2006-01-06 00:19:12 -0800 | [diff] [blame] | 165 | cputime_to_jiffies(kstat_cpu(i).cpustat.softirq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | os_data->os_cpu[j].per_cpu_iowait = |
Martin Schwidefsky | 089545f | 2006-01-06 00:19:12 -0800 | [diff] [blame] | 167 | cputime_to_jiffies(kstat_cpu(i).cpustat.iowait); |
Gerald Schaefer | 5b5dd21 | 2006-06-29 15:08:35 +0200 | [diff] [blame^] | 168 | os_data->os_cpu[j].per_cpu_steal = |
| 169 | cputime_to_jiffies(kstat_cpu(i).cpustat.steal); |
| 170 | os_data->os_cpu[j].cpu_id = i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | j++; |
| 172 | } |
| 173 | |
Gerald Schaefer | 5b5dd21 | 2006-06-29 15:08:35 +0200 | [diff] [blame^] | 174 | os_data->nr_cpus = j; |
| 175 | |
| 176 | new_size = sizeof(struct appldata_os_data) + |
| 177 | (os_data->nr_cpus * sizeof(struct appldata_os_per_cpu)); |
| 178 | if (ops.size != new_size) { |
| 179 | if (ops.active) { |
| 180 | rc = appldata_diag(APPLDATA_RECORD_OS_ID, |
| 181 | APPLDATA_START_INTERVAL_REC, |
| 182 | (unsigned long) ops.data, new_size, |
| 183 | ops.mod_lvl); |
| 184 | if (rc != 0) { |
| 185 | P_ERROR("os: START NEW DIAG 0xDC failed, " |
| 186 | "return code: %d, new size = %i\n", rc, |
| 187 | new_size); |
| 188 | P_INFO("os: stopping old record now\n"); |
| 189 | } else |
| 190 | P_INFO("os: new record size = %i\n", new_size); |
| 191 | |
| 192 | rc = appldata_diag(APPLDATA_RECORD_OS_ID, |
| 193 | APPLDATA_STOP_REC, |
| 194 | (unsigned long) ops.data, ops.size, |
| 195 | ops.mod_lvl); |
| 196 | if (rc != 0) |
| 197 | P_ERROR("os: STOP OLD DIAG 0xDC failed, " |
| 198 | "return code: %d, old size = %i\n", rc, |
| 199 | ops.size); |
| 200 | else |
| 201 | P_INFO("os: old record size = %i stopped\n", |
| 202 | ops.size); |
| 203 | } |
| 204 | ops.size = new_size; |
| 205 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 206 | os_data->timestamp = get_clock(); |
| 207 | os_data->sync_count_2++; |
| 208 | #ifdef APPLDATA_DEBUG |
| 209 | appldata_print_debug(os_data); |
| 210 | #endif |
| 211 | } |
| 212 | |
| 213 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 214 | /* |
| 215 | * appldata_os_init() |
| 216 | * |
| 217 | * init data, register ops |
| 218 | */ |
| 219 | static int __init appldata_os_init(void) |
| 220 | { |
Gerald Schaefer | 5b5dd21 | 2006-06-29 15:08:35 +0200 | [diff] [blame^] | 221 | int rc, max_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 222 | |
Gerald Schaefer | 5b5dd21 | 2006-06-29 15:08:35 +0200 | [diff] [blame^] | 223 | max_size = sizeof(struct appldata_os_data) + |
| 224 | (NR_CPUS * sizeof(struct appldata_os_per_cpu)); |
| 225 | if (max_size > APPLDATA_MAX_REC_SIZE) { |
| 226 | P_ERROR("Max. size of OS record = %i, bigger than maximum " |
| 227 | "record size (%i)\n", max_size, APPLDATA_MAX_REC_SIZE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 228 | rc = -ENOMEM; |
| 229 | goto out; |
| 230 | } |
Gerald Schaefer | 5b5dd21 | 2006-06-29 15:08:35 +0200 | [diff] [blame^] | 231 | P_DEBUG("max. sizeof(os) = %i, sizeof(os_cpu) = %lu\n", max_size, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 232 | sizeof(struct appldata_os_per_cpu)); |
| 233 | |
Gerald Schaefer | 5b5dd21 | 2006-06-29 15:08:35 +0200 | [diff] [blame^] | 234 | appldata_os_data = kzalloc(max_size, GFP_DMA); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 235 | if (appldata_os_data == NULL) { |
| 236 | P_ERROR("No memory for %s!\n", ops.name); |
| 237 | rc = -ENOMEM; |
| 238 | goto out; |
| 239 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 240 | |
| 241 | appldata_os_data->per_cpu_size = sizeof(struct appldata_os_per_cpu); |
| 242 | appldata_os_data->cpu_offset = offsetof(struct appldata_os_data, |
| 243 | os_cpu); |
| 244 | P_DEBUG("cpu offset = %u\n", appldata_os_data->cpu_offset); |
| 245 | |
| 246 | ops.data = appldata_os_data; |
Gerald Schaefer | 5b5dd21 | 2006-06-29 15:08:35 +0200 | [diff] [blame^] | 247 | ops.callback = &appldata_get_os_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 248 | rc = appldata_register_ops(&ops); |
| 249 | if (rc != 0) { |
| 250 | P_ERROR("Error registering ops, rc = %i\n", rc); |
| 251 | kfree(appldata_os_data); |
| 252 | } else { |
| 253 | P_DEBUG("%s-ops registered!\n", ops.name); |
| 254 | } |
| 255 | out: |
| 256 | return rc; |
| 257 | } |
| 258 | |
| 259 | /* |
| 260 | * appldata_os_exit() |
| 261 | * |
| 262 | * unregister ops |
| 263 | */ |
| 264 | static void __exit appldata_os_exit(void) |
| 265 | { |
| 266 | appldata_unregister_ops(&ops); |
| 267 | kfree(appldata_os_data); |
| 268 | P_DEBUG("%s-ops unregistered!\n", ops.name); |
| 269 | } |
| 270 | |
| 271 | |
| 272 | module_init(appldata_os_init); |
| 273 | module_exit(appldata_os_exit); |
| 274 | |
| 275 | MODULE_LICENSE("GPL"); |
| 276 | MODULE_AUTHOR("Gerald Schaefer"); |
| 277 | MODULE_DESCRIPTION("Linux-VM Monitor Stream, OS statistics"); |