2005-01-20 23:22:22 +00:00
|
|
|
// Berkeley Open Infrastructure for Network Computing
|
|
|
|
// http://boinc.berkeley.edu
|
2007-07-20 18:47:52 +00:00
|
|
|
// Copyright (C) 2007 University of California
|
2003-07-15 22:13:38 +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-15 22:13:38 +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.
|
2002-04-30 22:22:54 +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
|
2002-04-30 22:22:54 +00:00
|
|
|
|
2007-07-20 18:47:52 +00:00
|
|
|
// This is the primary sample BOINC application;
|
|
|
|
// it shows most of the features of the BOINC API.
|
|
|
|
//
|
|
|
|
// read "in", convert to upper case, write to "out"
|
|
|
|
//
|
|
|
|
// command line options (use for debugging various scenarios):
|
|
|
|
// -run_slow: sleep 1 second after each character; useful for debugging
|
|
|
|
// -cpu_time N: use about N CPU seconds after copying files
|
|
|
|
// -early_exit: exit(10) after 30 chars
|
|
|
|
// -early_crash: crash after 30 chars
|
2002-09-04 18:18:40 +00:00
|
|
|
//
|
2006-04-14 17:55:02 +00:00
|
|
|
|
2004-03-04 11:41:43 +00:00
|
|
|
#ifdef _WIN32
|
2004-06-16 23:29:48 +00:00
|
|
|
#include "boinc_win.h"
|
2005-11-21 18:34:44 +00:00
|
|
|
#else
|
|
|
|
#include "config.h"
|
2004-07-13 13:54:09 +00:00
|
|
|
#include <cstdio>
|
|
|
|
#include <cctype>
|
|
|
|
#include <ctime>
|
|
|
|
#include <cstring>
|
|
|
|
#include <cstdlib>
|
|
|
|
#include <csignal>
|
2002-10-24 17:06:42 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
2002-09-04 18:18:40 +00:00
|
|
|
|
2007-07-20 18:47:52 +00:00
|
|
|
#include "str_util.h"
|
2002-09-04 22:14:20 +00:00
|
|
|
#include "util.h"
|
2002-09-04 18:18:40 +00:00
|
|
|
#include "filesys.h"
|
2002-08-05 00:29:34 +00:00
|
|
|
#include "boinc_api.h"
|
2004-04-15 00:21:29 +00:00
|
|
|
#include "mfile.h"
|
2007-07-20 18:47:52 +00:00
|
|
|
#include "graphics2.h"
|
|
|
|
|
|
|
|
#ifdef APP_GRAPHICS
|
|
|
|
#include "uc2.h"
|
|
|
|
UC_SHMEM* shmem;
|
|
|
|
#endif
|
2002-04-30 22:22:54 +00:00
|
|
|
|
2004-06-30 18:17:21 +00:00
|
|
|
using std::string;
|
|
|
|
|
2002-09-04 18:18:40 +00:00
|
|
|
#define CHECKPOINT_FILE "upper_case_state"
|
2005-04-28 23:19:58 +00:00
|
|
|
#define INPUT_FILENAME "in"
|
|
|
|
#define OUTPUT_FILENAME "out"
|
2002-09-04 18:18:40 +00:00
|
|
|
|
2007-07-20 18:47:52 +00:00
|
|
|
bool run_slow = false;
|
|
|
|
bool early_exit = false;
|
|
|
|
bool early_crash = false;
|
|
|
|
bool early_sleep = false;
|
|
|
|
double cpu_time = 20;
|
|
|
|
|
|
|
|
static void use_some_cpu() {
|
|
|
|
double j = 3.14159;
|
|
|
|
int i, n = 0;
|
|
|
|
for (i=0; i<20000000; i++) {
|
|
|
|
n++;
|
|
|
|
j *= n+j-3.14159;
|
|
|
|
j /= (float)n;
|
|
|
|
}
|
|
|
|
}
|
2002-09-04 18:18:40 +00:00
|
|
|
|
|
|
|
int do_checkpoint(MFILE& mf, int nchars) {
|
|
|
|
int retval;
|
2003-11-08 00:56:55 +00:00
|
|
|
string resolved_name;
|
2002-09-04 18:18:40 +00:00
|
|
|
|
2003-11-08 00:56:55 +00:00
|
|
|
FILE* f = fopen("temp", "w");
|
2002-09-04 18:18:40 +00:00
|
|
|
if (!f) return 1;
|
|
|
|
fprintf(f, "%d", nchars);
|
|
|
|
fclose(f);
|
|
|
|
|
|
|
|
fprintf(stderr, "APP: upper_case checkpointing\n");
|
|
|
|
|
|
|
|
retval = mf.flush();
|
|
|
|
if (retval) return retval;
|
2004-06-08 14:37:42 +00:00
|
|
|
boinc_resolve_filename_s(CHECKPOINT_FILE, resolved_name);
|
2003-11-08 00:56:55 +00:00
|
|
|
retval = boinc_rename("temp", resolved_name.c_str());
|
2002-09-04 18:18:40 +00:00
|
|
|
if (retval) return retval;
|
|
|
|
|
2007-07-20 18:47:52 +00:00
|
|
|
//use_some_cpu();
|
|
|
|
fprintf(stderr, "APP: upper_case checkpoint done\n");
|
2002-09-04 18:18:40 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-07-20 18:47:52 +00:00
|
|
|
#ifdef APP_GRAPHICS
|
|
|
|
void update_shmem() {
|
|
|
|
if (!shmem) return;
|
|
|
|
shmem->fraction_done = boinc_get_fraction_done();
|
|
|
|
shmem->cpu_time = boinc_worker_thread_cpu_time();;
|
|
|
|
shmem->update_time = dtime();
|
|
|
|
boinc_get_status(&shmem->status);
|
2002-09-04 18:18:40 +00:00
|
|
|
}
|
2002-08-14 23:02:32 +00:00
|
|
|
#endif
|
2002-09-04 18:18:40 +00:00
|
|
|
|
2007-07-20 18:47:52 +00:00
|
|
|
int main(int argc, char **argv) {
|
|
|
|
int i;
|
|
|
|
int c, nchars = 0, retval, n;
|
|
|
|
double fsize, fd;
|
|
|
|
char input_path[512], output_path[512], chkpt_path[512];
|
2003-09-05 21:26:21 +00:00
|
|
|
MFILE out;
|
2007-07-20 18:47:52 +00:00
|
|
|
FILE* state, *infile;
|
|
|
|
|
|
|
|
for (i=0; i<argc; i++) {
|
|
|
|
if (!strcmp(argv[i], "-early_exit")) early_exit = true;
|
|
|
|
if (!strcmp(argv[i], "-early_crash")) early_crash = true;
|
|
|
|
if (!strcmp(argv[i], "-early_sleep")) early_sleep = true;
|
|
|
|
if (!strcmp(argv[i], "-run_slow")) run_slow = true;
|
|
|
|
if (!strcmp(argv[i], "-cpu_time")) {
|
|
|
|
cpu_time = atof(argv[++i]);
|
|
|
|
}
|
|
|
|
}
|
2002-06-20 23:35:44 +00:00
|
|
|
|
2007-07-20 18:47:52 +00:00
|
|
|
retval = boinc_init();
|
|
|
|
if (retval) exit(retval);
|
2002-09-04 18:18:40 +00:00
|
|
|
|
2007-07-20 18:47:52 +00:00
|
|
|
// open the input file (resolve logical name first)
|
|
|
|
//
|
|
|
|
boinc_resolve_filename(INPUT_FILENAME, input_path, sizeof(input_path));
|
|
|
|
infile = boinc_fopen(input_path, "r");
|
|
|
|
if (!infile) {
|
|
|
|
fprintf(stderr,
|
|
|
|
"Couldn't find input file, resolved name %s.\n", input_path
|
2003-09-04 00:18:27 +00:00
|
|
|
);
|
2002-10-30 04:30:28 +00:00
|
|
|
exit(-1);
|
2002-10-29 18:58:42 +00:00
|
|
|
}
|
2003-09-04 00:18:27 +00:00
|
|
|
|
2007-07-20 18:47:52 +00:00
|
|
|
// get size of input file (used to compute fraction done)
|
|
|
|
//
|
|
|
|
file_size(input_path, fsize);
|
|
|
|
|
|
|
|
boinc_resolve_filename(OUTPUT_FILENAME, output_path, sizeof(output_path));
|
2003-11-10 06:49:45 +00:00
|
|
|
|
2007-07-20 18:47:52 +00:00
|
|
|
// See if there's a valid checkpoint file.
|
|
|
|
// If so seek input file and truncate output file
|
|
|
|
//
|
|
|
|
boinc_resolve_filename(CHECKPOINT_FILE, chkpt_path, sizeof(chkpt_path));
|
|
|
|
state = boinc_fopen(chkpt_path, "r");
|
2002-09-04 18:18:40 +00:00
|
|
|
if (state) {
|
2007-07-20 18:47:52 +00:00
|
|
|
n = fscanf(state, "%d", &nchars);
|
|
|
|
fclose(state);
|
|
|
|
}
|
|
|
|
if (state && n==1) {
|
|
|
|
fseek(infile, nchars, SEEK_SET);
|
|
|
|
boinc_truncate(output_path, nchars);
|
|
|
|
retval = out.open(output_path, "a");
|
2002-09-04 18:18:40 +00:00
|
|
|
} else {
|
2007-07-20 18:47:52 +00:00
|
|
|
retval = out.open(output_path, "w");
|
2002-09-04 18:18:40 +00:00
|
|
|
}
|
|
|
|
if (retval) {
|
2003-09-04 00:18:27 +00:00
|
|
|
fprintf(stderr, "APP: upper_case output open failed:\n");
|
2007-07-20 18:47:52 +00:00
|
|
|
fprintf(stderr, "resolved name %s, retval %d\n", output_path, retval);
|
2003-09-04 00:18:27 +00:00
|
|
|
perror("open");
|
2002-09-04 18:18:40 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
2007-07-20 18:47:52 +00:00
|
|
|
|
|
|
|
#ifdef APP_GRAPHICS
|
|
|
|
// create shared mem segment for graphics, and arrange to update it
|
|
|
|
//
|
|
|
|
shmem = (UC_SHMEM*)boinc_graphics_make_shmem("uppercase", sizeof(UC_SHMEM));
|
|
|
|
boinc_register_timer_callback(update_shmem);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// main loop - read characters, convert to UC, write
|
|
|
|
//
|
- server code: at some point I made a global var "SCHED_CONFIG config",
mostly so that the parse function could assume
that everything was initially zero.
However, various back-end functions pass around SCHED_CONFIG&
as an argument (also named "config").
This creates a shadow, which is always bad.
Worse is the possibility that some projects have back-end programs
that have a SCHED_CONFIG variable that's automatic,
and therefore isn't zero initially,
and therefore isn't parsing correctly.
To fix this, I changed the 2 vectors in SCHED_CONFIG into pointers,
and have the parse routine zero the structure.
I was tempted to remove the SCHED_CONFIG& args to back-end functions,
but this would have broken some projects' code.
I did, however, change the name from config to config_loc
to avoid shadowing.
Also fixed various other compiler warnings.
svn path=/trunk/boinc/; revision=15541
2008-07-02 17:24:53 +00:00
|
|
|
for (i=0; ; i++) {
|
2007-07-20 18:47:52 +00:00
|
|
|
c = fgetc(infile);
|
2003-09-05 21:26:21 +00:00
|
|
|
|
2002-04-30 22:22:54 +00:00
|
|
|
if (c == EOF) break;
|
|
|
|
c = toupper(c);
|
2002-09-04 18:18:40 +00:00
|
|
|
out._putchar(c);
|
|
|
|
nchars++;
|
|
|
|
if (run_slow) {
|
2003-03-11 22:18:01 +00:00
|
|
|
boinc_sleep(1.);
|
2002-09-04 18:18:40 +00:00
|
|
|
}
|
2003-06-11 23:09:11 +00:00
|
|
|
|
2007-07-20 18:47:52 +00:00
|
|
|
if (early_exit && i>30) {
|
|
|
|
exit(-10);
|
2002-10-28 23:06:44 +00:00
|
|
|
}
|
2007-07-20 18:47:52 +00:00
|
|
|
|
|
|
|
if (early_crash && i>30) {
|
|
|
|
boinc_crash();
|
2003-06-28 00:10:47 +00:00
|
|
|
}
|
2007-07-20 18:47:52 +00:00
|
|
|
if (early_sleep && i>30) {
|
|
|
|
g_sleep = true;
|
|
|
|
while (1) boinc_sleep(1);
|
|
|
|
}
|
2002-10-28 23:06:44 +00:00
|
|
|
|
2007-07-20 18:47:52 +00:00
|
|
|
if (boinc_time_to_checkpoint()) {
|
2002-09-04 18:18:40 +00:00
|
|
|
retval = do_checkpoint(out, nchars);
|
|
|
|
if (retval) {
|
|
|
|
fprintf(stderr, "APP: upper_case checkpoint failed %d\n", retval);
|
2007-07-20 18:47:52 +00:00
|
|
|
exit(retval);
|
2002-09-04 18:18:40 +00:00
|
|
|
}
|
2002-08-24 00:41:25 +00:00
|
|
|
boinc_checkpoint_completed();
|
2002-06-24 21:29:05 +00:00
|
|
|
}
|
2003-11-10 06:49:45 +00:00
|
|
|
|
2007-07-20 18:47:52 +00:00
|
|
|
fd = nchars/fsize;
|
|
|
|
if (cpu_time) fd /= 2;
|
|
|
|
boinc_fraction_done(fd);
|
2002-04-30 22:22:54 +00:00
|
|
|
}
|
2007-07-20 18:47:52 +00:00
|
|
|
|
2002-09-04 18:18:40 +00:00
|
|
|
retval = out.flush();
|
|
|
|
if (retval) {
|
|
|
|
fprintf(stderr, "APP: upper_case flush failed %d\n", retval);
|
|
|
|
exit(1);
|
|
|
|
}
|
2007-07-20 18:47:52 +00:00
|
|
|
|
|
|
|
// burn up some CPU time if needed
|
|
|
|
//
|
2006-01-09 23:03:15 +00:00
|
|
|
if (cpu_time) {
|
|
|
|
double start = dtime();
|
2007-07-20 18:47:52 +00:00
|
|
|
while (1) {
|
|
|
|
double e = dtime()-start;
|
|
|
|
if (e > cpu_time) break;
|
|
|
|
fd = .5 + .5*(e/cpu_time);
|
|
|
|
boinc_fraction_done(fd);
|
|
|
|
|
|
|
|
if (boinc_time_to_checkpoint()) {
|
|
|
|
retval = do_checkpoint(out, nchars);
|
|
|
|
if (retval) {
|
|
|
|
fprintf(stderr, "APP: upper_case checkpoint failed %d\n", retval);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
boinc_checkpoint_completed();
|
|
|
|
}
|
|
|
|
|
2006-01-09 23:03:15 +00:00
|
|
|
use_some_cpu();
|
|
|
|
}
|
|
|
|
}
|
2007-07-20 18:47:52 +00:00
|
|
|
boinc_fraction_done(1);
|
|
|
|
#ifdef APP_GRAPHICS
|
|
|
|
update_shmem();
|
|
|
|
#endif
|
2002-08-05 00:29:34 +00:00
|
|
|
boinc_finish(0);
|
2006-01-09 23:03:15 +00:00
|
|
|
}
|
2003-06-11 23:09:11 +00:00
|
|
|
|
2007-07-20 18:47:52 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR Args, int WinMode) {
|
|
|
|
LPSTR command_line;
|
|
|
|
char* argv[100];
|
|
|
|
int argc;
|
2006-01-09 23:03:15 +00:00
|
|
|
|
2007-07-20 18:47:52 +00:00
|
|
|
command_line = GetCommandLine();
|
|
|
|
argc = parse_command_line( command_line, argv );
|
|
|
|
return main(argc, argv);
|
2002-09-18 21:54:36 +00:00
|
|
|
}
|
2007-07-20 18:47:52 +00:00
|
|
|
#endif
|
2002-09-18 22:31:36 +00:00
|
|
|
|
2005-01-02 18:29:53 +00:00
|
|
|
const char *BOINC_RCSID_33ac47a071 = "$Id$";
|
2007-07-20 18:47:52 +00:00
|
|
|
|