blob: 08b9e942a262eda28393f5d982f4f8d60093ca4b [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * Data gathering module for Linux-VM Monitor Stream, Stage 1.
3 * Collects misc. OS related data (CPU utilization, running processes).
4 *
Heiko Carstensa53c8fa2012-07-20 11:15:04 +02005 * Copyright IBM Corp. 2003, 2006
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
Gerald Schaefer5b5dd212006-06-29 15:08:35 +02007 * Author: Gerald Schaefer <gerald.schaefer@de.ibm.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 */
9
Gerald Schaefere7534b02008-12-25 13:39:41 +010010#define KMSG_COMPONENT "appldata"
11#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
12
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#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>
Gerald Schaefer1f38d612006-09-20 15:59:26 +020020#include <asm/appldata.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <asm/smp.h>
22
23#include "appldata.h"
24
25
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#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 */
41struct 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 Schaefer5b5dd212006-06-29 15:08:35 +020047 /* New in 2.6 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 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 Schaefer5b5dd212006-06-29 15:08:35 +020051
52 /* New in modification level 01 */
53 u32 per_cpu_steal; /* ... stolen by hypervisor */
54 u32 cpu_id; /* number of this CPU */
Gerald Schaeferf26d5832005-06-04 15:43:33 -070055} __attribute__((packed));
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
57struct 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 Schaefer5b5dd212006-06-29 15:08:35 +020074 /* New in 2.6 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 u32 nr_iowait; /* number of blocked threads
76 (waiting for I/O) */
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
78 /* per cpu data */
79 struct appldata_os_per_cpu os_cpu[0];
Gerald Schaeferf26d5832005-06-04 15:43:33 -070080} __attribute__((packed));
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
82static struct appldata_os_data *appldata_os_data;
83
Gerald Schaefer5b5dd212006-06-29 15:08:35 +020084static struct appldata_ops ops = {
Gerald Schaefer5b5dd212006-06-29 15:08:35 +020085 .name = "os",
86 .record_nr = APPLDATA_RECORD_OS_ID,
87 .owner = THIS_MODULE,
88 .mod_lvl = {0xF0, 0xF1}, /* EBCDIC "01" */
89};
90
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
Linus Torvalds1da177e2005-04-16 15:20:36 -070092/*
93 * appldata_get_os_data()
94 *
95 * gather OS data
96 */
97static void appldata_get_os_data(void *data)
98{
Gerald Schaefer5b5dd212006-06-29 15:08:35 +020099 int i, j, rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 struct appldata_os_data *os_data;
Gerald Schaefer5b5dd212006-06-29 15:08:35 +0200101 unsigned int new_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
103 os_data = data;
104 os_data->sync_count_1++;
105
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 os_data->nr_threads = nr_threads;
107 os_data->nr_running = nr_running();
108 os_data->nr_iowait = nr_iowait();
109 os_data->avenrun[0] = avenrun[0] + (FIXED_1/200);
110 os_data->avenrun[1] = avenrun[1] + (FIXED_1/200);
111 os_data->avenrun[2] = avenrun[2] + (FIXED_1/200);
112
113 j = 0;
114 for_each_online_cpu(i) {
115 os_data->os_cpu[j].per_cpu_user =
Frederic Weisbeckerdbf9a052017-01-31 04:09:19 +0100116 nsecs_to_jiffies(kcpustat_cpu(i).cpustat[CPUTIME_USER]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 os_data->os_cpu[j].per_cpu_nice =
Frederic Weisbeckerdbf9a052017-01-31 04:09:19 +0100118 nsecs_to_jiffies(kcpustat_cpu(i).cpustat[CPUTIME_NICE]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 os_data->os_cpu[j].per_cpu_system =
Frederic Weisbeckerdbf9a052017-01-31 04:09:19 +0100120 nsecs_to_jiffies(kcpustat_cpu(i).cpustat[CPUTIME_SYSTEM]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 os_data->os_cpu[j].per_cpu_idle =
Frederic Weisbeckerdbf9a052017-01-31 04:09:19 +0100122 nsecs_to_jiffies(kcpustat_cpu(i).cpustat[CPUTIME_IDLE]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 os_data->os_cpu[j].per_cpu_irq =
Frederic Weisbeckerdbf9a052017-01-31 04:09:19 +0100124 nsecs_to_jiffies(kcpustat_cpu(i).cpustat[CPUTIME_IRQ]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 os_data->os_cpu[j].per_cpu_softirq =
Frederic Weisbeckerdbf9a052017-01-31 04:09:19 +0100126 nsecs_to_jiffies(kcpustat_cpu(i).cpustat[CPUTIME_SOFTIRQ]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 os_data->os_cpu[j].per_cpu_iowait =
Frederic Weisbeckerdbf9a052017-01-31 04:09:19 +0100128 nsecs_to_jiffies(kcpustat_cpu(i).cpustat[CPUTIME_IOWAIT]);
Gerald Schaefer5b5dd212006-06-29 15:08:35 +0200129 os_data->os_cpu[j].per_cpu_steal =
Frederic Weisbeckerdbf9a052017-01-31 04:09:19 +0100130 nsecs_to_jiffies(kcpustat_cpu(i).cpustat[CPUTIME_STEAL]);
Gerald Schaefer5b5dd212006-06-29 15:08:35 +0200131 os_data->os_cpu[j].cpu_id = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 j++;
133 }
134
Gerald Schaefer5b5dd212006-06-29 15:08:35 +0200135 os_data->nr_cpus = j;
136
137 new_size = sizeof(struct appldata_os_data) +
138 (os_data->nr_cpus * sizeof(struct appldata_os_per_cpu));
139 if (ops.size != new_size) {
140 if (ops.active) {
141 rc = appldata_diag(APPLDATA_RECORD_OS_ID,
142 APPLDATA_START_INTERVAL_REC,
143 (unsigned long) ops.data, new_size,
144 ops.mod_lvl);
Gerald Schaeferd3ae9422008-07-14 09:59:34 +0200145 if (rc != 0)
Gerald Schaefere7534b02008-12-25 13:39:41 +0100146 pr_err("Starting a new OS data collection "
147 "failed with rc=%d\n", rc);
Gerald Schaefer5b5dd212006-06-29 15:08:35 +0200148
149 rc = appldata_diag(APPLDATA_RECORD_OS_ID,
150 APPLDATA_STOP_REC,
151 (unsigned long) ops.data, ops.size,
152 ops.mod_lvl);
153 if (rc != 0)
Gerald Schaefere7534b02008-12-25 13:39:41 +0100154 pr_err("Stopping a faulty OS data "
155 "collection failed with rc=%d\n", rc);
Gerald Schaefer5b5dd212006-06-29 15:08:35 +0200156 }
157 ops.size = new_size;
158 }
Heiko Carstens1aae0562013-01-30 09:49:40 +0100159 os_data->timestamp = get_tod_clock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 os_data->sync_count_2++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161}
162
163
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164/*
165 * appldata_os_init()
166 *
167 * init data, register ops
168 */
169static int __init appldata_os_init(void)
170{
Gerald Schaefer5b5dd212006-06-29 15:08:35 +0200171 int rc, max_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
Gerald Schaefer5b5dd212006-06-29 15:08:35 +0200173 max_size = sizeof(struct appldata_os_data) +
Gerald Schaefer69670372014-02-28 17:35:52 +0100174 (num_possible_cpus() * sizeof(struct appldata_os_per_cpu));
Gerald Schaefer5b5dd212006-06-29 15:08:35 +0200175 if (max_size > APPLDATA_MAX_REC_SIZE) {
Gerald Schaefere7534b02008-12-25 13:39:41 +0100176 pr_err("Maximum OS record size %i exceeds the maximum "
177 "record size %i\n", max_size, APPLDATA_MAX_REC_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 rc = -ENOMEM;
179 goto out;
180 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
Heiko Carstensc2f0e8c2010-06-08 18:58:09 +0200182 appldata_os_data = kzalloc(max_size, GFP_KERNEL | GFP_DMA);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 if (appldata_os_data == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 rc = -ENOMEM;
185 goto out;
186 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
188 appldata_os_data->per_cpu_size = sizeof(struct appldata_os_per_cpu);
189 appldata_os_data->cpu_offset = offsetof(struct appldata_os_data,
190 os_cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
192 ops.data = appldata_os_data;
Gerald Schaefer5b5dd212006-06-29 15:08:35 +0200193 ops.callback = &appldata_get_os_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 rc = appldata_register_ops(&ops);
Gerald Schaeferd3ae9422008-07-14 09:59:34 +0200195 if (rc != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 kfree(appldata_os_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197out:
198 return rc;
199}
200
201/*
202 * appldata_os_exit()
203 *
204 * unregister ops
205 */
206static void __exit appldata_os_exit(void)
207{
208 appldata_unregister_ops(&ops);
209 kfree(appldata_os_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210}
211
212
213module_init(appldata_os_init);
214module_exit(appldata_os_exit);
215
216MODULE_LICENSE("GPL");
217MODULE_AUTHOR("Gerald Schaefer");
218MODULE_DESCRIPTION("Linux-VM Monitor Stream, OS statistics");