gpt4 book ai didi

Linux CPU 频率缩放影响 timerfd 精度

转载 作者:可可西里 更新时间:2023-11-01 11:51:33 27 4
gpt4 key购买 nike

在 Linux 驱动的单核嵌入式 Cortex-A8 机器上,我遇到了 timerfd 的问题: 我需要每隔几毫秒触发一些 IO,到目前为止,我用这种方式创建的计时器一切正常:

int _timer_fd = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK);
int _flags = 0;
itimerspec _new_timer;
_new_timer.it_interval.tv_sec = interval / 1000000;
_new_timer.it_interval.tv_nsec = (interval % 1000000) * 1000;
_new_timer.it_value.tv_sec = _new_timer.it_interval.tv_sec;
_new_timer.it_value.tv_nsec = _new_timer.it_interval.tv_nsec;
timerfd_settime(_timer_fd, _flags, &_new_timer, NULL);

.. 和 select()正在处理文件描述符。

CPU 默认运行在 800MHz,可以缩减到 300MHz。即使在最低频率下,计时器也会定期触发,即使是在高系统负载和繁重的 IO 情况下。

现在问题来了:当我将 CPU 频率调节器设置为 ondemand 时切换频率时,定时器会错过几秒钟(我见过长达 2800 毫秒)的唤醒。

我说的IO涉及上传大文件(网络IO、提取/CPU、写入flash)。单独创建/提取大型存档似乎不是问题。

我修改了this handy little Python script使用timerfd每 100 毫秒打印一次 CPU 频率和时间差异,我可以重现问题!正在运行 test.py并开始上传(重 IO)给我以下输出:

f=300000 t=0.100021, count=01 *
f=600000 t=0.099609, count=01 * <== switch, but no problem
f=600000 t=0.099989, count=01 *
f=300000 t=0.100388, count=01 * <== switch, but no problem
f=300000 t=0.099874, count=01 *
f=300000 t=0.099944, count=01 *
f=300000 t=0.100000, count=01 *
f=600000 t=0.099615, count=01 * <== switch, but no problem
f=600000 t=0.100033, count=01 *
f=600000 t=0.099958, count=01 *
f=600000 t=0.100003, count=01 * <== IO starts
f=600000 t=0.100062, count=01 *
f=600000 t=0.100318, count=01 *
f=800000 t=0.418505, count=04 **** <== 3 misses
f=800000 t=0.081735, count=01 *
f=800000 t=0.100019, count=01 *
f=800000 t=0.099284, count=01 *
f=800000 t=0.100584, count=01 *
f=800000 t=0.100089, count=01 *
f=800000 t=0.099623, count=01 *
f=720000 t=1.854099, count=18 ****************** <== 17 misses
f=720000 t=0.046591, count=01 *
f=720000 t=0.099038, count=01 *
f=720000 t=0.100744, count=01 *
f=720000 t=0.099240, count=01 *
f=720000 t=0.100029, count=01 *
f=720000 t=0.099985, count=01 *
f=720000 t=0.100007, count=01 *
f=800000 t=2.715434, count=27 *************************** <== 26 misses
f=800000 t=0.085148, count=01 *
f=800000 t=0.099992, count=01 *
f=800000 t=0.099648, count=01 *
f=800000 t=0.100367, count=01 *
f=800000 t=0.099406, count=01 *
f=800000 t=0.099984, count=01 *
f=720000 t=2.446585, count=24 ************************ <== 23 misses
f=720000 t=0.054219, count=01 *
f=720000 t=0.099947, count=01 *
f=720000 t=0.099284, count=01 *
f=720000 t=0.100721, count=01 *
f=720000 t=0.099975, count=01 *
f=720000 t=0.100089, count=01 *
f=800000 t=2.391552, count=23 *********************** <== 22 misses
f=800000 t=0.015058, count=01 *
f=800000 t=0.092592, count=01 *
f=800000 t=0.100651, count=01 *
f=800000 t=0.099982, count=01 *
f=800000 t=0.099967, count=01 *

我试过了 this答案建议设置我的流程的优先级但没有效果。

这是我目前的结论:

  • 问题不是由我的 C 程序引起的,因为我可以用一些 Python 脚本重现它
  • CPU 性能不是问题,因为将频率固定为 300MHz 效果很好
  • 产生重负载的进程必须满足某些要求(见下文)——仅仅进行网络 IO 或 CPU 密集型操作是行不通的
  • 计时器间隔似乎只发生在 gpg 时。进程获取某些数据

