-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathncurses.c
368 lines (325 loc) · 8.68 KB
/
ncurses.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
/*
* Copyright (c) 2019 The DragonFly Project. All rights reserved.
*
* This code is derived from software contributed to The DragonFly Project
* by Matthew Dillon <[email protected]>
*
* This code uses concepts and configuration based on 'synth', by
* John R. Marino <[email protected]>, which was written in ada.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 3. Neither the name of The DragonFly Project nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific, prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include "dsynth.h"
#include <curses.h>
/*
* ncurses - LINES, COLS are the main things we care about
*/
static WINDOW *CWin;
static WINDOW *CMon;
static const char *Line0 = " Total - Built - Ignored - "
"Load - Pkg/hour - ";
static const char *Line1 = " Left - Failed - Skipped - "
"Swap - Impulse - --:--:-- ";
static const char *LineB = "==========================================="
"====================================";
static const char *LineI = " ID Duration Build Phase Origin "
" Lines";
static int LastReduce;
static monitorlog_t nclog;
#define TOTAL_COL 7
#define BUILT_COL 21
#define IGNORED_COL 36
#define LOAD_COL 48
#define GPKGRATE_COL 64
#define REDUCE_COL 71
#define LEFT_COL 7
#define FAILED_COL 21
#define SKIPPED_COL 36
#define SWAP_COL 48
#define IMPULSE_COL 64
#define TIME_COL 71
#define ID_COL 1
#define DURATION_COL 5
#define BUILD_PHASE_COL 15
#define ORIGIN_COL 32
#define LINES_COL 72
/*
* The row that the worker list starts on, and the row that the log starts
* on.
*/
#define WORKER_START 5
#define LOG_START (WORKER_START + MaxWorkers + 1)
static void NCursesReset(void);
static void
NCursesInit(void)
{
if (UseNCurses == 0)
return;
CWin = initscr();
NCursesReset();
intrflush(stdscr, FALSE);
nonl();
noecho();
cbreak();
start_color();
use_default_colors();
init_pair(1, COLOR_RED, -1);
init_pair(2, COLOR_GREEN, -1);
init_pair(3, -1, -1);
}
static void
NCursesReset(void)
{
int i;
if (UseNCurses == 0)
return;
if (CMon) {
delwin(CMon);
CMon = NULL;
}
werase(CWin);
curs_set(0);
redrawwin(CWin);
wrefresh(CWin);
mvwprintw(CWin, 0, 0, "%s", Line0);
mvwprintw(CWin, 1, 0, "%s", Line1);
mvwprintw(CWin, 2, 0, "%s", LineB);
mvwprintw(CWin, 3, 0, "%s", LineI);
mvwprintw(CWin, 4, 0, "%s", LineB);
for (i = 0; i < MaxWorkers; ++i) {
mvwprintw(CWin, WORKER_START + i, ID_COL, "%02d", i);
mvwprintw(CWin, WORKER_START + i, DURATION_COL, "--:--:--");
mvwprintw(CWin, WORKER_START + i, BUILD_PHASE_COL, "Idle");
mvwprintw(CWin, WORKER_START + i, ORIGIN_COL, "%38.38s", "");
mvwprintw(CWin, WORKER_START + i, LINES_COL, "%7.7s", "");
}
mvwprintw(CWin, WORKER_START + MaxWorkers, 0, "%s", LineB);
wrefresh(CWin);
CMon = subwin(CWin, 0, 0, LOG_START, 0);
scrollok(CMon, 1);
bzero(&nclog, sizeof(nclog));
nclog.fd = dlog00_fd();
nodelay(CMon, 1);
LastReduce = -1;
}
static void
NCursesUpdateTop(topinfo_t *info)
{
if (UseNCurses == 0)
return;
mvwprintw(CWin, 0, TOTAL_COL, "%-6d", info->total);
mvwprintw(CWin, 0, BUILT_COL, "%-6d", info->successful);
mvwprintw(CWin, 0, IGNORED_COL, "%-6d", info->ignored);
if (info->dload[0] > 999.9)
mvwprintw(CWin, 0, LOAD_COL, "%5.0f", info->dload[0]);
else
mvwprintw(CWin, 0, LOAD_COL, "%5.1f", info->dload[0]);
mvwprintw(CWin, 0, GPKGRATE_COL, "%-6d", info->pkgrate);
/*
* If dynamic worker reduction is active include a field,
* Otherwise blank the field.
*/
if (LastReduce != info->dynmaxworkers) {
LastReduce = info->dynmaxworkers;
if (MaxWorkers == LastReduce)
mvwprintw(CWin, 0, REDUCE_COL, " ");
else
mvwprintw(CWin, 0, REDUCE_COL, "Lim %-3d",
LastReduce);
}
mvwprintw(CWin, 1, LEFT_COL, "%-6d", info->remaining);
mvwprintw(CWin, 1, FAILED_COL, "%-6d", info->failed);
mvwprintw(CWin, 1, SKIPPED_COL, "%-6d", info->skipped);
if (info->noswap)
mvwprintw(CWin, 1, SWAP_COL, "- ");
else
mvwprintw(CWin, 1, SWAP_COL, "%5.1f", info->dswap);
mvwprintw(CWin, 1, IMPULSE_COL, "%-6d", info->pkgimpulse);
if (info->h > 99)
mvwprintw(CWin, 1, TIME_COL-1, "%3d:%02d:%02d",
info->h, info->m, info->s);
else
mvwprintw(CWin, 1, TIME_COL, "%02d:%02d:%02d",
info->h, info->m, info->s);
}
static void
NCursesUpdateLogs(void)
{
char *ptr;
char c;
ssize_t n;
int w;
if (UseNCurses == 0)
return;
for (;;) {
n = readlogline(&nclog, &ptr);
if (n < 0)
break;
if (n == 0)
continue;
/*
* Scroll down
*/
if (n > COLS)
w = COLS;
else
w = n;
c = ptr[w];
ptr[w] = 0;
/*
* Filter out these logs from the display (they remain in
* the 00*.log file) to reduce clutter.
*/
if (strncmp(ptr, "[XXX] Load=", 11) != 0) {
/*
* Output possibly colored log line
*/
wscrl(CMon, -1);
if (strstr(ptr, "] SUCCESS ")) {
wattrset(CMon, COLOR_PAIR(2));
} else if (strstr(ptr, "] FAILURE ")) {
wattrset(CMon, COLOR_PAIR(1));
}
mvwprintw(CMon, 0, 0, "%s", ptr);
wattrset(CMon, COLOR_PAIR(3));
}
ptr[w] = c;
}
}
static void
NCursesUpdate(worker_t *work, const char *portdir)
{
const char *phase;
const char *origin;
time_t t;
int i = work->index;
int h;
int m;
int s;
if (UseNCurses == 0)
return;
phase = "Unknown";
origin = "";
switch(work->state) {
case WORKER_NONE:
phase = "None";
/* fall through */
case WORKER_IDLE:
if (work->state == WORKER_IDLE)
phase = "Idle";
/* fall through */
case WORKER_FAILED:
if (work->state == WORKER_FAILED)
phase = "Failed";
/* fall through */
case WORKER_EXITING:
if (work->state == WORKER_EXITING)
phase = "Exiting";
mvwprintw(CWin, WORKER_START + i, DURATION_COL,
"--:--:--");
mvwprintw(CWin, WORKER_START + i, BUILD_PHASE_COL,
"%-16.16s", phase);
mvwprintw(CWin, WORKER_START + i, ORIGIN_COL,
"%-38.38s", "");
mvwprintw(CWin, WORKER_START + i, LINES_COL,
"%-7.7s", "");
return;
case WORKER_PENDING:
phase = "Pending";
break;
case WORKER_RUNNING:
phase = "Running";
break;
case WORKER_DONE:
phase = "Done";
break;
case WORKER_FROZEN:
phase = "FROZEN";
break;
default:
break;
}
t = time(NULL) - work->start_time;
s = t % 60;
m = t / 60 % 60;
h = t / 60 / 60;
if (work->state == WORKER_RUNNING)
phase = getphasestr(work->phase);
/*
* When called from the monitor frontend portdir has to be passed
* in directly because work->pkg is not mapped.
*/
if (portdir)
origin = portdir;
else if (work->pkg)
origin = work->pkg->portdir;
else
origin = "";
mvwprintw(CWin, WORKER_START + i, DURATION_COL,
"%02d:%02d:%02d", h, m, s);
mvwprintw(CWin, WORKER_START + i, BUILD_PHASE_COL,
"%-16.16s", phase);
mvwprintw(CWin, WORKER_START + i, ORIGIN_COL,
"%-38.38s", origin);
if (work->lines > 9999999) {
mvwprintw(CWin, WORKER_START + i, LINES_COL,
"%7s", "*MANY*%d", work->lines % 10);
} else {
mvwprintw(CWin, WORKER_START + i, LINES_COL,
"%7d", work->lines);
}
}
static void
NCursesSync(void)
{
int c;
if (UseNCurses == 0)
return;
while ((c = wgetch(CMon)) != ERR) {
if (c == KEY_RESIZE)
NCursesReset();
}
wrefresh(CWin);
wrefresh(CMon);
}
static void
NCursesDone(void)
{
if (UseNCurses == 0)
return;
endwin();
}
runstats_t NCursesRunStats = {
.init = NCursesInit,
.done = NCursesDone,
.reset = NCursesReset,
.update = NCursesUpdate,
.updateTop = NCursesUpdateTop,
.updateLogs = NCursesUpdateLogs,
.sync = NCursesSync
};