我正在尝试在内核模块中获得处理时间。
我获得了Proc Struct指针,从PROC i获得field p_mstart((
typedef struct proc {
.....
/*
* Microstate accounting, resource usage, and real-time profiling
*/
hrtime_t p_mstart; /* hi-res process start time */
这个返回我的数字: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);
这个数字是多少?在文档中,Solaris写下:
The gethrtime() function returns the current high-resolution real time. Time is expressed as nanoseconds since some arbitrary time in the past.
和在书中写的书中:
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
1976026375725303的数字根本没有意义。
如果我除以1,000,000,然后按3600划分以获取小时数,我得到528小时22天,但我的正常运行时间为5天。
基于Google Group收到的答案:comp.unix.solaris。
而不是去proc-> p_mstart
我需要服用
iterated_process_ptr ->p_user.u_start
这给我带来了与用户空间相同的结构(timestruc_t(
typedef struct psinfo {
psinfo ->pr_start; /* process start time, from the epoch */
数字1976026375725303根本没有任何意义。
是的。根据您引用的文档:
时间表示为纳秒,因为 过去。
因此,该值可用于计算该过程启动的时间:
hrtime_t howLongAgo = gethrtime() - p->p_mstart;
在该过程开始的时间之前,在纳米秒中产生一个值。
并注意,产生的值是准确的 - iterated_process_ptr ->p_user.u_start
的值会受到系统时钟的更改,因此您不能说:"此过程已经运行了3个小时,15分钟和3秒"知道系统时钟尚未以任何方式重置或修改。
根据Solaris 11 Gethrtime.9f人页:
描述
gethrtime()
功能返回当前高分辨率真实 时间。时间表示为纳秒,因为 过去;它与一天中的任何方式无关,,并且 因此,不受adjtime(2)
或settimeofday(3C)
。高分辨率计时器非常适合性能 测量任务,需要廉价,准确的间隔正时。返回值
gethrtime()
始终返回当前的高分辨率实时。 没有错误条件。...
注释
尽管高分辨率时间的单位始终是相同的(纳秒(,但 实际分辨率取决于硬件。高分时间是保证的 是单调的(它不会向后走,它不会定期 包裹(和线性(偶尔不会加快或放慢速度 调整,就像一天中的时间一样(,但不一定是唯一的:两个 足够接近的呼叫可能会返回相同的值。
用于此功能的时间表与
gethrtime(3C)
。这两个功能都返回的值可以是 为了比较目的交错。