Ninja
metrics.cc
Go to the documentation of this file.
1 // Copyright 2011 Google Inc. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "metrics.h"
16 
17 #include <errno.h>
18 #include <stdio.h>
19 #include <string.h>
20 
21 #include <algorithm>
22 #include <chrono>
23 
24 #include "util.h"
25 
26 using namespace std;
27 
29 
30 namespace {
31 
32 /// Compute a platform-specific high-res timer value that fits into an int64.
33 int64_t HighResTimer() {
34  auto now = chrono::steady_clock::now();
35  return chrono::duration_cast<chrono::steady_clock::duration>(
36  now.time_since_epoch())
37  .count();
38 }
39 
40 int64_t TimerToMicros(int64_t dt) {
41  // dt is in ticks. We want microseconds.
42  return chrono::duration_cast<chrono::microseconds>(
43  std::chrono::steady_clock::duration{ dt })
44  .count();
45 }
46 
47 int64_t TimerToMicros(double dt) {
48  // dt is in ticks. We want microseconds.
49  using DoubleSteadyClock =
50  std::chrono::duration<double, std::chrono::steady_clock::period>;
51  return chrono::duration_cast<chrono::microseconds>(DoubleSteadyClock{ dt })
52  .count();
53 }
54 
55 } // anonymous namespace
56 
58  metric_ = metric;
59  if (!metric_)
60  return;
61  start_ = HighResTimer();
62 }
64  if (!metric_)
65  return;
66  metric_->count++;
67  // Leave in the timer's natural frequency to avoid paying the conversion cost
68  // on every measurement.
69  int64_t dt = HighResTimer() - start_;
70  metric_->sum += dt;
71 }
72 
73 Metric* Metrics::NewMetric(const string& name) {
74  Metric* metric = new Metric;
75  metric->name = name;
76  metric->count = 0;
77  metric->sum = 0;
78  metrics_.push_back(metric);
79  return metric;
80 }
81 
83  int width = 0;
84  for (vector<Metric*>::iterator i = metrics_.begin();
85  i != metrics_.end(); ++i) {
86  width = max((int)(*i)->name.size(), width);
87  }
88 
89  printf("%-*s\t%-6s\t%-9s\t%s\n", width,
90  "metric", "count", "avg (us)", "total (ms)");
91  for (vector<Metric*>::iterator i = metrics_.begin();
92  i != metrics_.end(); ++i) {
93  Metric* metric = *i;
94  uint64_t micros = TimerToMicros(metric->sum);
95  double total = micros / (double)1000;
96  double avg = micros / (double)metric->count;
97  printf("%-*s\t%-6d\t%-8.1f\t%.1f\n", width, metric->name.c_str(),
98  metric->count, avg, total);
99  }
100 }
101 
102 double Stopwatch::Elapsed() const {
103  // Convert to micros after converting to double to minimize error.
104  return 1e-6 * TimerToMicros(static_cast<double>(NowRaw() - started_));
105 }
106 
108  return HighResTimer();
109 }
110 
112  return TimerToMicros(HighResTimer()) / 1000;
113 }
Metrics * g_metrics
Definition: metrics.cc:28
int64_t GetTimeMillis()
Get the current time as relative to some epoch.
Definition: metrics.cc:111
Definition: hash_map.h:26
The Metrics module is used for the debug mode that dumps timing stats of various actions.
Definition: metrics.h:27
int count
Number of times we've hit the code path.
Definition: metrics.h:30
std::string name
Definition: metrics.h:28
int64_t sum
Total time (in platform-dependent units) we've spent on the code path.
Definition: metrics.h:32
The singleton that stores metrics and prints the report.
Definition: metrics.h:49
void Report()
Print a summary report to stdout.
Definition: metrics.cc:82
Metric * NewMetric(const std::string &name)
Definition: metrics.cc:73
ScopedMetric(Metric *metric)
Definition: metrics.cc:57
~ScopedMetric()
Definition: metrics.cc:63
uint64_t NowRaw() const
Definition: metrics.cc:107
double Elapsed() const
Seconds since Restart() call.
Definition: metrics.cc:102
unsigned long long uint64_t
Definition: win32port.h:29
signed long long int64_t
A 64-bit integer type.
Definition: win32port.h:28