1 // This file is part of BOINC.
2 // http://boinc.berkeley.edu
3 // Copyright (C) 2008 University of California
5 // BOINC is free software; you can redistribute it and/or modify it
6 // under the terms of the GNU Lesser General Public License
7 // as published by the Free Software Foundation,
8 // either version 3 of the License, or (at your option) any later version.
10 // BOINC is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 // See the GNU Lesser General Public License for more details.
15 // You should have received a copy of the GNU Lesser General Public License
16 // along with BOINC. If not, see <http://www.gnu.org/licenses/>.
18 // sample_work_generator.C: an example BOINC work generator.
19 // This work generator has the following properties
20 // (you may need to change some or all of these):
22 // - Runs as a daemon, and creates an unbounded supply of work.
23 // It attempts to maintain a "cushion" of 100 unsent job instances.
24 // (your app may not work this way; e.g. you might create work in batches)
25 // - Creates work for the application "uppercase".
26 // - Creates a new input file for each job;
27 // the file (and the workunit names) contain a timestamp
28 // and sequence number, so that they're unique.
36 #include "error_numbers.h"
37 #include "backend_lib.h"
41 #include "sched_config.h"
42 #include "sched_util.h"
43 #include "sched_msgs.h"
46 #define int64 long long
47 // maintain at least this many unsent results
48 #define REPLICATION_FACTOR 2
64 char name[256], path[256], query[1024];
65 const char* infiles[1];
68 // retval = frt->do_query("CALL requestwork(1)");
69 frt->do_query("SELECT TableID, ChainStartPosition FROM generator_tables WHERE ChainsDone < (NumTargetChains/* - (NumTargetChains / 20)*/) AND ChainStartPosition < Keyspace - PartSize AND IsCompleted = 0 LIMIT 1;");
70 if(retval) return retval;
71 resp = mysql_store_result(frt->mysql);
72 if (!resp) return ERR_DB_NOT_FOUND;
73 row = mysql_fetch_row(resp);
74 mysql_free_result(resp);
75 if (!row) return ERR_DB_NOT_FOUND;
76 int tableid = atoi(row[0]);
77 int64 chainstart = atol(row[1]);
78 sprintf(query, "INSERT INTO generator_parts SET TableID = %s, ClientID = 1, AssignedTime = NOW(), Status = 1, ChainStart = %s\0", row[0], row[1]);
79 log_messages.printf(MSG_DEBUG, "%s\n", query);
80 retval = frt->do_query(query);
81 if(retval) return retval;
82 int partid = frt->insert_id();
84 sprintf(query, "UPDATE generator_tables SET ChainStartPosition = ChainStartPosition + PartSize WHERE TableID = %i\0", tableid);
85 log_messages.printf(MSG_DEBUG, "%s\n", query);
86 retval = frt->do_query(query);
87 if(retval) return retval;
90 log_messages.printf(MSG_DEBUG, "SELECTING FROM DB\n");
91 sprintf(query, "SELECT %i AS PartID, HashRoutine, Charset, MinLetters, MaxLetters, `Index`, ChainLength, PartSize AS ChainCount, '%lld' AS ChainStart, CheckPoints, Salt FROM generator_tables WHERE TableID = %i;\0", partid, chainstart, tableid);
92 log_messages.printf(MSG_DEBUG, "%s\n", query);
94 retval = frt->do_query(query);
95 if(retval) return retval;
96 resp = mysql_store_result(frt->mysql);
97 if (!resp) return ERR_DB_NOT_FOUND;
98 row = mysql_fetch_row(resp);
99 mysql_free_result(resp);
100 if (!row) return ERR_DB_NOT_FOUND;
101 char command_line[256];
102 // char filename[256];
103 sprintf(command_line, "%s %s %s %s %s %s %s %s %s", row[1], row[2], row[3], row[4], row[5], row[6], row[7], row[8], row[9]);
105 // make a unique name (for the job and its input file)
107 sprintf(name, "%s %s_%s#%s-%s_%s_%sx%s - %s", row[0], row[1], row[2], row[3], row[4], row[5], row[6], row[7], row[8]);
108 // sprintf(filename, "%s.txt", row[0]);
109 log_messages.printf(MSG_DEBUG, "%s\n", name);
111 read_file_malloc("templates/input_template.xml", wu_template);
112 // read_file_string("../download/charset.txt", charset);
113 /* retval = config.download_path(filename, path);
114 if (retval) return retval;
115 FILE* f = fopen(path, "wb");
116 if (!f) return ERR_FOPEN;
117 fwrite(charset.c_str(), charset.length(), 1, f);
119 infiles[0] = filename;*/
120 // Fill in the job parameters
124 strcpy(wu.name, name);
125 wu.rsc_fpops_est = 1e13;
126 wu.rsc_fpops_bound = 1e16;
127 wu.rsc_memory_bound = 1e8;
128 wu.rsc_disk_bound = 1e8;
129 wu.delay_bound = 4*86400;
131 wu.target_nresults = 1;
132 wu.max_error_results = 8;
133 wu.max_total_results = 10;
134 wu.max_success_results = 2;
135 // Register the job with BOINC
140 "templates/output_template.xml",
141 "../templates/output_template.xml",
153 check_stop_daemons();
155 retval = count_unsent_results(n, 0);
159 int njobs = (CUSHION-n)/REPLICATION_FACTOR;
160 log_messages.printf(MSG_DEBUG,
161 "Making %d jobs\n", njobs
163 for (int i=0; i<njobs; i++) {
166 log_messages.printf(MSG_CRITICAL,
167 "can't make job: %d\n", retval
172 // Now sleep for a few seconds to let the transitioner
173 // create instances for the jobs we just created.
174 // Otherwise we could end up creating an excess of jobs.
180 int main(int argc, char** argv) {
184 for (i=1; i<argc; i++) {
185 if (!strcmp(argv[i], "-d")) {
186 log_messages.set_debug_level(atoi(argv[++i]));
188 log_messages.printf(MSG_CRITICAL,
189 "bad cmdline arg: %s", argv[i]
194 if (config.parse_file("..")) {
195 log_messages.printf(MSG_CRITICAL,
196 "can't read config file\n"
200 retval = frt->open("rainbowtables-distrrtgen", config.db_host, config.db_user, config.db_passwd);
202 log_messages.printf(MSG_CRITICAL, "can't open db rainbowtables-distrrtgen\n");
206 retval = boinc_db.open(
207 config.db_name, config.db_host, config.db_user, config.db_passwd
210 log_messages.printf(MSG_CRITICAL, "can't open db\n");
213 if (app.lookup("where name='distrrtgen'")) {
214 log_messages.printf(MSG_CRITICAL, "can't find app\n");
217 if (read_file_malloc("../templates/input_template.xml", wu_template)) {
218 log_messages.printf(MSG_CRITICAL, "can't read WU template\n");
222 start_time = time(0);
225 log_messages.printf(MSG_NORMAL, "Starting\n");