Ninja
metrics.h
Go to the documentation of this file.
00001 // Copyright 2011 Google Inc. All Rights Reserved.
00002 //
00003 // Licensed under the Apache License, Version 2.0 (the "License");
00004 // you may not use this file except in compliance with the License.
00005 // You may obtain a copy of the License at
00006 //
00007 //     http://www.apache.org/licenses/LICENSE-2.0
00008 //
00009 // Unless required by applicable law or agreed to in writing, software
00010 // distributed under the License is distributed on an "AS IS" BASIS,
00011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00012 // See the License for the specific language governing permissions and
00013 // limitations under the License.
00014 
00015 #ifndef NINJA_METRICS_H_
00016 #define NINJA_METRICS_H_
00017 
00018 #include <string>
00019 #include <vector>
00020 using namespace std;
00021 
00022 #include "util.h"  // For int64_t.
00023 
00024 /// The Metrics module is used for the debug mode that dumps timing stats of
00025 /// various actions.  To use, see METRIC_RECORD below.
00026 
00027 /// A single metrics we're tracking, like "depfile load time".
00028 struct Metric {
00029   string name;
00030   /// Number of times we've hit the code path.
00031   int count;
00032   /// Total time (in micros) we've spent on the code path.
00033   int64_t sum;
00034 };
00035 
00036 
00037 /// A scoped object for recording a metric across the body of a function.
00038 /// Used by the METRIC_RECORD macro.
00039 struct ScopedMetric {
00040   explicit ScopedMetric(Metric* metric);
00041   ~ScopedMetric();
00042 
00043 private:
00044   Metric* metric_;
00045   /// Timestamp when the measurement started.
00046   /// Value is platform-dependent.
00047   int64_t start_;
00048 };
00049 
00050 /// The singleton that stores metrics and prints the report.
00051 struct Metrics {
00052   Metric* NewMetric(const string& name);
00053 
00054   /// Print a summary report to stdout.
00055   void Report();
00056 
00057 private:
00058   vector<Metric*> metrics_;
00059 };
00060 
00061 /// Get the current time as relative to some epoch.
00062 /// Epoch varies between platforms; only useful for measuring elapsed time.
00063 int64_t GetTimeMillis();
00064 
00065 /// A simple stopwatch which returns the time
00066 /// in seconds since Restart() was called.
00067 struct Stopwatch {
00068  public:
00069   Stopwatch() : started_(0) {}
00070 
00071   /// Seconds since Restart() call.
00072   double Elapsed() const {
00073     return 1e-6 * static_cast<double>(Now() - started_);
00074   }
00075 
00076   void Restart() { started_ = Now(); }
00077 
00078  private:
00079   uint64_t started_;
00080   uint64_t Now() const;
00081 };
00082 
00083 /// The primary interface to metrics.  Use METRIC_RECORD("foobar") at the top
00084 /// of a function to get timing stats recorded for each call of the function.
00085 #define METRIC_RECORD(name)                                             \
00086   static Metric* metrics_h_metric =                                     \
00087       g_metrics ? g_metrics->NewMetric(name) : NULL;                    \
00088   ScopedMetric metrics_h_scoped(metrics_h_metric);
00089 
00090 extern Metrics* g_metrics;
00091 
00092 #endif // NINJA_METRICS_H_