1 #define _GNU_SOURCE 2 #include 3 #include 4 #include 5 #include 6 #include 7 #include 8 #include 9 #include 0 #include 1 #include 2 3 #define gettid() syscall(__NR_gettid) 4 5 #define SCHED_DEADLINE 6 6 7 /* XXX use the proper syscall numbers */ 8 #ifdef __x86_64__ 9 #define __NR_sched_setattr 314 0 #define __NR_sched_getattr 315 1 #endif 2 #ifdef __i386__ 3 #define __NR_sched_setattr 351 4 #define __NR_sched_getattr 352 5 #endif 6 #ifdef __arm__ 7 #define __NR_sched_setattr 380 8 #define __NR_sched_getattr 381 9 #endif 0 1 static volatile int done; 2 3 struct sched_attr { 4 __u32 size; 5 6 __u32 sched_policy; 7 __u64 sched_flags; 8 9 /* SCHED_NORMAL, SCHED_BATCH */ 0 __s32 sched_nice; 1 2 /* SCHED_FIFO, SCHED_RR */ 3 __u32 sched_priority; 4 5 /* SCHED_DEADLINE (nsec) */ 6 __u64 sched_runtime; 7 __u64 sched_deadline; 8 __u64 sched_period; 9 }; 0 1 int sched_setattr(pid_t pid, 2 const struct sched_attr *attr, 3 unsigned int flags) 4 { 5 return syscall(__NR_sched_setattr, 6 pid, attr, flags); 7 } 8 9 int sched_getattr(pid_t pid, 0 struct sched_attr *attr, 1 unsigned int size, 2 unsigned int flags) 3 { 4 return syscall(__NR_sched_getattr, 5 pid, attr, size, flags); 6 } 7 8 void *run_deadline(void *data) 9 { 0 struct sched_attr attr; 1 int x = 0, ret; 2 unsigned int flags = 0; 3 4 printf("deadline thread start %ld\n", 5 gettid()); 6 7 attr.size = sizeof(attr); 8 attr.sched_flags = 0; 9 attr.sched_nice = 0; 0 attr.sched_priority = 0; 1 2 /* creates a 10ms/30ms reservation */ 3 attr.sched_policy = SCHED_DEADLINE; 4 attr.sched_runtime = 10 * 1000 * 1000; 5 attr.sched_period = 30 * 1000 * 1000; 6 attr.sched_deadline= 30 * 1000 * 1000; 7 8 ret = sched_setattr(0, &attr, flags); 9 if (ret < 0) { 0 done = 0; 1 perror("sched_setattr"); 2 exit(-1); 3 } 4 5 while (!done) { 6 x++; 7 } 8 return NULL; 9 } 0 1 int main (int argc, char **argv) 2 { 3 pthread_t thread; 4 5 printf("main thread [%ld]\n", gettid()); 6 pthread_create(&thread, NULL, run_deadline, NULL); 7 sleep(10); 8 done = 1; 9 pthread_join(thread, NULL); 0 printf("main dies [%ld]\n", gettid()); 1 return 0; 2 }