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_V8_STATISTICS_H_ 61cb0ef41Sopenharmony_ci#define INCLUDE_V8_STATISTICS_H_ 71cb0ef41Sopenharmony_ci 81cb0ef41Sopenharmony_ci#include <stddef.h> 91cb0ef41Sopenharmony_ci#include <stdint.h> 101cb0ef41Sopenharmony_ci 111cb0ef41Sopenharmony_ci#include <memory> 121cb0ef41Sopenharmony_ci#include <utility> 131cb0ef41Sopenharmony_ci#include <vector> 141cb0ef41Sopenharmony_ci 151cb0ef41Sopenharmony_ci#include "v8-local-handle.h" // NOLINT(build/include_directory) 161cb0ef41Sopenharmony_ci#include "v8-promise.h" // NOLINT(build/include_directory) 171cb0ef41Sopenharmony_ci#include "v8config.h" // NOLINT(build/include_directory) 181cb0ef41Sopenharmony_ci 191cb0ef41Sopenharmony_cinamespace v8 { 201cb0ef41Sopenharmony_ci 211cb0ef41Sopenharmony_ciclass Context; 221cb0ef41Sopenharmony_ciclass Isolate; 231cb0ef41Sopenharmony_ci 241cb0ef41Sopenharmony_cinamespace internal { 251cb0ef41Sopenharmony_ciclass ReadOnlyHeap; 261cb0ef41Sopenharmony_ci} // namespace internal 271cb0ef41Sopenharmony_ci 281cb0ef41Sopenharmony_ci/** 291cb0ef41Sopenharmony_ci * Controls how the default MeasureMemoryDelegate reports the result of 301cb0ef41Sopenharmony_ci * the memory measurement to JS. With kSummary only the total size is reported. 311cb0ef41Sopenharmony_ci * With kDetailed the result includes the size of each native context. 321cb0ef41Sopenharmony_ci */ 331cb0ef41Sopenharmony_cienum class MeasureMemoryMode { kSummary, kDetailed }; 341cb0ef41Sopenharmony_ci 351cb0ef41Sopenharmony_ci/** 361cb0ef41Sopenharmony_ci * Controls how promptly a memory measurement request is executed. 371cb0ef41Sopenharmony_ci * By default the measurement is folded with the next scheduled GC which may 381cb0ef41Sopenharmony_ci * happen after a while and is forced after some timeout. 391cb0ef41Sopenharmony_ci * The kEager mode starts incremental GC right away and is useful for testing. 401cb0ef41Sopenharmony_ci * The kLazy mode does not force GC. 411cb0ef41Sopenharmony_ci */ 421cb0ef41Sopenharmony_cienum class MeasureMemoryExecution { kDefault, kEager, kLazy }; 431cb0ef41Sopenharmony_ci 441cb0ef41Sopenharmony_ci/** 451cb0ef41Sopenharmony_ci * The delegate is used in Isolate::MeasureMemory API. 461cb0ef41Sopenharmony_ci * 471cb0ef41Sopenharmony_ci * It specifies the contexts that need to be measured and gets called when 481cb0ef41Sopenharmony_ci * the measurement is completed to report the results. 491cb0ef41Sopenharmony_ci */ 501cb0ef41Sopenharmony_ciclass V8_EXPORT MeasureMemoryDelegate { 511cb0ef41Sopenharmony_ci public: 521cb0ef41Sopenharmony_ci virtual ~MeasureMemoryDelegate() = default; 531cb0ef41Sopenharmony_ci 541cb0ef41Sopenharmony_ci /** 551cb0ef41Sopenharmony_ci * Returns true if the size of the given context needs to be measured. 561cb0ef41Sopenharmony_ci */ 571cb0ef41Sopenharmony_ci virtual bool ShouldMeasure(Local<Context> context) = 0; 581cb0ef41Sopenharmony_ci 591cb0ef41Sopenharmony_ci /** 601cb0ef41Sopenharmony_ci * This function is called when memory measurement finishes. 611cb0ef41Sopenharmony_ci * 621cb0ef41Sopenharmony_ci * \param context_sizes_in_bytes a vector of (context, size) pairs that 631cb0ef41Sopenharmony_ci * includes each context for which ShouldMeasure returned true and that 641cb0ef41Sopenharmony_ci * was not garbage collected while the memory measurement was in progress. 651cb0ef41Sopenharmony_ci * 661cb0ef41Sopenharmony_ci * \param unattributed_size_in_bytes total size of objects that were not 671cb0ef41Sopenharmony_ci * attributed to any context (i.e. are likely shared objects). 681cb0ef41Sopenharmony_ci */ 691cb0ef41Sopenharmony_ci virtual void MeasurementComplete( 701cb0ef41Sopenharmony_ci const std::vector<std::pair<Local<Context>, size_t>>& 711cb0ef41Sopenharmony_ci context_sizes_in_bytes, 721cb0ef41Sopenharmony_ci size_t unattributed_size_in_bytes) = 0; 731cb0ef41Sopenharmony_ci 741cb0ef41Sopenharmony_ci /** 751cb0ef41Sopenharmony_ci * Returns a default delegate that resolves the given promise when 761cb0ef41Sopenharmony_ci * the memory measurement completes. 771cb0ef41Sopenharmony_ci * 781cb0ef41Sopenharmony_ci * \param isolate the current isolate 791cb0ef41Sopenharmony_ci * \param context the current context 801cb0ef41Sopenharmony_ci * \param promise_resolver the promise resolver that is given the 811cb0ef41Sopenharmony_ci * result of the memory measurement. 821cb0ef41Sopenharmony_ci * \param mode the detail level of the result. 831cb0ef41Sopenharmony_ci */ 841cb0ef41Sopenharmony_ci static std::unique_ptr<MeasureMemoryDelegate> Default( 851cb0ef41Sopenharmony_ci Isolate* isolate, Local<Context> context, 861cb0ef41Sopenharmony_ci Local<Promise::Resolver> promise_resolver, MeasureMemoryMode mode); 871cb0ef41Sopenharmony_ci}; 881cb0ef41Sopenharmony_ci 891cb0ef41Sopenharmony_ci/** 901cb0ef41Sopenharmony_ci * Collection of shared per-process V8 memory information. 911cb0ef41Sopenharmony_ci * 921cb0ef41Sopenharmony_ci * Instances of this class can be passed to 931cb0ef41Sopenharmony_ci * v8::V8::GetSharedMemoryStatistics to get shared memory statistics from V8. 941cb0ef41Sopenharmony_ci */ 951cb0ef41Sopenharmony_ciclass V8_EXPORT SharedMemoryStatistics { 961cb0ef41Sopenharmony_ci public: 971cb0ef41Sopenharmony_ci SharedMemoryStatistics(); 981cb0ef41Sopenharmony_ci size_t read_only_space_size() { return read_only_space_size_; } 991cb0ef41Sopenharmony_ci size_t read_only_space_used_size() { return read_only_space_used_size_; } 1001cb0ef41Sopenharmony_ci size_t read_only_space_physical_size() { 1011cb0ef41Sopenharmony_ci return read_only_space_physical_size_; 1021cb0ef41Sopenharmony_ci } 1031cb0ef41Sopenharmony_ci 1041cb0ef41Sopenharmony_ci private: 1051cb0ef41Sopenharmony_ci size_t read_only_space_size_; 1061cb0ef41Sopenharmony_ci size_t read_only_space_used_size_; 1071cb0ef41Sopenharmony_ci size_t read_only_space_physical_size_; 1081cb0ef41Sopenharmony_ci 1091cb0ef41Sopenharmony_ci friend class V8; 1101cb0ef41Sopenharmony_ci friend class internal::ReadOnlyHeap; 1111cb0ef41Sopenharmony_ci}; 1121cb0ef41Sopenharmony_ci 1131cb0ef41Sopenharmony_ci/** 1141cb0ef41Sopenharmony_ci * Collection of V8 heap information. 1151cb0ef41Sopenharmony_ci * 1161cb0ef41Sopenharmony_ci * Instances of this class can be passed to v8::Isolate::GetHeapStatistics to 1171cb0ef41Sopenharmony_ci * get heap statistics from V8. 1181cb0ef41Sopenharmony_ci */ 1191cb0ef41Sopenharmony_ciclass V8_EXPORT HeapStatistics { 1201cb0ef41Sopenharmony_ci public: 1211cb0ef41Sopenharmony_ci HeapStatistics(); 1221cb0ef41Sopenharmony_ci size_t total_heap_size() { return total_heap_size_; } 1231cb0ef41Sopenharmony_ci size_t total_heap_size_executable() { return total_heap_size_executable_; } 1241cb0ef41Sopenharmony_ci size_t total_physical_size() { return total_physical_size_; } 1251cb0ef41Sopenharmony_ci size_t total_available_size() { return total_available_size_; } 1261cb0ef41Sopenharmony_ci size_t total_global_handles_size() { return total_global_handles_size_; } 1271cb0ef41Sopenharmony_ci size_t used_global_handles_size() { return used_global_handles_size_; } 1281cb0ef41Sopenharmony_ci size_t used_heap_size() { return used_heap_size_; } 1291cb0ef41Sopenharmony_ci size_t heap_size_limit() { return heap_size_limit_; } 1301cb0ef41Sopenharmony_ci size_t malloced_memory() { return malloced_memory_; } 1311cb0ef41Sopenharmony_ci size_t external_memory() { return external_memory_; } 1321cb0ef41Sopenharmony_ci size_t peak_malloced_memory() { return peak_malloced_memory_; } 1331cb0ef41Sopenharmony_ci size_t number_of_native_contexts() { return number_of_native_contexts_; } 1341cb0ef41Sopenharmony_ci size_t number_of_detached_contexts() { return number_of_detached_contexts_; } 1351cb0ef41Sopenharmony_ci 1361cb0ef41Sopenharmony_ci /** 1371cb0ef41Sopenharmony_ci * Returns a 0/1 boolean, which signifies whether the V8 overwrite heap 1381cb0ef41Sopenharmony_ci * garbage with a bit pattern. 1391cb0ef41Sopenharmony_ci */ 1401cb0ef41Sopenharmony_ci size_t does_zap_garbage() { return does_zap_garbage_; } 1411cb0ef41Sopenharmony_ci 1421cb0ef41Sopenharmony_ci private: 1431cb0ef41Sopenharmony_ci size_t total_heap_size_; 1441cb0ef41Sopenharmony_ci size_t total_heap_size_executable_; 1451cb0ef41Sopenharmony_ci size_t total_physical_size_; 1461cb0ef41Sopenharmony_ci size_t total_available_size_; 1471cb0ef41Sopenharmony_ci size_t used_heap_size_; 1481cb0ef41Sopenharmony_ci size_t heap_size_limit_; 1491cb0ef41Sopenharmony_ci size_t malloced_memory_; 1501cb0ef41Sopenharmony_ci size_t external_memory_; 1511cb0ef41Sopenharmony_ci size_t peak_malloced_memory_; 1521cb0ef41Sopenharmony_ci bool does_zap_garbage_; 1531cb0ef41Sopenharmony_ci size_t number_of_native_contexts_; 1541cb0ef41Sopenharmony_ci size_t number_of_detached_contexts_; 1551cb0ef41Sopenharmony_ci size_t total_global_handles_size_; 1561cb0ef41Sopenharmony_ci size_t used_global_handles_size_; 1571cb0ef41Sopenharmony_ci 1581cb0ef41Sopenharmony_ci friend class V8; 1591cb0ef41Sopenharmony_ci friend class Isolate; 1601cb0ef41Sopenharmony_ci}; 1611cb0ef41Sopenharmony_ci 1621cb0ef41Sopenharmony_ciclass V8_EXPORT HeapSpaceStatistics { 1631cb0ef41Sopenharmony_ci public: 1641cb0ef41Sopenharmony_ci HeapSpaceStatistics(); 1651cb0ef41Sopenharmony_ci const char* space_name() { return space_name_; } 1661cb0ef41Sopenharmony_ci size_t space_size() { return space_size_; } 1671cb0ef41Sopenharmony_ci size_t space_used_size() { return space_used_size_; } 1681cb0ef41Sopenharmony_ci size_t space_available_size() { return space_available_size_; } 1691cb0ef41Sopenharmony_ci size_t physical_space_size() { return physical_space_size_; } 1701cb0ef41Sopenharmony_ci 1711cb0ef41Sopenharmony_ci private: 1721cb0ef41Sopenharmony_ci const char* space_name_; 1731cb0ef41Sopenharmony_ci size_t space_size_; 1741cb0ef41Sopenharmony_ci size_t space_used_size_; 1751cb0ef41Sopenharmony_ci size_t space_available_size_; 1761cb0ef41Sopenharmony_ci size_t physical_space_size_; 1771cb0ef41Sopenharmony_ci 1781cb0ef41Sopenharmony_ci friend class Isolate; 1791cb0ef41Sopenharmony_ci}; 1801cb0ef41Sopenharmony_ci 1811cb0ef41Sopenharmony_ciclass V8_EXPORT HeapObjectStatistics { 1821cb0ef41Sopenharmony_ci public: 1831cb0ef41Sopenharmony_ci HeapObjectStatistics(); 1841cb0ef41Sopenharmony_ci const char* object_type() { return object_type_; } 1851cb0ef41Sopenharmony_ci const char* object_sub_type() { return object_sub_type_; } 1861cb0ef41Sopenharmony_ci size_t object_count() { return object_count_; } 1871cb0ef41Sopenharmony_ci size_t object_size() { return object_size_; } 1881cb0ef41Sopenharmony_ci 1891cb0ef41Sopenharmony_ci private: 1901cb0ef41Sopenharmony_ci const char* object_type_; 1911cb0ef41Sopenharmony_ci const char* object_sub_type_; 1921cb0ef41Sopenharmony_ci size_t object_count_; 1931cb0ef41Sopenharmony_ci size_t object_size_; 1941cb0ef41Sopenharmony_ci 1951cb0ef41Sopenharmony_ci friend class Isolate; 1961cb0ef41Sopenharmony_ci}; 1971cb0ef41Sopenharmony_ci 1981cb0ef41Sopenharmony_ciclass V8_EXPORT HeapCodeStatistics { 1991cb0ef41Sopenharmony_ci public: 2001cb0ef41Sopenharmony_ci HeapCodeStatistics(); 2011cb0ef41Sopenharmony_ci size_t code_and_metadata_size() { return code_and_metadata_size_; } 2021cb0ef41Sopenharmony_ci size_t bytecode_and_metadata_size() { return bytecode_and_metadata_size_; } 2031cb0ef41Sopenharmony_ci size_t external_script_source_size() { return external_script_source_size_; } 2041cb0ef41Sopenharmony_ci size_t cpu_profiler_metadata_size() { return cpu_profiler_metadata_size_; } 2051cb0ef41Sopenharmony_ci 2061cb0ef41Sopenharmony_ci private: 2071cb0ef41Sopenharmony_ci size_t code_and_metadata_size_; 2081cb0ef41Sopenharmony_ci size_t bytecode_and_metadata_size_; 2091cb0ef41Sopenharmony_ci size_t external_script_source_size_; 2101cb0ef41Sopenharmony_ci size_t cpu_profiler_metadata_size_; 2111cb0ef41Sopenharmony_ci 2121cb0ef41Sopenharmony_ci friend class Isolate; 2131cb0ef41Sopenharmony_ci}; 2141cb0ef41Sopenharmony_ci 2151cb0ef41Sopenharmony_ci} // namespace v8 2161cb0ef41Sopenharmony_ci 2171cb0ef41Sopenharmony_ci#endif // INCLUDE_V8_STATISTICS_H_ 218