-
Notifications
You must be signed in to change notification settings - Fork 36
/
scr.c
4009 lines (3464 loc) · 128 KB
/
scr.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
/*
* Copyright (c) 2009, Lawrence Livermore National Security, LLC.
* Produced at the Lawrence Livermore National Laboratory.
* Written by Adam Moody <moody20@llnl.gov>.
* LLNL-CODE-411039.
* All rights reserved.
* This file is part of The Scalable Checkpoint / Restart (SCR) library.
* For details, see https://sourceforge.net/projects/scalablecr/
* Please also read this file: LICENSE.TXT.
*/
/* All rights reserved. This program and the accompanying materials
* are made available under the terms of the BSD-3 license which accompanies this
* distribution in LICENSE.TXT
*
* This library 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 BSD-3 License in
* LICENSE.TXT for more details.
*
* GOVERNMENT LICENSE RIGHTS-OPEN SOURCE SOFTWARE
* The Government's rights to use, modify, reproduce, release, perform,
* display, or disclose this software are subject to the terms of the BSD-3
* License as provided in Contract No. B609815.
* Any reproduction of computer software, computer software documentation, or
* portions thereof marked with this legend must also reproduce the markings.
*
* Author: Christopher Holguin <christopher.a.holguin@intel.com>
*
* (C) Copyright 2015-2016 Intel Corporation.
*/
#include <assert.h>
#include "scr_globals.h"
#include "dtcmp.h"
#include "er.h"
#include "axl_mpi.h"
/* define which state we're in for API calls, this is to help ensure
* users call SCR functions in the correct order */
typedef enum {
SCR_STATE_UNINIT, /* before init and after finalize */
SCR_STATE_IDLE, /* between init/finalize */
SCR_STATE_RESTART, /* between start/complete restart */
SCR_STATE_CHECKPOINT, /* between start/complete checkpoint */
SCR_STATE_OUTPUT /* between start/complete output */
} SCR_STATE;
/* initialize our state to uninit */
static SCR_STATE scr_state = SCR_STATE_UNINIT;
/* tracks set of files in current dataset */
static scr_filemap* scr_map = NULL;
/* tracks redundancy descriptor for current dataset */
static scr_reddesc* scr_rd = NULL;
/* tracks whether a checkpoint is available for restart */
static int scr_have_restart;
static double scr_time_compute_start; /* records the start time of the current compute phase */
static double scr_time_compute_end; /* records the end time of the current compute phase */
static double scr_time_checkpoint_start; /* records the start time of the current checkpoint */
static double scr_time_checkpoint_end; /* records the end time of the current checkpoint */
static double scr_time_output_start; /* records the start time of the current output phase */
static double scr_time_output_end; /* records the end time of the current output phase */
static double scr_time_write_start; /* records the start time of the application write portion of the output phase */
static time_t scr_timestamp_output_start; /* record timestamp of start of output phase */
/* look up redundancy descriptor we should use for this dataset */
static scr_reddesc* scr_get_reddesc(const scr_dataset* dataset, int ndescs, scr_reddesc* descs)
{
int i;
/* assume we won't find one */
scr_reddesc* d = NULL;
/* determine whether dataset is flagged as output */
int is_output = scr_dataset_is_output(dataset);
/* if it's output, and if a reddesc is marked for output, use that one */
if (is_output) {
for (i=0; i < ndescs; i++) {
if (descs[i].enabled &&
descs[i].output)
{
/* found a reddesc explicitly marked for output */
d = &descs[i];
return d;
}
}
}
/* dataset is either not output, or one redundancy descriptor has not been marked
* explicitly for output */
/* determine whether dataset is a checkpoint */
int is_ckpt = scr_dataset_is_ckpt(dataset);
/* multi-level checkpoint, pick the right level */
if (is_ckpt) {
/* get our checkpoint id */
int ckpt_id;
if (scr_dataset_get_ckpt(dataset, &ckpt_id) == SCR_SUCCESS) {
/* got our id, now pick the redundancy descriptor that is:
* 1) enabled
* 2) has the highest interval that evenly divides id */
int i;
int interval = 0;
for (i=0; i < ndescs; i++) {
if (descs[i].enabled &&
interval < descs[i].interval &&
ckpt_id % descs[i].interval == 0)
{
d = &descs[i];
interval = descs[i].interval;
}
}
}
} else {
/* dataset is not a checkpoint, but there is no reddesc explicitly
* for output either, pick an enabled reddesc with interval 1 */
int i;
for (i=0; i < ndescs; i++) {
if (descs[i].enabled &&
descs[i].interval == 1)
{
d = &descs[i];
return d;
}
}
}
return d;
}
/* returns 1 if dataset id can be finalized in poststage, 0 otherwise */
static int scr_flush_can_poststage(int id)
{
int poststage = 0;
/* currently only BBAPI can support poststage */
const scr_storedesc* storedesc = scr_cache_get_storedesc(scr_cindex, id);
const char* type = storedesc->xfer;
if (strcmp(type, "BBAPI") == 0) {
poststage = 1;
}
return poststage;
}
static int scr_flush_finalize(void)
{
/* When using poststage, we can finalize flushes
* after the job completes rather than waiting on them here. */
int poststage = scr_flush_poststage;
/* check each outstanding async flush, if any */
if (poststage && scr_flush_async_in_progress()) {
/* Got at least one outstanding async flush.
* Check whether all async flushes can be handled in poststage.
* Iterate over all dataset ids still being flushed.
* If any can not be completed in poststage, disable all poststage. */
/* get ordered list of ids being flushed */
int flush_num = 0;
int* flush_ids = NULL;
scr_flush_async_get_list(scr_cindex, &flush_num, &flush_ids);
/* check that all flushes are using something we can complete in poststage */
int i;
for (i = 0; i < flush_num; i++) {
int id = flush_ids[i];
if (! scr_flush_can_poststage(id)) {
poststage = 0;
}
}
/* free list of flush ids */
scr_free(&flush_ids);
}
/* check latest checkpoint if it still needs to be flushed */
if (poststage && scr_flush > 0 && scr_flush_file_need_flush(scr_ckpt_dset_id)) {
/* latest checkpoint needs to be flushed,
* check whether we can also handle this checkpoint with poststage */
if (scr_flush_can_poststage(scr_ckpt_dset_id)) {
/* can finalize this flush in poststage */
if (! scr_flush_file_is_flushing(scr_ckpt_dset_id)) {
/* Initiate the flush now, but finish in the poststage.
* Start as an async flush, even if we're otherwise using sync flush. */
int flush_rc = scr_flush_async_start(scr_cindex, scr_ckpt_dset_id);
if (flush_rc != SCR_SUCCESS) {
scr_err("Flush of dataset %d failed @ %s:%d",
scr_ckpt_dset_id, __FILE__, __LINE__
);
}
}
} else {
/* cannot use poststage, so disable */
poststage = 0;
}
}
/* if we're not using postage, wait on all flushes now */
if (! poststage) {
/* wait on all async flushes to complete */
if (scr_flush_async_in_progress()) {
/* if any flush cannot use poststage, wait on them all to finish now */
int flush_rc = scr_flush_async_waitall(scr_cindex);
if (flush_rc != SCR_SUCCESS) {
scr_err("Flush of datasets failed @ %s:%d",
__FILE__, __LINE__
);
}
}
/* Flush checkpoint synchronously (wait for it to finish now). */
if (scr_flush > 0 && scr_flush_file_need_flush(scr_ckpt_dset_id)) {
int flush_rc = scr_flush_sync(scr_cindex, scr_ckpt_dset_id);
if (flush_rc != SCR_SUCCESS) {
scr_err("Flush of dataset %d failed @ %s:%d",
scr_ckpt_dset_id, __FILE__, __LINE__
);
}
}
}
if (scr_flush_async) {
scr_flush_async_finalize();
}
return SCR_SUCCESS;
}
/*
=========================================
Halt logic
=========================================
*/
#define SCR_TEST_AND_HALT (1)
#define SCR_TEST_BUT_DONT_HALT (2)
#define SCR_FINALIZE_CALLED "SCR_FINALIZE_CALLED"
/* writes entry to halt file to indicate that SCR should exit job at first opportunity */
static int scr_halt(const char* reason)
{
/* copy reason if one was given */
if (reason != NULL) {
kvtree_util_set_str(scr_halt_hash, SCR_HALT_KEY_EXIT_REASON, reason);
}
/* log the halt condition */
if (scr_log_enable) {
scr_log_halt(reason);
}
/* and write out the halt file */
int rc = scr_halt_sync_and_decrement(scr_halt_file, scr_halt_hash, 0);
return rc;
}
/* check whether we should halt the job */
static int scr_bool_check_halt_and_decrement(int halt_cond, int decrement)
{
/* assume we don't have to halt */
int need_to_halt = 0;
/* determine whether we should halt the job by calling exit
* if we detect an active halt condition */
int halt_exit = ((halt_cond == SCR_TEST_AND_HALT) && scr_halt_exit);
/* only rank 0 reads the halt file */
if (scr_my_rank_world == 0) {
/* TODO: all epochs are stored in ints, should be in unsigned ints? */
/* get current epoch seconds */
struct timeval tv;
gettimeofday(&tv, NULL);
int now = tv.tv_sec;
/* locks halt file, reads it to pick up new values, decrements the
* checkpoint counter, writes it out, and unlocks it */
scr_halt_sync_and_decrement(scr_halt_file, scr_halt_hash, decrement);
/* set halt seconds to value found in our halt hash */
int halt_seconds;
if (kvtree_util_get_int(scr_halt_hash, SCR_HALT_KEY_SECONDS, &halt_seconds) != KVTREE_SUCCESS) {
/* didn't find anything, so set value to 0 */
halt_seconds = 0;
}
/* if halt secs enabled, check the remaining time */
if (halt_seconds > 0) {
long int remaining = scr_env_seconds_remaining();
if (remaining >= 0 && remaining <= halt_seconds) {
if (halt_exit) {
scr_dbg(0, "Job exiting: Reached time limit: (seconds remaining = %ld) <= (SCR_HALT_SECONDS = %d).",
remaining, halt_seconds
);
scr_halt("TIME_LIMIT");
}
need_to_halt = 1;
}
}
/* check whether a reason has been specified */
char* reason;
if (kvtree_util_get_str(scr_halt_hash, SCR_HALT_KEY_EXIT_REASON, &reason) == KVTREE_SUCCESS) {
if (strcmp(reason, "") != 0) {
/* got a reason, but let's ignore SCR_FINALIZE_CALLED if it's set
* and assume user restarted intentionally */
if (strcmp(reason, SCR_FINALIZE_CALLED) != 0) {
/* since reason points at the EXIT_REASON string in the halt hash, and since
* scr_halt() resets this value, we need to copy the current reason */
char* tmp_reason = strdup(reason);
if (halt_exit && tmp_reason != NULL) {
scr_dbg(0, "Job exiting: Reason: %s.", tmp_reason);
scr_halt(tmp_reason);
}
scr_free(&tmp_reason);
need_to_halt = 1;
}
}
}
/* check whether we are out of checkpoints */
int checkpoints_left;
if (kvtree_util_get_int(scr_halt_hash, SCR_HALT_KEY_CHECKPOINTS, &checkpoints_left) == KVTREE_SUCCESS) {
if (checkpoints_left == 0) {
if (halt_exit) {
scr_dbg(0, "Job exiting: No more checkpoints remaining.");
scr_halt("NO_CHECKPOINTS_LEFT");
}
need_to_halt = 1;
}
}
/* check whether we need to exit before a specified time */
int exit_before;
if (kvtree_util_get_int(scr_halt_hash, SCR_HALT_KEY_EXIT_BEFORE, &exit_before) == KVTREE_SUCCESS) {
if (now >= (exit_before - halt_seconds)) {
if (halt_exit) {
time_t time_now = (time_t) now;
time_t time_exit = (time_t) exit_before - halt_seconds;
char str_now[256];
char str_exit[256];
strftime(str_now, sizeof(str_now), "%c", localtime(&time_now));
strftime(str_exit, sizeof(str_exit), "%c", localtime(&time_exit));
scr_dbg(0, "Job exiting: Current time (%s) is past ExitBefore-HaltSeconds time (%s).",
str_now, str_exit
);
scr_halt("EXIT_BEFORE_TIME");
}
need_to_halt = 1;
}
}
/* check whether we need to exit after a specified time */
int exit_after;
if (kvtree_util_get_int(scr_halt_hash, SCR_HALT_KEY_EXIT_AFTER, &exit_after) == KVTREE_SUCCESS) {
if (now >= exit_after) {
if (halt_exit) {
time_t time_now = (time_t) now;
time_t time_exit = (time_t) exit_after;
char str_now[256];
char str_exit[256];
strftime(str_now, sizeof(str_now), "%c", localtime(&time_now));
strftime(str_exit, sizeof(str_exit), "%c", localtime(&time_exit));
scr_dbg(0, "Job exiting: Current time (%s) is past ExitAfter time (%s).", str_now, str_exit);
scr_halt("EXIT_AFTER_TIME");
}
need_to_halt = 1;
}
}
}
/* broadcast halt decision from rank 0 */
MPI_Bcast(&need_to_halt, 1, MPI_INT, 0, scr_comm_world);
/* halt job if we need to, and flush latest checkpoint if needed */
if (need_to_halt && halt_exit) {
/* flush any pending datasets and shut down flush methods */
scr_flush_finalize();
/* sync up tasks before exiting (don't want tasks to exit so early that
* runtime kills others after timeout) */
MPI_Barrier(scr_comm_world);
/* and exit the job */
exit(0);
}
return need_to_halt;
}
/*
=========================================
Utility functions
=========================================
*/
/* flush the specified dataset id if needed */
static int scr_check_flush_id(scr_cache_index* cindex, int id)
{
/* assume we don't have to flush */
int need_flush = 0;
/* get info for current dataset */
scr_dataset* dataset = scr_dataset_new();
scr_cache_index_get_dataset(cindex, id, dataset);
/* if this is output we have to flush */
int is_output = scr_dataset_is_output(dataset);
if (is_output) {
need_flush = 1;
}
/* check whether user has flush enabled */
if (scr_flush > 0) {
/* TODO: get checkpoint id for dataset */
/* if this is a checkpoint, then every scr_flush checkpoints, flush the checkpoint set */
int is_ckpt = scr_dataset_is_ckpt(dataset);
if (is_ckpt) {
/* get checkpoint id for dataset */
int ckpt_id = 0;
scr_dataset_get_ckpt(dataset, &ckpt_id);
if (ckpt_id > 0 && ckpt_id % scr_flush == 0) {
need_flush = 1;
}
}
}
/* flush the dataset if needed */
if (need_flush) {
/* need to flush, determine whether to use async or sync flush */
if (scr_flush_async) {
/* start an async flush on the current dataset id */
scr_flush_async_start(cindex, id);
} else {
/* synchronously flush the current dataset */
int flush_rc = scr_flush_sync(cindex, id);
if (flush_rc != SCR_SUCCESS) {
scr_abort(-1, "Flush of dataset %d failed @ %s:%d",
id, __FILE__, __LINE__
);
}
}
}
/* free the dataset info */
scr_dataset_delete(&dataset);
return SCR_SUCCESS;
}
/* check whether a flush is needed, and execute flush if so */
static int scr_check_flush(scr_cache_index* cindex)
{
int rc= scr_check_flush_id(cindex, scr_dataset_id);
return rc;
}
/* on restart, check each cached dataset to see whether it should be flushed,
* to be called after scr_cache_rebuild and scr_flush_file_rebuild */
int scr_flush_restart(scr_cache_index* cindex)
{
/* get ordered list of dataset ids in cache */
int ndsets;
int* dsets;
scr_cache_index_list_datasets(cindex, &ndsets, &dsets);
/* iterate over ordered list of datasets in cache,
* flush each dataset if needed */
int i;
for (i = 0; i < ndsets; i++) {
int id = dsets[i];
/* check whether we need to flush data */
if (scr_flush_on_restart) {
/* TODO: We could be more efficient here.
* This may flush some checkpoints that aren't needed.
* For example, if we have two checkpoints in cache, we really
* only need to flush the most recent one. We could also
* avoid flushing a pure-output dataset that comes after the most
* recent checkpoint since in theory it'll be overwritten anyway. */
/* Application wants the latest checkpoint flushed to be able
* to read it during restart, so force a sync flush.
* We flush everything with sync here to maintain
* proper ordering of the current marker. */
int flush_rc = scr_flush_sync(cindex, id);
if (flush_rc != SCR_SUCCESS) {
scr_abort(-1, "Flush of dataset %d failed @ %s:%d",
id, __FILE__, __LINE__
);
}
} else {
/* otherwise, flush only if we need to flush */
scr_check_flush_id(cindex, id);
}
}
/* free allocated list of checkpoint ids */
scr_free(&dsets);
return SCR_SUCCESS;
}
/* given a dataset id and a filename,
* return the full path to the file which the caller should use to access the file */
static int scr_route_file(int id, const char* file, char* newfile, int n)
{
/* check that we got a file and newfile to write to */
if (file == NULL || strcmp(file, "") == 0 || newfile == NULL) {
return SCR_FAILURE;
}
/* check that user's filename is not too long */
if (strlen(file) >= SCR_MAX_FILENAME) {
scr_abort(-1, "file name (%s) is longer than SCR_MAX_FILENAME (%d) @ %s:%d",
file, SCR_MAX_FILENAME, __FILE__, __LINE__
);
}
/* convert path string to path object */
spath* path_file = spath_from_str(file);
/* determine whether we're in bypass mode for this dataset */
int bypass = 0;
scr_cache_index_get_bypass(scr_cindex, id, &bypass);
/* if we're in bypass route file to its location in prefix directory,
* otherwise place it in a cache directory */
if (bypass) {
/* build absolute path to file */
if (! spath_is_absolute(path_file)) {
/* the path is not absolute, so prepend the current working directory */
char cwd[SCR_MAX_FILENAME];
if (scr_getcwd(cwd, sizeof(cwd)) == SCR_SUCCESS) {
spath_prepend_str(path_file, cwd);
} else {
/* problem acquiring current working directory */
scr_abort(-1, "Failed to build absolute path to %s @ %s:%d",
file, __FILE__, __LINE__
);
}
}
/* TODO: should we check path is a child in prefix here? */
} else {
/* lookup the cache directory for this dataset */
char* dir = NULL;
scr_cache_index_get_dir(scr_cindex, id, &dir);
/* chop file to just the file name and prepend directory */
spath_basename(path_file);
spath_prepend_str(path_file, dir);
}
/* simplify the absolute path (removes "." and ".." entries) */
spath_reduce(path_file);
/* copy to user's buffer */
size_t n_size = (size_t) n;
spath_strcpy(newfile, n_size, path_file);
/* free the file path */
spath_delete(&path_file);
return SCR_SUCCESS;
}
/* given the current state, abort with an informative error message */
static void scr_state_transition_error(int state, const char* function, const char* file, int line)
{
switch(state) {
case SCR_STATE_UNINIT:
/* tried to call some SCR function while uninitialized */
scr_abort(-1, "Must call SCR_Init() before %s @ %s:%d",
function, file, line
);
break;
case SCR_STATE_RESTART:
/* tried to call some SCR function while in Start_restart region */
scr_abort(-1, "Must call SCR_Complete_restart() before %s @ %s:%d",
function, file, line
);
break;
case SCR_STATE_CHECKPOINT:
/* tried to call some SCR function while in Start_checkpoint region */
scr_abort(-1, "Must call SCR_Complete_checkpoint() before %s @ %s:%d",
function, file, line
);
break;
case SCR_STATE_OUTPUT:
/* tried to call some SCR function while in Start_output region */
scr_abort(-1, "Must call SCR_Complete_output() before %s @ %s:%d",
function, file, line
);
break;
}
/* fall back message, less informative, but at least something */
scr_abort(-1, "Called %s from invalid state %d @ %s:%d",
function, state, file, line
);
return;
}
/*
=========================================
Configuration parameters
=========================================
*/
/* read in environment variables */
static int scr_get_params()
{
const char* value;
kvtree* tmp;
double d;
unsigned long long ull;
/* TODO: move these into scr_param_init so that scr_enabled is available eg in SCR_Config */
/* user may want to disable SCR at runtime, read env var to avoid reading config files */
if ((value = getenv("SCR_ENABLE")) != NULL) {
scr_enabled = atoi(value);
}
/* if not enabled, bail with an error */
if (! scr_enabled) {
return SCR_FAILURE;
}
/* read in our configuration parameters */
scr_param_init();
/* check enabled parameter again, this time including settings from config files */
if ((value = scr_param_get("SCR_ENABLE")) != NULL) {
scr_enabled = atoi(value);
}
if (scr_my_rank_world == 0) {
scr_dbg(1, "SCR_ENABLE=%d", scr_enabled);
}
/* if not enabled, bail with an error */
if (! scr_enabled) {
scr_param_finalize();
return SCR_FAILURE;
}
/* set debug verbosity level */
if ((value = scr_param_get("SCR_DEBUG")) != NULL) {
scr_debug = atoi(value);
}
if (scr_my_rank_world == 0) {
scr_dbg(1, "SCR_DEBUG=%d", scr_debug);
}
/* set scr_prefix_path and scr_prefix */
value = scr_param_get("SCR_PREFIX");
scr_prefix_path = scr_get_prefix(value);
scr_prefix = spath_strdup(scr_prefix_path);
if (scr_my_rank_world == 0) {
scr_dbg(1, "SCR_PREFIX=%s", scr_prefix);
}
/* define the path to the .scr subdir within the prefix dir */
spath* path_prefix_scr = spath_dup(scr_prefix_path);
spath_append_str(path_prefix_scr, ".scr");
scr_prefix_scr = spath_strdup(path_prefix_scr);
spath_delete(&path_prefix_scr);
/* TODO: create store descriptor for prefix directory */
/* create the .scr subdirectory */
if (scr_my_rank_world == 0) {
mode_t mode_dir = scr_getmode(1, 1, 1);
if (scr_mkdir(scr_prefix_scr, mode_dir) != SCR_SUCCESS) {
scr_abort(-1, "Failed to create .scr subdirectory %s @ %s:%d",
scr_prefix_scr, __FILE__, __LINE__
);
}
}
/* set logging */
if ((value = scr_param_get("SCR_LOG_ENABLE")) != NULL) {
scr_log_enable = atoi(value);
}
if (scr_my_rank_world == 0) {
scr_dbg(1, "SCR_LOG_ENABLE=%d", scr_log_enable);
}
/* check whether SCR logging DB is enabled */
if ((value = scr_param_get("SCR_LOG_TXT_ENABLE")) != NULL) {
scr_log_txt_enable = atoi(value);
}
if (scr_my_rank_world == 0) {
scr_dbg(1, "SCR_LOG_TXT_ENABLE=%d", scr_log_txt_enable);
}
/* check whether SCR logging DB is enabled */
if ((value = scr_param_get("SCR_LOG_SYSLOG_ENABLE")) != NULL) {
scr_log_syslog_enable = atoi(value);
}
if (scr_my_rank_world == 0) {
scr_dbg(1, "SCR_LOG_SYSLOG_ENABLE=%d", scr_log_syslog_enable);
}
/* check whether SCR logging DB is enabled */
if ((value = scr_param_get("SCR_LOG_DB_ENABLE")) != NULL) {
scr_log_db_enable = atoi(value);
}
if (scr_my_rank_world == 0) {
scr_dbg(1, "SCR_LOG_DB_ENABLE=%d", scr_log_db_enable);
}
/* read in the debug level for database log messages */
if ((value = scr_param_get("SCR_LOG_DB_DEBUG")) != NULL) {
scr_log_db_debug = atoi(value);
}
if (scr_my_rank_world == 0) {
scr_dbg(1, "SCR_LOG_DB_DEBUG=%d", scr_log_db_debug);
}
/* SCR log DB connection parameters */
if ((value = scr_param_get("SCR_LOG_DB_HOST")) != NULL) {
scr_log_db_host = strdup(value);
}
if (scr_my_rank_world == 0) {
scr_dbg(1, "SCR_LOG_DB_HOST=%s", scr_log_db_host);
}
if ((value = scr_param_get("SCR_LOG_DB_USER")) != NULL) {
scr_log_db_user = strdup(value);
}
if (scr_my_rank_world == 0) {
scr_dbg(1, "SCR_LOG_DB_USER=%s", scr_log_db_user);
}
if ((value = scr_param_get("SCR_LOG_DB_PASS")) != NULL) {
scr_log_db_pass = strdup(value);
}
if (scr_my_rank_world == 0) {
scr_dbg(1, "SCR_LOG_DB_PASS=%s", scr_log_db_pass);
}
if ((value = scr_param_get("SCR_LOG_DB_NAME")) != NULL) {
scr_log_db_name = strdup(value);
}
if (scr_my_rank_world == 0) {
scr_dbg(1, "SCR_LOG_DB_NAME=%s", scr_log_db_name);
}
/* read username from SCR_USER_NAME, if not set, try to read from environment */
if ((value = scr_param_get("SCR_USER_NAME")) != NULL) {
scr_username = strdup(value);
} else {
scr_username = scr_env_username();
}
if (scr_my_rank_world == 0) {
scr_dbg(1, "SCR_USER_NAME=%s", scr_username);
}
/* check that the username is defined, fatal error if not */
if (scr_username == NULL) {
scr_abort(-1, "Failed to record username @ %s:%d",
__FILE__, __LINE__
);
}
/* read jobid from SCR_JOB_ID, if not set, try to read from environment */
if ((value = scr_param_get("SCR_JOB_ID")) != NULL) {
scr_jobid = strdup(value);
} else {
scr_jobid = scr_env_jobid();
}
if (scr_my_rank_world == 0) {
scr_dbg(1, "SCR_JOB_ID=%s", scr_jobid);
}
/* check that the jobid is defined, fatal error if not */
if (scr_jobid == NULL) {
/* if we don't have a job id, we may be running outside of a
* job allocation likely for testing purposes, create a default */
scr_jobid = strdup("defjobid");
if (scr_jobid == NULL) {
scr_abort(-1, "Failed to allocate memory to record jobid @ %s:%d",
__FILE__, __LINE__
);
}
}
/* read job name from SCR_JOB_NAME */
if ((value = scr_param_get("SCR_JOB_NAME")) != NULL) {
scr_jobname = strdup(value);
if (scr_jobname == NULL) {
scr_abort(-1, "Failed to allocate memory to record jobname (%s) @ %s:%d",
value, __FILE__, __LINE__
);
}
}
if (scr_my_rank_world == 0) {
scr_dbg(1, "SCR_JOB_NAME=%s", scr_jobname);
}
/* read cluster name from SCR_CLUSTER_NAME, if not set, try to read from environment */
if ((value = scr_param_get("SCR_CLUSTER_NAME")) != NULL) {
scr_clustername = strdup(value);
} else {
scr_clustername = scr_env_cluster();
}
if (scr_my_rank_world == 0) {
scr_dbg(1, "SCR_CLUSTER_NAME=%s", scr_clustername);
}
/* override default base control directory */
if ((value = scr_param_get("SCR_CNTL_BASE")) != NULL) {
scr_cntl_base = spath_strdup_reduce_str(value);
} else {
scr_cntl_base = spath_strdup_reduce_str(SCR_CNTL_BASE);
}
if (scr_my_rank_world == 0) {
scr_dbg(1, "SCR_CNTL_BASE=%s", scr_cntl_base);
}
/* override default base directory for checkpoint cache */
if ((value = scr_param_get("SCR_CACHE_BASE")) != NULL) {
scr_cache_base = spath_strdup_reduce_str(value);
} else {
scr_cache_base = spath_strdup_reduce_str(SCR_CACHE_BASE);
}
if (scr_my_rank_world == 0) {
scr_dbg(1, "SCR_CACHE_BASE=%s", scr_cache_base);
}
/* set maximum number of checkpoints to keep in cache */
if ((value = scr_param_get("SCR_CACHE_SIZE")) != NULL) {
scr_cache_size = atoi(value);
}
if (scr_my_rank_world == 0) {
scr_dbg(1, "SCR_CACHE_SIZE=%d", scr_cache_size);
}
/* fill in a hash of group descriptors */
scr_groupdesc_hash = kvtree_new();
tmp = (kvtree*) scr_param_get_hash(SCR_CONFIG_KEY_GROUPDESC);
if (tmp != NULL) {
kvtree_set(scr_groupdesc_hash, SCR_CONFIG_KEY_GROUPDESC, tmp);
}
/* fill in a hash of store descriptors */
scr_storedesc_hash = kvtree_new();
tmp = (kvtree*) scr_param_get_hash(SCR_CONFIG_KEY_STOREDESC);
if (tmp != NULL) {
kvtree_set(scr_storedesc_hash, SCR_CONFIG_KEY_STOREDESC, tmp);
}
/* ensure we have a store descriptor for the default cache directory */
tmp = kvtree_get_kv(scr_storedesc_hash, SCR_CONFIG_KEY_STOREDESC, scr_cache_base);
if (tmp == NULL) {
/* create a store descriptor for the cache directory */
tmp = kvtree_set_kv(scr_storedesc_hash, SCR_CONFIG_KEY_STOREDESC, scr_cache_base);
kvtree_util_set_int(tmp, SCR_CONFIG_KEY_COUNT, scr_cache_size);
}
/* ensure we have a store descriptor for the default control directory */
tmp = kvtree_get_kv(scr_storedesc_hash, SCR_CONFIG_KEY_STOREDESC, scr_cntl_base);
if (tmp == NULL) {
/* create a store descriptor for the control directory */
tmp = kvtree_set_kv(scr_storedesc_hash, SCR_CONFIG_KEY_STOREDESC, scr_cntl_base);
kvtree_util_set_int(tmp, SCR_CONFIG_KEY_COUNT, 0);
}
/* select copy method */
if ((value = scr_param_get("SCR_COPY_TYPE")) != NULL) {
if (strcasecmp(value, "single") == 0) {
scr_copy_type = SCR_COPY_SINGLE;
} else if (strcasecmp(value, "partner") == 0) {
scr_copy_type = SCR_COPY_PARTNER;
} else if (strcasecmp(value, "xor") == 0) {
scr_copy_type = SCR_COPY_XOR;
} else if (strcasecmp(value, "rs") == 0) {
scr_copy_type = SCR_COPY_RS;
} else {
scr_copy_type = SCR_COPY_FILE;
}
}
if (scr_my_rank_world == 0) {
scr_dbg(1, "SCR_COPY_TYPE=%d", scr_copy_type);
}
/* specify the number of tasks in xor set */
if ((value = scr_param_get("SCR_SET_SIZE")) != NULL) {
scr_set_size = atoi(value);
}
if (scr_my_rank_world == 0) {
scr_dbg(1, "SCR_SET_SIZE=%d", scr_set_size);
}
/* specify the number of failures we should tolerate per set */
if ((value = scr_param_get("SCR_SET_FAILURES")) != NULL) {
scr_set_failures = atoi(value);
}
if (scr_my_rank_world == 0) {
scr_dbg(1, "SCR_SET_FAILURES=%d", scr_set_failures);
}
/* specify the group name to protect failures */
if ((value = scr_param_get("SCR_GROUP")) != NULL) {
scr_group = strdup(value);
} else {
scr_group = strdup(SCR_GROUP);
}
if (scr_my_rank_world == 0) {
scr_dbg(1, "SCR_GROUP=%s", scr_group);
}
/* fill in a hash of redundancy descriptors */
scr_reddesc_hash = kvtree_new();
if (scr_copy_type == SCR_COPY_SINGLE) {
/* fill in info for one SINGLE checkpoint */
tmp = kvtree_set_kv(scr_reddesc_hash, SCR_CONFIG_KEY_CKPTDESC, "0");
kvtree_util_set_str(tmp, SCR_CONFIG_KEY_STORE, scr_cache_base);
kvtree_util_set_str(tmp, SCR_CONFIG_KEY_TYPE, "SINGLE");
} else if (scr_copy_type == SCR_COPY_PARTNER) {
/* fill in info for one PARTNER checkpoint */
tmp = kvtree_set_kv(scr_reddesc_hash, SCR_CONFIG_KEY_CKPTDESC, "0");
kvtree_util_set_str(tmp, SCR_CONFIG_KEY_STORE, scr_cache_base);
kvtree_util_set_str(tmp, SCR_CONFIG_KEY_TYPE, "PARTNER");
kvtree_util_set_str(tmp, SCR_CONFIG_KEY_GROUP, scr_group);
} else if (scr_copy_type == SCR_COPY_XOR) {
/* fill in info for one XOR checkpoint */
tmp = kvtree_set_kv(scr_reddesc_hash, SCR_CONFIG_KEY_CKPTDESC, "0");
kvtree_util_set_str(tmp, SCR_CONFIG_KEY_STORE, scr_cache_base);
kvtree_util_set_str(tmp, SCR_CONFIG_KEY_TYPE, "XOR");
kvtree_util_set_str(tmp, SCR_CONFIG_KEY_GROUP, scr_group);
kvtree_util_set_int(tmp, SCR_CONFIG_KEY_SET_SIZE, scr_set_size);
} else if (scr_copy_type == SCR_COPY_RS) {
/* fill in info for one RS checkpoint */
tmp = kvtree_set_kv(scr_reddesc_hash, SCR_CONFIG_KEY_CKPTDESC, "0");
kvtree_util_set_str(tmp, SCR_CONFIG_KEY_STORE, scr_cache_base);
kvtree_util_set_str(tmp, SCR_CONFIG_KEY_TYPE, "RS");
kvtree_util_set_str(tmp, SCR_CONFIG_KEY_GROUP, scr_group);
kvtree_util_set_int(tmp, SCR_CONFIG_KEY_SET_SIZE, scr_set_size);
kvtree_util_set_int(tmp, SCR_CONFIG_KEY_SET_FAILURES, scr_set_failures);
} else {
/* read info from our configuration files */
tmp = (kvtree*) scr_param_get_hash(SCR_CONFIG_KEY_CKPTDESC);
if (tmp != NULL) {
kvtree_set(scr_reddesc_hash, SCR_CONFIG_KEY_CKPTDESC, tmp);
} else {
scr_abort(-1, "Failed to define checkpoints @ %s:%d",
__FILE__, __LINE__
);
}
}
/* set whether to bypass cache and directly read from and write to prefix dir */
if ((value = scr_param_get("SCR_CACHE_BYPASS")) != NULL) {
/* if BYPASS is set explicitly, we use that */
scr_cache_bypass = atoi(value);
}
if (scr_my_rank_world == 0) {
scr_dbg(1, "SCR_CACHE_BYPASS=%d", scr_cache_bypass);
}
/* if job has fewer than SCR_HALT_SECONDS remaining after completing a checkpoint,
* halt it */
if ((value = scr_param_get("SCR_HALT_SECONDS")) != NULL) {
scr_halt_seconds = atoi(value);
}
if (scr_my_rank_world == 0) {
scr_dbg(1, "SCR_HALT_SECONDS=%d", scr_halt_seconds);
}
/* determine whether we should call exit() upon detecting a halt condition */
if ((value = scr_param_get("SCR_HALT_EXIT")) != NULL) {
scr_halt_exit = atoi(value);
}
if (scr_my_rank_world == 0) {
scr_dbg(1, "SCR_HALT_EXIT=%d", scr_halt_exit);
}
/* set MPI buffer size (file chunk size) */
if ((value = scr_param_get("SCR_MPI_BUF_SIZE")) != NULL) {
if (scr_abtoull(value, &ull) == SCR_SUCCESS) {
scr_mpi_buf_size = (int) ull;
if (scr_mpi_buf_size != ull) {
scr_abort(-1, "Value %s given for %s exceeds int range @ %s:%d",
value, SCR_MPI_BUF_SIZE, __FILE__, __LINE__
);
}