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: 20 31 64.5 %
Date: 2019-02-01 22:03:38 Branches: 4 12 33.3 %

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
class AsyncTraceWriter {
24
 public:
25
  virtual ~AsyncTraceWriter() {}
26
  virtual void AppendTraceEvent(TraceObject* trace_event) = 0;
27
  virtual void Flush(bool blocking) = 0;
28
  virtual void InitializeOnThread(uv_loop_t* loop) {}
29
};
30
31
328
class TracingController : public v8::platform::tracing::TracingController {
32
 public:
33
164
  TracingController() : v8::platform::tracing::TracingController() {}
34
35
  int64_t CurrentTimestampMicroseconds() override {
36
    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
327
  inline AgentWriterHandle() {}
52
493
  inline ~AgentWriterHandle() { reset(); }
53
54
  inline AgentWriterHandle(AgentWriterHandle&& other);
55
  inline AgentWriterHandle& operator=(AgentWriterHandle&& other);
56
  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
  inline Agent* agent() { return agent_; }
63
64
  inline v8::TracingController* GetTracingController();
65
66
 private:
67
164
  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
250211
  TracingController* GetTracingController() {
84
250211
    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
  void AddMetadataEvent(std::unique_ptr<TraceObject> event);
108
  // Flushes all writers registered through AddClient().
109
  void Flush(bool blocking);
110
111
  TraceConfig* CreateTraceConfig() const;
112
113
 private:
114
  friend class AgentWriterHandle;
115
116
  static void ThreadCb(void* arg);
117
  void InitializeWritersOnThread();
118
119
  void Start();
120
  void StopTracing();
121
  void Disconnect(int client);
122
123
  void Enable(int id, const std::set<std::string>& categories);
124
  void Disable(int id, const std::set<std::string>& categories);
125
126
  uv_thread_t thread_;
127
  uv_loop_t tracing_loop_;
128
129
  bool started_ = false;
130
  class ScopedSuspendTracing;
131
132
  // Each individual Writer has one id.
133
  int next_writer_id_ = 1;
134
  enum { kDefaultHandleId = -1 };
135
  // These maps store the original arguments to AddClient(), by id.
136
  std::unordered_map<int, std::multiset<std::string>> categories_;
137
  std::unordered_map<int, std::unique_ptr<AsyncTraceWriter>> writers_;
138
  std::unique_ptr<TracingController> tracing_controller_;
139
140
  // Variables related to initializing per-event-loop properties of individual
141
  // writers, such as libuv handles.
142
  Mutex initialize_writer_mutex_;
143
  ConditionVariable initialize_writer_condvar_;
144
  uv_async_t initialize_writer_async_;
145
  std::set<AsyncTraceWriter*> to_be_initialized_;
146
147
  Mutex metadata_events_mutex_;
148
  std::list<std::unique_ptr<TraceObject>> metadata_events_;
149
};
150
151
986
void AgentWriterHandle::reset() {
152
986
  if (agent_ != nullptr)
153
165
    agent_->Disconnect(id_);
154
986
  agent_ = nullptr;
155
986
}
156
157
165
AgentWriterHandle& AgentWriterHandle::operator=(AgentWriterHandle&& other) {
158
165
  reset();
159
165
  agent_ = other.agent_;
160
165
  id_ = other.id_;
161
165
  other.agent_ = nullptr;
162
165
  return *this;
163
}
164
165
AgentWriterHandle::AgentWriterHandle(AgentWriterHandle&& other) {
166
  *this = std::move(other);
167
}
168
169
void AgentWriterHandle::Enable(const std::set<std::string>& categories) {
170
  if (agent_ != nullptr) agent_->Enable(id_, categories);
171
}
172
173
void AgentWriterHandle::Disable(const std::set<std::string>& categories) {
174
  if (agent_ != nullptr) agent_->Disable(id_, categories);
175
}
176
177
318
inline v8::TracingController* AgentWriterHandle::GetTracingController() {
178
318
  return agent_ != nullptr ? agent_->GetTracingController() : nullptr;
179
}
180
181
}  // namespace tracing
182
}  // namespace node
183
184
#endif  // SRC_TRACING_AGENT_H_