File: create_data-ukwn.c

package info (click to toggle)
sysstat 12.5.2-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 19,224 kB
  • sloc: ansic: 46,886; sh: 1,299; xml: 1,287; tcl: 756; makefile: 728; perl: 257; python: 202
file content (454 lines) | stat: -rw-r--r-- 11,029 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
/*
 * create_data-ukwn.c: Create a binary data file with unknown activity type or unknown activity format
 * (C) 2019 by Sebastien GODARD (sysstat <at> orange.fr)
 *
 ***************************************************************************
 * This program is free software; you can redistribute it and/or modify it *
 * under the terms of the GNU General Public License as published  by  the *
 * Free Software Foundation; either version 2 of the License, or (at  your *
 * option) any later version.                                              *
 *                                                                         *
 * This program is distributed in the hope that it  will  be  useful,  but *
 * WITHOUT ANY WARRANTY; without the implied warranty  of  MERCHANTABILITY *
 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License *
 * for more details.                                                       *
 *                                                                         *
 * You should have received a copy of the GNU General Public License along *
 * with this program; if not, write to the Free Software Foundation, Inc., *
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335 USA              *
 ***************************************************************************
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>

#define UTSNAME_LEN		65
#define FILE_MAGIC_PADDING	48

#define FALSE	0
#define TRUE	1

/*
 *!!!!!!!!!!!!!!!!
 * Set it to FALSE to create a binary datafile with only unknown activity type and format.
 *!!!!!!!!!!!!!!!!
 */
#define INCLUDE_CPU_STAT	TRUE

