1 /*
2 * Copyright (c) Huawei Technologies Co., Ltd., 2015
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
11 * the GNU General Public License for more details.
12 */
13
14 #define _GNU_SOURCE
15 #include <unistd.h>
16 #include <stdio.h>
17 #include <stdlib.h>
18 #include <string.h>
19 #include <time.h>
20 #include <linux/unistd.h>
21 #include <linux/kernel.h>
22 #include <linux/types.h>
23 #include <sys/syscall.h>
24 #include <pthread.h>
25 #include <errno.h>
26
27 #include "test.h"
28 #include "lapi/sched.h"
29
30 char *TCID = "sched_getattr01";
31 int TST_TOTAL = 1;
32
33 #define RUNTIME_VAL 10000000
34 #define PERIOD_VAL 30000000
35 #define DEADLINE_VAL 30000000
36
run_deadline(void * data LTP_ATTRIBUTE_UNUSED)37 void *run_deadline(void *data LTP_ATTRIBUTE_UNUSED)
38 {
39 struct sched_attr attr, attr_copy;
40 int ret;
41 unsigned int flags = 0;
42 unsigned int size;
43
44 attr.size = sizeof(attr);
45 attr.sched_flags = 0;
46 attr.sched_nice = 0;
47 attr.sched_priority = 0;
48
49 /* This creates a 10ms/30ms reservation */
50 attr.sched_policy = SCHED_DEADLINE;
51 attr.sched_runtime = RUNTIME_VAL;
52 attr.sched_period = PERIOD_VAL;
53 attr.sched_deadline = DEADLINE_VAL;
54
55 ret = sched_setattr(0, &attr, flags);
56 if (ret < 0)
57 tst_brkm(TFAIL | TERRNO, NULL, "sched_setattr() failed");
58
59 size = sizeof(attr_copy);
60 ret = sched_getattr(0, &attr_copy, size, flags);
61 if (ret < 0)
62 tst_brkm(TFAIL | TERRNO, NULL, "sched_getattr() failed");
63
64 int fail = 0;
65
66 if (attr_copy.sched_runtime != RUNTIME_VAL) {
67 tst_resm(TINFO, "sched_runtime is incorrect (%"PRIu64"),"
68 " expected %u", attr.sched_runtime, RUNTIME_VAL);
69 fail++;
70 }
71 if (attr_copy.sched_period != PERIOD_VAL) {
72 tst_resm(TINFO, "sched_period is incorrect (%"PRIu64"),"
73 " expected %u", attr.sched_period, PERIOD_VAL);
74 fail++;
75 }
76 if (attr_copy.sched_deadline != DEADLINE_VAL) {
77 tst_resm(TINFO, "sched_deadline is incorrect (%"PRIu64"),"
78 " expected %u", attr.sched_deadline, DEADLINE_VAL);
79 fail++;
80 }
81
82 if (fail)
83 tst_resm(TFAIL, "attributes were read back incorrectly");
84 else
85 tst_resm(TPASS, "attributes were read back correctly");
86
87 return NULL;
88 }
89
main(int argc,char ** argv)90 int main(int argc, char **argv)
91 {
92 pthread_t thread;
93 int lc;
94
95 tst_parse_opts(argc, argv, NULL, NULL);
96
97 tst_require_root();
98
99 if ((tst_kvercmp(3, 14, 0)) < 0)
100 tst_brkm(TCONF, NULL, "EDF needs kernel 3.14 or higher");
101
102 for (lc = 0; TEST_LOOPING(lc); lc++) {
103 pthread_create(&thread, NULL, run_deadline, NULL);
104 pthread_join(thread, NULL);
105 }
106
107 tst_exit();
108 }
109