]>
Commit | Line | Data |
---|---|---|
0caa8cdf | 1 | // SPDX-License-Identifier: GPL-2.0 |
1da177e4 | 2 | /* |
1da177e4 LT |
3 | * Data gathering module for Linux-VM Monitor Stream, Stage 1. |
4 | * Collects misc. OS related data (CPU utilization, running processes). | |
5 | * | |
a53c8fab | 6 | * Copyright IBM Corp. 2003, 2006 |
1da177e4 | 7 | * |
5b5dd21a | 8 | * Author: Gerald Schaefer <gerald.schaefer@de.ibm.com> |
1da177e4 LT |
9 | */ |
10 | ||
e7534b0e GS |
11 | #define KMSG_COMPONENT "appldata" |
12 | #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt | |
13 | ||
1da177e4 LT |
14 | #include <linux/module.h> |
15 | #include <linux/init.h> | |
16 | #include <linux/slab.h> | |
17 | #include <linux/errno.h> | |
18 | #include <linux/kernel_stat.h> | |
19 | #include <linux/netdevice.h> | |
20 | #include <linux/sched.h> | |
4f17722c | 21 | #include <linux/sched/loadavg.h> |
03441a34 | 22 | #include <linux/sched/stat.h> |
1f38d613 | 23 | #include <asm/appldata.h> |
1da177e4 LT |
24 | #include <asm/smp.h> |
25 | ||
26 | #include "appldata.h" | |
27 | ||
1da177e4 LT |
28 | /* |
29 | * OS data | |
30 | * | |
31 | * This is accessed as binary data by z/VM. If changes to it can't be avoided, | |
32 | * the structure version (product ID, see appldata_base.c) needs to be changed | |
33 | * as well and all documentation and z/VM applications using it must be | |
34 | * updated. | |
1da177e4 LT |
35 | */ |
36 | struct appldata_os_per_cpu { | |
37 | u32 per_cpu_user; /* timer ticks spent in user mode */ | |
38 | u32 per_cpu_nice; /* ... spent with modified priority */ | |
39 | u32 per_cpu_system; /* ... spent in kernel mode */ | |
40 | u32 per_cpu_idle; /* ... spent in idle mode */ | |
41 | ||
5b5dd21a | 42 | /* New in 2.6 */ |
1da177e4 LT |
43 | u32 per_cpu_irq; /* ... spent in interrupts */ |
44 | u32 per_cpu_softirq; /* ... spent in softirqs */ | |
45 | u32 per_cpu_iowait; /* ... spent while waiting for I/O */ | |
5b5dd21a GS |
46 | |
47 | /* New in modification level 01 */ | |
48 | u32 per_cpu_steal; /* ... stolen by hypervisor */ | |
49 | u32 cpu_id; /* number of this CPU */ | |
f26d583e | 50 | } __attribute__((packed)); |
1da177e4 LT |
51 | |
52 | struct appldata_os_data { | |
53 | u64 timestamp; | |
54 | u32 sync_count_1; /* after VM collected the record data, */ | |
55 | u32 sync_count_2; /* sync_count_1 and sync_count_2 should be the | |
56 | same. If not, the record has been updated on | |
57 | the Linux side while VM was collecting the | |
58 | (possibly corrupt) data */ | |
59 | ||
60 | u32 nr_cpus; /* number of (virtual) CPUs */ | |
61 | u32 per_cpu_size; /* size of the per-cpu data struct */ | |
62 | u32 cpu_offset; /* offset of the first per-cpu data struct */ | |
63 | ||
64 | u32 nr_running; /* number of runnable threads */ | |
65 | u32 nr_threads; /* number of threads */ | |
66 | u32 avenrun[3]; /* average nr. of running processes during */ | |
67 | /* the last 1, 5 and 15 minutes */ | |
68 | ||
5b5dd21a | 69 | /* New in 2.6 */ |
1da177e4 LT |
70 | u32 nr_iowait; /* number of blocked threads |
71 | (waiting for I/O) */ | |
1da177e4 LT |
72 | |
73 | /* per cpu data */ | |
fa226f1d | 74 | struct appldata_os_per_cpu os_cpu[]; |
f26d583e | 75 | } __attribute__((packed)); |
1da177e4 LT |
76 | |
77 | static struct appldata_os_data *appldata_os_data; | |
78 | ||
5b5dd21a | 79 | static struct appldata_ops ops = { |
5b5dd21a GS |
80 | .name = "os", |
81 | .record_nr = APPLDATA_RECORD_OS_ID, | |
82 | .owner = THIS_MODULE, | |
83 | .mod_lvl = {0xF0, 0xF1}, /* EBCDIC "01" */ | |
84 | }; | |
85 | ||
1da177e4 | 86 | |
1da177e4 LT |
87 | /* |
88 | * appldata_get_os_data() | |
89 | * | |
90 | * gather OS data | |
91 | */ | |
92 | static void appldata_get_os_data(void *data) | |
93 | { | |
5b5dd21a | 94 | int i, j, rc; |
1da177e4 | 95 | struct appldata_os_data *os_data; |
5b5dd21a | 96 | unsigned int new_size; |
1da177e4 LT |
97 | |
98 | os_data = data; | |
99 | os_data->sync_count_1++; | |
100 | ||
1da177e4 LT |
101 | os_data->nr_threads = nr_threads; |
102 | os_data->nr_running = nr_running(); | |
103 | os_data->nr_iowait = nr_iowait(); | |
104 | os_data->avenrun[0] = avenrun[0] + (FIXED_1/200); | |
105 | os_data->avenrun[1] = avenrun[1] + (FIXED_1/200); | |
106 | os_data->avenrun[2] = avenrun[2] + (FIXED_1/200); | |
107 | ||
108 | j = 0; | |
109 | for_each_online_cpu(i) { | |
110 | os_data->os_cpu[j].per_cpu_user = | |
7fb1327e | 111 | nsecs_to_jiffies(kcpustat_cpu(i).cpustat[CPUTIME_USER]); |
1da177e4 | 112 | os_data->os_cpu[j].per_cpu_nice = |
7fb1327e | 113 | nsecs_to_jiffies(kcpustat_cpu(i).cpustat[CPUTIME_NICE]); |
1da177e4 | 114 | os_data->os_cpu[j].per_cpu_system = |
7fb1327e | 115 | nsecs_to_jiffies(kcpustat_cpu(i).cpustat[CPUTIME_SYSTEM]); |
1da177e4 | 116 | os_data->os_cpu[j].per_cpu_idle = |
7fb1327e | 117 | nsecs_to_jiffies(kcpustat_cpu(i).cpustat[CPUTIME_IDLE]); |
1da177e4 | 118 | os_data->os_cpu[j].per_cpu_irq = |
7fb1327e | 119 | nsecs_to_jiffies(kcpustat_cpu(i).cpustat[CPUTIME_IRQ]); |
1da177e4 | 120 | os_data->os_cpu[j].per_cpu_softirq = |
7fb1327e | 121 | nsecs_to_jiffies(kcpustat_cpu(i).cpustat[CPUTIME_SOFTIRQ]); |
1da177e4 | 122 | os_data->os_cpu[j].per_cpu_iowait = |
7fb1327e | 123 | nsecs_to_jiffies(kcpustat_cpu(i).cpustat[CPUTIME_IOWAIT]); |
5b5dd21a | 124 | os_data->os_cpu[j].per_cpu_steal = |
7fb1327e | 125 | nsecs_to_jiffies(kcpustat_cpu(i).cpustat[CPUTIME_STEAL]); |
5b5dd21a | 126 | os_data->os_cpu[j].cpu_id = i; |
1da177e4 LT |
127 | j++; |
128 | } | |
129 | ||
5b5dd21a GS |
130 | os_data->nr_cpus = j; |
131 | ||
28ccce5f | 132 | new_size = struct_size(os_data, os_cpu, os_data->nr_cpus); |
5b5dd21a GS |
133 | if (ops.size != new_size) { |
134 | if (ops.active) { | |
135 | rc = appldata_diag(APPLDATA_RECORD_OS_ID, | |
136 | APPLDATA_START_INTERVAL_REC, | |
137 | (unsigned long) ops.data, new_size, | |
138 | ops.mod_lvl); | |
d3ae942d | 139 | if (rc != 0) |
e7534b0e GS |
140 | pr_err("Starting a new OS data collection " |
141 | "failed with rc=%d\n", rc); | |
5b5dd21a GS |
142 | |
143 | rc = appldata_diag(APPLDATA_RECORD_OS_ID, | |
144 | APPLDATA_STOP_REC, | |
145 | (unsigned long) ops.data, ops.size, | |
146 | ops.mod_lvl); | |
147 | if (rc != 0) | |
e7534b0e GS |
148 | pr_err("Stopping a faulty OS data " |
149 | "collection failed with rc=%d\n", rc); | |
5b5dd21a GS |
150 | } |
151 | ops.size = new_size; | |
152 | } | |
1aae0560 | 153 | os_data->timestamp = get_tod_clock(); |
1da177e4 | 154 | os_data->sync_count_2++; |
1da177e4 LT |
155 | } |
156 | ||
157 | ||
1da177e4 LT |
158 | /* |
159 | * appldata_os_init() | |
160 | * | |
161 | * init data, register ops | |
162 | */ | |
163 | static int __init appldata_os_init(void) | |
164 | { | |
5b5dd21a | 165 | int rc, max_size; |
1da177e4 | 166 | |
28ccce5f | 167 | max_size = struct_size(appldata_os_data, os_cpu, num_possible_cpus()); |
5b5dd21a | 168 | if (max_size > APPLDATA_MAX_REC_SIZE) { |
e7534b0e GS |
169 | pr_err("Maximum OS record size %i exceeds the maximum " |
170 | "record size %i\n", max_size, APPLDATA_MAX_REC_SIZE); | |
1da177e4 LT |
171 | rc = -ENOMEM; |
172 | goto out; | |
173 | } | |
1da177e4 | 174 | |
c2f0e8c8 | 175 | appldata_os_data = kzalloc(max_size, GFP_KERNEL | GFP_DMA); |
1da177e4 | 176 | if (appldata_os_data == NULL) { |
1da177e4 LT |
177 | rc = -ENOMEM; |
178 | goto out; | |
179 | } | |
1da177e4 LT |
180 | |
181 | appldata_os_data->per_cpu_size = sizeof(struct appldata_os_per_cpu); | |
182 | appldata_os_data->cpu_offset = offsetof(struct appldata_os_data, | |
183 | os_cpu); | |
1da177e4 LT |
184 | |
185 | ops.data = appldata_os_data; | |
5b5dd21a | 186 | ops.callback = &appldata_get_os_data; |
1da177e4 | 187 | rc = appldata_register_ops(&ops); |
d3ae942d | 188 | if (rc != 0) |
1da177e4 | 189 | kfree(appldata_os_data); |
1da177e4 LT |
190 | out: |
191 | return rc; | |
192 | } | |
193 | ||
194 | /* | |
195 | * appldata_os_exit() | |
196 | * | |
197 | * unregister ops | |
198 | */ | |
199 | static void __exit appldata_os_exit(void) | |
200 | { | |
201 | appldata_unregister_ops(&ops); | |
202 | kfree(appldata_os_data); | |
1da177e4 LT |
203 | } |
204 | ||
205 | ||
206 | module_init(appldata_os_init); | |
207 | module_exit(appldata_os_exit); | |
208 | ||
209 | MODULE_LICENSE("GPL"); | |
210 | MODULE_AUTHOR("Gerald Schaefer"); | |
211 | MODULE_DESCRIPTION("Linux-VM Monitor Stream, OS statistics"); |