-
Notifications
You must be signed in to change notification settings - Fork 1
/
checkpoint.c
2240 lines (2030 loc) · 58.5 KB
/
checkpoint.c
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* checkpoint - checkpoint and restore stilities
*
* Copyright (c) 2018-2020 by Landon Curt Noll. All Rights Reserved.
*
* Permission to use, copy, modify, and distribute this software and
* its documentation for any purpose and without fee is hereby granted,
* provided that the above copyright, this permission notice and text
* this comment, and the disclaimer below appear in all of the following:
*
* supporting documentation
* source copies
* source works derived from this source
* binaries derived from this source or from derived source
*
* LANDON CURT NOLL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
* INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO
* EVENT SHALL LANDON CURT NOLL BE LIABLE FOR ANY SPECIAL, INDIRECT OR
* CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
* USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
* OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
* PERFORMANCE OF THIS SOFTWARE.
*
* chongo (Landon Curt Noll, http://www.isthe.com/chongo/index.html) /\oo/\
*
* Share and enjoy! :-)
*/
/* NUMERIC EXIT CODES: 70-99 checkpoint.c - reserved for internal errors */
#define _POSIX_SOURCE /* for fileno() */
#define _BSD_SOURCE /* for timerclear() */
#if defined(__APPLE__)
# define _DARWIN_C_SOURCE /* for macOS */
#endif
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <stdarg.h>
#include <errno.h>
#include <stdint.h>
#include <inttypes.h>
#include <time.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <string.h>
#include <sys/stat.h>
#include <signal.h>
#include <gmp.h>
#include <sys/types.h>
#include <sys/file.h>
#include <fcntl.h>
#include <stdbool.h>
#include <bits/local_lim.h>
#include "gmprime.h"
#include "riesel.h"
#include "debug.h"
#include "checkpoint.h"
/*
* checkpoint flags
*/
uint64_t checkpoint_alarm = 0; /* != 0 ==> a SIGALRM or SIGVTALRM went off, checkpoint and continue */
uint64_t checkpoint_and_end = 0; /* != 0 ==> a SIGHUP, SIGINT, SIGQUIT, SIGPIPE went off, checkpoint and exit */
/*
* checkpoint strings values
*/
static pid_t pid; /* our process ID */
static pid_t ppid; /* our parent's process ID */
static char hostname[HOST_NAME_MAX+1]; /* our hostname */
static char cwd[PATH_MAX+1]; /* our current working directory */
/*
* prime test stats
*/
static struct prime_stats beginrun; /* program start (or program restore) prime stats */
static struct prime_stats current; /* prime stats as of now */
static struct prime_stats restored; /* overall prime stats since restore (or start of not prior restore) */
static struct prime_stats total; /* updated total prime stats since start of the primality test */
/*
* static functions
*/
static void record_sigalarm(int signum);
static void record_signal_and_exit(int signum);
static void zerosize_stats(struct prime_stats *ptr);
static void load_prime_stats(struct prime_stats *ptr);
static void careful_write(const char *calling_funcion_name, FILE *stream, char *fmt, ...);
static void write_calc_timeval(FILE *stream, char *basename, char *subname, const struct timeval *value_ptr);
static void write_calc_date_time_str(FILE *stream, char *basename, char *subname, const struct timeval *value_ptr);
static void write_calc_prime_stats_ptr(FILE *stream, char *basename, struct prime_stats *ptr);
static void initialize_total_stats(void);
static void setup_checkpoint(char *checkpoint_dir, int checkpoint_secs);
static int mkdirp(char *path_arg, int mode, int duplicate);
static void setup_chkpt_links(unsigned long h, unsigned long n, unsigned long i, mpz_t u_term);
/*
* record_sigalarm - record a SIGALRM, SIGVTALRM
*
* given:
* signum the signal that has been delivered
*
* This is the signal handler for the SIGALRM or SIGVTALRM.
* It sets the checkpoint_alarm value to 1 and returns.
*
* Another function at a time may consult the checkpoint_alarm
* value and perform the checkpoint as needed.
*
* This function does not return on error.
*/
static void
record_sigalarm(int signum)
{
/*
* note when non-SIGALRM and non-SIGVTALRM is received
*/
if (signum != SIGALRM && signum != SIGVTALRM) {
fflush(stdout);
fflush(stderr);
err(70, __func__, "non-SIGALRM and non-SIGVTALRM detected: %d", signum);
return; // NOT REACHED
}
/*
* checkpoint at next opportunity
*/
if (checkpoint_alarm) {
fflush(stdout);
fflush(stderr);
dbg(DBG_LOW, "previous checkpoint_alarm value not cleared: %ld", checkpoint_and_end);
}
++checkpoint_alarm;
if (checkpoint_alarm <= 0) {
fflush(stdout);
fflush(stderr);
dbg(DBG_LOW, "checkpoint_alarm counter wraparound, reset to 1");
checkpoint_alarm = 1;
}
return;
}
/*
* record_signal_and_exit - record a SIGHUP, SIGINT, SIGQUIT, SIGPIPE
*
* given:
* signum the signal that has been delivered
*
* This is the signal handler for SIGHUP, SIGINT, SIGQUIT, SIGPIPE.
* It sets the checkpoint_alarm value to 1 and returns.
*
* Another function at a time may consult the checkpoint_alarm
* value and perform the checkpoint as needed and then exit.
* This function does not exit. It is the responbility of
* the appropriate checkpoint function to exit.
*
* This function does not return on error.
*/
static void
record_signal_and_exit(int signum)
{
/*
* note when non-SIGALRM is received
*/
switch (signum) {
case SIGHUP:
break;
case SIGINT:
break;
case SIGQUIT:
break;
case SIGPIPE:
break;
default:
fflush(stdout);
fflush(stderr);
err(71, __func__, "unexpected signal caught: %d", signum);
// exit(71);
return; // NOT REACHED
}
/*
* checkpoint at next opportunity and then exit
*/
if (checkpoint_and_end) {
fflush(stdout);
fflush(stderr);
dbg(DBG_LOW, "previous checkpoint_and_end value not cleared: %ld", checkpoint_and_end);
}
++checkpoint_and_end;
if (checkpoint_and_end <= 0) {
fflush(stdout);
fflush(stderr);
dbg(DBG_LOW, "checkpoint_and_end counter wraparound, reset to 1");
checkpoint_and_end = 1;
}
return;
}
/*
* zerosize_stats - zeroize prime stats
*
* given:
* ptr - pointer to a struct prime_stats or NULL
*
* This function does not return on error.
*/
static void
zerosize_stats(struct prime_stats *ptr)
{
/*
* paranoia
*/
if (ptr == NULL) {
err(72, __func__, "ptr is NULL");
return; // NOT REACHED
}
/*
* zeroize the prime stats
*/
memset(ptr, 0, sizeof(struct prime_stats));
timerclear(&ptr->now); // clear the time now
timerclear(&ptr->ru_utime); // clear user CPU time used
timerclear(&ptr->ru_stime); // clear system CPU time used
timerclear(&ptr->wall_clock); // clear wall clock time uused
return;
}
/*
* load_prime_stats - load prime stats
*
* given:
* ptr - pointer to a struct prime_stats or NULL
*
* This function does not return on error.
*/
static void
load_prime_stats(struct prime_stats *ptr)
{
struct rusage usage; // our current resource usage
/*
* paranoia
*/
if (ptr == NULL) {
err(73, __func__, "ptr is NULL");
return; // NOT REACHED
}
/*
* zerosize the stats
*/
zerosize_stats(ptr);
/*
* record the time
*/
if (gettimeofday(&ptr->now, NULL) < 0) {
errp(73, __func__, "gettimeofday error");
return; // NOT REACHED
}
/*
* get resource usage as of now
*/
memset(&usage, 0, sizeof(usage));
timerclear(&usage.ru_utime);
timerclear(&usage.ru_stime);
if (getrusage(RUSAGE_SELF, &usage) < 0) {
errp(73, __func__, "getrusage error");
return; // NOT REACHED
}
/*
* load the relivant resource usage information into our prime stats
*/
ptr->ru_utime = usage.ru_utime;
ptr->ru_stime = usage.ru_stime;
ptr->ru_maxrss = usage.ru_maxrss;
ptr->ru_minflt = usage.ru_minflt;
ptr->ru_majflt = usage.ru_majflt;
ptr->ru_inblock = usage.ru_inblock;
ptr->ru_oublock = usage.ru_oublock;
ptr->ru_nvcsw = usage.ru_nvcsw;
ptr->ru_nivcsw = usage.ru_nivcsw;
/*
* prime stats has been loaded
*/
return;
}
/*
* careful_write - carefully write to an open stream
*
* We will write to an open stream, taking care to detect
* I/O errors, EOF and other problems.
*
* given:
* calling_funcion_name - name of the calling function
* NOTE: usually passed as __func__
* stream - open checkpoint file stream to append to
* fmt - vfprintf format
*
* This function does not return on error.
*/
static void
careful_write(const char *calling_funcion_name, FILE *stream, char *fmt, ...)
{
va_list ap; /* argument pointer */
int ret; /* vfprintf() return value */
/*
* start the var arg setup and fetch our first arg
*/
va_start(ap, fmt);
/*
* firewall
*/
if (calling_funcion_name == NULL) {
va_end(ap); // clean up stdarg stuff
err(74, __func__, "calling_funcion_name is NULL");
return; // NOT REACHED
}
if (stream == NULL) {
va_end(ap); // clean up stdarg stuff
err(74, __func__, "stream is NULL");
return; // NOT REACHED
}
if (fmt == NULL) {
va_end(ap); // clean up stdarg stuff
err(74, __func__, "fmt is NULL");
return; // NOT REACHED
}
if (fileno(stream) < 0) {
va_end(ap); // clean up stdarg stuff
err(74, __func__, "stream is not valid");
return; // NOT REACHED
}
/*
* clear errors and status prior to doing the write
*/
clearerr(stream);
errno = 0;
/*
* perform the write
*/
ret = vfprintf(stream, fmt, ap);
/*
* analize the write
*/
if (ret <= 0 || ferror(stream) || feof(stream)) {
if (feof(stream)) {
errp(74, __func__,
"EOF in careful_write called by %s, errno: %d, ret: %d", calling_funcion_name, errno, ret);
} else if (ferror(stream)) {
errp(74, __func__,
"ferror in careful_write called by %s, errno: %d, ret: %d", calling_funcion_name, errno, ret);
} else {
errp(74, __func__,
"error in careful_write called by %s, errno: %d, ret: %d", calling_funcion_name, errno, ret);
}
return; // NOT REACHED
}
if (debuglevel >= DBG_VVHIGH) {
dbg(DBG_VVHIGH, "careful_write called by %s, vfprintf returned: %d", calling_funcion_name, ret);
}
/*
* no errors detected
*/
return;
}
/*
* write_calc_mpz_hex - write mpz value in hex to an open stream in calc format
*
* given:
* stream - open checkpoint file stream to append to
* basename - base variable name (NULL ==> just use subname)
* subname - subcomponent variable name
* value - const mpz_t value to write in hex
* NOTE: const __mpz_struct * is the same as const mpz_t
*
* This function does not return on error.
*/
void
write_calc_mpz_hex(FILE *stream, char *basename, char *subname, const mpz_t value)
{
int ret; /* mpz_out_str return */
/*
* firewall
*/
if (stream == NULL) {
err(75, __func__, "stream is NULL");
return; // NOT REACHED
}
if (subname == NULL) {
err(75, __func__, "subname is NULL");
return; // NOT REACHED
}
if (value == NULL) {
err(75, __func__, "value is NULL");
return; // NOT REACHED
}
/*
* write hex variable prefix
*/
if (basename == NULL) {
careful_write(__func__, stream, "%s = 0x", subname);
} else {
careful_write(__func__, stream, "%s_%s = 0x", basename, subname);
}
/*
* write value in hex
*
* NOTE: We have to duplicate some of the careful_write() logic
* because mpz_out_str returns 0 on I/O error.
*/
clearerr(stream);
errno = 0;
ret = mpz_out_str(stream, 16, value);
if (ret <= 0 || ferror(stream) || feof(stream)) {
if (feof(stream)) {
errp(75, __func__, "EOF during mpz_out_str called from %s, errno: %d, ret: %d", __func__, errno, ret);
} else if (ferror(stream)) {
errp(75, __func__, "ferror during mpz_out_str called from %s, errno: %d, ret: %d", __func__, errno, ret);
} else {
errp(75, __func__, "ferror during mpz_out_str called from %s, errno: %d, ret: %d", __func__, errno, ret);
}
return; // NOT REACHED
}
/*
* write hex variable suffix
*/
careful_write(__func__, stream, " ;\n");
/*
* no errors detected
*/
return;
}
/*
* write_calc_int64_t - write int64_t value to an open stream in calc format
*
* given:
* stream - open checkpoint file stream to append to
* basename - base variable name (NULL ==> just use subname)
* subname - subcomponent variable name
* value - int64_t value to write
*
* This function does not return on error.
*/
void
write_calc_int64_t(FILE *stream, char *basename, char *subname, const int64_t value)
{
/*
* firewall
*/
if (stream == NULL) {
err(76, __func__, "stream is NULL");
return; // NOT REACHED
}
if (subname == NULL) {
err(76, __func__, "subname is NULL");
return; // NOT REACHED
}
/*
* write the calc expression
*/
if (basename == NULL) {
careful_write(__func__, stream, "%s = %" PRId64 " ;\n", subname, value);
} else {
careful_write(__func__, stream, "%s_%s = %" PRId64 " ;\n", basename, subname, value);
}
/*
* no errors detected
*/
return;
}
/*
* write_calc_uint64_t - write uint64_t value to an open stream in calc format
*
* given:
* stream - open checkpoint file stream to append to
* basename - base variable name (NULL ==> just use subname)
* subname - subcomponent variable name
* value - uint64_t value to write
*
* This function does not return on error.
*/
void
write_calc_uint64_t(FILE *stream, char *basename, char *subname, const uint64_t value)
{
/*
* firewall
*/
if (stream == NULL) {
err(77, __func__, "stream is NULL");
return; // NOT REACHED
}
if (subname == NULL) {
err(77, __func__, "subname is NULL");
return; // NOT REACHED
}
/*
* write the calc expression
*/
if (basename == NULL) {
careful_write(__func__, stream, "%s = %" PRIu64 " ;\n", subname, value);
} else {
careful_write(__func__, stream, "%s_%s = %" PRIu64 " ;\n", basename, subname, value);
}
/*
* no errors detected
*/
return;
}
/*
* write_calc_str - write string to an open stream in calc format
*
* given:
* stream - open checkpoint file stream to append to
* basename - base variable name (NULL ==> just use subname)
* subname - subcomponent variable name
* value - string to write
*
* This function does not return on error.
*/
void
write_calc_str(FILE *stream, char *basename, char *subname, const char *value)
{
/*
* firewall
*/
if (stream == NULL) {
err(78, __func__, "stream is NULL");
return; // NOT REACHED
}
if (subname == NULL) {
err(78, __func__, "subname is NULL");
return; // NOT REACHED
}
if (value == NULL) {
err(78, __func__, "value is NULL");
return; // NOT REACHED
}
/*
* write the calc expression
*/
if (basename == NULL) {
careful_write(__func__, stream, "%s = \"%s\" ;\n", subname, value);
} else {
careful_write(__func__, stream, "%s_%s = \"%s\" ;\n", basename, subname, value);
}
/*
* no errors detected
*/
return;
}
/*
* write_calc_timeval - write a time value in seconds.microseconds to an open stream in calc format
*
* given:
* stream - open checkpoint file stream to append to
* basename - base variable name (NULL ==> just use subname)
* subname - subcomponent variable name
* value_ptr - pointer to a struct timeval
*
* This function does not return on error.
*/
static void
write_calc_timeval(FILE *stream, char *basename, char *subname, const struct timeval *value_ptr)
{
/*
* firewall
*/
if (stream == NULL) {
err(79, __func__, "stream is NULL");
return; // NOT REACHED
}
if (subname == NULL) {
err(79, __func__, "subname is NULL");
return; // NOT REACHED
}
if (value_ptr == NULL) {
err(79, __func__, "value_ptr is NULL");
return; // NOT REACHED
}
/*
* write the calc expression
*/
if (basename == NULL) {
careful_write(__func__, stream, "%s = %" PRIu64 ".%06d ;\n", subname, value_ptr->tv_sec, value_ptr->tv_usec);
} else {
careful_write(__func__, stream, "%s_%s = %" PRIu64 ".%06d ;\n",
basename, subname, value_ptr->tv_sec, value_ptr->tv_usec);
}
/*
* no errors detected
*/
return;
}
/*
* write_calc_date_time_str - write a time value in date time string to an open stream in calc format
*
* given:
* stream - open checkpoint file stream to append to
* basename - base variable name (NULL ==> just use subname)
* subname - subcomponent variable name
* value_ptr - pointer to a struct timeval
*
* This function does not return on error.
*/
static void
write_calc_date_time_str(FILE *stream, char *basename, char *subname, const struct timeval *value_ptr)
{
struct tm *tm_time; /* broken-down time */
char buf[BUFSIZ + 1]; /* time string buffer */
int ret; /* gmtime() and strftime() return */
/*
* firewall
*/
if (stream == NULL) {
err(80, __func__, "stream is NULL");
return; // NOT REACHED
}
if (subname == NULL) {
err(80, __func__, "subname is NULL");
return; // NOT REACHED
}
if (value_ptr == NULL) {
err(80, __func__, "value_ptr is NULL");
return; // NOT REACHED
}
/*
* convert timeval to broken-down time
*/
tm_time = gmtime(&value_ptr->tv_sec);
if (tm_time == NULL) {
errp(80, __func__, "gmtime returned NULL, errno: %d", errno);
return; // NOT REACHED
}
/*
* format as a date and time string
*/
memset(buf, 0, sizeof(buf));
ret = strftime(buf, BUFSIZ, "%F %T UTC", tm_time);
if (ret <= 0) {
errp(80, __func__, "strftime returned %d, errno: %d", ret, errno);
return; // NOT REACHED
}
buf[BUFSIZ] = '\0'; // paranoia
/*
* write date and time as a calc string
*/
write_calc_str(stream, basename, subname, buf);
/*
* no errors detected
*/
return;
}
/*
* write_calc_prime_stats_ptr - write prime stats to an open stream in calc format
*
* given:
* stream - open checkpoint file stream to append to
* basename - base variable name
* value_ptr - pointer to a struct prime_stats
*
* This function does not return on error.
*/
static void
write_calc_prime_stats_ptr(FILE *stream, char *basename, struct prime_stats *ptr)
{
/*
* firewall
*/
if (stream == NULL) {
err(81, __func__, "stream is NULL");
return; // NOT REACHED
}
if (basename == NULL) {
err(81, __func__, "basename is NULL");
return; // NOT REACHED
}
if (ptr == NULL) {
err(81, __func__, "ptr is NULL");
return; // NOT REACHED
}
/*
* write start time as a timestamp since the epoch
*/
write_calc_timeval(stream, basename, "timestamp", &ptr->now);
/*
* write start time as a timestamp since as a string
*/
write_calc_date_time_str(stream, basename, "date_time", &ptr->now);
/*
* write user CPU time used
*/
write_calc_timeval(stream, basename, "ru_utime", &ptr->ru_utime);
/*
* write system CPU time used
*/
write_calc_timeval(stream, basename, "ru_stime", &ptr->ru_stime);
/*
* write wall clock time used
*/
write_calc_timeval(stream, basename, "wall_clock", &ptr->wall_clock);
/*
* write maximum resident set size used in kilobytes
*/
write_calc_int64_t(stream, basename, "ru_maxrss", ptr->ru_maxrss);
/*
* write page reclaims (soft page faults)
*/
write_calc_int64_t(stream, basename, "ru_minflt", ptr->ru_minflt);
/*
* write page faults (hard page faults)
*/
write_calc_int64_t(stream, basename, "ru_majflt", ptr->ru_majflt);
/*
* write block input operations
*/
write_calc_int64_t(stream, basename, "ru_inblock", ptr->ru_inblock);
/*
* write block output operations
*/
write_calc_int64_t(stream, basename, "ru_oublock", ptr->ru_oublock);
/*
* write voluntary context switches
*/
write_calc_int64_t(stream, basename, "ru_nvcsw", ptr->ru_nvcsw);
/*
* write involuntary context switches
*/
write_calc_int64_t(stream, basename, "ru_nivcsw", ptr->ru_nivcsw);
/*
* no errors detected
*/
return;
}
/*
* write_calc_prime_stats - write prime stats to an open stream in calc format
*
* given:
* stream - open checkpoint file stream to append to
* extended - true ==> write only total stats, false ==> write all stats
*
* This function does not return on error.
*/
void
write_calc_prime_stats(FILE *stream, bool extended)
{
/*
* firewall
*/
if (stream == NULL) {
err(82, __func__, "stream is NULL");
return; // NOT REACHED
}
/*
* if -t was given
*/
if (extended) {
/*
* write beginrun stats
*/
write_calc_prime_stats_ptr(stream, "beginrun", &beginrun);
/*
* write current stats
*/
write_calc_prime_stats_ptr(stream, "current", ¤t);
/*
* write restored stats
*/
write_calc_prime_stats_ptr(stream, "restored", &restored);
}
/*
* write total stats
*/
write_calc_prime_stats_ptr(stream, "total", &total);
/*
* no errors detected
*/
return;
}
/*
* initialize_beginrun_stats - setup prime stats for the start of this run
*/
void
initialize_beginrun_stats(void)
{
/*
* initialize start of run prime stats
*/
load_prime_stats(&beginrun);
/*
* beginrun has been initialized
*/
return;
}
/*
* initialize_total_stats - setup prime stats for the start a primality test
*
* NOTE: This function assumes that initialize_beginrun_stats() was already
* called and that beginrun contains info from the start of this program.
*/
static void
initialize_total_stats(void)
{
/*
* no prior restore so clear restored stats
*/
zerosize_stats(&restored);
restored.now = beginrun.now;
restored.ru_maxrss = beginrun.ru_maxrss;
/*
* total will be the same as cleared restore stats
*/
total = restored;
/*
* beginrun and total have been initialized
*/
return;
}
/*
* update_stats - update prime stats
*
* NOTE: This updates both the stats for this run and the stats
* for the entire primality test.
*/
void
update_stats(void)
{
struct timeval diff; // difference between now and start
/*
* load prime stats for this checkpoint
*/
load_prime_stats(¤t);
/*
* update now
*/
total.now = current.now;
/*
* update user CPU time used
*/
if (!timercmp(¤t.ru_utime, &beginrun.ru_utime, <)) {
timersub(¤t.ru_utime, &beginrun.ru_utime, &diff);
} else {
warn(__func__, "user CPU time went backwards, assuming 0 difference");
timerclear(&diff);
}
timeradd(&restored.ru_utime, &diff, &total.ru_utime);
/*
* update system CPU time used
*/
if (!timercmp(¤t.ru_stime, &beginrun.ru_stime, <)) {
timersub(¤t.ru_stime, &beginrun.ru_stime, &diff);
} else {
warn(__func__, "user system CPU time went backwards, assuming 0 difference");
timerclear(&diff);
}
timeradd(&restored.ru_stime, &diff, &total.ru_stime);
/*
* update wall clock time used
*/
if (!timercmp(¤t.now, &beginrun.now, <)) {
timersub(¤t.now, &beginrun.now, &diff);
} else {
warn(__func__, "user wall clock time went backwards, assuming 0 difference");
timerclear(&diff);
}
current.wall_clock = diff;
timeradd(&restored.wall_clock, &diff, &total.wall_clock);
/*
* update maximum resident set size used in kilobytes
*/
if (current.ru_maxrss > total.ru_maxrss) {
total.ru_maxrss = current.ru_maxrss;
}
/*
* update page reclaims (soft page faults)
*/
total.ru_minflt = current.ru_minflt - beginrun.ru_minflt + restored.ru_minflt;
/*
* update page faults (hard page faults)
*/
total.ru_majflt = current.ru_majflt - beginrun.ru_majflt + restored.ru_majflt;
/*
* update block input operations
*/
total.ru_inblock = current.ru_inblock - beginrun.ru_inblock + restored.ru_inblock;
/*
* update block output operations
*/
total.ru_oublock = current.ru_oublock - beginrun.ru_oublock + restored.ru_oublock;
/*
* update voluntary context switches
*/
total.ru_nvcsw = current.ru_nvcsw - beginrun.ru_nvcsw + restored.ru_nvcsw;
/*
* update involuntary context switches
*/
total.ru_nivcsw = current.ru_nivcsw - beginrun.ru_nivcsw + restored.ru_nivcsw;
/*
* stats and been updated
*/
return;
}
/*
* initialize_checkpoint - setup checkpoint system
*