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):
|
|
|
|
//
|
|
|
|
|
|
|
|
// update_stats: update exponential average credit for users and hosts,
|
|
|
|
// and calculate total users/credit for teams
|
|
|
|
//
|
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
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#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"
|
|
|
|
#include "config.h"
|
|
|
|
#include "sched_util.h"
|
|
|
|
|
|
|
|
#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
|
|
|
|
|
|
|
int update_users() {
|
2003-06-04 17:21:26 +00:00
|
|
|
DB_USER user;
|
2003-03-08 00:09:40 +00:00
|
|
|
int retval;
|
|
|
|
|
2003-06-04 17:21:26 +00:00
|
|
|
while (!user.enumerate()) {
|
2003-03-08 00:09:40 +00:00
|
|
|
update_average(0, 0, user.expavg_credit, user.expavg_time);
|
2003-06-04 17:21:26 +00:00
|
|
|
retval = user.update();
|
2003-03-08 00:09:40 +00:00
|
|
|
if (retval) return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
2003-06-04 17:21:26 +00:00
|
|
|
while (!host.enumerate()) {
|
2003-03-08 00:09:40 +00:00
|
|
|
update_average(0, 0, host.expavg_credit, host.expavg_time);
|
2003-06-04 17:21:26 +00:00
|
|
|
retval = host.update();
|
2003-03-08 00:09:40 +00:00
|
|
|
if (retval) return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2003-06-04 17:21:26 +00:00
|
|
|
int get_team_credit(TEAM& team) {
|
2003-03-08 23:50:21 +00:00
|
|
|
int nusers;
|
|
|
|
double expavg_credit, total_credit;
|
|
|
|
int retval;
|
2003-06-04 17:21:26 +00:00
|
|
|
DB_USER user;
|
|
|
|
char buf[256];
|
2003-03-08 23:50:21 +00:00
|
|
|
|
|
|
|
// count the number of users on a 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;
|
|
|
|
|
|
|
|
// get the summed credit values for a team
|
2003-06-04 17:21:26 +00:00
|
|
|
sprintf(buf, "where teamid=%d", team.id);
|
|
|
|
retval = user.sum(expavg_credit, "expavg_credit", buf);
|
2003-03-08 23:50:21 +00:00
|
|
|
if (retval) return retval;
|
2003-06-04 17:21:26 +00:00
|
|
|
retval = user.sum(total_credit, "total_credit", buf);
|
2003-03-08 23:50:21 +00:00
|
|
|
if (retval) return retval;
|
|
|
|
|
2003-06-04 17:21:26 +00:00
|
|
|
team.nusers = nusers;
|
|
|
|
team.total_credit = total_credit;
|
|
|
|
team.expavg_credit = expavg_credit;
|
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;
|
|
|
|
|
2003-06-04 17:21:26 +00:00
|
|
|
while (!team.enumerate()) {
|
2003-03-08 23:50:21 +00:00
|
|
|
retval = get_team_credit(team);
|
2003-08-08 00:43:20 +00:00
|
|
|
if (retval) {
|
|
|
|
log_messages.printf(
|
|
|
|
SchedMessages::CRITICAL,
|
|
|
|
"update_teams: get_team_credit([TEAM#%d]) failed: %d\n",
|
|
|
|
team.id,
|
|
|
|
retval);
|
2003-08-08 00:57:01 +00:00
|
|
|
//return retval;
|
|
|
|
continue;
|
2003-08-08 00:43:20 +00:00
|
|
|
}
|
2003-03-08 23:50:21 +00:00
|
|
|
|
|
|
|
// update the team record
|
2003-06-04 17:21:26 +00:00
|
|
|
retval = team.update();
|
2003-03-08 23:50:21 +00:00
|
|
|
if (retval) return retval;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2003-03-08 00:09:40 +00:00
|
|
|
int main(int argc, char** argv) {
|
|
|
|
CONFIG config;
|
|
|
|
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
|
|
|
|
|
|
|
check_stop_trigger();
|
|
|
|
|
|
|
|
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 {
|
2003-07-02 02:02:18 +00:00
|
|
|
log_messages.printf(SchedMessages::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)) {
|
|
|
|
// log_messages.printf(SchedMessages::NORMAL, "Another copy of update_stats is already running\n");
|
|
|
|
// exit(1);
|
|
|
|
// }
|
|
|
|
// write_pid_file(PIDFILE);
|
2003-07-02 02:02:18 +00:00
|
|
|
log_messages.printf(SchedMessages::NORMAL, "Starting\n");
|
2003-07-01 00:39:54 +00:00
|
|
|
|
2003-03-08 00:09:40 +00:00
|
|
|
|
|
|
|
retval = config.parse_file();
|
|
|
|
if (retval) {
|
2003-07-02 02:02:18 +00:00
|
|
|
log_messages.printf(SchedMessages::CRITICAL, "Can't parse config file\n");
|
2003-03-08 00:09:40 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
retval = boinc_db_open(config.db_name, config.db_passwd);
|
|
|
|
if (retval) {
|
2003-07-02 02:02:18 +00:00
|
|
|
log_messages.printf(SchedMessages::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) {
|
2003-07-02 02:02:18 +00:00
|
|
|
log_messages.printf(SchedMessages::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) {
|
2003-07-02 02:02:18 +00:00
|
|
|
log_messages.printf(SchedMessages::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) {
|
2003-07-02 02:02:18 +00:00
|
|
|
log_messages.printf(SchedMessages::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;
|
|
|
|
}
|