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 | |
Gerald Schaefer | e7534b0 | 2008-12-25 13:39:41 +0100 | [diff] [blame] | 12 | #define KMSG_COMPONENT "appldata" |
| 13 | #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt |
| 14 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | #include <linux/module.h> |
| 16 | #include <linux/init.h> |
| 17 | #include <linux/slab.h> |
| 18 | #include <linux/errno.h> |
| 19 | #include <linux/kernel_stat.h> |
| 20 | #include <linux/netdevice.h> |
| 21 | #include <linux/sched.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 | |
| 27 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | #define LOAD_INT(x) ((x) >> FSHIFT) |
| 29 | #define LOAD_FRAC(x) LOAD_INT(((x) & (FIXED_1-1)) * 100) |
| 30 | |
| 31 | /* |
| 32 | * OS data |
| 33 | * |
| 34 | * This is accessed as binary data by z/VM. If changes to it can't be avoided, |
| 35 | * the structure version (product ID, see appldata_base.c) needs to be changed |
| 36 | * as well and all documentation and z/VM applications using it must be |
| 37 | * updated. |
| 38 | * |
| 39 | * The record layout is documented in the Linux for zSeries Device Drivers |
| 40 | * book: |
| 41 | * http://oss.software.ibm.com/developerworks/opensource/linux390/index.shtml |
| 42 | */ |
| 43 | struct appldata_os_per_cpu { |
| 44 | u32 per_cpu_user; /* timer ticks spent in user mode */ |
| 45 | u32 per_cpu_nice; /* ... spent with modified priority */ |
| 46 | u32 per_cpu_system; /* ... spent in kernel mode */ |
| 47 | u32 per_cpu_idle; /* ... spent in idle mode */ |
| 48 | |
Gerald Schaefer | 5b5dd21 | 2006-06-29 15:08:35 +0200 | [diff] [blame] | 49 | /* New in 2.6 */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | u32 per_cpu_irq; /* ... spent in interrupts */ |
| 51 | u32 per_cpu_softirq; /* ... spent in softirqs */ |
| 52 | u32 per_cpu_iowait; /* ... spent while waiting for I/O */ |
Gerald Schaefer | 5b5dd21 | 2006-06-29 15:08:35 +0200 | [diff] [blame] | 53 | |
| 54 | /* New in modification level 01 */ |
| 55 | u32 per_cpu_steal; /* ... stolen by hypervisor */ |
| 56 | u32 cpu_id; /* number of this CPU */ |
Gerald Schaefer | f26d583 | 2005-06-04 15:43:33 -0700 | [diff] [blame] | 57 | } __attribute__((packed)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | |
| 59 | struct appldata_os_data { |
| 60 | u64 timestamp; |
| 61 | u32 sync_count_1; /* after VM collected the record data, */ |
| 62 | u32 sync_count_2; /* sync_count_1 and sync_count_2 should be the |
| 63 | same. If not, the record has been updated on |
| 64 | the Linux side while VM was collecting the |
| 65 | (possibly corrupt) data */ |
| 66 | |
| 67 | u32 nr_cpus; /* number of (virtual) CPUs */ |
| 68 | u32 per_cpu_size; /* size of the per-cpu data struct */ |
| 69 | u32 cpu_offset; /* offset of the first per-cpu data struct */ |
| 70 | |
| 71 | u32 nr_running; /* number of runnable threads */ |
| 72 | u32 nr_threads; /* number of threads */ |
| 73 | u32 avenrun[3]; /* average nr. of running processes during */ |
| 74 | /* the last 1, 5 and 15 minutes */ |
| 75 | |
Gerald Schaefer | 5b5dd21 | 2006-06-29 15:08:35 +0200 | [diff] [blame] | 76 | /* New in 2.6 */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | u32 nr_iowait; /* number of blocked threads |
| 78 | (waiting for I/O) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | |
| 80 | /* per cpu data */ |
| 81 | struct appldata_os_per_cpu os_cpu[0]; |
Gerald Schaefer | f26d583 | 2005-06-04 15:43:33 -0700 | [diff] [blame] | 82 | } __attribute__((packed)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | |
| 84 | static struct appldata_os_data *appldata_os_data; |
| 85 | |
Gerald Schaefer | 5b5dd21 | 2006-06-29 15:08:35 +0200 | [diff] [blame] | 86 | static struct appldata_ops ops = { |
Gerald Schaefer | 5b5dd21 | 2006-06-29 15:08:35 +0200 | [diff] [blame] | 87 | .name = "os", |
| 88 | .record_nr = APPLDATA_RECORD_OS_ID, |
| 89 | .owner = THIS_MODULE, |
| 90 | .mod_lvl = {0xF0, 0xF1}, /* EBCDIC "01" */ |
| 91 | }; |
| 92 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | /* |
| 95 | * appldata_get_os_data() |
| 96 | * |
| 97 | * gather OS data |
| 98 | */ |
| 99 | static void appldata_get_os_data(void *data) |
| 100 | { |
Gerald Schaefer | 5b5dd21 | 2006-06-29 15:08:35 +0200 | [diff] [blame] | 101 | int i, j, rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | struct appldata_os_data *os_data; |
Gerald Schaefer | 5b5dd21 | 2006-06-29 15:08:35 +0200 | [diff] [blame] | 103 | unsigned int new_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | |
| 105 | os_data = data; |
| 106 | os_data->sync_count_1++; |
| 107 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | os_data->nr_threads = nr_threads; |
| 109 | os_data->nr_running = nr_running(); |
| 110 | os_data->nr_iowait = nr_iowait(); |
| 111 | os_data->avenrun[0] = avenrun[0] + (FIXED_1/200); |
| 112 | os_data->avenrun[1] = avenrun[1] + (FIXED_1/200); |
| 113 | os_data->avenrun[2] = avenrun[2] + (FIXED_1/200); |
| 114 | |
| 115 | j = 0; |
| 116 | for_each_online_cpu(i) { |
| 117 | os_data->os_cpu[j].per_cpu_user = |
Martin Schwidefsky | 089545f | 2006-01-06 00:19:12 -0800 | [diff] [blame] | 118 | cputime_to_jiffies(kstat_cpu(i).cpustat.user); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | os_data->os_cpu[j].per_cpu_nice = |
Martin Schwidefsky | 089545f | 2006-01-06 00:19:12 -0800 | [diff] [blame] | 120 | cputime_to_jiffies(kstat_cpu(i).cpustat.nice); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | os_data->os_cpu[j].per_cpu_system = |
Martin Schwidefsky | 089545f | 2006-01-06 00:19:12 -0800 | [diff] [blame] | 122 | cputime_to_jiffies(kstat_cpu(i).cpustat.system); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | os_data->os_cpu[j].per_cpu_idle = |
Martin Schwidefsky | 089545f | 2006-01-06 00:19:12 -0800 | [diff] [blame] | 124 | cputime_to_jiffies(kstat_cpu(i).cpustat.idle); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | os_data->os_cpu[j].per_cpu_irq = |
Martin Schwidefsky | 089545f | 2006-01-06 00:19:12 -0800 | [diff] [blame] | 126 | cputime_to_jiffies(kstat_cpu(i).cpustat.irq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | os_data->os_cpu[j].per_cpu_softirq = |
Martin Schwidefsky | 089545f | 2006-01-06 00:19:12 -0800 | [diff] [blame] | 128 | cputime_to_jiffies(kstat_cpu(i).cpustat.softirq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | os_data->os_cpu[j].per_cpu_iowait = |
Martin Schwidefsky | 089545f | 2006-01-06 00:19:12 -0800 | [diff] [blame] | 130 | cputime_to_jiffies(kstat_cpu(i).cpustat.iowait); |
Gerald Schaefer | 5b5dd21 | 2006-06-29 15:08:35 +0200 | [diff] [blame] | 131 | os_data->os_cpu[j].per_cpu_steal = |
| 132 | cputime_to_jiffies(kstat_cpu(i).cpustat.steal); |
| 133 | os_data->os_cpu[j].cpu_id = i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | j++; |
| 135 | } |
| 136 | |
Gerald Schaefer | 5b5dd21 | 2006-06-29 15:08:35 +0200 | [diff] [blame] | 137 | os_data->nr_cpus = j; |
| 138 | |
| 139 | new_size = sizeof(struct appldata_os_data) + |
| 140 | (os_data->nr_cpus * sizeof(struct appldata_os_per_cpu)); |
| 141 | if (ops.size != new_size) { |
| 142 | if (ops.active) { |
| 143 | rc = appldata_diag(APPLDATA_RECORD_OS_ID, |
| 144 | APPLDATA_START_INTERVAL_REC, |
| 145 | (unsigned long) ops.data, new_size, |
| 146 | ops.mod_lvl); |
Gerald Schaefer | d3ae942 | 2008-07-14 09:59:34 +0200 | [diff] [blame] | 147 | if (rc != 0) |
Gerald Schaefer | e7534b0 | 2008-12-25 13:39:41 +0100 | [diff] [blame] | 148 | pr_err("Starting a new OS data collection " |
| 149 | "failed with rc=%d\n", rc); |
Gerald Schaefer | 5b5dd21 | 2006-06-29 15:08:35 +0200 | [diff] [blame] | 150 | |
| 151 | rc = appldata_diag(APPLDATA_RECORD_OS_ID, |
| 152 | APPLDATA_STOP_REC, |
| 153 | (unsigned long) ops.data, ops.size, |
| 154 | ops.mod_lvl); |
| 155 | if (rc != 0) |
Gerald Schaefer | e7534b0 | 2008-12-25 13:39:41 +0100 | [diff] [blame] | 156 | pr_err("Stopping a faulty OS data " |
| 157 | "collection failed with rc=%d\n", rc); |
Gerald Schaefer | 5b5dd21 | 2006-06-29 15:08:35 +0200 | [diff] [blame] | 158 | } |
| 159 | ops.size = new_size; |
| 160 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | os_data->timestamp = get_clock(); |
| 162 | os_data->sync_count_2++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | /* |
| 167 | * appldata_os_init() |
| 168 | * |
| 169 | * init data, register ops |
| 170 | */ |
| 171 | static int __init appldata_os_init(void) |
| 172 | { |
Gerald Schaefer | 5b5dd21 | 2006-06-29 15:08:35 +0200 | [diff] [blame] | 173 | int rc, max_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 174 | |
Gerald Schaefer | 5b5dd21 | 2006-06-29 15:08:35 +0200 | [diff] [blame] | 175 | max_size = sizeof(struct appldata_os_data) + |
| 176 | (NR_CPUS * sizeof(struct appldata_os_per_cpu)); |
| 177 | if (max_size > APPLDATA_MAX_REC_SIZE) { |
Gerald Schaefer | e7534b0 | 2008-12-25 13:39:41 +0100 | [diff] [blame] | 178 | pr_err("Maximum OS record size %i exceeds the maximum " |
| 179 | "record size %i\n", max_size, APPLDATA_MAX_REC_SIZE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | rc = -ENOMEM; |
| 181 | goto out; |
| 182 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 183 | |
Heiko Carstens | c2f0e8c | 2010-06-08 18:58:09 +0200 | [diff] [blame] | 184 | appldata_os_data = kzalloc(max_size, GFP_KERNEL | GFP_DMA); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | if (appldata_os_data == NULL) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | rc = -ENOMEM; |
| 187 | goto out; |
| 188 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 189 | |
| 190 | appldata_os_data->per_cpu_size = sizeof(struct appldata_os_per_cpu); |
| 191 | appldata_os_data->cpu_offset = offsetof(struct appldata_os_data, |
| 192 | os_cpu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 193 | |
| 194 | ops.data = appldata_os_data; |
Gerald Schaefer | 5b5dd21 | 2006-06-29 15:08:35 +0200 | [diff] [blame] | 195 | ops.callback = &appldata_get_os_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 196 | rc = appldata_register_ops(&ops); |
Gerald Schaefer | d3ae942 | 2008-07-14 09:59:34 +0200 | [diff] [blame] | 197 | if (rc != 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 | kfree(appldata_os_data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | out: |
| 200 | return rc; |
| 201 | } |
| 202 | |
| 203 | /* |
| 204 | * appldata_os_exit() |
| 205 | * |
| 206 | * unregister ops |
| 207 | */ |
| 208 | static void __exit appldata_os_exit(void) |
| 209 | { |
| 210 | appldata_unregister_ops(&ops); |
| 211 | kfree(appldata_os_data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | |
| 215 | module_init(appldata_os_init); |
| 216 | module_exit(appldata_os_exit); |
| 217 | |
| 218 | MODULE_LICENSE("GPL"); |
| 219 | MODULE_AUTHOR("Gerald Schaefer"); |
| 220 | MODULE_DESCRIPTION("Linux-VM Monitor Stream, OS statistics"); |