|
你说的是 RMS 调度器?
如果是,测试程序可以这样写:
#include <stdio.h>
#include <pthread.h>
#include <time.h>
#include <sched_rms.h>
sched_rms_t rms;
#define __TIMEVAL_NSEC_MAX 1000000000
static void timespecSub (struct timespec *ptv1, const struct timespec *ptv2)
{
ptv1->tv_sec -= ptv2->tv_sec;
ptv1->tv_nsec -= ptv2->tv_nsec;
if (ptv1->tv_nsec >= __TIMEVAL_NSEC_MAX) {
ptv1->tv_sec++;
ptv1->tv_nsec -= __TIMEVAL_NSEC_MAX;
} else if (ptv1->tv_nsec < 0) {
ptv1->tv_sec--;
ptv1->tv_nsec += __TIMEVAL_NSEC_MAX;
}
}
int main (int argc, char **argv)
{
struct timespec period = {0, 5 * 1000 * 1000};
struct timespec last = {0, 0};
struct timespec now, diff;
volatile int a;
if (argc >= 2) {
period.tv_nsec = atoi(argv[1]) * 1000 * 1000;
}
sched_rms_init(&rms, pthread_self());
clock_gettime(CLOCK_MONOTONIC, &last);
while (1) {
sched_rms_period(&rms, &period); // 设置单调执行周期
clock_gettime(CLOCK_MONOTONIC, &now);
diff = now;
timespecSub(&diff, &last);
last = now;
for (a = 0; a < 1000; a++); // 这个地方可以放一个随机延迟函数来模拟程序代码执行所花的时间,可以是随机的, 但不能超过 rms 周期时间
printf("diff is %8lld sec %09ld nsec\n", diff.tv_sec, diff.tv_nsec);
}
return 0;
}
|
|