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 #if 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 #include <QRandomGenerator>
40
41 #include <memory>
42 #include <list>
43 #include <iostream>
44 #include <string>
45
46 #if ICECC_TEST_USE_OLD_MSG_API
47 #define ICECC_MSG_API_COMPAT(old, new) old
48 #else
49 #define ICECC_MSG_API_COMPAT(old, new) new
50 #endif
51
52 using namespace std;
53
54 IcecreamMonitor::IcecreamMonitor(HostInfoManager *manager, QObject *parent)
55 : Monitor(manager, parent)
56 {
57 setupDebug();
58 checkScheduler();
59 }
60
61 IcecreamMonitor::~IcecreamMonitor()
62 {
63 delete m_scheduler;
64 delete m_discover;
65 }
66
67 QList<Job> IcecreamMonitor::jobHistory() const
68 {
69 return m_rememberedJobs.values();
70 }
71
72 void IcecreamMonitor::checkScheduler(bool deleteit)
73 {
74 if (deleteit) {
75 m_rememberedJobs.clear();
76 delete m_scheduler;
77 m_scheduler = nullptr;
78 delete m_fd_notify;
79 m_fd_notify = nullptr;
80 m_fd_type = QSocketNotifier::Exception;
81 delete m_discover;
82 m_discover = nullptr;
83 setSchedulerState(Offline);
84 } else if (m_scheduler) {
85 return;
86 }
87 QTimer::singleShot(1000 + (QRandomGenerator::global()->generate() & 1023), this, &IcecreamMonitor::slotCheckScheduler); // TODO: check if correct
88 }
89
90 void IcecreamMonitor::registerNotify(int fd, QSocketNotifier::Type type, const char *slot)
91 {
92 if (m_fd_notify) {
93 if(m_fd_notify->socket() != fd || m_fd_notify->type() != type) {
94 m_fd_notify->disconnect(this);
95 m_fd_notify->deleteLater();
96 m_fd_notify = nullptr;
97 }
98 // Reuse, but the slot will change.
99 m_fd_notify->disconnect(this);
100 }
101 if (!m_fd_notify) {
102 m_fd_notify = new QSocketNotifier(fd, type, this);
103 m_fd_type = type;
104 }
105 QObject::connect(m_fd_notify, SIGNAL(activated(int)), slot);
106 }
107
108 void IcecreamMonitor::slotCheckScheduler()
109 {
110 if (m_scheduler) {
111 return;
112 }
113
114 const string hostname = currentSchedname().isEmpty() ? "" : currentSchedname().data();
115 list<string> names;
116 const uint port = currentSchedport();
117
118 if (!currentNetname().isEmpty()) {
119 names.push_front(currentNetname().data());
120 } else {
121 names.push_front("ICECREAM");
122 }
123
124 if (!qgetenv("USE_SCHEDULER").isEmpty()) {
125 names.push_front(""); // try $USE_SCHEDULER
126 }
127 for (auto it = names.begin(); it != names.end(); ++it) {
128 setCurrentNetname(QByteArray::fromStdString(*it));
129 if (!m_discover
130 || ((m_scheduler = m_discover->try_get_scheduler()) == NULL && m_discover->timed_out())) {
131 delete m_discover;
132 m_discover = new DiscoverSched(currentNetname().toStdString(), 2, hostname, port);
133 }
134
135 if (m_scheduler) {
136 hostInfoManager()->setSchedulerName(QString::fromLatin1(m_discover->schedulerName().data()));
137 hostInfoManager()->setNetworkName(QString::fromLatin1(m_discover->networkName().data()));
138 m_scheduler->setBulkTransfer();
139 delete m_discover;
140 m_discover = nullptr;
141 registerNotify(m_scheduler->fd,
142 QSocketNotifier::Read, SLOT(msgReceived()));
143
144 if (!m_scheduler->send_msg(MonLoginMsg())) {
145 checkScheduler(true);
146 QTimer::singleShot(0, this, &IcecreamMonitor::slotCheckScheduler);
147 } else {
148 setSchedulerState(Online);
149 }
150 return;
151 }
152
153 if (m_fd_type != QSocketNotifier::Write
154 && m_discover->connect_fd() >= 0) {
155 registerNotify(m_discover->connect_fd(),
156 QSocketNotifier::Write, SLOT(slotCheckScheduler()));
157 return;
158 } else if (m_fd_type != QSocketNotifier::Read
159 && m_discover->listen_fd() >= 0) {
160 registerNotify(m_discover->listen_fd(),
161 QSocketNotifier::Read, SLOT(slotCheckScheduler()));
162 }
163 if (m_fd_type == QSocketNotifier::Read) {
164 QTimer::singleShot(1000 + (QRandomGenerator::global()->generate() & 1023), this, &IcecreamMonitor::slotCheckScheduler);
165 }
166 }
167
168 setSchedulerState(Offline);
169 }
170
171 void IcecreamMonitor::msgReceived()
172 {
173 while (!m_scheduler->read_a_bit() || m_scheduler->has_msg())
174 if (!handle_activity()) {
175 break;
176 }
177 }
178
179 bool IcecreamMonitor::handle_activity()
180 {
181 std::unique_ptr<Msg> m(m_scheduler->get_msg());
182 if (!m) {
183 checkScheduler(true);
184 setSchedulerState(Offline);
185 return false;
186 }
187
188 switch (ICECC_MSG_API_COMPAT(m->type, *m)) {
189 case ICECC_MSG_API_COMPAT(M_MON_GET_CS, Msg::MON_GET_CS):
190 handle_getcs(m.get());
191 break;
192 case ICECC_MSG_API_COMPAT(M_MON_JOB_BEGIN, Msg::MON_JOB_BEGIN):
193 handle_job_begin(m.get());
194 break;
195 case ICECC_MSG_API_COMPAT(M_MON_JOB_DONE, Msg::MON_JOB_DONE):
196 handle_job_done(m.get());
197 break;
198 case ICECC_MSG_API_COMPAT(M_END, Msg::END):
199 std::cout << "END" << endl;
200 checkScheduler(true);
201 break;
202 case ICECC_MSG_API_COMPAT(M_MON_STATS, Msg::MON_STATS):
203 handle_stats(m.get());
204 break;
205 case ICECC_MSG_API_COMPAT(M_MON_LOCAL_JOB_BEGIN, Msg::MON_LOCAL_JOB_BEGIN):
206 handle_local_begin(m.get());
207 break;
208 case ICECC_MSG_API_COMPAT(M_JOB_LOCAL_DONE, Msg::JOB_LOCAL_DONE):
209 handle_local_done(m.get());
210 break;
211 default:
212 cout << "UNKNOWN message type " << ICECC_MSG_API_COMPAT(m->type, m->to_string()) << endl;
213 break;
214 }
215 return true;
216 }
217
218 void IcecreamMonitor::handle_getcs(Msg *_m)
219 {
220 auto *m = dynamic_cast<MonGetCSMsg *>(_m);
221 assert(m);
222 if (!m) {
223 return;
224 }
225
226 m_rememberedJobs[m->job_id] = Job(m->job_id, m->clientid,
227 QString::fromStdString(m->filename),
228 m->lang == CompileJob::Lang_C ?
229 QStringLiteral("C") :
230 QStringLiteral("C++")
231 );
232 emit jobUpdated(m_rememberedJobs[m->job_id]);
233 }
234
235 void IcecreamMonitor::handle_local_begin(Msg *_m)
236 {
237 auto *m = dynamic_cast<MonLocalJobBeginMsg *>(_m);
238 assert(m);
239 if (!m) {
240 return;
241 }
242
243 m_rememberedJobs[m->job_id] = Job(m->job_id, m->hostid,
244 QString::fromStdString(m->file),
245 QStringLiteral("C++"));
246 m_rememberedJobs[m->job_id].state = Job::LocalOnly;
247 emit jobUpdated(m_rememberedJobs[m->job_id]);
248 }
249
250 void IcecreamMonitor::handle_local_done(Msg *_m)
251 {
252 auto *m = dynamic_cast<JobLocalDoneMsg *>(_m);
253 assert(m);
254 if (!m) {
255 return;
256 }
257
258 JobList::iterator it = m_rememberedJobs.find(m->job_id);
259 if (it == m_rememberedJobs.end()) {
260 // we started in between
261 return;
262 }
263
264 (*it).state = Job::Finished;
265 emit jobUpdated(*it);
266
267 if (m_rememberedJobs.size() > 3000) { // now remove 1000
268 uint count = 1000;
269
270 while (--count)
271 m_rememberedJobs.erase(m_rememberedJobs.begin());
272 }
273 }
274
275 void IcecreamMonitor::handle_stats(Msg *_m)
276 {
277 auto *m = dynamic_cast<MonStatsMsg *>(_m);
278 assert(m);
279 if (!m) {
280 return;
281 }
282
283 QStringList statmsg = QString::fromStdString(m->statmsg).split(QLatin1Char('\n'));
284 HostInfo::StatsMap stats;
285 for (QStringList::ConstIterator it = statmsg.constBegin(); it != statmsg.constEnd();
286 ++it) {
287 QString key = *it;
288 key = key.left(key.indexOf(QLatin1Char(':')));
289 QString value = *it;
290 value = value.mid(value.indexOf(QLatin1Char(':')) + 1);
291 stats[key] = value;
292 }
293
294 HostInfo *hostInfo = hostInfoManager()->checkNode(m->hostid, stats);
295
296 if (!hostInfo) {
297 emit nodeRemoved(m->hostid);
298 } else {
299 emit nodeUpdated(m->hostid);
300 }
301 }
302
303 void IcecreamMonitor::handle_job_begin(Msg *_m)
304 {
305 auto *m = dynamic_cast<MonJobBeginMsg *>(_m);
306 assert(m);
307 if (!m) {
308 return;
309 }
310
311 JobList::iterator it = m_rememberedJobs.find(m->job_id);
312 if (it == m_rememberedJobs.end()) {
313 // we started in between
314 return;
315 }
316
317 HostInfo *hostInfo = hostInfoManager()->find(m->hostid);
318 assert(hostInfo);
319 if (hostInfo)
320 hostInfo->incJobs();
321
322 (*it).server = m->hostid;
323 (*it).startTime = m->stime;
324 (*it).state = Job::Compiling;
325
326 emit jobUpdated(*it);
327 }
328
329 void IcecreamMonitor::handle_job_done(Msg *_m)
330 {
331 auto *m = dynamic_cast<MonJobDoneMsg *>(_m);
332 assert(m);
333 if (!m) {
334 return;
335 }
336
337 JobList::iterator it = m_rememberedJobs.find(m->job_id);
338 if (it == m_rememberedJobs.end()) {
339 // we started in between
340 return;
341 }
342
343 HostInfo *hostInfo = hostInfoManager()->find((*it).server);
344 if (hostInfo)
345 hostInfo->decJobs();
346
347 (*it).exitcode = m->exitcode;
348 if (m->exitcode) {
349 (*it).state = Job::Failed;
350 } else {
351 (*it).state = Job::Finished;
352 (*it).real_msec = m->real_msec;
353 (*it).user_msec = m->user_msec;
354 (*it).sys_msec = m->sys_msec; /* system time used */
355 (*it).pfaults = m->pfaults; /* page faults */
356
357 (*it).in_compressed = m->in_compressed;
358 (*it).in_uncompressed = m->in_uncompressed;
359 (*it).out_compressed = m->out_compressed;
360 (*it).out_uncompressed = m->out_uncompressed;
361 }
362
363 emit jobUpdated(*it);
364 }
365
366 void IcecreamMonitor::setupDebug()
367 {
368 #if ICECC_HAVE_LOGGING_H
369 char *env = getenv("ICECC_DEBUG");
370 int debug_level = Error;
371
372 if (env) {
373 if (!strcasecmp(env, "info")) {
374 debug_level |= Info | Warning;
375 } else if (!strcasecmp(env, "warnings")) {
376 debug_level |= Warning; // taking out warning
377 } else { // any other value
378 debug_level |= Info | Debug | Warning;
379 }
380 }
381
382 std::string logfile;
383
384 if (const char *logfileEnv = getenv("ICECC_LOGFILE")) {
385 logfile = logfileEnv;
386 }
387
388 setup_debug(debug_level, logfile, "ICEMON");
389 #endif
390 }