2003-07-01 20:37:09 +00:00
|
|
|
// The contents of this file are subject to the BOINC Public License
|
2003-03-08 00:09:40 +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-07-01 20:37:09 +00:00
|
|
|
// http://boinc.berkeley.edu/license_1.0.txt
|
2003-07-08 21:30:47 +00:00
|
|
|
//
|
2003-03-08 00:09:40 +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-07-08 21:30:47 +00:00
|
|
|
// under the License.
|
|
|
|
//
|
|
|
|
// The Original Code is the Berkeley Open Infrastructure for Network Computing.
|
|
|
|
//
|
2003-03-08 00:09:40 +00:00
|
|
|
// The Initial Developer of the Original Code is the SETI@home project.
|
2003-07-01 20:37:09 +00:00
|
|
|
// Portions created by the SETI@home project are Copyright (C) 2002
|
2003-07-08 21:30:47 +00:00
|
|
|
// University of California at Berkeley. All Rights Reserved.
|
|
|
|
//
|
2003-03-08 00:09:40 +00:00
|
|
|
// Contributor(s):
|
|
|
|
//
|
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
|
|
|
//
|
2003-03-08 23:50:21 +00:00
|
|
|
// usage: update_stats [-update_teams] [-update_users] [-update_hosts] [-asynch]
|
2003-03-08 00:09:40 +00:00
|
|
|
|
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"
|
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
|
|
|
|
|
|
|
#define LOCKFILE "update_stats.out"
|
2003-06-20 01:31:03 +00:00
|
|
|
#define PIDFILE "update_stats.pid"
|
2003-03-08 00:09:40 +00:00
|
|
|
|
2004-01-14 20:24:24 +00:00
|
|
|
#define UPDATE_INTERVAL 3600*24*4;
|
|
|
|
|
|
|
|
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
|
|
|
|
2003-06-04 17:21:26 +00:00
|
|
|
while (!user.enumerate()) {
|
2004-01-14 20:24:24 +00:00
|
|
|
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);
|
2004-10-04 23:59:51 +00:00
|
|
|
sprintf(
|
|
|
|
buf,"expavg_credit=%f, expavg_time=%f",
|
|
|
|
user.expavg_credit, user.expavg_time
|
|
|
|
);
|
|
|
|
retval = user.update_field(buf);
|
2003-12-26 06:03:03 +00:00
|
|
|
if (retval) {
|
2004-04-08 08:15:23 +00:00
|
|
|
log_messages.printf(SCHED_MSG_LOG::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
|
|
|
|
2003-06-04 17:21:26 +00:00
|
|
|
while (!host.enumerate()) {
|
2004-01-14 20:24:24 +00:00
|
|
|
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) {
|
2004-04-08 08:15:23 +00:00
|
|
|
log_messages.printf(SCHED_MSG_LOG::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;
|
|
|
|
|
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
|
|
|
|
2003-06-04 17:21:26 +00:00
|
|
|
while (!team.enumerate()) {
|
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(
|
2004-04-08 08:15:23 +00:00
|
|
|
SCHED_MSG_LOG::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
|
|
|
}
|
2004-01-14 20:24:24 +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(
|
|
|
|
buf, "expavg_credit=%f, expavg_time=%f",
|
|
|
|
team.expavg_credit, team.expavg_time
|
|
|
|
);
|
|
|
|
retval = team.update_field(buf);
|
2003-12-26 06:03:03 +00:00
|
|
|
if (retval) {
|
2004-04-08 08:15:23 +00:00
|
|
|
log_messages.printf(SCHED_MSG_LOG::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;
|
|
|
|
bool do_update_hosts = false, asynch = 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 if (!strcmp(argv[i], "-asynch")) {
|
|
|
|
asynch = true;
|
|
|
|
} else {
|
2004-04-08 08:15:23 +00:00
|
|
|
log_messages.printf(SCHED_MSG_LOG::CRITICAL, "Unrecognized arg: %s\n", argv[i]);
|
2003-03-08 23:50:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (asynch) {
|
|
|
|
if (fork()) {
|
|
|
|
exit(0);
|
2003-03-08 00:09:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-07-08 21:30:47 +00:00
|
|
|
// // Call lock_file after fork(), because file locks are not always inherited
|
|
|
|
// if (lock_file(LOCKFILE)) {
|
2004-04-08 08:15:23 +00:00
|
|
|
// log_messages.printf(SCHED_MSG_LOG::NORMAL, "Another copy of update_stats is already running\n");
|
2003-07-08 21:30:47 +00:00
|
|
|
// exit(1);
|
|
|
|
// }
|
|
|
|
// write_pid_file(PIDFILE);
|
2004-04-08 08:15:23 +00:00
|
|
|
log_messages.printf(SCHED_MSG_LOG::NORMAL, "Starting\n");
|
2003-07-01 00:39:54 +00:00
|
|
|
|
2003-03-08 00:09:40 +00:00
|
|
|
|
2003-09-02 21:16:55 +00:00
|
|
|
retval = config.parse_file("..");
|
2003-03-08 00:09:40 +00:00
|
|
|
if (retval) {
|
2004-04-08 08:15:23 +00:00
|
|
|
log_messages.printf(SCHED_MSG_LOG::CRITICAL, "Can't parse config file\n");
|
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) {
|
2004-04-08 08:15:23 +00:00
|
|
|
log_messages.printf(SCHED_MSG_LOG::CRITICAL, "Can't open DB\n");
|
2003-03-08 00:09:40 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (do_update_users) {
|
|
|
|
retval = update_users();
|
|
|
|
if (retval) {
|
2004-04-08 08:15:23 +00:00
|
|
|
log_messages.printf(SCHED_MSG_LOG::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) {
|
2004-04-08 08:15:23 +00:00
|
|
|
log_messages.printf(SCHED_MSG_LOG::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) {
|
2004-04-08 08:15:23 +00:00
|
|
|
log_messages.printf(SCHED_MSG_LOG::CRITICAL, "update_teams failed: %d\n", retval);
|
2003-03-08 23:50:21 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-03-08 00:09:40 +00:00
|
|
|
return 0;
|
|
|
|
}
|