Prev: Process Communication
Next: TCP/IP stream socket
From: sinbad on 14 Apr 2010 12:10 hi, how does times() function calculate the processor time. what is the unit for processor time. thanks
From: Eric Sosman on 14 Apr 2010 13:03 On 4/14/2010 12:10 PM, sinbad wrote: > hi, > > how does times() function calculate the processor time. It retrieves information maintained by the kernel. The kernel knows when it gives a CPU to a process, and knows when it takes the CPU away, and thus knows how long the process ran on the CPU. When the process is on-CPU, the kernel also knows whether it is executing user-mode code or in-kernel system code. The rest is just a matter of adding up lots of on-off intervals. > what is the unit for processor time. The clock_t type, expressed in (1/CLOCKS_PER_SEC)-second units. Note that the unit of expression may be more or less precise (usually less) than the unit of original measurement. For example, the kernel might accumulate time in nanoseconds that are rounded to clock_t precision for reporting. -- Eric Sosman esosman(a)ieee-dot-org.invalid
From: sinbad on 14 Apr 2010 13:53 On Apr 14, 10:03 pm, Eric Sosman <esos...(a)ieee-dot-org.invalid> wrote: > On 4/14/2010 12:10 PM, sinbad wrote: > > > hi, > > > how does times() function calculate the processor time. > > It retrieves information maintained by the kernel. The > kernel knows when it gives a CPU to a process, and knows when > it takes the CPU away, and thus knows how long the process ran > on the CPU. When the process is on-CPU, the kernel also knows > whether it is executing user-mode code or in-kernel system code. > The rest is just a matter of adding up lots of on-off intervals. > > > what is the unit for processor time. > > The clock_t type, expressed in (1/CLOCKS_PER_SEC)-second > units. Note that the unit of expression may be more or less > precise (usually less) than the unit of original measurement. > For example, the kernel might accumulate time in nanoseconds > that are rounded to clock_t precision for reporting. > > -- > Eric Sosman > esos...(a)ieee-dot-org.invalid the return value of times() is a signed long int. if i run my process for longer time churning the cpu . Is it possible that times() will return a value that long int can't hold. clock_t p_time; p_time = times(NULL); i might be possible that times() would return a value that when assigned to p_time and used as signed long int would result in a negative value. what is the correct use of times() function? thanks
From: Eric Sosman on 14 Apr 2010 14:22 On 4/14/2010 1:53 PM, sinbad wrote: > On Apr 14, 10:03 pm, Eric Sosman<esos...(a)ieee-dot-org.invalid> wrote: >> On 4/14/2010 12:10 PM, sinbad wrote: >> >>> hi, >> >>> how does times() function calculate the processor time. >> >> It retrieves information maintained by the kernel. The >> kernel knows when it gives a CPU to a process, and knows when >> it takes the CPU away, and thus knows how long the process ran >> on the CPU. When the process is on-CPU, the kernel also knows >> whether it is executing user-mode code or in-kernel system code. >> The rest is just a matter of adding up lots of on-off intervals. >> >>> what is the unit for processor time. >> >> The clock_t type, expressed in (1/CLOCKS_PER_SEC)-second >> units. Note that the unit of expression may be more or less >> precise (usually less) than the unit of original measurement. >> For example, the kernel might accumulate time in nanoseconds >> that are rounded to clock_t precision for reporting. >> >> -- >> Eric Sosman >> esos...(a)ieee-dot-org.invalid (Please don't quote signatures.) > the return value of times() is a signed long int. No, the value returned by times() is a clock_t. On your system clock_t may be an alias for long, but on other systems clock_t may be an alias for some other type. > if i run my process > for longer time churning the cpu . Is it possible that > times() will return a value that long int can't hold. Yes. Or "no," actually, because it cannot possibly return a value outside the clock_t range. But if the "true" time is too large to express as a clock_t, times() will return some other value -- probably a value obtained by truncating the true value to clock_t range, losing high-order bits. > clock_t p_time; > p_time = times(NULL); > > i might be possible that times() would return a value > that when assigned to p_time and used as signed long int > would result in a negative value. Yes. > what is the correct use of times() function? clock_t t0, t1; t0 = times(NULL); ... do something ... t1 = times(NULL); printf ("Used %g CPU seconds\n", (double)( (t1 - t0) / (CLOCKS_PER_SEC + 0.0) )); printf ("... or more, if \"something\" took a long time.\n"); Used this way, times() is just a substitute for clock(). With a non-NULL argument, you can get additional information that clock() does not provide. Note that the value returned by a single call to times() or clock() is useless: You need to make two calls and subtract to discover how much CPU time was used in between. (If used with a non-NULL argument, though, the struct tms that times() fills in *is* meaningful.) http://www.opengroup.org/onlinepubs/009695399/functions/times.html -- Eric Sosman esosman(a)ieee-dot-org.invalid
From: Chris Friesen on 14 Apr 2010 15:14
On 04/14/2010 11:53 AM, sinbad wrote: > what is the correct use of times() function? It's actually difficult to use the return value of this function effectively. The return value can overflow, aliasing to negative numbers. This overlaps with the error code. The clock rate is variable, the starting point is arbitrary. On a more recent system, if all you want is intervals clock_gettime() is arguably more useful as it supports nanosecond resolution and various clocks (monotonic, realtime, per-process, per thread, etc.). Chris |