scclib
Stable Cloud Computing C++ Library
rwtimer.h
Go to the documentation of this file.
1 /*
2 BSD 3-Clause License
3 
4 Copyright (c) 2022, Stable Cloud Computing, Inc.
5 
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions are met:
8 
9 1. Redistributions of source code must retain the above copyright notice, this
10  list of conditions and the following disclaimer.
11 
12 2. Redistributions in binary form must reproduce the above copyright notice,
13  this list of conditions and the following disclaimer in the documentation
14  and/or other materials provided with the distribution.
15 
16 3. Neither the name of the copyright holder nor the names of its
17  contributors may be used to endorse or promote products derived from
18  this software without specific prior written permission.
19 
20 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
24 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31 #ifndef _SCC_UTIL_RWTIMER_H
32 #define _SCC_UTIL_RWTIMER_H
33 
34 #include <atomic>
35 #include <chrono>
36 #include <memory>
37 #include <util/iobase.h>
38 
39 namespace scc::util
40 {
41 
60 class ReadTimer : public PipelineReader
61 {
62  Reader* m_reader;
63  std::shared_ptr<Reader> m_shared;
64  std::atomic_int_least64_t m_ticks;
65  std::atomic_uint_least64_t m_calls;
66 
67 public:
69  ReadTimer();
71  ReadTimer(Reader&);
73  ReadTimer(const std::shared_ptr<Reader>&);
74 
75  virtual ~ReadTimer() {}
76 
78  void read_reset(Reader&);
80  virtual void read_reset(const std::shared_ptr<Reader>&);
81  virtual std::shared_ptr<Reader> read_shared() const { return m_shared; }
82 
84  virtual size_t read(void*, size_t);
85 
86  std::chrono::nanoseconds read_dur() const { return std::chrono::nanoseconds(m_ticks); }
87  void read_dur(std::chrono::nanoseconds v) { m_ticks = v.count(); }
88  void read_dur_reset() { m_ticks = 0; }
89 
90  uint64_t read_calls() const { return m_calls; }
91  void read_calls_reset() { m_calls = 0; }
92 };
93 
99 class WriteTimer : public PipelineWriter
100 {
101  Writer* m_writer;
102  std::shared_ptr<Writer> m_shared;
103  std::atomic_int_least64_t m_ticks;
104  std::atomic_uint_least64_t m_calls;
105 
106 public:
108  WriteTimer();
110  WriteTimer(Writer&);
112  WriteTimer(const std::shared_ptr<Writer>&);
113 
114  virtual ~WriteTimer() {}
115 
117  void write_reset(Writer&);
119  virtual void write_reset(const std::shared_ptr<Writer>&);
120  virtual std::shared_ptr<Writer> write_shared() const { return m_shared; }
121 
123  virtual size_t write(const void*, size_t);
124 
125  std::chrono::nanoseconds write_dur() const { return std::chrono::nanoseconds(m_ticks); }
126  void write_dur(std::chrono::nanoseconds v) { m_ticks = v.count(); }
127  void write_dur_reset() { m_ticks = 0; }
128 
129  uint64_t write_calls() const { return m_calls; }
130  void write_calls_reset() { m_calls = 0; }
131 };
132 
138 class RwTimer : public ReadTimer, public WriteTimer
139 {
140 public:
142  RwTimer();
144  RwTimer(Reader&, Writer&);
146  RwTimer(const std::shared_ptr<Reader>&, const std::shared_ptr<Writer>&);
147 
148  virtual ~RwTimer() {}
149 };
150 
153 }
154 
155 #endif
Adds timer to a read stream.
Definition: rwtimer.h:61
ReadTimer()
Reads return 0 until reset.
Definition: iostream.cc:700
virtual size_t read(void *, size_t)
Read and update time over underlying read.
Definition: iostream.cc:714
void read_reset(Reader &)
Reset the chained reader.
Definition: iostream.cc:726
Adds byte count to a read/write stream.
Definition: rwtimer.h:139
RwTimer()
Reads and writes return 0 until reset.
Definition: iostream.cc:791
Adds timer to a write stream.
Definition: rwtimer.h:100
void write_reset(Writer &)
Reset the chained writer.
Definition: iostream.cc:772
virtual size_t write(const void *, size_t)
Write and update time over underlying write.
Definition: iostream.cc:760
WriteTimer()
Writes return 0 until reset.
Definition: iostream.cc:746
Input/output stream base reader/writer interface classes.
Pipeline reader to carry out processing in a pipeline (chain of readers).
Definition: iobase.h:76
Pipeline writer to carry out processing in a pipeline (chain of writers).
Definition: iobase.h:95
Interface class for objects which can be read.
Definition: iobase.h:67
Interface class for objects which can be written.
Definition: iobase.h:86