-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbitrate.cpp
365 lines (299 loc) · 9.88 KB
/
bitrate.cpp
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
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <caputils/caputils.h>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cerrno>
#include <csignal>
#include <iostream>
#include <iomanip>
#include <getopt.h>
#include "extract.hpp"
#include "http.hpp"
static int show_zero = 0;
static int viz_hack = 0;
static const char* iface = NULL;
static char* influx_mpid = nullptr;
const char* program_name = NULL;
static const char* influx_url = "http://localhost:8086/write?db=testdb";
static const char* influx_user = "miffo";
static const char* influx_pwd = "konko";
static const char* influx_tag = NULL;
enum {
HTTP_CREATED = 204,
};
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;
}
class Output {
public:
virtual void write_header(double sampleFrequency, double tSample){};
virtual void write_trailer(){};
virtual void write_sample(double t, double bitrate) = 0;
};
class DefaultOutput: public Output {
public:
virtual void write_header(double sampleFrequency, double tSample){
fprintf(stdout, "sampleFrequency: %.2fHz\n", sampleFrequency);
fprintf(stdout, "tSample: %fs\n", tSample);
fprintf(stdout, "\n");
fprintf(stdout, "Time \t Bitrate (bps)\n");
}
virtual void write_sample(double t, double bitrate){
fprintf(stdout, "%.15f\t%.15f\n", t, bitrate);
}
};
class CSVOutput: public Output {
public:
CSVOutput(char delimiter, bool show_header)
: delimiter(delimiter)
, show_header(show_header){
}
virtual void write_header(double sampleFrequency, double tSample){
if ( show_header ){
fprintf(stdout, "\"Time (tSample: %f)\"%c\"Bitrate (bps)\"\n", tSample, delimiter);
}
}
virtual void write_sample(double t, double bitrate){
fprintf(stdout, "%.15f%c%.15f\n", t, delimiter, bitrate);
}
private:
char delimiter;
bool show_header;
};
class InfluxOutput: public Output {
public:
InfluxOutput(const char* url, const char* user, const char* pass)
: http(url, user, pass) {
}
virtual void write_sample(double t, double bitrate){
static char str[1500];
if (influx_tag) {
sprintf(str, "bitrate,mpid=%s,%s value=%g %llu",
influx_mpid, influx_tag,bitrate, (long long int)(t*1e9));
} else {
sprintf(str, "bitrate,mpid=%s value=%g %llu",
influx_mpid, bitrate, (long long int)(t*1e9));
}
fprintf(stderr,"Sending:%s ", str);
// const char* statusstr=http.POSTstr(str);
const int status = http.POST(str);
if ( status != HTTP_CREATED ){
fprintf(stderr, "influx(1) returned HTTP %d\n", status);
} else {
fprintf(stdout," OK \n");
}
}
protected:
HTTPOutput http;
};
static double my_round (double value){
static const double bias = 0.0005;
return (floor(value + bias));
}
class BitrateCalculator: public Extractor {
public:
BitrateCalculator()
: Extractor()
, bits(0.0){
set_formatter(FORMAT_DEFAULT);
}
void set_mpid(const mampid_t mpid){
if ( influx_mpid ) return; /* @todo only first mpid will be used */
influx_mpid = strdup(mampid_get(mpid));
}
void set_formatter(enum Formatter format){
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: output = new InfluxOutput(influx_url, influx_user, influx_pwd); break;
}
}
using Extractor::set_formatter;
virtual void reset(){
bits = 0.0;
Extractor::reset();
}
protected:
virtual void write_header(int index){
output->write_header(sampleFrequency, to_double(tSample));
}
virtual void write_trailer(int index){
output->write_trailer();
}
virtual void write_sample(double t){
const double bitrate = my_round(bits / to_double(tSample));
if ( viz_hack ){
t *= sampleFrequency;
}
if ( show_zero || bitrate > 0 ){
output->write_sample(t, 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;
double bits;
};
static const char* short_options = "u:U:P:Q:p:i:q:m:l:f:zxtTh";
static struct option long_options[]= {
{"packets", required_argument, 0, 'p'},
{"iface", required_argument, 0, 'i'},
{"level", required_argument, 0, 'q'},
{"sampleFrequency", required_argument, 0, 'm'},
{"linkCapacity", required_argument, 0, 'l'},
{"format", required_argument, 0, 'f'},
{"show-zero", no_argument, 0, 'z'},
{"no-show-zero", no_argument, 0, 'x'},
{"relative-time", no_argument, 0, 't'},
{"absolute-time", no_argument, 0, 'T'},
{"viz-hack", no_argument, &viz_hack, 1},
{"influx-url", required_argument, 0, 'u'},
{"influx-user", required_argument, 0, 'U'},
{"influx-pwd", required_argument, 0, 'P'},
{"influx-tag", required_argument, 0, 'Q'},
{"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) 2004 Patrik Arlos <patrik.arlos@bth.se>\n");
printf("(C) 2012 David Sveningsson <ext@sidvind.com>\n");
printf("(C) 2012 Vamsi krishna Konakalla <vkk@bth.se>\n\n");
printf("Usage: %s [OPTIONS] STREAM\n", program_name);
printf(" -i, --iface For ethernet-based streams, this is the interface\n"
" to listen on. For other streams it is ignored.\n"
" -m, --sampleFrequency Sampling frequency in Hz. Prefixes: k, m, g.\n"
" -q, --level Level to calculate bitrate on. At level N, only\n"
" packet size at particular layer is considered.\n"
" - link: all bits captured at physical level.\n"
" - network: payload field at link layer.\n"
" - transport: payload at network layer.\n"
" - application: payload field at transport level.\n"
" Default is link-layer.\n"
" -l, --linkCapacity Link capacity in BPS, default is 100e6 (100 Mbps).\n"
" -p, --packets=N Stop after N packets.\n"
" -z, --show-zero Show bitrate when zero.\n"
" -x, --no-show-zero Don't show bitrate when zero [default]\n"
" -f, --format=FORMAT Set a specific output format. See below for list\n"
" of supported formats.\n"
" -t, --relative-time Show timestamps relative to the first packet.\n"
" -T, --absolute-time Show timestamps with absolute values (default).\n"
" -h, --help This text.\n"
"\n"
"Influx\n"
" -u, --influx-url URL used to send data to Infux; \n "
" cf. http://localhost:8086/write?db=testdb \n"
" -U --influx-user Influx Username for authentication.\n"
" -P --influx-pwd Influx Password for authentication.\n"
" -Q --influx-tag Additional Influx tag. tag=<string>\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 */
}
BitrateCalculator app;
char* format = nullptr;
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 */
/* format initialization is deferred until all args has been
* consumed */
format = strdup(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 'z':
show_zero = 1;
break;
case 'x':
show_zero = 0;
break;
case 't': /* --relative-time */
app.set_relative_time(true);
break;
case 'T': /* --absolute-time */
app.set_relative_time(false);
break;
case 'u': /* --influx-url */
influx_url = optarg;
break;
case 'U': /* --influx-user */
influx_user = optarg;
break;
case 'P': /* --influx-pwd */
influx_pwd = optarg;
break;
case 'Q': /* --influx-tag */
influx_tag = optarg;
break;
case 'h':
show_usage();
return 0;
default:
fprintf (stderr, "%s: ?? getopt returned character code 0%o ??\n", program_name, op);
}
}
/* load output format */
if ( format ){
app.set_formatter(format);
free(format);
}
/* handle C-c */
signal(SIGINT, handle_sigint);
int ret;
/* Open stream(s) */
stream_t stream;
if ( (ret=stream_from_getopt(&stream, argv, optind, argc, iface, "-", program_name, 0)) != 0 ) {
return ret; /* Error already shown */
}
stream_print_info(stream, stderr);
app.reset();
app.process_stream(stream, &filter);
/* Release resources */
free(influx_mpid);
stream_close(stream);
filter_close(&filter);
return 0;
}