-
Notifications
You must be signed in to change notification settings - Fork 0
/
checkpoint.c
256 lines (220 loc) · 6.67 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
#include <stdint.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <openssl/sha.h>
#include <signal.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include "hex.h"
#include "checkpoint.h"
#include "simulazione.h"
#include "infofile/infofile.h"
static char *filename;
//
// Add info (time and filename and crc sum) in .info file
// make filename a binary file with random number state
// xyz polymer state and laplacian
// close with exit 99
//
// When jobs start, check if info file is there
// if it's there and contains valid checkpoint information
// start with it, if it's there but it is corrupted fail
// corrupted includes 1) inconsistent with command line
// 2) without checkpoint information
// 3) with corrupted checkpoint information
// if it is not there, start a new job
//
// First time you should init_checkpoint
int prepare_checkpoint(unsigned long long int toprint) {
// block SIGUSR1 signal
#if NUM_THREADS > 1
// Something smart should be there
#else
sigset_t signal_set;
sigemptyset(&signal_set); sigaddset(&signal_set, SIGUSR1);
sigprocmask(SIG_BLOCK, &signal_set, NULL);
#endif
void *dest = checkpoint;
// Buffer
memcpy(dest, buffer, buffer_size); dest += buffer_size;
// Global state
memcpy(dest, &mc_time, sizeof(mc_time)); dest += sizeof(mc_time);
// Random state
memcpy(dest, &dsfmt, sizeof(dsfmt_t)); dest += sizeof(dsfmt_t);
// Local variables
*(unsigned long long int *)dest = toprint; dest += sizeof(unsigned long long int);
// File offsets
#if defined(GETXYZ)
*(off_t *)dest = ftello(simufiles.xyzfile); dest += sizeof(off_t);
#endif
#if defined(GETXLINK)
*(off_t *)dest = ftello(simufiles.xlkfile); dest += sizeof(off_t);
#endif
#if defined(GETPERF)
*(off_t *)dest = ftello(simufiles.accfile); dest += sizeof(off_t);
#endif
#if defined(GETENERGY)
*(off_t *)dest = ftello(simufiles.ctcfile); dest += sizeof(off_t);
#endif
//
#if NUM_THREADS > 1
// Here too
#else
sigprocmask(SIG_UNBLOCK, &signal_set, NULL);
#endif
return 0;
}
// Before you should init_checkpoint
int load_checkpoint(const char* hash,
unsigned long long int *toprint) {
// block SIGUSR1 signal
#if NUM_THREADS > 1
// Something smart should be there
#else
sigset_t signal_set;
sigemptyset(&signal_set); sigaddset(&signal_set, SIGUSR1);
sigprocmask(SIG_BLOCK, &signal_set, NULL);
#endif
int fd = open(filename, O_RDONLY);
if (fd == -1)
return -4;
struct stat buf;
if (fstat(fd, &buf)) {
fprintf(stderr, "Could not open checkpoint file\n");
close(fd);
return -1;
};
if (checkpoint_size != buf.st_size) {
fprintf(stderr, "Checkpoint size and file doesn't match\n");
close(fd);
return -2;
}
read(fd, checkpoint, checkpoint_size);
close(fd);
// Hash it
unsigned char hash_cmp[SHA_DIGEST_LENGTH]; // == 20
char checksum[sizeof(char) * (SHA_DIGEST_LENGTH * 2 + 1)]; // Hexadecimal
SHA_CTX context;
SHA1_Init(&context);
SHA1_Update(&context, checkpoint, checkpoint_size);
SHA1_Final(hash_cmp, &context);
bin2hex(checksum, hash_cmp, sizeof(hash_cmp));
if (strcmp(checksum, hash) != 0) {
fprintf(stderr, "Checkpoint hash doesn't match: %s %s\n", checksum, hash);
return -3;
}
// Parse it
void *source = checkpoint;
// Buffer
memcpy(buffer, source, buffer_size); source += buffer_size;
// Global state
memcpy(&mc_time, source, sizeof(mc_time)); source += sizeof(mc_time);
// Random state
memcpy(&dsfmt, source, sizeof(dsfmt_t)); source += sizeof(dsfmt_t);
// Local variables
*toprint = *(unsigned long long int *)source; source += sizeof(unsigned long long int);
// File offsets
#if defined(GETXYZ)
fseeko(simufiles.xyzfile, 0, SEEK_END);
fseeko(simufiles.xyzfile, *(off_t *)source, SEEK_SET); source += sizeof(off_t);
#endif
#if defined(GETXLINK)
fseeko(simufiles.xlkfile, 0, SEEK_END);
fseeko(simufiles.xlkfile, *(off_t *)source, SEEK_SET); source += sizeof(off_t);
#endif
#if defined(GETPERF)
fseeko(simufiles.accfile, 0, SEEK_END);
fseeko(simufiles.accfile, *(off_t *)source, SEEK_SET); source += sizeof(off_t);
#endif
#if defined(GETENERGY)
fseeko(simufiles.ctxfile, 0, SEEK_END);
fseeko(simufiles.ctxfile, *(off_t *)source, SEEK_SET); source += sizeof(off_t);
#endif
//
struct data_infofile d; d.time = time(NULL); d.type = is_time_infofile; append_infofile(infos, "RESUME", d);
#if NUM_THREADS > 1
// Here too
#else
sigprocmask(SIG_UNBLOCK, &signal_set, NULL);
#endif
return 0;
}
static
void print_checkpointinfo(char *word, char *filename, char *checksum) {
time_t tstamp = time(NULL);
struct data_infofile info;
info.type = is_list_infofile;
info.list = malloc(sizeof(struct list_infofile));
info.list -> next = malloc(sizeof(struct list_infofile));
info.list -> next -> next = malloc(sizeof(struct list_infofile));
info.list -> data.s = filename;
info.list -> next -> data.s = checksum;
info.list -> next -> next -> data.time = tstamp;
info.list -> data.type = info.list -> next -> data.type = is_s_infofile;
info.list -> next -> next -> data.type = is_time_infofile;
append_infofile(infos, word, info);
}
int make_checkpoint() {
// Write to file
int fd = open(filename, O_CREAT | O_WRONLY | O_TRUNC,
S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH);
write(fd, checkpoint, checkpoint_size);
close(fd);
// Hash it
unsigned char hash[SHA_DIGEST_LENGTH]; // == 20
char checksum[sizeof(char) * (SHA_DIGEST_LENGTH * 2 + 1)]; // Hexadecimal
SHA_CTX context;
SHA1_Init(&context);
SHA1_Update(&context, checkpoint, checkpoint_size);
SHA1_Final(hash, &context);
bin2hex(checksum, hash, sizeof(hash));
print_checkpointinfo("CHECKPOINT", filename, checksum);
return 0;
}
int init_checkpoint(const char *fname,
unsigned long long int toprint) {
filename = malloc(sizeof(char) * (strlen(fname) + 1));
strcpy(filename, fname);
checkpoint_size =
// Buffer
buffer_size +
// Global state
sizeof(struct mc_time_t) +
// Random state
sizeof(dsfmt_t) +
// Local variables
sizeof(toprint) +
// File offsets
#if defined(GETXYZ)
sizeof(off_t) +
#endif
#if defined(GETXLINK)
sizeof(off_t) +
#endif
#if defined(GETPERF)
sizeof(off_t) +
#endif
#if defined(GETENERGY)
sizeof(off_t) +
#endif
//
0;
if(posix_memalign((void **)&checkpoint, 32, checkpoint_size))
return -1;
prepare_checkpoint(toprint);
// Activate checkpoint signal
#if NUM_THREADS > 1
// Something smart should be there
#else
{
sigset_t signal_set;
sigemptyset(&signal_set); sigaddset(&signal_set, SIGUSR1);
sigprocmask(SIG_UNBLOCK, &signal_set, NULL);
}
#endif
return 0;
}