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 // This program serves as both
19 // - An example BOINC application, illustrating the use of the BOINC API
20 // - A program for testing various features of BOINC
21 //
22 // NOTE: this file exists as both
23 // boinc/apps/upper_case.cpp
24 // and
25 // boinc_samples/example_app/uc2.cpp
26 // If you update one, please update the other!
27 
28 // The program converts a mixed-case file to upper case:
29 // read "in", convert to upper case, write to "out"
30 //
31 // command line options
32 // --cpu_time N: use about N CPU seconds after copying files
33 // --critical_section: run most of the time in a critical section
34 // --early_exit: exit(10) after 30 chars
35 // --early_crash: crash after 30 chars
36 // --run_slow: sleep 1 second after each character
37 // --trickle_up: sent a trickle-up message
38 // --trickle_down: receive a trickle-up message
39 // --network_usage: tell the client we used some network
40 //
41 
42 #ifdef _WIN32
43 #include "boinc_win.h"
44 #else
45 #include "config.h"
46 #include <cstdio>
47 #include <cctype>
48 #include <ctime>
49 #include <cstring>
50 #include <cstdlib>
51 #include <csignal>
52 #include <unistd.h>
53 #endif
54 
55 #include "str_util.h"
56 #include "util.h"
57 #include "filesys.h"
58 #include "boinc_api.h"
59 #include "mfile.h"
60 #include "graphics2.h"
61 #include "uc2.h"
62 
63 #ifdef APP_GRAPHICS
64 UC_SHMEM* shmem;
65 #endif
66 
67 using std::string;
68 
69 #define CHECKPOINT_FILE "upper_case_state"
70 #define INPUT_FILENAME "in"
71 #define OUTPUT_FILENAME "out"
72 
73 bool run_slow = false;
74 bool early_exit = false;
75 bool early_crash = false;
76 bool early_sleep = false;
77 bool trickle_up = false;
78 bool trickle_down = false;
79 bool critical_section = false;    // run most of the time in a critical section
80 bool report_fraction_done = true;
81 bool network_usage = false;
82 double cpu_time = 20, comp_result;
83 
84 // do about .5 seconds of computing
85 // (note: I needed to add an arg to this;
86 // otherwise the MS C++ compiler optimizes away
87 // all but the first call to it!)
88 //
do_some_computing(int foo)89 static double do_some_computing(int foo) {
90     double x = 3.14159*foo;
91     int i;
92     for (i=0; i<50000000; i++) {
93         x += 5.12313123;
94         x *= 0.5398394834;
95     }
96     return x;
97 }
98 
do_checkpoint(MFILE & mf,int nchars)99 int do_checkpoint(MFILE& mf, int nchars) {
100     int retval;
101     string resolved_name;
102 
103     FILE* f = fopen("temp", "w");
104     if (!f) return 1;
105     fprintf(f, "%d", nchars);
106     fclose(f);
107 
108     retval = mf.flush();
109     if (retval) return retval;
110     boinc_resolve_filename_s(CHECKPOINT_FILE, resolved_name);
111     retval = boinc_rename("temp", resolved_name.c_str());
112     if (retval) return retval;
113 
114     return 0;
115 }
116 
117 #ifdef APP_GRAPHICS
update_shmem()118 void update_shmem() {
119     if (!shmem) return;
120 
121     // always do this; otherwise a graphics app will immediately
122     // assume we're not alive
123     shmem->update_time = dtime();
124 
125     // Check whether a graphics app is running,
126     // and don't bother updating shmem if so.
127     // This doesn't matter here,
128     // but may be worth doing if updating shmem is expensive.
129     //
130     if (shmem->countdown > 0) {
131         // the graphics app sets this to 5 every time it renders a frame
132         shmem->countdown--;
133     } else {
134         return;
135     }
136     shmem->fraction_done = boinc_get_fraction_done();
137     shmem->cpu_time = boinc_worker_thread_cpu_time();;
138     boinc_get_status(&shmem->status);
139 }
140 #endif
141 
main(int argc,char ** argv)142 int main(int argc, char **argv) {
143     int i;
144     int c, nchars = 0, retval, n;
145     double fsize, fd;
146     char input_path[512], output_path[512], chkpt_path[512], buf[256];
147     MFILE out;
148     FILE* state, *infile;
149 
150     for (i=0; i<argc; i++) {
151         if (strstr(argv[i], "early_exit")) early_exit = true;
152         if (strstr(argv[i], "early_crash")) early_crash = true;
153         if (strstr(argv[i], "early_sleep")) early_sleep = true;
154         if (strstr(argv[i], "run_slow")) run_slow = true;
155         if (strstr(argv[i], "critical_section")) critical_section = true;
156         if (strstr(argv[i], "network_usage")) network_usage = true;
157         if (strstr(argv[i], "cpu_time")) {
158             cpu_time = atof(argv[++i]);
159         }
160         if (strstr(argv[i], "trickle_up")) trickle_up = true;
161         if (strstr(argv[i], "trickle_down")) trickle_down = true;
162     }
163     retval = boinc_init();
164     if (retval) {
165         fprintf(stderr, "%s boinc_init returned %d\n",
166             boinc_msg_prefix(buf, sizeof(buf)), retval
167         );
168         exit(retval);
169     }
170 
171     fprintf(stderr, "%s app started; CPU time %f, flags:%s%s%s%s%s%s%s\n",
172         boinc_msg_prefix(buf, sizeof(buf)),
173         cpu_time,
174         early_exit?" early_exit":"",
175         early_crash?" early_crash":"",
176         early_sleep?" early_sleep":"",
177         run_slow?" run_slow":"",
178         critical_section?" critical_section":"",
179         trickle_up?" trickle_up":"",
180         trickle_down?" trickle_down":""
181     );
182 
183     // open the input file (resolve logical name first)
184     //
185     boinc_resolve_filename(INPUT_FILENAME, input_path, sizeof(input_path));
186     infile = boinc_fopen(input_path, "r");
187     if (!infile) {
188         fprintf(stderr,
189             "%s Couldn't find input file, resolved name %s.\n",
190             boinc_msg_prefix(buf, sizeof(buf)), input_path
191         );
192         exit(-1);
193     }
194 
195     // get size of input file (used to compute fraction done)
196     //
197     file_size(input_path, fsize);
198 
199     boinc_resolve_filename(OUTPUT_FILENAME, output_path, sizeof(output_path));
200 
201     // See if there's a valid checkpoint file.
202     // If so seek input file and truncate output file
203     //
204     boinc_resolve_filename(CHECKPOINT_FILE, chkpt_path, sizeof(chkpt_path));
205     state = boinc_fopen(chkpt_path, "r");
206     if (state) {
207         n = fscanf(state, "%d", &nchars);
208         fclose(state);
209     }
210     if (state && n==1) {
211         fseek(infile, nchars, SEEK_SET);
212         boinc_truncate(output_path, nchars);
213         retval = out.open(output_path, "ab");
214     } else {
215         retval = out.open(output_path, "wb");
216     }
217     if (retval) {
218         fprintf(stderr, "%s APP: upper_case output open failed:\n",
219             boinc_msg_prefix(buf, sizeof(buf))
220         );
221         fprintf(stderr, "%s resolved name %s, retval %d\n",
222             boinc_msg_prefix(buf, sizeof(buf)), output_path, retval
223         );
224         perror("open");
225         exit(1);
226     }
227 
228 #ifdef APP_GRAPHICS
229     // create shared mem segment for graphics, and arrange to update it
230     //
231     shmem = (UC_SHMEM*)boinc_graphics_make_shmem("uppercase", sizeof(UC_SHMEM));
232     if (!shmem) {
233         fprintf(stderr, "%s failed to create shared mem segment\n",
234             boinc_msg_prefix(buf, sizeof(buf))
235         );
236     }
237     update_shmem();
238     boinc_register_timer_callback(update_shmem);
239 #endif
240 
241     if (network_usage) {
242         boinc_network_usage(5., 17.);
243     }
244 
245     // main loop - read characters, convert to UC, write
246     //
247     for (i=0; ; i++) {
248         c = fgetc(infile);
249         if (c == EOF) break;
250 
251         c = toupper(c);
252         out._putchar(c);
253         nchars++;
254 
255         if (run_slow) {
256             boinc_sleep(1.);
257         }
258 
259         if (early_exit && i>30) {
260             exit(-10);
261         }
262 
263         if (early_crash && i>30) {
264             boinc_crash();
265         }
266         if (early_sleep && i>30) {
267             boinc_disable_timer_thread = true;
268             while (1) boinc_sleep(1);
269         }
270 
271         if (boinc_time_to_checkpoint()) {
272             retval = do_checkpoint(out, nchars);
273             if (retval) {
274                 fprintf(stderr, "%s APP: upper_case checkpoint failed %d\n",
275                     boinc_msg_prefix(buf, sizeof(buf)), retval
276                 );
277                 exit(retval);
278             }
279             boinc_checkpoint_completed();
280         }
281 
282 		if (report_fraction_done) {
283 			fd = nchars/fsize;
284 			if (cpu_time) fd /= 2;
285 			boinc_fraction_done(fd);
286 		}
287     }
288 
289     retval = out.flush();
290     if (retval) {
291         fprintf(stderr, "%s APP: upper_case flush failed %d\n",
292             boinc_msg_prefix(buf, sizeof(buf)), retval
293         );
294         exit(1);
295     }
296 
297     if (trickle_up) {
298         boinc_send_trickle_up(
299             const_cast<char*>("example_app"),
300             const_cast<char*>("sample trickle message")
301         );
302     }
303 
304     if (trickle_down) {
305         boinc_sleep(10);
306         retval = boinc_receive_trickle_down(buf, sizeof(buf));
307         if (!retval) {
308             fprintf(stderr, "Got trickle-down message: %s\n", buf);
309         }
310     }
311 
312     // burn up some CPU time if needed
313     //
314     if (cpu_time) {
315         double start = dtime();
316         for (i=0; ; i++) {
317             double e = dtime()-start;
318             if (e > cpu_time) break;
319 			if (report_fraction_done) {
320 				fd = .5 + .5*(e/cpu_time);
321 				boinc_fraction_done(fd);
322 			}
323 
324             if (boinc_time_to_checkpoint()) {
325                 retval = do_checkpoint(out, nchars);
326                 if (retval) {
327                     fprintf(stderr, "%s APP: upper_case checkpoint failed %d\n",
328                         boinc_msg_prefix(buf, sizeof(buf)), retval
329                     );
330                     exit(1);
331                 }
332                 boinc_checkpoint_completed();
333             }
334             if (critical_section) {
335                 boinc_begin_critical_section();
336             }
337             comp_result = do_some_computing(i);
338             if (critical_section) {
339                 boinc_end_critical_section();
340             }
341         }
342     }
343     boinc_fraction_done(1);
344 #ifdef APP_GRAPHICS
345     update_shmem();
346 #endif
347     boinc_finish(0);
348 }
349