]> git.sesse.net Git - freerainbowtables/blob - BOINC software/BOINC server apps/index_calculator_workgenerator/index_calculator_workgenerator.cpp
initial
[freerainbowtables] / BOINC software / BOINC server apps / index_calculator_workgenerator / index_calculator_workgenerator.cpp
1 // This file is part of BOINC.
2 // http://boinc.berkeley.edu
3 // Copyright (C) 2008 University of California
4 //
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.
9 //
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.
14 //
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/>.
17
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):
21 //
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.
29
30 #include <unistd.h>
31 #include <cstdlib>
32 #include <string>
33 #include <cstring>
34
35 #include "boinc_db.h"
36 #include "error_numbers.h"
37 #include "backend_lib.h"
38 #include "parse.h"
39 #include "util.h"
40
41 #include "sched_config.h"
42 #include "sched_util.h"
43 #include "sched_msgs.h"
44
45 #define CUSHION 100
46 #define int64 long long
47     // maintain at least this many unsent results
48 #define REPLICATION_FACTOR 2
49 // globals
50 //
51 char* wu_template;
52 DB_APP app;
53 DB_CONN *frt;
54 int start_time;
55 int seqno;
56
57 using namespace std;
58 // create one new job
59 //
60 int make_job() { 
61     DB_WORKUNIT wu;
62     MYSQL_RES* resp;    
63     MYSQL_ROW row;
64     char name[256], path[256], query[1024];
65     const char* infiles[1];
66     int retval;
67     string charset;
68 //    frt->do_query("SELECT lookupid, hashroutine, charset, minletters, maxletters, `index`, chainlength, hash WHERE inuse = 0 LIMIT 1;");
69  
70     sprintf(query, "SELECT rcl.lookupid, rchl.hashroutine, rcts.charset, rcts.minletters, rcts.maxletters, rct.`index`, rcts.chainlength, rchl.hash FROM rainbowcrack_cracker_lookups rcl INNER JOIN rainbowcrack_cracker_hashlist rchl ON rchl.hashid = rcl.hashid INNER JOIN rainbowcrack_cracker_tables rct ON rcl.tableid = rct.tableid INNER JOIN rainbowcrack_cracker_tablesets rcts ON rcts.tablesetid = rct.tablesetid INNER JOIN rainbowcrack_cracker_requests rcr ON rcr.hashid = rcl.hashid WHERE rchl.password IS NULL AND hasindices = 0 AND rchl.hashid  = 139192 LIMIT 1");
71     log_messages.printf(MSG_DEBUG, "%s\n", query);
72     retval = frt->do_query(query);
73     if(retval) {
74        log_messages.printf(MSG_DEBUG, "Query returned %i\n", retval);   
75         return retval;
76
77     }
78     resp = mysql_store_result(frt->mysql);
79     if (!resp) return ERR_DB_NOT_FOUND;
80     row = mysql_fetch_row(resp);
81     mysql_free_result(resp);
82     if (!row) return ERR_DB_NOT_FOUND;
83         
84                 /*
85     sprintf(query, "UPDATE rainbowcrack_cracker_lookupqueue SET inuse = 1 WHERE lookupid = %s", row[0]);
86     log_messages.printf(MSG_DEBUG, "%s\n", query);
87     retval = frt->do_query(query);
88     if(retval)  return retval;  
89 */
90     sprintf(query, "UPDATE rainbowcrack_cracker_lookups SET hasindices = 1 WHERE lookupid = %s", row[0]);
91     log_messages.printf(MSG_DEBUG, "%s\n", query);
92     retval = frt->do_query(query);
93     if(retval)  return retval;
94
95     char command_line[256];
96     char filename[256];
97     sprintf(command_line, "%s %s %s %s %s %s %s", row[1], row[2], row[3], row[4], row[5], row[6], row[7]);
98
99     // make a unique name (for the job and its input file)
100     //
101     sprintf(name, "%s %s_%s#%s-%s_%s_%s", row[0], row[1], row[2], row[3], row[4], row[5], row[6]);
102 //    sprintf(filename, "%s.txt", row[0]);
103     log_messages.printf(MSG_DEBUG, "%s\n", name); 
104     
105     read_file_malloc("../templates/index_calculator_input_template.xml", wu_template);
106     // Fill in the job parameters
107     //
108     wu.clear();
109     wu.appid = app.id;
110     strcpy(wu.name, name);
111     wu.rsc_fpops_est = 1e12;
112     wu.rsc_fpops_bound = 1e14;
113     wu.rsc_memory_bound = 1e8;
114     wu.rsc_disk_bound = 1e8;
115     wu.delay_bound = 86400;
116     wu.min_quorum = 2;
117     wu.target_nresults = 2;
118     wu.max_error_results = 8;
119     wu.max_total_results = 10;
120     wu.max_success_results = 2;
121     // Register the job with BOINC
122     //
123     return create_work(
124         wu,
125         wu_template,
126         "templates/output_template.xml",
127         "../templates/output_template.xml",
128         NULL,
129         0,
130         config,
131         command_line
132     );
133 }
134
135 void main_loop() {
136     int retval;
137
138     while (1) {
139         check_stop_daemons();
140         int n;
141         retval = count_unsent_results(n, 0);
142         if (n > CUSHION) {
143             sleep(60);
144         } else {
145             int njobs = (CUSHION-n)/REPLICATION_FACTOR;
146             log_messages.printf(MSG_DEBUG,
147                 "Making %d jobs\n", njobs
148             );
149             for (int i=0; i<njobs; i++) {
150                 retval = make_job();
151                 if (retval) {
152                     log_messages.printf(MSG_CRITICAL,
153                         "can't make job: %d\n", retval
154                     );
155                     exit(retval);
156                 }
157             }
158             // Now sleep for a few seconds to let the transitioner
159             // create instances for the jobs we just created.
160             // Otherwise we could end up creating an excess of jobs.
161             sleep(5);
162         }
163     }
164 }
165
166 int main(int argc, char** argv) {
167     int i, retval;
168     frt = new DB_CONN();
169
170     for (i=1; i<argc; i++) {
171         if (!strcmp(argv[i], "-d")) {
172             log_messages.set_debug_level(atoi(argv[++i]));
173         } else {
174             log_messages.printf(MSG_CRITICAL,
175                 "bad cmdline arg: %s", argv[i]
176             );
177         }
178     }
179
180     if (config.parse_file("..")) {
181         log_messages.printf(MSG_CRITICAL,
182             "can't read config file\n"
183         );
184         exit(1);
185     }
186     retval = frt->open("rainbowtables-distrrtgen", config.db_host, config.db_user, config.db_passwd);
187     if (retval) {
188         log_messages.printf(MSG_CRITICAL, "can't open db rainbowtables-distrrtgen\n");
189         exit(1);
190     }
191
192     retval = boinc_db.open(
193         config.db_name, config.db_host, config.db_user, config.db_passwd
194     );
195     if (retval) {
196         log_messages.printf(MSG_CRITICAL, "can't open db\n");
197         exit(1);
198     }
199     if (app.lookup("where name='index_calculator'")) {
200         log_messages.printf(MSG_CRITICAL, "can't find app\n");
201         exit(1);
202     }
203     if (read_file_malloc("../templates/index_calculator_input_template.xml", wu_template)) {
204         log_messages.printf(MSG_CRITICAL, "can't read WU template\n");
205         exit(1);
206     }
207
208     start_time = time(0);
209     seqno = 0;
210
211     log_messages.printf(MSG_NORMAL, "Starting\n");
212
213     main_loop();
214 }
215