From d1889e632eeedc4447c7ce1c18bab54a65c6c8d9 Mon Sep 17 00:00:00 2001 From: juncheng Date: Sat, 14 Dec 2024 00:18:10 -0500 Subject: [PATCH] allow lcs reader to load n_req directly; fix a bug in version; clean up; add lcs_reader.py --- libCacheSim/bin/traceUtils/traceConvLCS.cpp | 21 +- .../traceReader/customizedReader/lcs.c | 6 +- .../traceReader/customizedReader/lcs.h | 3 +- scripts/lcs_reader.py | 207 ++++++++++++++++++ 4 files changed, 213 insertions(+), 24 deletions(-) create mode 100644 scripts/lcs_reader.py diff --git a/libCacheSim/bin/traceUtils/traceConvLCS.cpp b/libCacheSim/bin/traceUtils/traceConvLCS.cpp index 47350124..5e77f87e 100644 --- a/libCacheSim/bin/traceUtils/traceConvLCS.cpp +++ b/libCacheSim/bin/traceUtils/traceConvLCS.cpp @@ -51,8 +51,8 @@ void convert_to_lcs(reader_t *reader, std::string ofilepath, bool output_txt, bo std::unordered_map ttl_cnt; lcs_trace_stat_t stat; - stat.version = 1; memset(&stat, 0, sizeof(stat)); + stat.version = CURR_STAT_VERSION; int64_t n_req_total = get_num_of_req(reader); obj_map.reserve(n_req_total / 100 + 1e4); @@ -98,18 +98,6 @@ void convert_to_lcs(reader_t *reader, std::string ofilepath, bool output_txt, bo } } - // if (lcs_ver == 1) { - // // lcs_req_v1_t *lcs_req_v1 = reinterpret_cast(lcs_req); - // lcs_req_v1_t lcs_req; - // lcs_req.clock_time = req->clock_time; - // lcs_req.obj_id = req->obj_id; - // lcs_req.obj_size = req->obj_size; - // lcs_req.next_access_vtime = req->next_access_vtime; - - // ofile_temp.write(reinterpret_cast(&lcs_req), sizeof(lcs_req_v1)); - // } else if (lcs_ver == 2) { - - // lcs_req_v2_t *lcs_req_v2 = reinterpret_cast(lcs_req); lcs_req_full_t lcs_req; lcs_req.clock_time = req->clock_time; lcs_req.obj_id = req->obj_id; @@ -141,7 +129,6 @@ void convert_to_lcs(reader_t *reader, std::string ofilepath, bool output_txt, bo } ofile_temp.write(reinterpret_cast(&lcs_req), sizeof(lcs_req_full_t)); - // } stat.n_req_byte += req->obj_size; stat.n_req += 1; @@ -258,7 +245,7 @@ static void _analyze_trace(lcs_trace_stat_t &stat, const std::unordered_map(freq_cnt_vec[i].first)); - log_rank[n] = log(static_cast(n+1)); + log_rank[n] = log(static_cast(n + 1)); n++; } } @@ -276,8 +263,8 @@ static void _analyze_trace(lcs_trace_stat_t &stat, const std::unordered_mapversion > MAX_LCS_VERSION) { - ERROR("invalid trace file, lcs version %ld is not supported\n", (unsigned long)header->version); - return false; - } - lcs_trace_stat_t *stat = &(header->stat); if (stat->n_req < 0 || stat->n_obj < 0) { ERROR("invalid trace file, n_req %ld, n_obj %ld\n", (unsigned long)stat->n_req, (unsigned long)stat->n_obj); @@ -50,6 +45,7 @@ int lcsReader_setup(reader_t *reader) { reader->trace_format = BINARY_TRACE_FORMAT; reader->trace_start_offset = sizeof(lcs_trace_header_t); reader->obj_id_is_num = true; + reader->n_total_req = header->stat.n_req; if (reader->lcs_ver == 1) { reader->item_size = sizeof(lcs_req_v1_t); diff --git a/libCacheSim/traceReader/customizedReader/lcs.h b/libCacheSim/traceReader/customizedReader/lcs.h index 0aa1b4c9..1cc56f29 100644 --- a/libCacheSim/traceReader/customizedReader/lcs.h +++ b/libCacheSim/traceReader/customizedReader/lcs.h @@ -24,8 +24,7 @@ extern "C" { #define LCS_TRACE_START_MAGIC 0x123456789abcdef0 #define LCS_TRACE_END_MAGIC 0x123456789abcdef0 - -#define MAX_LCS_VERSION 2 +#define CURR_STAT_VERSION 1 #define N_MOST_COMMON 16 /******************************************************************************/ diff --git a/scripts/lcs_reader.py b/scripts/lcs_reader.py new file mode 100644 index 00000000..c3edc630 --- /dev/null +++ b/scripts/lcs_reader.py @@ -0,0 +1,207 @@ +# see [lcs.h](https://github.com/1a1a11a/libCacheSim/blob/develop/libCacheSim/traceReader/customizedReader/lcs.h) for the definition of the trace format +# typedef struct lcs_trace_stat { +# int64_t version; // version of the stat +# int64_t n_req; // number of requests +# int64_t n_obj; // number of objects +# int64_t n_req_byte; // number of bytes requested +# int64_t n_obj_byte; // number of unique bytes + +# int64_t start_timestamp; // in seconds +# int64_t end_timestamp; // in seconds + +# int64_t n_read; // number of read requests +# int64_t n_write; // number of write requests +# int64_t n_delete; // number of delete requests + +# // object size +# int64_t smallest_obj_size; +# int64_t largest_obj_size; +# int64_t most_common_obj_sizes[N_MOST_COMMON]; +# float most_common_obj_size_ratio[N_MOST_COMMON]; + +# // popularity +# // the request count of the most popular objects +# int64_t highest_freq[N_MOST_COMMON]; +# // unpopular objects: +# int32_t most_common_freq[N_MOST_COMMON]; +# float most_common_freq_ratio[N_MOST_COMMON]; +# // zipf alpha +# double skewness; + +# // tenant info +# int32_t n_tenant; +# int32_t most_common_tenants[N_MOST_COMMON]; +# float most_common_tenant_ratio[N_MOST_COMMON]; + +# // key-value cache and object cache specific +# int32_t n_ttl; +# int32_t smallest_ttl; +# int32_t largest_ttl; +# int32_t most_common_ttls[N_MOST_COMMON]; +# float most_common_ttl_ratio[N_MOST_COMMON]; + +# int64_t unused[897]; +# } __attribute__((packed)) lcs_trace_stat_t; + +# typedef struct lcs_trace_header { +# uint64_t start_magic; +# // the version of lcs trace, see lcs_v1, lcs_v2, etc. +# uint64_t version; +# struct lcs_trace_stat stat; + +# uint64_t unused[21]; +# uint64_t end_magic; +# } __attribute__((packed)) lcs_trace_header_t; +# + +# typedef struct __attribute__((packed)) lcs_req_v1 { +# uint32_t clock_time; +# // this is the hash of key in key-value cache +# // or the logical block address in block cache +# uint64_t obj_id; +# uint32_t obj_size; +# int64_t next_access_vtime; +# } lcs_req_v1_t; + +# typedef struct __attribute__((packed)) lcs_req_v2 { +# uint32_t clock_time; +# uint64_t obj_id; +# uint32_t obj_size; +# uint32_t op : 8; +# uint32_t tenant : 24; +# int64_t next_access_vtime; +# } lcs_req_v2_t; + + +# typedef struct __attribute__((packed)) lcs_req_v3 { +# int64_t clock_time; +# uint64_t obj_id; +# int64_t obj_size; +# uint32_t op : 8; +# uint32_t tenant : 24; +# int64_t next_access_vtime; +# } lcs_req_v3_t; + + +import struct + + +LCS_HEADER_SIZE = 1024 * 8 +LCS_TRACE_STAT_SIZE = 1000 * 8 +LCS_STRAT_MAGIC = 0x123456789ABCDEF0 +LCS_END_MAGIC = 0x123456789ABCDEF0 +N_MOST_COMMON = 16 + + +def parse_stat(b, print_stat=True): + + # basic info + ( + ver, + n_req, + n_obj, + n_req_byte, + n_obj_byte, + start_ts, + end_ts, + n_read, + n_write, + n_delete, + ) = struct.unpack(" 0 and n_req >= n_max_req: + break + + ifile.close() + + +if __name__ == "__main__": + import sys + + if len(sys.argv) < 2: + print(f"Usage: {sys.argv[0]} /path/trace [n_req]") + sys.exit(1) + + read_trace(sys.argv[1], int(sys.argv[2]) if len(sys.argv) > 2 else -1)