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
33 Metrics* g_metrics = NULL;
34
35 namespace {
36
37 #ifndef _WIN32
38 /// Compute a platform-specific high-res timer value that fits into an int64.
HighResTimer()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.
TimerToMicros(int64_t dt)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
ScopedMetric(Metric * metric)80 ScopedMetric::ScopedMetric(Metric* metric) {
81 metric_ = metric;
82 if (!metric_)
83 return;
84 start_ = HighResTimer();
85 }
~ScopedMetric()86 ScopedMetric::~ScopedMetric() {
87 if (!metric_)
88 return;
89 metric_->count++;
90 int64_t dt = TimerToMicros(HighResTimer() - start_);
91 metric_->sum += dt;
92 }
93
NewMetric(const string & name)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
Report()103 void Metrics::Report() {
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
Now() const122 uint64_t Stopwatch::Now() const {
123 return TimerToMicros(HighResTimer());
124 }
125
GetTimeMillis()126 int64_t GetTimeMillis() {
127 return TimerToMicros(HighResTimer()) / 1000;
128 }
129
130