allocation.h 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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_SERVICES_HEAP_PROFILING_ALLOCATION_H_
  5. #define COMPONENTS_SERVICES_HEAP_PROFILING_ALLOCATION_H_
  6. #include <unordered_map>
  7. #include <vector>
  8. #include "components/services/heap_profiling/public/mojom/heap_profiling_client.mojom.h"
  9. namespace heap_profiling {
  10. using Address = uint64_t;
  11. using mojom::AllocatorType;
  12. // The struct is a descriptor of an allocation site. It is used as a unique
  13. // key in the AllocationMap.
  14. struct AllocationSite {
  15. AllocationSite(AllocatorType allocator,
  16. std::vector<Address>&& stack,
  17. int context_id);
  18. AllocationSite(const AllocationSite&) = delete;
  19. AllocationSite& operator=(const AllocationSite&) = delete;
  20. ~AllocationSite();
  21. // Type of the allocator responsible for the allocation. Possible values are
  22. // kMalloc, kPartitionAlloc, or kOilpan.
  23. const AllocatorType allocator;
  24. // Program call stack at the moment of allocation. Each address is correspond
  25. // to a code memory location in the inspected process.
  26. const std::vector<Address> stack;
  27. // Each allocation call may be associated with a context string.
  28. // This field contains the id of the context string. The string itself is
  29. // stored in |context_map| array in ExportParams class.
  30. const int context_id;
  31. struct Hash {
  32. size_t operator()(const AllocationSite& alloc) const { return alloc.hash_; }
  33. };
  34. private:
  35. const uint32_t hash_;
  36. };
  37. inline bool operator==(const AllocationSite& a, const AllocationSite& b) {
  38. return a.allocator == b.allocator && a.stack == b.stack &&
  39. a.context_id == b.context_id;
  40. }
  41. // Data associated with an allocation site in the AllocationMap.
  42. struct AllocationMetrics {
  43. // Total size of allocations associated with a given sample.
  44. size_t size = 0;
  45. // Number of allocations associated with the sample.
  46. float count = 0;
  47. };
  48. using AllocationMap =
  49. std::unordered_map<AllocationSite, AllocationMetrics, AllocationSite::Hash>;
  50. } // namespace heap_profiling
  51. #endif // COMPONENTS_SERVICES_HEAP_PROFILING_ALLOCATION_H_