v8-statistics.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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. #ifndef INCLUDE_V8_STATISTICS_H_
  5. #define INCLUDE_V8_STATISTICS_H_
  6. #include <stddef.h>
  7. #include <stdint.h>
  8. #include <memory>
  9. #include <utility>
  10. #include <vector>
  11. #include "v8-local-handle.h" // NOLINT(build/include_directory)
  12. #include "v8-promise.h" // NOLINT(build/include_directory)
  13. #include "v8config.h" // NOLINT(build/include_directory)
  14. namespace v8 {
  15. class Context;
  16. class Isolate;
  17. namespace internal {
  18. class ReadOnlyHeap;
  19. } // namespace internal
  20. /**
  21. * Controls how the default MeasureMemoryDelegate reports the result of
  22. * the memory measurement to JS. With kSummary only the total size is reported.
  23. * With kDetailed the result includes the size of each native context.
  24. */
  25. enum class MeasureMemoryMode { kSummary, kDetailed };
  26. /**
  27. * Controls how promptly a memory measurement request is executed.
  28. * By default the measurement is folded with the next scheduled GC which may
  29. * happen after a while and is forced after some timeout.
  30. * The kEager mode starts incremental GC right away and is useful for testing.
  31. * The kLazy mode does not force GC.
  32. */
  33. enum class MeasureMemoryExecution { kDefault, kEager, kLazy };
  34. /**
  35. * The delegate is used in Isolate::MeasureMemory API.
  36. *
  37. * It specifies the contexts that need to be measured and gets called when
  38. * the measurement is completed to report the results.
  39. */
  40. class V8_EXPORT MeasureMemoryDelegate {
  41. public:
  42. virtual ~MeasureMemoryDelegate() = default;
  43. /**
  44. * Returns true if the size of the given context needs to be measured.
  45. */
  46. virtual bool ShouldMeasure(Local<Context> context) = 0;
  47. /**
  48. * This function is called when memory measurement finishes.
  49. *
  50. * \param context_sizes_in_bytes a vector of (context, size) pairs that
  51. * includes each context for which ShouldMeasure returned true and that
  52. * was not garbage collected while the memory measurement was in progress.
  53. *
  54. * \param unattributed_size_in_bytes total size of objects that were not
  55. * attributed to any context (i.e. are likely shared objects).
  56. */
  57. virtual void MeasurementComplete(
  58. const std::vector<std::pair<Local<Context>, size_t>>&
  59. context_sizes_in_bytes,
  60. size_t unattributed_size_in_bytes) = 0;
  61. /**
  62. * Returns a default delegate that resolves the given promise when
  63. * the memory measurement completes.
  64. *
  65. * \param isolate the current isolate
  66. * \param context the current context
  67. * \param promise_resolver the promise resolver that is given the
  68. * result of the memory measurement.
  69. * \param mode the detail level of the result.
  70. */
  71. static std::unique_ptr<MeasureMemoryDelegate> Default(
  72. Isolate* isolate, Local<Context> context,
  73. Local<Promise::Resolver> promise_resolver, MeasureMemoryMode mode);
  74. };
  75. /**
  76. * Collection of shared per-process V8 memory information.
  77. *
  78. * Instances of this class can be passed to
  79. * v8::V8::GetSharedMemoryStatistics to get shared memory statistics from V8.
  80. */
  81. class V8_EXPORT SharedMemoryStatistics {
  82. public:
  83. SharedMemoryStatistics();
  84. size_t read_only_space_size() { return read_only_space_size_; }
  85. size_t read_only_space_used_size() { return read_only_space_used_size_; }
  86. size_t read_only_space_physical_size() {
  87. return read_only_space_physical_size_;
  88. }
  89. private:
  90. size_t read_only_space_size_;
  91. size_t read_only_space_used_size_;
  92. size_t read_only_space_physical_size_;
  93. friend class V8;
  94. friend class internal::ReadOnlyHeap;
  95. };
  96. /**
  97. * Collection of V8 heap information.
  98. *
  99. * Instances of this class can be passed to v8::Isolate::GetHeapStatistics to
  100. * get heap statistics from V8.
  101. */
  102. class V8_EXPORT HeapStatistics {
  103. public:
  104. HeapStatistics();
  105. size_t total_heap_size() { return total_heap_size_; }
  106. size_t total_heap_size_executable() { return total_heap_size_executable_; }
  107. size_t total_physical_size() { return total_physical_size_; }
  108. size_t total_available_size() { return total_available_size_; }
  109. size_t total_global_handles_size() { return total_global_handles_size_; }
  110. size_t used_global_handles_size() { return used_global_handles_size_; }
  111. size_t used_heap_size() { return used_heap_size_; }
  112. size_t heap_size_limit() { return heap_size_limit_; }
  113. size_t malloced_memory() { return malloced_memory_; }
  114. size_t external_memory() { return external_memory_; }
  115. size_t peak_malloced_memory() { return peak_malloced_memory_; }
  116. size_t number_of_native_contexts() { return number_of_native_contexts_; }
  117. size_t number_of_detached_contexts() { return number_of_detached_contexts_; }
  118. /**
  119. * Returns a 0/1 boolean, which signifies whether the V8 overwrite heap
  120. * garbage with a bit pattern.
  121. */
  122. size_t does_zap_garbage() { return does_zap_garbage_; }
  123. private:
  124. size_t total_heap_size_;
  125. size_t total_heap_size_executable_;
  126. size_t total_physical_size_;
  127. size_t total_available_size_;
  128. size_t used_heap_size_;
  129. size_t heap_size_limit_;
  130. size_t malloced_memory_;
  131. size_t external_memory_;
  132. size_t peak_malloced_memory_;
  133. bool does_zap_garbage_;
  134. size_t number_of_native_contexts_;
  135. size_t number_of_detached_contexts_;
  136. size_t total_global_handles_size_;
  137. size_t used_global_handles_size_;
  138. friend class V8;
  139. friend class Isolate;
  140. };
  141. class V8_EXPORT HeapSpaceStatistics {
  142. public:
  143. HeapSpaceStatistics();
  144. const char* space_name() { return space_name_; }
  145. size_t space_size() { return space_size_; }
  146. size_t space_used_size() { return space_used_size_; }
  147. size_t space_available_size() { return space_available_size_; }
  148. size_t physical_space_size() { return physical_space_size_; }
  149. private:
  150. const char* space_name_;
  151. size_t space_size_;
  152. size_t space_used_size_;
  153. size_t space_available_size_;
  154. size_t physical_space_size_;
  155. friend class Isolate;
  156. };
  157. class V8_EXPORT HeapObjectStatistics {
  158. public:
  159. HeapObjectStatistics();
  160. const char* object_type() { return object_type_; }
  161. const char* object_sub_type() { return object_sub_type_; }
  162. size_t object_count() { return object_count_; }
  163. size_t object_size() { return object_size_; }
  164. private:
  165. const char* object_type_;
  166. const char* object_sub_type_;
  167. size_t object_count_;
  168. size_t object_size_;
  169. friend class Isolate;
  170. };
  171. class V8_EXPORT HeapCodeStatistics {
  172. public:
  173. HeapCodeStatistics();
  174. size_t code_and_metadata_size() { return code_and_metadata_size_; }
  175. size_t bytecode_and_metadata_size() { return bytecode_and_metadata_size_; }
  176. size_t external_script_source_size() { return external_script_source_size_; }
  177. size_t cpu_profiler_metadata_size() { return cpu_profiler_metadata_size_; }
  178. private:
  179. size_t code_and_metadata_size_;
  180. size_t bytecode_and_metadata_size_;
  181. size_t external_script_source_size_;
  182. size_t cpu_profiler_metadata_size_;
  183. friend class Isolate;
  184. };
  185. } // namespace v8
  186. #endif // INCLUDE_V8_STATISTICS_H_