Skip to content

Commit b6196bc

Browse files
authored
#269 print per-loop stats with --stats=0 (#288)
1 parent 5bdca6d commit b6196bc

File tree

5 files changed

+32
-14
lines changed

5 files changed

+32
-14
lines changed

src/common/flows.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ static inline flow_entry_type_t hash_put_data(flow_hash_table_t *fht, const uint
147147
res = FLOW_ENTRY_INVALID;
148148
}
149149

150-
dbgx(2, "flow type=%d\n", (int)res);
150+
dbgx(2, "flow type=%d", (int)res);
151151
return res;
152152
}
153153

@@ -321,7 +321,7 @@ flow_hash_table_t *flow_hash_table_init(size_t n)
321321
{
322322
flow_hash_table_t *fht;
323323
if (!is_power_of_2(n))
324-
errx(-1, "invalid table size: %zu\n", n);
324+
errx(-1, "invalid table size: %zu", n);
325325

326326
fht = safe_malloc(sizeof(*fht));
327327
fht->num_buckets = n;

src/common/utils.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -157,10 +157,10 @@ packet_stats(const tcpreplay_stats_t *stats)
157157
}
158158

159159
if (diff_us >= 1000000)
160-
printf("Actual: " COUNTER_SPEC " packets (" COUNTER_SPEC " bytes) sent in %zd.%02zd seconds.\n",
161-
stats->pkts_sent, stats->bytes_sent, (ssize_t)diff.tv_sec, (ssize_t)(diff.tv_usec / (100 * 1000)));
160+
printf("Actual: " COUNTER_SPEC " packets (" COUNTER_SPEC " bytes) sent in %zd.%02zd seconds\n",
161+
stats->pkts_sent, stats->bytes_sent, (ssize_t)diff.tv_sec, (ssize_t)(diff.tv_usec / (10 * 1000)));
162162
else
163-
printf("Actual: " COUNTER_SPEC " packets (" COUNTER_SPEC " bytes) sent in %zd.%06zd seconds.\n",
163+
printf("Actual: " COUNTER_SPEC " packets (" COUNTER_SPEC " bytes) sent in %zd.%06zd seconds\n",
164164
stats->pkts_sent, stats->bytes_sent, (ssize_t)diff.tv_sec, (ssize_t)diff.tv_usec);
165165

166166

src/tcpedit/tcpedit.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ tcpedit_init(tcpedit_t **tcpedit_ex, int dlt)
330330
tcpedit->runtime.dlt1 = dlt;
331331
tcpedit->runtime.dlt2 = dlt;
332332

333-
dbgx(1, "Input file (1) datalink type is %s\n",
333+
dbgx(1, "Input file (1) datalink type is %s",
334334
pcap_datalink_val_to_name(dlt));
335335

336336
#ifdef FORCE_ALIGN
@@ -494,7 +494,7 @@ tcpedit_close(tcpedit_t *tcpedit)
494494

495495
assert(tcpedit);
496496
dbgx(1, "tcpedit processed " COUNTER_SPEC " bytes in " COUNTER_SPEC
497-
" packets.\n", tcpedit->runtime.total_bytes,
497+
" packets.", tcpedit->runtime.total_bytes,
498498
tcpedit->runtime.pkts_edited);
499499

500500
/* free buffer if required */

src/tcpreplay_api.c

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,9 @@ tcpreplay_init()
9898
/* set the default MTU size */
9999
ctx->options->mtu = DEFAULT_MTU;
100100

101+
/* disable periodic statistics */
102+
ctx->options->stats = -1;
103+
101104
/* disable limit send */
102105
ctx->options->limit_send = -1;
103106

@@ -1149,7 +1152,7 @@ tcpreplay_prepare(tcpreplay_t *ctx)
11491152
int
11501153
tcpreplay_replay(tcpreplay_t *ctx)
11511154
{
1152-
int rcode;
1155+
int rcode, loop, total_loops;
11531156

11541157
assert(ctx);
11551158

@@ -1172,21 +1175,36 @@ tcpreplay_replay(tcpreplay_t *ctx)
11721175
return -1;
11731176
}
11741177

1175-
11761178
ctx->running = true;
1179+
total_loops = ctx->options->loop;
1180+
loop = 0;
11771181

11781182
/* main loop, when not looping forever (or until abort) */
11791183
if (ctx->options->loop > 0) {
11801184
while (ctx->options->loop-- && !ctx->abort) { /* limited loop */
1185+
if (ctx->options->stats == 0)
1186+
printf("Loop %d of %d...\n", ++loop, total_loops);
11811187
if ((rcode = tcpr_replay_index(ctx)) < 0)
11821188
return rcode;
1183-
if (ctx->options->loop > 0 && !ctx->abort && ctx->options->loopdelay_ms > 0)
1184-
usleep(ctx->options->loopdelay_ms * 1000);
1189+
if (ctx->options->loop > 0) {
1190+
if (!ctx->abort && ctx->options->loopdelay_ms > 0) {
1191+
usleep(ctx->options->loopdelay_ms * 1000);
1192+
gettimeofday(&ctx->stats.end_time, NULL);
1193+
}
1194+
1195+
if (ctx->options->stats == 0)
1196+
packet_stats(&ctx->stats);
1197+
}
11851198
}
11861199
} else {
11871200
while (!ctx->abort) { /* loop forever unless user aborts */
1201+
if (ctx->options->stats == 0)
1202+
printf("Loop %d...\n", ++loop);
11881203
if ((rcode = tcpr_replay_index(ctx)) < 0)
11891204
return rcode;
1205+
1206+
if (ctx->options->stats == 0 && !ctx->abort)
1207+
packet_stats(&ctx->stats);
11901208
}
11911209
}
11921210

src/tcpreplay_opts.def

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -596,10 +596,10 @@ EOText;
596596
flag = {
597597
name = stats;
598598
arg-type = number;
599-
arg-range = "1->";
600-
descrip = "Print statistics every X seconds";
599+
arg-range = "0->";
600+
descrip = "Print statistics every X seconds, or every loop if '0'";
601601
doc = <<- EOText
602-
Note that this is very much a "best effort" and long delays between
602+
Note that timed delays are a "best effort" and long delays between
603603
sending packets may cause equally long delays between printing statistics.
604604
EOText;
605605
};

0 commit comments

Comments
 (0)