2002-04-30 22:22:54 +00:00
|
|
|
// The contents of this file are subject to the Mozilla Public License
|
|
|
|
// 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
|
|
|
|
// http://www.mozilla.org/MPL/
|
|
|
|
//
|
|
|
|
// 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
|
|
|
|
// under the License.
|
|
|
|
//
|
|
|
|
// The Original Code is the Berkeley Open Infrastructure for Network Computing.
|
|
|
|
//
|
|
|
|
// The Initial Developer of the Original Code is the SETI@home project.
|
|
|
|
// Portions created by the SETI@home project are Copyright (C) 2002
|
|
|
|
// University of California at Berkeley. All Rights Reserved.
|
|
|
|
//
|
|
|
|
// Contributor(s):
|
|
|
|
//
|
|
|
|
|
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.
|
|
|
|
|
2002-04-30 22:22:54 +00:00
|
|
|
#include <string.h>
|
|
|
|
|
2003-02-26 01:04:42 +00:00
|
|
|
#include "windows_cpp.h"
|
|
|
|
|
2002-04-30 22:22:54 +00:00
|
|
|
#include "math.h"
|
|
|
|
#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"
|
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) {
|
|
|
|
x = exp(delta_t*EXP_DECAY_RATE);
|
|
|
|
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) {
|
|
|
|
double t, dt;
|
|
|
|
bool upload_active, download_active;
|
|
|
|
|
|
|
|
t = dtime();
|
|
|
|
if (last_time == 0) {
|
|
|
|
dt = 0;
|
2002-04-30 22:22:54 +00:00
|
|
|
} else {
|
2003-02-26 00:47:57 +00:00
|
|
|
dt = t - last_time;
|
2002-04-30 22:22:54 +00:00
|
|
|
}
|
2003-02-26 00:47:57 +00:00
|
|
|
last_time = t;
|
|
|
|
|
|
|
|
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
|
|
|
|
//
|
2002-04-30 22:22:54 +00:00
|
|
|
int NET_STATS::write(FILE* out, bool to_server) {
|
|
|
|
fprintf(out,
|
|
|
|
"<net_stats>\n"
|
|
|
|
" <bwup>%f</bwup>\n"
|
2003-02-26 00:47:57 +00:00
|
|
|
" <bwdown>%f</bwdown>\n"
|
|
|
|
"</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
|
|
|
|
//
|
2002-04-30 22:22:54 +00:00
|
|
|
int NET_STATS::parse(FILE* in) {
|
|
|
|
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));
|
|
|
|
while (fgets(buf, 256, in)) {
|
|
|
|
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;
|
|
|
|
}
|
2002-04-30 22:22:54 +00:00
|
|
|
else fprintf(stderr, "NET_STATS::parse(): unrecognized: %s\n", buf);
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|