Ticket #1202: sample_work_generator.cpp.diff

File sample_work_generator.cpp.diff, 8.8 KB (added by Christian Beer, 12 years ago)
  • sample_work_generator.cpp

     
    2626// - Creates a new input file for each job;
    2727//   the file (and the workunit names) contain a timestamp
    2828//   and sequence number, so they're unique.
     29// - If \"input_dir\" is set, each file in this directory is used as
     30//   an input file (appended with timestamp and sequence number.
    2931
    3032#include <sys/param.h>
    3133#include <unistd.h>
     
    3335#include <string>
    3436#include <cstring>
    3537
     38#include "filesys.h"
    3639#include "boinc_db.h"
    3740#include "error_numbers.h"
    3841#include "backend_lib.h"
     
    5255const char* app_name = "example_app";
    5356const char* in_template_file = "example_app_in";
    5457const char* out_template_file = "example_app_out";
     58const char* input_dir = "none";
    5559
    5660char* in_template;
    5761DB_APP app;
     
    6064
    6165// create one new job
    6266//
    63 int make_job() {
    64     DB_WORKUNIT wu;
    65     char name[256], path[MAXPATHLEN];
     67int make_job(const char* filename = "none") {
     68    DB_WORKUNIT job;
     69    char name[256], path[MAXPATHLEN], dst_path[MAXPATHLEN], src_path[MAXPATHLEN];
    6670    const char* infiles[1];
    6771    int retval;
    6872
    69     // make a unique name (for the job and its input file)
    70     //
    71     sprintf(name, "%s_%d_%d", app_name, start_time, seqno++);
     73    if(!strcmp(input_dir, "none")) {
     74        // make a unique name (for the job and its input file)
     75        //
     76        sprintf(name, "%s_%d_%d", app_name, start_time, seqno++);
    7277
    73     // Create the input file.
    74     // Put it at the right place in the download dir hierarchy
    75     //
    76     retval = config.download_path(name, path);
    77     if (retval) return retval;
    78     FILE* f = fopen(path, "w");
    79     if (!f) return ERR_FOPEN;
    80     fprintf(f, "This is the input file for job %s", name);
    81     fclose(f);
     78        // Create the input file.
     79        // Put it at the right place in the download dir hierarchy
     80        //
     81        retval = config.download_path(name, path);
     82        if (retval) return retval;
     83        FILE* f = fopen(path, "w");
     84        if (!f) return ERR_FOPEN;
     85        fprintf(f, "This is the input file for job %s", name);
     86        fclose(f);
     87    } else {
     88        // make a unique name (for the job and its input file)
     89        //
     90        sprintf(name, "%s_%s_%d_%d", app_name, filename, start_time, seqno++);
     91        log_messages.printf(MSG_DEBUG,
     92            "found input file %s and creating job %s\n", filename, name
     93        );
     94        retval = config.download_path(name, dst_path);
     95        if (retval) return retval;
     96        sprintf(src_path, "%s/%s", input_dir, filename);
     97        log_messages.printf(MSG_DEBUG,
     98            "move file from %s to %s\n", src_path, dst_path
     99        );
     100        retval = rename(src_path, dst_path);
     101        if (retval) {
     102            log_messages.printf(MSG_CRITICAL,
     103                "rename: %d, errno is %d\n", retval, errno
     104            );
     105            return retval;
     106        }
     107    }
    82108
    83109    // Fill in the job parameters
    84110    //
    85     wu.clear();
    86     wu.appid = app.id;
    87     strcpy(wu.name, name);
    88     wu.rsc_fpops_est = 1e12;
    89     wu.rsc_fpops_bound = 1e14;
    90     wu.rsc_memory_bound = 1e8;
    91     wu.rsc_disk_bound = 1e8;
    92     wu.delay_bound = 86400;
    93     wu.min_quorum = REPLICATION_FACTOR;
    94     wu.target_nresults = REPLICATION_FACTOR;
    95     wu.max_error_results = REPLICATION_FACTOR*4;
    96     wu.max_total_results = REPLICATION_FACTOR*8;
    97     wu.max_success_results = REPLICATION_FACTOR*4;
     111    job.clear();
     112    job.appid = app.id;
     113    strcpy(job.name, name);
     114    job.rsc_fpops_est = 1e12;
     115    job.rsc_fpops_bound = 1e14;
     116    job.rsc_memory_bound = 1e8;
     117    job.rsc_disk_bound = 1e8;
     118    job.delay_bound = 86400;
     119    job.min_quorum = REPLICATION_FACTOR;
     120    job.target_nresults = REPLICATION_FACTOR;
     121    job.max_error_results = REPLICATION_FACTOR*4;
     122    job.max_total_results = REPLICATION_FACTOR*8;
     123    job.max_success_results = REPLICATION_FACTOR*4;
    98124    infiles[0] = name;
    99125
    100126    // Register the job with BOINC
    101127    //
    102128    sprintf(path, "templates/%s", out_template_file);
    103129    return create_work(
    104         wu,
     130        job,
    105131        in_template,
    106132        path,
    107133        config.project_path(path),
     
    112138}
    113139
    114140void main_loop() {
     141    std::string file;
    115142    int retval;
    116143
    117144    while (1) {
     
    127154        if (n > CUSHION) {
    128155            daemon_sleep(10);
    129156        } else {
    130             int njobs = (CUSHION-n)/REPLICATION_FACTOR;
    131             log_messages.printf(MSG_DEBUG,
    132                 "Making %d jobs\n", njobs
    133             );
    134             for (int i=0; i<njobs; i++) {
    135                 retval = make_job();
    136                 if (retval) {
    137                     log_messages.printf(MSG_CRITICAL,
    138                         "can't make job: %s\n", boincerror(retval)
     157            if(!strcmp(input_dir, "none")) {
     158                int njobs = (CUSHION-n)/REPLICATION_FACTOR;
     159                log_messages.printf(MSG_DEBUG,
     160                    "Making %d jobs\n", njobs
     161                );
     162                for (int i=0; i<njobs; i++) {
     163                    retval = make_job();
     164                    if (retval) {
     165                        log_messages.printf(MSG_CRITICAL,
     166                            "can't make job: %s\n", boincerror(retval)
     167                        );
     168                        exit(retval);
     169                    }
     170                }
     171            } else {
     172                DirScanner dirscan(input_dir);
     173                // check if there are files in input_dir
     174                if (dirscan.scan(file)) {
     175                    // files found, now create work for them
     176                    int njobs = (CUSHION-n)/REPLICATION_FACTOR;
     177                    log_messages.printf(MSG_DEBUG,
     178                        "Making %d jobs from files in %s\n", njobs, input_dir
    139179                    );
    140                     exit(retval);
     180                    for (int i=0; i<njobs; i++) {
     181                        retval = make_job(file.c_str());
     182                        if (retval) {
     183                            log_messages.printf(MSG_CRITICAL,
     184                                "can't make job: %s\n", boincerror(retval)
     185                            );
     186                            exit(retval);
     187                        }
     188                        if (!dirscan.scan(file)) {
     189                            log_messages.printf(MSG_DEBUG,
     190                                "no more input files in: %s\n", input_dir
     191                            );
     192                            break; // no more files but CUSHION not yet reached
     193                        }
     194                    }
     195                } else {
     196                    // no more files found, wait some time
     197                    log_messages.printf(MSG_DEBUG,
     198                        "no more input files in: %s\n", input_dir
     199                    );
     200                    daemon_sleep(60); //increase this time if you can't supply a steady stream of input files
    141201                }
    142202            }
    143203            // Now sleep for a few seconds to let the transitioner
     
    157217        "- Creates work for the application \"example_app\".\n"
    158218        "- Creates a new input file for each job;\n"
    159219        "  the file (and the workunit names) contain a timestamp\n"
    160         "  and sequence number, so that they're unique.\n\n"
     220        "  and sequence number, so that they're unique.\n"
     221        "- If \"input_dir\" is set, each file in this directory is used as\n"
     222        "  an input file (appended with timestamp and sequence number.\n\n"
    161223        "Usage: %s [OPTION]...\n\n"
    162224        "Options:\n"
    163225        "  [ --app X                Application name (default: example_app)\n"
    164226        "  [ --in_template_file     Input template (default: example_app_in)\n"
    165227        "  [ --out_template_file    Output template (default: example_app_out)\n"
     228        "  [ --input_dir            Input directory (default: none)\n"
    166229        "  [ -d X ]                 Sets debug level to X.\n"
    167230        "  [ -h | --help ]          Shows this help text.\n"
    168231        "  [ -v | --version ]       Shows version information.\n",
     
    173236int main(int argc, char** argv) {
    174237    int i, retval;
    175238    char buf[256];
    176 
     239   
    177240    for (i=1; i<argc; i++) {
    178241        if (is_arg(argv[i], "d")) {
    179242            if (!argv[++i]) {
     
    190253            in_template_file = argv[++i];
    191254        } else if (!strcmp(argv[i], "--out_template_file")) {
    192255            out_template_file = argv[++i];
     256        } else if (!strcmp(argv[i], "--input_dir")) {
     257            input_dir = argv[++i];
     258            if(strlen(input_dir) == (strrchr(input_dir,'/')-input_dir+1)) {
     259                log_messages.printf(MSG_CRITICAL, "%s must not have a trailing slash\n\n", argv[i]);
     260                exit(1);
     261            }
    193262        } else if (is_arg(argv[i], "h") || is_arg(argv[i], "help")) {
    194263            usage(argv[0]);
    195264            exit(0);