|
11 | 11 | #include <unordered_set> |
12 | 12 | #include <vector> |
13 | 13 |
|
| 14 | +#ifdef V8_HEAP_PROFILER_SAMPLE_LABELS |
| 15 | +#include <string> |
| 16 | +#include <utility> |
| 17 | +#endif // V8_HEAP_PROFILER_SAMPLE_LABELS |
| 18 | + |
14 | 19 | #include "cppgc/common.h" // NOLINT(build/include_directory) |
15 | 20 | #include "v8-local-handle.h" // NOLINT(build/include_directory) |
16 | 21 | #include "v8-message.h" // NOLINT(build/include_directory) |
@@ -791,26 +796,39 @@ class V8_EXPORT AllocationProfile { |
791 | 796 | * Represent a single sample recorded for an allocation. |
792 | 797 | */ |
793 | 798 | struct Sample { |
794 | | - /** |
795 | | - * id of the node in the profile tree. |
796 | | - */ |
| 799 | + Sample(uint32_t node_id, size_t size, unsigned int count, |
| 800 | + uint64_t sample_id) |
| 801 | + : node_id(node_id), size(size), count(count), sample_id(sample_id) {} |
| 802 | +#ifdef V8_HEAP_PROFILER_SAMPLE_LABELS |
| 803 | + Sample(uint32_t node_id, size_t size, unsigned int count, |
| 804 | + uint64_t sample_id, |
| 805 | + std::vector<std::pair<std::string, std::string>> labels) |
| 806 | + : node_id(node_id), |
| 807 | + size(size), |
| 808 | + count(count), |
| 809 | + sample_id(sample_id), |
| 810 | + labels(std::move(labels)) {} |
| 811 | +#endif // V8_HEAP_PROFILER_SAMPLE_LABELS |
| 812 | + |
| 813 | + /** id of the node in the profile tree. */ |
797 | 814 | uint32_t node_id; |
798 | | - |
799 | | - /** |
800 | | - * Size of the sampled allocation object. |
801 | | - */ |
| 815 | + /** Size of the sampled allocation object. */ |
802 | 816 | size_t size; |
803 | | - |
804 | | - /** |
805 | | - * The number of objects of such size that were sampled. |
806 | | - */ |
| 817 | + /** The number of objects of such size that were sampled. */ |
807 | 818 | unsigned int count; |
808 | | - |
809 | 819 | /** |
810 | 820 | * Unique time-ordered id of the allocation sample. Can be used to track |
811 | 821 | * what samples were added or removed between two snapshots. |
812 | 822 | */ |
813 | 823 | uint64_t sample_id; |
| 824 | +#ifdef V8_HEAP_PROFILER_SAMPLE_LABELS |
| 825 | + /** |
| 826 | + * Embedder-provided labels captured at allocation time via the |
| 827 | + * HeapProfileSampleLabelsCallback. Each pair is (key, value). |
| 828 | + * Empty if no callback is registered or the callback returned no labels. |
| 829 | + */ |
| 830 | + std::vector<std::pair<std::string, std::string>> labels; |
| 831 | +#endif // V8_HEAP_PROFILER_SAMPLE_LABELS |
814 | 832 | }; |
815 | 833 |
|
816 | 834 | /** |
@@ -1001,6 +1019,24 @@ class V8_EXPORT HeapProfiler { |
1001 | 1019 | v8::Isolate* isolate, const v8::Local<v8::Value>& v8_value, |
1002 | 1020 | uint16_t class_id, void* data); |
1003 | 1021 |
|
| 1022 | +#ifdef V8_HEAP_PROFILER_SAMPLE_LABELS |
| 1023 | + /** |
| 1024 | + * Callback invoked during sampling heap profiler allocation events to |
| 1025 | + * retrieve embedder-defined labels for the current execution context. |
| 1026 | + * |
| 1027 | + * |data| is the opaque pointer passed to SetHeapProfileSampleLabelsCallback. |
| 1028 | + * |context| is the ContinuationPreservedEmbedderData (CPED) value, which |
| 1029 | + * the embedder can use to look up the current async context (e.g., route). |
| 1030 | + * |
| 1031 | + * Write labels to out_labels and return true, or return false if no labels |
| 1032 | + * apply. The caller provides a stack-local vector; returning false avoids |
| 1033 | + * any heap allocation on the hot path. |
| 1034 | + */ |
| 1035 | + using HeapProfileSampleLabelsCallback = bool (*)( |
| 1036 | + void* data, v8::Local<v8::Value> context, |
| 1037 | + std::vector<std::pair<std::string, std::string>>* out_labels); |
| 1038 | +#endif // V8_HEAP_PROFILER_SAMPLE_LABELS |
| 1039 | + |
1004 | 1040 | /** Returns the number of snapshots taken. */ |
1005 | 1041 | int GetSnapshotCount(); |
1006 | 1042 |
|
@@ -1261,6 +1297,18 @@ class V8_EXPORT HeapProfiler { |
1261 | 1297 |
|
1262 | 1298 | void SetGetDetachednessCallback(GetDetachednessCallback callback, void* data); |
1263 | 1299 |
|
| 1300 | +#ifdef V8_HEAP_PROFILER_SAMPLE_LABELS |
| 1301 | + /** |
| 1302 | + * Registers a callback that the sampling heap profiler invokes on each |
| 1303 | + * allocation to retrieve embedder-defined string labels. The labels are |
| 1304 | + * stored on AllocationProfile::Sample::labels. |
| 1305 | + * |
| 1306 | + * Pass nullptr to clear the callback. |
| 1307 | + */ |
| 1308 | + void SetHeapProfileSampleLabelsCallback( |
| 1309 | + HeapProfileSampleLabelsCallback callback, void* data = nullptr); |
| 1310 | +#endif // V8_HEAP_PROFILER_SAMPLE_LABELS |
| 1311 | + |
1264 | 1312 | /** |
1265 | 1313 | * Returns whether the heap profiler is currently taking a snapshot. |
1266 | 1314 | */ |
|
0 commit comments