profiledata.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. // -*- Mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*-
  2. // Copyright (c) 2007, Google Inc.
  3. // All rights reserved.
  4. //
  5. // Redistribution and use in source and binary forms, with or without
  6. // modification, are permitted provided that the following conditions are
  7. // met:
  8. //
  9. // * Redistributions of source code must retain the above copyright
  10. // notice, this list of conditions and the following disclaimer.
  11. // * Redistributions in binary form must reproduce the above
  12. // copyright notice, this list of conditions and the following disclaimer
  13. // in the documentation and/or other materials provided with the
  14. // distribution.
  15. // * Neither the name of Google Inc. nor the names of its
  16. // contributors may be used to endorse or promote products derived from
  17. // this software without specific prior written permission.
  18. //
  19. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  23. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  25. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  26. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  27. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. //
  31. // ---
  32. // Author: Sanjay Ghemawat
  33. // Chris Demetriou (refactoring)
  34. //
  35. // Collect profiling data.
  36. //
  37. // The profile data file format is documented in
  38. // doc/cpuprofile-fileformat.html
  39. #ifndef BASE_PROFILEDATA_H_
  40. #define BASE_PROFILEDATA_H_
  41. #include <config.h>
  42. #include <time.h> // for time_t
  43. #include <stdint.h>
  44. #include "base/basictypes.h"
  45. // A class that accumulates profile samples and writes them to a file.
  46. //
  47. // Each sample contains a stack trace and a count. Memory usage is
  48. // reduced by combining profile samples that have the same stack trace
  49. // by adding up the associated counts.
  50. //
  51. // Profile data is accumulated in a bounded amount of memory, and will
  52. // flushed to a file as necessary to stay within the memory limit.
  53. //
  54. // Use of this class assumes external synchronization. The exact
  55. // requirements of that synchronization are that:
  56. //
  57. // - 'Add' may be called from asynchronous signals, but is not
  58. // re-entrant.
  59. //
  60. // - None of 'Start', 'Stop', 'Reset', 'Flush', and 'Add' may be
  61. // called at the same time.
  62. //
  63. // - 'Start', 'Stop', or 'Reset' should not be called while 'Enabled'
  64. // or 'GetCurrent' are running, and vice versa.
  65. //
  66. // A profiler which uses asyncronous signals to add samples will
  67. // typically use two locks to protect this data structure:
  68. //
  69. // - A SpinLock which is held over all calls except for the 'Add'
  70. // call made from the signal handler.
  71. //
  72. // - A SpinLock which is held over calls to 'Start', 'Stop', 'Reset',
  73. // 'Flush', and 'Add'. (This SpinLock should be acquired after
  74. // the first SpinLock in all cases where both are needed.)
  75. class ProfileData {
  76. public:
  77. struct State {
  78. bool enabled; // Is profiling currently enabled?
  79. time_t start_time; // If enabled, when was profiling started?
  80. char profile_name[1024]; // Name of file being written, or '\0'
  81. int samples_gathered; // Number of samples gathered to far (or 0)
  82. };
  83. class Options {
  84. public:
  85. Options();
  86. // Get and set the sample frequency.
  87. int frequency() const {
  88. return frequency_;
  89. }
  90. void set_frequency(int frequency) {
  91. frequency_ = frequency;
  92. }
  93. private:
  94. int frequency_; // Sample frequency.
  95. };
  96. static const int kMaxStackDepth = 64; // Max stack depth stored in profile
  97. ProfileData();
  98. ~ProfileData();
  99. // If data collection is not already enabled start to collect data
  100. // into fname. Parameters related to this profiling run are specified
  101. // by 'options'.
  102. //
  103. // Returns true if data collection could be started, otherwise (if an
  104. // error occurred or if data collection was already enabled) returns
  105. // false.
  106. bool Start(const char *fname, const Options& options);
  107. // If data collection is enabled, stop data collection and write the
  108. // data to disk.
  109. void Stop();
  110. // Stop data collection without writing anything else to disk, and
  111. // discard any collected data.
  112. void Reset();
  113. // If data collection is enabled, record a sample with 'depth'
  114. // entries from 'stack'. (depth must be > 0.) At most
  115. // kMaxStackDepth stack entries will be recorded, starting with
  116. // stack[0].
  117. //
  118. // This function is safe to call from asynchronous signals (but is
  119. // not re-entrant).
  120. void Add(int depth, const void* const* stack);
  121. // If data collection is enabled, write the data to disk (and leave
  122. // the collector enabled).
  123. void FlushTable();
  124. // Is data collection currently enabled?
  125. bool enabled() const { return out_ >= 0; }
  126. // Get the current state of the data collector.
  127. void GetCurrentState(State* state) const;
  128. private:
  129. static const int kAssociativity = 4; // For hashtable
  130. static const int kBuckets = 1 << 10; // For hashtable
  131. static const int kBufferLength = 1 << 18; // For eviction buffer
  132. // Type of slots: each slot can be either a count, or a PC value
  133. typedef uintptr_t Slot;
  134. // Hash-table/eviction-buffer entry (a.k.a. a sample)
  135. struct Entry {
  136. Slot count; // Number of hits
  137. Slot depth; // Stack depth
  138. Slot stack[kMaxStackDepth]; // Stack contents
  139. };
  140. // Hash table bucket
  141. struct Bucket {
  142. Entry entry[kAssociativity];
  143. };
  144. Bucket* hash_; // hash table
  145. Slot* evict_; // evicted entries
  146. int num_evicted_; // how many evicted entries?
  147. int out_; // fd for output file.
  148. int count_; // How many samples recorded
  149. int evictions_; // How many evictions
  150. size_t total_bytes_; // How much output
  151. char* fname_; // Profile file name
  152. time_t start_time_; // Start time, or 0
  153. // Move 'entry' to the eviction buffer.
  154. void Evict(const Entry& entry);
  155. // Write contents of eviction buffer to disk.
  156. void FlushEvicted();
  157. DISALLOW_COPY_AND_ASSIGN(ProfileData);
  158. };
  159. #endif // BASE_PROFILEDATA_H_