- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
在 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答案建议设置我的流程的优先级但没有效果。
这是我目前的结论:
gpg
时。进程获取某些数据所以我的问题是:我需要一个准确的计时器,间隔大约为 10 毫秒(几毫秒的抖动就可以了)。我可以用 timerfd
实现吗? ?我有哪些选择?
使用的内核版本是4.4.19 (OpenEmbedded/Yocto)
复制
目前我不知道有什么方法可以重现所描述的行为:
nginx
安装proxy_pass
ing端口80
到其他一些端口,例如8081receive.py
在将收听 POST
的设备上请求,接收一个大文件并将其通过管道传输到 GnuPG
test.py
在设备上观察 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 aboveimport 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 generateimport 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/
关于这个话题已经说了很多,但是我找不到我的问题的确切答案。 JavaScript 无法准确表示 0.1 等小数,这是可以理解的。 例如,由于乘法运算期间发生舍入误差,这是正确的: 0.1 * 3 ==
在 zig 中,可以使用“{d}”以十进制表示法打印浮点值。这将自动以全精度打印该值。有没有办法指定位数?是针对每个值,还是作为某种全局设置? 最佳答案 这将限制小数点后的位数,四舍五入和零填充: f
我正在进行的项目需要高精度。减法时我遇到的问题在这里说明: >> 1-0.9999999999999999 ans = 1.1102e-16 >> 1-0.99999999999999999 ans
是否可以使变量本身的精度成为将在运行时定义的变量? 说,如果我尝试编译: SUBROUTINE FOO( VARIABLE, PRECISION_VALUE ) IMPLICI
我正在查询 SQLite 数据库以获取纬度/经度详细信息。 SELECT * FROM tblMain where latitude > -33.866 and latitude 151.20
我一直使用下划线将整数定义为 Fortran 中的特定类型。 下面是一段代码,用于演示 1_8 的含义,例如: program main implicit none integer(2)
我正在寻找一种方法来告诉 pint 要打印多少个有效数字。例如,当我输入以下内容时: import pint ureg = pint.UnitRegistry() print(3*ureg.m /9)
我正在从事一个项目,目标是从山上追踪动物。在第一个实地考察季中,我们使用了 OpenTags 和经过校准的摄像头,虽然可以正常工作,但需要大量的处理/校准,而且至关重要的是,当系统出现问题时无法提供任
在 JavaScript 中有没有一种方法可以确定一个数除以另一个数是否会得到整数?就像 18.4/0.002 给我们 9200,但是 18.4/0.1 给我们 183.99999999999997。
我正在尝试使用 Big.js 在 javascript 中完成此计算 r = (a * b)/ sqrt( ( a*sin(θ) )^2 + ( b*cos(θ) )^2 ) 我也试过 math.js
我有这个片段着色器代码,它在 iOS 模拟器(非视网膜)和 iPad2(非视网膜)之间显示不同: highp vec2 textCoord; textCoord.x = gl_Fr
这个问题在这里已经有了答案: C++ calculating more precise than double or long double (2 个答案) 关闭 6 年前。 是否有任何浮点类型在小
我似乎一直困惑的三个问题: 为什么代码是 x & ~077比这行代码 x & 0177700 更好。是因为精度损失较小吗? 为什么此代码对于设置数字中的第 5 位不正确? num = num + 0x
这个问题在这里已经有了答案: 关闭 10 年前。 Possible Duplicate: Precision of Floating Point 我正在尝试使用一些 float 来计算概率,但我的最
由于微 Controller 的精度,我定义了一个包含两个 float 比率的符号,而不是直接写结果。 #define INTERVAL (0.01F/0.499F) 代替 #defi
我试图比较这 3 种搜索算法,起初我使用 time.h 库但没有任何反应,输出始终是 0.00000 秒。现在我试图在循环中使用一些计数器。但我在这里也有问题, 任何人都可以帮我处理代码吗? 这是我的
char buf[10]; int counter, x = 0; snprintf (buf, sizeof buf , "%.100d%n", x, &counter); printf("Coun
我注意到在评估向量时对我来说是不可预测的行为。直接执行它与在循环中进行索引似乎是完全不同的。谁能帮我解决这个问题?我知道可能在它如何进行每个操作中都有解释,所以我需要一些关于如何查找它的键 多谢指教提
我想在我的应用程序中使用精确的 gps 定位。所以我遵循了一个简单的教程(LocationManager 的基本用法,明确要求 GPS 提供商,要求更新 0 ms,0 m)并创建了一个应用程序。我对更
float 在 1.0f 和 0.0f 之间有多少位精度,这样每个值都可以唯一表示? 例如,如果第一个小数 float 不能表示 0.13f,答案就是 float 只有一位精度。 最佳答案 std::
我是一名优秀的程序员,十分优秀!