dodo  0.0.1
A C++ library to create containerized Linux services
thread.hpp
Go to the documentation of this file.
1 /*
2  * This file is part of the dodo library (https://github.com/jmspit/dodo).
3  * Copyright (c) 2019 Jan-Marten Spit.
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, version 3.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 /**
19  * @file thread.hpp
20  * Defines the dodo::threads::Thread class.
21  */
22 
23 #ifndef threads_thread_hpp
24 #define threads_thread_hpp
25 
26 #include <mutex>
27 #include <thread>
28 #include <string>
29 
30 #include <sys/time.h>
31 #include <sys/resource.h>
32 
33 namespace dodo::threads {
34 
35  /**
36  * Abstract Thread class. Inherit and implement pure virtual run() to run threaded code.
37  */
38  class Thread {
39  public:
40 
41  /**
42  * Constructor
43  */
44  Thread();
45 
46  /**
47  * Destructor
48  */
49  virtual ~Thread();
50 
51  /**
52  * Start the thread.
53  */
54  void start();
55 
56  /**
57  * Wait for the thread to join the current thread / wait for the thread to finish.
58  */
59  void wait();
60 
61  /**
62  * Get the thread id.
63  * @return that id.
64  */
65  std::thread::id getId() const;
66 
67  /**
68  * Take a snapshot of the thread's resource usage.
69  */
70  void snapRUsage();
71 
72  /**
73  * Get the average user mode cpu (cpu seconds/second) since thread start()
74  * @return that utilization.
75  */
76  double getAvgUserCPU();
77 
78  /**
79  * Get the average system mode cpu (cpu seconds/second) since thread start()
80  * @return that utilization.
81  */
82  double getAvgSysCPU();
83 
84  /**
85  * Get the average minor fault rate since thread start()
86  * @return that rate.
87  */
88  double getAvgMinFltRate();
89 
90  /**
91  * Get the average major fault rate since thread start()
92  * @return that rate.
93  */
94  double getAvgMajFltRate();
95 
96  /**
97  * Get the average block in rate since thread start()
98  * @return that rate.
99  */
100  double getAvgBlkInRate();
101 
102  /**
103  * Get the average block out rate since thread start()
104  * @return that rate.
105  */
106  double getAvgBlkOutRate();
107 
108  /**
109  * Get the average voluntary context switch rate since thread start()
110  * @return that rate.
111  */
112  double getAvgVCtx();
113 
114  /**
115  * Get the average involuntary context switch rate since thread start()
116  * @return that rate.
117  */
118  double getAvgICtx();
119 
120  /**
121  * Get the maximum resident set size seen on the thread.
122  * @return that size.
123  */
124  long getMaxRSS();
125 
126  /**
127  * Get the user mode cpu (cpu seconds/second) since last sample.
128  * @return that utilization.
129  */
130  double getLastUserCPU();
131 
132  /**
133  * Get the system mode cpu (cpu seconds/second) since last sample.
134  * @return that utilization.
135  */
136  double getLastSysCPU();
137 
138  /**
139  * Get the minor fault rate since last sample.
140  * @return that rate.
141  */
142  double getLastMinFltRate();
143 
144  /**
145  * Get the major fault rate since last sample.
146  * @return that rate.
147  */
148  double getLastMajFltRate();
149 
150  /**
151  * Get last block in rate since last sample.
152  * @return that rate.
153  */
154  double getLastBlkInRate();
155 
156  /**
157  * Get last block out rate since last sample.
158  * @return that rate.
159  */
160  double getLastBlkOutRate();
161 
162  /**
163  * Get voluntary context switch rate since last sample.
164  * @return that rate.
165  */
166  double getLastVCtx();
167 
168  /**
169  * Get involuntary context switch rate since last sample.
170  * @return that rate.
171  */
172  double getLastICtx();
173 
174  /**
175  * Time, in seconds, since thread start.
176  * @return that time.
177  */
178  double getRunTime();
179 
180  /**
181  * Time, in seconds, since last statistic update
182  * @return that time.
183  */
184  double getSnapDiffTime();
185 
186  /**
187  * Return the tid.
188  * @return the TID.
189  */
190  pid_t getTID() const { return tid_; };
191 
192  protected:
193 
194  /**
195  * Decsendants must override the run function.
196  */
197  virtual void run() = 0;
198 
199  /**
200  * The std::tread object.
201  */
202  std::thread* thread_;
203 
204  /**
205  * Time Thread started.
206  */
207  struct timeval start_time_;
208 
209  /**
210  * Time of previous statistics snapshot
211  */
212  struct timeval prev_snap_time_;
213 
214  /**
215  * Time of last statistics snapshot
216  */
217  struct timeval snap_time_;
218 
219  /**
220  * Previous statistics
221  */
222  struct rusage prev_rusage_;
223 
224  /**
225  * Last statistics
226  */
227  struct rusage rusage_;
228 
229  private:
230 
231  /**
232  * Posix thread method - calls dodo::threads::Thread:::run().
233  * @param context Is used to pass a Thread* so that its run() method can be called.
234  * @return nullptr (always)
235  */
236  static void* thread_method( void* context );
237 
238  /**
239  * The linux tid.
240  */
241  pid_t tid_;
242  };
243 
244 }
245 
246 #endif
dodo::threads::Thread::run
virtual void run()=0
Decsendants must override the run function.
dodo::threads::Thread::getAvgVCtx
double getAvgVCtx()
Get the average voluntary context switch rate since thread start()
Definition: thread.cpp:104
dodo::threads::Thread::getAvgMinFltRate
double getAvgMinFltRate()
Get the average minor fault rate since thread start()
Definition: thread.cpp:88
dodo::threads::Thread::thread_
std::thread * thread_
The std::tread object.
Definition: thread.hpp:202
dodo::threads::Thread::wait
void wait()
Wait for the thread to join the current thread / wait for the thread to finish.
Definition: thread.cpp:64
dodo::threads::Thread::getRunTime
double getRunTime()
Time, in seconds, since thread start.
Definition: thread.cpp:156
dodo::threads::Thread::getTID
pid_t getTID() const
Return the tid.
Definition: thread.hpp:190
dodo::threads::Thread::getLastMinFltRate
double getLastMinFltRate()
Get the minor fault rate since last sample.
Definition: thread.cpp:126
dodo::threads::Thread::getLastMajFltRate
double getLastMajFltRate()
Get the major fault rate since last sample.
Definition: thread.cpp:131
dodo::threads::Thread::getLastBlkInRate
double getLastBlkInRate()
Get last block in rate since last sample.
Definition: thread.cpp:136
dodo::threads::Thread::getAvgUserCPU
double getAvgUserCPU()
Get the average user mode cpu (cpu seconds/second) since thread start()
Definition: thread.cpp:80
dodo::threads::Thread::getLastUserCPU
double getLastUserCPU()
Get the user mode cpu (cpu seconds/second) since last sample.
Definition: thread.cpp:116
dodo::threads::Thread::getLastBlkOutRate
double getLastBlkOutRate()
Get last block out rate since last sample.
Definition: thread.cpp:141
dodo::threads::Thread::start
void start()
Start the thread.
Definition: thread.cpp:58
dodo::threads::Thread::getLastICtx
double getLastICtx()
Get involuntary context switch rate since last sample.
Definition: thread.cpp:151
dodo::threads::Thread::rusage_
struct rusage rusage_
Last statistics.
Definition: thread.hpp:227
dodo::threads::Thread::prev_rusage_
struct rusage prev_rusage_
Previous statistics.
Definition: thread.hpp:222
dodo::threads::Thread::Thread
Thread()
Constructor.
Definition: thread.cpp:45
dodo::threads::Thread::getId
std::thread::id getId() const
Get the thread id.
Definition: thread.cpp:75
dodo::threads::Thread::getAvgICtx
double getAvgICtx()
Get the average involuntary context switch rate since thread start()
Definition: thread.cpp:108
dodo::threads::Thread::snapRUsage
void snapRUsage()
Take a snapshot of the thread's resource usage.
Definition: thread.cpp:68
dodo::threads::Thread::getAvgMajFltRate
double getAvgMajFltRate()
Get the average major fault rate since thread start()
Definition: thread.cpp:92
dodo::threads::Thread::snap_time_
struct timeval snap_time_
Time of last statistics snapshot.
Definition: thread.hpp:217
dodo::threads::Thread::prev_snap_time_
struct timeval prev_snap_time_
Time of previous statistics snapshot.
Definition: thread.hpp:212
dodo::threads::Thread::getMaxRSS
long getMaxRSS()
Get the maximum resident set size seen on the thread.
Definition: thread.cpp:112
dodo::threads::Thread::getAvgBlkInRate
double getAvgBlkInRate()
Get the average block in rate since thread start()
Definition: thread.cpp:96
dodo::threads::Thread
Abstract Thread class.
Definition: thread.hpp:38
dodo::threads::Thread::getSnapDiffTime
double getSnapDiffTime()
Time, in seconds, since last statistic update.
Definition: thread.cpp:160
dodo::threads::Thread::getLastSysCPU
double getLastSysCPU()
Get the system mode cpu (cpu seconds/second) since last sample.
Definition: thread.cpp:121
dodo::threads::Thread::getAvgSysCPU
double getAvgSysCPU()
Get the average system mode cpu (cpu seconds/second) since thread start()
Definition: thread.cpp:84
dodo::threads::Thread::~Thread
virtual ~Thread()
Destructor.
Definition: thread.cpp:54
dodo::threads
Interface for Thread programming.
Definition: mutex.hpp:29
dodo::threads::Thread::tid_
pid_t tid_
The linux tid.
Definition: thread.hpp:241
dodo::threads::Thread::getLastVCtx
double getLastVCtx()
Get voluntary context switch rate since last sample.
Definition: thread.cpp:146
dodo::threads::Thread::start_time_
struct timeval start_time_
Time Thread started.
Definition: thread.hpp:207
dodo::threads::Thread::thread_method
static void * thread_method(void *context)
Posix thread method - calls dodo::threads::Thread:::run().
Definition: thread.cpp:33
dodo::threads::Thread::getAvgBlkOutRate
double getAvgBlkOutRate()
Get the average block out rate since thread start()
Definition: thread.cpp:100