Revision: 32978
http://sourceforge.net/p/opalvoip/code/32978
Author: rjongbloed
Date: 2014-10-10 11:40:42 +0000 (Fri, 10 Oct 2014)
Log Message:
-----------
Finished Linux version of Process and System CPU times.
Modified Paths:
--------------
ptlib/trunk/src/ptlib/unix/tlibthrd.cxx
Modified: ptlib/trunk/src/ptlib/unix/tlibthrd.cxx
===================================================================
--- ptlib/trunk/src/ptlib/unix/tlibthrd.cxx 2014-10-10 11:17:47 UTC (rev 32977)
+++ ptlib/trunk/src/ptlib/unix/tlibthrd.cxx 2014-10-10 11:40:42 UTC (rev 32978)
@@ -939,16 +939,8 @@
}
-bool PThread::GetTimes(Times & times)
+static bool InternalGetTimes(const char * filename, PThread::Times & times)
{
- // Do not use any PTLib functions in here as they could to a PTRACE, and this deadlock
- times.m_name = m_threadName;
- times.m_threadId = m_threadId;
- times.m_uniqueId = PX_linuxId;
-
- std::stringstream statFileName;
- statFileName << "/proc/" << getpid() << "/task/" << PX_linuxId << "/stat";
-
/* From the man page on the "stat" file
Status information about the process. This is used by ps(1). It is defined in /usr/src/linux/fs/proc/array.c.
The fields, in order, with their proper scanf(3) format specifiers, are:
@@ -1023,7 +1015,7 @@
*/
for (int retry = 0; retry < 3; ++retry) {
- std::ifstream statfile(statFileName.str().c_str());
+ std::ifstream statfile(filename);
char line[1000];
statfile.getline(line, sizeof(line));
@@ -1065,7 +1057,6 @@
times.m_kernel = jiffies_to_msecs(stime);
times.m_user = jiffies_to_msecs(utime);
- times.m_real = (PX_endTick != 0 ? PX_endTick : PTimer::Tick()) - PX_startTick;
return true;
}
@@ -1073,15 +1064,23 @@
}
+bool PThread::GetTimes(Times & times)
+{
+ // Do not use any PTLib functions in here as they could to a PTRACE, and this deadlock
+ times.m_name = m_threadName;
+ times.m_threadId = m_threadId;
+ times.m_uniqueId = PX_linuxId;
+ times.m_real = (PX_endTick != 0 ? PX_endTick : PTimer::Tick()) - PX_startTick;
+
+ return InternalGetTimes(PSTRSTRM("/proc/" << getpid() << "/task/" << PX_linuxId << "/stat"), times);
+}
+
+
bool PProcess::GetProcessTimes(Times & times) const
{
times.m_name = GetName();
-
- std::ifstream pf("/proc/self/stat");
- if (!pf.is_open())
- return false;
-
- return true;
+ times.m_real = PTime() - GetStartTime();
+ return InternalGetTimes("/proc/self/stat", times);
}
@@ -1089,16 +1088,20 @@
{
times.m_name = "SYSTEM";
- std::ifstream statfile("/proc/stat");
+ for (int retry = 0; retry < 3; ++retry) {
+ std::ifstream statfile("/proc/stat");
- char dummy[10];
- unsigned user, nice, system, idle, iowait, irq, softirq, steal, guest, guest_nice;
- statfile >> dummy >> user >> nice >> system >> idle >> iowait >> irq >> softirq >> steal >> guest >> guest_nice;
- if (!statfile.good())
- return false;
-
- times.m_kernel = system
- return true;
+ char dummy[10];
+ unsigned user, nice, system, idle, iowait, irq, softirq, steal, guest, guest_nice;
+ statfile >> dummy >> user >> nice >> system >> idle >> iowait >> irq >> softirq >> steal >> guest >> guest_nice;
+ if (statfile.good()) {
+ times.m_kernel = jiffies_to_msecs(system);
+ times.m_user = jiffies_to_msecs(user);
+ times.m_real = times.m_kernel + times.m_user + jiffies_to_msecs(idle);
+ return true;
+ }
+ }
+ return false;
}
#else
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|