2017-06-25 72 views
1

在Solaris中,
我需要從內核空間和用戶空間獲取進程啓動時間。
我發現一個給定過程的2個開始時間,它們是不相等的!
其中一個位於proc struct(鏈接舊,但結構幾乎相同),另一個位於ps_info struct
在執行過程中,在內核空間,如果我使用比較struct proc中的starttime和solaris中的struct psinfo_t

struct proc* iterated_process_ptr = curproc; 

我得到以下結構:

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

如果我填補從用戶空間結構psinfo_t這樣的:

char psfile[64]; 
psinfo_t psinfo; 
sprintf(psfile, "/proc/ProcessID/psinfo"); 
if ((fd = open(psfile, O_RDONLY)) >= 0) 
    if (read(fd, &psinfo, sizeof(psinfo_t)) != -1)  

結構psinfo被填充,它看起來像:

typedef struct psinfo { 
timestruc_t pr_start; /* process start time, from the epoch */ 

2個開始時間有什麼區別?

如果我爲同一過程進行操作,值不同,這意味着高分辨率過程開始時間與過程時期開始時間不同。

什麼是高分辨率過程開始?

感謝

回答

1

結構is filled in here in OpenSolaris (see line 1008)fork()系統調用下p_mstart場:

/* 
    * Make proc entry for child process 
    */ 
    mutex_init(&cp->p_splock, NULL, MUTEX_DEFAULT, NULL); 
    mutex_init(&cp->p_crlock, NULL, MUTEX_DEFAULT, NULL); 
    mutex_init(&cp->p_pflock, NULL, MUTEX_DEFAULT, NULL); 
#if defined(__x86) 
    mutex_init(&cp->p_ldtlock, NULL, MUTEX_DEFAULT, NULL); 
#endif 
    mutex_init(&cp->p_maplock, NULL, MUTEX_DEFAULT, NULL); 
    cp->p_stat = SIDL; 
    cp->p_mstart = gethrtime(); 
    cp->p_as = &kas; 

Per the man page for gethrtime()

gethrtime()函數返回當前高分辨率實時 時間。時間表示爲自從過去的某個任意時間以來的納秒;它不以任何方式相關,一天的時間

因此,p_mstart場充滿了可用於判斷進程究竟有多少納秒前開始的時間,通過差異與回報當前呼叫的值爲gethrtime()

hrtime_t duration = gethrtime - p->p_mstart;