]> rtime.felk.cvut.cz Git - frescor/fwp.git/blob - fwp/mngr/gui.c
Small fixes to get frsh distributed test working again.
[frescor/fwp.git] / fwp / mngr / gui.c
1 #include "gui.h"
2 #include "fwp_foreach.h"
3 #include <ncurses.h>
4 #include <fwp_contract.h>
5 #include "fwp_admctrl.h"
6
7 void gui_init(void)
8 {
9         initscr();
10         cbreak();
11         noecho();
12 }
13
14 static char *num_with_unit(char *buf, unsigned num, const char *units[])
15 {
16         const int div[] = {100,10,1};
17         unsigned order = 0;
18         int d;
19
20         while (num > 999 && units[(order+3)/3] != NULL) {
21                 num /= 10;
22                 order++;
23         }
24
25         d = div[(order+2)%3];
26         if (d == 1)
27                 sprintf(buf, "%4d %s", num, units[(order+2)/3]);
28         else
29                 sprintf(buf, "%d.%.*d %s", num/d, (d==10) ? 1 : 2, num%d, units[(order+2)/3]);
30         return buf;
31 }
32
33 char *bandwidth_to_text(char *buf, unsigned bandwidth_bps)
34 {
35         const char *unit[] = {"bps", "kbps", "Mbps", NULL};
36         return num_with_unit(buf, bandwidth_bps, unit);
37 }
38
39 char *usec_to_text(char *buf, unsigned usec)
40 {
41         const char *unit[] = {"us", "ms", "s", NULL};
42         return num_with_unit(buf, usec, unit);
43 }
44
45 #define addfield(width, title, format, ...)                             \
46         move(y, x);                                                     \
47         x+=width;                                                       \
48         if (cd == NULL) addstr(title);                                  \
49         else {                                                          \
50                 snprintf(str, sizeof(str), format, __VA_ARGS__);        \
51                 addstr(str);                                            \
52         }
53
54 static void print_contract(int y, fwp_contract_data_t *cd)
55 {
56         const char *ac_ids[] = { [FWP_AC_VO]="VO", [FWP_AC_VI]="VI", [FWP_AC_BE]="BE", [FWP_AC_BK]="BK" };
57         struct fwp_contract *c = &cd->contract;
58         char str[200], s1[20];
59         int x = 0;
60         
61         addfield(4,  "ID", "%d", cd->id);
62         addfield(11, "BW", "%s", bandwidth_to_text(s1, (long long)1000*1000*8*c->budget/c->period_usec));
63         addfield(11, "Budget", "%d bytes", c->budget);
64         addfield(11, "Period", "%s", usec_to_text(s1, c->period_usec));
65         addfield(11, "Deadline", "%s", usec_to_text(s1, c->deadline_usec));
66         addfield(3,  "AC", "%s", ac_ids[cd->vres_params.ac_id]);
67         addfield(11, "Num partics", "%d", fwp_participant_table_nr_participants());
68 }
69
70 void gui_print_status()
71 {
72         fwp_contract_data_t *cd;
73         struct foreach_contract fec;
74         int y;
75         char str[200];
76
77         clear();                /* Repaint the screen completely to
78                                  * delete error messages */
79         mvaddstr(0, 0, "FWP Manager");
80         sprintf(str, "Reserved utilization: %d%%", fwp_reserved_utilization/100);
81         mvaddstr(2, 0, str);
82
83         /* Print header */
84         y=4;
85         print_contract(y, NULL);
86         y++;
87
88         /* Print contracts */
89         fec.contdata_new = NULL;
90         for (cd=foreach_contract_begin(&fec);
91              cd;
92              cd=foreach_contract_next(&fec)) {
93                 print_contract(y, cd);
94                 y++;
95         }
96         refresh();
97 }
98
99 void gui_end(void)
100 {
101         endwin();
102 }