xref: /linux/arch/s390/appldata/appldata_os.c (revision 36ca1195ad7f760a6af3814cb002bd3a3d4b4db1)
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  *
7  * Copyright (C) 2003 IBM Corporation, IBM Deutschland Entwicklung GmbH.
8  *
9  * Author: Gerald Schaefer <geraldsc@de.ibm.com>
10  */
11 
12 #include <linux/config.h>
13 #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>
20 #include <asm/smp.h>
21 
22 #include "appldata.h"
23 
24 
25 #define MY_PRINT_NAME	"appldata_os"		/* for debug messages, etc. */
26 #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  */
41 struct 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 
47 // New in 2.6 -->
48 	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  */
51 // <-- New in 2.6
52 } __attribute__((packed));
53 
54 struct appldata_os_data {
55 	u64 timestamp;
56 	u32 sync_count_1;	/* after VM collected the record data, */
57 	u32 sync_count_2;	/* sync_count_1 and sync_count_2 should be the
58 				   same. If not, the record has been updated on
59 				   the Linux side while VM was collecting the
60 				   (possibly corrupt) data */
61 
62 	u32 nr_cpus;		/* number of (virtual) CPUs        */
63 	u32 per_cpu_size;	/* size of the per-cpu data struct */
64 	u32 cpu_offset;		/* offset of the first per-cpu data struct */
65 
66 	u32 nr_running;		/* number of runnable threads      */
67 	u32 nr_threads;		/* number of threads               */
68 	u32 avenrun[3];		/* average nr. of running processes during */
69 				/* the last 1, 5 and 15 minutes */
70 
71 // New in 2.6 -->
72 	u32 nr_iowait;		/* number of blocked threads
73 				   (waiting for I/O)               */
74 // <-- New in 2.6
75 
76 	/* per cpu data */
77 	struct appldata_os_per_cpu os_cpu[0];
78 } __attribute__((packed));
79 
80 static struct appldata_os_data *appldata_os_data;
81 
82 
83 static inline void appldata_print_debug(struct appldata_os_data *os_data)
84 {
85 	int a0, a1, a2, i;
86 
87 	P_DEBUG("--- OS - RECORD ---\n");
88 	P_DEBUG("nr_threads   = %u\n", os_data->nr_threads);
89 	P_DEBUG("nr_running   = %u\n", os_data->nr_running);
90 	P_DEBUG("nr_iowait    = %u\n", os_data->nr_iowait);
91 	P_DEBUG("avenrun(int) = %8x / %8x / %8x\n", os_data->avenrun[0],
92 		os_data->avenrun[1], os_data->avenrun[2]);
93 	a0 = os_data->avenrun[0];
94 	a1 = os_data->avenrun[1];
95 	a2 = os_data->avenrun[2];
96 	P_DEBUG("avenrun(float) = %d.%02d / %d.%02d / %d.%02d\n",
97 		LOAD_INT(a0), LOAD_FRAC(a0), LOAD_INT(a1), LOAD_FRAC(a1),
98 		LOAD_INT(a2), LOAD_FRAC(a2));
99 
100 	P_DEBUG("nr_cpus = %u\n", os_data->nr_cpus);
101 	for (i = 0; i < os_data->nr_cpus; i++) {
102 		P_DEBUG("cpu%u : user = %u, nice = %u, system = %u, "
103 			"idle = %u, irq = %u, softirq = %u, iowait = %u\n",
104 				i,
105 				os_data->os_cpu[i].per_cpu_user,
106 				os_data->os_cpu[i].per_cpu_nice,
107 				os_data->os_cpu[i].per_cpu_system,
108 				os_data->os_cpu[i].per_cpu_idle,
109 				os_data->os_cpu[i].per_cpu_irq,
110 				os_data->os_cpu[i].per_cpu_softirq,
111 				os_data->os_cpu[i].per_cpu_iowait);
112 	}
113 
114 	P_DEBUG("sync_count_1 = %u\n", os_data->sync_count_1);
115 	P_DEBUG("sync_count_2 = %u\n", os_data->sync_count_2);
116 	P_DEBUG("timestamp    = %lX\n", os_data->timestamp);
117 }
118 
119 /*
120  * appldata_get_os_data()
121  *
122  * gather OS data
123  */
124 static void appldata_get_os_data(void *data)
125 {
126 	int i, j;
127 	struct appldata_os_data *os_data;
128 
129 	os_data = data;
130 	os_data->sync_count_1++;
131 
132 	os_data->nr_cpus = num_online_cpus();
133 
134 	os_data->nr_threads = nr_threads;
135 	os_data->nr_running = nr_running();
136 	os_data->nr_iowait  = nr_iowait();
137 	os_data->avenrun[0] = avenrun[0] + (FIXED_1/200);
138 	os_data->avenrun[1] = avenrun[1] + (FIXED_1/200);
139 	os_data->avenrun[2] = avenrun[2] + (FIXED_1/200);
140 
141 	j = 0;
142 	for_each_online_cpu(i) {
143 		os_data->os_cpu[j].per_cpu_user =
144 					kstat_cpu(i).cpustat.user;
145 		os_data->os_cpu[j].per_cpu_nice =
146 					kstat_cpu(i).cpustat.nice;
147 		os_data->os_cpu[j].per_cpu_system =
148 					kstat_cpu(i).cpustat.system;
149 		os_data->os_cpu[j].per_cpu_idle =
150 					kstat_cpu(i).cpustat.idle;
151 		os_data->os_cpu[j].per_cpu_irq =
152 					kstat_cpu(i).cpustat.irq;
153 		os_data->os_cpu[j].per_cpu_softirq =
154 					kstat_cpu(i).cpustat.softirq;
155 		os_data->os_cpu[j].per_cpu_iowait =
156 					kstat_cpu(i).cpustat.iowait;
157 		j++;
158 	}
159 
160 	os_data->timestamp = get_clock();
161 	os_data->sync_count_2++;
162 #ifdef APPLDATA_DEBUG
163 	appldata_print_debug(os_data);
164 #endif
165 }
166 
167 
168 static struct appldata_ops ops = {
169 	.ctl_nr    = CTL_APPLDATA_OS,
170 	.name	   = "os",
171 	.record_nr = APPLDATA_RECORD_OS_ID,
172 	.callback  = &appldata_get_os_data,
173 	.owner     = THIS_MODULE,
174 };
175 
176 
177 /*
178  * appldata_os_init()
179  *
180  * init data, register ops
181  */
182 static int __init appldata_os_init(void)
183 {
184 	int rc, size;
185 
186 	size = sizeof(struct appldata_os_data) +
187 		(NR_CPUS * sizeof(struct appldata_os_per_cpu));
188 	if (size > APPLDATA_MAX_REC_SIZE) {
189 		P_ERROR("Size of record = %i, bigger than maximum (%i)!\n",
190 			size, APPLDATA_MAX_REC_SIZE);
191 		rc = -ENOMEM;
192 		goto out;
193 	}
194 	P_DEBUG("sizeof(os) = %i, sizeof(os_cpu) = %lu\n", size,
195 		sizeof(struct appldata_os_per_cpu));
196 
197 	appldata_os_data = kmalloc(size, GFP_DMA);
198 	if (appldata_os_data == NULL) {
199 		P_ERROR("No memory for %s!\n", ops.name);
200 		rc = -ENOMEM;
201 		goto out;
202 	}
203 	memset(appldata_os_data, 0, size);
204 
205 	appldata_os_data->per_cpu_size = sizeof(struct appldata_os_per_cpu);
206 	appldata_os_data->cpu_offset   = offsetof(struct appldata_os_data,
207 							os_cpu);
208 	P_DEBUG("cpu offset = %u\n", appldata_os_data->cpu_offset);
209 
210 	ops.data = appldata_os_data;
211 	ops.size = size;
212 	rc = appldata_register_ops(&ops);
213 	if (rc != 0) {
214 		P_ERROR("Error registering ops, rc = %i\n", rc);
215 		kfree(appldata_os_data);
216 	} else {
217 		P_DEBUG("%s-ops registered!\n", ops.name);
218 	}
219 out:
220 	return rc;
221 }
222 
223 /*
224  * appldata_os_exit()
225  *
226  * unregister ops
227  */
228 static void __exit appldata_os_exit(void)
229 {
230 	appldata_unregister_ops(&ops);
231 	kfree(appldata_os_data);
232 	P_DEBUG("%s-ops unregistered!\n", ops.name);
233 }
234 
235 
236 module_init(appldata_os_init);
237 module_exit(appldata_os_exit);
238 
239 MODULE_LICENSE("GPL");
240 MODULE_AUTHOR("Gerald Schaefer");
241 MODULE_DESCRIPTION("Linux-VM Monitor Stream, OS statistics");
242