所以我的问题是:我需要一个准确的计时器,间隔大约为 10 毫秒(几毫秒的抖动就可以了)。我可以用 timerfd 实现吗? ?我有哪些选择?

使用的内核版本是4.4.19 (OpenEmbedded/Yocto)

复制

目前我不知道有什么方法可以重现所描述的行为:

  • 在具有网络访问权限的嵌入式设备上有nginx安装proxy_pass ing端口80到其他一些端口,例如8081
  • 运行receive.py在将收听 POST 的设备上请求,接收一个大文件并将其通过管道传输到 GnuPG
  • 运行test.py在设备上观察 CPU 频率和定时器精度
  • 将 cpu 调速器设置为 ondemand : echo ondemand > /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor
  • 使用upload.py在另一台机器上发送一个包含随机内容的 10M 文件到嵌入式
  • 上传数据的内容好像很重要! upload.py <ip/hostname> 10000000将生成一个随机字节流并将其存储到名为 data-out 的文件中之前 POST对其进行处理 - 在大多数情况下,您不会看到计时器间隙 - 如果您可以观察到它们,您可以保留该文件并在以后重用它
  • 正在运行 upload.py从嵌入式设备(无网络)或遗漏 nginx行不通!

文件

This is the modified version of test.py which produces the output above

import asyncore, time, timerfd.async

class TestDispatcher(timerfd.async.dispatcher):
def __init__(self, *args):
super().__init__(*args)
self._last_t = time.time()

def handle_expire(self, count):
t = time.time()
f = open('/sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq').readline().strip('\n')
print("f=%s t=%.6f, count=%0.2d %s" % (f, t - self._last_t, count, '*' * count))
self._last_t = t

dispatcher = TestDispatcher(timerfd.CLOCK_MONOTONIC)
dispatcher.settime(0, timerfd.itimerspec(0.1, 1))
asyncore.loop()

receive.py

import subprocess, http.server, socketserver
class InstallationHandler(http.server.BaseHTTPRequestHandler):
def do_POST(self):
gpg_process = subprocess.Popen(
['gpg', '--homedir', '/home/root/.gnupg', '-u', 'Name', '-d'],
stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
tar_process = subprocess.Popen(
['tar', '-C', '.', '-xzf', '-'],
stdin=gpg_process.stdout, stderr=subprocess.PIPE)
content_length = int(self.headers['content-length'])
while content_length > 0:
content_length -= gpg_process.stdin.write(
self.rfile.read(min(1000, content_length)))
gpg_process.stdin.close()
self.send_response(201)
self.end_headers()

socketserver.TCPServer.allow_reuse_address = True
socketserver.TCPServer(('', 8081), InstallationHandler).serve_forever()

upload.py - provide either a file name to upload or a number of bytes to generate

import http.client, sys, os
if os.path.exists(sys.argv[2]):
print('read.. %r' % sys.argv[2])
b = open(sys.argv[2], 'rb').read()
else:
print('generate random data..')
b = os.urandom(int(sys.argv[2]))
open('data-out', 'wb').write(b)
b = bytes(b)
print('size=%d' % len(b))
h = http.client.HTTPConnection(sys.argv[1])
h.request('POST', '/upload/calibration_data', b)
print(h.getresponse().read())

最佳答案

初步回答。假设您不想禁用 cpufreq 或进行任何其他会导致功耗变化的侵入式内核配置更改。

让我假设抖动不是来自 cpu 时钟和定时器时钟之间的某种奇怪的交互,这很难消除。

我们还假设您愿意稍微修改一下。在那种情况下...使用您自己的硬件计时器!

ARM SoC 通常有许多硬件定时器,而 Linux 通常只使用其中两个:一个用于提供定时器(即 timerfd 和其他定时器接口(interface)),另一个用于计时。这意味着您通常有很多空闲且可用的硬件计时器。

不幸的是,Linux 不提供任何框架或接口(interface)来使用它们,因此您必须自己动手。例如here有一个 MIPS SoC AR9331 的例子。

为您的 ARM SoC 做这件事只需阅读数据表、检查寄存器并可能修改该示例,或者提出您自己的解决方案。

抖动会小很多,因为它是一个硬件定时器,会产生中断,因此不受常规负载的影响。

如果您希望抖动更小,可以尝试快速中断 (FIQ)。 Bootlin(前 Free Electrons)在他们的 blog 上解释了这个绝妙的技巧。 .

关于Linux CPU 频率缩放影响 timerfd 精度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48683823/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com