ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / arch / s390 / appldata / appldata_os.c
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 };
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 };
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 < NR_CPUS; i++) {
102                 if (!cpu_online(i)) continue;
103                 P_DEBUG("cpu%u : user = %u, nice = %u, system = %u, "
104                         "idle = %u, irq = %u, softirq = %u, iowait = %u\n",
105                                 i,
106                                 os_data->os_cpu[i].per_cpu_user,
107                                 os_data->os_cpu[i].per_cpu_nice,
108                                 os_data->os_cpu[i].per_cpu_system,
109                                 os_data->os_cpu[i].per_cpu_idle,
110                                 os_data->os_cpu[i].per_cpu_irq,
111                                 os_data->os_cpu[i].per_cpu_softirq,
112                                 os_data->os_cpu[i].per_cpu_iowait);
113         }
114
115         P_DEBUG("sync_count_1 = %u\n", os_data->sync_count_1);
116         P_DEBUG("sync_count_2 = %u\n", os_data->sync_count_2);
117         P_DEBUG("timestamp    = %lX\n", os_data->timestamp);
118 }
119
120 /*
121  * appldata_get_os_data()
122  *
123  * gather OS data
124  */
125 static void appldata_get_os_data(void *data)
126 {
127         int i;
128         struct appldata_os_data *os_data;
129
130         os_data = data;
131         os_data->sync_count_1++;
132
133         os_data->nr_cpus = num_online_cpus();
134
135         os_data->nr_threads = nr_threads;
136         os_data->nr_running = nr_running();
137         os_data->nr_iowait  = nr_iowait();
138         os_data->avenrun[0] = avenrun[0] + (FIXED_1/200);
139         os_data->avenrun[1] = avenrun[1] + (FIXED_1/200);
140         os_data->avenrun[2] = avenrun[2] + (FIXED_1/200);
141
142         for (i = 0; i < num_online_cpus(); i++) {
143                 os_data->os_cpu[i].per_cpu_user =
144                                         kstat_cpu(i).cpustat.user;
145                 os_data->os_cpu[i].per_cpu_nice =
146                                         kstat_cpu(i).cpustat.nice;
147                 os_data->os_cpu[i].per_cpu_system =
148                                         kstat_cpu(i).cpustat.system;
149                 os_data->os_cpu[i].per_cpu_idle =
150                                         kstat_cpu(i).cpustat.idle;
151                 os_data->os_cpu[i].per_cpu_irq =
152                                         kstat_cpu(i).cpustat.irq;
153                 os_data->os_cpu[i].per_cpu_softirq =
154                                         kstat_cpu(i).cpustat.softirq;
155                 os_data->os_cpu[i].per_cpu_iowait =
156                                         kstat_cpu(i).cpustat.iowait;
157         }
158
159         os_data->timestamp = get_clock();
160         os_data->sync_count_2++;
161 #ifdef APPLDATA_DEBUG
162         appldata_print_debug(os_data);
163 #endif
164 }
165
166
167 static struct appldata_ops ops = {
168         .ctl_nr    = CTL_APPLDATA_OS,
169         .name      = "os",
170         .record_nr = APPLDATA_RECORD_OS_ID,
171         .callback  = &appldata_get_os_data,
172         .owner     = THIS_MODULE,
173 };
174
175
176 /*
177  * appldata_os_init()
178  *
179  * init data, register ops
180  */
181 static int __init appldata_os_init(void)
182 {
183         int rc, size;
184
185         size = sizeof(struct appldata_os_data) +
186                 (NR_CPUS * sizeof(struct appldata_os_per_cpu));
187         if (size > APPLDATA_MAX_REC_SIZE) {
188                 P_ERROR("Size of record = %i, bigger than maximum (%i)!\n",
189                         size, APPLDATA_MAX_REC_SIZE);
190                 rc = -ENOMEM;
191                 goto out;
192         }
193         P_DEBUG("sizeof(os) = %i, sizeof(os_cpu) = %lu\n", size,
194                 sizeof(struct appldata_os_per_cpu));
195
196         appldata_os_data = kmalloc(size, GFP_DMA);
197         if (appldata_os_data == NULL) {
198                 P_ERROR("No memory for %s!\n", ops.name);
199                 rc = -ENOMEM;
200                 goto out;
201         }
202         memset(appldata_os_data, 0, size);
203
204         appldata_os_data->per_cpu_size = sizeof(struct appldata_os_per_cpu);
205         appldata_os_data->cpu_offset   = offsetof(struct appldata_os_data,
206                                                         os_cpu);
207         P_DEBUG("cpu offset = %u\n", appldata_os_data->cpu_offset);
208
209         ops.data = appldata_os_data;
210         ops.size = size;
211         rc = appldata_register_ops(&ops);
212         if (rc != 0) {
213                 P_ERROR("Error registering ops, rc = %i\n", rc);
214                 kfree(appldata_os_data);
215         } else {
216                 P_DEBUG("%s-ops registered!\n", ops.name);
217         }
218 out:
219         return rc;
220 }
221
222 /*
223  * appldata_os_exit()
224  *
225  * unregister ops
226  */
227 static void __exit appldata_os_exit(void)
228 {
229         appldata_unregister_ops(&ops);
230         kfree(appldata_os_data);
231         P_DEBUG("%s-ops unregistered!\n", ops.name);
232 }
233
234
235 module_init(appldata_os_init);
236 module_exit(appldata_os_exit);
237
238 MODULE_LICENSE("GPL");
239 MODULE_AUTHOR("Gerald Schaefer");
240 MODULE_DESCRIPTION("Linux-VM Monitor Stream, OS statistics");