MAST3
Multidisciplinary-design Adaptation and Sensitivity Toolkit (MAST)
perf_log.hpp
Go to the documentation of this file.
1 /*
2 * MAST: Multidisciplinary-design Adaptation and Sensitivity Toolkit
3 * Copyright (C) 2013-2020 Manav Bhatia and MAST authors
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19 
20 #ifndef __mast__perflog_h__
21 #define __mast__perflog_h__
22 
23 #include <sys/time.h>
24 #include <sys/resource.h>
25 #include <map>
26 #include <stack>
27 
29 #include <mast/base/exceptions.hpp>
30 
31 namespace MAST {
32 namespace Utility {
33 
35 
36 public:
37 
38  struct EventData {
39 
40  EventData(): sys(0.), user(0.), ncalls(0) { }
41 
45  };
46 
47 
48  class Event {
49 
50  public:
51 
52  inline Event(const std::string& nm):
53  _name_ref (nm),
54  _sys_time (0.),
55  _user_time (0.)
56  { }
57 
58 
60  _name_ref (e._name_ref),
61  _sys_time (e._sys_time),
62  _user_time (e._user_time)
63  { }
64 
65  inline ~Event() { }
66 
67  inline const std::string& name() const { return _name_ref;}
68 
69  inline void start() {
70 
71  rusage r;
72  getrusage(RUSAGE_SELF, &r);
73  _sys_time = - (r.ru_stime.tv_sec + 1.e-6 * r.ru_stime.tv_usec);
74  _user_time = - (r.ru_utime.tv_sec + 1.e-6 * r.ru_utime.tv_usec);
75  }
76 
77  inline void stop() {
78 
79  rusage r;
80  getrusage(RUSAGE_SELF, &r);
81  _sys_time += r.ru_stime.tv_sec + 1.e-6 * r.ru_stime.tv_usec;
82  _user_time += r.ru_utime.tv_sec + 1.e-6 * r.ru_utime.tv_usec;
83  }
84 
85  inline real_t get_sys_time () const { return _sys_time; }
86  inline real_t get_user_time () const { return _user_time; }
87 
88  private:
89 
90  const std::string& _name_ref;
93  };
94 
95  using map_type = std::map<std::string, MAST::Utility::PerformanceLogging::EventData>;
96  using stack_type = std::stack<MAST::Utility::PerformanceLogging::Event>;
97 
99 
101 
102  inline void start_event(const std::string& nm) {
103 
104  map_type::iterator it = _get_event_data(nm);
106  }
107 
108  inline void stop_event(const std::string& nm) {
109 
110 
111  // the object cannot be empty
112  Assert1(!_event_stack.empty(), !_event_stack.empty(),
113  "Empty event stack");
114 
116  v = _event_stack.top();
117 
118  // make sure that the top object has the same name as provided here
119  Assert0(nm == v.name(),
120  "Name provided must match as the last started event.");
121 
122  map_type::iterator it = _get_event_data(nm);
123  it->second.ncalls++;
124  it->second.sys += v.get_sys_time();
125  it->second.user += v.get_user_time();
126 
127  _event_stack.pop();
128  }
129 
130 
131 protected:
132 
133  inline map_type::iterator _get_event_data(const std::string& nm) {
134 
135  map_type::iterator
136  it = _event_time.find(nm),
137  end = _event_time.end();
138 
139  if (it == end)
140  it = _event_time.insert(std::make_pair(nm, MAST::Utility::PerformanceLogging::EventData())).first;
141 
142  return it;
143  }
144 
147 };
148 
149 } // namespace Utility
150 } // namespace MAST
151 
152 #endif // __mast__perflog_h__
153 
#define Assert1(cond, v1, msg)
Definition: exceptions.hpp:143
std::map< std::string, MAST::Utility::PerformanceLogging::EventData > map_type
Definition: perf_log.hpp:95
#define Assert0(cond, msg)
Definition: exceptions.hpp:134
std::stack< MAST::Utility::PerformanceLogging::Event > stack_type
Definition: perf_log.hpp:96
unsigned int uint_t
const std::string & name() const
Definition: perf_log.hpp:67
void stop_event(const std::string &nm)
Definition: perf_log.hpp:108
Event(const MAST::Utility::PerformanceLogging::Event &e)
Definition: perf_log.hpp:59
void start_event(const std::string &nm)
Definition: perf_log.hpp:102
double real_t
map_type::iterator _get_event_data(const std::string &nm)
Definition: perf_log.hpp:133