summaryrefslogtreecommitdiffstats
path: root/src/metrics.h
diff options
context:
space:
mode:
authorEvan Martin <martine@danga.com>2012-01-05 21:35:47 (GMT)
committerEvan Martin <martine@danga.com>2012-01-05 22:03:55 (GMT)
commitd5165bafdabbfba23d33a5e0ceb9e0e680384183 (patch)
tree7f61a5d96d62c1f75d60b761b7d1059a0783c2bd /src/metrics.h
parent9e31e01595656cc2dacd2f97fb6e765799518254 (diff)
downloadNinja-d5165bafdabbfba23d33a5e0ceb9e0e680384183.zip
Ninja-d5165bafdabbfba23d33a5e0ceb9e0e680384183.tar.gz
Ninja-d5165bafdabbfba23d33a5e0ceb9e0e680384183.tar.bz2
add a '-d stats' flag for detailed timings
1) Add a system for recording detailed timing info of functions. 2) Add a -d flag for requesting debug info at runtime, with the above as the first user.
Diffstat (limited to 'src/metrics.h')
-rw-r--r--src/metrics.h64
1 files changed, 64 insertions, 0 deletions
diff --git a/src/metrics.h b/src/metrics.h
new file mode 100644
index 0000000..74f5f8f
--- /dev/null
+++ b/src/metrics.h
@@ -0,0 +1,64 @@
+// Copyright 2011 Google Inc. All Rights Reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#include <string>
+#include <vector>
+using namespace std;
+
+#include "util.h" // For int64_t.
+
+/// The Metrics module is used for the debug mode that dumps timing stats of
+/// various actions. To use, see METRIC_RECORD below.
+
+/// A single metrics we're tracking, like "depfile load time".
+struct Metric {
+ string name;
+ /// Number of times we've hit the code path.
+ int count;
+ /// Total time (in micros) we've spent on the code path.
+ int64_t sum;
+};
+
+/// A scoped object for recording a metric across the body of a function.
+/// Used by the METRIC_RECORD macro.
+struct ScopedMetric {
+ explicit ScopedMetric(Metric* metric);
+ ~ScopedMetric();
+
+private:
+ Metric* metric_;
+ /// Timestamp when the measurement started.
+ /// Value is platform-dependent.
+ int64_t start_;
+};
+
+/// The singleton that stores metrics and prints the report.
+struct Metrics {
+ Metric* NewMetric(const string& name);
+
+ /// Print a summary report to stdout.
+ void Report();
+
+private:
+ vector<Metric*> metrics_;
+};
+
+/// The primary interface to metrics. Use METRIC_RECORD("foobar") at the top
+/// of a function to get timing stats recorded for each call of the function.
+#define METRIC_RECORD(name) \
+ static Metric* metrics_h_metric = \
+ g_metrics ? g_metrics->NewMetric(name) : NULL; \
+ ScopedMetric metrics_h_scoped(metrics_h_metric);
+
+extern Metrics* g_metrics;