How to check on process time?

phys137 at mailcity.com phys137 at mailcity.com
Wed Jan 10 19:49:35 EST 2001


long time ago i used sun's call 'getresourceusage' to obtain process times
on bsd - some other unixes had similar calls or not

on nt there are nice GetProcessTime and GetThreadTime calls

maybe a module like that could be added to Python's library?
I'm not volunteering to write it.
In case somebody wants here's some considerations:

most systems return process/thread cpu time with purported high resolution
(like 100nanoseconds) but the real accuracy is 1-10ms, sometimes even worse.
So no point in returning all these detailed stuff - 1ms accuracy is enough

most systems return elapsed time with similar accuracy when using 'standard'
c library. Yet some have microsecond clocks which actually work. On NT
current call for that is QueryPerformanceCounter and
QueryPerformanceFrequency.

System call overheads are in order of several microseconds.

64bit integers are generally useful for that - pity Python doesn't have
them.

here's some very bad sample code from nt (it should use __int64 instead)

        HANDLE hP = OpenProcess(PROCESS_QUERY_INFORMATION, true,
GetCurrentProcessId());
        FILETIME creation, exit, kernel, user;
        if (!GetProcessTimes((HANDLE) hP, &creation, &exit, &kernel, &user))
        {
                return -1;
        }
        // we divide processor time (in .1ms units) by 256, to make it
        // fit into 32 bit int. then we return the sum of kernel and user
        // the units are then 25.5 microseconds and range is 109951 seconds
        // all of these because of the lack of 64 bit add / subtract in
NT...
        LARGE_INTEGER li;
        li.LowPart = kernel.dwLowDateTime;
        li.HighPart = kernel.dwHighDateTime;
        long kernelTime = Int64ShraMod32(li.QuadPart,8) & 0xFFFFFFFF;
        li.LowPart = user.dwLowDateTime;
        li.HighPart = user.dwHighDateTime;
        long userTime = Int64ShraMod32(li.QuadPart,8) & 0xFFFFFFFF;
        return kernelTime + userTime;



"Donn Cave" <donn at u.washington.edu> wrote in message
news:93iip0$jhu$1 at nntp6.u.washington.edu...
> Quoth noahspurrier at my-deja.com:
> | Which Python library do I want to get extensive process information?
> | I need to look at any process running -- not just the parent, current,
> | or child processes.
>
> As far as I know, that isn't supported on contemporary computer
> platforms, at least in any meaningfully portable way, and that
> would be why you don't see anything for it in Python.  The "ps"
> command doesn't use any kind of API, it just reads kernel memory
> where this stuff is stored.
>
> If it doesn't need to be portable, and your present platform
> supports it, I'd say use the "/proc" filesystem.  The following
> prints accumulated times, on FreeBSD 4.1.
>
> Donn Cave, donn at u.washington.edu
> ----------------------------------------
> import errno
> import posix
> import string
>
> def ps():
>         proc = posix.listdir('/proc')
>         for pid in proc:
>                 try:
>                         pidval = string.atoi(pid)
>                 except ValueError:
> #  Ignore /proc/curproc etc.
>                         continue
>                 try:
>                         fp = open('/proc/%s/status' % (pid,), 'r')
>                 except IOError, val:
>                         if val.errno == errno.ENOENT:
>                                 print pid, '... already gone'
>                                 continue
>                         else:
>                                 raise
>                 ln = fp.readline()
>                 fp.close()
>                 ln = string.split(ln)
>                 cmd = ln[0]
>                 uss, usu = map(string.atoi, string.split(ln[8], ','))
>                 sys, syu = map(string.atoi, string.split(ln[9], ','))
>                 print '%5s %-14s%6d.%06d %d.%09d' % (pid, cmd, uss, usu,
sys, syu)
>
> ps()





More information about the Python-list mailing list