GCC Code Coverage Report
Directory: ../ Exec Total Coverage
File: /home/iojs/build/workspace/node-test-commit-linux-coverage/nodes/benchmark/out/../src/tracing/agent.h Lines: 36 36 100.0 %
Date: 2019-01-07 12:15:22 Branches: 8 12 66.7 %

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
67
class AsyncTraceWriter {
24
 public:
25
67
  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
7122
class TracingController : public v8::platform::tracing::TracingController {
32
 public:
33
3562
  TracingController() : v8::platform::tracing::TracingController() {}
34
35
2919
  int64_t CurrentTimestampMicroseconds() override {
36
2919
    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
3803
  inline AgentWriterHandle() {}
52
7363
  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
 private:
67
3560
  inline AgentWriterHandle(Agent* agent, int id) : agent_(agent), id_(id) {}
68
69
  AgentWriterHandle(const AgentWriterHandle& other) = delete;
70
  AgentWriterHandle& operator=(const AgentWriterHandle& other) = delete;
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
1106539
  TracingController* GetTracingController() {
84
1106539
    return tracing_controller_.get();
85
  }
86
87
  enum UseDefaultCategoryMode {
88
    kUseDefaultCategories,
89
    kIgnoreDefaultCategories
90
  };
91
92
  // Destroying the handle disconnects the client
93
  AgentWriterHandle AddClient(const std::set<std::string>& categories,
94
                              std::unique_ptr<AsyncTraceWriter> writer,
95
                              enum UseDefaultCategoryMode mode);
96
  // A handle that is only used for managing the default categories
97
  // (which can then implicitly be used through using `USE_DEFAULT_CATEGORIES`
98
  // when adding a client later).
99
  AgentWriterHandle DefaultHandle();
100
101
  // Returns a comma-separated list of enabled categories.
102
  std::string GetEnabledCategories() const;
103
104
  // Writes to all writers registered through AddClient().
105
  void AppendTraceEvent(TraceObject* trace_event);
106
107
592
  void AddMetadataEvent(std::unique_ptr<TraceObject> event) {
108
592
    metadata_events_.push_back(std::move(event));
109
593
  }
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
  static void ThreadCb(void* arg);
119
  void InitializeWritersOnThread();
120
121
  void Start();
122
  void StopTracing();
123
  void Disconnect(int client);
124
125
  void Enable(int id, const std::set<std::string>& categories);
126
  void Disable(int id, const std::set<std::string>& categories);
127
128
  uv_thread_t thread_;
129
  uv_loop_t tracing_loop_;
130
131
  bool started_ = false;
132
  class ScopedSuspendTracing;
133
134
  // Each individual Writer has one id.
135
  int next_writer_id_ = 1;
136
  enum { kDefaultHandleId = -1 };
137
  // These maps store the original arguments to AddClient(), by id.
138
  std::unordered_map<int, std::multiset<std::string>> categories_;
139
  std::unordered_map<int, std::unique_ptr<AsyncTraceWriter>> writers_;
140
  std::unique_ptr<TracingController> tracing_controller_;
141
142
  // Variables related to initializing per-event-loop properties of individual
143
  // writers, such as libuv handles.
144
  Mutex initialize_writer_mutex_;
145
  ConditionVariable initialize_writer_condvar_;
146
  uv_async_t initialize_writer_async_;
147
  std::set<AsyncTraceWriter*> to_be_initialized_;
148
  std::list<std::unique_ptr<TraceObject>> metadata_events_;
149
};
150
151
14567
void AgentWriterHandle::reset() {
152
14567
  if (agent_ != nullptr)
153
3560
    agent_->Disconnect(id_);
154
14567
  agent_ = nullptr;
155
14567
}
156
157
3560
AgentWriterHandle& AgentWriterHandle::operator=(AgentWriterHandle&& other) {
158
3560
  reset();
159
3560
  agent_ = other.agent_;
160
3560
  id_ = other.id_;
161
3560
  other.agent_ = nullptr;
162
3560
  return *this;
163
}
164
165
AgentWriterHandle::AgentWriterHandle(AgentWriterHandle&& other) {
166
  *this = std::move(other);
167
}
168
169
16
void AgentWriterHandle::Enable(const std::set<std::string>& categories) {
170
16
  if (agent_ != nullptr) agent_->Enable(id_, categories);
171
16
}
172
173
4
void AgentWriterHandle::Disable(const std::set<std::string>& categories) {
174
4
  if (agent_ != nullptr) agent_->Disable(id_, categories);
175
4
}
176
177
7023
inline v8::TracingController* AgentWriterHandle::GetTracingController() {
178
7023
  return agent_ != nullptr ? agent_->GetTracingController() : nullptr;
179
}
180
181
}  // namespace tracing
182
}  // namespace node
183
184
#endif  // SRC_TRACING_AGENT_H_