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 <sched.h>
26 #include <errno.h>
27
28 #include "test.h"
29 #include "lapi/syscalls.h"
30 #include "lapi/sched.h"
31
32 char *TCID = "sched_getattr01";
33 int TST_TOTAL = 1;
34
35 #define SCHED_DEADLINE 6
36 #define RUNTIME_VAL 10000000
37 #define PERIOD_VAL 30000000
38 #define DEADLINE_VAL 30000000
39
run_deadline(void * data LTP_ATTRIBUTE_UNUSED)40 void *run_deadline(void *data LTP_ATTRIBUTE_UNUSED)
41 {
42 struct sched_attr attr, attr_copy;
43 int ret;
44 unsigned int flags = 0;
45 unsigned int size;
46
47 attr.size = sizeof(attr);
48 attr.sched_flags = 0;
49 attr.sched_nice = 0;
50 attr.sched_priority = 0;
51
52 /* This creates a 10ms/30ms reservation */
53 attr.sched_policy = SCHED_DEADLINE;
54 attr.sched_runtime = RUNTIME_VAL;
55 attr.sched_period = PERIOD_VAL;
56 attr.sched_deadline = DEADLINE_VAL;
57
58 ret = sched_setattr(0, &attr, flags);
59 if (ret < 0)
60 tst_brkm(TFAIL | TERRNO, NULL, "sched_setattr() failed");
61
62 size = sizeof(attr_copy);
63 ret = sched_getattr(0, &attr_copy, size, flags);
64 if (ret < 0)
65 tst_brkm(TFAIL | TERRNO, NULL, "sched_getattr() failed");
66
67 int fail = 0;
68
69 if (attr_copy.sched_runtime != RUNTIME_VAL) {
70 tst_resm(TINFO, "sched_runtime is incorrect (%"PRIu64"),"
71 " expected %u", attr.sched_runtime, RUNTIME_VAL);
72 fail++;
73 }
74 if (attr_copy.sched_period != PERIOD_VAL) {
75 tst_resm(TINFO, "sched_period is incorrect (%"PRIu64"),"
76 " expected %u", attr.sched_period, PERIOD_VAL);
77 fail++;
78 }
79 if (attr_copy.sched_deadline != DEADLINE_VAL) {
80 tst_resm(TINFO, "sched_deadline is incorrect (%"PRIu64"),"
81 " expected %u", attr.sched_deadline, DEADLINE_VAL);
82 fail++;
83 }
84
85 if (fail)
86 tst_resm(TFAIL, "attributes were read back incorrectly");
87 else
88 tst_resm(TPASS, "attributes were read back correctly");
89
90 return NULL;
91 }
92
main(int argc,char ** argv)93 int main(int argc, char **argv)
94 {
95 pthread_t thread;
96 int lc;
97
98 tst_parse_opts(argc, argv, NULL, NULL);
99
100 tst_require_root();
101
102 if ((tst_kvercmp(3, 14, 0)) < 0)
103 tst_brkm(TCONF, NULL, "EDF needs kernel 3.14 or higher");
104
105 for (lc = 0; TEST_LOOPING(lc); lc++) {
106 pthread_create(&thread, NULL, run_deadline, NULL);
107 pthread_join(thread, NULL);
108 }
109
110 tst_exit();
111 }
112