]> rtime.felk.cvut.cz Git - frescor/frsh.git/blob - frsh_api/tests/dummy_spare_capacity.c
Added renegotiation and spare capacity demos for dummy resource
[frescor/frsh.git] / frsh_api / tests / dummy_spare_capacity.c
1 #include <frsh.h>
2 #include <error.h>
3 #include <signal.h>
4 #include <getopt.h>
5 #include <ul_logreg.h>
6 #include <res_dummy.h>
7 #include <semaphore.h>
8
9 sem_t finish;
10
11 void int_handler(int signal)
12 {
13         sem_post(&finish);
14 }
15
16 static struct option long_opts[] = {
17     { "loglevel", 1, 0, 'l' },
18     { 0, 0, 0, 0}
19 };
20
21 static void
22 usage(void)
23 {
24         printf("usage: cpu_spare_capacity [ options ]\n");
25         printf("  -l, --loglevel <number>|<domain>=<number>,...\n");
26 }
27
28
29 #define MSEC(x) { x/1000, (x%1000) * 1000000 }
30 frsh_utilization_set_t utilization_set = {
31         .size = 2,
32         .utilizations = {
33                 { .budget = MSEC(20),  .period = MSEC(100),  .deadline = MSEC(100) },
34                 { .budget = MSEC(50),  .period = MSEC(100),  .deadline = MSEC(100) },
35         },
36 };
37
38 int main(int argc, char *argv[])
39 {
40         frsh_vres_id_t vres;
41         frsh_contract_t contract;
42         frsh_rel_time_t zero = fosa_msec_to_rel_time(0);
43         int ret;
44         char opt;
45
46         sem_init(&finish, 0, 0);
47
48         while ((opt = getopt_long(argc, argv, "l:", &long_opts[0], NULL)) != EOF) {
49                 switch (opt) {
50                         case 'l':
51                                 ul_log_domain_arg2levels(optarg);
52                                 break;
53                         case 'h':
54                         /*default:*/
55                                 usage();
56                                 exit(opt == 'h' ? 0 : 1);
57                 }
58         }
59
60         ret = frsh_init();
61         if (ret) PERROR_AND_EXIT(ret, "frsh_init");
62         
63         signal(SIGINT, int_handler);
64         signal(SIGTERM, int_handler);
65
66         /* Contract negotiation for CPU */
67         ret = frsh_contract_init(&contract);
68         if (ret) PERROR_AND_EXIT(ret, "frsh_contract_init");
69                 
70         ret = frsh_contract_set_basic_params(&contract,
71                                              &utilization_set.utilizations[0].budget,
72                                              &utilization_set.utilizations[0].period,
73                                              FRSH_WT_BOUNDED,
74                                              FRSH_CT_REGULAR);
75         if (ret) PERROR_AND_EXIT(ret, "frsh_contract_set_basic_params");
76
77         ret = frsh_contract_set_resource_and_label(&contract ,
78                                                    DUMMY_RESOURCE_TYPE, DUMMY_RESOURCE_ID,
79                                                    "spare cap test");
80         if (ret) PERROR_AND_EXIT(ret, "frsh_contract_set_resource_and_label");
81
82         ret = frsh_contract_set_reclamation_params(&contract,
83                                                    &zero,
84                                                    &utilization_set.utilizations[utilization_set.size-1].budget,
85                                                    &utilization_set.utilizations[utilization_set.size-1].period,
86                                                    FRSH_GR_DISCRETE,
87                                                    &utilization_set,
88                                                    0,
89                                                    0);
90         if (ret) PERROR_AND_EXIT(ret, "frsh_contract_set_reclamation_params");
91
92         ret = frsh_contract_negotiate(&contract, &vres);
93         if (ret) PERROR_AND_EXIT(ret, "frsh_contract_negotiate");
94
95         sem_wait(&finish);
96
97         ret = frsh_contract_cancel(vres);
98         if (ret) PERROR_AND_EXIT(ret, "frsh_contract_cancel");
99
100         return 0;       
101 }
102