get process start time in kernel module solaris

485 views Asked by At

I am trying to get process start time in kernel module.
I get the proc struct pointer, and from the proc I take field p_mstart ()

typedef struct  proc {
.....
/*
* Microstate accounting, resource usage, and real-time profiling
*/
hrtime_t p_mstart;      /* hi-res process start time */

this return me the number: 1976026375725303

struct proc* iterated_process_ptr = curproc
LOG("***KERNEL***: PID=%d, StartTime=%lld",iterated_process_ptr->p_pidp->pid_id, iterated_process_ptr->p_mstart);

What is this number ? In the documentation solaris write:

The gethrtime() function returns the current high-resolution real time. Time is expressed as nanoseconds since some arbitrary time in the past.

And in the book Solaris Internals they write:

Within the process, the operating system maintains a high-resolution teimstamp that marks process start and terminate times, A p_mstart field, the process start time, is set in the kernel fork() code when the process is created.... it return 64-bit value expressed in nanosecond

The number 1976026375725303 does not make sense at all.
If i divide by 1,000,000,000 and then by 3600 in order to get hours, i get 528 hours, 22 days, but my uptime is 5 days..

2

There are 2 answers

0
ilansch On BEST ANSWER

Based on answer received at google group: comp.unix.solaris.

Instead of going to proc -> p_mstart

I need to take

iterated_process_ptr ->p_user.u_start  

This bring me the same struct (timestruc_t) as userspace

typedef struct psinfo {  

psinfo ->pr_start;  /* process start time, from the epoch */
3
Andrew Henle On

The number 1976026375725303 does not make sense at all.

Yes it does. Per the very documentation that you quoted:

Time is expressed as nanoseconds since some arbitrary time in the past.

Thus, the value can be used to calculate how long ago the process started:

hrtime_t howLongAgo = gethrtime() - p->p_mstart;

That produces a value in nanoseconds for how long ago the process started.

And note that the value produced is accurate - the value from iterated_process_ptr ->p_user.u_start is subject to system clock changes, so you can't say, "This process has been running for 3 hours, 15 minutes, and 3 seconds" unless you also know the system clock hasn't been reset or modified in any way.

Per the Solaris 11 gethrtime.9F man page:

Description

The gethrtime() function returns the current high-resolution real time. Time is expressed as nanoseconds since some arbitrary time in the past; it is not correlated in any way to the time of day, and thus is not subject to resetting or drifting by way of adjtime(2) or settimeofday(3C). The hi-res timer is ideally suited to performance measurement tasks, where cheap, accurate interval timing is required.

Return Values

gethrtime() always returns the current high-resolution real time. There are no error conditions.

...

Notes

Although the units of hi-res time are always the same (nanoseconds), the actual resolution is hardware dependent. Hi-res time is guaranteed to be monotonic (it does not go backward, it does not periodically wrap) and linear (it does not occasionally speed up or slow down for adjustment, as the time of day can), but not necessarily unique: two sufficiently proximate calls might return the same value.

The time base used for this function is the same as that for gethrtime(3C). Values returned by both of these functions can be interleaved for comparison purposes.