源程序是这样的:
#include <stdio.h>
#include <errno.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <glob.h>
#include <sys/swap.h>
#include <sys/sysinfo.h>
#include <stdint.h>
static inline uint64_t get_cycle_count()
{
unsigned int lo, hi;
__asm__ __volatile__("rdtsc" : "=a" (lo), "=d" (hi));
return ((uint64_t)hi << 32) | lo;
}
int main()
{
uint64_t old_cycle, new_cycle;
int i = 0;
int delayms = 700;
old_cycle = get_cycle_count();
usleep(delayms * 1000);
new_cycle = get_cycle_count();
printf("%d\n", (new_cycle - old_cycle) / (delayms * 1000));
return 0;
}
关键点是usleep的延时,这里在单核cpu上是没有问题的,但对于多核心可能会出现线程前后运行在不同的cpu上的问题,如何解决呢?其实也没有好的解决办法!稳妥的方法当然是直接proc了:

除此之外就只能上驱动了!
探讨了在多核CPU环境下使用usleep进行时间测量的问题,指出可能因线程迁移导致测量不准确,并提出直接使用proc或驱动级解决方案。
1890

被折叠的 条评论
为什么被折叠?



