15 #define GAUDIKERNEL_PROCSTAT_CPP 18 static const long TICK_TO_100NSEC = 100000;
19 #endif // not __APPLE__ 51 # define strcasecmp _stricmp 52 # define strncasecmp _strnicmp 57 #define getpid _getpid 63 typedef long (WINAPI *__NtQueryInformationProcess)(
66 long ProcessInformationClass,
67 void* ProcessInformation,
68 unsigned long ProcessInformationLength,
69 unsigned long* ReturnLength
71 __NtQueryInformationProcess NtQueryInformationProcess;
73 #else // UNIX...: first the EGCS stuff, then the OS dependent includes 84 #include "sys/times.h" 85 #include <sys/types.h> 86 #include <sys/signal.h> 87 #include <sys/syscall.h> 89 #include <sys/procfs.h> 92 #include <sys/resource.h> 303 static long pg_size = sysconf(_SC_PAGESIZE);
312 ost <<
"/proc/" << pid <<
"/stat";
314 if((fd=open(fname.
c_str(),O_RDONLY))<0) {
319 lseek(fd,0,SEEK_SET);
320 if((cnt=
read(fd,buf,
sizeof(buf)))<0) {
331 "%d %s %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 %lu %lu %lu %lu %lu %lu %lu",
377 static inline long processID(
long pid) {
378 return (pid>0) ? pid : (::getpid());
380 #endif // not __APPLE__ 389 if ( pid != ::getpid() ) {
391 m_handle = ::OpenProcess(PROCESS_QUERY_INFORMATION|PROCESS_VM_READ,
FALSE,
395 m_handle =
reinterpret_cast<void*
>(
static_cast<long>(::getpid()));
397 m_needRelease =
true;
402 m_needRelease =
false;
406 if ( m_needRelease ) {
408 ::CloseHandle(m_handle);
418 static bool first =
true;
421 void* mh = ::LoadLibrary(
"NTDll.dll");
423 NtApi::NtQueryInformationProcess = (NtApi::__NtQueryInformationProcess)
424 ::GetProcAddress((HINSTANCE)mh,
"NtQueryInformationProcess");
436 if (info==0)
return 0;
440 #if defined(_WIN32) && WINVER>=0x0400 // Windows NT 442 status = NtApi::NtQueryInformationProcess(h.
handle(),
447 status = (status==0) ? 1 : 0;
448 #elif defined(_WIN32) // Windows 95,98... 449 #elif defined(__linux) 453 getrusage(RUSAGE_SELF, &usage);
454 info->ReadOperationCount = usage.ru_inblock;
455 info->WriteOperationCount = usage.ru_oublock;
456 info->OtherOperationCount = 0;
457 info->ReadTransferCount = usage.ru_inblock;
458 info->WriteTransferCount = usage.ru_oublock;
459 info->OtherTransferCount = 0;
469 if (info==0)
return 0;
472 if ( fetch ==
Quota ) {
473 #if defined(_WIN32) && WINVER>=0x0400 // Windows NT 475 status = NtApi::NtQueryInformationProcess(h.
handle(),
480 status = (status==0) ? 1 : 0;
481 #elif defined(_WIN32) // Windows 95,98... 482 #elif defined(__linux) // Linux 487 getrlimit(RLIMIT_DATA, &lim);
488 if ( lim.rlim_max == RLIM_INFINITY ) lim.rlim_max = 0xFFFFFFFF;
489 info->PeakPagedPoolUsage = lim.rlim_cur;
490 info->PagedPoolUsage = lim.rlim_cur;
491 info->PagedPoolLimit = lim.rlim_max;
493 getrlimit(RLIMIT_STACK, &lim);
494 if ( lim.rlim_max == RLIM_INFINITY ) lim.rlim_max = 0xFFFFFFFF;
495 info->PeakNonPagedPoolUsage = lim.rlim_cur;
496 info->NonPagedPoolUsage = lim.rlim_cur;
497 info->NonPagedPoolLimit = lim.rlim_max;
501 info->PeakPagefileUsage = prc.
rss * pg_size;
502 info->PagefileUsage = prc.
rss * pg_size;
503 info->PagefileLimit = 0xFFFFFFFF;
504 #elif defined(__APPLE__) 514 if (info==0)
return 0;
519 #if defined(_WIN32) && WINVER>=0x0400 // Windows NT 521 status = NtApi::NtQueryInformationProcess(h.
handle(),
526 #elif defined(_WIN32) // Windows 95,98... 529 if (pid>0) status = 0;
533 status = (status==0) ? 1 : 0;
546 if (info==0)
return 0;
550 #if defined(_WIN32) && WINVER>=0x0400 // Windows NT 552 status = NtApi::NtQueryInformationProcess(h.
handle(),
557 status = (status==0) ? 1 : 0;
558 #elif defined(_WIN32) // Windows 95,98... 559 #elif defined(__linux) // Linux 560 const ssize_t bufsize = 1024;
562 pid = processID(pid);
563 sprintf(buf,
"/proc/%ld/statm", pid);
564 long size, resident, share, trs, lrs, drs, dt;
565 int fd = open(buf,O_RDONLY);
566 ssize_t nread =
read(fd, buf, bufsize);
568 if ( nread < bufsize && nread >= 0 )
570 fd = sscanf(buf,
"%ld %ld %ld %ld %ld %ld %ld",
571 &size, &resident, &share, &trs, &drs, &lrs, &dt);
574 info->PeakVirtualSize = prc.
vsize;
575 info->VirtualSize = prc.
vsize;
576 info->PeakWorkingSetSize = resident * pg_size;
577 info->WorkingSetSize = resident * pg_size;
578 info->QuotaPeakPagedPoolUsage = share * pg_size;
579 info->QuotaPagedPoolUsage = share * pg_size;
580 info->QuotaNonPagedPoolUsage = (trs+drs)* pg_size;
581 info->QuotaPeakNonPagedPoolUsage = (trs+drs)* pg_size;
583 info->PagefileUsage = prc.
vsize-resident*pg_size;
584 info->PeakPagefileUsage = prc.
vsize-resident*pg_size;
585 #elif defined(__APPLE__) 595 if (info==0)
return 0;
598 if ( fetch ==
Quota ) {
599 #if defined(_WIN32) && WINVER>=0x0400 // Windows NT 601 status = NtApi::NtQueryInformationProcess(h.
handle(),
606 status = (status==0) ? 1 : 0;
607 #elif defined(_WIN32) // Windows 95,98... 608 #elif defined(__linux) // Linux 612 if (pid>0 && pid!=::getpid())
return 0;
615 getrlimit(RLIMIT_DATA, &lim);
616 if ( lim.rlim_max == RLIM_INFINITY ) lim.rlim_max = 0xFFFFFFFF;
617 info->PagedPoolLimit = lim.rlim_max;
619 getrlimit(RLIMIT_STACK, &lim);
620 if ( lim.rlim_max == RLIM_INFINITY ) lim.rlim_max = 0xFFFFFFFF;
621 info->NonPagedPoolLimit = lim.rlim_max;
622 info->MinimumWorkingSetSize = 0;
624 getrlimit(RLIMIT_RSS, &lim);
625 if ( lim.rlim_max == RLIM_INFINITY ) lim.rlim_max = 0xFFFFFFFF;
626 info->MaximumWorkingSetSize = lim.rlim_max;
628 getrlimit(RLIMIT_AS, &lim);
629 if ( lim.rlim_max == RLIM_INFINITY ) lim.rlim_max = 0xFFFFFFFF;
630 info->PagefileLimit = lim.rlim_max;
632 getrlimit(RLIMIT_CPU, &lim);
633 if ( lim.rlim_max == RLIM_INFINITY ) lim.rlim_max = 0xFFFFFFFF;
634 info->TimeLimit = lim.rlim_max;
635 #elif defined(__APPLE__) 645 if (info==0)
return 0;
649 #if defined(_WIN32) && WINVER>=0x0400 // Windows NT 651 status = NtApi::NtQueryInformationProcess(h.
handle(),
656 status = (status==0) ? 1 : 0;
657 #elif defined(_WIN32) // Windows 95,98... 658 #elif defined(__linux) // Linux 660 pid = processID(pid);
662 info->ExitStatus = 0;
664 info->BasePriority = 2*15-prc.
priority;
667 info->AffinityMask = prc.
flags;
670 info->UniqueProcessId = pid;
671 info->InheritedFromUniqueProcessId = prc.
ppid;
681 if (info==0)
return 0;
684 if ( fetch ==
Times ) {
685 #if defined(_WIN32) && WINVER>=0x0400 // Windows NT 687 status = NtApi::NtQueryInformationProcess(h.
handle(),
692 status = (status==0) ? 1 : 0;
693 #elif defined(_WIN32) // Windows 95,98... 694 #elif defined(__linux) // Linux 696 bool myself = pid<=0 || pid==::getpid();
697 if ( myself && prc_start == 0 ) {
703 prc_start = (prc.
starttime+offset) * TICK_TO_100NSEC;
709 info->UserTime = tmsb.tms_utime * TICK_TO_100NSEC;
710 info->KernelTime = tmsb.tms_stime * TICK_TO_100NSEC;
711 info->CreateTime = prc_start;
721 info->UserTime = t.tms_utime * TICK_TO_100NSEC;
722 info->KernelTime = t.tms_stime * TICK_TO_100NSEC;
723 info->CreateTime = (prc.
starttime+offset) * TICK_TO_100NSEC;
729 #elif defined(__APPLE__) 732 #else // no /proc file system: assume sys_start for the first call 734 static clock_t sys_start = times(0);
736 clock_t now = times(&tmsb);
737 info->CreateTime = offset + now;
738 info->UserTime = tmsb.tms_utime;
739 info->KernelTime = tmsb.tms_stime;
740 info->CreateTime *= TICK_TO_100NSEC;
741 info->UserTime *= TICK_TO_100NSEC;
742 info->KernelTime *= TICK_TO_100NSEC;
Process/Thread System and User Time NtQueryInformationProcess using ProcessTimes NtQueryInformationTh...
Note: OS specific details for environment resolution.
Process I/O Counters NtQueryInformationProcess using ProcessIoCounters.
void usage(std::string argv0)
def read(f, regex='.*', skipevents=0)
GAUDI_API ProcessHandle processHandle()
Handle to running process.
void readProcStat(long pid, linux_proc &pinfo)
unsigned long long starttime
Process Quotas NtQueryInformationProcess using ProcessQuotaLimits NtQueryInformationProcess using Pro...
long query(long pid, InfoType info, PROCESS_BASIC_INFORMATION *buffer)
struct _PEB * PPEB
Basic Process Information NtQueryInformationProcess using ProcessBasicInfo.
Process Virtual Memory Counters NtQueryInformationProcess using ProcessVmCounters.
Process Pooled Quota Usage and Limits NtQueryInformationProcess using ProcessPooledUsageAndLimits.
virtual ~ProcessDescriptor()
InfoType
Enumeration for fetching information.
void * ProcessHandle
Definition of the process handle.