summaryrefslogtreecommitdiff
path: root/src/tracing/agent.cc
blob: 9cc21863e3ed421ae1a1788a6e9830f3af1dd0b8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#include "tracing/agent.h"

#include <sstream>
#include <string>
#include "tracing/node_trace_buffer.h"
#include "tracing/node_trace_writer.h"

namespace node {
namespace tracing {

namespace {

class ScopedSuspendTracing {
 public:
  ScopedSuspendTracing(TracingController* controller, Agent* agent)
                       : controller_(controller), agent_(agent) {
    controller->StopTracing();
  }

  ~ScopedSuspendTracing() {
    TraceConfig* config = agent_->CreateTraceConfig();
    if (config != nullptr) {
      controller_->StartTracing(config);
    }
  }

 private:
  TracingController* controller_;
  Agent* agent_;
};

std::set<std::string> flatten(
    const std::unordered_map<int, std::set<std::string>>& map) {
  std::set<std::string> result;
  for (const auto& id_value : map)
    result.insert(id_value.second.begin(), id_value.second.end());
  return result;
}

}  // namespace

using v8::platform::tracing::TraceConfig;
using v8::platform::tracing::TraceWriter;
using std::string;

Agent::Agent(const std::string& log_file_pattern)
    : log_file_pattern_(log_file_pattern) {
  tracing_controller_ = new TracingController();
  tracing_controller_->Initialize(nullptr);
}

void Agent::Start() {
  if (started_)
    return;

  CHECK_EQ(uv_loop_init(&tracing_loop_), 0);

  NodeTraceBuffer* trace_buffer_ = new NodeTraceBuffer(
      NodeTraceBuffer::kBufferChunks, this, &tracing_loop_);
  tracing_controller_->Initialize(trace_buffer_);

  // This thread should be created *after* async handles are created
  // (within NodeTraceWriter and NodeTraceBuffer constructors).
  // Otherwise the thread could shut down prematurely.
  CHECK_EQ(0, uv_thread_create(&thread_, [](void* arg) {
    Agent* agent = static_cast<Agent*>(arg);
    uv_run(&agent->tracing_loop_, UV_RUN_DEFAULT);
  }, this));
  started_ = true;
}

AgentWriterHandle Agent::AddClient(
    const std::set<std::string>& categories,
    std::unique_ptr<AsyncTraceWriter> writer) {
  Start();
  ScopedSuspendTracing suspend(tracing_controller_, this);
  int id = next_writer_id_++;
  writers_[id] = std::move(writer);
  categories_[id] = categories;

  return AgentWriterHandle(this, id);
}

void Agent::Stop() {
  file_writer_.reset();
}

void Agent::StopTracing() {
  if (!started_)
    return;
  // Perform final Flush on TraceBuffer. We don't want the tracing controller
  // to flush the buffer again on destruction of the V8::Platform.
  tracing_controller_->StopTracing();
  tracing_controller_->Initialize(nullptr);
  started_ = false;

  // Thread should finish when the tracing loop is stopped.
  uv_thread_join(&thread_);
}

void Agent::Disconnect(int client) {
  ScopedSuspendTracing suspend(tracing_controller_, this);
  writers_.erase(client);
  categories_.erase(client);
}

void Agent::Enable(const std::string& categories) {
  if (categories.empty())
    return;
  std::set<std::string> categories_set;
  std::istringstream category_list(categories);
  while (category_list.good()) {
    std::string category;
    getline(category_list, category, ',');
    categories_set.emplace(std::move(category));
  }
  Enable(categories_set);
}

void Agent::Enable(const std::set<std::string>& categories) {
  if (categories.empty())
    return;

  file_writer_categories_.insert(categories.begin(), categories.end());
  std::set<std::string> full_list(file_writer_categories_.begin(),
                                  file_writer_categories_.end());
  if (file_writer_.empty()) {
    // Ensure background thread is running
    Start();
    std::unique_ptr<NodeTraceWriter> writer(
        new NodeTraceWriter(log_file_pattern_, &tracing_loop_));
    file_writer_ = AddClient(full_list, std::move(writer));
  } else {
    ScopedSuspendTracing suspend(tracing_controller_, this);
    categories_[file_writer_.id_] = full_list;
  }
}

void Agent::Disable(const std::set<std::string>& categories) {
  for (const std::string& category : categories) {
    auto it = file_writer_categories_.find(category);
    if (it != file_writer_categories_.end())
      file_writer_categories_.erase(it);
  }
  if (file_writer_.empty())
    return;
  ScopedSuspendTracing suspend(tracing_controller_, this);
  categories_[file_writer_.id_] = { file_writer_categories_.begin(),
                                    file_writer_categories_.end() };
}

TraceConfig* Agent::CreateTraceConfig() const {
  if (categories_.empty())
    return nullptr;
  TraceConfig* trace_config = new TraceConfig();
  for (const auto& category : flatten(categories_)) {
    trace_config->AddIncludedCategory(category.c_str());
  }
  return trace_config;
}

std::string Agent::GetEnabledCategories() const {
  std::string categories;
  for (const std::string& category : flatten(categories_)) {
    if (!categories.empty())
      categories += ',';
    categories += category;
  }
  return categories;
}

void Agent::AppendTraceEvent(TraceObject* trace_event) {
  for (const auto& id_writer : writers_)
    id_writer.second->AppendTraceEvent(trace_event);
}

void Agent::Flush(bool blocking) {
  for (const auto& id_writer : writers_)
    id_writer.second->Flush(blocking);
}
}  // namespace tracing
}  // namespace node