纳秒级C++程序空闲/节流

Nanosecond-level C++ program idling / throttling

本文关键字:节流 程序 C++      更新时间:2023-10-16

我正在编写一个运行微控制器模拟器的C++程序。运行模拟器的线程在如下所示的循环中执行此操作:

while(should_run) {
simulator->RunSingleClockCycle();
}

在我的笔记本电脑上,它设法以大约 60 MHz 的速度运行模拟器。由于我只需要模拟器以 16MHz 的速度运行,因此我可以在此 while 循环中使用 busyloop 来减慢它的速度,以便它以 16MHz 运行,因为不可能再睡一纳秒。

由于此线程正在运行模拟器并繁忙循环,因此它会导致我的一个内核上的 CPU 使用率为 100%。如果有一种方法可以以某种方式限制线程,使其以16MHz(无繁忙循环(运行微控制器,它将减少模拟器的资源使用。

是否有任何半跨平台(macOS + linux(的方法可以在C++中实现这种线程限制?

在传统硬件上,您永远不会获得纳秒级计时。例如,在我的运行此代码的系统上(不考虑竞争条件(:

#include <thread>
#include <chrono>
#include <future>
#include <iostream>

int main()
{
unsigned int counter = 0;
auto res = std::async(std::launch::async, [&]()
{
while (true)
{
std::cout << "Count : " << counter << 'n';
counter = 0;
std::this_thread::sleep_for(std::chrono::seconds(1));
}
});

auto timetoWait = 1'000'000'000 / 16'000'000;
while (true)
{
++counter;
std::this_thread::sleep_for(std::chrono::nanoseconds(timetoWait));
}
}

我每秒最多获得 600-700k 的"操作"。远非模拟器应该运行的 1600 万。

你最好只是忙着循环。

你可以使用chrono来实现这个目的,我很确定它适用于Linux和Windows。

我不知道有任何方法可以测量100%准确的时间。 这些时间本身会调用一些时间,但它们相对准确。

#include <iostream>
#include <chrono>
bool wait(long long nanoseconds)
{
// if the number of ns to wait is not zero
if(0 != nanoseconds)
{
// init high resolution clock
std::chrono::high_resolution_clock                              hrc             = {};
// get the start and stop timepoints
std::chrono::time_point<std::chrono::high_resolution_clock>     start           = hrc.now(),
  stop            = hrc.now();
// calculate the number of ns passed by subtracting the time at the start from the time at the stop
std::chrono::nanoseconds                                        time_passed     = stop - start;
// while the number of ns to wait for is bigger than the number of ns passed
while(nanoseconds > time_passed.count())
{
// get the new stop timepoint
stop            = hrc.now();
// calculate the new number of ns passed
time_passed     = stop - start;
}
// the wait has ended
return true;
}
// the function failed
return false;
}
int main()
{
printf("startn");
// wait for 5 seconds
wait(5000000000);
printf("stopn");
getchar();
return 0;
}

您也可以使用 rdtsc 指令来提高准确性,但我无法使示例代码正常工作,所以我还是会发布它。

这是汇编代码(我的IDE,Visual Studio,不支持x64上的内联汇编,所以我不得不单独编写(

.model flat, c
.code
get_curr_cycle proc
cpuid
cpuid
cpuid
rdtsc
shl edx, 32
or edx, eax
mov eax, edx
retn
get_curr_cycle endp
end

下面是 C++ 代码。

#include <iostream>
extern "C" unsigned int get_curr_cycle();
bool wait(long long nanoseconds)
{
if(0 != nanoseconds)
{
unsigned int    start           = get_curr_cycle(),
stop            = get_curr_cycle();
unsigned int    time_passed     = (stop - start);
while(nanoseconds > time_passed)
{
stop            = get_curr_cycle();
time_passed     = (stop - start);
}
}
return false;
}
int main()
{
printf("startn");
// wait for 5 seconds
wait(5000000000);
printf("stopn");
getchar();
return 0;
}

我认为您可能正在寻找的是类似的东西

std::this_thread::sleep_for(std::chrono::nanoseconds(1));

另请参阅:

https://en.cppreference.com/w/cpp/thread/sleep_for

https://en.cppreference.com/w/cpp/thread/sleep_until

https://en.cppreference.com/w/cpp/chrono/duration