11cb0ef41Sopenharmony_ci// Copyright 2021 the V8 project authors. All rights reserved.
21cb0ef41Sopenharmony_ci// Use of this source code is governed by a BSD-style license that can be
31cb0ef41Sopenharmony_ci// found in the LICENSE file.
41cb0ef41Sopenharmony_ci
51cb0ef41Sopenharmony_ci#ifndef INCLUDE_CPPGC_HEAP_STATISTICS_H_
61cb0ef41Sopenharmony_ci#define INCLUDE_CPPGC_HEAP_STATISTICS_H_
71cb0ef41Sopenharmony_ci
81cb0ef41Sopenharmony_ci#include <cstddef>
91cb0ef41Sopenharmony_ci#include <cstdint>
101cb0ef41Sopenharmony_ci#include <string>
111cb0ef41Sopenharmony_ci#include <vector>
121cb0ef41Sopenharmony_ci
131cb0ef41Sopenharmony_cinamespace cppgc {
141cb0ef41Sopenharmony_ci
151cb0ef41Sopenharmony_ci/**
161cb0ef41Sopenharmony_ci * `HeapStatistics` contains memory consumption and utilization statistics for a
171cb0ef41Sopenharmony_ci * cppgc heap.
181cb0ef41Sopenharmony_ci */
191cb0ef41Sopenharmony_cistruct HeapStatistics final {
201cb0ef41Sopenharmony_ci  /**
211cb0ef41Sopenharmony_ci   * Specifies the detail level of the heap statistics. Brief statistics contain
221cb0ef41Sopenharmony_ci   * only the top-level allocated and used memory statistics for the entire
231cb0ef41Sopenharmony_ci   * heap. Detailed statistics also contain a break down per space and page, as
241cb0ef41Sopenharmony_ci   * well as freelist statistics and object type histograms. Note that used
251cb0ef41Sopenharmony_ci   * memory reported by brief statistics and detailed statistics might differ
261cb0ef41Sopenharmony_ci   * slightly.
271cb0ef41Sopenharmony_ci   */
281cb0ef41Sopenharmony_ci  enum DetailLevel : uint8_t {
291cb0ef41Sopenharmony_ci    kBrief,
301cb0ef41Sopenharmony_ci    kDetailed,
311cb0ef41Sopenharmony_ci  };
321cb0ef41Sopenharmony_ci
331cb0ef41Sopenharmony_ci  /**
341cb0ef41Sopenharmony_ci   * Object statistics for a single type.
351cb0ef41Sopenharmony_ci   */
361cb0ef41Sopenharmony_ci  struct ObjectStatsEntry {
371cb0ef41Sopenharmony_ci    /**
381cb0ef41Sopenharmony_ci     * Number of allocated bytes.
391cb0ef41Sopenharmony_ci     */
401cb0ef41Sopenharmony_ci    size_t allocated_bytes;
411cb0ef41Sopenharmony_ci    /**
421cb0ef41Sopenharmony_ci     * Number of allocated objects.
431cb0ef41Sopenharmony_ci     */
441cb0ef41Sopenharmony_ci    size_t object_count;
451cb0ef41Sopenharmony_ci  };
461cb0ef41Sopenharmony_ci
471cb0ef41Sopenharmony_ci  /**
481cb0ef41Sopenharmony_ci   * Page granularity statistics. For each page the statistics record the
491cb0ef41Sopenharmony_ci   * allocated memory size and overall used memory size for the page.
501cb0ef41Sopenharmony_ci   */
511cb0ef41Sopenharmony_ci  struct PageStatistics {
521cb0ef41Sopenharmony_ci    /** Overall committed amount of memory for the page. */
531cb0ef41Sopenharmony_ci    size_t committed_size_bytes = 0;
541cb0ef41Sopenharmony_ci    /** Resident amount of memory held by the page. */
551cb0ef41Sopenharmony_ci    size_t resident_size_bytes = 0;
561cb0ef41Sopenharmony_ci    /** Amount of memory actually used on the page. */
571cb0ef41Sopenharmony_ci    size_t used_size_bytes = 0;
581cb0ef41Sopenharmony_ci    /** Statistics for object allocated on the page. Filled only when
591cb0ef41Sopenharmony_ci     * NameProvider::HideInternalNames() is false. */
601cb0ef41Sopenharmony_ci    std::vector<ObjectStatsEntry> object_statistics;
611cb0ef41Sopenharmony_ci  };
621cb0ef41Sopenharmony_ci
631cb0ef41Sopenharmony_ci  /**
641cb0ef41Sopenharmony_ci   * Statistics of the freelist (used only in non-large object spaces). For
651cb0ef41Sopenharmony_ci   * each bucket in the freelist the statistics record the bucket size, the
661cb0ef41Sopenharmony_ci   * number of freelist entries in the bucket, and the overall allocated memory
671cb0ef41Sopenharmony_ci   * consumed by these freelist entries.
681cb0ef41Sopenharmony_ci   */
691cb0ef41Sopenharmony_ci  struct FreeListStatistics {
701cb0ef41Sopenharmony_ci    /** bucket sizes in the freelist. */
711cb0ef41Sopenharmony_ci    std::vector<size_t> bucket_size;
721cb0ef41Sopenharmony_ci    /** number of freelist entries per bucket. */
731cb0ef41Sopenharmony_ci    std::vector<size_t> free_count;
741cb0ef41Sopenharmony_ci    /** memory size consumed by freelist entries per size. */
751cb0ef41Sopenharmony_ci    std::vector<size_t> free_size;
761cb0ef41Sopenharmony_ci  };
771cb0ef41Sopenharmony_ci
781cb0ef41Sopenharmony_ci  /**
791cb0ef41Sopenharmony_ci   * Space granularity statistics. For each space the statistics record the
801cb0ef41Sopenharmony_ci   * space name, the amount of allocated memory and overall used memory for the
811cb0ef41Sopenharmony_ci   * space. The statistics also contain statistics for each of the space's
821cb0ef41Sopenharmony_ci   * pages, its freelist and the objects allocated on the space.
831cb0ef41Sopenharmony_ci   */
841cb0ef41Sopenharmony_ci  struct SpaceStatistics {
851cb0ef41Sopenharmony_ci    /** The space name */
861cb0ef41Sopenharmony_ci    std::string name;
871cb0ef41Sopenharmony_ci    /** Overall committed amount of memory for the heap. */
881cb0ef41Sopenharmony_ci    size_t committed_size_bytes = 0;
891cb0ef41Sopenharmony_ci    /** Resident amount of memory held by the heap. */
901cb0ef41Sopenharmony_ci    size_t resident_size_bytes = 0;
911cb0ef41Sopenharmony_ci    /** Amount of memory actually used on the space. */
921cb0ef41Sopenharmony_ci    size_t used_size_bytes = 0;
931cb0ef41Sopenharmony_ci    /** Statistics for each of the pages in the space. */
941cb0ef41Sopenharmony_ci    std::vector<PageStatistics> page_stats;
951cb0ef41Sopenharmony_ci    /** Statistics for the freelist of the space. */
961cb0ef41Sopenharmony_ci    FreeListStatistics free_list_stats;
971cb0ef41Sopenharmony_ci  };
981cb0ef41Sopenharmony_ci
991cb0ef41Sopenharmony_ci  /** Overall committed amount of memory for the heap. */
1001cb0ef41Sopenharmony_ci  size_t committed_size_bytes = 0;
1011cb0ef41Sopenharmony_ci  /** Resident amount of memory help by the heap. */
1021cb0ef41Sopenharmony_ci  size_t resident_size_bytes = 0;
1031cb0ef41Sopenharmony_ci  /** Amount of memory actually used on the heap. */
1041cb0ef41Sopenharmony_ci  size_t used_size_bytes = 0;
1051cb0ef41Sopenharmony_ci  /** Detail level of this HeapStatistics. */
1061cb0ef41Sopenharmony_ci  DetailLevel detail_level;
1071cb0ef41Sopenharmony_ci
1081cb0ef41Sopenharmony_ci  /** Statistics for each of the spaces in the heap. Filled only when
1091cb0ef41Sopenharmony_ci   * `detail_level` is `DetailLevel::kDetailed`. */
1101cb0ef41Sopenharmony_ci  std::vector<SpaceStatistics> space_stats;
1111cb0ef41Sopenharmony_ci
1121cb0ef41Sopenharmony_ci  /**
1131cb0ef41Sopenharmony_ci   * Vector of `cppgc::GarbageCollected` type names.
1141cb0ef41Sopenharmony_ci   */
1151cb0ef41Sopenharmony_ci  std::vector<std::string> type_names;
1161cb0ef41Sopenharmony_ci};
1171cb0ef41Sopenharmony_ci
1181cb0ef41Sopenharmony_ci}  // namespace cppgc
1191cb0ef41Sopenharmony_ci
1201cb0ef41Sopenharmony_ci#endif  // INCLUDE_CPPGC_HEAP_STATISTICS_H_
121