Node.js  v8.x
Node.js is a JavaScript runtime built on Chrome's V8 JavaScript engine
node_trace_writer.h
Go to the documentation of this file.
1 #ifndef SRC_TRACING_NODE_TRACE_WRITER_H_
2 #define SRC_TRACING_NODE_TRACE_WRITER_H_
3 
4 #include <sstream>
5 #include <queue>
6 
7 #include "node_mutex.h"
8 #include "libplatform/v8-tracing.h"
9 #include "uv.h"
10 
11 namespace node {
12 namespace tracing {
13 
14 using v8::platform::tracing::TraceObject;
15 using v8::platform::tracing::TraceWriter;
16 using v8::platform::tracing::TracingController;
17 
18 class NodeTraceWriter : public TraceWriter {
19  public:
20  explicit NodeTraceWriter(uv_loop_t* tracing_loop);
22 
23  void AppendTraceEvent(TraceObject* trace_event) override;
24  void Flush() override;
25  void Flush(bool blocking);
26 
27  static const int kTracesPerFile = 1 << 19;
28 
29  private:
30  struct WriteRequest {
31  uv_fs_t req;
32  NodeTraceWriter* writer;
33  std::string str;
34  int highest_request_id;
35  };
36 
37  static void WriteCb(uv_fs_t* req);
38  void OpenNewFileForStreaming();
39  void WriteToFile(std::string&& str, int highest_request_id);
40  void WriteSuffix();
41  static void FlushSignalCb(uv_async_t* signal);
42  void FlushPrivate();
43  static void ExitSignalCb(uv_async_t* signal);
44 
45  uv_loop_t* tracing_loop_;
46  // Triggers callback to initiate writing the contents of stream_ to disk.
47  uv_async_t flush_signal_;
48  // Triggers callback to close async objects, ending the tracing thread.
49  uv_async_t exit_signal_;
50  // Prevents concurrent R/W on state related to serialized trace data
51  // before it's written to disk, namely stream_ and total_traces_.
52  Mutex stream_mutex_;
53  // Prevents concurrent R/W on state related to write requests.
54  Mutex request_mutex_;
55  // Allows blocking calls to Flush() to wait on a condition for
56  // trace events to be written to disk.
57  ConditionVariable request_cond_;
58  // Used to wait until async handles have been closed.
59  ConditionVariable exit_cond_;
60  int fd_ = -1;
61  std::queue<WriteRequest*> write_req_queue_;
62  int num_write_requests_ = 0;
63  int highest_request_id_completed_ = 0;
64  int total_traces_ = 0;
65  int file_num_ = 0;
66  std::ostringstream stream_;
67  TraceWriter* json_trace_writer_ = nullptr;
68  bool exited_ = false;
69 };
70 
71 } // namespace tracing
72 } // namespace node
73 
74 #endif // SRC_TRACING_NODE_TRACE_WRITER_H_
void AppendTraceEvent(TraceObject *trace_event) override
uv_fs_t req
Definition: node_file.cc:374
NodeTraceWriter(uv_loop_t *tracing_loop)