• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2015 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 #ifndef STATS_H_
16 #define STATS_H_
17 
18 #include <mutex>
19 #include <string>
20 #include <unordered_map>
21 
22 using namespace std;
23 
24 class Stats {
25  public:
26   explicit Stats(const char* name);
27 
28   void DumpTop() const;
29   string String() const;
30 
31  private:
32   void Start();
33   double End(const char* msg);
34 
35   friend class ScopedStatsRecorder;
36 
37   const char* name_;
38   double elapsed_;
39   int cnt_;
40   mutable mutex mu_;
41   unordered_map<string, double> detailed_;
42 };
43 
44 class ScopedStatsRecorder {
45  public:
46   explicit ScopedStatsRecorder(Stats* st, const char* msg = 0);
47   ~ScopedStatsRecorder();
48 
49  private:
50   Stats* st_;
51   const char* msg_;
52 };
53 
54 void ReportAllStats();
55 
56 #define COLLECT_STATS(name) \
57   static Stats stats(name); \
58   ScopedStatsRecorder ssr(&stats)
59 
60 #define COLLECT_STATS_WITH_SLOW_REPORT(name, msg) \
61   static Stats stats(name);                       \
62   ScopedStatsRecorder ssr(&stats, msg)
63 
64 #endif  // STATS_H_
65