Skip to content

Commit 9d3727f

Browse files
committed
Add comments, fix formatting, and add allocator stats emitter.
1 parent 793a2c8 commit 9d3727f

File tree

7 files changed

+844
-515
lines changed

7 files changed

+844
-515
lines changed

.clang-format

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
Language: Cpp
2+
Language: C
33
# BasedOnStyle: LLVM
44
AccessModifierOffset: -3
55
AlignAfterOpenBracket: Align

include/splinterdb/splinterdb.h

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,17 @@ splinterdb_stats_print_insertion(const splinterdb *kvs);
418418
void
419419
splinterdb_stats_print_lookup(const splinterdb *kvs);
420420

421+
void
422+
splinterdb_stats_reset(splinterdb *kvs);
423+
424+
/*
425+
* Statistics Emitters
426+
*
427+
* Reports insertion or lookup statistics by calling the passed
428+
* function with the passed void* data pointer. For programmatic
429+
* access to stats without having to parse the output
430+
* of splinterdb_stats_print_{insertion|lookup}
431+
*/
421432
void
422433
splinterdb_stats_emit_insertion(const splinterdb *kvs,
423434
void *user_data,
@@ -428,7 +439,4 @@ splinterdb_stats_emit_lookup(const splinterdb *kvs,
428439
void *user_data,
429440
emit_stat_fn user_fn);
430441

431-
void
432-
splinterdb_stats_reset(splinterdb *kvs);
433-
434442
#endif // _SPLINTERDB_H_

src/allocator.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// Copyright 2018-2021 VMware, Inc.
22
// SPDX-License-Identifier: Apache-2.0
33

4+
// clang-format off
5+
46
/*
57
* allocator.h --
68
*
@@ -139,6 +141,7 @@ typedef uint64 (*base_addr_fn)(const allocator *al, uint64 addr);
139141

140142
typedef void (*print_fn)(allocator *al);
141143
typedef void (*assert_fn)(allocator *al);
144+
typedef void (*emit_fn)(allocator* al, void* user_data, emit_stat_fn user_fn);
142145

143146
/*
144147
* Define an abstract allocator interface, holding different allocation-related
@@ -165,6 +168,7 @@ typedef struct allocator_ops {
165168

166169
print_fn print_stats;
167170
print_fn print_allocated;
171+
emit_fn emit_stats;
168172
} allocator_ops;
169173

170174
// To sub-class cache, make a cache your first field;
@@ -294,3 +298,10 @@ allocator_page_valid(allocator *al, uint64 addr)
294298
return FALSE;
295299
}
296300
}
301+
302+
// clang-format on
303+
304+
static inline void allocator_emit_stats(allocator* al, void* user_data, emit_stat_fn user_fn)
305+
{
306+
return al->ops->emit_stats(al, user_data, user_fn);
307+
}

src/clockcache.c

Lines changed: 63 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -2541,17 +2541,21 @@ clockcache_io_stats(clockcache *cc, uint64 *read_bytes, uint64 *write_bytes)
25412541
*read_bytes = read_pages * 4 * KiB;
25422542
}
25432543

2544+
// State struct for collecting cache statistics; captures common data
2545+
// between print and emit stats functions.
2546+
//
25442547
typedef struct cache_stats_collection {
2545-
cache_stats global_stats;
2546-
uint64 page_writes;
2547-
fraction miss_time[NUM_PAGE_TYPES];
2548-
fraction avg_prefetch_pages[NUM_PAGE_TYPES];
2549-
fraction avg_write_pages;
2550-
2548+
2549+
cache_stats global_stats;
2550+
uint64 page_writes;
2551+
fraction miss_time[NUM_PAGE_TYPES];
2552+
fraction avg_prefetch_pages[NUM_PAGE_TYPES];
2553+
fraction avg_write_pages;
2554+
25512555
} cache_stats_collection;
25522556

25532557
int
2554-
cache_stats_collection_create(clockcache *cc, cache_stats_collection* out)
2558+
cache_stats_collection_create(clockcache *cc, cache_stats_collection *out)
25552559
{
25562560
if (!cc->cfg->use_stats) {
25572561
return -1;
@@ -2562,7 +2566,8 @@ cache_stats_collection_create(clockcache *cc, cache_stats_collection* out)
25622566
for (uint64_t i = 0; i < MAX_THREADS; i++) {
25632567
for (page_type type = 0; type < NUM_PAGE_TYPES; type++) {
25642568
out->global_stats.cache_hits[type] += cc->stats[i].cache_hits[type];
2565-
out->global_stats.cache_misses[type] += cc->stats[i].cache_misses[type];
2569+
out->global_stats.cache_misses[type] +=
2570+
cc->stats[i].cache_misses[type];
25662571
out->global_stats.cache_miss_time_ns[type] +=
25672572
cc->stats[i].cache_miss_time_ns[type];
25682573
out->global_stats.page_writes[type] += cc->stats[i].page_writes[type];
@@ -2577,14 +2582,16 @@ cache_stats_collection_create(clockcache *cc, cache_stats_collection* out)
25772582

25782583

25792584
for (page_type type = 0; type < NUM_PAGE_TYPES; type++) {
2580-
out->miss_time[type] =
2581-
init_fraction(out->global_stats.cache_miss_time_ns[type], SEC_TO_NSEC(1));
2582-
out->avg_prefetch_pages[type] = init_fraction(
2583-
out->global_stats.page_reads[type] - out->global_stats.cache_misses[type],
2584-
out->global_stats.prefetches_issued[type]);
2585+
out->miss_time[type] = init_fraction(
2586+
out->global_stats.cache_miss_time_ns[type], SEC_TO_NSEC(1));
2587+
out->avg_prefetch_pages[type] =
2588+
init_fraction(out->global_stats.page_reads[type]
2589+
- out->global_stats.cache_misses[type],
2590+
out->global_stats.prefetches_issued[type]);
25852591
}
2586-
out->avg_write_pages = init_fraction(out->page_writes - out->global_stats.syncs_issued,
2587-
out->global_stats.writes_issued);
2592+
out->avg_write_pages =
2593+
init_fraction(out->page_writes - out->global_stats.syncs_issued,
2594+
out->global_stats.writes_issued);
25882595

25892596
return 0;
25902597
}
@@ -2594,9 +2601,9 @@ clockcache_print_stats(platform_log_handle *log_handle, clockcache *cc)
25942601
{
25952602
cache_stats_collection col;
25962603
if (cache_stats_collection_create(cc, &col) != 0) {
2597-
return;
2604+
return;
25982605
}
2599-
2606+
26002607
// clang-format off
26012608
platform_log(log_handle, "Cache Statistics\n");
26022609
platform_log(log_handle, "-----------------------------------------------------------------------------------------------\n");
@@ -2658,55 +2665,56 @@ clockcache_print_stats(platform_log_handle *log_handle, clockcache *cc)
26582665
allocator_print_stats(cc->al);
26592666
}
26602667

2668+
26612669
void
2662-
clockcache_emit_page_type_stats(void* user_data, emit_stat_fn user_fn,
2663-
const cache_stats_collection* col,
2664-
page_type type, const char* name)
2670+
clockcache_emit_stats(void *user_data, emit_stat_fn user_fn, clockcache *cc)
26652671
{
2666-
char name_buf[256];
2672+
cache_stats_collection col;
2673+
if (cache_stats_collection_create(cc, &col) != 0) {
2674+
return;
2675+
}
26672676

2668-
snprintf(name_buf, sizeof(name_buf), "splinterdb.cache.%s.hits", name);
2669-
user_fn(user_data, name_buf, col->global_stats.cache_hits[type]);
2670-
2671-
snprintf(name_buf, sizeof(name_buf), "splinterdb.cache.%s.misses", name);
2672-
user_fn(user_data, name_buf, col->global_stats.cache_misses[type]);
2673-
2674-
snprintf(name_buf, sizeof(name_buf), "splinterdb.cache.%s.miss_time.num", name);
2675-
user_fn(user_data, name_buf, col->miss_time[type].numerator);
2677+
user_fn(user_data, "splinterdb.cache.page_writes", col.page_writes);
26762678

2677-
snprintf(name_buf, sizeof(name_buf), "splinterdb.cache.%s.miss_time.div", name);
2678-
user_fn(user_data, name_buf, col->miss_time[type].denominator);
2679+
char name_buf[256];
26792680

2680-
snprintf(name_buf, sizeof(name_buf), "splinterdb.cache.%s.writes", name);
2681-
user_fn(user_data, name_buf, col->global_stats.page_writes[type]);
2681+
for (page_type type = PAGE_TYPE_FIRST; type < NUM_PAGE_TYPES; type++) {
2682+
const char *name = page_type_str[type];
26822683

2683-
snprintf(name_buf, sizeof(name_buf), "splinterdb.cache.%s.reads", name);
2684-
user_fn(user_data, name_buf, col->global_stats.page_reads[type]);
2684+
snprintf(name_buf, sizeof(name_buf), "splinterdb.cache.%s.hits", name);
2685+
user_fn(user_data, name_buf, col.global_stats.cache_hits[type]);
26852686

2686-
snprintf(name_buf, sizeof(name_buf), "splinterdb.cache.%s.avg_prefetch.num", name);
2687-
user_fn(user_data, name_buf, col->avg_prefetch_pages[type].numerator);
2688-
2689-
snprintf(name_buf, sizeof(name_buf), "splinterdb.cache.%s.avg_prefetch.div", name);
2690-
user_fn(user_data, name_buf, col->avg_prefetch_pages[type].denominator);
2691-
}
2687+
snprintf(name_buf, sizeof(name_buf), "splinterdb.cache.%s.misses", name);
2688+
user_fn(user_data, name_buf, col.global_stats.cache_misses[type]);
26922689

2693-
void
2694-
clockcache_emit_stats(void* user_data, emit_stat_fn user_fn, clockcache *cc)
2695-
{
2696-
cache_stats_collection col;
2697-
if (cache_stats_collection_create(cc, &col) != 0) {
2698-
return;
2699-
}
2690+
snprintf(
2691+
name_buf, sizeof(name_buf), "splinterdb.cache.%s.miss_time.num", name);
2692+
user_fn(user_data, name_buf, col.miss_time[type].numerator);
27002693

2701-
user_fn(user_data, "splinterdb.cache.page_writes", col.page_writes);
2694+
snprintf(
2695+
name_buf, sizeof(name_buf), "splinterdb.cache.%s.miss_time.div", name);
2696+
user_fn(user_data, name_buf, col.miss_time[type].denominator);
2697+
2698+
snprintf(name_buf, sizeof(name_buf), "splinterdb.cache.%s.writes", name);
2699+
user_fn(user_data, name_buf, col.global_stats.page_writes[type]);
2700+
2701+
snprintf(name_buf, sizeof(name_buf), "splinterdb.cache.%s.reads", name);
2702+
user_fn(user_data, name_buf, col.global_stats.page_reads[type]);
27022703

2703-
clockcache_emit_page_type_stats(user_data, user_fn, &col, PAGE_TYPE_TRUNK, "trunk");
2704-
clockcache_emit_page_type_stats(user_data, user_fn, &col, PAGE_TYPE_BRANCH, "branch");
2705-
clockcache_emit_page_type_stats(user_data, user_fn, &col, PAGE_TYPE_MEMTABLE, "memtable");
2706-
clockcache_emit_page_type_stats(user_data, user_fn, &col, PAGE_TYPE_FILTER, "filter");
2707-
clockcache_emit_page_type_stats(user_data, user_fn, &col, PAGE_TYPE_LOG, "log");
2708-
clockcache_emit_page_type_stats(user_data, user_fn, &col, PAGE_TYPE_SUPERBLOCK, "superblock");
2704+
snprintf(name_buf,
2705+
sizeof(name_buf),
2706+
"splinterdb.cache.%s.avg_prefetch.num",
2707+
name);
2708+
user_fn(user_data, name_buf, col.avg_prefetch_pages[type].numerator);
2709+
2710+
snprintf(name_buf,
2711+
sizeof(name_buf),
2712+
"splinterdb.cache.%s.avg_prefetch.div",
2713+
name);
2714+
user_fn(user_data, name_buf, col.avg_prefetch_pages[type].denominator);
2715+
}
27092716

2717+
allocator_emit_stats(cc->al, user_data, user_fn);
27102718
}
27112719

27122720
void

0 commit comments

Comments
 (0)