dodo  0.0.1
A C++ library to create containerized Linux services
thread.cpp
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.cpp
20  * Implements the dodo::threads::Thread class.
21  */
22 
23 #include <iostream>
24 #include <cstring>
25 #include <unistd.h>
26 #include <sys/syscall.h>
27 
28 #include "threads/thread.hpp"
29 #include "common/util.hpp"
30 
31 namespace dodo::threads {
32 
33  void* Thread::thread_method( void* context ) {
34  Thread* t = ((Thread*)context);
35  if ( t ) {
36  t->tid_ = static_cast<pid_t>(syscall(SYS_gettid));
37  gettimeofday( &t->start_time_, NULL );
38  t->snapRUsage();
39  t->run();
40  return static_cast<void*>(t);
41  }
42  return 0;
43  }
44 
45  Thread::Thread() : thread_(0), tid_(0) {
46  gettimeofday( &start_time_, NULL );
47  gettimeofday( &prev_snap_time_, NULL );
48  gettimeofday( &snap_time_, NULL );
49  memset( &prev_rusage_, 0, sizeof( prev_rusage_ ) );
50  memset( &rusage_, 0, sizeof( rusage_ ) );
51  };
52 
53 
55  if ( thread_ ) delete thread_;
56  }
57 
58  void Thread::start() {
59  if ( !thread_ ) {
60  thread_ = new std::thread( this->thread_method, this );
61  }
62  }
63 
64  void Thread::wait() {
65  if ( thread_ && thread_->joinable() ) thread_->join();
66  }
67 
71  getrusage( RUSAGE_THREAD, &rusage_ );
72  gettimeofday( &snap_time_, NULL );
73  }
74 
75  std::thread::id Thread::getId() const {
76  if ( thread_ ) return thread_->get_id();
77  return std::thread::id();
78  }
79 
81  return ((double)rusage_.ru_utime.tv_sec + (double)rusage_.ru_utime.tv_usec/1.0E6)/getRunTime();
82  }
83 
85  return ((double)rusage_.ru_stime.tv_sec + (double)rusage_.ru_stime.tv_usec/1.0E6)/getRunTime();
86  }
87 
89  return (double)rusage_.ru_minflt/getRunTime();
90  }
91 
93  return (double)rusage_.ru_majflt/getRunTime();
94  }
95 
97  return (double)rusage_.ru_inblock/getRunTime();
98  }
99 
101  return (double)rusage_.ru_oublock/getRunTime();
102  }
103 
105  return (double)rusage_.ru_nvcsw/getRunTime();
106  }
107 
109  return (double)rusage_.ru_nivcsw/getRunTime();
110  }
111 
113  return rusage_.ru_maxrss;
114  }
115 
117  return ( ( (double)rusage_.ru_utime.tv_sec + (double)rusage_.ru_utime.tv_usec/1.0E6 ) -
118  ( (double)prev_rusage_.ru_utime.tv_sec + (double)prev_rusage_.ru_utime.tv_usec/1.0E6 ) ) / getSnapDiffTime();
119  }
120 
122  return ( ( (double)rusage_.ru_stime.tv_sec + (double)rusage_.ru_stime.tv_usec/1.0E6 ) -
123  ( (double)prev_rusage_.ru_stime.tv_sec + (double)prev_rusage_.ru_stime.tv_usec/1.0E6 ) ) / getSnapDiffTime();
124  }
125 
127  return ( ( (double)rusage_.ru_minflt ) -
128  ( (double)prev_rusage_.ru_minflt ) ) / getSnapDiffTime();
129  }
130 
132  return ( ( (double)rusage_.ru_majflt ) -
133  ( (double)prev_rusage_.ru_majflt ) ) / getSnapDiffTime();
134  }
135 
137  return ( ( (double)rusage_.ru_inblock ) -
138  ( (double)prev_rusage_.ru_inblock ) ) / getSnapDiffTime();
139  }
140 
142  return ( ( (double)rusage_.ru_oublock ) -
143  ( (double)prev_rusage_.ru_oublock ) ) / getSnapDiffTime();
144  }
145 
147  return ( ( (double)rusage_.ru_nvcsw ) -
148  ( (double)prev_rusage_.ru_nvcsw ) ) / getSnapDiffTime();
149  }
150 
152  return ( ( (double)rusage_.ru_nivcsw ) -
153  ( (double)prev_rusage_.ru_nivcsw ) ) / getSnapDiffTime();
154  }
155 
158  }
159 
162  }
163 
164 }
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::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
thread.hpp
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::common::getSecondDiff
double getSecondDiff(struct timeval &t1, struct timeval &t2)
Return difference in seconds as a double.
Definition: util.hpp:126
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
util.hpp
dodo::threads::Thread::getAvgBlkOutRate
double getAvgBlkOutRate()
Get the average block out rate since thread start()
Definition: thread.cpp:100