0
点赞
收藏
分享

微信扫一扫

嵌入式Linux设备读取CPU温度的方法


​​http://embedded.kleier.selfhost.me/raspberry_cpu_temp.php​​


​​http://elinux.org/Jetson/Thermal​​

To observe the temperature changes with small heat capacity the timely resolution must be as high as possible. Therefore the measuring script must be efficient.

How does compiled code compare with interpreted code in terms of speed?

ARM 平台可能是这样:

# cat /sys/devices/virtual/thermal/thermal_zone0/temp

# cat /sys/class/thermal/thermal_zone0/temp

x86 平台可能是这样:

# cat /proc/acpi/thermal_zone/THRM/temperature

为了更方便读取 CPU 温度信息,我们可以编写一些小程序来提高效率。

一、Bash 脚本

cat /proc/uptime > ${OUTFILE}
for ((i=0;i<10000;++i)) ; do
( date "+%s.%3N" ; cat /sys/devices/virtual/thermal/thermal_zone0/temp) |\
awk '{if (NR%2) date = $1; else print date, $1;}'
done >> ${OUTFILE}
cat /proc/uptime >> ${OUTFILE}

二、C 语言

#define _POSIX_C_SOURCE 199309L // enable interface for struct timespec ...

#include <time.h> // struct timespec, clock_gettime
#include <stdio.h> // FILE, fscanf, stdout, printf

const unsigned N = 10000;

typedef struct
{struct timespec t;
unsigned temp;}
RecT;

int main ()
{RecT r [N];
for (unsigned i = 0; i < N; ++i)
{(void) clock_gettime (CLOCK_REALTIME, &r [i].t);
FILE *fp = fopen ("/sys/devices/virtual/thermal/thermal_zone0/temp", "r");
(void) fscanf (fp, "%u", &r [i].temp);
(void) fclose (fp);}
for (unsigned i = 0; i < N; ++i)
printf ("%lu.%06llu %u\n", r [i].t.tv_sec, (r [i].t.tv_nsec + 500ull) / 1000ull, r [i].temp);
return 0;}


举报

相关推荐

0 条评论