| 6 |
| 7 | #include "base/metrics/metrics_hashes.h" |
| 8 | #include "base/no_destructor.h" |
| 9 | #include "base/profiler/stack_sampling_profiler.h" |
| 10 |
| 11 | namespace base { |
| 12 |
| 13 | ScopedSampleMetadata::ScopedSampleMetadata(StringPiece name, int64_t value) |
| 14 | : name_hash_(HashMetricName(name)) { |
| 15 | GetSampleMetadataRecorder()->Set(name_hash_, nullopt, value); |
| 16 | } |
| 17 |
| 18 | ScopedSampleMetadata::ScopedSampleMetadata(StringPiece name, |
| 19 | int64_t key, |
| 20 | int64_t value) |
| 21 | : name_hash_(HashMetricName(name)), key_(key) { |
| 22 | GetSampleMetadataRecorder()->Set(name_hash_, key, value); |
| 23 | } |
| 24 |
| 25 | ScopedSampleMetadata::~ScopedSampleMetadata() { |
| 26 | GetSampleMetadataRecorder()->Remove(name_hash_, key_); |
| 27 | } |
| 28 |
| 29 | void SetSampleMetadata(StringPiece name, int64_t value) { |
| 30 | GetSampleMetadataRecorder()->Set(HashMetricName(name), nullopt, value); |
| 31 | } |
| 32 |
| 33 | void SetSampleMetadata(StringPiece name, int64_t key, int64_t value) { |
| 34 | GetSampleMetadataRecorder()->Set(HashMetricName(name), key, value); |
| 35 | } |
| 36 |
| 37 | void RemoveSampleMetadata(StringPiece name) { |
| 38 | GetSampleMetadataRecorder()->Remove(HashMetricName(name), nullopt); |
| 39 | } |
| 40 |
| 41 | void RemoveSampleMetadata(StringPiece name, int64_t key) { |
| 42 | GetSampleMetadataRecorder()->Remove(HashMetricName(name), key); |
| 43 | } |
| 44 |
| 45 | // This function is friended by StackSamplingProfiler so must live directly in |
| 46 | // the base namespace. |
| 47 | void ApplyMetadataToPastSamplesImpl(TimeTicks period_start, |
| 48 | TimeTicks period_end, |
| 49 | int64_t name_hash, |
| 50 | Optional<int64_t> key, |
| 51 | int64_t value) { |
| 52 | StackSamplingProfiler::ApplyMetadataToPastSamples(period_start, period_end, |
| 53 | name_hash, key, value); |
| 54 | } |
| 55 |
| 56 | void ApplyMetadataToPastSamples(TimeTicks period_start, |
| 57 | TimeTicks period_end, |
| 58 | StringPiece name, |
| 59 | int64_t value) { |
| 60 | return ApplyMetadataToPastSamplesImpl(period_start, period_end, |
| 61 | HashMetricName(name), nullopt, value); |
| 62 | } |
| 63 |
| 64 | void ApplyMetadataToPastSamples(TimeTicks period_start, |
| 65 | TimeTicks period_end, |
| 66 | StringPiece name, |
| 67 | int64_t key, |
| 68 | int64_t value) { |
| 69 | return ApplyMetadataToPastSamplesImpl(period_start, period_end, |
| 70 | HashMetricName(name), key, value); |
| 71 | } |
| 72 |
| 73 | MetadataRecorder* GetSampleMetadataRecorder() { |
| 74 | static NoDestructor<MetadataRecorder> instance; |
| 75 | return instance.get(); |
| 76 | } |
| 77 |
| 78 | } // namespace base |
| 79 |