-
Notifications
You must be signed in to change notification settings - Fork 5
/
sample_work_generator.cpp
222 lines (200 loc) · 7 KB
/
sample_work_generator.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
// This file is part of BOINC.
// http://boinc.berkeley.edu
// Copyright (C) 2008 University of California
//
// BOINC 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 3 of the License, or (at your option) any later version.
//
// BOINC 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.
//
// You should have received a copy of the GNU Lesser General Public License
// along with BOINC. If not, see <http://www.gnu.org/licenses/>.
// sample_work_generator.cpp: this is a modified version of the vanilla BOINC work generator.
// This work generator has the following properties
// (you may need to change some or all of these):
//
// - Runs as a daemon, and creates an unbounded supply of work.
// It attempts to maintain a "cushion" of 100 unsent job instances.
// (your app may not work this way; e.g. you might create work in batches)
// - Creates work for the application "cernvm".
// - Uses the -i, -inputfile or --inputfile to specify which VM has to be used.
#include <unistd.h>
#include <cstdlib>
#include <string>
#include <cstring>
#include "boinc_db.h"
#include "error_numbers.h"
#include "backend_lib.h"
#include "parse.h"
#include "util.h"
#include "svn_version.h"
#include "sched_config.h"
#include "sched_util.h"
#include "sched_msgs.h"
#include "str_util.h"
#define CUSHION 100
// maintain at least this many unsent results
#define REPLICATION_FACTOR 2
// globals
//
char* wu_template;
DB_APP app;
int start_time;
int seqno;
// create one new job
//
int make_job(char* inputfile) {
DB_WORKUNIT wu;
char name[256], path[256];
const char* infiles[1];
int retval;
// make a unique name (for the job and its input file)
//
sprintf(name, "uc_%d_%d", start_time, seqno++);
// Create the input file.
// Put it at the right place in the download dir hierarchy
//
retval = config.download_path(name, path);
if (retval) return retval;
FILE* f = fopen(path, "w");
if (!f) return ERR_FOPEN;
fprintf(f, "This is the input file for job %s", name);
fclose(f);
// Fill in the job parameters
//
wu.clear();
wu.appid = app.id;
strcpy(wu.name, name);
wu.rsc_fpops_est = 1e12;
wu.rsc_fpops_bound = 1e14;
wu.rsc_memory_bound = 1e8;
wu.rsc_disk_bound = 1e8;
wu.delay_bound = 86400;
wu.min_quorum = REPLICATION_FACTOR;
wu.target_nresults = REPLICATION_FACTOR;
wu.max_error_results = REPLICATION_FACTOR*4;
wu.max_total_results = REPLICATION_FACTOR*8;
wu.max_success_results = REPLICATION_FACTOR*4;
// Specify the input file. If thre is no argument, use the last hard-coded version
if (inputfile != "") infiles[0] = inputfile;
else infiles[0] = "cernvm.vmdk.gz=cernvm_2.3.3_vmdk.gz";
// Register the job with BOINC
//
return create_work(
wu,
wu_template,
"templates/cernvm_result",
config.project_path("templates/cernvm_result"),
infiles,
1,
config
);
}
void main_loop(char* inputfile) {
int retval;
while (1) {
check_stop_daemons();
int n;
retval = count_unsent_results(n, 0);
if (n > CUSHION) {
sleep(60);
} else {
int njobs = (CUSHION-n)/REPLICATION_FACTOR;
log_messages.printf(MSG_DEBUG,
"Making %d jobs\n", njobs
);
for (int i=0; i<njobs; i++) {
retval = make_job(inputfile);
if (retval) {
log_messages.printf(MSG_CRITICAL,
"can't make job: %d\n", retval
);
exit(retval);
}
}
// Now sleep for a few seconds to let the transitioner
// create instances for the jobs we just created.
// Otherwise we could end up creating an excess of jobs.
sleep(5);
}
}
}
void usage(char *name) {
fprintf(stderr, "This is an example BOINC work generator.\n"
"This work generator has the following properties\n"
"(you may need to change some or all of these):\n"
"- Runs as a daemon, and creates an unbounded supply of work.\n"
" It attempts to maintain a \"cushion\" of 100 unsent job instances.\n"
" (your app may not work this way; e.g. you might create work in batches)\n"
"- Creates work for the application \"cernvm\".\n"
"- Creates a new input file for each job;\n"
" the file (and the workunit names) contain a timestamp\n"
" and sequence number, so that they're unique.\n\n"
"Usage: %s [OPTION]...\n\n"
"Options:\n"
" [ -d X ] Sets debug level to X.\n"
" [ -h | -help | --help ] Shows this help text.\n"
" [ -v | --version | --version ] Shows version information.\n",
name
);
}
int main(int argc, char** argv) {
int i, retval;
char* inputfile="";
for (i=1; i<argc; i++) {
if (!strcmp(argv[i], "-d")) {
if(!argv[++i]) {
log_messages.printf(MSG_CRITICAL, "%s requires an argument\n\n", argv[--i]);
usage(argv[0]);
exit(1);
}
log_messages.set_debug_level(atoi(argv[i]));
} else if(!strcmp(argv[i], "-h") || !strcmp(argv[i], "-help") || !strcmp(argv[i], "--help")) {
usage(argv[0]);
exit(0);
} else if(!strcmp(argv[i], "-v") || !strcmp(argv[i], "-version") || !strcmp(argv[i], "--version")) {
printf("%s\n", SVN_VERSION);
exit(0);
} else if(!strcmp(argv[i], "-i") || !strcmp(argv[i], "-inputfile") || !strcmp(argv[i], "--inputfile")) {
// Input file
printf("Input file: %s\n", argv[i+1]);
inputfile = argv[i+1];
i = i + 1;
} else {
log_messages.printf(MSG_CRITICAL, "unknown command line argument: %s\n\n", argv[i]);
usage(argv[0]);
exit(1);
}
}
retval = config.parse_file();
if (retval) {
log_messages.printf(MSG_CRITICAL,
"Can't parse config.xml: %s\n", boincerror(retval)
);
exit(1);
}
retval = boinc_db.open(
config.db_name, config.db_host, config.db_user, config.db_passwd
);
if (retval) {
log_messages.printf(MSG_CRITICAL, "can't open db\n");
exit(1);
}
if (app.lookup("where name='cernvm'")) {
log_messages.printf(MSG_CRITICAL, "can't find app\n");
exit(1);
}
if (read_file_malloc(config.project_path("templates/cernvm_wu_disk_bound"), wu_template)) {
log_messages.printf(MSG_CRITICAL, "can't read WU template\n");
exit(1);
}
start_time = time(0);
seqno = 0;
log_messages.printf(MSG_NORMAL, "Starting\n");
main_loop(inputfile);
}