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