Flutter Engine
The Flutter Engine
metrics.cc
Go to the documentation of this file.
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#include "vm/metrics.h"
6
7#include "vm/isolate.h"
8#include "vm/json_stream.h"
9#include "vm/log.h"
10#include "vm/native_entry.h"
11#include "vm/object.h"
12#include "vm/runtime_entry.h"
13
14namespace dart {
15
17 print_metrics,
18 false,
19 "Print metrics when isolates (and the VM) are shutdown.");
20
21Metric* Metric::vm_list_head_ = nullptr;
22
23Metric::Metric() : unit_(kCounter), value_(0) {}
25
27 const char* name,
28 const char* description,
29 Unit unit) {
30 // Only called once.
31 ASSERT(name != nullptr);
32 isolate_group_ = isolate_group;
33 name_ = name;
34 description_ = description;
35 unit_ = unit;
36}
37
38#if !defined(PRODUCT)
40 const char* name,
41 const char* description,
42 Unit unit) {
43 // Only called once.
44 ASSERT(name != nullptr);
45 isolate_ = isolate;
46 name_ = name;
47 description_ = description;
48 unit_ = unit;
49}
50
51void Metric::InitInstance(const char* name,
52 const char* description,
53 Unit unit) {
54 // Only called once.
55 ASSERT(name != nullptr);
56 name_ = name;
57 description_ = description;
58 unit_ = unit;
59}
60
61static const char* UnitString(intptr_t unit) {
62 switch (unit) {
64 return "counter";
65 case Metric::kByte:
66 return "byte";
68 return "us";
69 default:
71 }
73 return nullptr;
74}
75
77 JSONObject obj(stream);
78 obj.AddProperty("type", "Counter");
79 obj.AddProperty("name", name_);
80 obj.AddProperty("description", description_);
81 obj.AddProperty("unit", UnitString(unit()));
82 if (isolate_ == nullptr && isolate_group_ == nullptr) {
83 obj.AddFixedServiceId("vm/metrics/%s", name_);
84 } else {
85 obj.AddFixedServiceId("metrics/native/%s", name_);
86 }
87 // TODO(johnmccutchan): Overflow?
88 double value_as_double = static_cast<double>(Value());
89 obj.AddProperty("value", value_as_double);
90}
91#endif // !defined(PRODUCT)
92
93char* Metric::ValueToString(int64_t value, Unit unit) {
94 Thread* thread = Thread::Current();
95 ASSERT(thread != nullptr);
96 Zone* zone = thread->zone();
97 ASSERT(zone != nullptr);
98 switch (unit) {
99 case kCounter:
100 return zone->PrintToString("%" Pd64 "", value);
101 case kByte: {
102 const char* scaled_suffix = "B";
103 double scaled_value = static_cast<double>(value);
104 if (value > GB) {
105 scaled_suffix = "GB";
106 scaled_value /= GB;
107 } else if (value > MB) {
108 scaled_suffix = "MB";
109 scaled_value /= MB;
110 } else if (value > KB) {
111 scaled_suffix = "kB";
112 scaled_value /= KB;
113 }
114 return zone->PrintToString("%.3f %s (%" Pd64 " B)", scaled_value,
115 scaled_suffix, value);
116 }
117 case kMicrosecond: {
118 const char* scaled_suffix = "us";
119 double scaled_value = static_cast<double>(value);
121 scaled_suffix = "s";
122 scaled_value /= kMicrosecondsPerSecond;
123 } else if (value > kMicrosecondsPerMillisecond) {
124 scaled_suffix = "ms";
125 scaled_value /= kMicrosecondsPerMillisecond;
126 }
127 return zone->PrintToString("%.3f %s (%" Pd64 " us)", scaled_value,
128 scaled_suffix, value);
129 }
130 default:
131 UNREACHABLE();
132 return nullptr;
133 }
134}
135
137 Thread* thread = Thread::Current();
138 ASSERT(thread != nullptr);
139 Zone* zone = thread->zone();
140 ASSERT(zone != nullptr);
141 return zone->PrintToString("%s %s", name(), ValueToString(Value(), unit()));
142}
143
147}
148
152}
153
157}
158
161 // UsedInWords requires a safepoint to access all the TLAB pointers without a
162 // data race, so coarsen this metric to capacity. Preferable to locking during
163 // new-space allocation.
165}
166
170}
171
175}
176
177int64_t MetricHeapUsed::Value() const {
181}
182
183#if !defined(PRODUCT)
186}
187
188int64_t MetricCurrentRSS::Value() const {
189 return Service::CurrentRSS();
190}
191
192int64_t MetricPeakRSS::Value() const {
193 return Service::MaxRSS();
194}
195#endif // !defined(PRODUCT)
196
199}
200
201void MaxMetric::SetValue(int64_t new_value) {
202 if (new_value > value()) {
203 set_value(new_value);
204 }
205}
206
209}
210
211void MinMetric::SetValue(int64_t new_value) {
212 if (new_value < value()) {
213 set_value(new_value);
214 }
215}
216
217} // namespace dart
#define UNREACHABLE()
Definition: assert.h:248
@ kNew
Definition: heap.h:38
@ kOld
Definition: heap.h:39
intptr_t ExternalInWords(Space space) const
Definition: heap.cc:811
intptr_t UsedInWords(Space space) const
Definition: heap.cc:802
intptr_t CapacityInWords(Space space) const
Definition: heap.cc:806
Heap * heap() const
Definition: isolate.h:296
static IsolateGroup * Current()
Definition: isolate.h:539
static intptr_t IsolateListLength()
Definition: isolate.cc:3541
void AddProperty(const char *name, bool b) const
Definition: json_stream.h:395
void AddFixedServiceId(const char *format,...) const PRINTF_ATTRIBUTE(2
Definition: json_stream.cc:503
void SetValue(int64_t new_value)
Definition: metrics.cc:201
virtual int64_t Value() const
Definition: metrics.cc:188
virtual int64_t Value() const
Definition: metrics.cc:167
virtual int64_t Value() const
Definition: metrics.cc:172
virtual int64_t Value() const
Definition: metrics.cc:159
virtual int64_t Value() const
Definition: metrics.cc:149
virtual int64_t Value() const
Definition: metrics.cc:154
virtual int64_t Value() const
Definition: metrics.cc:144
virtual int64_t Value() const
Definition: metrics.cc:177
virtual int64_t Value() const
Definition: metrics.cc:184
virtual int64_t Value() const
Definition: metrics.cc:192
void PrintJSON(JSONStream *stream)
Definition: metrics.cc:76
static char * ValueToString(int64_t value, Unit unit)
Definition: metrics.cc:93
IsolateGroup * isolate_group() const
Definition: metrics.h:108
void set_value(int64_t value)
Definition: metrics.h:96
virtual ~Metric()
Definition: metrics.cc:24
void InitInstance(Isolate *isolate, const char *name, const char *description, Unit unit)
Definition: metrics.cc:39
const char * description() const
Definition: metrics.h:101
virtual int64_t Value() const
Definition: metrics.h:114
Isolate * isolate() const
Definition: metrics.h:105
@ kMicrosecond
Definition: metrics.h:58
Unit unit() const
Definition: metrics.h:102
int64_t value() const
Definition: metrics.h:95
char * ToString()
Definition: metrics.cc:136
const char * name() const
Definition: metrics.h:100
void SetValue(int64_t new_value)
Definition: metrics.cc:211
static int64_t CurrentRSS()
Definition: service.cc:1451
static int64_t MaxRSS()
Definition: service.cc:1466
Zone * zone() const
Definition: thread_state.h:37
static Thread * Current()
Definition: thread.h:362
char * PrintToString(const char *format,...) PRINTF_ATTRIBUTE(2
Definition: zone.cc:313
#define ASSERT(E)
uint8_t value
Definition: dart_vm.cc:33
constexpr int64_t kMaxInt64
Definition: globals.h:486
constexpr intptr_t MB
Definition: globals.h:530
constexpr int64_t kMinInt64
Definition: globals.h:485
constexpr intptr_t kMicrosecondsPerMillisecond
Definition: globals.h:561
const char *const name
constexpr intptr_t kMicrosecondsPerSecond
Definition: globals.h:562
constexpr intptr_t KB
Definition: globals.h:528
constexpr intptr_t GB
Definition: globals.h:532
DEFINE_FLAG(bool, print_cluster_information, false, "Print information about clusters written to snapshot")
static const char * UnitString(intptr_t unit)
Definition: metrics.cc:61
constexpr intptr_t kWordSize
Definition: globals.h:509
#define Pd64
Definition: globals.h:416