-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtimescale.cpp
More file actions
423 lines (350 loc) · 10.5 KB
/
timescale.cpp
File metadata and controls
423 lines (350 loc) · 10.5 KB
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
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <caputils/caputils.h>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cerrno>
#include <csignal>
#include <cinttypes>
#include <getopt.h>
#include <functional>
#include <stdexcept>
#include "extract.hpp"
static const char* iface = NULL;
static const stream_stat* stat = NULL;
const char* program_name = NULL;
static void handle_sigint(int signum){
if ( !keep_running ){
fprintf(stderr, "\rGot SIGINT again, terminating.\n");
abort();
}
fprintf(stderr, "\rAborting capture.\n");
keep_running = false;
}
static void show_stats(int signum){
if ( !stat ) return;
fprintf(stderr, "%s: %'" PRIu64 " packets has been read.\n", program_name, stat->read);
};
static double my_round (double value){
static const double bias = 0.0005;
return (floor(value + bias));
}
static double fastpow(double x, int y){
switch ( y ){
case 1: return x;
case 2: return x*x;
case 3: return x*x*x;
default: return pow(x, (double)y);
}
}
class Bin {
public:
Bin(int level, int timescale, int moments, Bin* next = nullptr)
: next(next)
, level(level)
, timescale(timescale)
, num_moments(moments)
, accumulator(nullptr)
, previous(0.0)
, counter(0) {
setup_accumulator();
}
~Bin(){
delete [] accumulator;
delete next;
}
void setup_accumulator(){
accumulator = new qd_real[num_moments];
for ( int i = 0; i < num_moments; i++ ){
accumulator[i] = 0.0;
}
}
/**
* Called for each value.
*/
void feed(double value){
for ( int i = 0; i < num_moments; i++ ){
accumulator[i] += fastpow(value, i+1);
}
if ( ++counter % timescale == 0 ){
sample();
}
}
/**
* Called when enough datapoints was gathered.
*/
void sample(){
const double mean = to_double((accumulator[0] - previous) / timescale);
previous = accumulator[0];
if ( !next ){
next = new Bin(level+1, timescale, num_moments);
}
next->feed(mean);
}
void recursive_visit(std::function<void(Bin*)> callback){
callback(this);
if ( next ){
next->recursive_visit(callback);
}
}
void recursive_visit(std::function<void(const Bin*)> callback) const {
callback(this);
if ( next ){
((const Bin*)next)->recursive_visit(callback);
}
}
private:
friend class Timescale;
friend class DefaultOutput;
friend class CSVOutput;
Bin* next;
const int level;
const int timescale;
const int num_moments;
qd_real* accumulator;
qd_real previous;
int counter;
};
class Output {
public:
virtual ~Output(){}
virtual void write_output(const Bin* bin, int timescale, int num_moments, double sampleFrequency, double tSample) = 0;
};
class DefaultOutput: public Output {
public:
virtual void write_output(const Bin* bin, int timescale, int num_moments, double sampleFrequency, double tSample){
int width[num_moments];
for ( int i = 0; i < num_moments; i++ ){
width[i] = str_width_for_moment(bin, i);
}
fprintf(stdout, "sampleFrequency: %.2fHz\n", sampleFrequency);
fprintf(stdout, "tSample: %fs\n", tSample);
fprintf(stdout, "timescale: %d\n", timescale);
fprintf(stdout, "\n");
fprintf(stdout, "Tscale ");
for ( int i = 0; i < num_moments; i++ ){
fprintf(stdout, "%*s%d ", width[i], "M", i+1);
}
fprintf(stdout, " Samples\n");
bin->recursive_visit([&](const Bin* cur){
fprintf(stdout, "%-8g ", pow((double)cur->timescale, (double)cur->level) * tSample);
for ( int i = 0; i < num_moments; i++ ){
fprintf(stdout, "%*g ", width[i]+1, to_double(cur->accumulator[i] / cur->counter));
}
fprintf(stdout, " %d\n", cur->counter);
});
}
private:
size_t str_width_for_moment(const Bin* bin, int index){
size_t max = 0;
char buf[64];
bin->recursive_visit([&](const Bin* cur){
size_t width = snprintf(buf, sizeof(buf), "%g", to_double(cur->accumulator[index] / cur->counter));
max = width > max ? width : max;
});
return max;
}
};
class CSVOutput: public Output {
public:
CSVOutput(char delimiter, bool show_header)
: delimiter(delimiter)
, show_header(show_header){
}
virtual void write_output(const Bin* bin, int timescale, int num_moments, double sampleFrequency, double tSample){
if ( show_header ){
fprintf(stdout, "\"Tscale (%dx, %.2fHz)\"", timescale, sampleFrequency);
for ( int i = 0; i < num_moments; i++ ){
fprintf(stdout, "%c\"M%d\"", delimiter, i+1);
}
fprintf(stdout, "%c\"Samples\"\n", delimiter);
}
bin->recursive_visit([&](const Bin* cur){
fprintf(stdout, "%g", pow((double)cur->timescale, (double)cur->level) * tSample);
for ( int i = 0; i < num_moments; i++ ){
fprintf(stdout, "%c%f", delimiter, to_double(cur->accumulator[i] / cur->counter));
}
fprintf(stdout, "%c%d\n", delimiter, cur->counter);
});
};
private:
char delimiter;
bool show_header;
};
class Timescale: public Extractor {
public:
Timescale()
: Extractor()
, output(nullptr)
, num_moments(3)
, timescale(10)
, bin(nullptr)
, bits(0.0) {
set_formatter(FORMAT_DEFAULT);
}
virtual ~Timescale(){
delete bin;
delete output;
}
void set_timescale(int timescale){
this->timescale = timescale;
}
void set_moments(int moments){
num_moments = moments;
}
virtual void set_formatter(enum Formatter format){
delete output;
switch (format){
case FORMAT_DEFAULT: output = new DefaultOutput; break;
case FORMAT_CSV: output = new CSVOutput(';', false); break;
case FORMAT_TSV: output = new CSVOutput('\t', false); break;
case FORMAT_MATLAB: output = new CSVOutput('\t', true); break;
case FORMAT_INFLUX:
throw std::runtime_error("Influx not supported for timescale yet");
}
}
using Extractor::set_formatter;
virtual void reset(){
Extractor::reset();
if ( !bin ){
bin = new Bin(0, timescale, num_moments);
bits = 0.0;
}
}
void write_summary(){
output->write_output(bin, timescale, num_moments, sampleFrequency, to_double(tSample));
}
protected:
virtual void write_trailer(int index){
}
virtual void write_sample(double t){
const double bitrate = my_round(bits / to_double(tSample));
bin->feed(bitrate);
bits = 0.0;
}
virtual void accumulate(qd_real fraction, unsigned long packet_bits, const cap_head* cp, int counter){
bits += my_round(to_double(fraction) * packet_bits);
}
private:
Output* output;
int num_moments;
int timescale;
Bin* bin;
double bits;
};
static const char* short_options = "p:q:m:l:f:t:n:h";
static struct option long_options[]= {
{"packets", required_argument, 0, 'p'},
{"level", required_argument, 0, 'q'},
{"sampleFrequency", required_argument, 0, 'm'},
{"linkCapacity", required_argument, 0, 'l'},
{"format", required_argument, 0, 'f'},
{"timescale", required_argument, 0, 't'},
{"moments", required_argument, 0, 'n'},
{"help", no_argument, 0, 'h'},
{0, 0, 0, 0} /* sentinel */
};
static void show_usage(void){
printf("%s-" VERSION " (libcap_utils-%s)\n", program_name, caputils_version(NULL));
printf("(C) 2012 David Sveningsson <david.sveningsson@bth.se>\n");
printf("Usage: %s [OPTIONS] STREAM\n", program_name);
printf(" -m, --sampleFrequency Sampling frequency in Hz. Valid prefixes are 'k', 'm' and 'g'.\n"
" -q, --level Level to calculate bitrate {link (default), network, transport and application}\n"
" At level N, only packet size at particular layer is considered, use filters to select particular streams.\n"
" - link: all bits captured at physical level, i.e link + network + transport + application\n"
" - network: payload field at link layer, network + transport + application\n"
" - transport: payload at network layer, transport + application\n"
" - application: The payload field at transport level, ie.application\n"
" Default is link.\n"
" -l, --linkCapacity Link capacity in bits per second default 100 Mbps, (eg.input 100e6) \n"
" -p, --packets=N Stop after N packets.\n"
" -f, --format=FORMAT Set a specific output format. See below for list of supported formats.\n"
" -t, --timescale=SCALE Set timescale [default: 10].\n"
" -n, --moments=MOMENTS Show N moments [default: 3].\n"
" -h, --help This text.\n\n");
output_format_list();
filter_from_argv_usage();
}
int main(int argc, char **argv){
/* extract program name from path. e.g. /path/to/MArCd -> MArCd */
const char* separator = strrchr(argv[0], '/');
if ( separator ){
program_name = separator + 1;
} else {
program_name = argv[0];
}
struct filter filter;
if ( filter_from_argv(&argc, argv, &filter) != 0 ){
return 0; /* error already shown */
}
Timescale app;
app.set_ignore_marker(true);
int op, option_index = -1;
while ( (op = getopt_long(argc, argv, short_options, long_options, &option_index)) != -1 ){
switch (op){
case 0: /* long opt */
case '?': /* unknown opt */
break;
case 'f': /* --format */
app.set_formatter(optarg);
break;
case 'p':
app.set_max_packets(atoi(optarg));
break;
case 'm' : /* --sampleFrequency */
app.set_sampling_frequency(optarg);
break;
case 'q': /* --level */
app.set_extraction_level(optarg);
break;
case 'l': /* --link */
app.set_link_capacity(optarg);
break;
case 'i':
iface = optarg;
break;
case 't': /* --timescale */
app.set_timescale(atoi(optarg));
break;
case 'n': /* --moments */
app.set_moments(atoi(optarg));
break;
case 'h':
show_usage();
return 0;
default:
fprintf (stderr, "%s: ?? getopt returned character code 0%o ??\n", program_name, op);
}
}
/* handle C-c */
signal(SIGINT, handle_sigint);
signal(SIGUSR1, show_stats);
if ( optind == argc ){
fprintf(stderr, "%s: no input files, see -h for usage.\n", program_name);
exit(1);
}
int ret;
/* Open stream(s) */
stream_t stream;
stream_addr_t addr;
for ( int i = optind; i < argc; i++ ){
if ( !keep_running ) break;
const char* filename = argv[i];
stream_addr_str(&addr, filename, 0);
if ( (ret=stream_open(&stream, &addr, nullptr, 0)) != 0 ) {
fprintf(stderr, "%s: stream_open() failed with code 0x%08X: %s\n", program_name, ret, caputils_error_string(ret));
continue;
}
stat = stream_get_stat(stream);
app.reset();
app.process_stream(stream, &filter);
stream_close(stream);
stream = nullptr;
}
app.write_summary();
/* Release resources */
filter_close(&filter);
return keep_running ? 0 : 1;
}