OpenZGY/C++ API and Internals (ALPHA)
Access seismic data stored in ZGY format.
timer.h
Go to the documentation of this file.
1 // Copyright 2017-2020, Schlumberger
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 #pragma once
16 
17 #include "../declspec.h"
18 
19 namespace InternalZGY {
20 #if 0
21 }
22 #endif
23 
52 class OPENZGY_TEST_API Timer {
53 
54 private: // readonly, i.e. set only in constructor
55  bool enabled_; // If false, the class is just a stub.
56  int skip_; // Do not accumulate the N first results
57  long long frequency_; // Frequency of internal timer, in Hertz
58  long long overhead_; // Estimated overhead of reading timer, in usec
59  char name_[256]; // Optional name of this timer
60  char buff_[256]; // Returned from getValue().
61 
62 private: // mutable
63  int laps_; // Number of accumulated results
64  long long last_; // Time for last lap
65  long long total_; // Accumulated total time
66  long long begin_; // Time of last call to Start();
67  long long end_; // Time of last call to Stop();
68  bool running_; // Guard against two Stop() in a row.
69  int verbose_; // Used by higher levels only.
70 
71 private: // non-inlined versions of operations
72  void doStart();
73  void doStop();
74  void doReset();
75  long long getNativeTime();
76  long long getNativeFrequency();
77 
78 public:
79  Timer(bool enable = true, const char* name = 0, int skip = 0, bool startrunning = true);
80 
81  // ACCESSORS
82  bool getEnabled() const { return enabled_; }
83  double getFrequency() const { return static_cast<double>(frequency_); }
84  double getLast() const { return static_cast<double>(last_) / static_cast<double>(frequency_); }
85  double getTotal() const { return static_cast<double>(total_) / static_cast<double>(frequency_); }
86  double getOverhead() const { return static_cast<double>(overhead_) / static_cast<double>(frequency_); }
87  int getCount() const { return laps_ < skip_ ? 0 : laps_ - skip_; }
88  const char* getName() const { return name_; }
89  int getSkip() const { return skip_ < laps_ ? skip_ : laps_; }
90  bool getRunning() const { return running_; }
91  int getVerbose() const { return verbose_; }
92  static void getValue_s(char *buf, int len, const char *name, int count, double total, bool running, bool details, bool msonly);
93  const char* getValue(bool details = false, bool msonly = false);
94 
95  // OPERATIONS
96  void setVerbose(int v) { verbose_ = v; }
97  void start() { if (enabled_) doStart(); }
98  void stop() { if (enabled_) doStop(); }
99  void reset() { if (enabled_) doReset(); }
100 };
101 
111 class OPENZGY_TEST_API PrintingTimer : public Timer
112 {
113  int level_;
114 public:
115  explicit PrintingTimer(const char *name, int level = 1, bool startrunning = true);
116  ~PrintingTimer();
117  void print();
118 };
119 
133 class OPENZGY_TEST_API RawPrintingTimer : public Timer
134 {
135  int level_;
136 public:
137  explicit RawPrintingTimer(const char *name, int level = 1);
138  ~RawPrintingTimer();
139  void print();
140 };
141 
146 class OPENZGY_TEST_API SummaryTimer
147 {
148  class Impl;
149  Impl *pimpl_;
150 public:
151  explicit SummaryTimer(const char* name);
152  virtual ~SummaryTimer();
153  SummaryTimer(const SummaryTimer&) = delete;
154  SummaryTimer& operator=(const SummaryTimer&) = delete;
155 
156  // ACCESSORS
157  double getFrequency() const;
158  int getCount() const;
159  double getTotal() const;
160  double getLast() const;
161  const char* getName() const;
162  const char* getValue(bool details, bool msonly) const;
163 
164  // OPERATIONS
165  void reset();
166  void add(int count, double total, double last);
167  void add(const Timer& t);
168 };
169 
174 class OPENZGY_TEST_API SummaryPrintingTimer : public SummaryTimer
175 {
176 public:
177  explicit SummaryPrintingTimer(const char *name);
178  virtual ~SummaryPrintingTimer();
179  virtual void print();
180 };
181 
209 class OPENZGY_TEST_API SimpleTimer : public Timer
210 {
211  SummaryTimer& owner_;
212 public:
213  explicit SimpleTimer(SummaryTimer& owner, bool enabled = true)
214  : Timer(enabled)
215  , owner_(owner)
216  {
217  }
218  ~SimpleTimer()
219  {
220  if (getEnabled()) {
221  stop();
222  owner_.add(*this);
223  }
224  }
225 };
226 
227 } // end namespace
InternalZGY::SummaryTimer
Hold the timing results from zero or more Timer instances.
Definition: timer.h:146
InternalZGY::SimpleTimer
Timer that knows where to store the result.
Definition: timer.h:209
InternalZGY::Timer
Definition: timer.h:52
InternalZGY::SummaryPrintingTimer
SummaryTimer that prints its result when going out of scope.
Definition: timer.h:174
InternalZGY::RawPrintingTimer
Timer that prints its result when going out of scope.
Definition: timer.h:133
InternalZGY::SummaryTimer::Impl
Definition: timer.cpp:365
InternalZGY::Timer::Timer
Timer(bool enable=true, const char *name=0, int skip=0, bool startrunning=true)
Definition: timer.cpp:113
InternalZGY
Implementation not visible to clients.
InternalZGY::PrintingTimer
Timer that prints its result when going out of scope.
Definition: timer.h:111