blkparse: split off the timestamp correction code in to a separate function
[blktrace.git] / verify_blkparse.c
... / ...
CommitLineData
1#include <stdio.h>
2#include <stdlib.h>
3#include <fcntl.h>
4#include <string.h>
5#include <unistd.h>
6#include <errno.h>
7
8int main(int argc, char *argv[])
9{
10 double this_time, last_time;
11 char line[256], last_line[256], *p;
12 int major, minor, cpu, nr, alias;
13 long MAX_CPUS;
14 unsigned long long total_entries;
15 unsigned int *last_seq;
16 unsigned int seq;
17 FILE *f;
18
19#ifdef _SC_NPROCESSORS_ONLN
20 MAX_CPUS = sysconf(_SC_NPROCESSORS_ONLN);
21 if (MAX_CPUS < 1)
22 {
23 fprintf(stderr, "Could not determine number of CPUs online:\n%s\n",
24 strerror (errno));
25 fprintf(stderr, "Assuming 1024\n");
26 MAX_CPUS = 1024;
27 }
28#else
29 MAX_CPUS = CPU_SETSIZE;
30#endif
31
32 last_seq = malloc( sizeof(unsigned int) * MAX_CPUS );
33 for (nr = 0; nr < MAX_CPUS; nr++)
34 last_seq[nr] = -1;
35
36 if (argc < 2) {
37 fprintf(stderr, "%s: file\n", argv[0]);
38 return 1;
39 }
40
41 f = fopen(argv[1], "r");
42 if (!f) {
43 perror("fopen");
44 return 1;
45 }
46
47 last_time = 0;
48 alias = nr = 0;
49 total_entries = 0;
50 while ((p = fgets(line, sizeof(line), f)) != NULL) {
51 if (sscanf(p, "%3d,%3d %5d %8d %lf", &major, &minor, &cpu, &seq, &this_time) != 5)
52 break;
53
54 if (this_time < last_time) {
55 fprintf(stdout, "last: %s", last_line);
56 fprintf(stdout, "this: %s", p);
57 nr++;
58 }
59
60 last_time = this_time;
61
62 if (cpu >= MAX_CPUS) {
63 fprintf(stderr, "cpu%d too large\n", cpu);
64 break;
65 }
66
67 if (last_seq[cpu] == seq) {
68 fprintf(stdout, "alias on sequence %u\n", seq);
69 alias++;
70 }
71
72 last_seq[cpu] = seq;
73 total_entries++;
74 strcpy(last_line, line);
75 }
76
77 fprintf(stdout, "Events %Lu: %d unordered, %d aliases\n", total_entries, nr, alias);
78 fclose(f);
79
80 return nr != 0;
81}