Skip to content
This repository was archived by the owner on Sep 17, 2024. It is now read-only.

Commit da2e30f

Browse files
authored
fix(profiling): cap double precision (#206)
* fix(profiling): cap double precision * fix(headers): use math.h
1 parent 9e209fe commit da2e30f

File tree

1 file changed

+18
-23
lines changed

1 file changed

+18
-23
lines changed

bindings/cpu_profiler.cc

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
#include <node_api.h>
33

44
#include <assert.h>
5+
#include <math.h>
6+
57
#include <string>
68
#include <unordered_map>
79
#include <functional>
@@ -109,14 +111,8 @@ void MeasurementsTicker::heap_callback() {
109111
isolate->GetHeapStatistics(&heap_stats);
110112
uint64_t ts = uv_hrtime();
111113

112-
auto it = heap_listeners.begin();
113-
while (it != heap_listeners.end()) {
114-
if (it->second(ts, heap_stats)) {
115-
it = heap_listeners.erase(it);
116-
}
117-
else {
118-
++it;
119-
}
114+
for(auto cb : heap_listeners) {
115+
cb.second(ts, heap_stats);
120116
}
121117
}
122118

@@ -149,7 +145,7 @@ void MeasurementsTicker::cpu_callback() {
149145
uint64_t ts = uv_hrtime();
150146
if(count < 1) {
151147
for(auto cb : cpu_listeners) {
152-
cb.second(ts, 0);
148+
cb.second(ts, 0.0);
153149
}
154150
return;
155151
}
@@ -173,21 +169,14 @@ void MeasurementsTicker::cpu_callback() {
173169
double total_avg = total / count;
174170
double rate = 1.0 - idle_avg / total_avg;
175171

176-
auto it = cpu_listeners.begin();
177-
178172
if(rate < 0.0) {
179173
rate = 0.0;
180174
}
181175

182-
while (it != cpu_listeners.end()) {
183-
if (it->second(ts, rate)) {
184-
it = cpu_listeners.erase(it);
185-
}
186-
else {
187-
++it;
188-
}
176+
for(auto cb : cpu_listeners) {
177+
cb.second(ts, rate);
189178
}
190-
179+
191180
uv_free_cpu_info(cpu, count);
192181
}
193182

@@ -644,14 +633,18 @@ static void GetSamples(const napi_env& env, const v8::CpuProfile* profile, const
644633
}
645634
}
646635

636+
static double RoundDoubleToPrecision(double value, double precision){
637+
return (floor((value * pow(10, precision) + 0.5)) / pow(10, precision));
638+
}
639+
647640
static napi_value TranslateMeasurementsDouble(const napi_env& env, const char* unit, const uint16_t size, const std::vector<double>& values, const std::vector<uint64_t>& timestamps) {
648641
if (size > values.size() || size > timestamps.size()) {
649-
napi_throw_range_error(env, "NAPI_ERROR", "Memory measurement size is larger than the number of values or timestamps");
642+
napi_throw_range_error(env, "NAPI_ERROR", "CPU measurement size is larger than the number of values or timestamps");
650643
return nullptr;
651644
}
652645

653646
if (values.size() != timestamps.size()) {
654-
napi_throw_range_error(env, "NAPI_ERROR", "Memory measurement entries are corrupt, expected values and timestamps to be of equal length");
647+
napi_throw_range_error(env, "NAPI_ERROR", "CPU measurement entries are corrupt, expected values and timestamps to be of equal length");
655648
return nullptr;
656649
}
657650

@@ -665,12 +658,14 @@ static napi_value TranslateMeasurementsDouble(const napi_env& env, const char* u
665658
napi_value values_array;
666659
napi_create_array(env, &values_array);
667660

668-
for (size_t i = 0; i < size; i++) {
661+
uint16_t idx = size;
662+
663+
for (size_t i = 0; i < idx; i++) {
669664
napi_value entry;
670665
napi_create_object(env, &entry);
671666

672667
napi_value value;
673-
napi_create_double(env, values[i], &value);
668+
napi_create_double(env, RoundDoubleToPrecision(values[i], 4), &value);
674669

675670
napi_value ts;
676671
napi_create_int64(env, timestamps[i], &ts);

0 commit comments

Comments
 (0)