Może coś źle kombinuję.
Zastanawia mnie, czemu jest taki czas po nanosleep3
jeśli pomiędzy wywołaniami clock_gettime
czas jest mniejszy od 1us.
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdarg.h>
#include <unistd.h>
#include <fcntl.h>
#include <getopt.h>
#include <pthread.h>
#include <signal.h>
#include <sched.h>
#include <string.h>
#include <time.h>
#include <errno.h>
#include <limits.h>
#include <linux/unistd.h>
#include <sys/prctl.h>
#include <sys/stat.h>
#include <sys/sysinfo.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <sys/utsname.h>
#include <sys/mman.h>
#include <sys/syscall.h>
#define NSEC_PER_SEC (1000000000)
void timespec_add_nanosec(struct timespec * t, long int interval){
t->tv_nsec += interval;
while (t->tv_nsec >= NSEC_PER_SEC){
t->tv_nsec -= NSEC_PER_SEC;
t->tv_sec++;
}
}
int clock_nanosleep2(clockid_t clock_id, int flags,
const struct timespec *request,
struct timespec *remain){
struct timespec t;
int ret;
do{
ret = clock_gettime(clock_id, &t);
}while(t.tv_sec <= request->tv_sec);
return ret;
}
void clock_nanosleep3(const struct timespec *request){
struct timespec t;
do{
clock_gettime(CLOCK_MONOTONIC, &t);
}while(t.tv_sec <= request->tv_sec);
}
int main(){
int i;
struct sched_param param;
#if 1
param.sched_priority = sched_get_priority_max(SCHED_FIFO);
if(sched_setscheduler(0, SCHED_FIFO, ¶m) == -1) {
perror("sched_setscheduler failed");
exit(-1);
}
mlockall(MCL_CURRENT | MCL_FUTURE);
setpriority(PRIO_PROCESS, getpid(), -20);
#endif
#if 1
cpu_set_t set;
CPU_ZERO(&set);
CPU_SET(0, &set);
if (sched_setaffinity( getpid(), sizeof( cpu_set_t ), &set )){
perror("sched_setaffinity failed");
exit(-1);
}
#endif
struct timespec t, t2, t3;
clock_gettime(CLOCK_MONOTONIC ,&t);
t.tv_sec++;
t.tv_nsec = 0;
//! TEST1
#if 1
for(i=0; i<10; ++i){
//clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &t, NULL);
//clock_nanosleep2(CLOCK_MONOTONIC, TIMER_ABSTIME, &t, NULL);
clock_nanosleep3(&t);
clock_gettime(CLOCK_MONOTONIC ,&t2);
clock_gettime(CLOCK_MONOTONIC ,&t3);
printf("%ld %ld\n", t2.tv_nsec, t3.tv_nsec);
t.tv_sec++;
}
#endif
//! TEST2
#if 0
int max = 0;
for(i=0; i<5000; ++i){
//clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &t, NULL);
//clock_nanosleep2(CLOCK_MONOTONIC, TIMER_ABSTIME, &t, NULL);
clock_nanosleep3(&t);
clock_gettime(CLOCK_MONOTONIC ,&t2);
if( (t2.tv_nsec - t.tv_nsec) > max)
max = t2.tv_nsec - t.tv_nsec;
timespec_add_nanosec(&t, 1000*1000 );
}
printf("max: %d\n", max);
#endif
return 0;
}
Wynik (czas w nanosekundach):
$ sudo ./main
7654 8352
8096 8795
7766 8535
7851 8619
8071 8769
7238 7937
8496 9195
7515 8284
8054 8822