gpt4 book ai didi

c++ - 如何在 C 中以较低的时钟速度运行程序

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:43:35 25 4
gpt4 key购买 nike

我需要模拟硬件的工作原理(不适用于视频游戏)。

此组件以 1 Ghz 运行,而我的 PC 以 2.5 和 2.7 Ghz 运行。

所以我试图告诉电脑以较低的速度运行这个特定的进程。

我已经尝试过计时器,但它不会做:当处理小的时间间隔时,进程不会准确地跟踪时间(我需要跟踪毫秒,但它无法完成整齐地)还要计算时间间隔,你会失去一些 CPU 时间

请记住,我没有外包给社区,我是自己工作,但也许你们可以帮助集思广益:)

最佳答案

前提

我从您的问题和评论中了解到,您需要在 12.5 Hz CPU 上运行程序。我可以想到单步执行指令,就像调试器一样,但它不是等待你单步执行指令,而是在每个 时间延迟 执行每条指令(很像你说的你试过的). 所以,如果这个前提是错误的,请告诉我,我会删除我的答案,因为它是基于它的。

理念

如果您的时钟计数是 80ms,这意味着您每 80ms 至少可以执行一条指令。不幸的是 sleep函数只会在 内接受unsigned int 参数,所以这是行不通的。但是,有系统调用 nanosleep ,它可以让你调整 sleep nanoseconds .

因此,要将该毫秒转换为纳秒,请将其乘以 106,这将为您提供 80000000 纳秒 的 sleep 时间。正如您已经提到的,函数调用和模拟器时间会浪费一些时间,但我认为这是您为模拟器付出的代价(而且您总是可以修补时间做更精细的调整)。所以,nanosleep 是:

#include <time.h>

int nanosleep(const struct timespec *req, struct timespec *rem);

struct timespec {
time_t tv_sec; /* seconds */
long tv_nsec; /* nanoseconds */
};

另一个是 Linux 系统调用 ptrace

#include <sys/ptrace.h>

long ptrace(enum __ptrace_request request, pid_t pid,
void *addr, void *data);

这个函数可以让你用跟踪的进程做各种事情,我建议你阅读手册。这是非常有用的。该系统调用是调试软件的基本功能,您可以在 How Debuggers Work 上阅读教程。在这里。

其实我的想法是从那篇教程中得到的(我前几天看过),我会稍微修改一下代码来做模拟器,所以我也建议阅读教程。


代码

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#include <time.h>
#include <sys/ptrace.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/reg.h>
#include <sys/user.h>

#define MILLI 80
#define HZ ((double)1000/(double)MILLI)

/* milli to nano */
#define m2n(a) (a*1000*1000)

void run_target(char *prog)
{
printf("Emulating %.2lf Hz to proccess %s...\n\n", HZ, prog);

if (ptrace(PTRACE_TRACEME, 0, 0, 0) < 0) {
perror("ptrace");
return ;
}

execl(prog, prog, (char*)NULL);
}

void run_emulator(pid_t child)
{
int wait_status;
struct timespec req;
unsigned long int count = 1;

/* set up the emulation speed */
req.tv_sec = 0;
req.tv_nsec = m2n(MILLI);

/* wait for stop on first instruction */
wait(&wait_status);
while (WIFSTOPPED(wait_status)) {
/* this loop will repeat at every instruction, so it executes the
* instruction and sleeps for the amount of time needed to
* emulate the wanted speed.
*/
if (ptrace(PTRACE_SINGLESTEP, child, 0, 0) < 0) {
perror("ptrace");
return ;
}
wait(&wait_status);

/* this does the sleep */
nanosleep(&req, NULL);
}
}

int main(int argc, char *argv[])
{
pid_t child;

if (argc < 2) {
fprintf(stderr, "Usage: %s [prog_name]\n", argv[0]);
exit(EXIT_FAILURE);
}

child = fork();

if (!child)
run_target(argv[1]);
else if (child > 0)
run_emulator(child);
else {
perror("fork");
exit(EXIT_FAILURE);
}

return 0;
}

为了进行快速测试,我用 Assembly 编写了这个简单的 fat(5) 计算器,它有 65 条指令(当然是在我的机器上):

 .section .data
.section .text
.globl _start
.globl factorial
_start:

pushq $5
call factorial
movq %rax, %rdi
movq $0x3c, %rax
syscall

.type factorial, @function
factorial:
pushq %rbp
movq %rsp, %rbp
movq 16(%rbp), %rax
cmpq $1, %rax
je end_factorial
decq %rax
pushq %rax
call factorial
movq 16(%rbp), %rbx
imulq %rbx, %rax

end_factorial:
movq %rbp, %rsp
popq %rbp
ret

组装、链接、运行并查看结果:

$ as -o fat.o fat.s
$ ld -o fat fat.o
$ ./fat
$ echo $?
120
$

因此,它可以工作并计算 5 的阶乘。因此,如果我的数学计算正确,65 条指令将花费 65/12.5 秒在 12.5Hz CPU 上运行,对吗? 65/12.5 = 5.2

$ time ./lower ./fat
Emulating 12.50 Hz to proccess ./fat...

Returned: 30720

real 0m5.211s
user 0m0.000s
sys 0m0.008s

手册引用

关于c++ - 如何在 C 中以较低的时钟速度运行程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32325637/

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