trace_buffer.h 4.1 KB

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