1
0
mirror of https://git.dev.opencascade.org/repos/occt.git synced 2025-06-30 12:14:08 +03:00

0023019: OSD_Chronometer fails to compile due to lack of clock_gettime() on Mac OS X

Usage of task_info() system function in OSD_Chronometer::GetThreadCPU() to retrieve process info.
This commit is contained in:
kgv 2012-03-13 10:18:47 +04:00
parent 5f8b738ea5
commit 270a5d4e0c

View File

@ -34,9 +34,9 @@
//======================================================================= //=======================================================================
//Selon les plateformes on doit avoir le nombre de clicks par secondes //Selon les plateformes on doit avoir le nombre de clicks par secondes
//qui est l unite de mesure du temps. //qui est l unite de mesure du temps.
//======================================================================= //=======================================================================
#ifndef sysconf #ifndef sysconf
# define _sysconf sysconf # define _sysconf sysconf
#endif #endif
@ -54,9 +54,14 @@
# include <limits.h> # include <limits.h>
#endif #endif
#if (defined(__APPLE__))
#include <mach/task.h>
#include <mach/mach.h>
#endif
//======================================================================= //=======================================================================
//function : GetProcessCPU //function : GetProcessCPU
//purpose : //purpose :
//======================================================================= //=======================================================================
void OSD_Chronometer::GetProcessCPU (Standard_Real& UserSeconds, Standard_Real& SystemSeconds) void OSD_Chronometer::GetProcessCPU (Standard_Real& UserSeconds, Standard_Real& SystemSeconds)
{ {
@ -65,7 +70,7 @@ void OSD_Chronometer::GetProcessCPU (Standard_Real& UserSeconds, Standard_Real&
#else #else
static const long aCLK_TCK = CLK_TCK; static const long aCLK_TCK = CLK_TCK;
#endif #endif
tms CurrentTMS; tms CurrentTMS;
times (&CurrentTMS); times (&CurrentTMS);
@ -75,30 +80,39 @@ void OSD_Chronometer::GetProcessCPU (Standard_Real& UserSeconds, Standard_Real&
//======================================================================= //=======================================================================
//function : GetThreadCPU //function : GetThreadCPU
//purpose : //purpose :
//======================================================================= //=======================================================================
void OSD_Chronometer::GetThreadCPU (Standard_Real& UserSeconds, Standard_Real& SystemSeconds) void OSD_Chronometer::GetThreadCPU (Standard_Real& theUserSeconds,
Standard_Real& theSystemSeconds)
{ {
UserSeconds = SystemSeconds = 0.; theUserSeconds = theSystemSeconds = 0.0;
#if (defined(__APPLE__))
#if defined(_POSIX_TIMERS) && defined(_POSIX_THREAD_CPUTIME) struct task_thread_times_info aTaskInfo;
mach_msg_type_number_t aTaskInfoCount = TASK_THREAD_TIMES_INFO_COUNT;
if (KERN_SUCCESS == task_info(mach_task_self(), TASK_THREAD_TIMES_INFO,
(task_info_t )&aTaskInfo, &aTaskInfoCount))
{
theUserSeconds = Standard_Real(aTaskInfo.user_time.seconds) + 0.000001 * aTaskInfo.user_time.microseconds;
theSystemSeconds = Standard_Real(aTaskInfo.system_time.seconds) + 0.000001 * aTaskInfo.system_time.microseconds;
}
#elif defined(_POSIX_TIMERS) && defined(_POSIX_THREAD_CPUTIME)
// on Linux, only user times are available for threads via clock_gettime() // on Linux, only user times are available for threads via clock_gettime()
struct timespec t; struct timespec t;
if ( ! clock_gettime(CLOCK_THREAD_CPUTIME_ID,&t) ) if (!clock_gettime (CLOCK_THREAD_CPUTIME_ID, &t))
{ {
UserSeconds = t.tv_sec + 0.000000001 * t.tv_nsec; theUserSeconds = t.tv_sec + 0.000000001 * t.tv_nsec;
} }
#elif defined(SOLARIS) #elif defined(SOLARIS)
// on Solaris, both user and system times are available as LWP times // on Solaris, both user and system times are available as LWP times
struct rusage rut; struct rusage rut;
if ( ! getrusage (RUSAGE_LWP, &rut) ) if (!getrusage (RUSAGE_LWP, &rut))
{ {
UserSeconds = rut.ru_utime.tv_sec + 0.000001 * rut.ru_utime.tv_usec; theUserSeconds = rut.ru_utime.tv_sec + 0.000001 * rut.ru_utime.tv_usec;
SystemSeconds = rut.ru_stime.tv_sec + 0.000001 * rut.ru_stime.tv_usec; theSystemSeconds = rut.ru_stime.tv_sec + 0.000001 * rut.ru_stime.tv_usec;
} }
#else #else
#pragma error "OS is not supported yet; code to be ported" #pragma error "OS is not supported yet; code to be ported"
#endif #endif
} }
#else #else
@ -113,7 +127,7 @@ void OSD_Chronometer::GetThreadCPU (Standard_Real& UserSeconds, Standard_Real& S
//purpose : Encode time defined by FILETIME structure //purpose : Encode time defined by FILETIME structure
// (100s nanoseconds since January 1, 1601) to 64-bit integer // (100s nanoseconds since January 1, 1601) to 64-bit integer
//======================================================================= //=======================================================================
static inline __int64 EncodeFILETIME (PFILETIME pFt) static inline __int64 EncodeFILETIME (PFILETIME pFt)
{ {
__int64 qw; __int64 qw;
@ -122,11 +136,11 @@ static inline __int64 EncodeFILETIME (PFILETIME pFt)
qw |= pFt -> dwLowDateTime; qw |= pFt -> dwLowDateTime;
return qw; return qw;
} }
//======================================================================= //=======================================================================
//function : GetProcessCPU //function : GetProcessCPU
//purpose : //purpose :
//======================================================================= //=======================================================================
void OSD_Chronometer::GetProcessCPU (Standard_Real& UserSeconds, Standard_Real& SystemSeconds) void OSD_Chronometer::GetProcessCPU (Standard_Real& UserSeconds, Standard_Real& SystemSeconds)
{ {
@ -138,7 +152,7 @@ void OSD_Chronometer::GetProcessCPU (Standard_Real& UserSeconds, Standard_Real&
//======================================================================= //=======================================================================
//function : GetThreadCPU //function : GetThreadCPU
//purpose : //purpose :
//======================================================================= //=======================================================================
void OSD_Chronometer::GetThreadCPU (Standard_Real& UserSeconds, Standard_Real& SystemSeconds) void OSD_Chronometer::GetThreadCPU (Standard_Real& UserSeconds, Standard_Real& SystemSeconds)
{ {
@ -154,27 +168,27 @@ void OSD_Chronometer::GetThreadCPU (Standard_Real& UserSeconds, Standard_Real& S
//======================================================================= //=======================================================================
//function : OSD_Chronometer //function : OSD_Chronometer
//purpose : //purpose :
//======================================================================= //=======================================================================
OSD_Chronometer::OSD_Chronometer(const Standard_Boolean ThisThreadOnly) OSD_Chronometer::OSD_Chronometer(const Standard_Boolean ThisThreadOnly)
{ {
ThreadOnly = ThisThreadOnly; ThreadOnly = ThisThreadOnly;
Start_user = Start_sys = 0.; Start_user = Start_sys = 0.;
Cumul_user = Cumul_sys = 0.; Cumul_user = Cumul_sys = 0.;
Stopped = Standard_True; Stopped = Standard_True;
} }
//======================================================================= //=======================================================================
//function : Destroy //function : Destroy
//purpose : //purpose :
//======================================================================= //=======================================================================
void OSD_Chronometer::Destroy () void OSD_Chronometer::Destroy ()
{ {
} }
//======================================================================= //=======================================================================
//function : Reset //function : Reset
//purpose : //purpose :
//======================================================================= //=======================================================================
void OSD_Chronometer::Reset () void OSD_Chronometer::Reset ()
{ {
@ -185,9 +199,9 @@ void OSD_Chronometer::Reset ()
//======================================================================= //=======================================================================
//function : Stop //function : Stop
//purpose : //purpose :
//======================================================================= //=======================================================================
void OSD_Chronometer::Stop () void OSD_Chronometer::Stop ()
{ {
if ( !Stopped ) { if ( !Stopped ) {
Standard_Real Curr_user, Curr_sys; Standard_Real Curr_user, Curr_sys;
@ -199,31 +213,31 @@ void OSD_Chronometer::Stop ()
Cumul_user += Curr_user - Start_user; Cumul_user += Curr_user - Start_user;
Cumul_sys += Curr_sys - Start_sys; Cumul_sys += Curr_sys - Start_sys;
Stopped = Standard_True; Stopped = Standard_True;
} }
// else cerr << "WARNING: OSD_Chronometer already stopped !\n" << flush; // else cerr << "WARNING: OSD_Chronometer already stopped !\n" << flush;
} }
//======================================================================= //=======================================================================
//function : Start //function : Start
//purpose : //purpose :
//======================================================================= //=======================================================================
void OSD_Chronometer::Start () void OSD_Chronometer::Start ()
{ {
if ( Stopped ) { if ( Stopped ) {
if ( ThreadOnly ) if ( ThreadOnly )
GetThreadCPU (Start_user, Start_sys); GetThreadCPU (Start_user, Start_sys);
else else
GetProcessCPU (Start_user, Start_sys); GetProcessCPU (Start_user, Start_sys);
Stopped = Standard_False; Stopped = Standard_False;
} }
// else cerr << "WARNING: OSD_Chronometer already running !\n" << flush; // else cerr << "WARNING: OSD_Chronometer already running !\n" << flush;
} }
//======================================================================= //=======================================================================
//function : Show //function : Show
//purpose : //purpose :
//======================================================================= //=======================================================================
void OSD_Chronometer::Show () void OSD_Chronometer::Show ()
{ {
@ -232,13 +246,13 @@ void OSD_Chronometer::Show ()
//======================================================================= //=======================================================================
//function : Show //function : Show
//purpose : //purpose :
//======================================================================= //=======================================================================
void OSD_Chronometer::Show (Standard_OStream& os) void OSD_Chronometer::Show (Standard_OStream& os)
{ {
Standard_Boolean StopSav = Stopped; Standard_Boolean StopSav = Stopped;
if (!StopSav) Stop(); if (!StopSav) Stop();
std::streamsize prec = os.precision (12); std::streamsize prec = os.precision (12);
os << "CPU user time: " << Cumul_user << " seconds " << endl; os << "CPU user time: " << Cumul_user << " seconds " << endl;
os << "CPU system time: " << Cumul_sys << " seconds " << endl; os << "CPU system time: " << Cumul_sys << " seconds " << endl;
os.precision (prec); os.precision (prec);
@ -253,7 +267,7 @@ void OSD_Chronometer::Show (Standard_Real& second)
{ {
Standard_Boolean StopSav = Stopped; Standard_Boolean StopSav = Stopped;
if (!StopSav) Stop(); if (!StopSav) Stop();
second = Cumul_user; second = Cumul_user;
if (!StopSav) Start(); if (!StopSav) Start();
} }
//======================================================================= //=======================================================================
@ -265,8 +279,8 @@ void OSD_Chronometer::Show (Standard_Real& user,
{ {
Standard_Boolean StopSav = Stopped; Standard_Boolean StopSav = Stopped;
if (!StopSav) Stop(); if (!StopSav) Stop();
user = Cumul_user; user = Cumul_user;
system = Cumul_sys; system = Cumul_sys;
if (!StopSav) Start(); if (!StopSav) Start();
} }