1 /* -*- c-file-style: "java"; indent-tabs-mode: nil; tab-width: 4; fill-column: 78 -*-
2  *
3  * distcc -- A simple distributed compiler system
4  *
5  * Copyright (C) 2003, 2004 by Martin Pool <mbp@samba.org>
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
20  * USA.
21  */
22 
23 #include <config.h>
24 
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <errno.h>
29 #include <unistd.h>
30 #include <fcntl.h>
31 
32 #include "types.h"
33 #include "distcc.h"
34 #include "rpc.h"
35 #include "trace.h"
36 #include "exitcode.h"
37 #include "snprintf.h"
38 #include "mon.h"
39 
40 
41 /**
42  * @file
43  *
44  * Plain text monitor program.  Just prints out the state once, or
45  * repeatedly, kind of like Linux vmstat.
46  */
47 
48 
49 const char *rs_program_name = "distccmon-text";
50 
51 
usage(void)52 static void usage(void)
53 {
54     fprintf(stderr, "usage: distccmon-text [DELAY]\n"
55 "\n"
56 "Displays current compilation jobs in text form.\n"
57 "\n"
58 "If delay is specified, repeatedly updates after that many (fractional)\n"
59 "seconds.  Otherwise, runs just once.\n");
60 }
61 
main(int argc,char * argv[])62 int main(int argc, char *argv[])
63 {
64     struct dcc_task_state *list;
65     int ret;
66     float delay;
67     char *end;
68 
69     dcc_set_trace_from_env();
70 
71     if (argc == 1)
72         delay = 0.0;
73     else if (argc == 2) {
74         delay = strtod(argv[1], &end);
75         if (*end) {
76             usage();
77             return 1;
78         }
79     } else {
80         usage();
81         return 1;
82     }
83 
84     /* We might be writing to e.g. a pipe that's being read by some
85      * other program, so make sure we're always line buffered. */
86     setvbuf (stdout, NULL, _IOLBF, BUFSIZ);
87 
88     do {
89         struct dcc_task_state *i;
90 
91         if ((ret = dcc_mon_poll(&list)))
92             return ret;
93 
94         for (i = list; i; i = i->next) {
95 #if 1
96             if (i->curr_phase == DCC_PHASE_DONE)
97                 continue;
98 #endif
99             /* Assume 80 cols = */
100             printf("%6ld  %-10.10s  %-30.30s %24.24s[%d]\n",
101                    (long) i->cpid,
102                    dcc_get_phase_name(i->curr_phase),
103                    i->file, i->host, i->slot);
104         }
105 
106         printf("\n");
107 
108         /* XXX: usleep() is probably not very portable */
109         usleep(delay * 1000000);
110 
111         dcc_task_state_free(list);
112     } while (delay);
113 
114     return 0;
115 }
116