|
9 | 9 | * \\ \ \ / |
10 | 10 | * -============' |
11 | 11 | * |
12 | | - * Copyright (c) 2018 Hevake and contributors, all rights reserved. |
| 12 | + * Copyright (c) 2024 Hevake and contributors, all rights reserved. |
13 | 13 | * |
14 | 14 | * This file is part of cpp-tbox (https://github.com/cpp-main/cpp-tbox) |
15 | 15 | * Use of this source code is governed by MIT license that can be found |
16 | 16 | * in the LICENSE file in the root of the source tree. All contributing |
17 | 17 | * project authors may be found in the CONTRIBUTORS.md file in the root |
18 | 18 | * of the source tree. |
19 | 19 | */ |
20 | | -#include <tbox/base/log.h> |
21 | | -#include <tbox/base/log_output.h> |
22 | | -#include <tbox/base/scope_exit.hpp> |
| 20 | +#include <iostream> |
| 21 | + |
| 22 | +#include <tbox/util/fs.h> |
| 23 | +#include <tbox/util/buffer.h> |
| 24 | +#include <tbox/util/scalable_integer.h> |
| 25 | + |
| 26 | +#include "writer.h" |
23 | 27 |
|
24 | 28 | using namespace std; |
| 29 | +using namespace tbox; |
| 30 | + |
| 31 | +using StringVec = std::vector<std::string>; |
| 32 | + |
| 33 | +void PrintUsage(const char *proc_name) |
| 34 | +{ |
| 35 | + std::cout |
| 36 | + << "Usage: " << proc_name << " <dir_path> [output_filename]" << std::endl |
| 37 | + << "Exp : " << proc_name << " /some/where/my_proc.20240531_032237.114" << std::endl |
| 38 | + << " " << proc_name << " /some/where/my_proc.20240531_032237.114 records.json" << std::endl; |
| 39 | +} |
| 40 | + |
| 41 | +bool PickRecord(util::Buffer &buffer, uint64_t &end_diff_us, uint64_t &duration_us, uint64_t &thread_index, uint64_t &name_index) |
| 42 | +{ |
| 43 | + uint8_t *buffer_begin = buffer.readableBegin(); |
| 44 | + size_t buffer_size = buffer.readableSize(); |
| 45 | + |
| 46 | + size_t parse_size = 0; |
| 47 | + size_t data_size = 0; |
| 48 | + |
| 49 | + parse_size = util::ParseScalableInteger((buffer_begin + data_size), (buffer_size - data_size), end_diff_us); |
| 50 | + if (parse_size == 0) |
| 51 | + return false; |
| 52 | + data_size += parse_size; |
| 53 | + |
| 54 | + parse_size += util::ParseScalableInteger((buffer_begin + data_size), (buffer_size - data_size), duration_us); |
| 55 | + if (parse_size == 0) |
| 56 | + return false; |
| 57 | + data_size += parse_size; |
| 58 | + |
| 59 | + parse_size += util::ParseScalableInteger((buffer_begin + data_size), (buffer_size - data_size), thread_index); |
| 60 | + if (parse_size == 0) |
| 61 | + return false; |
| 62 | + data_size += parse_size; |
| 63 | + |
| 64 | + parse_size += util::ParseScalableInteger((buffer_begin + data_size), (buffer_size - data_size), name_index); |
| 65 | + if (parse_size == 0) |
| 66 | + return false; |
| 67 | + data_size += parse_size; |
| 68 | + |
| 69 | + buffer.hasRead(data_size); |
| 70 | + return true; |
| 71 | +} |
| 72 | + |
| 73 | +void ParseRecordFile(const std::string &filename, const StringVec &names, const StringVec &threads, trace::Writer &writer) |
| 74 | +{ |
| 75 | + std::ifstream ifs(filename, std::ifstream::binary); |
| 76 | + if (!ifs) { |
| 77 | + std::cerr << "read '" << filename << "' fail!" << std::endl; |
| 78 | + return; |
| 79 | + } |
| 80 | + |
| 81 | + uint64_t last_end_ts_us = 0; |
| 82 | + util::Buffer buffer; |
| 83 | + |
| 84 | + while (true) { |
| 85 | + char tmp[1024]; |
| 86 | + auto rsize = ifs.readsome(tmp, sizeof(tmp)); |
| 87 | + if (rsize == 0) |
| 88 | + break; |
| 89 | + |
| 90 | + buffer.append(tmp, rsize); |
| 91 | + |
| 92 | + while (buffer.readableSize() >= 4) { |
| 93 | + uint64_t end_diff_us, duration_us, thread_index, name_index; |
| 94 | + if (!PickRecord(buffer, end_diff_us, duration_us, thread_index, name_index)) |
| 95 | + break; |
| 96 | + |
| 97 | + uint64_t end_ts_us = last_end_ts_us + end_diff_us; |
| 98 | + uint64_t start_ts_us = end_ts_us - duration_us; |
| 99 | + last_end_ts_us = end_ts_us; |
| 100 | + |
| 101 | + std::string name = "unknown-name", thread = "unknown-thread"; |
| 102 | + if (!names.empty() && name_index < names.size()) |
| 103 | + name = names[name_index]; |
| 104 | + if (!threads.empty() && thread_index < threads.size()) |
| 105 | + thread = threads[thread_index]; |
| 106 | + |
| 107 | + writer.writeRecorder(name, thread, start_ts_us, duration_us); |
| 108 | + } |
| 109 | + } |
| 110 | +} |
25 | 111 |
|
26 | 112 | int main(int argc, char **argv) |
27 | 113 | { |
28 | | - LogOutput_Enable(); |
29 | | - LogInfo("hello"); |
30 | | - LogOutput_Disable(); |
| 114 | + if (argc < 2) { |
| 115 | + PrintUsage(argv[0]); |
| 116 | + return 0; |
| 117 | + } |
| 118 | + |
| 119 | + std::string dir_path = argv[1]; |
| 120 | + if (!util::fs::IsDirectoryExist(dir_path)) { |
| 121 | + std::cout << "Error: dir_path '" << dir_path << "' not exist!" << std::endl; |
| 122 | + return 0; |
| 123 | + } |
| 124 | + |
| 125 | + std::string output_filename = dir_path + "/output.json"; |
| 126 | + if (argc > 2) |
| 127 | + output_filename = argv[2]; |
| 128 | + |
| 129 | + trace::Writer writer; |
| 130 | + if (!writer.open(output_filename)) { |
| 131 | + std::cout << "Error: output_filename '" << output_filename << "' can't be create!" << std::endl; |
| 132 | + return 0; |
| 133 | + } |
| 134 | + |
| 135 | + //! 从 threads.txt 与 names.txt 导入数据 |
| 136 | + std::string names_filename = dir_path + "/names.txt"; |
| 137 | + std::string threads_filename = dir_path + "/threads.txt"; |
| 138 | + StringVec name_vec, thread_vec; |
| 139 | + if (!util::fs::ReadAllLinesFromTextFile(names_filename, name_vec)) |
| 140 | + std::cerr << "Warn: load names.txt fail!" << std::endl; |
| 141 | + if (!util::fs::ReadAllLinesFromTextFile(threads_filename, thread_vec)) |
| 142 | + std::cerr << "Warn: load threads.txt fail!" << std::endl; |
| 143 | + |
| 144 | + writer.writeHeader(); |
| 145 | + |
| 146 | + writer.writeRecorder("hello()", "12", 1000, 15); |
| 147 | + writer.writeRecorder("event()", "12", 1005, 0); |
| 148 | + writer.writeRecorder("world()", "13", 1010, 3); |
| 149 | + |
| 150 | + writer.writeFooter(); |
31 | 151 | return 0; |
32 | 152 | } |
| 153 | + |
0 commit comments