这个计算是如何工作的?

How is this Calculation working?

我遇到过这段代码,其中计算了自 epoc 和自启动以来的纳秒数,但我不知道如何计算?

http://code.metager.de/source/xref/cloudius-systems/osv/arch/aarch64/arm-clock.cc#61

 s64 arm_clock::uptime()  
{
    u64 cntvct;
    asm volatile ("isb; mrs %0, cntvct_el0; isb; " : "=r"(cntvct) :: "memory");

    cntvct = ((__uint128_t)cntvct * NANO_PER_SEC) / this->freq_hz;
    return cntvct; 
}

CNTVCT是循环计数寄存器,为什么这个寄存器的值除以CPU频率再乘以NANO_PER_SEC?

此外,如何从这些纳秒中得出时间?

why is the value of this register divided by CPU frequency and then multiplied by NANO_PER_SEC?

是换算单位

CNTVCT每个时钟周期递增

频率是每秒时钟周期

NANO_PER_SEC 是纳秒每秒

所以查看单位:

CNTVCT * NANO_PER_SEC
-------------
frequency   

他们是:

clocks * nano/sec
------------------------
clocks/sec

等于(分子和分母乘以sec

clocks * nano
-------------
clocks

等于(分子分母除以clocks

nano

所以你需要除以频率得到秒并乘以纳秒得到纳秒

Also, how can Time of day be derived from these nanoseconds?

纳秒是从启动时间开始的,而不是纪元

但是,您可以获取当前时间,减去当前纳秒,然后向前调整纳秒