1// Copyright 2021 the V8 project 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
5#ifndef INCLUDE_CPPGC_HEAP_STATISTICS_H_
6#define INCLUDE_CPPGC_HEAP_STATISTICS_H_
7
8#include <cstddef>
9#include <cstdint>
10#include <string>
11#include <vector>
12
13namespace cppgc {
14
15/**
16 * `HeapStatistics` contains memory consumption and utilization statistics for a
17 * cppgc heap.
18 */
19struct HeapStatistics final {
20  /**
21   * Specifies the detail level of the heap statistics. Brief statistics contain
22   * only the top-level allocated and used memory statistics for the entire
23   * heap. Detailed statistics also contain a break down per space and page, as
24   * well as freelist statistics and object type histograms. Note that used
25   * memory reported by brief statistics and detailed statistics might differ
26   * slightly.
27   */
28  enum DetailLevel : uint8_t {
29    kBrief,
30    kDetailed,
31  };
32
33  /**
34   * Object statistics for a single type.
35   */
36  struct ObjectStatsEntry {
37    /**
38     * Number of allocated bytes.
39     */
40    size_t allocated_bytes;
41    /**
42     * Number of allocated objects.
43     */
44    size_t object_count;
45  };
46
47  /**
48   * Page granularity statistics. For each page the statistics record the
49   * allocated memory size and overall used memory size for the page.
50   */
51  struct PageStatistics {
52    /** Overall committed amount of memory for the page. */
53    size_t committed_size_bytes = 0;
54    /** Resident amount of memory held by the page. */
55    size_t resident_size_bytes = 0;
56    /** Amount of memory actually used on the page. */
57    size_t used_size_bytes = 0;
58    /** Statistics for object allocated on the page. Filled only when
59     * NameProvider::HideInternalNames() is false. */
60    std::vector<ObjectStatsEntry> object_statistics;
61  };
62
63  /**
64   * Statistics of the freelist (used only in non-large object spaces). For
65   * each bucket in the freelist the statistics record the bucket size, the
66   * number of freelist entries in the bucket, and the overall allocated memory
67   * consumed by these freelist entries.
68   */
69  struct FreeListStatistics {
70    /** bucket sizes in the freelist. */
71    std::vector<size_t> bucket_size;
72    /** number of freelist entries per bucket. */
73    std::vector<size_t> free_count;
74    /** memory size consumed by freelist entries per size. */
75    std::vector<size_t> free_size;
76  };
77
78  /**
79   * Space granularity statistics. For each space the statistics record the
80   * space name, the amount of allocated memory and overall used memory for the
81   * space. The statistics also contain statistics for each of the space's
82   * pages, its freelist and the objects allocated on the space.
83   */
84  struct SpaceStatistics {
85    /** The space name */
86    std::string name;
87    /** Overall committed amount of memory for the heap. */
88    size_t committed_size_bytes = 0;
89    /** Resident amount of memory held by the heap. */
90    size_t resident_size_bytes = 0;
91    /** Amount of memory actually used on the space. */
92    size_t used_size_bytes = 0;
93    /** Statistics for each of the pages in the space. */
94    std::vector<PageStatistics> page_stats;
95    /** Statistics for the freelist of the space. */
96    FreeListStatistics free_list_stats;
97  };
98
99  /** Overall committed amount of memory for the heap. */
100  size_t committed_size_bytes = 0;
101  /** Resident amount of memory help by the heap. */
102  size_t resident_size_bytes = 0;
103  /** Amount of memory actually used on the heap. */
104  size_t used_size_bytes = 0;
105  /** Detail level of this HeapStatistics. */
106  DetailLevel detail_level;
107
108  /** Statistics for each of the spaces in the heap. Filled only when
109   * `detail_level` is `DetailLevel::kDetailed`. */
110  std::vector<SpaceStatistics> space_stats;
111
112  /**
113   * Vector of `cppgc::GarbageCollected` type names.
114   */
115  std::vector<std::string> type_names;
116};
117
118}  // namespace cppgc
119
120#endif  // INCLUDE_CPPGC_HEAP_STATISTICS_H_
121