counters.cc 8.88 KB
Newer Older
1
// Copyright 2012 the V8 project authors. All rights reserved.
2 3
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
4

5
#include "src/logging/counters.h"
6

7 8
#include "src/base/atomic-utils.h"
#include "src/base/platform/elapsed-timer.h"
9
#include "src/base/platform/time.h"
10
#include "src/builtins/builtins-definitions.h"
11
#include "src/execution/isolate.h"
12 13
#include "src/logging/log-inl.h"
#include "src/logging/log.h"
14

15 16
namespace v8 {
namespace internal {
17

18
StatsTable::StatsTable(Counters* counters)
19 20 21
    : lookup_function_(nullptr),
      create_histogram_function_(nullptr),
      add_histogram_sample_function_(nullptr) {}
22

23
void StatsTable::SetCounterFunction(CounterLookupCallback f) {
24 25
  lookup_function_ = f;
}
26

27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
namespace {
std::atomic<int> unused_counter_dump{0};
}

bool StatsCounter::Enabled() { return GetPtr() != &unused_counter_dump; }

std::atomic<int>* StatsCounter::SetupPtrFromStatsTable() {
  // {Init} must have been called.
  DCHECK_NOT_NULL(counters_);
  DCHECK_NOT_NULL(name_);
  int* location = counters_->FindLocation(name_);
  std::atomic<int>* ptr =
      location ? base::AsAtomicPtr(location) : &unused_counter_dump;
#ifdef DEBUG
  std::atomic<int>* old_ptr = ptr_.exchange(ptr, std::memory_order_release);
  DCHECK_IMPLIES(old_ptr, old_ptr == ptr);
#else
  ptr_.store(ptr, std::memory_order_release);
#endif
  return ptr;
47 48
}

49 50
void Histogram::AddSample(int sample) {
  if (Enabled()) {
51
    counters_->AddHistogramSample(histogram_, sample);
52 53 54
  }
}

55
void* Histogram::CreateHistogram() const {
56
  return counters_->CreateHistogram(name_, min_, max_, num_buckets_);
57 58
}

59 60 61 62 63 64
void TimedHistogram::Stop(base::ElapsedTimer* timer) {
  DCHECK(Enabled());
  AddTimedSample(timer->Elapsed());
  timer->Stop();
}

65 66
void TimedHistogram::AddTimedSample(base::TimeDelta sample) {
  if (Enabled()) {
67
    int64_t sample_int = resolution_ == TimedHistogramResolution::MICROSECOND
68 69 70 71 72 73
                             ? sample.InMicroseconds()
                             : sample.InMilliseconds();
    AddSample(static_cast<int>(sample_int));
  }
}

74 75 76 77 78
void TimedHistogram::RecordAbandon(base::ElapsedTimer* timer,
                                   Isolate* isolate) {
  if (Enabled()) {
    DCHECK(timer->IsStarted());
    timer->Stop();
79
    int64_t sample = resolution_ == TimedHistogramResolution::MICROSECOND
80 81 82 83 84
                         ? base::TimeDelta::Max().InMicroseconds()
                         : base::TimeDelta::Max().InMilliseconds();
    AddSample(static_cast<int>(sample));
  }
  if (isolate != nullptr) {
85 86
    V8FileLogger::CallEventLogger(isolate, name(), v8::LogEventStatus::kEnd,
                                  true);
87 88 89
  }
}

90 91 92 93 94 95 96 97 98 99 100 101
#ifdef DEBUG
bool TimedHistogram::ToggleRunningState(bool expect_to_run) const {
  static thread_local base::LazyInstance<
      std::unordered_map<const TimedHistogram*, bool>>::type active_timer =
      LAZY_INSTANCE_INITIALIZER;
  bool is_running = (*active_timer.Pointer())[this];
  DCHECK_NE(is_running, expect_to_run);
  (*active_timer.Pointer())[this] = !is_running;
  return true;
}
#endif

102
Counters::Counters(Isolate* isolate)
103 104
    :
#ifdef V8_RUNTIME_CALL_STATS
105
      runtime_call_stats_(RuntimeCallStats::kMainIsolateThread),
106 107 108 109
      worker_thread_runtime_call_stats_(),
#endif
      isolate_(isolate),
      stats_table_(this) {
110 111 112 113 114 115 116
  static const struct {
    Histogram Counters::*member;
    const char* caption;
    int min;
    int max;
    int num_buckets;
  } kHistograms[] = {
117
#define HR(name, caption, min, max, num_buckets) \
118 119
  {&Counters::name##_, #caption, min, max, num_buckets},
      HISTOGRAM_RANGE_LIST(HR)
120
#undef HR
121 122
  };
  for (const auto& histogram : kHistograms) {
123 124 125
    (this->*histogram.member)
        .Initialize(histogram.caption, histogram.min, histogram.max,
                    histogram.num_buckets, this);
126
  }
127

128 129
  const int DefaultTimedHistogramNumBuckets = 50;

130
  static const struct {
131
    NestedTimedHistogram Counters::*member;
132 133
    const char* caption;
    int max;
134 135
    TimedHistogramResolution res;
  } kNestedTimedHistograms[] = {
yangguo's avatar
yangguo committed
136
#define HT(name, caption, max, res) \
137 138
  {&Counters::name##_, #caption, max, TimedHistogramResolution::res},
      NESTED_TIMED_HISTOGRAM_LIST(HT) NESTED_TIMED_HISTOGRAM_LIST_SLOW(HT)
139
#undef HT
140
  };
141
  for (const auto& timer : kNestedTimedHistograms) {
142 143 144
    (this->*timer.member)
        .Initialize(timer.caption, 0, timer.max, timer.res,
                    DefaultTimedHistogramNumBuckets, this);
145 146 147 148 149 150
  }

  static const struct {
    TimedHistogram Counters::*member;
    const char* caption;
    int max;
151
    TimedHistogramResolution res;
152 153
  } kTimedHistograms[] = {
#define HT(name, caption, max, res) \
154
  {&Counters::name##_, #caption, max, TimedHistogramResolution::res},
155 156 157 158
      TIMED_HISTOGRAM_LIST(HT)
#undef HT
  };
  for (const auto& timer : kTimedHistograms) {
159 160 161
    (this->*timer.member)
        .Initialize(timer.caption, 0, timer.max, timer.res,
                    DefaultTimedHistogramNumBuckets, this);
162
  }
163

164 165 166 167 168 169
  static const struct {
    AggregatableHistogramTimer Counters::*member;
    const char* caption;
  } kAggregatableHistogramTimers[] = {
#define AHT(name, caption) {&Counters::name##_, #caption},
      AGGREGATABLE_HISTOGRAM_TIMER_LIST(AHT)
170
#undef AHT
171 172
  };
  for (const auto& aht : kAggregatableHistogramTimers) {
173 174 175
    (this->*aht.member)
        .Initialize(aht.caption, 0, 10000000, DefaultTimedHistogramNumBuckets,
                    this);
176
  }
177

178 179 180 181 182 183
  static const struct {
    Histogram Counters::*member;
    const char* caption;
  } kHistogramPercentages[] = {
#define HP(name, caption) {&Counters::name##_, #caption},
      HISTOGRAM_PERCENTAGE_LIST(HP)
184
#undef HP
185 186
  };
  for (const auto& percentage : kHistogramPercentages) {
187 188
    (this->*percentage.member)
        .Initialize(percentage.caption, 0, 101, 100, this);
189
  }
190

191 192 193 194 195 196 197 198 199 200 201 202
  // Exponential histogram assigns bucket limits to points
  // p[1], p[2], ... p[n] such that p[i+1] / p[i] = constant.
  // The constant factor is equal to the n-th root of (high / low),
  // where the n is the number of buckets, the low is the lower limit,
  // the high is the upper limit.
  // For n = 50, low = 1000, high = 500000: the factor = 1.13.
  static const struct {
    Histogram Counters::*member;
    const char* caption;
  } kLegacyMemoryHistograms[] = {
#define HM(name, caption) {&Counters::name##_, #caption},
      HISTOGRAM_LEGACY_MEMORY_LIST(HM)
203
#undef HM
204 205
  };
  for (const auto& histogram : kLegacyMemoryHistograms) {
206 207
    (this->*histogram.member)
        .Initialize(histogram.caption, 1000, 500000, 50, this);
208
  }
209

210
  static constexpr struct {
211 212 213
    StatsCounter Counters::*member;
    const char* caption;
  } kStatsCounters[] = {
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
#define SC(name, caption) {&Counters::name##_, "c:" caption},
#define BARE_SC(name, caption) SC(name, #caption)
#define COUNT_AND_SIZE_SC(name)            \
  SC(count_of_##name, "V8.CountOf_" #name) \
  SC(size_of_##name, "V8.SizeOf_" #name)
#define CODE_KIND_SC(name) COUNT_AND_SIZE_SC(CODE_TYPE_##name)
#define FIXED_ARRAY_INSTANCE_TYPE_SC(name) COUNT_AND_SIZE_SC(FIXED_ARRAY_##name)

      // clang-format off
  STATS_COUNTER_LIST_1(BARE_SC)
  STATS_COUNTER_LIST_2(BARE_SC)
  STATS_COUNTER_NATIVE_CODE_LIST(BARE_SC)
  INSTANCE_TYPE_LIST(COUNT_AND_SIZE_SC)
  CODE_KIND_LIST(CODE_KIND_SC)
  FIXED_ARRAY_SUB_INSTANCE_TYPE_LIST(FIXED_ARRAY_INSTANCE_TYPE_SC)
  // clang-format on

#undef FIXED_ARRAY_INSTANCE_TYPE_SC
#undef CODE_KIND_SC
#undef COUNT_AND_SIZE_SC
#undef BARE_SC
235
#undef SC
236 237
  };
  for (const auto& counter : kStatsCounters) {
238
    (this->*counter.member).Init(this, counter.caption);
239
  }
240 241
}

242 243 244
void Counters::ResetCounterFunction(CounterLookupCallback f) {
  stats_table_.SetCounterFunction(f);

245 246 247
#define SC(name, caption) name##_.Reset();
  STATS_COUNTER_LIST_1(SC)
  STATS_COUNTER_LIST_2(SC)
248
  STATS_COUNTER_NATIVE_CODE_LIST(SC)
249 250
#undef SC

251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
#define SC(name)              \
  count_of_##name##_.Reset(); \
  size_of_##name##_.Reset();
  INSTANCE_TYPE_LIST(SC)
#undef SC

#define SC(name)                        \
  count_of_CODE_TYPE_##name##_.Reset(); \
  size_of_CODE_TYPE_##name##_.Reset();
  CODE_KIND_LIST(SC)
#undef SC

#define SC(name)                          \
  count_of_FIXED_ARRAY_##name##_.Reset(); \
  size_of_FIXED_ARRAY_##name##_.Reset();
  FIXED_ARRAY_SUB_INSTANCE_TYPE_LIST(SC)
#undef SC
}

270 271
void Counters::ResetCreateHistogramFunction(CreateHistogramCallback f) {
  stats_table_.SetCreateHistogramFunction(f);
272

273 274 275 276
#define HR(name, caption, min, max, num_buckets) name##_.Reset();
  HISTOGRAM_RANGE_LIST(HR)
#undef HR

yangguo's avatar
yangguo committed
277
#define HT(name, caption, max, res) name##_.Reset();
278
  NESTED_TIMED_HISTOGRAM_LIST(HT)
279 280
#undef HT

281
#define HT(name, caption, max, res) name##_.Reset();
282
  NESTED_TIMED_HISTOGRAM_LIST_SLOW(HT)
283 284
#undef HT

285
#define HT(name, caption, max, res) name##_.Reset();
286
  TIMED_HISTOGRAM_LIST(HT)
287 288
#undef HT

289
#define AHT(name, caption) name##_.Reset();
290
  AGGREGATABLE_HISTOGRAM_TIMER_LIST(AHT)
291 292
#undef AHT

293
#define HP(name, caption) name##_.Reset();
294
  HISTOGRAM_PERCENTAGE_LIST(HP)
295 296 297
#undef HP

#define HM(name, caption) name##_.Reset();
298
  HISTOGRAM_LEGACY_MEMORY_LIST(HM)
299 300 301
#undef HM
}

302 303
}  // namespace internal
}  // namespace v8