File: call_stack_profile_metadata.h

package info (click to toggle)
chromium 120.0.6099.224-1~deb11u1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 6,112,112 kB
  • sloc: cpp: 32,907,025; ansic: 8,148,123; javascript: 3,679,536; python: 2,031,248; asm: 959,718; java: 804,675; xml: 617,256; sh: 111,417; objc: 100,835; perl: 88,443; cs: 53,032; makefile: 29,579; fortran: 24,137; php: 21,162; tcl: 21,147; sql: 20,809; ruby: 17,735; pascal: 12,864; yacc: 8,045; lisp: 3,388; lex: 1,323; ada: 727; awk: 329; jsp: 267; csh: 117; exp: 43; sed: 37
file content (115 lines) | stat: -rw-r--r-- 4,565 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
// Copyright 2019 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef COMPONENTS_METRICS_CALL_STACK_PROFILE_METADATA_H_
#define COMPONENTS_METRICS_CALL_STACK_PROFILE_METADATA_H_

#include <map>
#include <unordered_map>
#include <utility>

#include "base/profiler/metadata_recorder.h"
#include "third_party/abseil-cpp/absl/types/optional.h"
#include "third_party/metrics_proto/sampled_profile.pb.h"

namespace metrics {

// Helper class for maintaining metadata state across samples and generating
// metadata proto messages.
class CallStackProfileMetadata {
 public:
  CallStackProfileMetadata();
  ~CallStackProfileMetadata();

  CallStackProfileMetadata(const CallStackProfileMetadata& other) = delete;
  CallStackProfileMetadata& operator=(const CallStackProfileMetadata& other) =
      delete;

  // Records the metadata for the next sample.
  void RecordMetadata(
      const base::MetadataRecorder::MetadataProvider& metadata_provider);

  // Creates MetadataItems for the currently active metadata, adding new name
  // hashes to |metadata_name_hashes| if necessary. The same
  // |metadata_name_hashes| must be passed to each invocation, and must not be
  // modified outside this function.
  google::protobuf::RepeatedPtrField<CallStackProfile::MetadataItem>
  CreateSampleMetadata(
      google::protobuf::RepeatedField<uint64_t>* metadata_name_hashes);

  // Applies the |item| to the samples between |begin| and |end| in
  // |stack_samples|. Overwrites any existing metadata item with the same key
  // and value that are already applied to the samples.
  void ApplyMetadata(
      const base::MetadataRecorder::Item& item,
      google::protobuf::RepeatedPtrField<
          CallStackProfile::StackSample>::iterator begin,
      google::protobuf::RepeatedPtrField<
          CallStackProfile::StackSample>::iterator end,
      google::protobuf::RepeatedPtrField<CallStackProfile::StackSample>*
          stack_samples,
      google::protobuf::RepeatedField<uint64_t>* metadata_name_hashes);

  // Set the value provided in |src_item| to |dest_item|, where |src_item| is
  // in chromium format, and |dest_item| is in proto format. Intended for
  // setting profile-global metadata items. Setting per-sample metadata should
  // be done via the functions above.
  void SetMetadata(
      const base::MetadataRecorder::Item& src_item,
      CallStackProfile::MetadataItem* dest_item,
      google::protobuf::RepeatedField<uint64_t>* metadata_name_hashes);

 private:
  // Comparison function for the metadata map.
  struct MetadataKey;
  struct MetadataKeyCompare {
    bool operator()(const MetadataKey& a, const MetadataKey& b) const;
  };

  // Definitions for a map-based representation of sample metadata.
  struct MetadataKey {
    MetadataKey(uint64_t name_hash, absl::optional<int64_t> key);

    MetadataKey(const MetadataKey& other);
    MetadataKey& operator=(const MetadataKey& other);

    // The name_hash and optional user-specified key uniquely identifies a
    // metadata value. See base::MetadataRecorder for details.
    uint64_t name_hash;
    absl::optional<int64_t> key;
  };
  using MetadataMap = std::map<MetadataKey, int64_t, MetadataKeyCompare>;

  // Creates the metadata map from the array of items.
  MetadataMap CreateMetadataMap(base::MetadataRecorder::ItemArray items,
                                size_t item_count);

  // Returns all metadata items with new values in the current sample.
  MetadataMap GetNewOrModifiedMetadataItems(const MetadataMap& current_items,
                                            const MetadataMap& previous_items);

  // Returns all metadata items deleted since the previous sample.
  MetadataMap GetDeletedMetadataItems(const MetadataMap& current_items,
                                      const MetadataMap& previous_items);

  // Appends the |name_hash| to |name_hashes| if it's not already
  // present. Returns its index in |name_hashes|.
  size_t MaybeAppendNameHash(
      uint64_t name_hash,
      google::protobuf::RepeatedField<uint64_t>* metadata_name_hashes);

  // The data provided for the next sample.
  base::MetadataRecorder::ItemArray metadata_items_;
  size_t metadata_item_count_ = 0;

  // The data provided for the previous sample.
  MetadataMap previous_items_;

  // Maps metadata hash to index in |metadata_name_hash| array.
  std::unordered_map<uint64_t, int> metadata_hashes_cache_;
};

}  // namespace metrics

#endif  // COMPONENTS_METRICS_CALL_STACK_PROFILE_METADATA_H_