@@ -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+ //
25442547typedef 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
25532557int
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+
26612669void
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
27122720void
0 commit comments