blob: 54f375627532a107e05056b4e9896479bf3bb27e [file] [log] [blame]
Greg Kroah-Hartman0caa8cd2017-11-24 15:00:37 +01001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * Data gathering module for Linux-VM Monitor Stream, Stage 1.
4 * Collects misc. OS related data (CPU utilization, running processes).
5 *
Heiko Carstensa53c8fa2012-07-20 11:15:04 +02006 * Copyright IBM Corp. 2003, 2006
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 *
Gerald Schaefer5b5dd212006-06-29 15:08:35 +02008 * Author: Gerald Schaefer <gerald.schaefer@de.ibm.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 */
10
Gerald Schaefere7534b02008-12-25 13:39:41 +010011#define KMSG_COMPONENT "appldata"
12#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
13
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#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>
Ingo Molnar4f177222017-02-08 08:45:17 +010021#include <linux/sched/loadavg.h>
Ingo Molnar03441a32017-02-08 18:51:35 +010022#include <linux/sched/stat.h>
Gerald Schaefer1f38d612006-09-20 15:59:26 +020023#include <asm/appldata.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <asm/smp.h>
25
26#include "appldata.h"
27
Linus Torvalds1da177e2005-04-16 15:20:36 -070028/*
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.
35 *
36 * The record layout is documented in the Linux for zSeries Device Drivers
37 * book:
38 * http://oss.software.ibm.com/developerworks/opensource/linux390/index.shtml
39 */
40struct appldata_os_per_cpu {
41 u32 per_cpu_user; /* timer ticks spent in user mode */
42 u32 per_cpu_nice; /* ... spent with modified priority */
43 u32 per_cpu_system; /* ... spent in kernel mode */
44 u32 per_cpu_idle; /* ... spent in idle mode */
45
Gerald Schaefer5b5dd212006-06-29 15:08:35 +020046 /* New in 2.6 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 u32 per_cpu_irq; /* ... spent in interrupts */
48 u32 per_cpu_softirq; /* ... spent in softirqs */
49 u32 per_cpu_iowait; /* ... spent while waiting for I/O */
Gerald Schaefer5b5dd212006-06-29 15:08:35 +020050
51 /* New in modification level 01 */
52 u32 per_cpu_steal; /* ... stolen by hypervisor */
53 u32 cpu_id; /* number of this CPU */
Gerald Schaeferf26d5832005-06-04 15:43:33 -070054} __attribute__((packed));
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
56struct appldata_os_data {
57 u64 timestamp;
58 u32 sync_count_1; /* after VM collected the record data, */
59 u32 sync_count_2; /* sync_count_1 and sync_count_2 should be the
60 same. If not, the record has been updated on
61 the Linux side while VM was collecting the
62 (possibly corrupt) data */
63
64 u32 nr_cpus; /* number of (virtual) CPUs */
65 u32 per_cpu_size; /* size of the per-cpu data struct */
66 u32 cpu_offset; /* offset of the first per-cpu data struct */
67
68 u32 nr_running; /* number of runnable threads */
69 u32 nr_threads; /* number of threads */
70 u32 avenrun[3]; /* average nr. of running processes during */
71 /* the last 1, 5 and 15 minutes */
72
Gerald Schaefer5b5dd212006-06-29 15:08:35 +020073 /* New in 2.6 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 u32 nr_iowait; /* number of blocked threads
75 (waiting for I/O) */
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
77 /* per cpu data */
78 struct appldata_os_per_cpu os_cpu[0];
Gerald Schaeferf26d5832005-06-04 15:43:33 -070079} __attribute__((packed));
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
81static struct appldata_os_data *appldata_os_data;
82
Gerald Schaefer5b5dd212006-06-29 15:08:35 +020083static struct appldata_ops ops = {
Gerald Schaefer5b5dd212006-06-29 15:08:35 +020084 .name = "os",
85 .record_nr = APPLDATA_RECORD_OS_ID,
86 .owner = THIS_MODULE,
87 .mod_lvl = {0xF0, 0xF1}, /* EBCDIC "01" */
88};
89
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
Linus Torvalds1da177e2005-04-16 15:20:36 -070091/*
92 * appldata_get_os_data()
93 *
94 * gather OS data
95 */
96static void appldata_get_os_data(void *data)
97{
Gerald Schaefer5b5dd212006-06-29 15:08:35 +020098 int i, j, rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 struct appldata_os_data *os_data;
Gerald Schaefer5b5dd212006-06-29 15:08:35 +0200100 unsigned int new_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
102 os_data = data;
103 os_data->sync_count_1++;
104
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 os_data->nr_threads = nr_threads;
106 os_data->nr_running = nr_running();
107 os_data->nr_iowait = nr_iowait();
108 os_data->avenrun[0] = avenrun[0] + (FIXED_1/200);
109 os_data->avenrun[1] = avenrun[1] + (FIXED_1/200);
110 os_data->avenrun[2] = avenrun[2] + (FIXED_1/200);
111
112 j = 0;
113 for_each_online_cpu(i) {
114 os_data->os_cpu[j].per_cpu_user =
Frederic Weisbecker7fb13272017-01-31 04:09:19 +0100115 nsecs_to_jiffies(kcpustat_cpu(i).cpustat[CPUTIME_USER]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 os_data->os_cpu[j].per_cpu_nice =
Frederic Weisbecker7fb13272017-01-31 04:09:19 +0100117 nsecs_to_jiffies(kcpustat_cpu(i).cpustat[CPUTIME_NICE]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 os_data->os_cpu[j].per_cpu_system =
Frederic Weisbecker7fb13272017-01-31 04:09:19 +0100119 nsecs_to_jiffies(kcpustat_cpu(i).cpustat[CPUTIME_SYSTEM]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 os_data->os_cpu[j].per_cpu_idle =
Frederic Weisbecker7fb13272017-01-31 04:09:19 +0100121 nsecs_to_jiffies(kcpustat_cpu(i).cpustat[CPUTIME_IDLE]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 os_data->os_cpu[j].per_cpu_irq =
Frederic Weisbecker7fb13272017-01-31 04:09:19 +0100123 nsecs_to_jiffies(kcpustat_cpu(i).cpustat[CPUTIME_IRQ]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 os_data->os_cpu[j].per_cpu_softirq =
Frederic Weisbecker7fb13272017-01-31 04:09:19 +0100125 nsecs_to_jiffies(kcpustat_cpu(i).cpustat[CPUTIME_SOFTIRQ]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 os_data->os_cpu[j].per_cpu_iowait =
Frederic Weisbecker7fb13272017-01-31 04:09:19 +0100127 nsecs_to_jiffies(kcpustat_cpu(i).cpustat[CPUTIME_IOWAIT]);
Gerald Schaefer5b5dd212006-06-29 15:08:35 +0200128 os_data->os_cpu[j].per_cpu_steal =
Frederic Weisbecker7fb13272017-01-31 04:09:19 +0100129 nsecs_to_jiffies(kcpustat_cpu(i).cpustat[CPUTIME_STEAL]);
Gerald Schaefer5b5dd212006-06-29 15:08:35 +0200130 os_data->os_cpu[j].cpu_id = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 j++;
132 }
133
Gerald Schaefer5b5dd212006-06-29 15:08:35 +0200134 os_data->nr_cpus = j;
135
136 new_size = sizeof(struct appldata_os_data) +
137 (os_data->nr_cpus * sizeof(struct appldata_os_per_cpu));
138 if (ops.size != new_size) {
139 if (ops.active) {
140 rc = appldata_diag(APPLDATA_RECORD_OS_ID,
141 APPLDATA_START_INTERVAL_REC,
142 (unsigned long) ops.data, new_size,
143 ops.mod_lvl);
Gerald Schaeferd3ae9422008-07-14 09:59:34 +0200144 if (rc != 0)
Gerald Schaefere7534b02008-12-25 13:39:41 +0100145 pr_err("Starting a new OS data collection "
146 "failed with rc=%d\n", rc);
Gerald Schaefer5b5dd212006-06-29 15:08:35 +0200147
148 rc = appldata_diag(APPLDATA_RECORD_OS_ID,
149 APPLDATA_STOP_REC,
150 (unsigned long) ops.data, ops.size,
151 ops.mod_lvl);
152 if (rc != 0)
Gerald Schaefere7534b02008-12-25 13:39:41 +0100153 pr_err("Stopping a faulty OS data "
154 "collection failed with rc=%d\n", rc);
Gerald Schaefer5b5dd212006-06-29 15:08:35 +0200155 }
156 ops.size = new_size;
157 }
Heiko Carstens1aae0562013-01-30 09:49:40 +0100158 os_data->timestamp = get_tod_clock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 os_data->sync_count_2++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160}
161
162
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163/*
164 * appldata_os_init()
165 *
166 * init data, register ops
167 */
168static int __init appldata_os_init(void)
169{
Gerald Schaefer5b5dd212006-06-29 15:08:35 +0200170 int rc, max_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171
Gerald Schaefer5b5dd212006-06-29 15:08:35 +0200172 max_size = sizeof(struct appldata_os_data) +
Gerald Schaefer69670372014-02-28 17:35:52 +0100173 (num_possible_cpus() * sizeof(struct appldata_os_per_cpu));
Gerald Schaefer5b5dd212006-06-29 15:08:35 +0200174 if (max_size > APPLDATA_MAX_REC_SIZE) {
Gerald Schaefere7534b02008-12-25 13:39:41 +0100175 pr_err("Maximum OS record size %i exceeds the maximum "
176 "record size %i\n", max_size, APPLDATA_MAX_REC_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 rc = -ENOMEM;
178 goto out;
179 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180
Heiko Carstensc2f0e8c2010-06-08 18:58:09 +0200181 appldata_os_data = kzalloc(max_size, GFP_KERNEL | GFP_DMA);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 if (appldata_os_data == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 rc = -ENOMEM;
184 goto out;
185 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186
187 appldata_os_data->per_cpu_size = sizeof(struct appldata_os_per_cpu);
188 appldata_os_data->cpu_offset = offsetof(struct appldata_os_data,
189 os_cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
191 ops.data = appldata_os_data;
Gerald Schaefer5b5dd212006-06-29 15:08:35 +0200192 ops.callback = &appldata_get_os_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 rc = appldata_register_ops(&ops);
Gerald Schaeferd3ae9422008-07-14 09:59:34 +0200194 if (rc != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 kfree(appldata_os_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196out:
197 return rc;
198}
199
200/*
201 * appldata_os_exit()
202 *
203 * unregister ops
204 */
205static void __exit appldata_os_exit(void)
206{
207 appldata_unregister_ops(&ops);
208 kfree(appldata_os_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209}
210
211
212module_init(appldata_os_init);
213module_exit(appldata_os_exit);
214
215MODULE_LICENSE("GPL");
216MODULE_AUTHOR("Gerald Schaefer");
217MODULE_DESCRIPTION("Linux-VM Monitor Stream, OS statistics");