【问题标题】:get process start time in kernel module solaris在内核模块 solaris 中获取进程开始时间
【发布时间】:2017-06-26 08:06:23
【问题描述】:

我正在尝试在内核模块中获取进程启动时间。
我得到proc struct pointer,并从proc中获取字段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.

他们在 Solaris Internals 一书中写道:

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,000 然后除以 3600 以获得小时数,我得到 528 小时 22 天,但我的正常运行时间是 5 天..

【问题讨论】:

    标签: c time solaris unix-timestamp high-resolution-clock


    【解决方案1】:

    基于在 google 小组收到的回答: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 */
    

    【讨论】:

      【解决方案2】:

      1976026375725303 这个数字根本没有意义。

      是的。根据您引用的文档:

      时间表示为纳秒,因为在 过去。

      因此,该值可用于计算进程开始的时间:

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

      这会产生一个以纳秒为单位的值,表示进程开始的时间。

      请注意,生成的值是准确的 - 来自iterated_process_ptr ->p_user.u_start 的值受系统时钟变化的影响,因此您不能说“此进程已运行 3 小时 15 分 3 秒”,除非你也知道系统时钟没有被重置或以任何方式修改。

      根据Solaris 11 gethrtime.9F man page

      说明

      gethrtime()函数返回当前高分辨率实数 时间。 时间表示为纳秒,因为在某些任意时间 过去;它与一天中的时间没有任何关系,并且 因此不受adjtime(2) 的重置或漂移的影响或 settimeofday(3C)。高分辨率计时器非常适合性能 测量任务,需要廉价、准确的间隔定时。

      返回值

      gethrtime() 总是返回当前的高分辨率实时。 没有错误条件。

      ...

      备注

      虽然高分辨率时间的单位始终相同(纳秒), 实际分辨率取决于硬件。保证高分辨率时间 是单调的(它不会倒退,它不会周期性地 wrap)和线性(它不会偶尔加速或减速 调整,一天中的时间可以),但不一定是唯一的:两个 足够接近的调用可能会返回相同的值。

      此功能使用的时基与 gethrtime(3C)。这两个函数返回的值可以是 交错用于比较。

      【讨论】:

      • 谢谢,我不是在测量进程的生命周期,我从内核向用户发送进程列表,并且对于每个进程我做一些事情,如果进程死了,我不想做一些事情对于错误的进程..也许一个新进程以确切的 pid 出现..所以我保存了 processId+StartTime,这是确保我在谈论同一个进程的最佳方法。而且从用户空间我没有找到为给定进程ID(整数是ID)获取高分辨率时间的方法
      • @ilansch 并且从用户空间我没有找到获取给定进程 ID 的高分辨率时间的方法(整数是 ID) 它可能无法从用户空间访问.没什么大不了的,但在内核空间内,pid 和p_mstart 应该保证是唯一的。对于具有相同 PID 的两个进程,我看不出有任何方法可以让进程 ID 快速翻转以获取 p_mstart 的相同值。第一个必须启动、运行和终止,然后进程 ID 必须翻转,并且新进程以相同的 PID 启动,所有这些都在 gethrtime() 滚动到下一个值之前。
      • 嗨,这就是重点,不可能 2 个进程将具有相同的 PID 和启动时间,但可能有 2 个进程具有相同的 PID(一个会死并且会发生新的)....这就是为什么我在标记我的进程时需要PID + Starttime
      猜你喜欢
      • 2017-11-08
      • 2021-05-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-26
      • 1970-01-01
      • 2014-09-03
      相关资源
      最近更新 更多