c - 监控 CPU 温度增量 T 内核模块

标签 c linux hardware sensors

我正在尝试监控 CPU 温度并计算 Linux 内核模块中的 delta T。我对内核模块知之甚少,但我正在使用 do_div() 除以整数。 我不明白为什么我总是以 1 为底。这是我的代码:

deltaT = sfan_temp - sfan_temp_old;
remainder = do_div ( deltaT, sfan_temp );

我的输出始终是 deltaT = 1 和余数 = x。 我想要的输出是 deltaT = x 和 remainder = y。 我的 delta T 公式是:

(sfan_temp-sfan_temp_old)/sfan_temp * 100; 

例如,如果 sfan_temp = 75°C 且 sfan_temp_old = 65°C,则

(75-65)/75*100 = 13.3333333

最佳答案

我不知道你是否应该使用do_div()。但是如果你使用它的话:

来自div64.h:

// the semantics of do_div() macros are:
uint32_t do_div(uint64_t *n, uint32_t base) {
  uint32_t remainder = *n % base;
  *n = *n / base;
  return remainder;
} 

在你的例子中:

n = 75 - 65 = 10
base = 75

// =>
remainder = 10 % 75 = 10
deltaT = n = 10 / 75 = 0

在这种情况下,对于 deltaT,您如何获得 1 而不是 0 尚不清楚。

do_div() 之前应用 *100:

n = (sfan_temp - sfan_temp_old)*100;
remainder = do_div(n, sfan_temp)

// =>
remainder = 1000 % 75 = 25 // remainder/sfan_temp = 0.3333
n = 1000/75 = 13

关于c - 监控 CPU 温度增量 T 内核模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7859273/

相关文章:

c - 我的回调结构不起作用

linux - 像 CGI 一样在 linux apache 中运行 EXE 文件

hadoop - 在笔记本电脑上安装 hadoop 的硬件要求

c++ - 如何在 Linux 上使用带有 EG20T 芯片组的英特尔凌动 Q7 模块上的 CAN 总线?

c - strcat 给出意外结果

linux - Linux 如何管理和跟踪物理内存页面?

architecture - RISC-V 中 JAL 的定义是什么以及如何使用它?

time - 如何编写具有尽可能接近实时特性的应用程序?

c - malloc 有时起作用,有时不起作用(通用链表)

linux - 我可以用 supervisord 监控守护进程/服务吗?