12 static const long TICK_TO_100NSEC = 100000;
13 #endif // not __APPLE__
52 #include <sys/signal.h>
53 #include <sys/syscall.h>
54 #include <sys/times.h>
55 #include <sys/types.h>
58 # include <sys/procfs.h>
61 #include <sys/resource.h>
272 static long pg_size = sysconf( _SC_PAGESIZE );
280 std::ostringstream ost;
282 ost <<
"/proc/" << pid <<
"/stat";
283 std::string fname = ost.str();
284 if ( (
fd = open( fname.c_str(), O_RDONLY ) ) < 0 ) {
285 std::cerr <<
"Failed to open " << ost.str() << std::endl;
289 lseek(
fd, 0, SEEK_SET );
290 if ( ( cnt =
read(
fd, buf,
sizeof( buf ) - 1 ) ) < 0 ) {
291 std::cout <<
"LINUX Read of Proc file failed:" << std::endl;
303 "%d %400s %c %d %d %d %d %d %lu %lu %lu %lu %lu %lu %lu %ld %ld %ld %ld %ld %ld %llu %lu %ld %lu %lu %lu %lu "
304 "%lu %lu %lu %lu %lu %lu %lu",
319 static inline long processID(
long pid ) {
return ( pid > 0 ) ? pid : ( ::getpid() ); }
320 #endif // not __APPLE__
328 if ( pid != ::getpid() ) {
330 m_handle =
reinterpret_cast<void*
>(
static_cast<long>( ::getpid() ) );
340 if ( m_needRelease ) { m_handle =
nullptr; }
344 if ( info == 0 )
return 0;
348 #if defined( __linux )
352 getrusage( RUSAGE_SELF, &
usage );
367 if ( info == 0 )
return 0;
371 #if defined( __linux ) // Linux
376 getrlimit( RLIMIT_DATA, &lim );
377 if ( lim.rlim_max == RLIM_INFINITY ) lim.rlim_max = 0xFFFFFFFF;
382 getrlimit( RLIMIT_STACK, &lim );
383 if ( lim.rlim_max == RLIM_INFINITY ) lim.rlim_max = 0xFFFFFFFF;
393 #elif defined( __APPLE__ )
405 if ( info == 0 )
return 0;
413 status = ( status == 0 ) ? 1 : 0;
424 if ( info == 0 )
return 0;
428 #if defined( __linux ) // Linux
429 const ssize_t bufsize = 1024;
431 pid = processID( pid );
432 sprintf( buf,
"/proc/%ld/statm", pid );
433 long size, resident, share, trs, lrs, drs, dt;
434 int fd = open( buf, O_RDONLY );
435 ssize_t nread =
read(
fd, buf, bufsize );
437 if ( nread < bufsize && nread >= 0 ) buf[nread] =
'\0';
438 fd = sscanf( buf,
"%ld %ld %ld %ld %ld %ld %ld", &
size, &resident, &share, &trs, &drs, &lrs, &dt );
452 #elif defined( __APPLE__ )
462 if ( info == 0 )
return 0;
466 #if defined( __linux ) // Linux
470 if ( pid > 0 && pid != ::getpid() )
return 0;
473 getrlimit( RLIMIT_DATA, &lim );
474 if ( lim.rlim_max == RLIM_INFINITY ) lim.rlim_max = 0xFFFFFFFF;
477 getrlimit( RLIMIT_STACK, &lim );
478 if ( lim.rlim_max == RLIM_INFINITY ) lim.rlim_max = 0xFFFFFFFF;
482 getrlimit( RLIMIT_RSS, &lim );
483 if ( lim.rlim_max == RLIM_INFINITY ) lim.rlim_max = 0xFFFFFFFF;
486 getrlimit( RLIMIT_AS, &lim );
487 if ( lim.rlim_max == RLIM_INFINITY ) lim.rlim_max = 0xFFFFFFFF;
490 getrlimit( RLIMIT_CPU, &lim );
491 if ( lim.rlim_max == RLIM_INFINITY ) lim.rlim_max = 0xFFFFFFFF;
493 #elif defined( __APPLE__ )
503 if ( info == 0 )
return 0;
507 #if defined( __linux ) // Linux
509 pid = processID( pid );
529 if ( info == 0 )
return 0;
533 #if defined( __linux ) // Linux
534 static long long prc_start = 0;
535 bool myself = pid <= 0 || pid == ::getpid();
536 if ( myself && prc_start == 0 ) {
541 static long long offset =
542 100 *
static_cast<long long>(
time(
nullptr ) ) -
static_cast<long long>( times( &tmsb ) );
543 prc_start = ( prc.
starttime + offset ) * TICK_TO_100NSEC;
549 info->
UserTime = tmsb.tms_utime * TICK_TO_100NSEC;
550 info->
KernelTime = tmsb.tms_stime * TICK_TO_100NSEC;
556 static long long offset =
557 100 *
static_cast<long long>(
time(
nullptr ) ) -
static_cast<long long>( times( &tmsb ) );
561 info->
UserTime =
t.tms_utime * TICK_TO_100NSEC;
569 #elif defined( __APPLE__ )
573 #else // no /proc file system: assume sys_start for the first call
575 static clock_t sys_start = times( 0 );
576 static long long offset = 100 *
long long(
time( 0 ) ) - sys_start;
577 clock_t now = times( &tmsb );