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
|
/* ft2sort -- Sort Feather-Trace events in a binary file by sequence number.
* Copyright (C) 2011 B. Brandenburg.
*
* 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 even 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-1301 USA.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <sys/time.h>
#include <errno.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/mman.h>
#include "mapping.h"
#include "timestamp.h"
static unsigned int holes = 0;
static unsigned int reordered = 0;
#define LOOK_AHEAD 1000
/* wall-clock time in seconds */
double wctime(void)
{
struct timeval tv;
gettimeofday(&tv, NULL);
return (tv.tv_sec + 1E-6 * tv.tv_usec);
}
static struct timestamp* find_lowest_seq_no(struct timestamp* start,
struct timestamp* end,
unsigned int seqno)
{
struct timestamp *pos, *min = start;
if (end > start + LOOK_AHEAD)
end = start + LOOK_AHEAD;
for (pos = start; pos != end && min->seq_no != seqno; pos++)
if (pos->seq_no < min->seq_no)
min = pos;
return min;
}
static void move_record(struct timestamp* target, struct timestamp* pos)
{
struct timestamp tmp, *prev;
while (pos > target) {
/* shift backwards */
tmp = *pos;
prev = pos - 1;
*pos = *prev;
*prev = tmp;
pos = prev;
}
}
static void reorder(struct timestamp* start, struct timestamp* end)
{
struct timestamp* pos, *tmp;
unsigned int last_seqno = 0;
for (pos = start; pos != end; pos++) {
/* check for for holes in the sequence number */
if (last_seqno && last_seqno + 1 != pos->seq_no) {
tmp = find_lowest_seq_no(pos, end, last_seqno + 1);
if (tmp->seq_no == last_seqno + 1)
/* Good, we found it. */
/* Move it to the right place. */
reordered++;
else {
/* bad, there's a hole here */
holes++;
fprintf(stderr, "HOLE: %u instead of %u\n", tmp->seq_no, last_seqno + 1);
}
move_record(pos, tmp);
}
last_seqno = pos->seq_no;
}
}
static inline uint64_t bget(int x, uint64_t quad)
{
return (((0xffll << 8 * x) & quad) >> 8 * x);
}
static inline uint64_t bput(uint64_t b, int pos)
{
return (b << 8 * pos);
}
static inline uint64_t ntohx(uint64_t q)
{
return (bput(bget(0, q), 7) | bput(bget(1, q), 6) |
bput(bget(2, q), 5) | bput(bget(3, q), 4) |
bput(bget(4, q), 3) | bput(bget(5, q), 2) |
bput(bget(6, q), 1) | bput(bget(7, q), 0));
}
static void restore_byte_order(struct timestamp* start, struct timestamp* end)
{
struct timestamp* pos = start;
while (pos !=end) {
pos->timestamp = ntohx(pos->timestamp);
pos->seq_no = ntohl(pos->seq_no);
pos++;
}
}
#define USAGE \
"Usage: ftsort [-e] <logfile> \n" \
" -e: endianess swap -- restores byte order \n" \
"\n" \
"WARNING: Changes are permanent.\n"
static void die(char* msg)
{
if (errno)
perror("error: ");
fprintf(stderr, "%s\n", msg);
fprintf(stderr, "%s", USAGE);
exit(1);
}
#define OPTS "e"
int main(int argc, char** argv)
{
void* mapped;
size_t size, count;
struct timestamp *ts, *end;
int swap_byte_order = 0;
int opt;
double start, stop;
while ((opt = getopt(argc, argv, OPTS)) != -1) {
switch (opt) {
case 'e':
swap_byte_order = 1;
break;
default:
die("Unknown option.");
break;
}
}
if (argc - optind != 1)
die("arguments missing");
start = wctime();
if (map_file_rw(argv[optind], &mapped, &size))
die("could not map file");
ts = (struct timestamp*) mapped;
count = size / sizeof(struct timestamp);
end = ts + count;
if (swap_byte_order)
restore_byte_order(ts, end);
reorder(ts, end);
/* write back */
msync(ts, size, MS_SYNC | MS_INVALIDATE);
stop = wctime();
fprintf(stderr,
"Total : %10d\n"
"Holes : %10d\n"
"Reordered : %10d\n"
"Size : %10.2f Mb\n"
"Time : %10.2f s\n"
"Throughput : %10.2f Mb/s\n",
(int) count,
holes, reordered,
((double) size) / 1024.0 / 1024.0,
(stop - start),
((double) size) / 1024.0 / 1024.0 / (stop - start));
return 0;
}
|