Debian Codegraph @

src/icecreammonitor.cc

raw
1 /*
2 This file is part of Icecream.
3
4 Copyright (c) 2003 Frerich Raabe <[email protected]>
5 Copyright (c) 2003,2004 Stephan Kulow <[email protected]>
6 Copyright (c) 2003,2004 Cornelius Schumacher <[email protected]>
7 Copyright (c) 2007 Dirk Mueller <[email protected]>
8
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License along
20 with this program; if not, write to the Free Software Foundation, Inc.,
21 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 */
23
24 #include "icecreammonitor.h"
25
26 #include "hostinfo.h"
27 #include "statusview.h"
28
29 #include <config-icemon.h>
30
31 #include <icecc/comm.h>
32 #ifdef ICECC_HAVE_LOGGING_H
33 #include <icecc/logging.h>
34 #endif
35
36 #include <qdebug.h>
37 #include <qsocketnotifier.h>
38 #include <qtimer.h>
39
40 #include <list>
41 #include <iostream>
42 #include <string>
43
44 using namespace std;
45
46 IcecreamMonitor::IcecreamMonitor(HostInfoManager *manager, QObject *parent)
47 : Monitor(manager, parent)
48 , m_scheduler(nullptr)
49 , m_discover(nullptr)
50 , m_fd_notify(nullptr)
51 , m_fd_type(QSocketNotifier::Exception)
52 {
53 setupDebug();
54 checkScheduler();
55 }
56
57 IcecreamMonitor::~IcecreamMonitor()
58 {
59 delete m_scheduler;
60 delete m_discover;
61 }
62
63 QList<Job> IcecreamMonitor::jobHistory() const
64 {
65 return m_rememberedJobs.values();
66 }
67
68 void IcecreamMonitor::checkScheduler(bool deleteit)
69 {
70 if (deleteit) {
71 m_rememberedJobs.clear();
72 delete m_scheduler;
73 m_scheduler = nullptr;
74 delete m_fd_notify;
75 m_fd_notify = nullptr;
76 m_fd_type = QSocketNotifier::Exception;
77 delete m_discover;
78 m_discover = nullptr;
79 setSchedulerState(Offline);
80 } else if (m_scheduler) {
81 return;
82 }
83 QTimer::singleShot(1000 + (qrand() & 1023), this, SLOT(slotCheckScheduler())); // TODO: check if correct
84 }
85
86 void IcecreamMonitor::registerNotify(int fd, QSocketNotifier::Type type, const char *slot)
87 {
88 if (m_fd_notify) {
89 if(m_fd_notify->socket() != fd || m_fd_notify->type() != type) {
90 m_fd_notify->disconnect(this);
91 m_fd_notify->deleteLater();
92 m_fd_notify = nullptr;
93 }
94 // Reuse, but the slot will change.
95 m_fd_notify->disconnect(this);
96 }
97 if (!m_fd_notify) {
98 m_fd_notify = new QSocketNotifier(fd, type, this);
99 m_fd_type = type;
100 }
101 QObject::connect(m_fd_notify, SIGNAL(activated(int)), slot);
102 }
103
104 void IcecreamMonitor::slotCheckScheduler()
105 {
106 if (m_scheduler) {
107 return;
108 }
109
110 const string hostname = currentSchedname().isEmpty() ? "" : currentSchedname().data();
111 list<string> names;
112 const uint port = currentSchedport();
113
114 if (!currentNetname().isEmpty()) {
115 names.push_front(currentNetname().data());
116 } else {
117 names.push_front("ICECREAM");
118 }
119
120 if (!qgetenv("USE_SCHEDULER").isEmpty()) {
121 names.push_front(""); // try $USE_SCHEDULER
122 }
123 for (auto it = names.begin(); it != names.end(); ++it) {
124 setCurrentNetname(QByteArray::fromStdString(*it));
125 if (!m_discover
126 || ((m_scheduler = m_discover->try_get_scheduler()) == NULL && m_discover->timed_out())) {
127 delete m_discover;
128 m_discover = new DiscoverSched(currentNetname().toStdString(), 2, hostname, port);
129 }
130
131 if (m_scheduler) {
132 hostInfoManager()->setSchedulerName(QString::fromLatin1(m_discover->schedulerName().data()));
133 hostInfoManager()->setNetworkName(QString::fromLatin1(m_discover->networkName().data()));
134 m_scheduler->setBulkTransfer();
135 delete m_discover;
136 m_discover = nullptr;
137 registerNotify(m_scheduler->fd,
138 QSocketNotifier::Read, SLOT(msgReceived()));
139
140 if (!m_scheduler->send_msg(MonLoginMsg())) {
141 checkScheduler(true);
142 QTimer::singleShot(0, this, SLOT(slotCheckScheduler()));
143 } else {
144 setSchedulerState(Online);
145 }
146 return;
147 }
148
149 if (m_fd_type != QSocketNotifier::Write
150 && m_discover->connect_fd() >= 0) {
151 registerNotify(m_discover->connect_fd(),
152 QSocketNotifier::Write, SLOT(slotCheckScheduler()));
153 return;
154 } else if (m_fd_type != QSocketNotifier::Read
155 && m_discover->listen_fd() >= 0) {
156 registerNotify(m_discover->listen_fd(),
157 QSocketNotifier::Read, SLOT(slotCheckScheduler()));
158 }
159 if (m_fd_type == QSocketNotifier::Read) {
160 QTimer::singleShot(1000 + (qrand() & 1023), this, SLOT(slotCheckScheduler()));
161 }
162 }
163
164 setSchedulerState(Offline);
165 }
166
167 void IcecreamMonitor::msgReceived()
168 {
169 while (!m_scheduler->read_a_bit() || m_scheduler->has_msg())
170 if (!handle_activity()) {
171 break;
172 }
173 }
174
175 bool IcecreamMonitor::handle_activity()
176 {
177 Msg *m = m_scheduler->get_msg();
178 if (!m) {
179 checkScheduler(true);
180 setSchedulerState(Offline);
181 return false;
182 }
183
184 switch (m->type) {
185 case M_MON_GET_CS:
186 handle_getcs(m);
187 break;
188 case M_MON_JOB_BEGIN:
189 handle_job_begin(m);
190 break;
191 case M_MON_JOB_DONE:
192 handle_job_done(m);
193 break;
194 case M_END:
195 std::cout << "END" << endl;
196 checkScheduler(true);
197 break;
198 case M_MON_STATS:
199 handle_stats(m);
200 break;
201 case M_MON_LOCAL_JOB_BEGIN:
202 handle_local_begin(m);
203 break;
204 case M_JOB_LOCAL_DONE:
205 handle_local_done(m);
206 break;
207 default:
208 cout << "UNKNOWN" << endl;
209 break;
210 }
211 delete m;
212 return true;
213 }
214
215 void IcecreamMonitor::handle_getcs(Msg *_m)
216 {
217 MonGetCSMsg *m = dynamic_cast<MonGetCSMsg *>(_m);
218 if (!m) {
219 return;
220 }
221 m_rememberedJobs[m->job_id] = Job(m->job_id, m->clientid,
222 QString::fromStdString(m->filename),
223 m->lang == CompileJob::Lang_C ?
224 QStringLiteral("C") :
225 QStringLiteral("C++")
226 );
227 emit jobUpdated(m_rememberedJobs[m->job_id]);
228 }
229
230 void IcecreamMonitor::handle_local_begin(Msg *_m)
231 {
232 MonLocalJobBeginMsg *m = dynamic_cast<MonLocalJobBeginMsg *>(_m);
233 if (!m) {
234 return;
235 }
236
237 m_rememberedJobs[m->job_id] = Job(m->job_id, m->hostid,
238 QString::fromStdString(m->file),
239 QStringLiteral("C++"));
240 m_rememberedJobs[m->job_id].state = Job::LocalOnly;
241 emit jobUpdated(m_rememberedJobs[m->job_id]);
242 }
243
244 void IcecreamMonitor::handle_local_done(Msg *_m)
245 {
246 JobLocalDoneMsg *m = dynamic_cast<JobLocalDoneMsg *>(_m);
247 if (!m) {
248 return;
249 }
250
251 JobList::iterator it = m_rememberedJobs.find(m->job_id);
252 if (it == m_rememberedJobs.end()) {
253 // we started in between
254 return;
255 }
256
257 (*it).state = Job::Finished;
258 emit jobUpdated(*it);
259
260 if (m_rememberedJobs.size() > 3000) { // now remove 1000
261 uint count = 1000;
262
263 while (--count)
264 m_rememberedJobs.erase(m_rememberedJobs.begin());
265 }
266 }
267
268 void IcecreamMonitor::handle_stats(Msg *_m)
269 {
270 MonStatsMsg *m = dynamic_cast<MonStatsMsg *>(_m);
271 if (!m) {
272 return;
273 }
274
275 QStringList statmsg = QString::fromStdString(m->statmsg).split(QLatin1Char('\n'));
276 HostInfo::StatsMap stats;
277 for (QStringList::ConstIterator it = statmsg.constBegin(); it != statmsg.constEnd();
278 ++it) {
279 QString key = *it;
280 key = key.left(key.indexOf(QLatin1Char(':')));
281 QString value = *it;
282 value = value.mid(value.indexOf(QLatin1Char(':')) + 1);
283 stats[key] = value;
284 }
285
286 HostInfo *hostInfo = hostInfoManager()->checkNode(m->hostid, stats);
287
288 if (!hostInfo) {
289 emit nodeRemoved(m->hostid);
290 } else {
291 emit nodeUpdated(m->hostid);
292 }
293 }
294
295 void IcecreamMonitor::handle_job_begin(Msg *_m)
296 {
297 MonJobBeginMsg *m = dynamic_cast<MonJobBeginMsg *>(_m);
298 if (!m) {
299 return;
300 }
301
302 JobList::iterator it = m_rememberedJobs.find(m->job_id);
303 if (it == m_rememberedJobs.end()) {
304 // we started in between
305 return;
306 }
307
308 HostInfo *hostInfo = hostInfoManager()->find(m->hostid);
309 if (hostInfo)
310 hostInfo->incJobs();
311
312 (*it).server = m->hostid;
313 (*it).startTime = m->stime;
314 (*it).state = Job::Compiling;
315
316 emit jobUpdated(*it);
317 }
318
319 void IcecreamMonitor::handle_job_done(Msg *_m)
320 {
321 MonJobDoneMsg *m = dynamic_cast<MonJobDoneMsg *>(_m);
322 if (!m) {
323 return;
324 }
325
326 JobList::iterator it = m_rememberedJobs.find(m->job_id);
327 if (it == m_rememberedJobs.end()) {
328 // we started in between
329 return;
330 }
331
332 HostInfo *hostInfo = hostInfoManager()->find((*it).server);
333 if (hostInfo)
334 hostInfo->decJobs();
335
336 (*it).exitcode = m->exitcode;
337 if (m->exitcode) {
338 (*it).state = Job::Failed;
339 } else {
340 (*it).state = Job::Finished;
341 (*it).real_msec = m->real_msec;
342 (*it).user_msec = m->user_msec;
343 (*it).sys_msec = m->sys_msec; /* system time used */
344 (*it).pfaults = m->pfaults; /* page faults */
345
346 (*it).in_compressed = m->in_compressed;
347 (*it).in_uncompressed = m->in_uncompressed;
348 (*it).out_compressed = m->out_compressed;
349 (*it).out_uncompressed = m->out_uncompressed;
350 }
351
352 emit jobUpdated(*it);
353 }
354
355 void IcecreamMonitor::setupDebug()
356 {
357 #ifdef ICECC_HAVE_LOGGING_H
358 char *env = getenv("ICECC_DEBUG");
359 int debug_level = Error;
360
361 if (env) {
362 if (!strcasecmp(env, "info")) {
363 debug_level |= Info | Warning;
364 } else if (!strcasecmp(env, "warnings")) {
365 debug_level |= Warning; // taking out warning
366 } else { // any other value
367 debug_level |= Info | Debug | Warning;
368 }
369 }
370
371 std::string logfile;
372
373 if (const char *logfileEnv = getenv("ICECC_LOGFILE")) {
374 logfile = logfileEnv;
375 }
376
377 setup_debug(debug_level, logfile, "ICEMON");
378 #endif
379 }