简体   繁体   English

Linux - 更快地读取或收集文件内容(例如每秒 cpu 温度)

[英]Linux - read or collect file content faster (e.g. cpu temp every sec.)

I'm working on a system on which ubuntu is running.我正在运行 ubuntu 的系统上工作。 I'm reading basic data like CPU frequency and temperature out of the thermal zones provided in /sys/class/thermal.我正在从 /sys/class/thermal 中提供的热区读取基本数据,例如 CPU 频率和温度。

Unfortunately, I've got around 100 thermal_zones from which I need to read the data.不幸的是,我有大约 100 个 thermal_zones,我需要从中读取数据。 I do it with:我这样做:

for SENSOR_NODE in /sys/class/thermal/thermal_zone*; do printf "%s: %s\n" $(cat ${SENSOR_NODE}/type) $(cat ${SENSOR_NODE}/temp); done

To collect all data takes ~2.5-3 sec.收集所有数据大约需要 2.5-3 秒。 which is way to long.这太长了。 Since I want to collect the data every second my question is, if there is a way to "read" or "collect" the data faster?因为我想每秒收集数据,所以我的问题是,是否有一种方法可以更快地“读取”或“收集”数据?

Thank you in advance先感谢您

There's only so much you can do while writing your code in shell, but let's start with the basics.在 shell 中编写代码时,您只能做这么多,但让我们从基础开始。

  • Command substitutions, $(...) , are expensive: They require creating a FIFO, fork() ing a new subprocess, connecting the FIFO to that subprocess's stdout, reading from the FIFO and waiting for the commands running in that subshell to exit.命令替换$(...)是昂贵的:它们需要创建一个 FIFO, fork()一个新的子进程,将 FIFO 连接到该子进程的标准输出,从 FIFO 读取并等待在该子 shell 中运行的命令退出.
  • External commands, like cat , are expensive: They require linking and loading a separate executable;cat这样的外部命令是昂贵的:它们需要链接和加载一个单独的可执行文件; and when you run them without exec (in which case they inherit and consume the shell's process ID), they also require a new process to be fork() ed off.当你在没有exec的情况下运行它们时(在这种情况下它们继承并使用 shell 的进程 ID),它们需要一个新进程来fork() ed off。

All POSIX-compliant shells give you a read command:所有 POSIX 兼容的 shell 都会为您提供read命令:

for sensor_node in /sys/class/thermal/thermal_zone*; do
  read -r sensor_type <"$sensor_node/type" || continue
  read -r sensor_temp <"$sensor_node/temp" || continue
  printf '%s: %s\n' "$sensor_type" "$sensor_temp"
done

...which lets you avoid the command substitution overhead and the overhead of cat . ...这可以让您避免命令替换开销和cat的开销。 However, read reads content only one byte at a time;但是, read一次只读取一个字节的内容; so while you're not paying that overhead, it's still relatively slow.因此,虽然您没有支付这笔开销,但它仍然相对较慢。

If you switch from /bin/sh to bash , you get a faster alternative:如果你从/bin/sh切换到bash ,你会得到一个更快的选择:

for sensor_node in /sys/class/thermal/thermal_zone*; do
  printf '%s: %s\n' "$(<"$sensor_node/type)" "$(<"$sensor_node/temp")"
done

...as $(<file) doesn't need to do the one-byte-at-a-time reads that read does. ...因为$(<file)不需要像 read 那样进行一次一个字节的read That's only faster for being bash , though;不过,这只会比bash更快; it doesn't mean it's actually fast .这并不意味着它实际上很快 There's a reason modern production monitoring systems are typically written in Go or with a JavaScript runtime like Node.现代生产监控系统通常使用Go或 JavaScript 运行时(如 Node.js)编写是有原因的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM