diff options
author | misterpoe <raymondksi@gmail.com> | 2016-08-05 14:04:25 -0700 |
---|---|---|
committer | Matt Loring <mattloring@google.com> | 2016-12-20 12:31:09 -0800 |
commit | ba4847e879424ad173289e8fb96cc86a09ee899b (patch) | |
tree | a0d4d9135b3f3872325517a226bb6bcd7b751923 /src/tracing/node_trace_buffer.h | |
parent | 613798335c4313176dfa9f3bafc1fada82293428 (diff) | |
download | android-node-v8-ba4847e879424ad173289e8fb96cc86a09ee899b.tar.gz android-node-v8-ba4847e879424ad173289e8fb96cc86a09ee899b.tar.bz2 android-node-v8-ba4847e879424ad173289e8fb96cc86a09ee899b.zip |
src: Node Tracing Controller
This commit adds support for trace-event tracing to Node.js. It provides
a mechanism to centralize tracing information generated by V8, Node
core, and userspace code. It includes:
- A trace writer responsible for serializing traces and cycling the
output files so that no individual file becomes to large.
- A buffer for aggregating traces to allow for batched flushes.
- An agent which initializes the tracing controller and ensures that
trace serialization is done on a separate thread.
- A set of macros for generating trace events.
- Tests and documentation.
Author: Raymond Kang <raymondksi@gmail.com>
Author: Kelvin Jin <kelvinjin@google.com>
Author: Matthew Loring <mattloring@google.com>
Author: Jason Ginchereau <jasongin@microsoft.com>
PR-URL: https://github.com/nodejs/node/pull/9304
Reviewed-By: Trevor Norris <trev.norris@gmail.com>
Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
Reviewed-By: Josh Gavant <josh.gavant@outlook.com>
Diffstat (limited to 'src/tracing/node_trace_buffer.h')
-rw-r--r-- | src/tracing/node_trace_buffer.h | 89 |
1 files changed, 89 insertions, 0 deletions
diff --git a/src/tracing/node_trace_buffer.h b/src/tracing/node_trace_buffer.h new file mode 100644 index 0000000000..619799fdb2 --- /dev/null +++ b/src/tracing/node_trace_buffer.h @@ -0,0 +1,89 @@ +#ifndef SRC_NODE_TRACE_BUFFER_H_ +#define SRC_NODE_TRACE_BUFFER_H_ + +#include "node_mutex.h" +#include "tracing/node_trace_writer.h" +#include "libplatform/v8-tracing.h" + +#include <atomic> + +namespace node { +namespace tracing { + +using v8::platform::tracing::TraceBuffer; +using v8::platform::tracing::TraceBufferChunk; +using v8::platform::tracing::TraceObject; + +// forward declaration +class NodeTraceBuffer; + +class InternalTraceBuffer { + public: + InternalTraceBuffer(size_t max_chunks, uint32_t id, + NodeTraceWriter* trace_writer, + NodeTraceBuffer* external_buffer); + + TraceObject* AddTraceEvent(uint64_t* handle); + TraceObject* GetEventByHandle(uint64_t handle); + void Flush(bool blocking); + bool IsFull() const { + return total_chunks_ == max_chunks_ && chunks_[total_chunks_ - 1]->IsFull(); + } + bool IsFlushing() const { + return flushing_; + } + + private: + uint64_t MakeHandle(size_t chunk_index, uint32_t chunk_seq, + size_t event_index) const; + void ExtractHandle(uint64_t handle, uint32_t* buffer_id, size_t* chunk_index, + uint32_t* chunk_seq, size_t* event_index) const; + size_t Capacity() const { return max_chunks_ * TraceBufferChunk::kChunkSize; } + + Mutex mutex_; + bool flushing_; + size_t max_chunks_; + NodeTraceWriter* trace_writer_; + NodeTraceBuffer* external_buffer_; + std::vector<std::unique_ptr<TraceBufferChunk>> chunks_; + size_t total_chunks_ = 0; + uint32_t current_chunk_seq_ = 1; + uint32_t id_; +}; + +class NodeTraceBuffer : public TraceBuffer { + public: + NodeTraceBuffer(size_t max_chunks, NodeTraceWriter* trace_writer, + uv_loop_t* tracing_loop); + ~NodeTraceBuffer(); + + TraceObject* AddTraceEvent(uint64_t* handle) override; + TraceObject* GetEventByHandle(uint64_t handle) override; + bool Flush() override; + + static const size_t kBufferChunks = 1024; + + private: + bool TryLoadAvailableBuffer(); + static void NonBlockingFlushSignalCb(uv_async_t* signal); + static void ExitSignalCb(uv_async_t* signal); + + uv_loop_t* tracing_loop_; + uv_async_t flush_signal_; + uv_async_t exit_signal_; + bool exited_ = false; + // Used exclusively for exit logic. + Mutex exit_mutex_; + // Used to wait until async handles have been closed. + ConditionVariable exit_cond_; + std::unique_ptr<NodeTraceWriter> trace_writer_; + // TODO: Change std::atomic to something less contentious. + std::atomic<InternalTraceBuffer*> current_buf_; + InternalTraceBuffer buffer1_; + InternalTraceBuffer buffer2_; +}; + +} // namespace tracing +} // namespace node + +#endif // SRC_NODE_TRACING_CONTROLLER_H_ |