11#include " tracing/agent.h"
22
3- #include < sstream>
43#include < string>
54#include " tracing/node_trace_buffer.h"
6- #include " tracing/node_trace_writer.h"
75
86namespace node {
97namespace tracing {
108
11- namespace {
12-
13- class ScopedSuspendTracing {
9+ class Agent ::ScopedSuspendTracing {
1410 public:
15- ScopedSuspendTracing (TracingController* controller, Agent* agent)
16- : controller_(controller), agent_(agent) {
17- controller->StopTracing ();
11+ ScopedSuspendTracing (TracingController* controller, Agent* agent,
12+ bool do_suspend = true )
13+ : controller_(controller), agent_(do_suspend ? agent : nullptr ) {
14+ if (do_suspend) {
15+ CHECK (agent_->started_ );
16+ controller->StopTracing ();
17+ }
1818 }
1919
2020 ~ScopedSuspendTracing () {
21+ if (agent_ == nullptr ) return ;
2122 TraceConfig* config = agent_->CreateTraceConfig ();
2223 if (config != nullptr ) {
2324 controller_->StartTracing (config);
@@ -29,8 +30,10 @@ class ScopedSuspendTracing {
2930 Agent* agent_;
3031};
3132
33+ namespace {
34+
3235std::set<std::string> flatten (
33- const std::unordered_map<int , std::set <std::string>>& map) {
36+ const std::unordered_map<int , std::multiset <std::string>>& map) {
3437 std::set<std::string> result;
3538 for (const auto & id_value : map)
3639 result.insert (id_value.second .begin (), id_value.second .end ());
@@ -43,18 +46,17 @@ using v8::platform::tracing::TraceConfig;
4346using v8::platform::tracing::TraceWriter;
4447using std::string;
4548
46- Agent::Agent (const std::string& log_file_pattern)
47- : log_file_pattern_(log_file_pattern) {
49+ Agent::Agent () {
4850 tracing_controller_ = new TracingController ();
4951 tracing_controller_->Initialize (nullptr );
52+
53+ CHECK_EQ (uv_loop_init (&tracing_loop_), 0 );
5054}
5155
5256void Agent::Start () {
5357 if (started_)
5458 return ;
5559
56- CHECK_EQ (uv_loop_init (&tracing_loop_), 0 );
57-
5860 NodeTraceBuffer* trace_buffer_ = new NodeTraceBuffer (
5961 NodeTraceBuffer::kBufferChunks , this , &tracing_loop_);
6062 tracing_controller_->Initialize (trace_buffer_);
@@ -71,18 +73,30 @@ void Agent::Start() {
7173
7274AgentWriterHandle Agent::AddClient (
7375 const std::set<std::string>& categories,
74- std::unique_ptr<AsyncTraceWriter> writer) {
76+ std::unique_ptr<AsyncTraceWriter> writer,
77+ enum UseDefaultCategoryMode mode) {
7578 Start ();
79+
80+ const std::set<std::string>* use_categories = &categories;
81+
82+ std::set<std::string> categories_with_default;
83+ if (mode == kUseDefaultCategories ) {
84+ categories_with_default.insert (categories.begin (), categories.end ());
85+ categories_with_default.insert (categories_[kDefaultHandleId ].begin (),
86+ categories_[kDefaultHandleId ].end ());
87+ use_categories = &categories_with_default;
88+ }
89+
7690 ScopedSuspendTracing suspend (tracing_controller_, this );
7791 int id = next_writer_id_++;
7892 writers_[id] = std::move (writer);
79- categories_[id] = categories ;
93+ categories_[id] = { use_categories-> begin (), use_categories-> end () } ;
8094
8195 return AgentWriterHandle (this , id);
8296}
8397
84- void Agent::Stop () {
85- file_writer_. reset ( );
98+ AgentWriterHandle Agent::DefaultHandle () {
99+ return AgentWriterHandle ( this , kDefaultHandleId );
86100}
87101
88102void Agent::StopTracing () {
@@ -99,54 +113,30 @@ void Agent::StopTracing() {
99113}
100114
101115void Agent::Disconnect (int client) {
116+ if (client == kDefaultHandleId ) return ;
102117 ScopedSuspendTracing suspend (tracing_controller_, this );
103118 writers_.erase (client);
104119 categories_.erase (client);
105120}
106121
107- void Agent::Enable (const std::string& categories) {
122+ void Agent::Enable (int id, const std::set<std:: string> & categories) {
108123 if (categories.empty ())
109124 return ;
110- std::set<std::string> categories_set;
111- std::istringstream category_list (categories);
112- while (category_list.good ()) {
113- std::string category;
114- getline (category_list, category, ' ,' );
115- categories_set.emplace (std::move (category));
116- }
117- Enable (categories_set);
118- }
119125
120- void Agent::Enable (const std::set<std::string>& categories) {
121- if (categories.empty ())
122- return ;
123-
124- file_writer_categories_.insert (categories.begin (), categories.end ());
125- std::set<std::string> full_list (file_writer_categories_.begin (),
126- file_writer_categories_.end ());
127- if (file_writer_.empty ()) {
128- // Ensure background thread is running
129- Start ();
130- std::unique_ptr<NodeTraceWriter> writer (
131- new NodeTraceWriter (log_file_pattern_, &tracing_loop_));
132- file_writer_ = AddClient (full_list, std::move (writer));
133- } else {
134- ScopedSuspendTracing suspend (tracing_controller_, this );
135- categories_[file_writer_.id_ ] = full_list;
136- }
126+ ScopedSuspendTracing suspend (tracing_controller_, this ,
127+ id != kDefaultHandleId );
128+ categories_[id].insert (categories.begin (), categories.end ());
137129}
138130
139- void Agent::Disable (const std::set<std::string>& categories) {
131+ void Agent::Disable (int id, const std::set<std::string>& categories) {
132+ ScopedSuspendTracing suspend (tracing_controller_, this ,
133+ id != kDefaultHandleId );
134+ std::multiset<std::string>& writer_categories = categories_[id];
140135 for (const std::string& category : categories) {
141- auto it = file_writer_categories_ .find (category);
142- if (it != file_writer_categories_ .end ())
143- file_writer_categories_ .erase (it);
136+ auto it = writer_categories .find (category);
137+ if (it != writer_categories .end ())
138+ writer_categories .erase (it);
144139 }
145- if (file_writer_.empty ())
146- return ;
147- ScopedSuspendTracing suspend (tracing_controller_, this );
148- categories_[file_writer_.id_ ] = { file_writer_categories_.begin (),
149- file_writer_categories_.end () };
150140}
151141
152142TraceConfig* Agent::CreateTraceConfig () const {
@@ -178,5 +168,6 @@ void Agent::Flush(bool blocking) {
178168 for (const auto & id_writer : writers_)
179169 id_writer.second ->Flush (blocking);
180170}
171+
181172} // namespace tracing
182173} // namespace node
0 commit comments