trace_buffer.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. // Copyright 2015 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef BASE_TRACE_EVENT_TRACE_BUFFER_H_
  5. #define BASE_TRACE_EVENT_TRACE_BUFFER_H_
  6. #include <stddef.h>
  7. #include <stdint.h>
  8. #include "base/base_export.h"
  9. #include "base/trace_event/trace_event.h"
  10. #include "base/trace_event/trace_event_impl.h"
  11. namespace base {
  12. namespace trace_event {
  13. // TraceBufferChunk is the basic unit of TraceBuffer.
  14. class BASE_EXPORT TraceBufferChunk {
  15. public:
  16. explicit TraceBufferChunk(uint32_t seq);
  17. ~TraceBufferChunk();
  18. void Reset(uint32_t new_seq);
  19. TraceEvent* AddTraceEvent(size_t* event_index);
  20. bool IsFull() const { return next_free_ == kTraceBufferChunkSize; }
  21. uint32_t seq() const { return seq_; }
  22. size_t capacity() const { return kTraceBufferChunkSize; }
  23. size_t size() const { return next_free_; }
  24. TraceEvent* GetEventAt(size_t index) {
  25. DCHECK(index < size());
  26. return &chunk_[index];
  27. }
  28. const TraceEvent* GetEventAt(size_t index) const {
  29. DCHECK(index < size());
  30. return &chunk_[index];
  31. }
  32. void EstimateTraceMemoryOverhead(TraceEventMemoryOverhead* overhead);
  33. // These values must be kept consistent with the numbers of bits of
  34. // chunk_index and event_index fields in TraceEventHandle
  35. // (in trace_event_impl.h).
  36. static const size_t kMaxChunkIndex = (1u << 26) - 1;
  37. static const size_t kTraceBufferChunkSize = 64;
  38. private:
  39. size_t next_free_;
  40. std::unique_ptr<TraceEventMemoryOverhead> cached_overhead_estimate_;
  41. TraceEvent chunk_[kTraceBufferChunkSize];
  42. uint32_t seq_;
  43. };
  44. // TraceBuffer holds the events as they are collected.
  45. class BASE_EXPORT TraceBuffer {
  46. public:
  47. virtual ~TraceBuffer() = default;
  48. virtual std::unique_ptr<TraceBufferChunk> GetChunk(size_t* index) = 0;
  49. virtual void ReturnChunk(size_t index,
  50. std::unique_ptr<TraceBufferChunk> chunk) = 0;
  51. virtual bool IsFull() const = 0;
  52. virtual size_t Size() const = 0;
  53. virtual size_t Capacity() const = 0;
  54. virtual TraceEvent* GetEventByHandle(TraceEventHandle handle) = 0;
  55. // For iteration. Each TraceBuffer can only be iterated once.
  56. virtual const TraceBufferChunk* NextChunk() = 0;
  57. // Computes an estimate of the size of the buffer, including all the retained
  58. // objects.
  59. virtual void EstimateTraceMemoryOverhead(
  60. TraceEventMemoryOverhead* overhead) = 0;
  61. static TraceBuffer* CreateTraceBufferRingBuffer(size_t max_chunks);
  62. static TraceBuffer* CreateTraceBufferVectorOfSize(size_t max_chunks);
  63. };
  64. // TraceResultBuffer collects and converts trace fragments returned by TraceLog
  65. // to JSON output.
  66. class BASE_EXPORT TraceResultBuffer {
  67. public:
  68. using OutputCallback = base::RepeatingCallback<void(const std::string&)>;
  69. // If you don't need to stream JSON chunks out efficiently, and just want to
  70. // get a complete JSON string after calling Finish, use this struct to collect
  71. // JSON trace output.
  72. struct BASE_EXPORT SimpleOutput {
  73. OutputCallback GetCallback();
  74. void Append(const std::string& json_string);
  75. // Do what you want with the json_output_ string after calling
  76. // TraceResultBuffer::Finish.
  77. std::string json_output;
  78. };
  79. TraceResultBuffer();
  80. ~TraceResultBuffer();
  81. // Set callback. The callback will be called during Start with the initial
  82. // JSON output and during AddFragment and Finish with following JSON output
  83. // chunks. The callback target must live past the last calls to
  84. // TraceResultBuffer::Start/AddFragment/Finish.
  85. void SetOutputCallback(OutputCallback json_chunk_callback);
  86. // Start JSON output. This resets all internal state, so you can reuse
  87. // the TraceResultBuffer by calling Start.
  88. void Start();
  89. // Call AddFragment 0 or more times to add trace fragments from TraceLog.
  90. void AddFragment(const std::string& trace_fragment);
  91. // When all fragments have been added, call Finish to complete the JSON
  92. // formatted output.
  93. void Finish();
  94. private:
  95. OutputCallback output_callback_;
  96. bool append_comma_;
  97. };
  98. } // namespace trace_event
  99. } // namespace base
  100. #endif // BASE_TRACE_EVENT_TRACE_BUFFER_H_