From: Juri Lelli <juri.lelli@arm.com>
To: peterz@infradead.org
Cc: luca.abeni@unitn.it, rdunlap@infradead.org, mingo@redhat.com,
henrik@austad.us, raistlin@linux.it, juri.lelli@gmail.com,
juri.lelli@arm.com, linux-doc@vger.kernel.org,
linux-kernel@vger.kernel.org
Subject: [PATCH v4 5/5] Documentation/scheduler/sched-deadline.txt: add minimal main() appendix
Date: Tue, 9 Sep 2014 10:57:16 +0100 [thread overview]
Message-ID: <1410256636-26171-6-git-send-email-juri.lelli@arm.com> (raw)
In-Reply-To: <1410256636-26171-1-git-send-email-juri.lelli@arm.com>
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>
Cc: Randy Dunlap <rdunlap@infradead.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Henrik Austad <henrik@austad.us>
Cc: Dario Faggioli <raistlin@linux.it>
Cc: Juri Lelli <juri.lelli@gmail.com>
Cc: linux-doc@vger.kernel.org
Cc: linux-kernel@vger.kernel.org
---
Documentation/scheduler/sched-deadline.txt | 126 +++++++++++++++++++++++++++++
1 file changed, 126 insertions(+)
diff --git a/Documentation/scheduler/sched-deadline.txt b/Documentation/scheduler/sched-deadline.txt
index b4aad31..cd8274c 100644
--- a/Documentation/scheduler/sched-deadline.txt
+++ b/Documentation/scheduler/sched-deadline.txt
@@ -16,6 +16,7 @@ CONTENTS
5.1 SCHED_DEADLINE and cpusets HOWTO
6. Future plans
A. Test suite
+ B. Minimal main()
0. WARNING
@@ -397,3 +398,128 @@ Appendix A. Test suite
application, given that you know its pid:
# schedtool -E -t 10000000:100000000 my_app_pid
+
+Appendix B. Minimal main()
+==========================
+
+ We provide in what follows a simple (ugly) self-contained code snippet
+ showing how SCHED_DEADLINE reservations can be created by a real-time
+ application developer.
+
+ #define _GNU_SOURCE
+ #include <unistd.h>
+ #include <stdio.h>
+ #include <stdlib.h>
+ #include <string.h>
+ #include <time.h>
+ #include <linux/unistd.h>
+ #include <linux/kernel.h>
+ #include <linux/types.h>
+ #include <sys/syscall.h>
+ #include <pthread.h>
+
+ #define gettid() syscall(__NR_gettid)
+
+ #define SCHED_DEADLINE 6
+
+ /* XXX use the proper syscall numbers */
+ #ifdef __x86_64__
+ #define __NR_sched_setattr 314
+ #define __NR_sched_getattr 315
+ #endif
+
+ #ifdef __i386__
+ #define __NR_sched_setattr 351
+ #define __NR_sched_getattr 352
+ #endif
+
+ #ifdef __arm__
+ #define __NR_sched_setattr 380
+ #define __NR_sched_getattr 381
+ #endif
+
+ static volatile int done;
+
+ struct sched_attr {
+ __u32 size;
+
+ __u32 sched_policy;
+ __u64 sched_flags;
+
+ /* SCHED_NORMAL, SCHED_BATCH */
+ __s32 sched_nice;
+
+ /* SCHED_FIFO, SCHED_RR */
+ __u32 sched_priority;
+
+ /* SCHED_DEADLINE (nsec) */
+ __u64 sched_runtime;
+ __u64 sched_deadline;
+ __u64 sched_period;
+ };
+
+ int sched_setattr(pid_t pid,
+ const struct sched_attr *attr,
+ unsigned int flags)
+ {
+ return syscall(__NR_sched_setattr, pid, attr, flags);
+ }
+
+ int sched_getattr(pid_t pid,
+ struct sched_attr *attr,
+ unsigned int size,
+ unsigned int flags)
+ {
+ return syscall(__NR_sched_getattr, pid, attr, size, flags);
+ }
+
+ void *run_deadline(void *data)
+ {
+ struct sched_attr attr;
+ int x = 0;
+ int ret;
+ unsigned int flags = 0;
+
+ printf("deadline thread started [%ld]\n", gettid());
+
+ attr.size = sizeof(attr);
+ attr.sched_flags = 0;
+ attr.sched_nice = 0;
+ attr.sched_priority = 0;
+
+ /* This creates a 10ms/30ms reservation */
+ attr.sched_policy = SCHED_DEADLINE;
+ attr.sched_runtime = 10 * 1000 * 1000;
+ attr.sched_period = attr.sched_deadline = 30 * 1000 * 1000;
+
+ ret = sched_setattr(0, &attr, flags);
+ if (ret < 0) {
+ done = 0;
+ perror("sched_setattr");
+ exit(-1);
+ }
+
+ while (!done) {
+ x++;
+ }
+
+ printf("deadline thread dies [%ld]\n", gettid());
+ return NULL;
+ }
+
+ int main (int argc, char **argv)
+ {
+ pthread_t thread;
+
+ printf("main thread [%ld]\n", gettid());
+
+ pthread_create(&thread, NULL, run_deadline, NULL);
+
+ sleep(10);
+
+ done = 1;
+ pthread_join(thread, NULL);
+
+ printf("main dies [%ld]\n", gettid());
+ return 0;
+ }
--
2.0.4
next prev parent reply other threads:[~2014-09-09 9:59 UTC|newest]
Thread overview: 14+ messages / expand[flat|nested] mbox.gz Atom feed top
2014-09-09 9:57 [PATCH v4 0/5] SCHED_DEADLINE documentation fixes and improvements Juri Lelli
2014-09-09 9:57 ` [PATCH v4 1/5] Documentation/scheduler/sched-deadline.txt: fix terminology and improve clarity Juri Lelli
2014-09-16 10:56 ` [tip:sched/core] Documentation/scheduler/sched-deadline.txt: Fix " tip-bot for Luca Abeni
2014-09-09 9:57 ` [PATCH v4 2/5] Documentation/scheduler/sched-deadline.txt: Rewrite section 4 intro Juri Lelli
2014-09-16 10:57 ` [tip:sched/core] " tip-bot for Juri Lelli
2014-09-09 9:57 ` [PATCH v4 3/5] Documentation/scheduler/sched-deadline.txt: improve and clarify AC bits Juri Lelli
2014-09-16 10:57 ` [tip:sched/core] Documentation/scheduler/sched-deadline.txt: Improve " tip-bot for Luca Abeni
2014-09-09 9:57 ` [PATCH v4 4/5] Documentation/scheduler/sched-deadline.txt: add tests suite appendix Juri Lelli
2014-09-16 10:57 ` [tip:sched/core] Documentation/scheduler/sched-deadline.txt: Add " tip-bot for Juri Lelli
2014-09-09 9:57 ` Juri Lelli [this message]
2014-09-16 10:57 ` [tip:sched/core] Documentation/scheduler/sched-deadline.txt: Add minimal main() appendix tip-bot for Juri Lelli
2014-09-09 21:17 ` [PATCH v4 0/5] SCHED_DEADLINE documentation fixes and improvements Henrik Austad
2014-09-10 8:03 ` Juri Lelli
2014-09-10 8:34 ` Peter Zijlstra
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1410256636-26171-6-git-send-email-juri.lelli@arm.com \
--to=juri.lelli@arm.com \
--cc=henrik@austad.us \
--cc=juri.lelli@gmail.com \
--cc=linux-doc@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=luca.abeni@unitn.it \
--cc=mingo@redhat.com \
--cc=peterz@infradead.org \
--cc=raistlin@linux.it \
--cc=rdunlap@infradead.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).