Skip to content

Commit 30f97cc

Browse files
committed
FT-300 Add mean / stddev tracking for block allocation sizes to
ba_replay
1 parent 600a864 commit 30f97cc

File tree

1 file changed

+37
-7
lines changed

1 file changed

+37
-7
lines changed

tools/ba_replay.cc

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ PATENT RIGHTS GRANT:
9191

9292
#include <db.h>
9393

94+
#include <math.h>
9495
#include <stdio.h>
9596
#include <string.h>
9697

@@ -308,6 +309,26 @@ static vector<string> canonicalize_trace_from(FILE *file) {
308309
return canonicalized_trace;
309310
}
310311

312+
struct streaming_variance_calculator {
313+
int64_t n_samples;
314+
int64_t mean;
315+
int64_t variance;
316+
317+
// math credit: AoCP, Donald Knuth, '62
318+
void add_sample(int64_t x) {
319+
n_samples++;
320+
if (n_samples == 1) {
321+
mean = x;
322+
variance = 0;
323+
} else {
324+
int64_t old_mean = mean;
325+
mean = old_mean + ((x - old_mean) / n_samples);
326+
variance = (((n_samples - 1) * variance) +
327+
((x - old_mean) * (x - mean))) / n_samples;
328+
}
329+
}
330+
};
331+
311332
struct canonical_trace_stats {
312333
uint64_t n_lines_replayed;
313334

@@ -318,6 +339,9 @@ struct canonical_trace_stats {
318339
uint64_t n_free;
319340
uint64_t n_destroy;
320341

342+
struct streaming_variance_calculator alloc_hot_bytes;
343+
struct streaming_variance_calculator alloc_cold_bytes;
344+
321345
canonical_trace_stats() {
322346
memset(this, 0, sizeof(*this));
323347
}
@@ -389,6 +413,7 @@ static void replay_canonicalized_trace(const vector<string> &canonicalized_trace
389413
ba->alloc_block(size, heat, &offset);
390414
seq_num_to_offset[asn] = offset;
391415
heat ? stats->n_alloc_hot++ : stats->n_alloc_cold++;
416+
heat ? stats->alloc_hot_bytes.add_sample(size) : stats->alloc_cold_bytes.add_sample(size);
392417
} else if (fn == "ba_trace_free_asn") {
393418
// replay a `free' on a block whose offset is the result of an alloc with an asn
394419
const uint64_t asn = parse_uint64(&ptr, line_num);
@@ -559,13 +584,18 @@ int main(void) {
559584
printf("\n");
560585
printf("Overall trace stats:\n");
561586
printf("\n");
562-
printf(" n_lines_played: %9" PRIu64 "\n", stats.n_lines_replayed);
563-
printf(" n_create: %9" PRIu64 "\n", stats.n_create);
564-
printf(" n_create_from_blockpairs: %9" PRIu64 "\n", stats.n_create_from_blockpairs);
565-
printf(" n_alloc_hot: %9" PRIu64 "\n", stats.n_alloc_hot);
566-
printf(" n_alloc_cold: %9" PRIu64 "\n", stats.n_alloc_cold);
567-
printf(" n_free: %9" PRIu64 "\n", stats.n_free);
568-
printf(" n_destroy: %9" PRIu64 "\n", stats.n_destroy);
587+
printf(" n_lines_played: %15" PRIu64 "\n", stats.n_lines_replayed);
588+
printf(" n_create: %15" PRIu64 "\n", stats.n_create);
589+
printf(" n_create_from_blockpairs: %15" PRIu64 "\n", stats.n_create_from_blockpairs);
590+
printf(" n_alloc_hot: %15" PRIu64 "\n", stats.n_alloc_hot);
591+
printf(" n_alloc_cold: %15" PRIu64 "\n", stats.n_alloc_cold);
592+
printf(" n_free: %15" PRIu64 "\n", stats.n_free);
593+
printf(" n_destroy: %15" PRIu64 "\n", stats.n_destroy);
594+
printf("\n");
595+
printf(" avg_alloc_hot: %15" PRIu64 "\n", stats.alloc_hot_bytes.mean);
596+
printf(" stddev_alloc_hot: %15" PRIu64 "\n", (uint64_t) sqrt(stats.alloc_hot_bytes.variance));
597+
printf(" avg_alloc_cold: %15" PRIu64 "\n", stats.alloc_cold_bytes.mean);
598+
printf(" stddev_alloc_cold: %15" PRIu64 "\n", (uint64_t) sqrt(stats.alloc_cold_bytes.variance));
569599
printf("\n");
570600

571601
return 0;

0 commit comments

Comments
 (0)