boinc/client/time_stats.C

132 lines
3.8 KiB
C

// Berkeley Open Infrastructure for Network Computing
// http://boinc.berkeley.edu
// Copyright (C) 2005 University of California
//
// 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.
//
// 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.
//
// 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
#ifdef _WIN32
#include "boinc_win.h"
#endif
#ifndef _WIN32
#include <cstdio>
#include <ctime>
#include <cmath>
#endif
#include "parse.h"
#include "util.h"
#include "error_numbers.h"
#include "client_msgs.h"
#include "time_stats.h"
// exponential decay constant.
// The last 30 days have a weight of 1/e;
// everything before that has a weight of (1-1/e)
const float ALPHA = (SECONDS_PER_DAY*30);
TIME_STATS::TIME_STATS() {
last_update = 0;
first = true;
on_frac = 1;
connected_frac = 1;
active_frac = 1;
}
// Update time statistics based on current activities
//
void TIME_STATS::update(bool is_connected, bool is_active) {
int now = time(0), dt;
double w1, w2;
if (last_update == 0) {
// this is the first time this client has executed.
// Assume that its state has always been what it is now
on_frac = 1;
connected_frac = is_connected?1:0;
active_frac = true;
first = false;
last_update = now;
} else {
dt = now - last_update;
if (dt <= 0) return;
w1 = 1 - exp(-dt/ALPHA);
w2 = 1 - w1;
if (first) {
// the client has just started; this is the first call.
// The client has been off (and disconnected and inactive)
// since the last time it ran.
on_frac *= w2;
connected_frac *= w2;
active_frac *= w2;
first = false;
} else {
on_frac = w1 + w2*on_frac;
connected_frac *= w2;
if (is_connected) connected_frac += w1;
active_frac *= w2;
if (is_active) active_frac += w1;
}
last_update = now;
}
}
// Write XML based time statistics
//
int TIME_STATS::write(MIOFILE& out, bool to_server) {
out.printf(
"<time_stats>\n"
" <on_frac>%f</on_frac>\n"
" <connected_frac>%f</connected_frac>\n"
" <active_frac>%f</active_frac>\n",
on_frac,
connected_frac,
active_frac
);
if (!to_server) {
out.printf(
" <last_update>%d</last_update>\n",
last_update
);
}
out.printf("</time_stats>\n");
return 0;
}
// Parse XML based time statistics, usually from client_state.xml
//
int TIME_STATS::parse(MIOFILE& in) {
char buf[256];
SCOPE_MSG_LOG scope_messages(log_messages, CLIENT_MSG_LOG::DEBUG_STATE);
while (in.fgets(buf, 256)) {
if (match_tag(buf, "</time_stats>")) return 0;
else if (parse_int(buf, "<last_update>", last_update)) continue;
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;
else scope_messages.printf("TIME_STATS::parse(): unrecognized: %s\n", buf);
}
return ERR_XML_PARSE;
}
const char *BOINC_RCSID_472504d8c2 = "$Id$";