int main(void)
{
	struct file_magic_12_1_7 {
		unsigned short sysstat_magic;
		unsigned short format_magic;
		unsigned char sysstat_version;
		unsigned char sysstat_patchlevel;
		unsigned char sysstat_sublevel;
		unsigned char sysstat_extraversion;
		unsigned int header_size;	/* Init me! */
		unsigned int upgraded;
		unsigned int hdr_types_nr[3];
		unsigned char pad[FILE_MAGIC_PADDING];
	};

	struct file_magic_12_1_7 f_magic = {
		.sysstat_magic = 0xd596,
		.format_magic = 0x2175,
		.sysstat_version = 12,
		.sysstat_patchlevel = 1,
		.sysstat_sublevel = 7,
		.sysstat_extraversion = 0,
		.upgraded = 0,
		.hdr_types_nr = {1, 1, 12}
	};

	struct file_header_12_1_7 {
		unsigned long long sa_ust_time;
		unsigned long sa_hz		__attribute__ ((aligned (8)));
		unsigned int sa_cpu_nr		__attribute__ ((aligned (8)));
		unsigned int sa_act_nr;
		int sa_year;
		unsigned int act_types_nr[3];
		unsigned int rec_types_nr[3];
		unsigned int act_size;	/* Init me! */
		unsigned int rec_size;	/* Init me! */
		unsigned int extra_next;
		unsigned char sa_day;
		unsigned char sa_month;
		char sa_sizeof_long;
		char sa_sysname[UTSNAME_LEN];
		char sa_nodename[UTSNAME_LEN];
		char sa_release[UTSNAME_LEN];
		char sa_machine[UTSNAME_LEN];
	};

	struct file_header_12_1_7 f_header = {
		.sa_ust_time = 1568533161,
		.sa_hz = 100,
		.sa_cpu_nr = 3,
		.sa_act_nr = (INCLUDE_CPU_STAT ? 3 : 2),
		.sa_year = 2019,
		.act_types_nr = {0, 0, 9},
		.rec_types_nr = {2, 0, 1},
		.extra_next = FALSE,
		.sa_day = 15,
		.sa_month = 9,
		.sa_sizeof_long = 8,
		.sa_sysname = "Linux",
		.sa_nodename = "localhost.localdomain",
		.sa_release = "5.0.16-100.fc28.x86_64",
		.sa_machine = "x86_64"
	};

	struct file_activity_12_1_7 {
		unsigned int id;
		unsigned int magic;
		int nr;
		int nr2;
		int has_nr;
		int size;	/* Init me! */
		unsigned int types_nr[3];
	};

	struct file_activity_12_1_7 f_activity_a_cpu = {
		.id = 1,	/* A_CPU */
		.magic = 0x8b,
		.nr = 3,
		.nr2 = 1,
		.has_nr = TRUE,
		.types_nr = {10, 0, 0}
	};

	struct file_activity_12_1_7 f_activity_a_pcsw = {
		.id = 2,	/* A_PCSW */
		.magic = 0xff,	/* Unknown activity format */
		.nr = 1,
		.nr2 = 1,
		.has_nr = FALSE,
		.types_nr = {0, 1, 0}
	};

	struct file_activity_12_1_7 f_activity_a_unknown = {
		.id = 0xff,	/* Unknown activity */
		.magic = 0x8a,	/* Unknown activity format */
		.nr = 2,
		.nr2 = 1,
		.has_nr = TRUE,
		.types_nr = {1, 1, 0}
	};

	struct record_header_12_1_7 {
		unsigned long long uptime_cs;
		unsigned long long ust_time;
		unsigned int extra_next;
		unsigned char record_type;
		unsigned char hour;
		unsigned char minute;
		unsigned char second;
	};

	struct record_header_12_1_7 r_header_1 = {
		.uptime_cs = 15000,
		.ust_time = 1568540000,
		.extra_next = FALSE,
		.record_type = 1,	/* R_STATS */
		.hour = 11,
		.minute = 5,
		.second = 58
	};

	struct record_header_12_1_7 r_header_2 = {
		.uptime_cs = 15200,
		.ust_time = 1568540200,
		.extra_next = FALSE,
		.record_type = 1,	/* R_STATS */
		.hour = 11,
		.minute = 6,
		.second = 1
	};

	struct stats_cpu_12_1_7 {
		unsigned long long cpu_user;
		unsigned long long cpu_nice;
		unsigned long long cpu_sys;
		unsigned long long cpu_idle;
		unsigned long long cpu_iowait;
		unsigned long long cpu_steal;
		unsigned long long cpu_hardirq;
		unsigned long long cpu_softirq;
		unsigned long long cpu_guest;
		unsigned long long cpu_guest_nice;
	};

	struct stats_cpu_12_1_7 s_cpu_0_1 = {
		.cpu_user = 1000,
		.cpu_nice = 0,
		.cpu_sys = 500,
		.cpu_idle = 0,
		.cpu_iowait = 0,
		.cpu_steal = 0,
		.cpu_hardirq = 0,
		.cpu_softirq = 0,
		.cpu_guest = 0,
		.cpu_guest_nice = 0
	};

	struct stats_cpu_12_1_7 s_cpu_1_1 = {
		.cpu_user = 1000,
		.cpu_nice = 0,
		.cpu_sys = 0,
		.cpu_idle = 0,
		.cpu_iowait = 0,
		.cpu_steal = 0,
		.cpu_hardirq = 0,
		.cpu_softirq = 0,
		.cpu_guest = 0,
		.cpu_guest_nice = 0
	};

	struct stats_cpu_12_1_7 s_cpu_2_1 = {
		.cpu_user = 0,
		.cpu_nice = 0,
		.cpu_sys = 500,
		.cpu_idle = 0,
		.cpu_iowait = 0,
		.cpu_steal = 0,
		.cpu_hardirq = 0,
		.cpu_softirq = 0,
		.cpu_guest = 0,
		.cpu_guest_nice = 0
	};

	struct stats_cpu_12_1_7 s_cpu_0_2 = {
		.cpu_user = 1100,
		.cpu_nice = 0,
		.cpu_sys = 500,
		.cpu_idle = 100,
		.cpu_iowait = 0,
		.cpu_steal = 0,
		.cpu_hardirq = 0,
		.cpu_softirq = 0,
		.cpu_guest = 0,
		.cpu_guest_nice = 0
	};

	struct stats_cpu_12_1_7 s_cpu_1_2 = {
		.cpu_user = 1100,
		.cpu_nice = 0,
		.cpu_sys = 0,
		.cpu_idle = 0,
		.cpu_iowait = 0,
		.cpu_steal = 0,
		.cpu_hardirq = 0,
		.cpu_softirq = 0,
		.cpu_guest = 0,
		.cpu_guest_nice = 0
	};

	struct stats_cpu_12_1_7 s_cpu_2_2 = {
		.cpu_user = 0,
		.cpu_nice = 0,
		.cpu_sys = 500,
		.cpu_idle = 100,
		.cpu_iowait = 0,
		.cpu_steal = 0,
		.cpu_hardirq = 0,
		.cpu_softirq = 0,
		.cpu_guest = 0,
		.cpu_guest_nice = 0
	};

	struct stats_pcsw_ukwn {
		unsigned long	processes	__attribute__ ((aligned (8)));
	};

	struct stats_pcsw_ukwn s_pcsw_1 = {
		.processes = 543
	};

	struct stats_pcsw_ukwn s_pcsw_2 = {
		.processes = 643
	};

	struct stats_unknown {
		unsigned long long unknown_ull;
		unsigned long      unknown_ul    __attribute__ ((aligned (8)));
	};

	struct stats_unknown s_ukwn_0_1 = {
		.unknown_ull = 123456789,
		.unknown_ul  = 12345
	};

	struct stats_unknown s_ukwn_1_1 = {
		.unknown_ull = 987654321,
		.unknown_ul  = 54321
	};

	struct stats_unknown s_ukwn_0_2 = {
		.unknown_ull = 234567891,
		.unknown_ul  = 23456
	};

	struct stats_unknown s_ukwn_1_2 = {
		.unknown_ull = 198765432,
		.unknown_ul  = 65432
	};

	int fd;
	int nr_cpu, nr_ukwn;
	char fname[16];

	/* Open data file */
	if (INCLUDE_CPU_STAT) {
		strcpy(fname, "data-ukwn");
	}
	else {
		strcpy(fname, "data-ukwn0");
	}
	if ((fd = open(fname, O_CREAT | O_WRONLY,
		       S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)) < 0) {
		perror("open");
		exit(1);
	}

	/* Write file magic structure */
	f_magic.header_size = sizeof(struct file_header_12_1_7);
	if (write(fd, &f_magic, sizeof(struct file_magic_12_1_7)) < 0) {
		perror("write file magic");
		exit(1);
	}

	/* Write file header structure */
	f_header.act_size = sizeof(struct file_activity_12_1_7);
	f_header.rec_size = sizeof(struct record_header_12_1_7);
	if (write(fd, &f_header, sizeof(struct file_header_12_1_7)) < 0) {
		perror("write file header");
		exit(1);
	}

	/* Write file activity list */
	if (INCLUDE_CPU_STAT) {
		f_activity_a_cpu.size = sizeof(struct stats_cpu_12_1_7);
		if (write(fd, &f_activity_a_cpu, sizeof(struct file_activity_12_1_7)) < 0) {
			perror("write file activity A_CPU");
			exit(1);
		}
	}
	f_activity_a_pcsw.size = sizeof(struct stats_pcsw_ukwn);
	if (write(fd, &f_activity_a_pcsw, sizeof(struct file_activity_12_1_7)) < 0) {
		perror("write file activity A_PCSW");
		exit(1);
	}
	f_activity_a_unknown.size = sizeof(struct stats_unknown);
	if (write(fd, &f_activity_a_unknown, sizeof(struct file_activity_12_1_7)) < 0) {
		perror("write file activity A_UNKNOWN");
		exit(1);
	}

	/* Write R_STAT #1 record */
	if (write(fd, &r_header_1, sizeof(struct record_header_12_1_7)) < 0) {
		perror("write STAT #1 record");
		exit(1);
	}

	if (INCLUDE_CPU_STAT) {
		/* Write A_CPU stats */
		nr_cpu = 3;
		if (write(fd, &nr_cpu, sizeof(int)) < 0) {
			perror("write nr_cpu #1");
			exit(1);
		}
		if (write(fd, &s_cpu_0_1, sizeof(struct stats_cpu_12_1_7)) < 0) {
			perror("write CPU stats 0_1");
			exit(1);
		}
		if (write(fd, &s_cpu_1_1, sizeof(struct stats_cpu_12_1_7)) < 0) {
			perror("write CPU stats 1_1");
			exit(1);
		}
		if (write(fd, &s_cpu_2_1, sizeof(struct stats_cpu_12_1_7)) < 0) {
			perror("write CPU stats 2_1");
			exit(1);
		}
	}

	/* Write A_PCSW stats */
	if (write(fd, &s_pcsw_1, sizeof(struct stats_pcsw_ukwn)) < 0) {
		perror("write PCSW stats 1");
		exit(1);
	}

	/* Write A_UNKNOWN stats */
	nr_ukwn = 2;
	if (write(fd, &nr_ukwn, sizeof(int)) < 0) {
		perror("write nr_ukwn #1");
		exit(1);
	}
	if (write(fd, &s_ukwn_0_1, sizeof(struct stats_unknown)) < 0) {
		perror("write UNKNOWN stats 0_1");
		exit(1);
	}
	if (write(fd, &s_ukwn_1_1, sizeof(struct stats_unknown)) < 0) {
		perror("write UNKNOWN stats 1_1");
		exit(1);
	}

	/* Write R_STAT #2 record */
	if (write(fd, &r_header_2, sizeof(struct record_header_12_1_7)) < 0) {
		perror("write STAT #2 record");
		exit(1);
	}

	if (INCLUDE_CPU_STAT) {
		/* Write A_CPU stats */
		nr_cpu = 3;
		if (write(fd, &nr_cpu, sizeof(int)) < 0) {
			perror("write nr_cpu #2");
			exit(1);
		}
		if (write(fd, &s_cpu_0_2, sizeof(struct stats_cpu_12_1_7)) < 0) {
			perror("write CPU stats 0_2");
			exit(1);
		}
		if (write(fd, &s_cpu_1_2, sizeof(struct stats_cpu_12_1_7)) < 0) {
			perror("write CPU stats 1_2");
			exit(1);
		}
		if (write(fd, &s_cpu_2_2, sizeof(struct stats_cpu_12_1_7)) < 0) {
			perror("write CPU stats 2_2");
			exit(1);
		}
	}

	/* Write A_PCSW stats */
	if (write(fd, &s_pcsw_2, sizeof(struct stats_pcsw_ukwn)) < 0) {
		perror("write PCSW stats 2");
		exit(1);
	}

	/* Write A_UNKNOWN stats */
	nr_ukwn = 2;
	if (write(fd, &nr_ukwn, sizeof(int)) < 0) {
		perror("write nr_ukwn #2");
		exit(1);
	}
	if (write(fd, &s_ukwn_0_2, sizeof(struct stats_unknown)) < 0) {
		perror("write UNKNOWN stats 0_2");
		exit(1);
	}
	if (write(fd, &s_ukwn_1_2, sizeof(struct stats_unknown)) < 0) {
		perror("write UNKNOWN stats 1_2");
		exit(1);
	}

	/* Close data file */
	close(fd);

	return 0;
}