]> rtime.felk.cvut.cz Git - frescor/frsh.git/blob - frsh_api/tests/cpu_spare_capacity.c
Fixed printf warnings on 64-bit arch
[frescor/frsh.git] / frsh_api / tests / cpu_spare_capacity.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_spare_capacity [ 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 = 2,
58         .utilizations = {
59                 { .budget = MSEC(20),  .period = MSEC(100),  .deadline = MSEC(100) },
60                 { .budget = MSEC(50),  .period = MSEC(100),  .deadline = MSEC(100) },
61         },
62 };
63
64 int main(int argc, char *argv[])
65 {
66         frsh_vres_id_t vres;
67         frsh_thread_attr_t attr;
68         frsh_thread_id_t thread;
69         frsh_contract_t contract;
70         frsh_rel_time_t zero = fosa_msec_to_rel_time(0);
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         ret = frsh_contract_set_basic_params(&contract,
97                                              &utilization_set.utilizations[0].budget,
98                                              &utilization_set.utilizations[0].period,
99                                              FRSH_WT_BOUNDED,
100                                              FRSH_CT_REGULAR);
101         if (ret) PERROR_AND_EXIT(ret, "frsh_contract_set_basic_params");
102
103         ret = frsh_contract_set_resource_and_label(&contract, FRSH_RT_PROCESSOR,
104                                                    FRSH_CPU_ID_DEFAULT, "spare cap test");
105         if (ret) PERROR_AND_EXIT(ret, "frsh_contract_set_resource_and_label");
106
107         ret = frsh_contract_set_reclamation_params(&contract,
108                                                    &zero,
109                                                    &utilization_set.utilizations[utilization_set.size-1].budget,
110                                                    &utilization_set.utilizations[utilization_set.size-1].period,
111                                                    FRSH_GR_DISCRETE,
112                                                    &utilization_set,
113                                                    0,
114                                                    0);
115         if (ret) PERROR_AND_EXIT(ret, "frsh_contract_set_reclamation_params");
116
117         ret = frsh_contract_negotiate(&contract, &vres);
118         if (ret) PERROR_AND_EXIT(ret, "frsh_contract_negotiate");
119
120         printf("Aqcpu vres negotiated, vres-ID: %p\n", vres);
121         pthread_attr_init(&attr);
122         ret = frsh_thread_create_and_bind(vres, &thread, &attr, 
123                                 work_thread, (void*) NULL);
124         if (ret) PERROR_AND_EXIT(ret, "frsh_thread_create_and_bind");
125         
126         pthread_join(thread.pthread_id, (void**) NULL); 
127
128         ret = frsh_contract_cancel(vres);
129         if (ret) PERROR_AND_EXIT(ret, "frsh_contract_cancel");
130
131         return 0;       
132 }
133