2005-01-20 23:22:22 +00:00
|
|
|
// Berkeley Open Infrastructure for Network Computing
|
|
|
|
// http://boinc.berkeley.edu
|
|
|
|
// Copyright (C) 2005 University of California
|
2004-07-13 13:54:09 +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.
|
2004-07-13 13:54:09 +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
|
|
|
|
2003-02-26 00:47:57 +00:00
|
|
|
// NET_STATS estimates average network throughput,
|
|
|
|
// i.e. the average total throughput in both the up and down directions.
|
|
|
|
// Here's how it works: NET_STATS::poll() is called every second or so.
|
|
|
|
// If there are any file transfers active,
|
|
|
|
// it increments elapsed time and byte counts,
|
|
|
|
// and maintains an exponential average of throughput.
|
|
|
|
|
2003-10-16 19:03:49 +00:00
|
|
|
#include "cpp.h"
|
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 <cstring>
|
|
|
|
#include <cmath>
|
2004-03-04 11:41:43 +00:00
|
|
|
#endif
|
2003-02-26 01:04:42 +00:00
|
|
|
|
2002-04-30 22:22:54 +00:00
|
|
|
#include "parse.h"
|
|
|
|
#include "time.h"
|
|
|
|
|
2003-02-26 00:47:57 +00:00
|
|
|
#include "util.h"
|
2002-07-11 01:09:53 +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"
|
2003-07-03 05:01:29 +00:00
|
|
|
|
2002-04-30 22:22:54 +00:00
|
|
|
#include "net_stats.h"
|
|
|
|
|
2003-02-26 00:47:57 +00:00
|
|
|
#define EXP_DECAY_RATE (1./SECONDS_PER_DAY)
|
2002-04-30 22:22:54 +00:00
|
|
|
|
|
|
|
NET_STATS::NET_STATS() {
|
2003-02-26 00:47:57 +00:00
|
|
|
last_time = 0;
|
|
|
|
memset(&up, 0, sizeof(up));
|
|
|
|
memset(&down, 0, sizeof(down));
|
2002-04-30 22:22:54 +00:00
|
|
|
}
|
|
|
|
|
2003-02-26 00:47:57 +00:00
|
|
|
void NET_INFO::update(double dt, double nb, bool active) {
|
|
|
|
if (active) {
|
|
|
|
delta_t += dt;
|
|
|
|
delta_nbytes += nb-last_bytes;
|
|
|
|
}
|
|
|
|
last_bytes = nb;
|
|
|
|
}
|
2002-04-30 22:22:54 +00:00
|
|
|
|
2003-02-26 00:47:57 +00:00
|
|
|
double NET_INFO::throughput() {
|
|
|
|
double x, tp;
|
|
|
|
if (starting_throughput > 0) {
|
|
|
|
if (delta_t > 0) {
|
2005-06-09 05:43:35 +00:00
|
|
|
x = exp(-delta_t*EXP_DECAY_RATE);
|
2003-02-26 00:47:57 +00:00
|
|
|
tp = delta_nbytes/delta_t;
|
|
|
|
return x*starting_throughput + (1-x)*tp;
|
2002-04-30 22:22:54 +00:00
|
|
|
} else {
|
2003-02-26 00:47:57 +00:00
|
|
|
return starting_throughput;
|
2002-04-30 22:22:54 +00:00
|
|
|
}
|
2003-02-26 00:47:57 +00:00
|
|
|
} else if (delta_t > 0) {
|
|
|
|
return delta_nbytes/delta_t;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void NET_STATS::poll(NET_XFER_SET& nxs) {
|
2005-06-07 19:22:50 +00:00
|
|
|
double dt;
|
2003-02-26 00:47:57 +00:00
|
|
|
bool upload_active, download_active;
|
|
|
|
|
|
|
|
if (last_time == 0) {
|
|
|
|
dt = 0;
|
2002-04-30 22:22:54 +00:00
|
|
|
} else {
|
2005-06-07 19:22:50 +00:00
|
|
|
dt = gstate.now - last_time;
|
2002-04-30 22:22:54 +00:00
|
|
|
}
|
2005-06-07 19:22:50 +00:00
|
|
|
last_time = gstate.now;
|
2003-02-26 00:47:57 +00:00
|
|
|
|
|
|
|
nxs.check_active(upload_active, download_active);
|
|
|
|
up.update(dt, nxs.bytes_up, upload_active);
|
|
|
|
down.update(dt, nxs.bytes_down, download_active);
|
2002-04-30 22:22:54 +00:00
|
|
|
}
|
|
|
|
|
2002-07-15 23:21:20 +00:00
|
|
|
// Write XML based network statistics
|
|
|
|
//
|
2004-09-22 21:08:26 +00:00
|
|
|
int NET_STATS::write(MIOFILE& out) {
|
2004-06-12 04:45:36 +00:00
|
|
|
out.printf(
|
2002-04-30 22:22:54 +00:00
|
|
|
"<net_stats>\n"
|
2004-11-24 07:02:01 +00:00
|
|
|
" <bwup>%g</bwup>\n"
|
|
|
|
" <bwdown>%g</bwdown>\n"
|
2003-02-26 00:47:57 +00:00
|
|
|
"</net_stats>\n",
|
|
|
|
up.throughput(),
|
|
|
|
down.throughput()
|
2002-04-30 22:22:54 +00:00
|
|
|
);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2002-07-15 23:21:20 +00:00
|
|
|
// Read XML based network statistics
|
|
|
|
//
|
2004-06-12 04:45:36 +00:00
|
|
|
int NET_STATS::parse(MIOFILE& in) {
|
2002-04-30 22:22:54 +00:00
|
|
|
char buf[256];
|
2003-02-26 00:47:57 +00:00
|
|
|
double bwup, bwdown;
|
2002-08-15 22:03:41 +00:00
|
|
|
|
2002-04-30 22:22:54 +00:00
|
|
|
memset(this, 0, sizeof(NET_STATS));
|
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, "</net_stats>")) return 0;
|
2003-02-26 00:47:57 +00:00
|
|
|
else if (parse_double(buf, "<bwup>", bwup)) {
|
|
|
|
up.starting_throughput = bwup;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else if (parse_double(buf, "<bwdown>", bwdown)) {
|
|
|
|
down.starting_throughput = bwdown;
|
|
|
|
continue;
|
|
|
|
}
|
2003-07-03 05:01:29 +00:00
|
|
|
else msg_printf(NULL, MSG_ERROR, "NET_STATS::parse(): unrecognized: %s\n", buf);
|
2002-04-30 22:22:54 +00:00
|
|
|
}
|
2004-01-30 22:19:19 +00:00
|
|
|
return ERR_XML_PARSE;
|
2002-04-30 22:22:54 +00:00
|
|
|
}
|
2004-12-08 00:40:19 +00:00
|
|
|
|
2005-01-02 18:29:53 +00:00
|
|
|
const char *BOINC_RCSID_733b4006f5 = "$Id$";
|