bes Updated for version 3.21.1
The Backend Server (BES) is the lower two tiers of the Hyrax data server
BESStopWatch.h
1// BESStopWatch.h
2
3// This file is part of bes, A C++ back-end server implementation framework
4// for the OPeNDAP Data Access Protocol.
5
6// Copyright (c) 2004-2009 University Corporation for Atmospheric Research
7// Author: Patrick West <pwest@ucar.edu> and Jose Garcia <jgarcia@ucar.edu>
8//
9// This library is free software; you can redistribute it and/or
10// modify it under the terms of the GNU Lesser General Public
11// License as published by the Free Software Foundation; either
12// version 2.1 of the License, or (at your option) any later version.
13//
14// This library is distributed in the hope that it will be useful,
15// but WITHOUT ANY WARRANTY; without even the implied warranty of
16// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17// Lesser General Public License for more details.
18//
19// You should have received a copy of the GNU Lesser General Public
20// License along with this library; if not, write to the Free Software
21// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22//
23// You can contact University Corporation for Atmospheric Research at
24// 3080 Center Green Drive, Boulder, CO 80301
25
26// (c) COPYRIGHT University Corporation for Atmospheric Research 2004-2005
27// Please read the full copyright statement in the file COPYRIGHT_UCAR.
28//
29// Authors:
30// ndp Nathan Potter <ndp@opendap.org>
31// pwest Patrick West <pwest@ucar.edu>
32// jgarcia Jose Garcia <jgarcia@ucar.edu>
33
34#ifndef I_BESStopWatch_h
35#define I_BESStopWatch_h 1
36
37#include <sys/time.h>
38#include <sys/resource.h>
39
40#if HAVE_UNISTD_H
41#include <unistd.h>
42#endif
43
44#include <BESDataHandlerInterface.h>
45#include <BESDataNames.h>
46#include <BESLog.h>
47
48#include "BESObj.h"
49
50#define COMMAND_TIMING 1
51
52static const std::string TIMING_LOG_KEY = "timing";
53static const std::string MISSING_LOG_PARAM;
54
55// This macro is used to start a timer if any of the BESDebug flags it tests are set.
56// The advantage of this macro is that it drops to zero code when NDEBUG is defined.
57// Our code often uses a macro that leaves the BESStopWatch object definition in the
58// code when NDEBUG is defined. This is not a problem, but it does make the code a bit
59// slower. jhrg 5/17/24
60#ifndef NDEBUG
61
62#define BES_STOPWATCH_START(module, message) \
63BESStopWatch besTimer; \
64if (BESISDEBUG((module)) || BESISDEBUG(TIMING_LOG_KEY) || BESLog::TheLog()->is_verbose()) \
65 besTimer.start((message))
66
67#define BES_STOPWATCH_START_DHI(module, message, DHI) \
68BESStopWatch besTimer; \
69if (BESISDEBUG((module)) || BESISDEBUG(TIMING_LOG_KEY) || BESLog::TheLog()->is_verbose()) \
70besTimer.start((message), DHI)
71
72#else
73#define BES_STOPWATCH_START(module, msg)
74#define BES_STOPWATCH_START_DHI(module, msg, DHI)
75#endif
76
77// This macro is used specifically to time the execution of a command. It does not depend
78// on the code being built in developer mode. jhrg 11/24/24
79#ifdef COMMAND_TIMING
80
81#define BES_MODULE_TIMING(message) BESStopWatch commandTimer; \
82 commandTimer.start(string("Module timing: ") + (message))
83
84// Note the BES_COMMAND_TIMING macro assumes that the "message" string ends with white-space.
85#define BES_COMMAND_TIMING(message, DHI) BESStopWatch commandTimer; \
86 commandTimer.start(string("Command timing: ") + (message) + (DHI->data[LOG_INFO]), DHI)
87
88#else
89#define BES_MODULE_TIMING(message)
90#define BES_COMMAND_TIMING(message, DHI)
91#endif
92
93class BESStopWatch;
94
95namespace bes_timing {
96extern BESStopWatch *elapsedTimeToReadStart;
97extern BESStopWatch *elapsedTimeToTransmitStart;
98}
99
100class BESStopWatch : public BESObj {
101private:
102 std::string d_timer_name;
103 std::string d_req_id;
104 std::string d_log_name = TIMING_LOG_KEY;
105 bool d_started = false;
106
107 struct timeval d_start_usage{};
108 struct timeval d_stop_usage{};
109
110 unsigned long int get_elapsed_us() const;
111 unsigned long int get_start_us() const;
112 unsigned long int get_stop_us() const;
113 static bool get_time_of_day(struct timeval &time_val);
114
115 public:
116
118 BESStopWatch() = default;
119 BESStopWatch(const BESStopWatch &copy_from) = default;
120 BESStopWatch &operator=(const BESStopWatch &copy_from) = default;
121
127 explicit BESStopWatch(const std::string &logName) : d_log_name(logName) { }
128
136 ~BESStopWatch() override;
137
147 virtual bool start(const std::string &name, const std::string &reqID = {BESLog::TheLog()->get_request_id()});
148
149
158 virtual bool start(const std::string &name, BESDataHandlerInterface *dhi);
159
160 void dump(std::ostream &strm) const override;
161};
162
163#endif // I_BESStopWatch_h
164
top level BES object to house generic methods
Definition BESObj.h:54
BESStopWatch()=default
Makes a new BESStopWatch with a logName of TIMING_LOG_KEY.
void dump(std::ostream &strm) const override
dumps information about this object
virtual bool start(const std::string &name, const std::string &reqID={BESLog::TheLog() ->get_request_id()})
BESStopWatch(const std::string &logName)
~BESStopWatch() override