1// Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file
2// for details. All rights reserved. Use of this source code is governed by a
3// BSD-style license that can be found in the LICENSE file.
4
5#ifndef RUNTIME_VM_METRICS_H_
6#define RUNTIME_VM_METRICS_H_
7
8#include "vm/allocation.h"
9
10namespace dart {
11
12class Isolate;
13class IsolateGroup;
14class JSONStream;
15
16// Metrics for each isolate group.
17//
18// Golem uses `--print-metrics` and relies on
19//
20// heap.old.capacity.max
21// heap.new.capacity.max
22//
23// g3 uses metrics via Dart API:
24//
25// Dart_Heap{Old,New}{Used,Capacity,External}
26//
27// All metrics are exposed via vm-service protocol.
28//
29#define DART_API_ISOLATE_GROUP_METRIC_LIST(V) \
30 V(MetricHeapOldUsed, HeapOldUsed, "heap.old.used", kByte) \
31 V(MetricHeapOldCapacity, HeapOldCapacity, "heap.old.capacity", kByte) \
32 V(MetricHeapOldExternal, HeapOldExternal, "heap.old.external", kByte) \
33 V(MetricHeapNewUsed, HeapNewUsed, "heap.new.used", kByte) \
34 V(MetricHeapNewCapacity, HeapNewCapacity, "heap.new.capacity", kByte) \
35 V(MetricHeapNewExternal, HeapNewExternal, "heap.new.external", kByte)
36
37#define ISOLATE_GROUP_METRIC_LIST(V) \
38 DART_API_ISOLATE_GROUP_METRIC_LIST(V) \
39 V(MaxMetric, HeapOldUsedMax, "heap.old.used.max", kByte) \
40 V(MaxMetric, HeapOldCapacityMax, "heap.old.capacity.max", kByte) \
41 V(MaxMetric, HeapNewUsedMax, "heap.new.used.max", kByte) \
42 V(MaxMetric, HeapNewCapacityMax, "heap.new.capacity.max", kByte) \
43 V(MetricHeapUsed, HeapGlobalUsed, "heap.global.used", kByte) \
44 V(MaxMetric, HeapGlobalUsedMax, "heap.global.used.max", kByte)
45
46// Metrics for each isolate.
47//
48// All metrics are exposed via vm-service protocol.
49#define ISOLATE_METRIC_LIST(V) \
50 V(Metric, RunnableLatency, "isolate.runnable.latency", kMicrosecond) \
51 V(Metric, RunnableHeapSize, "isolate.runnable.heap", kByte)
52
53class Metric {
54 public:
55 enum Unit {
56 kCounter,
57 kByte,
58 kMicrosecond,
59 };
60
61 Metric();
62
63#if !defined(PRODUCT)
64 static void Init();
65 static void Cleanup();
66#endif // !defined(PRODUCT)
67
68 // Initialize a metric for an isolate.
69 void InitInstance(Isolate* isolate,
70 const char* name,
71 const char* description,
72 Unit unit);
73
74 // Initialize a metric for an isolate group.
75 void InitInstance(IsolateGroup* isolate_group,
76 const char* name,
77 const char* description,
78 Unit unit);
79
80 // Initialize a metric for the VM.
81 void InitInstance(const char* name, const char* description, Unit unit);
82
83 virtual ~Metric();
84
85#ifndef PRODUCT
86 void PrintJSON(JSONStream* stream);
87#endif // !PRODUCT
88
89 // Returns a zone allocated string.
90 static char* ValueToString(int64_t value, Unit unit);
91
92 // Returns a zone allocated string.
93 char* ToString();
94
95 int64_t value() const { return value_; }
96 void set_value(int64_t value) { value_ = value; }
97
98 void increment() { value_++; }
99
100 const char* name() const { return name_; }
101 const char* description() const { return description_; }
102 Unit unit() const { return unit_; }
103
104 // Only non-null for isolate specific metrics.
105 Isolate* isolate() const { return isolate_; }
106
107 // Only non-null for isolate group specific metrics.
108 IsolateGroup* isolate_group() const { return isolate_group_; }
109
110 static Metric* vm_head() { return vm_list_head_; }
111
112 // Override to get a callback when value is serialized to JSON.
113 // Use this for metrics that produce their value on demand.
114 virtual int64_t Value() const { return value(); }
115
116 private:
117 Isolate* isolate_ = nullptr;
118 IsolateGroup* isolate_group_ = nullptr;
119 const char* name_ = nullptr;
120 const char* description_ = nullptr;
121 Unit unit_;
122 int64_t value_;
123
124 static Metric* vm_list_head_;
125 DISALLOW_COPY_AND_ASSIGN(Metric);
126};
127
128// A Metric class that reports the maximum value observed.
129// Initial maximum is kMinInt64.
130class MaxMetric : public Metric {
131 public:
132 MaxMetric();
133
134 void SetValue(int64_t new_value);
135};
136
137// A Metric class that reports the minimum value observed.
138// Initial minimum is kMaxInt64.
139class MinMetric : public Metric {
140 public:
141 MinMetric();
142
143 void SetValue(int64_t new_value);
144};
145
146class MetricHeapOldUsed : public Metric {
147 public:
148 virtual int64_t Value() const;
149};
150
151class MetricHeapOldCapacity : public Metric {
152 public:
153 virtual int64_t Value() const;
154};
155
156class MetricHeapOldExternal : public Metric {
157 public:
158 virtual int64_t Value() const;
159};
160
161class MetricHeapNewUsed : public Metric {
162 public:
163 virtual int64_t Value() const;
164};
165
166class MetricHeapNewCapacity : public Metric {
167 public:
168 virtual int64_t Value() const;
169};
170
171class MetricHeapNewExternal : public Metric {
172 public:
173 virtual int64_t Value() const;
174};
175
176#if !defined(PRODUCT)
177class MetricIsolateCount : public Metric {
178 public:
179 virtual int64_t Value() const;
180};
181
182class MetricCurrentRSS : public Metric {
183 public:
184 virtual int64_t Value() const;
185};
186
187class MetricPeakRSS : public Metric {
188 public:
189 virtual int64_t Value() const;
190};
191#endif // !defined(PRODUCT)
192
193class MetricHeapUsed : public Metric {
194 public:
195 virtual int64_t Value() const;
196};
197
198} // namespace dart
199
200#endif // RUNTIME_VM_METRICS_H_
201

source code of dart_sdk/runtime/vm/metrics.h