ca4f97a350b5c1ac4912b482db1c2d2a91bce4d1
[platform/upstream/ninja.git] / src / metrics.cc
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 "util.h"
28
29 Metrics* g_metrics = NULL;
30
31 namespace {
32
33 #ifndef _WIN32
34 /// Compute a platform-specific high-res timer value that fits into an int64.
35 int64_t HighResTimer() {
36   timeval tv;
37   if (gettimeofday(&tv, NULL) < 0)
38     Fatal("gettimeofday: %s", strerror(errno));
39   return (int64_t)tv.tv_sec * 1000*1000 + tv.tv_usec;
40 }
41
42 /// Convert a delta of HighResTimer() values to microseconds.
43 int64_t TimerToMicros(int64_t dt) {
44   // No conversion necessary.
45   return dt;
46 }
47 #else
48 int64_t LargeIntegerToInt64(const LARGE_INTEGER& i) {
49   return ((int64_t)i.HighPart) << 32 | i.LowPart;
50 }
51
52 int64_t HighResTimer() {
53   LARGE_INTEGER counter;
54   if (!QueryPerformanceCounter(&counter))
55     Fatal("QueryPerformanceCounter: %s", GetLastErrorString().c_str());
56   return LargeIntegerToInt64(counter);
57 }
58
59 int64_t TimerToMicros(int64_t dt) {
60   static int64_t ticks_per_sec = 0;
61   if (!ticks_per_sec) {
62     LARGE_INTEGER freq;
63     if (!QueryPerformanceFrequency(&freq))
64       Fatal("QueryPerformanceFrequency: %s", GetLastErrorString().c_str());
65     ticks_per_sec = LargeIntegerToInt64(freq);
66   }
67
68   // dt is in ticks.  We want microseconds.
69   return (dt * 1000000) / ticks_per_sec;
70 }
71 #endif
72
73 }  // anonymous namespace
74
75
76 ScopedMetric::ScopedMetric(Metric* metric) {
77   metric_ = metric;
78   if (!metric_)
79     return;
80   start_ = HighResTimer();
81 }
82 ScopedMetric::~ScopedMetric() {
83   if (!metric_)
84     return;
85   metric_->count++;
86   int64_t dt = TimerToMicros(HighResTimer() - start_);
87   metric_->sum += dt;
88 }
89
90 Metric* Metrics::NewMetric(const string& name) {
91   Metric* metric = new Metric;
92   metric->name = name;
93   metric->count = 0;
94   metric->sum = 0;
95   metrics_.push_back(metric);
96   return metric;
97 }
98
99 void Metrics::Report() {
100   int width = 0;
101   for (vector<Metric*>::iterator i = metrics_.begin();
102        i != metrics_.end(); ++i) {
103     width = max((int)(*i)->name.size(), width);
104   }
105
106   printf("%-*s\t%-6s\t%-9s\t%s\n", width,
107          "metric", "count", "avg (us)", "total (ms)");
108   for (vector<Metric*>::iterator i = metrics_.begin();
109        i != metrics_.end(); ++i) {
110     Metric* metric = *i;
111     double total = metric->sum / (double)1000;
112     double avg = metric->sum / (double)metric->count;
113     printf("%-*s\t%-6d\t%-8.1f\t%.1f\n", width, metric->name.c_str(),
114            metric->count, avg, total);
115   }
116 }
117
118 uint64_t Stopwatch::Now() const {
119   return TimerToMicros(HighResTimer());
120 }
121
122 int64_t GetTimeMillis() {
123   return TimerToMicros(HighResTimer()) / 1000;
124 }
125