2005-01-20 23:22:22 +00:00
|
|
|
// Berkeley Open Infrastructure for Network Computing
|
|
|
|
// http://boinc.berkeley.edu
|
|
|
|
// Copyright (C) 2005 University of California
|
2003-08-20 18:56:52 +00:00
|
|
|
//
|
2005-01-20 23:22:22 +00:00
|
|
|
// This is free software; you can redistribute it and/or
|
|
|
|
// modify it under the terms of the GNU Lesser General Public
|
|
|
|
// License as published by the Free Software Foundation;
|
|
|
|
// either version 2.1 of the License, or (at your option) any later version.
|
2003-08-20 18:56:52 +00:00
|
|
|
//
|
2005-01-20 23:22:22 +00:00
|
|
|
// This software is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
// See the GNU Lesser General Public License for more details.
|
2002-04-30 22:22:54 +00:00
|
|
|
//
|
2005-01-20 23:22:22 +00:00
|
|
|
// To view the GNU Lesser General Public License visit
|
|
|
|
// http://www.gnu.org/copyleft/lesser.html
|
|
|
|
// or write to the Free Software Foundation, Inc.,
|
|
|
|
// 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
2002-04-30 22:22:54 +00:00
|
|
|
|
2004-03-04 11:41:43 +00:00
|
|
|
#ifdef _WIN32
|
2004-06-16 23:16:08 +00:00
|
|
|
#include "boinc_win.h"
|
2004-03-04 11:41:43 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef _WIN32
|
2005-11-21 18:34:44 +00:00
|
|
|
#include "config.h"
|
2004-07-13 13:54:09 +00:00
|
|
|
#include <cstdio>
|
|
|
|
#include <ctime>
|
|
|
|
#include <cmath>
|
2004-03-04 11:41:43 +00:00
|
|
|
#endif
|
2002-04-30 22:22:54 +00:00
|
|
|
|
2005-05-06 02:09:35 +00:00
|
|
|
#ifdef HAVE_SYS_SOCKET_H
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#endif
|
|
|
|
|
2002-04-30 22:22:54 +00:00
|
|
|
#include "parse.h"
|
2003-02-24 21:25:16 +00:00
|
|
|
#include "util.h"
|
2002-04-30 22:22:54 +00:00
|
|
|
#include "error_numbers.h"
|
2004-04-08 08:15:23 +00:00
|
|
|
#include "client_msgs.h"
|
2005-06-07 19:22:50 +00:00
|
|
|
#include "client_state.h"
|
2005-03-29 23:56:50 +00:00
|
|
|
#include "network.h"
|
2006-06-17 04:55:28 +00:00
|
|
|
#include "log_flags.h"
|
2002-04-30 22:22:54 +00:00
|
|
|
|
|
|
|
#include "time_stats.h"
|
|
|
|
|
|
|
|
// exponential decay constant.
|
2006-08-29 23:15:47 +00:00
|
|
|
// The last 10 days have a weight of 1/e;
|
2002-04-30 22:22:54 +00:00
|
|
|
// everything before that has a weight of (1-1/e)
|
|
|
|
|
2006-08-29 23:15:47 +00:00
|
|
|
const float ALPHA = (SECONDS_PER_DAY*10);
|
2005-01-28 21:46:41 +00:00
|
|
|
//const float ALPHA = 60; // for testing
|
2002-04-30 22:22:54 +00:00
|
|
|
|
2006-07-10 04:09:55 +00:00
|
|
|
bool BOINC_OUTAGE::is_recent() const {
|
|
|
|
return gstate.now - 60 * SECONDS_PER_DAY < end;
|
|
|
|
}
|
|
|
|
|
2002-04-30 22:22:54 +00:00
|
|
|
TIME_STATS::TIME_STATS() {
|
|
|
|
last_update = 0;
|
2002-07-15 23:21:20 +00:00
|
|
|
first = true;
|
2002-04-30 22:22:54 +00:00
|
|
|
on_frac = 1;
|
|
|
|
connected_frac = 1;
|
|
|
|
active_frac = 1;
|
2005-06-13 16:51:14 +00:00
|
|
|
cpu_efficiency = 1;
|
2002-04-30 22:22:54 +00:00
|
|
|
}
|
|
|
|
|
2002-07-15 23:21:20 +00:00
|
|
|
// Update time statistics based on current activities
|
2005-01-28 19:01:08 +00:00
|
|
|
// NOTE: we don't set the state-file dirty flag here,
|
|
|
|
// so these get written to disk only when other activities
|
|
|
|
// cause this to happen. Maybe should change this.
|
2002-07-15 23:21:20 +00:00
|
|
|
//
|
2005-06-07 19:22:50 +00:00
|
|
|
void TIME_STATS::update(bool is_active) {
|
2005-01-28 19:01:08 +00:00
|
|
|
double dt, w1, w2;
|
2002-04-30 22:22:54 +00:00
|
|
|
|
|
|
|
if (last_update == 0) {
|
|
|
|
// this is the first time this client has executed.
|
2005-01-28 21:46:41 +00:00
|
|
|
// Assume that everything is active
|
2002-04-30 22:22:54 +00:00
|
|
|
|
|
|
|
on_frac = 1;
|
2005-01-28 21:46:41 +00:00
|
|
|
connected_frac = 1;
|
|
|
|
active_frac = 1;
|
2002-04-30 22:22:54 +00:00
|
|
|
first = false;
|
2005-06-07 19:22:50 +00:00
|
|
|
last_update = gstate.now;
|
2002-04-30 22:22:54 +00:00
|
|
|
} else {
|
2005-06-07 19:22:50 +00:00
|
|
|
dt = gstate.now - last_update;
|
2005-01-28 19:01:08 +00:00
|
|
|
if (dt <= 10) return;
|
2005-01-28 21:46:41 +00:00
|
|
|
w1 = 1 - exp(-dt/ALPHA); // weight for recent period
|
|
|
|
w2 = 1 - w1; // weight for everything before that
|
|
|
|
// (close to zero if long gap)
|
2006-11-02 21:35:47 +00:00
|
|
|
|
|
|
|
// the following always returns UNKNOWN,
|
|
|
|
// but leave it here for now
|
|
|
|
//
|
2005-04-04 21:14:49 +00:00
|
|
|
int connected_state = get_connected_state();
|
2006-11-02 21:35:47 +00:00
|
|
|
|
2002-04-30 22:22:54 +00:00
|
|
|
if (first) {
|
|
|
|
// the client has just started; this is the first call.
|
2005-01-28 21:46:41 +00:00
|
|
|
//
|
2002-04-30 22:22:54 +00:00
|
|
|
on_frac *= w2;
|
|
|
|
first = false;
|
2006-07-10 04:09:55 +00:00
|
|
|
BOINC_OUTAGE outage;
|
|
|
|
outage.start = last_update;
|
|
|
|
outage.end = gstate.now;
|
|
|
|
outages.push_back(outage);
|
|
|
|
gstate.set_client_state_dirty("Outage");
|
2002-04-30 22:22:54 +00:00
|
|
|
} else {
|
|
|
|
on_frac = w1 + w2*on_frac;
|
2005-01-28 21:46:41 +00:00
|
|
|
if (connected_frac < 0) connected_frac = 0;
|
|
|
|
switch (connected_state) {
|
|
|
|
case CONNECTED_STATE_NOT_CONNECTED:
|
|
|
|
connected_frac *= w2;
|
|
|
|
break;
|
|
|
|
case CONNECTED_STATE_CONNECTED:
|
|
|
|
connected_frac *= w2;
|
|
|
|
connected_frac += w1;
|
|
|
|
break;
|
|
|
|
case CONNECTED_STATE_UNKNOWN:
|
|
|
|
connected_frac = -1;
|
|
|
|
}
|
2002-04-30 22:22:54 +00:00
|
|
|
active_frac *= w2;
|
2006-07-10 04:09:55 +00:00
|
|
|
if (is_active) {
|
|
|
|
active_frac += w1;
|
|
|
|
if (inactive_start) {
|
|
|
|
BOINC_OUTAGE outage;
|
|
|
|
outage.start = inactive_start;
|
|
|
|
outage.end = gstate.now;
|
|
|
|
outages.push_back(outage);
|
|
|
|
gstate.set_client_state_dirty("Outage");
|
|
|
|
inactive_start = 0;
|
|
|
|
}
|
|
|
|
} else if (inactive_start == 0){
|
|
|
|
inactive_start = gstate.now;
|
|
|
|
}
|
2002-04-30 22:22:54 +00:00
|
|
|
}
|
2005-06-07 19:22:50 +00:00
|
|
|
last_update = gstate.now;
|
2006-09-06 15:51:26 +00:00
|
|
|
if (log_flags.time_debug) {
|
2006-09-07 20:39:25 +00:00
|
|
|
msg_printf(0, MSG_INFO,
|
|
|
|
"[time_debug] dt %f w2 %f on %f; active %f; conn %f",
|
2006-09-06 15:51:26 +00:00
|
|
|
dt, w2, on_frac, active_frac, connected_frac
|
|
|
|
);
|
|
|
|
}
|
2002-04-30 22:22:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-06-13 16:51:14 +00:00
|
|
|
void TIME_STATS::update_cpu_efficiency(double cpu_wall_time, double cpu_time) {
|
2006-06-17 04:55:28 +00:00
|
|
|
double old_cpu_efficiency = cpu_efficiency;
|
2005-06-15 04:53:08 +00:00
|
|
|
if (cpu_wall_time < .01) return;
|
2005-06-19 01:22:39 +00:00
|
|
|
double w = exp(-cpu_wall_time/SECONDS_PER_DAY);
|
2005-06-13 16:51:14 +00:00
|
|
|
double e = cpu_time/cpu_wall_time;
|
2005-06-19 01:22:39 +00:00
|
|
|
if (e<0) {
|
2006-11-02 21:35:47 +00:00
|
|
|
return;
|
2005-06-19 01:22:39 +00:00
|
|
|
}
|
2005-06-13 16:51:14 +00:00
|
|
|
cpu_efficiency = w*cpu_efficiency + (1-w)*e;
|
2006-06-21 22:08:20 +00:00
|
|
|
if (log_flags.cpu_sched_debug){
|
2006-06-17 04:55:28 +00:00
|
|
|
msg_printf(0, MSG_INFO,
|
2006-09-07 20:39:25 +00:00
|
|
|
"[cpu_sched_debug] CPU efficiency old %f new %f wall %f CPU %f w %f e %f",
|
2006-06-17 04:55:28 +00:00
|
|
|
old_cpu_efficiency, cpu_efficiency, cpu_wall_time,
|
|
|
|
cpu_time, w, e
|
|
|
|
);
|
|
|
|
}
|
2005-06-13 16:51:14 +00:00
|
|
|
}
|
|
|
|
|
2002-07-15 23:21:20 +00:00
|
|
|
// Write XML based time statistics
|
|
|
|
//
|
2004-06-12 04:45:36 +00:00
|
|
|
int TIME_STATS::write(MIOFILE& out, bool to_server) {
|
|
|
|
out.printf(
|
2002-04-30 22:22:54 +00:00
|
|
|
"<time_stats>\n"
|
|
|
|
" <on_frac>%f</on_frac>\n"
|
|
|
|
" <connected_frac>%f</connected_frac>\n"
|
2005-06-13 16:51:14 +00:00
|
|
|
" <active_frac>%f</active_frac>\n"
|
|
|
|
" <cpu_efficiency>%f</cpu_efficiency>\n",
|
2002-04-30 22:22:54 +00:00
|
|
|
on_frac,
|
|
|
|
connected_frac,
|
2005-06-13 16:51:14 +00:00
|
|
|
active_frac,
|
|
|
|
cpu_efficiency
|
2002-04-30 22:22:54 +00:00
|
|
|
);
|
|
|
|
if (!to_server) {
|
2004-06-12 04:45:36 +00:00
|
|
|
out.printf(
|
2005-01-28 20:03:57 +00:00
|
|
|
" <last_update>%f</last_update>\n",
|
2002-04-30 22:22:54 +00:00
|
|
|
last_update
|
|
|
|
);
|
|
|
|
}
|
2006-08-08 21:46:00 +00:00
|
|
|
#if 0
|
|
|
|
// too much text. Maybe just send the longest outage?
|
|
|
|
//
|
2006-07-10 04:09:55 +00:00
|
|
|
if (outages.size()) {
|
|
|
|
out.printf(" <outages>\n");
|
|
|
|
unsigned int i;
|
|
|
|
for (i=0; i<outages.size(); i++) {
|
|
|
|
if (outages[i].is_recent()) {
|
|
|
|
out.printf(
|
|
|
|
" <outage>\n"
|
|
|
|
" <start>%f</start>\n"
|
|
|
|
" <end>%f</end>\n"
|
|
|
|
" </outage>\n",
|
|
|
|
outages[i].start,
|
|
|
|
outages[i].end
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
out.printf(" </outages>\n");
|
|
|
|
}
|
2006-08-08 21:46:00 +00:00
|
|
|
#endif
|
2004-06-12 04:45:36 +00:00
|
|
|
out.printf("</time_stats>\n");
|
2002-04-30 22:22:54 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2002-07-15 23:21:20 +00:00
|
|
|
// Parse XML based time statistics, usually from client_state.xml
|
|
|
|
//
|
2004-06-12 04:45:36 +00:00
|
|
|
int TIME_STATS::parse(MIOFILE& in) {
|
2002-04-30 22:22:54 +00:00
|
|
|
char buf[256];
|
2002-08-15 22:03:41 +00:00
|
|
|
|
2004-06-12 04:45:36 +00:00
|
|
|
while (in.fgets(buf, 256)) {
|
2002-04-30 22:22:54 +00:00
|
|
|
if (match_tag(buf, "</time_stats>")) return 0;
|
2005-01-28 20:03:57 +00:00
|
|
|
else if (parse_double(buf, "<last_update>", last_update)) continue;
|
2002-04-30 22:22:54 +00:00
|
|
|
else if (parse_double(buf, "<on_frac>", on_frac)) continue;
|
|
|
|
else if (parse_double(buf, "<connected_frac>", connected_frac)) continue;
|
|
|
|
else if (parse_double(buf, "<active_frac>", active_frac)) continue;
|
2005-06-19 01:22:39 +00:00
|
|
|
else if (parse_double(buf, "<cpu_efficiency>", cpu_efficiency)) {
|
|
|
|
if (cpu_efficiency < 0) cpu_efficiency = 1;
|
|
|
|
if (cpu_efficiency > 1) cpu_efficiency = 1;
|
|
|
|
continue;
|
2006-07-10 04:09:55 +00:00
|
|
|
} else if (match_tag(buf, "<outages>")){
|
|
|
|
while(in.fgets(buf, 256)) {
|
|
|
|
if (match_tag(buf, "</outages>")) break;
|
|
|
|
else if (match_tag(buf, "<outage>")) {
|
|
|
|
BOINC_OUTAGE outage;
|
|
|
|
while (in.fgets(buf, 256)) {
|
|
|
|
if (match_tag(buf, "</outage>")) {
|
|
|
|
outages.push_back(outage);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else if (parse_double(buf, "<start>", outage.start)) continue;
|
|
|
|
else if (parse_double(buf, "<end>", outage.end)) continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2006-06-22 19:40:30 +00:00
|
|
|
if (log_flags.unparsed_xml) {
|
2007-01-25 23:39:06 +00:00
|
|
|
msg_printf(0, MSG_INFO,
|
2006-09-07 20:39:25 +00:00
|
|
|
"[unparsed_xml] TIME_STATS::parse(): unrecognized: %s\n", buf
|
2006-06-22 19:40:30 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2002-04-30 22:22:54 +00:00
|
|
|
}
|
|
|
|
return ERR_XML_PARSE;
|
|
|
|
}
|
2004-12-08 00:40:19 +00:00
|
|
|
|
2006-07-10 04:09:55 +00:00
|
|
|
double TIME_STATS::get_longest_boinc_outage() const {
|
|
|
|
unsigned int i;
|
|
|
|
double longest_outage = 0;
|
|
|
|
for (i=0; i<outages.size(); i++) {
|
|
|
|
if (outages[i].is_recent()) {
|
|
|
|
double outage_length = outages[i].end - outages[i].start;
|
|
|
|
if (outage_length > longest_outage) longest_outage = outage_length;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return longest_outage;
|
|
|
|
}
|
|
|
|
|
2005-01-02 18:29:53 +00:00
|
|
|
const char *BOINC_RCSID_472504d8c2 = "$Id$";
|