2003-06-11 23:36:48 +00:00
|
|
|
// The contents of this file are subject to the BOINC Public License
|
2002-04-30 22:22:54 +00:00
|
|
|
// Version 1.0 (the "License"); you may not use this file except in
|
|
|
|
// compliance with the License. You may obtain a copy of the License at
|
2003-06-11 23:36:48 +00:00
|
|
|
// http://boinc.berkeley.edu/license_1.0.txt
|
2003-08-20 18:56:52 +00:00
|
|
|
//
|
2002-04-30 22:22:54 +00:00
|
|
|
// Software distributed under the License is distributed on an "AS IS"
|
|
|
|
// basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
|
|
|
|
// License for the specific language governing rights and limitations
|
2003-08-20 18:56:52 +00:00
|
|
|
// under the License.
|
|
|
|
//
|
|
|
|
// The Original Code is the Berkeley Open Infrastructure for Network Computing.
|
|
|
|
//
|
2002-04-30 22:22:54 +00:00
|
|
|
// The Initial Developer of the Original Code is the SETI@home project.
|
|
|
|
// Portions created by the SETI@home project are Copyright (C) 2002
|
2003-08-20 18:56:52 +00:00
|
|
|
// University of California at Berkeley. All Rights Reserved.
|
|
|
|
//
|
2002-04-30 22:22:54 +00:00
|
|
|
// Contributor(s):
|
|
|
|
//
|
|
|
|
|
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
|
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
|
|
|
|
|
|
|
#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"
|
2002-04-30 22:22:54 +00:00
|
|
|
|
|
|
|
#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)
|
|
|
|
|
2003-08-07 23:38:38 +00:00
|
|
|
const float ALPHA = (SECONDS_PER_DAY*30);
|
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;
|
|
|
|
}
|
|
|
|
|
2002-07-15 23:21:20 +00:00
|
|
|
// Update time statistics based on current activities
|
|
|
|
//
|
2002-04-30 22:22:54 +00:00
|
|
|
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;
|
2003-08-20 18:56:52 +00:00
|
|
|
active_frac = true;
|
2002-04-30 22:22:54 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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"
|
|
|
|
" <active_frac>%f</active_frac>\n",
|
|
|
|
on_frac,
|
|
|
|
connected_frac,
|
|
|
|
active_frac
|
|
|
|
);
|
|
|
|
if (!to_server) {
|
2004-06-12 04:45:36 +00:00
|
|
|
out.printf(
|
2002-04-30 22:22:54 +00:00
|
|
|
" <last_update>%d</last_update>\n",
|
|
|
|
last_update
|
|
|
|
);
|
|
|
|
}
|
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-05-18 22:47:08 +00:00
|
|
|
SCOPE_MSG_LOG scope_messages(log_messages, CLIENT_MSG_LOG::DEBUG_STATE);
|
|
|
|
|
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;
|
|
|
|
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;
|
2004-05-18 22:47:08 +00:00
|
|
|
else scope_messages.printf("TIME_STATS::parse(): unrecognized: %s\n", buf);
|
2002-04-30 22:22:54 +00:00
|
|
|
}
|
|
|
|
return ERR_XML_PARSE;
|
|
|
|
}
|
2004-12-08 00:40:19 +00:00
|
|
|
|
2005-01-02 18:29:53 +00:00
|
|
|
const char *BOINC_RCSID_472504d8c2 = "$Id$";
|