]> rtime.felk.cvut.cz Git - frescor/frsh.git/blob - frsh_api/tests/cpu_renegotiation.c
5e7d11fd84bed9885d25a3085b5749081c7ff51b
[frescor/frsh.git] / frsh_api / tests / cpu_renegotiation.c
1 #include <frsh.h>
2 #include <aqcpu_res.h>
3 #include <error.h>
4 #include <signal.h>
5 #include <getopt.h>
6 #include <ul_logreg.h>
7
8 volatile int finish = 0;
9
10 void int_handler(int signal)
11 {
12         finish = 1;
13 }
14
15 void* work_thread()
16 {
17         int ret;
18         unsigned long i = 0;
19         volatile int j;
20         frsh_vres_id_t vres_id;
21         frsh_rel_time_t budget;
22         frsh_rel_time_t period;
23
24         while(!finish) {
25                 i++;
26                 
27                 for (j=0; j<1000000; j++);
28                 
29                 ret = frsh_thread_get_vres_id(fosa_thread_self(), &vres_id);
30                 if (ret) PERROR_AND_EXIT(ret, "frsh_get_vres_id");
31
32                 ret = frsh_vres_get_budget_and_period(vres_id, &budget, &period);
33                 if (ret) PERROR_AND_EXIT(ret, "frsh_vres_get_budget_and_period");
34
35                 printf("Consuming budget %ld ms - %lu\n",
36                        fosa_rel_time_to_msec(budget), i);
37         }
38
39         return EXIT_SUCCESS;
40 }
41
42 static struct option long_opts[] = {
43     { "loglevel", 1, 0, 'l' },
44     { 0, 0, 0, 0}
45 };
46
47 static void
48 usage(void)
49 {
50         printf("usage: cpu_renegotiation [ options ]\n");
51         printf("  -l, --loglevel <number>|<domain>=<number>,...\n");
52 }
53
54
55 #define MSEC(x) { x/1000, (x%1000) * 1000000 }
56 frsh_utilization_set_t utilization_set = {
57         .size = 3,
58         .utilizations = {
59                 { .budget = MSEC(10),  .period = MSEC(100),  .deadline = MSEC(100) },
60                 { .budget = MSEC(20),  .period = MSEC(100),  .deadline = MSEC(100) },
61                 { .budget = MSEC(30),  .period = MSEC(100),  .deadline = MSEC(100) },
62         },
63 };
64
65 int main(int argc, char *argv[])
66 {
67         frsh_vres_id_t vres;
68         frsh_thread_attr_t attr;
69         frsh_thread_id_t thread;
70         frsh_contract_t contract;
71         int ret;
72         char opt;
73
74         while ((opt = getopt_long(argc, argv, "l:", &long_opts[0], NULL)) != EOF) {
75                 switch (opt) {
76                         case 'l':
77                                 ul_log_domain_arg2levels(optarg);
78                                 break;
79                         case 'h':
80                         /*default:*/
81                                 usage();
82                                 exit(opt == 'h' ? 0 : 1);
83                 }
84         }
85
86         ret = frsh_init();
87         if (ret) PERROR_AND_EXIT(ret, "frsh_init");
88         
89         signal(SIGINT, int_handler);
90         signal(SIGTERM, int_handler);
91
92         /* Contract negotiation for CPU */
93         ret = frsh_contract_init(&contract);
94         if (ret) PERROR_AND_EXIT(ret, "frsh_contract_init");
95
96         int variant = 0;
97         bool first = true;
98         while (!finish) {
99                 
100                 ret = frsh_contract_set_basic_params(&contract,
101                                                      &utilization_set.utilizations[variant].budget,
102                                                      &utilization_set.utilizations[variant].period,
103                                                      FRSH_WT_BOUNDED,
104                                                      FRSH_CT_REGULAR);
105                 if (ret) PERROR_AND_EXIT(ret, "frsh_contract_set_basic_params");
106
107                 ret = frsh_contract_set_resource_and_label(&contract, FRSH_RT_PROCESSOR,
108                                                            FRSH_CPU_ID_DEFAULT, "renegotiation test");
109                 if (ret) PERROR_AND_EXIT(ret, "frsh_contract_set_resource_and_label");
110
111                 if (first) {
112                         ret = frsh_contract_negotiate(&contract, &vres);
113                         if (ret) PERROR_AND_EXIT(ret, "frsh_contract_negotiate");
114
115                         printf("Aqcpu vres negotiated, vres-ID: %d\n", (int) vres);
116                         pthread_attr_init(&attr);
117                         ret = frsh_thread_create_and_bind(vres, &thread, &attr, 
118                                                           work_thread, (void*) NULL);
119                         if (ret) PERROR_AND_EXIT(ret, "frsh_thread_create_and_bind");
120                         first = false;
121                 } else {
122                         ret = frsh_contract_renegotiate_sync(&contract, vres);
123                         if (ret == 0) {
124                                 printf("Renegotiation accepted\n");
125                         } else if (ret == FRSH_ERR_CONTRACT_REJECTED) {
126                                 printf("Renegotiaton REJECTED\n");
127                         } else
128                                 PERROR_AND_EXIT(ret, "frsh_contract_renegotiate_sync");
129                 }
130                 sleep(5);
131                 variant++;
132                 if (variant >= utilization_set.size) variant = 0;
133         }
134         
135         pthread_join(thread.pthread_id, (void**) NULL); 
136
137         ret = frsh_contract_cancel(vres);
138         if (ret) PERROR_AND_EXIT(ret, "frsh_contract_cancel");
139
140         return 0;       
141 }
142