2002-05-30 07:14:21 +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):
|
|
|
|
//
|
|
|
|
|
2002-12-03 18:57:40 +00:00
|
|
|
// feeder [-asynch]
|
2002-05-30 07:14:21 +00:00
|
|
|
//
|
|
|
|
// Creates a shared memory segment containing DB info,
|
|
|
|
// including results/workunits to send.
|
|
|
|
// This means that the scheduler CGI program doesn't have to
|
|
|
|
// access the DB to get this info.
|
2002-06-01 20:26:21 +00:00
|
|
|
//
|
|
|
|
// -asynch fork and run in a separate process
|
2002-05-30 07:14:21 +00:00
|
|
|
|
|
|
|
// TODO:
|
|
|
|
// - check for wu/results that don't get sent for a long time;
|
|
|
|
// generate a warning message
|
|
|
|
|
2002-12-03 18:57:40 +00:00
|
|
|
// Trigger files:
|
|
|
|
// The feeder program periodically checks for two trigger files:
|
2002-05-30 07:14:21 +00:00
|
|
|
//
|
2002-12-03 18:57:40 +00:00
|
|
|
// stop_server: destroy shmem and exit
|
|
|
|
// leave trigger file there (for other daemons)
|
|
|
|
// reread_db: update DB contents in existing shmem
|
|
|
|
// delete trigger file
|
2002-05-30 07:14:21 +00:00
|
|
|
|
2002-11-06 19:42:23 +00:00
|
|
|
// If you get an "Invalid argument" error when trying to run the feeder,
|
|
|
|
// it is likely that you aren't able to allocate enough shared memory.
|
|
|
|
// Either increase the maximum shared memory segment size in the kernel
|
|
|
|
// configuration, or decrease the MAX_PLATFORMS, MAX_APPS
|
|
|
|
// MAX_APP_VERSIONS, and MAX_WU_RESULTS in sched_shmem.h
|
|
|
|
|
2002-05-30 07:14:21 +00:00
|
|
|
#include <stdio.h>
|
2002-07-11 20:41:24 +00:00
|
|
|
#include <stdlib.h>
|
2002-09-26 23:12:13 +00:00
|
|
|
#include <string.h>
|
2002-05-30 08:33:30 +00:00
|
|
|
#if HAVE_UNISTD_H
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
2002-05-30 07:14:21 +00:00
|
|
|
|
|
|
|
#include "db.h"
|
|
|
|
#include "shmem.h"
|
2002-10-03 18:33:46 +00:00
|
|
|
#include "config.h"
|
2002-05-30 07:14:21 +00:00
|
|
|
#include "sched_shmem.h"
|
|
|
|
|
|
|
|
#define RESULTS_PER_ENUM 100
|
2002-12-03 18:57:40 +00:00
|
|
|
#define STOP_SERVER_FILENAME "stop_server"
|
|
|
|
#define REREAD_DB_FILENAME "reread_db"
|
2002-05-30 07:14:21 +00:00
|
|
|
|
2002-10-03 18:33:46 +00:00
|
|
|
CONFIG config;
|
|
|
|
|
2002-12-03 18:57:40 +00:00
|
|
|
int check_triggers(SCHED_SHMEM* ssp) {
|
2002-05-30 07:14:21 +00:00
|
|
|
FILE* f;
|
2002-12-03 18:57:40 +00:00
|
|
|
|
|
|
|
f = fopen(STOP_SERVER_FILENAME, "r");
|
|
|
|
if (f) {
|
|
|
|
fclose(f);
|
2002-06-01 20:26:21 +00:00
|
|
|
detach_shmem((void*)ssp);
|
2002-10-03 18:33:46 +00:00
|
|
|
destroy_shmem(config.shmem_key);
|
2002-05-30 07:14:21 +00:00
|
|
|
exit(0);
|
2002-12-03 18:57:40 +00:00
|
|
|
}
|
|
|
|
f = fopen(REREAD_DB_FILENAME, "r");
|
|
|
|
if (f) {
|
|
|
|
fclose(f);
|
2002-05-30 07:14:21 +00:00
|
|
|
ssp->init();
|
|
|
|
ssp->scan_tables();
|
2002-12-03 18:57:40 +00:00
|
|
|
unlink(REREAD_DB_FILENAME);
|
2002-05-30 07:14:21 +00:00
|
|
|
}
|
2002-11-22 20:20:58 +00:00
|
|
|
|
2002-05-30 07:14:21 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2002-06-01 20:26:21 +00:00
|
|
|
// Try keep the wu_results array filled.
|
|
|
|
// This is actually a little tricky.
|
|
|
|
// We use an enumerator.
|
|
|
|
// The inner loop scans the wu_result table,
|
|
|
|
// looking for empty slots and trying to fill them in.
|
|
|
|
// When the enumerator reaches the end, it is restarted;
|
|
|
|
// hopefully there will be some new workunits.
|
|
|
|
// There are two complications:
|
|
|
|
// - An enumeration may return results already in the array.
|
|
|
|
// So, for each result, we scan the entire array to make sure
|
|
|
|
// it's not there already. Can this be streamlined?
|
|
|
|
// - We must avoid excessive re-enumeration,
|
|
|
|
// especially when the number of results is less than the array size.
|
|
|
|
// Crude approach: if a "collision" (as above) occurred on
|
|
|
|
// a pass through the array, wait a long time (60 sec)
|
|
|
|
//
|
|
|
|
void feeder_loop(SCHED_SHMEM* ssp) {
|
2002-05-30 07:14:21 +00:00
|
|
|
int i, j, nadditions, ncollisions, retval;
|
|
|
|
RESULT result;
|
|
|
|
WORKUNIT wu;
|
|
|
|
bool no_wus, collision, restarted_enum;
|
2002-12-03 18:57:40 +00:00
|
|
|
|
2002-05-30 07:14:21 +00:00
|
|
|
while (1) {
|
|
|
|
nadditions = 0;
|
|
|
|
ncollisions = 0;
|
|
|
|
no_wus = false;
|
|
|
|
restarted_enum = false;
|
|
|
|
for (i=0; i<ssp->nwu_results; i++) {
|
|
|
|
if (!ssp->wu_results[i].present) {
|
2002-11-07 19:31:34 +00:00
|
|
|
result.state = RESULT_STATE_UNSENT;
|
|
|
|
retval = db_result_enum_state(result, RESULTS_PER_ENUM);
|
2002-05-30 07:14:21 +00:00
|
|
|
if (retval) {
|
|
|
|
|
|
|
|
// if we already restarted the enum on this pass,
|
|
|
|
// there's no point in doing it again.
|
|
|
|
//
|
|
|
|
if (restarted_enum) {
|
|
|
|
printf("feeder: already restarted enum\n");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// restart the enumeration
|
|
|
|
//
|
|
|
|
restarted_enum = true;
|
2002-11-07 19:31:34 +00:00
|
|
|
result.state = RESULT_STATE_UNSENT;
|
|
|
|
retval = db_result_enum_state(result, RESULTS_PER_ENUM);
|
2002-05-30 07:14:21 +00:00
|
|
|
printf("feeder: restarting enumeration: %d\n", retval);
|
|
|
|
if (retval) {
|
2002-10-09 04:56:41 +00:00
|
|
|
printf("feeder: enumeration returned nothing\n");
|
2002-05-30 07:14:21 +00:00
|
|
|
no_wus = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
collision = false;
|
|
|
|
for (j=0; j<ssp->nwu_results; j++) {
|
|
|
|
if (ssp->wu_results[j].present
|
|
|
|
&& ssp->wu_results[j].result.id == result.id
|
|
|
|
) {
|
|
|
|
ncollisions++;
|
|
|
|
collision = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!collision) {
|
2002-10-09 04:56:41 +00:00
|
|
|
printf("feeder: adding result %d in slot %d\n", result.id, i);
|
2002-05-30 07:14:21 +00:00
|
|
|
retval = db_workunit(result.workunitid, wu);
|
2002-10-09 04:56:41 +00:00
|
|
|
if (retval) {
|
2002-10-21 22:45:54 +00:00
|
|
|
printf("feeder: can't read workunit %d: %d\n", result.workunitid, retval);
|
2002-10-09 04:56:41 +00:00
|
|
|
continue;
|
|
|
|
}
|
2002-05-30 07:14:21 +00:00
|
|
|
ssp->wu_results[i].result = result;
|
|
|
|
ssp->wu_results[i].workunit = wu;
|
|
|
|
ssp->wu_results[i].present = true;
|
|
|
|
nadditions++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (nadditions == 0) {
|
2002-10-09 04:56:41 +00:00
|
|
|
printf("feeder: no results added\n");
|
2002-05-30 07:14:21 +00:00
|
|
|
sleep(1);
|
|
|
|
} else {
|
|
|
|
printf("feeder: added %d results to array\n", nadditions);
|
|
|
|
}
|
|
|
|
if (no_wus) {
|
|
|
|
printf("feeder: no results available\n");
|
2002-06-01 20:26:21 +00:00
|
|
|
sleep(5);
|
2002-05-30 07:14:21 +00:00
|
|
|
}
|
|
|
|
if (ncollisions) {
|
|
|
|
printf("feeder: some results already in array - sleeping\n");
|
2002-06-01 20:26:21 +00:00
|
|
|
sleep(5);
|
2002-05-30 07:14:21 +00:00
|
|
|
}
|
2002-10-09 04:56:41 +00:00
|
|
|
fflush(stdout);
|
2002-12-03 18:57:40 +00:00
|
|
|
check_triggers(ssp);
|
2002-06-01 20:26:21 +00:00
|
|
|
ssp->ready = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char** argv) {
|
|
|
|
SCHED_SHMEM* ssp;
|
|
|
|
int i, retval;
|
|
|
|
bool asynch = false;
|
|
|
|
void* p;
|
|
|
|
|
2002-12-03 18:57:40 +00:00
|
|
|
unlink(STOP_SERVER_FILENAME);
|
|
|
|
unlink(REREAD_DB_FILENAME);
|
2002-10-09 04:56:41 +00:00
|
|
|
|
2002-10-03 18:33:46 +00:00
|
|
|
retval = config.parse_file();
|
|
|
|
if (retval) {
|
|
|
|
fprintf(stderr, "feeder: can't parse config file\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2002-06-01 20:26:21 +00:00
|
|
|
for (i=1; i<argc; i++) {
|
|
|
|
if (!strcmp(argv[i], "-asynch")) {
|
|
|
|
asynch = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-11-08 08:54:16 +00:00
|
|
|
if (asynch) {
|
|
|
|
if (fork()!=0) {
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-10-03 18:33:46 +00:00
|
|
|
retval = destroy_shmem(config.shmem_key);
|
2002-06-01 20:26:21 +00:00
|
|
|
if (retval) {
|
|
|
|
fprintf(stderr, "feeder: can't destroy shmem\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
2002-10-03 18:33:46 +00:00
|
|
|
retval = create_shmem(config.shmem_key, sizeof(SCHED_SHMEM), &p);
|
2002-06-01 20:26:21 +00:00
|
|
|
if (retval) {
|
|
|
|
fprintf(stderr, "feeder: can't create shmem\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
ssp = (SCHED_SHMEM*)p;
|
|
|
|
ssp->init();
|
2002-10-03 18:33:46 +00:00
|
|
|
retval = db_open(config.db_name, config.db_passwd);
|
2002-06-01 20:26:21 +00:00
|
|
|
if (retval) {
|
|
|
|
fprintf(stderr, "feeder: db_open: %d\n", retval);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
ssp->scan_tables();
|
|
|
|
|
|
|
|
printf(
|
|
|
|
"feeder: read\n"
|
|
|
|
"%d platforms\n"
|
|
|
|
"%d apps\n"
|
|
|
|
"%d app_versions\n",
|
|
|
|
ssp->nplatforms,
|
|
|
|
ssp->napps,
|
|
|
|
ssp->napp_versions
|
|
|
|
);
|
|
|
|
|
2002-11-08 08:54:16 +00:00
|
|
|
feeder_loop(ssp);
|
2002-05-30 07:14:21 +00:00
|
|
|
}
|