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-07-08 21:30:47 +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-07-08 21:30:47 +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.
|
2003-03-08 00:09:40 +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.,
|
2007-10-09 11:35:47 +00:00
|
|
|
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
2005-01-20 23:22:22 +00:00
|
|
|
|
2004-01-14 20:24:24 +00:00
|
|
|
// update_stats:
|
|
|
|
// Update average credit for idle users, hosts and teams.
|
|
|
|
// These fields are updates as new credit is granted;
|
|
|
|
// the purpose of this program is to decay credit of entities
|
|
|
|
// that are inactive for long periods.
|
|
|
|
// Hence it should be run about once a day at most.
|
|
|
|
//
|
|
|
|
// Also updates the nusers field of teams
|
2003-03-08 00:09:40 +00:00
|
|
|
//
|
2007-04-18 20:49:58 +00:00
|
|
|
// usage: update_stats [-update_teams] [-update_users] [-update_hosts]
|
2005-11-21 18:34:44 +00:00
|
|
|
#include "config.h"
|
2004-07-13 13:54:09 +00:00
|
|
|
#include <cstdio>
|
|
|
|
#include <cstring>
|
|
|
|
#include <cstdlib>
|
2003-03-08 00:09:40 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
|
2003-04-07 19:06:00 +00:00
|
|
|
#include "boinc_db.h"
|
2003-03-08 00:09:40 +00:00
|
|
|
#include "util.h"
|
2007-05-11 16:30:13 +00:00
|
|
|
#include "str_util.h"
|
|
|
|
|
2003-08-15 00:45:25 +00:00
|
|
|
#include "sched_config.h"
|
2003-03-08 00:09:40 +00:00
|
|
|
#include "sched_util.h"
|
2004-04-08 08:15:23 +00:00
|
|
|
#include "sched_msgs.h"
|
2003-03-08 00:09:40 +00:00
|
|
|
|
2005-12-17 17:20:05 +00:00
|
|
|
#ifdef EINSTEIN_AT_HOME
|
|
|
|
#define UPDATE_INTERVAL 3600*24;
|
|
|
|
#else
|
2004-01-14 20:24:24 +00:00
|
|
|
#define UPDATE_INTERVAL 3600*24*4;
|
2005-12-17 17:20:05 +00:00
|
|
|
#endif
|
2004-01-14 20:24:24 +00:00
|
|
|
|
|
|
|
double update_time_cutoff;
|
|
|
|
|
2003-03-08 00:09:40 +00:00
|
|
|
int update_users() {
|
2003-06-04 17:21:26 +00:00
|
|
|
DB_USER user;
|
2003-03-08 00:09:40 +00:00
|
|
|
int retval;
|
2004-10-04 23:59:51 +00:00
|
|
|
char buf[256];
|
2003-03-08 00:09:40 +00:00
|
|
|
|
2006-06-21 19:48:41 +00:00
|
|
|
while (!user.enumerate("where expavg_credit>0.1")) {
|
|
|
|
if (user.expavg_time > update_time_cutoff) continue;
|
2004-04-07 06:51:42 +00:00
|
|
|
update_average(0, 0, CREDIT_HALF_LIFE, user.expavg_credit, user.expavg_time);
|
2006-06-21 19:48:41 +00:00
|
|
|
sprintf( buf, "expavg_credit=%f, expavg_time=%f",
|
2004-10-04 23:59:51 +00:00
|
|
|
user.expavg_credit, user.expavg_time
|
|
|
|
);
|
|
|
|
retval = user.update_field(buf);
|
2003-12-26 06:03:03 +00:00
|
|
|
if (retval) {
|
2005-09-26 23:28:48 +00:00
|
|
|
log_messages.printf(SCHED_MSG_LOG::MSG_CRITICAL, "Can't update user %d\n", user.id);
|
2003-12-26 06:03:03 +00:00
|
|
|
return retval;
|
|
|
|
}
|
2003-03-08 00:09:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int update_hosts() {
|
2003-06-04 17:21:26 +00:00
|
|
|
DB_HOST host;
|
2003-03-08 00:09:40 +00:00
|
|
|
int retval;
|
2004-10-04 23:59:51 +00:00
|
|
|
char buf[256];
|
2003-03-08 00:09:40 +00:00
|
|
|
|
2006-06-21 19:48:41 +00:00
|
|
|
while (!host.enumerate("where expavg_credit>0.1")) {
|
|
|
|
if (host.expavg_time > update_time_cutoff) continue;
|
2004-04-07 06:51:42 +00:00
|
|
|
update_average(0, 0, CREDIT_HALF_LIFE, host.expavg_credit, host.expavg_time);
|
2004-10-04 23:59:51 +00:00
|
|
|
sprintf(
|
|
|
|
buf,"expavg_credit=%f, expavg_time=%f",
|
|
|
|
host.expavg_credit, host.expavg_time
|
|
|
|
);
|
|
|
|
retval = host.update_field(buf);
|
2003-12-26 06:03:03 +00:00
|
|
|
if (retval) {
|
2005-09-26 23:28:48 +00:00
|
|
|
log_messages.printf(SCHED_MSG_LOG::MSG_CRITICAL, "Can't update host %d\n", host.id);
|
2003-12-26 06:03:03 +00:00
|
|
|
return retval;
|
|
|
|
}
|
2003-03-08 00:09:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2004-01-14 20:24:24 +00:00
|
|
|
int get_team_totals(TEAM& team) {
|
2003-03-08 23:50:21 +00:00
|
|
|
int nusers;
|
|
|
|
int retval;
|
2003-06-04 17:21:26 +00:00
|
|
|
DB_USER user;
|
|
|
|
char buf[256];
|
2003-03-08 23:50:21 +00:00
|
|
|
|
2003-08-12 20:58:24 +00:00
|
|
|
// count the number of users on the team
|
2003-06-04 17:21:26 +00:00
|
|
|
//
|
|
|
|
sprintf(buf, "where teamid=%d", team.id);
|
|
|
|
retval = user.count(nusers, buf);
|
2003-03-08 23:50:21 +00:00
|
|
|
if (retval) return retval;
|
|
|
|
|
2006-01-22 16:35:08 +00:00
|
|
|
if (team.nusers != nusers) {
|
|
|
|
log_messages.printf(
|
|
|
|
SCHED_MSG_LOG::MSG_CRITICAL,
|
|
|
|
"updating member count for [TEAM#%d]: database has %d users, count shows %d\n",
|
|
|
|
team.id, team.nusers, nusers
|
|
|
|
);
|
|
|
|
}
|
2003-06-04 17:21:26 +00:00
|
|
|
team.nusers = nusers;
|
2003-03-08 23:50:21 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// fill in the nusers, total_credit and expavg_credit fields
|
|
|
|
// of the team table.
|
|
|
|
// This may take a while; don't do it often
|
|
|
|
//
|
|
|
|
int update_teams() {
|
2003-06-04 17:21:26 +00:00
|
|
|
DB_TEAM team;
|
2003-03-08 23:50:21 +00:00
|
|
|
int retval;
|
2004-10-04 23:59:51 +00:00
|
|
|
char buf[256];
|
2003-03-08 23:50:21 +00:00
|
|
|
|
2006-06-21 19:48:41 +00:00
|
|
|
while (!team.enumerate("where expavg_credit>0.1")) {
|
2004-01-14 20:24:24 +00:00
|
|
|
retval = get_team_totals(team);
|
2003-08-08 00:43:20 +00:00
|
|
|
if (retval) {
|
|
|
|
log_messages.printf(
|
2005-09-26 23:28:48 +00:00
|
|
|
SCHED_MSG_LOG::MSG_CRITICAL,
|
2003-08-08 00:43:20 +00:00
|
|
|
"update_teams: get_team_credit([TEAM#%d]) failed: %d\n",
|
|
|
|
team.id,
|
2004-01-14 20:24:24 +00:00
|
|
|
retval
|
|
|
|
);
|
2003-08-08 00:57:01 +00:00
|
|
|
continue;
|
2003-08-08 00:43:20 +00:00
|
|
|
}
|
2006-06-21 19:48:41 +00:00
|
|
|
if (team.expavg_time < update_time_cutoff) {
|
2004-04-07 06:51:42 +00:00
|
|
|
update_average(0, 0, CREDIT_HALF_LIFE, team.expavg_credit, team.expavg_time);
|
2004-01-14 20:24:24 +00:00
|
|
|
}
|
2004-10-04 23:59:51 +00:00
|
|
|
sprintf(
|
2006-01-22 16:35:08 +00:00
|
|
|
buf, "expavg_credit=%f, expavg_time=%f, nusers=%d",
|
|
|
|
team.expavg_credit, team.expavg_time, team.nusers
|
2004-10-04 23:59:51 +00:00
|
|
|
);
|
|
|
|
retval = team.update_field(buf);
|
2003-12-26 06:03:03 +00:00
|
|
|
if (retval) {
|
2005-09-26 23:28:48 +00:00
|
|
|
log_messages.printf(SCHED_MSG_LOG::MSG_CRITICAL, "Can't update team %d\n", team.id);
|
2003-12-26 06:03:03 +00:00
|
|
|
return retval;
|
|
|
|
}
|
2003-03-08 23:50:21 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2003-03-08 00:09:40 +00:00
|
|
|
int main(int argc, char** argv) {
|
2003-09-02 21:16:55 +00:00
|
|
|
SCHED_CONFIG config;
|
2003-03-08 00:09:40 +00:00
|
|
|
int retval, i;
|
2003-03-08 23:50:21 +00:00
|
|
|
bool do_update_teams = false, do_update_users = false;
|
2007-04-18 20:49:58 +00:00
|
|
|
bool do_update_hosts = false;
|
2003-03-08 00:09:40 +00:00
|
|
|
|
2004-01-14 20:24:24 +00:00
|
|
|
update_time_cutoff = time(0) - UPDATE_INTERVAL;
|
|
|
|
|
2004-05-03 19:30:01 +00:00
|
|
|
check_stop_daemons();
|
2003-03-08 00:09:40 +00:00
|
|
|
|
|
|
|
for (i=1; i<argc; i++) {
|
|
|
|
if (!strcmp(argv[i], "-update_teams")) {
|
|
|
|
do_update_teams = true;
|
|
|
|
} else if (!strcmp(argv[i], "-update_users")) {
|
|
|
|
do_update_users = true;
|
|
|
|
} else if (!strcmp(argv[i], "-update_hosts")) {
|
|
|
|
do_update_hosts = true;
|
2003-06-11 23:36:40 +00:00
|
|
|
} else if (!strcmp(argv[i], "-d")) {
|
2003-07-02 02:02:18 +00:00
|
|
|
log_messages.set_debug_level(atoi(argv[++i]));
|
2003-03-08 23:50:21 +00:00
|
|
|
} else {
|
2005-09-26 23:28:48 +00:00
|
|
|
log_messages.printf(SCHED_MSG_LOG::MSG_CRITICAL, "Unrecognized arg: %s\n", argv[i]);
|
2003-03-08 23:50:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-09-26 23:28:48 +00:00
|
|
|
log_messages.printf(SCHED_MSG_LOG::MSG_NORMAL, "Starting\n");
|
2003-07-01 00:39:54 +00:00
|
|
|
|
2003-09-02 21:16:55 +00:00
|
|
|
retval = config.parse_file("..");
|
2003-03-08 00:09:40 +00:00
|
|
|
if (retval) {
|
2007-05-11 16:30:13 +00:00
|
|
|
log_messages.printf(SCHED_MSG_LOG::MSG_CRITICAL,
|
|
|
|
"Can't parse ../config.xml: %s\n", boincerror(retval)
|
|
|
|
);
|
2003-03-08 00:09:40 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
2004-01-15 23:53:13 +00:00
|
|
|
retval = boinc_db.open(config.db_name, config.db_host, config.db_user, config.db_passwd);
|
2003-03-08 00:09:40 +00:00
|
|
|
if (retval) {
|
2005-09-26 23:28:48 +00:00
|
|
|
log_messages.printf(SCHED_MSG_LOG::MSG_CRITICAL, "Can't open DB\n");
|
2003-03-08 00:09:40 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
2007-02-13 21:41:07 +00:00
|
|
|
retval = boinc_db.set_isolation_level(READ_UNCOMMITTED);
|
|
|
|
if (retval) {
|
|
|
|
log_messages.printf(SCHED_MSG_LOG::MSG_CRITICAL,
|
|
|
|
"boinc_db.set_isolation_level: %d; %s\n", retval, boinc_db.error_string()
|
|
|
|
);
|
|
|
|
}
|
2003-03-08 00:09:40 +00:00
|
|
|
|
|
|
|
if (do_update_users) {
|
|
|
|
retval = update_users();
|
|
|
|
if (retval) {
|
2005-09-26 23:28:48 +00:00
|
|
|
log_messages.printf(SCHED_MSG_LOG::MSG_CRITICAL, "update_users failed: %d\n", retval);
|
2003-03-08 00:09:40 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (do_update_hosts) {
|
|
|
|
retval = update_hosts();
|
|
|
|
if (retval) {
|
2005-09-26 23:28:48 +00:00
|
|
|
log_messages.printf(SCHED_MSG_LOG::MSG_CRITICAL, "update_hosts failed: %d\n", retval);
|
2003-03-08 00:09:40 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-03-08 23:50:21 +00:00
|
|
|
if (do_update_teams) {
|
|
|
|
retval = update_teams();
|
|
|
|
if (retval) {
|
2005-09-26 23:28:48 +00:00
|
|
|
log_messages.printf(SCHED_MSG_LOG::MSG_CRITICAL, "update_teams failed: %d\n", retval);
|
2003-03-08 23:50:21 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-06-19 17:40:53 +00:00
|
|
|
log_messages.printf(SCHED_MSG_LOG::MSG_NORMAL, "Finished\n");
|
2003-03-08 00:09:40 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2004-12-08 00:40:19 +00:00
|
|
|
|
2005-01-02 18:29:53 +00:00
|
|
|
const char *BOINC_RCSID_6b05e9ecce = "$Id$";
|