GCC Code Coverage Report
Directory: ../ Exec Total Coverage
File: /home/iojs/build/workspace/node-test-commit-linux-coverage-daily/nodes/benchmark/out/../src/tracing/agent.h Lines: 35 35 100.0 %
Date: 2019-02-13 22:28:58 Branches: 8 14 57.1 %

Line Branch Exec Source
1
#ifndef SRC_TRACING_AGENT_H_
2
#define SRC_TRACING_AGENT_H_
3
4
#include "libplatform/v8-tracing.h"
5
#include "uv.h"
6
#include "v8.h"
7
#include "util.h"
8
#include "node_mutex.h"
9
10
#include <list>
11
#include <set>
12
#include <string>
13
#include <unordered_map>
14
15
namespace node {
16
namespace tracing {
17
18
using v8::platform::tracing::TraceConfig;
19
using v8::platform::tracing::TraceObject;
20
21
class Agent;
22
23
68
class AsyncTraceWriter {
24
 public:
25
68
  virtual ~AsyncTraceWriter() {}
26
  virtual void AppendTraceEvent(TraceObject* trace_event) = 0;
27
  virtual void Flush(bool blocking) = 0;
28
2
  virtual void InitializeOnThread(uv_loop_t* loop) {}
29
};
30
31
8538
class TracingController : public v8::platform::tracing::TracingController {
32
 public:
33
4269
  TracingController() : v8::platform::tracing::TracingController() {}
34
35
3519
  int64_t CurrentTimestampMicroseconds() override {
36
3519
    return uv_hrtime() / 1000;
37
  }
38
  void AddMetadataEvent(
39
      const unsigned char* category_group_enabled,
40
      const char* name,
41
      int num_args,
42
      const char** arg_names,
43
      const unsigned char* arg_types,
44
      const uint64_t* arg_values,
45
      std::unique_ptr<v8::ConvertableToTraceFormat>* convertable_values,
46
      unsigned int flags);
47
};
48
49
class AgentWriterHandle {
50
 public:
51
8932
  inline AgentWriterHandle() {}
52
13197
  inline ~AgentWriterHandle() { reset(); }
53
54
  inline AgentWriterHandle(AgentWriterHandle&& other);
55
  inline AgentWriterHandle& operator=(AgentWriterHandle&& other);
56
3
  inline bool empty() const { return agent_ == nullptr; }
57
  inline void reset();
58
59
  inline void Enable(const std::set<std::string>& categories);
60
  inline void Disable(const std::set<std::string>& categories);
61
62
14
  inline Agent* agent() { return agent_; }
63
64
  inline v8::TracingController* GetTracingController();
65
66
  AgentWriterHandle(const AgentWriterHandle& other) = delete;
67
  AgentWriterHandle& operator=(const AgentWriterHandle& other) = delete;
68
69
 private:
70
4271
  inline AgentWriterHandle(Agent* agent, int id) : agent_(agent), id_(id) {}
71
72
  Agent* agent_ = nullptr;
73
  int id_;
74
75
  friend class Agent;
76
};
77
78
class Agent {
79
 public:
80
  Agent();
81
  ~Agent();
82
83
5222805
  TracingController* GetTracingController() {
84
5222805
    TracingController* controller = tracing_controller_.get();
85
5222891
    CHECK_NOT_NULL(controller);
86
5222891
    return controller;
87
  }
88
89
  enum UseDefaultCategoryMode {
90
    kUseDefaultCategories,
91
    kIgnoreDefaultCategories
92
  };
93
94
  // Destroying the handle disconnects the client
95
  AgentWriterHandle AddClient(const std::set<std::string>& categories,
96
                              std::unique_ptr<AsyncTraceWriter> writer,
97
                              enum UseDefaultCategoryMode mode);
98
  // A handle that is only used for managing the default categories
99
  // (which can then implicitly be used through using `USE_DEFAULT_CATEGORIES`
100
  // when adding a client later).
101
  AgentWriterHandle DefaultHandle();
102
103
  // Returns a comma-separated list of enabled categories.
104
  std::string GetEnabledCategories() const;
105
106
  // Writes to all writers registered through AddClient().
107
  void AppendTraceEvent(TraceObject* trace_event);
108
109
  void AddMetadataEvent(std::unique_ptr<TraceObject> event);
110
  // Flushes all writers registered through AddClient().
111
  void Flush(bool blocking);
112
113
  TraceConfig* CreateTraceConfig() const;
114
115
 private:
116
  friend class AgentWriterHandle;
117
118
  void InitializeWritersOnThread();
119
120
  void Start();
121
  void StopTracing();
122
  void Disconnect(int client);
123
124
  void Enable(int id, const std::set<std::string>& categories);
125
  void Disable(int id, const std::set<std::string>& categories);
126
127
  uv_thread_t thread_;
128
  uv_loop_t tracing_loop_;
129
130
  bool started_ = false;
131
  class ScopedSuspendTracing;
132
133
  // Each individual Writer has one id.
134
  int next_writer_id_ = 1;
135
  enum { kDefaultHandleId = -1 };
136
  // These maps store the original arguments to AddClient(), by id.
137
  std::unordered_map<int, std::multiset<std::string>> categories_;
138
  std::unordered_map<int, std::unique_ptr<AsyncTraceWriter>> writers_;
139
  std::unique_ptr<TracingController> tracing_controller_;
140
141
  // Variables related to initializing per-event-loop properties of individual
142
  // writers, such as libuv handles.
143
  Mutex initialize_writer_mutex_;
144
  ConditionVariable initialize_writer_condvar_;
145
  uv_async_t initialize_writer_async_;
146
  std::set<AsyncTraceWriter*> to_be_initialized_;
147
148
  Mutex metadata_events_mutex_;
149
  std::list<std::unique_ptr<TraceObject>> metadata_events_;
150
};
151
152
26346
void AgentWriterHandle::reset() {
153
26346
  if (agent_ != nullptr)
154
4272
    agent_->Disconnect(id_);
155
26346
  agent_ = nullptr;
156
26346
}
157
158
4272
AgentWriterHandle& AgentWriterHandle::operator=(AgentWriterHandle&& other) {
159
4272
  reset();
160
4272
  agent_ = other.agent_;
161
4272
  id_ = other.id_;
162
4272
  other.agent_ = nullptr;
163
4272
  return *this;
164
}
165
166
AgentWriterHandle::AgentWriterHandle(AgentWriterHandle&& other) {
167
  *this = std::move(other);
168
}
169
170
16
void AgentWriterHandle::Enable(const std::set<std::string>& categories) {
171
16
  if (agent_ != nullptr) agent_->Enable(id_, categories);
172
16
}
173
174
4
void AgentWriterHandle::Disable(const std::set<std::string>& categories) {
175
4
  if (agent_ != nullptr) agent_->Disable(id_, categories);
176
4
}
177
178
8466
inline v8::TracingController* AgentWriterHandle::GetTracingController() {
179
8466
  return agent_ != nullptr ? agent_->GetTracingController() : nullptr;
180
}
181
182
}  // namespace tracing
183
}  // namespace node
184
185
#endif  // SRC_TRACING_AGENT_H_