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 #ifndef _WIN32
22 #include <sys/time.h>
23 #else
24 #include <windows.h>
25 #endif
26 
27 #include <algorithm>
28 
29 #include "util.h"
30 
31 using namespace std;
32 
34 
35 namespace {
36 
37 #ifndef _WIN32
38 /// Compute a platform-specific high-res timer value that fits into an int64.
39 int64_t HighResTimer() {
40  timeval tv;
41  if (gettimeofday(&tv, NULL) < 0)
42  Fatal("gettimeofday: %s", strerror(errno));
43  return (int64_t)tv.tv_sec * 1000*1000 + tv.tv_usec;
44 }
45 
46 /// Convert a delta of HighResTimer() values to microseconds.
47 int64_t TimerToMicros(int64_t dt) {
48  // No conversion necessary.
49  return dt;
50 }
51 #else
52 int64_t LargeIntegerToInt64(const LARGE_INTEGER& i) {
53  return ((int64_t)i.HighPart) << 32 | i.LowPart;
54 }
55 
56 int64_t HighResTimer() {
57  LARGE_INTEGER counter;
58  if (!QueryPerformanceCounter(&counter))
59  Fatal("QueryPerformanceCounter: %s", GetLastErrorString().c_str());
60  return LargeIntegerToInt64(counter);
61 }
62 
63 int64_t TimerToMicros(int64_t dt) {
64  static int64_t ticks_per_sec = 0;
65  if (!ticks_per_sec) {
66  LARGE_INTEGER freq;
67  if (!QueryPerformanceFrequency(&freq))
68  Fatal("QueryPerformanceFrequency: %s", GetLastErrorString().c_str());
69  ticks_per_sec = LargeIntegerToInt64(freq);
70  }
71 
72  // dt is in ticks. We want microseconds.
73  return (dt * 1000000) / ticks_per_sec;
74 }
75 #endif
76 
77 } // anonymous namespace
78 
79 
81  metric_ = metric;
82  if (!metric_)
83  return;
84  start_ = HighResTimer();
85 }
87  if (!metric_)
88  return;
89  metric_->count++;
90  int64_t dt = TimerToMicros(HighResTimer() - start_);
91  metric_->sum += dt;
92 }
93 
94 Metric* Metrics::NewMetric(const string& name) {
95  Metric* metric = new Metric;
96  metric->name = name;
97  metric->count = 0;
98  metric->sum = 0;
99  metrics_.push_back(metric);
100  return metric;
101 }
102 
104  int width = 0;
105  for (vector<Metric*>::iterator i = metrics_.begin();
106  i != metrics_.end(); ++i) {
107  width = max((int)(*i)->name.size(), width);
108  }
109 
110  printf("%-*s\t%-6s\t%-9s\t%s\n", width,
111  "metric", "count", "avg (us)", "total (ms)");
112  for (vector<Metric*>::iterator i = metrics_.begin();
113  i != metrics_.end(); ++i) {
114  Metric* metric = *i;
115  double total = metric->sum / (double)1000;
116  double avg = metric->sum / (double)metric->count;
117  printf("%-*s\t%-6d\t%-8.1f\t%.1f\n", width, metric->name.c_str(),
118  metric->count, avg, total);
119  }
120 }
121 
123  return TimerToMicros(HighResTimer());
124 }
125 
127  return TimerToMicros(HighResTimer()) / 1000;
128 }
129 
ScopedMetric(Metric *metric)
Definition: metrics.cc:80
void Report()
Print a summary report to stdout.
Definition: metrics.cc:103
int64_t sum
Total time (in micros) we&#39;ve spent on the code path.
Definition: metrics.h:32
int count
Number of times we&#39;ve hit the code path.
Definition: metrics.h:30
int64_t GetTimeMillis()
Get the current time as relative to some epoch.
Definition: metrics.cc:126
Metrics * g_metrics
Definition: metrics.cc:33
The Metrics module is used for the debug mode that dumps timing stats of various actions.
Definition: metrics.h:27
std::string name
Definition: metrics.h:28
signed long long int64_t
A 64-bit integer type.
Definition: win32port.h:28
Metric * NewMetric(const std::string &name)
Definition: metrics.cc:94
~ScopedMetric()
Definition: metrics.cc:86
uint64_t Now() const
Definition: metrics.cc:122
void Fatal(const char *msg,...)
Log a fatal message and exit.
Definition: util.cc:59
The singleton that stores metrics and prints the report.
Definition: metrics.h:50
unsigned long long uint64_t
Definition: win32port.h:29