diff options
author | Juri Lelli <juri.lelli@arm.com> | 2014-09-09 05:57:16 -0400 |
---|---|---|
committer | Ingo Molnar <mingo@kernel.org> | 2014-09-16 04:23:45 -0400 |
commit | 13924d2a983fc1557eb737ea59e2324adb538fa2 (patch) | |
tree | 314f9518880ce2901572b89d8fa1be2895e2998e | |
parent | f5801933ce595ba6eb77d170ab0dfbcd5c894e11 (diff) |
Documentation/scheduler/sched-deadline.txt: Add minimal main() appendix
Add an appendix providing a simple self-contained code snippet
showing how SCHED_DEADLINE reservations can be created by
application developers.
Signed-off-by: Juri Lelli <juri.lelli@arm.com>
Reviewed-by: Henrik Austad <henrik@austad.us>
Cc: Randy Dunlap <rdunlap@infradead.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Dario Faggioli <raistlin@linux.it>
Cc: Juri Lelli <juri.lelli@gmail.com>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Link: http://lkml.kernel.org/r/1410256636-26171-6-git-send-email-juri.lelli@arm.com
[ Fixed some whitespace inconsistencies. ]
Signed-off-by: Ingo Molnar <mingo@kernel.org>
-rw-r--r-- | Documentation/scheduler/sched-deadline.txt | 126 |
1 files changed, 126 insertions, 0 deletions
diff --git a/Documentation/scheduler/sched-deadline.txt b/Documentation/scheduler/sched-deadline.txt index b4aad31cf3be..21461a0441c1 100644 --- a/Documentation/scheduler/sched-deadline.txt +++ b/Documentation/scheduler/sched-deadline.txt | |||
@@ -16,6 +16,7 @@ CONTENTS | |||
16 | 5.1 SCHED_DEADLINE and cpusets HOWTO | 16 | 5.1 SCHED_DEADLINE and cpusets HOWTO |
17 | 6. Future plans | 17 | 6. Future plans |
18 | A. Test suite | 18 | A. Test suite |
19 | B. Minimal main() | ||
19 | 20 | ||
20 | 21 | ||
21 | 0. WARNING | 22 | 0. WARNING |
@@ -397,3 +398,128 @@ Appendix A. Test suite | |||
397 | application, given that you know its pid: | 398 | application, given that you know its pid: |
398 | 399 | ||
399 | # schedtool -E -t 10000000:100000000 my_app_pid | 400 | # schedtool -E -t 10000000:100000000 my_app_pid |
401 | |||
402 | Appendix B. Minimal main() | ||
403 | ========================== | ||
404 | |||
405 | We provide in what follows a simple (ugly) self-contained code snippet | ||
406 | showing how SCHED_DEADLINE reservations can be created by a real-time | ||
407 | application developer. | ||
408 | |||
409 | #define _GNU_SOURCE | ||
410 | #include <unistd.h> | ||
411 | #include <stdio.h> | ||
412 | #include <stdlib.h> | ||
413 | #include <string.h> | ||
414 | #include <time.h> | ||
415 | #include <linux/unistd.h> | ||
416 | #include <linux/kernel.h> | ||
417 | #include <linux/types.h> | ||
418 | #include <sys/syscall.h> | ||
419 | #include <pthread.h> | ||
420 | |||
421 | #define gettid() syscall(__NR_gettid) | ||
422 | |||
423 | #define SCHED_DEADLINE 6 | ||
424 | |||
425 | /* XXX use the proper syscall numbers */ | ||
426 | #ifdef __x86_64__ | ||
427 | #define __NR_sched_setattr 314 | ||
428 | #define __NR_sched_getattr 315 | ||
429 | #endif | ||
430 | |||
431 | #ifdef __i386__ | ||
432 | #define __NR_sched_setattr 351 | ||
433 | #define __NR_sched_getattr 352 | ||
434 | #endif | ||
435 | |||
436 | #ifdef __arm__ | ||
437 | #define __NR_sched_setattr 380 | ||
438 | #define __NR_sched_getattr 381 | ||
439 | #endif | ||
440 | |||
441 | static volatile int done; | ||
442 | |||
443 | struct sched_attr { | ||
444 | __u32 size; | ||
445 | |||
446 | __u32 sched_policy; | ||
447 | __u64 sched_flags; | ||
448 | |||
449 | /* SCHED_NORMAL, SCHED_BATCH */ | ||
450 | __s32 sched_nice; | ||
451 | |||
452 | /* SCHED_FIFO, SCHED_RR */ | ||
453 | __u32 sched_priority; | ||
454 | |||
455 | /* SCHED_DEADLINE (nsec) */ | ||
456 | __u64 sched_runtime; | ||
457 | __u64 sched_deadline; | ||
458 | __u64 sched_period; | ||
459 | }; | ||
460 | |||
461 | int sched_setattr(pid_t pid, | ||
462 | const struct sched_attr *attr, | ||
463 | unsigned int flags) | ||
464 | { | ||
465 | return syscall(__NR_sched_setattr, pid, attr, flags); | ||
466 | } | ||
467 | |||
468 | int sched_getattr(pid_t pid, | ||
469 | struct sched_attr *attr, | ||
470 | unsigned int size, | ||
471 | unsigned int flags) | ||
472 | { | ||
473 | return syscall(__NR_sched_getattr, pid, attr, size, flags); | ||
474 | } | ||
475 | |||
476 | void *run_deadline(void *data) | ||
477 | { | ||
478 | struct sched_attr attr; | ||
479 | int x = 0; | ||
480 | int ret; | ||
481 | unsigned int flags = 0; | ||
482 | |||
483 | printf("deadline thread started [%ld]\n", gettid()); | ||
484 | |||
485 | attr.size = sizeof(attr); | ||
486 | attr.sched_flags = 0; | ||
487 | attr.sched_nice = 0; | ||
488 | attr.sched_priority = 0; | ||
489 | |||
490 | /* This creates a 10ms/30ms reservation */ | ||
491 | attr.sched_policy = SCHED_DEADLINE; | ||
492 | attr.sched_runtime = 10 * 1000 * 1000; | ||
493 | attr.sched_period = attr.sched_deadline = 30 * 1000 * 1000; | ||
494 | |||
495 | ret = sched_setattr(0, &attr, flags); | ||
496 | if (ret < 0) { | ||
497 | done = 0; | ||
498 | perror("sched_setattr"); | ||
499 | exit(-1); | ||
500 | } | ||
501 | |||
502 | while (!done) { | ||
503 | x++; | ||
504 | } | ||
505 | |||
506 | printf("deadline thread dies [%ld]\n", gettid()); | ||
507 | return NULL; | ||
508 | } | ||
509 | |||
510 | int main (int argc, char **argv) | ||
511 | { | ||
512 | pthread_t thread; | ||
513 | |||
514 | printf("main thread [%ld]\n", gettid()); | ||
515 | |||
516 | pthread_create(&thread, NULL, run_deadline, NULL); | ||
517 | |||
518 | sleep(10); | ||
519 | |||
520 | done = 1; | ||
521 | pthread_join(thread, NULL); | ||
522 | |||
523 | printf("main dies [%ld]\n", gettid()); | ||
524 | return 0; | ||
525 | } | ||