Skip to content

Commit 410b29c

Browse files
committed
tidy(trace): 删除生成Json记录中的cat字段,因为没有用处;优化demo
1 parent 213fd66 commit 410b29c

File tree

2 files changed

+26
-10
lines changed

2 files changed

+26
-10
lines changed

examples/trace/demo/main.cpp

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,45 +19,61 @@
1919
*/
2020

2121
#include <thread>
22+
#include <mutex>
2223
#include <tbox/trace/recorder.h>
2324
#include <tbox/trace/sink.h>
2425

2526
using namespace tbox;
2627

28+
std::mutex g_lock;
29+
2730
void Bar()
2831
{
32+
std::lock_guard<std::mutex> lg(g_lock);
2933
RECORD_SCOPE();
3034
std::this_thread::sleep_for(std::chrono::milliseconds(10));
35+
RECORD_SCOPE();
3136
}
3237
void Foo()
3338
{
3439
RECORD_SCOPE();
3540
Bar();
36-
RECORD_START(abc);
41+
RECORD_SCOPE();
3742
std::this_thread::sleep_for(std::chrono::milliseconds(5));
38-
RECORD_STOP(abc);
3943
}
4044

4145
void Do() {
42-
for (int i = 0; i < 100; ++i)
46+
RECORD_SCOPE();
47+
for (int i = 0; i < 3; ++i)
4348
Foo();
4449
}
4550

4651
int main(int argc, char **argv)
4752
{
53+
//! 配置Track导出方式
4854
auto &ts = trace::Sink::GetInstance();
49-
ts.setRecordFileMaxSize(1<<20);
5055
ts.setPathPrefix("/tmp/test/trace-demo");
51-
ts.enable();
56+
ts.setRecordFileMaxSize(1<<20);
57+
ts.enable(); //! 开始记录
5258

5359
RECORD_EVENT();
5460

55-
auto t = std::thread(Do);
56-
Do();
61+
RECORD_START(c);
62+
RECORD_START(a);
63+
auto t1 = std::thread(Do);
64+
auto t2 = std::thread(Do);
65+
auto t3 = std::thread(Do);
66+
RECORD_STOP(a);
5767

58-
t.join();
68+
RECORD_START(b);
69+
t1.join();
70+
t2.join();
71+
t3.join();
72+
RECORD_STOP(b);
73+
RECORD_STOP(c);
5974

6075
RECORD_EVENT();
61-
ts.disable();
76+
77+
ts.disable(); //! 停止记录
6278
return 0;
6379
}

modules/trace/tools/to_json/writer.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ bool Writer::writeRecorder(const std::string &name, const std::string &tid, uint
4949
ofs_ << ',' << std::endl;
5050
is_first_record_ = false;
5151

52-
ofs_ << R"({"name":")" << name << R"(","cat":"function","pid":"","tid":")"
52+
ofs_ << R"({"name":")" << name << R"(","pid":"","tid":")"
5353
<< tid << R"(","ts":)" << start_ts_us << ',';
5454

5555
if (duration_us != 0)

0 commit comments

Comments
 (0)