| 6 | #define BASE_PROFILER_SAMPLE_METADATA_H_ |
| 7 |
| 8 | #include "base/optional.h" |
| 9 | #include "base/profiler/metadata_recorder.h" |
| 10 | #include "base/strings/string_piece.h" |
| 11 |
| 12 | // ----------------------------------------------------------------------------- |
| 13 | // Usage documentation |
| 14 | // ----------------------------------------------------------------------------- |
| 15 | // |
| 16 | // Overview: |
| 17 | // These functions provide a means to control the metadata attached to samples |
| 18 | // collected by the stack sampling profiler. Metadata state is shared between |
| 19 | // all threads within a process. |
| 20 | // |
| 21 | // Any samples collected by the sampling profiler will include the active |
| 22 | // metadata. This enables us to later analyze targeted subsets of samples |
| 23 | // (e.g. those collected during paint or layout). |
| 24 | // |
| 25 | // For example: |
| 26 | // |
| 27 | // void DidStartLoad() { |
| 28 | // base::SetSampleMetadata("Renderer.IsLoading", 1); |
| 29 | // } |
| 30 | // |
| 31 | // void DidFinishLoad() { |
| 32 | // base::RemoveSampleMetadata("Renderer.IsLoading"); |
| 33 | // } |
| 34 | // |
| 35 | // Alternatively, ScopedSampleMetadata can be used to ensure that the metadata |
| 36 | // is removed correctly. |
| 37 | // |
| 38 | // For example: |
| 39 | // |
| 40 | // void DoExpensiveWork() { |
| 41 | // base::ScopedSampleMetadata metadata("xyz", 1); |
| 42 | // if (...) { |
| 43 | // ... |
| 44 | // if (...) { |
| 45 | // ... |
| 46 | // return; |
| 47 | // } |
| 48 | // } |
| 49 | // ... |
| 50 | // } |
| 51 |
| 52 | namespace base { |
| 53 |
| 54 | class BASE_EXPORT ScopedSampleMetadata { |
| 55 | public: |
| 56 | // Set the metadata value associated with |name|. |
| 57 | ScopedSampleMetadata(StringPiece name, int64_t value); |
| 58 |
| 59 | // Set the metadata value associated with the pair (|name|, |key|). This |
| 60 | // constructor allows the metadata to be associated with an additional |
| 61 | // user-defined key. One might supply a key based on the frame id, for |
| 62 | // example, to distinguish execution in service of scrolling between different |
| 63 | // frames. Prefer the previous constructor if no user-defined metadata is |
| 64 | // required. Note: values specified for a name and key are stored separately |
| 65 | // from values specified with only a name. |
| 66 | ScopedSampleMetadata(StringPiece name, int64_t key, int64_t value); |
| 67 |
| 68 | ScopedSampleMetadata(const ScopedSampleMetadata&) = delete; |
| 69 | ~ScopedSampleMetadata(); |
| 70 |
| 71 | ScopedSampleMetadata& operator=(const ScopedSampleMetadata&) = delete; |
| 72 |
| 73 | private: |
| 74 | const uint64_t name_hash_; |
| 75 | Optional<int64_t> key_; |
| 76 | }; |
| 77 |
| 78 | // Set the metadata value associated with |name| in the process-global stack |
| 79 | // sampling profiler metadata, overwriting any previous value set for that |
| 80 | // |name|. |
| 81 | BASE_EXPORT void SetSampleMetadata(StringPiece name, int64_t value); |
| 82 |
| 83 | // Set the metadata value associated with the pair (|name|, |key|) in the |
| 84 | // process-global stack sampling profiler metadata, overwriting any previous |
| 85 | // value set for that (|name|, |key|) pair. This constructor allows the metadata |
| 86 | // to be associated with an additional user-defined key. One might supply a key |
| 87 | // based on the frame id, for example, to distinguish execution in service of |
| 88 | // scrolling between different frames. Prefer the previous function if no |
| 89 | // user-defined metadata is required. Note: values specified for a name and key |
| 90 | // are stored separately from values specified with only a name. |
| 91 | BASE_EXPORT void SetSampleMetadata(StringPiece name, |
| 92 | int64_t key, |
| 93 | int64_t value); |
| 94 |
| 95 | // Removes the metadata item with the specified name from the process-global |
| 96 | // stack sampling profiler metadata. |
| 97 | // |
| 98 | // If such an item doesn't exist, this has no effect. |
| 99 | BASE_EXPORT void RemoveSampleMetadata(StringPiece name); |
| 100 |
| 101 | // Removes the metadata item with the specified (|name|, |key|) pair from the |
| 102 | // process-global stack sampling profiler metadata. This function does not alter |
| 103 | // values set with the name |name| but no key. |
| 104 | // |
| 105 | // If such an item doesn't exist, this has no effect. |
| 106 | BASE_EXPORT void RemoveSampleMetadata(StringPiece name, int64_t key); |
| 107 |
| 108 | // Applies the specified metadata to samples already recorded between |
| 109 | // |period_start| and |period_end| in all thread's active profiles, subject to |
| 110 | // the condition that the profile fully encompasses the period and the profile |
| 111 | // has not already completed. The condition ensures that the metadata is applied |
| 112 | // only if all execution during its scope was seen in the profile. This avoids |
| 113 | // biasng the samples towards the 'middle' of the execution seen during the |
| 114 | // metadata scope (i.e. because the start or end of execution was missed), at |
| 115 | // the cost of missing execution that are longer than the profiling period, or |
| 116 | // extend before or after it. |period_end| must be <= TimeTicks::Now(). |
| 117 | BASE_EXPORT void ApplyMetadataToPastSamples(TimeTicks period_start, |
| 118 | TimeTicks period_end, |
| 119 | StringPiece name, |
| 120 | int64_t value); |
| 121 | BASE_EXPORT void ApplyMetadataToPastSamples(TimeTicks period_start, |
| 122 | TimeTicks period_end, |
| 123 | StringPiece name, |
| 124 | int64_t key, |
| 125 | int64_t value); |
| 126 |
| 127 | // Returns the process-global metadata recorder instance used for tracking |
| 128 | // sampling profiler metadata. |
| 129 | // |
| 130 | // This function should not be called by non-profiler related code. |
| 131 | BASE_EXPORT MetadataRecorder* GetSampleMetadataRecorder(); |
| 132 |
| 133 | } // namespace base |
| 134 |
| 135 | #endif // BASE_PROFILER_SAMPLE_METADATA_H_ |
| 136 |