1
2
3
4
5
6
7
8
9
10
11
12#define KMSG_COMPONENT "appldata"
13#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
14
15#include <linux/module.h>
16#include <linux/init.h>
17#include <linux/slab.h>
18#include <linux/errno.h>
19#include <linux/kernel_stat.h>
20#include <linux/netdevice.h>
21#include <linux/sched.h>
22#include <asm/appldata.h>
23#include <asm/smp.h>
24
25#include "appldata.h"
26
27
28#define LOAD_INT(x) ((x) >> FSHIFT)
29#define LOAD_FRAC(x) LOAD_INT(((x) & (FIXED_1-1)) * 100)
30
31
32
33
34
35
36
37
38
39
40
41
42
43struct appldata_os_per_cpu {
44 u32 per_cpu_user;
45 u32 per_cpu_nice;
46 u32 per_cpu_system;
47 u32 per_cpu_idle;
48
49
50 u32 per_cpu_irq;
51 u32 per_cpu_softirq;
52 u32 per_cpu_iowait;
53
54
55 u32 per_cpu_steal;
56 u32 cpu_id;
57} __attribute__((packed));
58
59struct appldata_os_data {
60 u64 timestamp;
61 u32 sync_count_1;
62 u32 sync_count_2;
63
64
65
66
67 u32 nr_cpus;
68 u32 per_cpu_size;
69 u32 cpu_offset;
70
71 u32 nr_running;
72 u32 nr_threads;
73 u32 avenrun[3];
74
75
76
77 u32 nr_iowait;
78
79
80
81 struct appldata_os_per_cpu os_cpu[0];
82} __attribute__((packed));
83
84static struct appldata_os_data *appldata_os_data;
85
86static struct appldata_ops ops = {
87 .name = "os",
88 .record_nr = APPLDATA_RECORD_OS_ID,
89 .owner = THIS_MODULE,
90 .mod_lvl = {0xF0, 0xF1},
91};
92
93
94
95
96
97
98
99static void appldata_get_os_data(void *data)
100{
101 int i, j, rc;
102 struct appldata_os_data *os_data;
103 unsigned int new_size;
104
105 os_data = data;
106 os_data->sync_count_1++;
107
108 os_data->nr_threads = nr_threads;
109 os_data->nr_running = nr_running();
110 os_data->nr_iowait = nr_iowait();
111 os_data->avenrun[0] = avenrun[0] + (FIXED_1/200);
112 os_data->avenrun[1] = avenrun[1] + (FIXED_1/200);
113 os_data->avenrun[2] = avenrun[2] + (FIXED_1/200);
114
115 j = 0;
116 for_each_online_cpu(i) {
117 os_data->os_cpu[j].per_cpu_user =
118 cputime_to_jiffies(kstat_cpu(i).cpustat.user);
119 os_data->os_cpu[j].per_cpu_nice =
120 cputime_to_jiffies(kstat_cpu(i).cpustat.nice);
121 os_data->os_cpu[j].per_cpu_system =
122 cputime_to_jiffies(kstat_cpu(i).cpustat.system);
123 os_data->os_cpu[j].per_cpu_idle =
124 cputime_to_jiffies(kstat_cpu(i).cpustat.idle);
125 os_data->os_cpu[j].per_cpu_irq =
126 cputime_to_jiffies(kstat_cpu(i).cpustat.irq);
127 os_data->os_cpu[j].per_cpu_softirq =
128 cputime_to_jiffies(kstat_cpu(i).cpustat.softirq);
129 os_data->os_cpu[j].per_cpu_iowait =
130 cputime_to_jiffies(kstat_cpu(i).cpustat.iowait);
131 os_data->os_cpu[j].per_cpu_steal =
132 cputime_to_jiffies(kstat_cpu(i).cpustat.steal);
133 os_data->os_cpu[j].cpu_id = i;
134 j++;
135 }
136
137 os_data->nr_cpus = j;
138
139 new_size = sizeof(struct appldata_os_data) +
140 (os_data->nr_cpus * sizeof(struct appldata_os_per_cpu));
141 if (ops.size != new_size) {
142 if (ops.active) {
143 rc = appldata_diag(APPLDATA_RECORD_OS_ID,
144 APPLDATA_START_INTERVAL_REC,
145 (unsigned long) ops.data, new_size,
146 ops.mod_lvl);
147 if (rc != 0)
148 pr_err("Starting a new OS data collection "
149 "failed with rc=%d\n", rc);
150
151 rc = appldata_diag(APPLDATA_RECORD_OS_ID,
152 APPLDATA_STOP_REC,
153 (unsigned long) ops.data, ops.size,
154 ops.mod_lvl);
155 if (rc != 0)
156 pr_err("Stopping a faulty OS data "
157 "collection failed with rc=%d\n", rc);
158 }
159 ops.size = new_size;
160 }
161 os_data->timestamp = get_clock();
162 os_data->sync_count_2++;
163}
164
165
166
167
168
169
170
171static int __init appldata_os_init(void)
172{
173 int rc, max_size;
174
175 max_size = sizeof(struct appldata_os_data) +
176 (NR_CPUS * sizeof(struct appldata_os_per_cpu));
177 if (max_size > APPLDATA_MAX_REC_SIZE) {
178 pr_err("Maximum OS record size %i exceeds the maximum "
179 "record size %i\n", max_size, APPLDATA_MAX_REC_SIZE);
180 rc = -ENOMEM;
181 goto out;
182 }
183
184 appldata_os_data = kzalloc(max_size, GFP_KERNEL | GFP_DMA);
185 if (appldata_os_data == NULL) {
186 rc = -ENOMEM;
187 goto out;
188 }
189
190 appldata_os_data->per_cpu_size = sizeof(struct appldata_os_per_cpu);
191 appldata_os_data->cpu_offset = offsetof(struct appldata_os_data,
192 os_cpu);
193
194 ops.data = appldata_os_data;
195 ops.callback = &appldata_get_os_data;
196 rc = appldata_register_ops(&ops);
197 if (rc != 0)
198 kfree(appldata_os_data);
199out:
200 return rc;
201}
202
203
204
205
206
207
208static void __exit appldata_os_exit(void)
209{
210 appldata_unregister_ops(&ops);
211 kfree(appldata_os_data);
212}
213
214
215module_init(appldata_os_init);
216module_exit(appldata_os_exit);
217
218MODULE_LICENSE("GPL");
219MODULE_AUTHOR("Gerald Schaefer");
220MODULE_DESCRIPTION("Linux-VM Monitor Stream, OS statistics");
221