call_stack_profile_metadata.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. // Copyright 2019 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 COMPONENTS_METRICS_CALL_STACK_PROFILE_METADATA_H_
  5. #define COMPONENTS_METRICS_CALL_STACK_PROFILE_METADATA_H_
  6. #include <map>
  7. #include <unordered_map>
  8. #include <utility>
  9. #include "base/profiler/metadata_recorder.h"
  10. #include "third_party/abseil-cpp/absl/types/optional.h"
  11. #include "third_party/metrics_proto/sampled_profile.pb.h"
  12. namespace metrics {
  13. // Helper class for maintaining metadata state across samples and generating
  14. // metadata proto messages.
  15. class CallStackProfileMetadata {
  16. public:
  17. CallStackProfileMetadata();
  18. ~CallStackProfileMetadata();
  19. CallStackProfileMetadata(const CallStackProfileMetadata& other) = delete;
  20. CallStackProfileMetadata& operator=(const CallStackProfileMetadata& other) =
  21. delete;
  22. // Records the metadata for the next sample.
  23. void RecordMetadata(
  24. const base::MetadataRecorder::MetadataProvider& metadata_provider);
  25. // Creates MetadataItems for the currently active metadata, adding new name
  26. // hashes to |metadata_name_hashes| if necessary. The same
  27. // |metadata_name_hashes| must be passed to each invocation, and must not be
  28. // modified outside this function.
  29. google::protobuf::RepeatedPtrField<CallStackProfile::MetadataItem>
  30. CreateSampleMetadata(
  31. google::protobuf::RepeatedField<uint64_t>* metadata_name_hashes);
  32. // Applies the |item| to the samples between |begin| and |end| in
  33. // |stack_samples|. Overwrites any existing metadata item with the same key
  34. // and value that are already applied to the samples.
  35. void ApplyMetadata(
  36. const base::MetadataRecorder::Item& item,
  37. google::protobuf::RepeatedPtrField<
  38. CallStackProfile::StackSample>::iterator begin,
  39. google::protobuf::RepeatedPtrField<
  40. CallStackProfile::StackSample>::iterator end,
  41. google::protobuf::RepeatedPtrField<CallStackProfile::StackSample>*
  42. stack_samples,
  43. google::protobuf::RepeatedField<uint64_t>* metadata_name_hashes);
  44. private:
  45. // Comparison function for the metadata map.
  46. struct MetadataKey;
  47. struct MetadataKeyCompare {
  48. bool operator()(const MetadataKey& a, const MetadataKey& b) const;
  49. };
  50. // Definitions for a map-based representation of sample metadata.
  51. struct MetadataKey {
  52. MetadataKey(uint64_t name_hash, absl::optional<int64_t> key);
  53. MetadataKey(const MetadataKey& other);
  54. MetadataKey& operator=(const MetadataKey& other);
  55. // The name_hash and optional user-specified key uniquely identifies a
  56. // metadata value. See base::MetadataRecorder for details.
  57. uint64_t name_hash;
  58. absl::optional<int64_t> key;
  59. };
  60. using MetadataMap = std::map<MetadataKey, int64_t, MetadataKeyCompare>;
  61. // Creates the metadata map from the array of items.
  62. MetadataMap CreateMetadataMap(base::MetadataRecorder::ItemArray items,
  63. size_t item_count);
  64. // Returns all metadata items with new values in the current sample.
  65. MetadataMap GetNewOrModifiedMetadataItems(const MetadataMap& current_items,
  66. const MetadataMap& previous_items);
  67. // Returns all metadata items deleted since the previous sample.
  68. MetadataMap GetDeletedMetadataItems(const MetadataMap& current_items,
  69. const MetadataMap& previous_items);
  70. // Appends the |name_hash| to |name_hashes| if it's not already
  71. // present. Returns its index in |name_hashes|.
  72. size_t MaybeAppendNameHash(
  73. uint64_t name_hash,
  74. google::protobuf::RepeatedField<uint64_t>* metadata_name_hashes);
  75. // The data provided for the next sample.
  76. base::MetadataRecorder::ItemArray metadata_items_;
  77. size_t metadata_item_count_ = 0;
  78. // The data provided for the previous sample.
  79. MetadataMap previous_items_;
  80. // Maps metadata hash to index in |metadata_name_hash| array.
  81. std::unordered_map<uint64_t, int> metadata_hashes_cache_;
  82. };
  83. } // namespace metrics
  84. #endif // COMPONENTS_METRICS_CALL_STACK_PROFILE_METADATA_H_