- 
                Notifications
    You must be signed in to change notification settings 
- Fork 1
Add script to convert Meta Tectonic traces into object traces (for use with libCacheSim) #8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,104 @@ | ||||||||||||||||||||||||||||
| // Script to convert Meta's block storage traces to libCacheSim csv format | ||||||||||||||||||||||||||||
| // | ||||||||||||||||||||||||||||
| // Usage: | ||||||||||||||||||||||||||||
| // g++ conv_meta_block.cpp -o conv_meta_block | ||||||||||||||||||||||||||||
| // ./conv_meta_block block_traces_1_conved.csv block_traces_1.csv | ||||||||||||||||||||||||||||
|  | ||||||||||||||||||||||||||||
| #include <fstream> | ||||||||||||||||||||||||||||
| #include <iostream> | ||||||||||||||||||||||||||||
| #include <random> | ||||||||||||||||||||||||||||
| #include <sstream> | ||||||||||||||||||||||||||||
| #include <unordered_set> | ||||||||||||||||||||||||||||
| #include <vector> | ||||||||||||||||||||||||||||
|  | ||||||||||||||||||||||||||||
| constexpr size_t CHUNK_SIZE = 128 * 1024; // 128 KiB | ||||||||||||||||||||||||||||
| constexpr double SAMPLE_RATIO = 0.1; // sample SAMPLE_RATIO fraction of blocks | ||||||||||||||||||||||||||||
| constexpr size_t SAMPLE_SEED = 42; | ||||||||||||||||||||||||||||
|  | ||||||||||||||||||||||||||||
| int main(int argc, char* argv[]) { | ||||||||||||||||||||||||||||
| if (argc < 3) { | ||||||||||||||||||||||||||||
| std::cout << "Usage: " << argv[0] | ||||||||||||||||||||||||||||
| << " <output_csv> <input_csv1> [<input_csv2> ...]\n"; | ||||||||||||||||||||||||||||
| return EXIT_FAILURE; | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|  | ||||||||||||||||||||||||||||
| // open files | ||||||||||||||||||||||||||||
| std::ofstream output_csv{argv[1]}; | ||||||||||||||||||||||||||||
| if (!output_csv) { | ||||||||||||||||||||||||||||
| std::cout << "Error: Could not open file " << argv[1] << std::endl; | ||||||||||||||||||||||||||||
| There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. | ||||||||||||||||||||||||||||
| return EXIT_FAILURE; | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
| std::vector<std::ifstream> input_csvs{}; | ||||||||||||||||||||||||||||
| for (size_t i = 2; i < argc; ++i) { | ||||||||||||||||||||||||||||
| input_csvs.emplace_back(argv[i]); | ||||||||||||||||||||||||||||
| if (!input_csvs.back()) { | ||||||||||||||||||||||||||||
| std::cout << "Error: Could not open file " << argv[i] << std::endl; | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
| 
      Comment on lines
    
      +32
     to 
      +37
    
   There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The current logic prints an error but continues if an input file fails to open. This could lead to an incomplete conversion without a clear failure signal. It's more robust to exit immediately upon failing to open a required file. Also, error messages should be directed to  
        Suggested change
       
 | ||||||||||||||||||||||||||||
|  | ||||||||||||||||||||||||||||
| // vars for sampling | ||||||||||||||||||||||||||||
| std::unordered_set<std::string> keep{}; | ||||||||||||||||||||||||||||
| std::unordered_set<std::string> dont_keep{}; | ||||||||||||||||||||||||||||
| std::mt19937 gen{SAMPLE_SEED}; | ||||||||||||||||||||||||||||
| std::uniform_real_distribution<> dis(0.0, 1.0); | ||||||||||||||||||||||||||||
|  | ||||||||||||||||||||||||||||
| // header of output csv, following libCacheSim csv format | ||||||||||||||||||||||||||||
| output_csv << "timestamp,obj_id,obj_size\n"; | ||||||||||||||||||||||||||||
|  | ||||||||||||||||||||||||||||
| // fields of input csvs we are interested in, following Meta's block storage | ||||||||||||||||||||||||||||
| // traces format | ||||||||||||||||||||||||||||
| size_t op_time; // 0 | ||||||||||||||||||||||||||||
| std::string block_id; // 1 | ||||||||||||||||||||||||||||
| size_t io_size; // 3 | ||||||||||||||||||||||||||||
| size_t io_offset; // 4 | ||||||||||||||||||||||||||||
| std::string rs_shard_id; // 10 | ||||||||||||||||||||||||||||
| 
      Comment on lines
    
      +50
     to 
      +54
    
   There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using magic numbers for column indices, as indicated by the comments, makes the code harder to read and maintain. If the column order of the input trace changes, you'd have to find all occurrences of these numbers. It would be better to define these as named constants. For example: constexpr size_t kColOpTime = 0;
constexpr size_t kColBlockId = 1;
// ...etc.
// Then use them in the loop:
op_time = std::stoull(cells[kColOpTime]); | ||||||||||||||||||||||||||||
|  | ||||||||||||||||||||||||||||
| // read input_csvs, write to output_csv | ||||||||||||||||||||||||||||
| std::string line; | ||||||||||||||||||||||||||||
| std::string cell; | ||||||||||||||||||||||||||||
| std::vector<std::string> cells{}; | ||||||||||||||||||||||||||||
| for (auto& input_csv : input_csvs) { | ||||||||||||||||||||||||||||
| getline(input_csv, line); // skip header | ||||||||||||||||||||||||||||
| while (getline(input_csv, line)) { | ||||||||||||||||||||||||||||
| cells.clear(); | ||||||||||||||||||||||||||||
|  | ||||||||||||||||||||||||||||
| std::stringstream row(line); | ||||||||||||||||||||||||||||
| while (getline(row, cell, ',')) { | ||||||||||||||||||||||||||||
| cells.push_back(cell); | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|  | ||||||||||||||||||||||||||||
| op_time = std::stoull(cells[0]); | ||||||||||||||||||||||||||||
| block_id = cells[1]; | ||||||||||||||||||||||||||||
| io_size = std::stoull(cells[3]); | ||||||||||||||||||||||||||||
| io_offset = std::stoull(cells[4]); | ||||||||||||||||||||||||||||
| rs_shard_id = cells[10]; | ||||||||||||||||||||||||||||
| 
      Comment on lines
    
      +70
     to 
      +74
    
   There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The CSV parsing logic here is not robust and could cause the program to crash if it encounters malformed data. 
 You should add validation to handle these cases gracefully, for instance by wrapping the parsing in a  if (cells.size() < 11) {
    std::cerr << "Skipping malformed line (not enough columns): " << line << std::endl;
    continue;
}
try {
    op_time = std::stoull(cells[0]);
    block_id = cells[1];
    io_size = std::stoull(cells[3]);
    io_offset = std::stoull(cells[4]);
    rs_shard_id = cells[10];
} catch (const std::exception& e) {
    std::cerr << "Skipping line due to conversion error: " << e.what() << " in line: " << line << std::endl;
    continue;
} | ||||||||||||||||||||||||||||
|  | ||||||||||||||||||||||||||||
| // Reference for doing conversion: | ||||||||||||||||||||||||||||
| // https://github.com/facebook/CacheLib/commit/23a888e54e4fed22f81c114e3ca9af95d7f0787c | ||||||||||||||||||||||||||||
| // Each storage node block is uniquely identified via block_id + | ||||||||||||||||||||||||||||
| // rs_shard_id. It's in turn broken into chunks to be cached. We | ||||||||||||||||||||||||||||
| // represent a chunk of a storage node block as the cache object. | ||||||||||||||||||||||||||||
| std::string real_block_id = block_id + "_" + rs_shard_id; | ||||||||||||||||||||||||||||
| if (keep.count(real_block_id) == 0 && | ||||||||||||||||||||||||||||
| dont_keep.count(real_block_id) == 0) { | ||||||||||||||||||||||||||||
| double u = dis(gen); | ||||||||||||||||||||||||||||
| if (u > SAMPLE_RATIO) { | ||||||||||||||||||||||||||||
| dont_keep.insert(real_block_id); | ||||||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||||
| keep.insert(real_block_id); | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|  | ||||||||||||||||||||||||||||
| if (dont_keep.count(real_block_id) > 0) { | ||||||||||||||||||||||||||||
| continue; | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|  | ||||||||||||||||||||||||||||
| size_t start_chunk = io_offset / CHUNK_SIZE; | ||||||||||||||||||||||||||||
| size_t end_chunk = (io_offset + io_size - 1) / CHUNK_SIZE; | ||||||||||||||||||||||||||||
| for (size_t chunk_id = start_chunk; chunk_id <= end_chunk; ++chunk_id) { | ||||||||||||||||||||||||||||
| std::string obj_id = real_block_id + "_" + std::to_string(chunk_id); | ||||||||||||||||||||||||||||
| output_csv << op_time << ',' << obj_id << ',' << CHUNK_SIZE << '\n'; | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's standard practice to print usage information to
std::cerrrather thanstd::cout. This allows users to redirect standard output (e.g., to a file) without capturing usage or error text.