* @brief dump the age distribution of cached objects to a file * * WARNNING: this function obtain the age via evicting the cached objects * so the cache state will change after calling this function * * @param cache * @param req used to provide the current time * @param ofilepath * @return true * @return false */
| 437 | * @return false |
| 438 | */ |
| 439 | bool dump_cached_obj_age(cache_t *cache, const request_t *req, const char *ofilepath) { |
| 440 | FILE *ofile = fopen(ofilepath, "a"); |
| 441 | if (ofile == NULL) { |
| 442 | perror("fopen failed"); |
| 443 | return false; |
| 444 | } |
| 445 | |
| 446 | /* clear/reset eviction age counters */ |
| 447 | for (int i = 0; i < EVICTION_AGE_ARRAY_SZE; i++) { |
| 448 | cache->log_eviction_age_cnt[i] = 0; |
| 449 | } |
| 450 | |
| 451 | int64_t n_cached_obj = cache->get_n_obj(cache); |
| 452 | int64_t n_evicted_obj = 0; |
| 453 | /* evict all the objects */ |
| 454 | while (cache->get_occupied_byte(cache) > 0) { |
| 455 | cache->evict(cache, req); |
| 456 | n_evicted_obj++; |
| 457 | } |
| 458 | assert(n_cached_obj == n_evicted_obj); |
| 459 | |
| 460 | int64_t n_ages = 0; |
| 461 | /* dump the cached objects' ages */ |
| 462 | fprintf(ofile, "%s, cached_obj age, cache size: %lu, ", cache->cache_name, (unsigned long)cache->cache_size); |
| 463 | for (int i = 0; i < EVICTION_AGE_ARRAY_SZE; i++) { |
| 464 | if (cache->log_eviction_age_cnt[i] == 0) { |
| 465 | continue; |
| 466 | } |
| 467 | n_ages += cache->log_eviction_age_cnt[i]; |
| 468 | fprintf(ofile, "%lld:%ld, ", (long long)pow(EVICTION_AGE_LOG_BASE, i), (long)cache->log_eviction_age_cnt[i]); |
| 469 | } |
| 470 | fprintf(ofile, "\n"); |
| 471 | assert(n_ages == n_cached_obj); |
| 472 | |
| 473 | fclose(ofile); |
| 474 | return true; |
| 475 | } |
| 476 | |
| 477 | void generate_cache_name(cache_t *cache, char *str_dest, int str_dest_len) { |
| 478 | int len = snprintf(str_dest, str_dest_len, "%s", cache->cache_name); |