checkpoint.c
32.3 KB
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
/*
* Copyright (C) 1996-1998 by the Board of Trustees
* of Leland Stanford Junior University.
*
* This file is part of the SimOS distribution.
* See LICENSE file for terms of the license.
*
*/
/*****************************************************************
* checkpoint.c
*
* Generic checkpoint handling code.
*****************************************************************/
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include "sim.h"
#include "simutil.h"
#include "sim_error.h"
#include "cpu_interface.h"
#include "remote_access.h"
#include "checkpoint.h"
#include "cpu_interface.h"
#include "machine_params.h"
#include "params.h"
#define MAX_GROUPS 50
#define MAX_LENGTH 128
#define IS_REMOTE_CPT(NAME) (strchr(NAME,':') != NULL)
typedef enum cpttype {CPT_INTEGER = 0, CPT_STRING, CPT_BLOCK} CptType;
static char lineFormat[3][20] = {"%s = %s", "%s [%d] = %s", "%s [%d,%d] = %s"};
#ifdef __alpha
#define INT_FORMAT "int:%ld"
#define HEX_FORMAT "int:0x%lx"
#define BLOCK_FORMAT "binary:position=%ld,size=%ld;"
typedef long cptlong;
#else
#define INT_FORMAT "int:%lld"
#define HEX_FORMAT "int:0x%llx"
#define BLOCK_FORMAT "binary:position=%lld,size=%lld;"
typedef int64 cptlong;
#endif
#define STRING_FORMAT "string:%s"
typedef struct compressiondata {
CptCompression type;
char compress[MAX_PATH_LENGTH];
char decompress[MAX_PATH_LENGTH];
char extender[10];
} CompressionData;
typedef struct cptentry {
char *name;
int i1, i2;
CptType type;
bool optional;
int64 intval;
int64 ptrval;
struct cptentry *prev;
struct cptentry *next;
} CptEntry;
typedef struct cptgroup {
char *tag;
FILE *stream;
int binfd;
int binpos;
int failure;
CptCompression compressCpt[2];
CptCompression compressBin[2];
CptEntry *tail;
CptEntry *hint;
CptCallback *callback[NUM_CPU_TYPES];
} CptGroup;
static CptGroup cptGroup[MAX_GROUPS];
static int cptNumGroups;
static bool cptRemote;
static CompressionData compression[] = {
{ NO_COMPRESSION, "cat", "cat", "" },
{ GZIP, "gzip", "gunzip", ".gz" },
{ COMPRESS, "compress", "zcat", ".Z" }
};
typedef enum {ITER_CPT, ITER_LOG, NUM_ITERS} CptIter;
static CptGroup *NewGroup(char *tag);
static CptGroup *FindGroup(char *tag);
static CptGroup *IterGroup(CptIter num);
static CptEntry *NewEntry(char *name, int i1, int i2, CptType type);
static void AddEntry(CptGroup *group, CptEntry *entry);
static void FreeEntry(CptEntry *entry);
static CptEntry *FindEntryInList(CptGroup *group, char *name, int i1, int i2);
static CptEntry *FindEntryInFile(CptGroup *group, char *name, int i1, int i2);
static bool ParseInteger(char *str, cptlong *val);
static bool ParseBlock(char *str, cptlong *ptr, cptlong *val);
static bool ParseString(char *str, char *val);
static int64 ReadInteger(CptGroup *group, char *name,
int i1, int i2);
static char *ReadString(CptGroup *group, char *name,
int i1, int i2);
static int64 ReadBlock(CptGroup *group, char *name,
int i1, int i2, int *size);
static void WriteHexInteger(CptGroup *group, char *name,
int i1, int i2, int64 val);
static void WriteInteger(CptGroup *group, char *name,
int i1, int i2, int64 val);
static void WriteString(CptGroup *group, char *name,
int i1, int i2, char *ptr);
static void WriteBlock(CptGroup *group, char *name,
int i1, int i2, int64 pos, int64 val);
static CptCallback LogCheckpointCB;
static FILE *OpenFile(CptMode mode, char *filename, CptCompression compress);
static FILE *OpenCompressedFile(CptMode mode, CptCompression c, FILE *fp);
static void OpenCptFile(CptMode mode, CptGroup *group);
static void OpenBinFile(CptMode mode, CptGroup *group);
static void CloseCptFile(CptGroup *group);
static void CloseBinFile(CptGroup *group);
static void SaveLine(CptGroup *group, char *name,
int i1, int i2, char *valstring);
static int RestoreLine(CptGroup *group, char *name,
int *i1, int *i2, char *valstring);
static void SaveBlock(CptGroup *group, caddr_t addr, int size);
static int RestoreBlock(CptGroup *group, caddr_t addr, int pos, int size);
static int outputFD = -1;
static int compressPID = -1;
char *CheckpointID;
int CheckpointCompress;
char *cptSaveDir;
char cptName[MAX_PATH_LENGTH];
CptVersion cptVersion;
void
Simcpt_Init(void)
{
cptSaveDir = SaveString(".");
cptNumGroups = 0;
cptVersion.saveVer = CPT_VERSION;
cptVersion.saveSub = CPT_SUB_VERSION;
Simcpt_Register("log", LogCheckpointCB, ALL_CPUS);
}
extern char *EthersimHostname; /* crissy */
extern char *EtherAddress; /* 8:1:2:3:4:60 */
void
Simcpt_Register(char *tag, CptCallback *callback, CPUType cputype)
{
CptGroup *group = NewGroup(tag);
if (group != NULL) {
if (cputype == ALL_CPUS) {
for (cputype = BASE; cputype < NUM_CPU_TYPES; cputype++) {
group->callback[cputype] = callback;
}
} else if (cputype != NO_CPU) {
group->callback[cputype] = callback;
}
}
}
void
Simcpt_UseCompression(char *name, CptCompression compress,
CptCompressMode mode)
{
CptGroup *group = FindGroup(name);
if (!CheckpointCompress) return;
if (group != NULL) {
if (mode != CPT_FILE_ONLY)
group->compressBin[CPT_SAVE] = compress;
if (mode != BIN_FILE_ONLY)
group->compressCpt[CPT_SAVE] = compress;
}
}
int
CheckpointFile(CptMode mode, CptGroup *group)
{
CptDescriptor cptd;
int retval = 0;
CPUType cputype = (mode == CPT_SAVE)?simosCPUType:BASE;
/* CPUType cputype = CPUVec.cpuType;*/
cptd.mode = mode;
cptd.group = group;
SIM_DEBUG(('g', "CPT: %s checkpoint file %s\n",
mode == CPT_SAVE ? "Saving" : "Restoring", group->tag));
cptd.group->failure = FALSE;
/* Ugly hack -- when restoring the execstate, always use the base
mode callback */
if (!strcmp(group->tag, "execstate") && (mode == CPT_RESTORE))
cputype = BASE;
/* Even uglier hack: Tcl manages the file itself, so don't go
* through the usual opening/closing stuff.
*/
if (!strcmp(group->tag, "tcl")) {
ASSERT(group->callback[cputype]);
retval = (group->callback[cputype])(&cptd);
goto done;
}
if (group->callback[cputype] == NULL) {
if (!strcmp(group->tag,"HOHA")) {
/*
* I guess that HOHA already stands for horrible hack anyway
*/
return 0;
}
CPUWarning("CPT: Cpt failed -- no callback registered for %s in %s\n",
group->tag, simName[cputype]);
return -1;
}
Simcpt_Begin(&cptd);
retval = (group->callback[cputype])(&cptd);
Simcpt_End(&cptd);
done:
group->failure = group->failure || (retval != 0);
if (group->failure) CPUError("CPT: Checkpoint failed, tag = %s\n",
group->tag);
SIM_DEBUG(('g', "CPT: Done %s checkpoint file %s\n",
mode == CPT_SAVE ? "saving" : "restoring", group->tag));
return (group->failure) ? -1 : 0;
}
void
Simcpt_Checkpoint(CptMode mode, char *name)
{
CptGroup *group;
if (mode == CPT_SAVE) {
int num;
char pathname[MAX_PATH_LENGTH];
/* Check if the checkpoint save directory exists and is writeable */
if (access(cptSaveDir, F_OK | W_OK | X_OK) != 0) {
CPUWarning("CPT: Bad checkpoint save directory %s\n",cptSaveDir);
perror("CPT: Cannot write checkpoint");
CPUWarning("\n");
return;
}
/* Search for an unused checkpoint number */
for (num = 1; ; num++) {
sprintf(cptName, "CPT.%s.%03d", CheckpointID, num);
sprintf(pathname, "%s/%s.log", cptSaveDir, cptName);
if (access(pathname, F_OK) != 0)
break;
}
cptRemote = FALSE;
} else if (IS_REMOTE_CPT(name)) {
Simrmt_cptinit(name, cptName);
cptRemote = TRUE;
} else {
strcpy(cptName, name);
cptRemote = FALSE;
}
CPUWarning("CPT: %s checkpoint %s\n",
mode == CPT_SAVE ? "Saving" : "Restoring",
cptName);
if (mode == CPT_SAVE) {
while ((group = IterGroup(ITER_CPT)) != NULL) {
CheckpointFile(mode, group);
}
CPUWarning("CPT: End checkpoint save\n");
} else {
CheckpointFile(mode, FindGroup("log"));
}
}
int
Simcpt_Restore(char *tag)
{
CptGroup *group = FindGroup(tag);
if (group != NULL) {
return CheckpointFile(CPT_RESTORE, group);
} else {
CPUWarning("CPT: Checkpoint file %s has not been restored\n", tag);
return -1;
}
}
void
Simcpt_Begin(CptDescriptor *cptd)
{
SIM_DEBUG(('g', "CPT: Simcpt_Begin %s\n", cptd->group->tag));
OpenCptFile(cptd->mode, cptd->group);
}
void
Simcpt_End(CptDescriptor *cptd)
{
CloseCptFile(cptd->group);
if (cptd->group->binfd > 0)
CloseBinFile(cptd->group);
SIM_DEBUG(('g', "CPT: Simcpt_End %s\n", cptd->group->tag));
if (cptd->mode == CPT_RESTORE) {
/* Verify that all data were used here and free the entries */
if (cptd->group->tail != NULL) {
CptEntry *entry = cptd->group->tail;
CptEntry *oldentry;
do {
if (!entry->optional) {
CPUWarning("CPT: Entry %s [%d,%d] in file %s not used\n",
entry->name, entry->i1, entry->i2, cptd->group->tag);
}
oldentry = entry;
entry = oldentry->next;
FreeEntry(oldentry);
} while (entry != cptd->group->tail);
cptd->group->tail = NULL;
}
SIM_DEBUG(('g', "CPT: Done restoring from checkpoint file %s\n",
cptd->group->tag));
}
}
static CptGroup *
NewGroup(char *tag)
{
CptGroup *group = FindGroup(tag);
if (group != NULL) {
return group;
} else if (cptNumGroups == MAX_GROUPS) {
CPUWarning("CPT: Too many cpt groups -- creation of group %s failed\n",
tag);
return NULL;
} else {
group = cptGroup + cptNumGroups++;
group->tag = SaveString(tag);
group->binfd = -1;
return group;
}
}
static CptGroup *
FindGroup(char *tag)
{
int g;
CptGroup *group = cptGroup;
for (g = 0; g < cptNumGroups; g++, group++) {
if (!strcmp(group->tag, tag))
return group;
}
return NULL;
}
static CptGroup *
IterGroup(CptIter num)
{
static int currentGroup[NUM_ITERS];
if (currentGroup[num] == cptNumGroups) {
currentGroup[num] = 0;
return NULL;
} else {
return cptGroup + currentGroup[num]++;
}
}
static CptEntry *
NewEntry(char *name, int i1, int i2, CptType type)
{
CptEntry *entry = (CptEntry *) MALLOC(sizeof(CptEntry), name);
entry->name = SaveString(name);
entry->i1 = i1;
entry->i2 =i2;
entry->type = type;
entry->optional = FALSE;
return entry;
}
static void
AddEntry(CptGroup *group, CptEntry *entry)
{
/* Keep list in order of insertion */
if (group->tail == NULL) {
group->tail = group->hint = entry->prev = entry->next = entry;
} else {
entry->prev = group->tail;
entry->next = group->tail->next;
group->tail->next->prev = entry;
group->tail->next = entry;
group->tail = entry;
}
}
static void
FreeEntry(CptEntry *entry)
{
free(entry->name);
free(entry);
}
/* Finds entry in list, and removes it if found before returning it */
/* If the entry found is optional, keep looking to see if a value loaded
from the checkpoint file is also present in the list */
static CptEntry *
FindEntryInList(CptGroup *group, char *name, int i1, int i2)
{
if (group->tail == NULL) {
return NULL;
} else {
CptEntry *entry = group->hint;
CptEntry *optentry = NULL;
/* Circularly linked list; start at the hint pointer and make one pass through */
do {
if (!strcmp(entry->name,name) && (entry->i1 == i1) && (entry->i2 == i2)) {
if (entry->prev == entry) {
group->tail = NULL;
} else {
group->hint = entry->next;
entry->prev->next = entry->next;
entry->next->prev = entry->prev;
if (group->tail == entry)
group->tail = entry->prev;
}
if (entry->optional)
/* If this entry is optional, keep looking for an entry from the file */
optentry = entry;
else
/* Otherwise, go ahead and return this entry */
return entry;
}
entry = entry->next;
} while (entry != group->hint);
/* If we get here, no non-optional entry was found. */
return optentry;
}
}
static int
LogCheckpointCB(CptDescriptor *cptd)
{
if (cptd->mode == CPT_SAVE) {
CptGroup *group;
int g = 0;
while ((group = IterGroup(ITER_LOG)) != NULL) {
SaveLine(cptd->group, "CheckpointFile", g, NO_INDEX, group->tag);
g++;
}
}
return 0;
}
static void
SaveLine(CptGroup *group, char *name, int i1, int i2, char *valstring)
{
if (group->stream == NULL) {
CPUPrint("CPT: Checkpoint file %s not opened for writing\n",group->tag);
return;
}
if (i1 == NO_INDEX) {
fprintf(group->stream, lineFormat[0], name, valstring);
} else if (i2 == NO_INDEX) {
fprintf(group->stream, lineFormat[1], name, i1, valstring);
} else {
fprintf(group->stream, lineFormat[2], name, i1, i2, valstring);
}
fprintf(group->stream, "\n");
}
static int
RestoreLine(CptGroup *group, char *name, int *i1, int *i2, char *valstring)
{
char line[MAX_LENGTH];
int status = 0;
if (group->stream == NULL) {
CPUPrint("CPT: Checkpoint file %s not opened for reading\n",group->tag);
return -2;
}
do status = GetLineFromFile(group->stream, line, MAX_LENGTH);
while ((status > 0) && (line[0] == '#')); /* Ignore comment lines */
ASSERT( strlen(line)< MAX_LENGTH);
if (status > 0) {
if (sscanf(line, lineFormat[2], name, i1, i2, valstring) == 4) {
/* Nothing to do -- all arguments in place */
} else if (sscanf(line, lineFormat[1], name, i1, valstring) == 3) {
*i2 = NO_INDEX;
} else if (sscanf(line, lineFormat[0], name, valstring) == 2) {
*i1 = NO_INDEX;
*i2 = NO_INDEX;
} else {
status = -2;
}
}
return status;
}
static void
SaveBlock(CptGroup *group, caddr_t addr, int size)
{
if (group->binfd < 0) {
OpenBinFile(CPT_SAVE, group);
}
write(group->binfd, addr, size);
group->binpos += size;
}
static int
RestoreBlock(CptGroup *group, caddr_t addr, int pos, int size)
{
int cnt;
if (group->binfd < 0) {
OpenBinFile(CPT_RESTORE, group);
}
if(outputFD >= 0) {
pid_t retPid;
int waitState;
CPUPrint("CPT: Waiting for death of child\n");
while (1) {
retPid = wait(&waitState);
if (retPid != compressPID) {
continue;
}
if (WIFSTOPPED(waitState)) {
CPUWarning("CPT: decompress process stopped by signal %d\nWaiting...\n",
WSTOPSIG(waitState));
continue;
}
if (WIFEXITED(waitState) && (WEXITSTATUS(waitState)==0)) {
break;
}
CPUWarning("CPT: Decompress of checkpoint file failed\n");
if (WIFEXITED(waitState)) {
CPUWarning("CPT: Exit status of %d\n", WEXITSTATUS(waitState));
} else {
CPUWarning("CPT: Terminate by signal %d\n", WTERMSIG(waitState));
}
group->failure = TRUE;
return -1;
}
CloseBinFile(group);
return 0;
}
if (group->binpos != pos) {
/* Test for group->failure so this message is only given once */
if (!group->failure) {
CPUWarning("CPT: The binary file for %s was restored out of order\n",
group->tag);
group->failure = TRUE;
return -1;
}
}
while (size > 0) {
cnt = read(group->binfd, addr, size);
if (cnt <= 0) {
/* Test for group->failure so this message is only given once */
if (!group->failure) {
perror("CPT: RestoreBlock");
CPUWarning("CPT: Error in reading the binary file for %s addr=%llx size=%llx \n",
group->tag, (uint64)addr, (uint64)size);
}
group->failure = TRUE;
return -1;
}
group->binpos += cnt;
addr += cnt;
size -= cnt;
}
return 0;
}
/* Read checkpoint file, searching for the given entry and returning its value.
Any intervening values are placed on the entry list for the group */
static CptEntry *
FindEntryInFile(CptGroup *group, char *name, int i1, int i2)
{
while (TRUE) {
char fname[MAX_LENGTH];
char fvalstr[MAX_LENGTH];
int fi1, fi2;
int status = RestoreLine(group, fname, &fi1, &fi2, fvalstr);
if (status <= 0)
return NULL;
else {
CptEntry *entry;
cptlong ival;
cptlong pval;
char buf[MAX_LENGTH];
if (ParseInteger(fvalstr, &ival)) {
entry = NewEntry(fname, fi1, fi2, CPT_INTEGER);
entry->intval = ival;
} else if (ParseBlock(fvalstr, &pval, &ival)) {
entry = NewEntry(fname, fi1, fi2, CPT_BLOCK);
entry->ptrval = pval;
entry->intval = ival;
} else if (ParseString(fvalstr, buf)) {
ASSERT( strlen(buf) < MAX_LENGTH);
entry = NewEntry(fname, fi1, fi2, CPT_STRING);
entry->ptrval = (int64) PTR_TO_UINT64(SaveString(buf));
} else {
CPUWarning("CPT: Could not parse value %s\n", fvalstr);
continue;
}
if (!strcmp(name, fname) && (i1 == fi1) && (i2 == fi2)) {
/* Check to see if the entry is also resident in the in-memory list */
CptEntry *listEntry = FindEntryInList(group, name, i1, i2);
if (listEntry != NULL) {
if (listEntry->optional == FALSE)
CPUWarning("CPT: Entry %s [%d,%d] appears multiple times in file\n",name,i1,i2);
}
return entry;
} else
AddEntry(group, entry);
}
}
return NULL;
}
static FILE *
OpenCompressedFile(CptMode mode, CptCompression c, FILE *fp)
{
int pfd[2];
int ffd = fileno(fp);
if (pipe(pfd) < 0) {
perror("pipe");
CPUWarning("CPT: Cannot create pipe to compress file.\n");
}
compressPID = fork();
if (compressPID == (pid_t) -1) {
perror("fork");
CPUWarning("CPT: Cannot fork compress process.\n");
} else if (compressPID == 0) {
/* Child process */
if (mode == CPT_SAVE) {
SIM_DEBUG(('g', "CPT: Using %s to compress file\n",
compression[c].compress));
close(pfd[1]);
dup2(pfd[0], 0);
dup2(ffd, 1);
execlp(compression[c].compress, compression[c].compress, (char *) 0);
} else {
SIM_DEBUG(('g', "CPT: Using %s to decompress file\n"
,compression[c].decompress));
close(pfd[0]);
if (outputFD >= 0) {
dup2(outputFD, 1);
} else {
dup2(pfd[1], 1);
}
dup2(ffd, 0);
execlp(compression[c].decompress, compression[c].decompress,
(char *) 0);
}
/* Should not return */
CPUError("execlp of compress/decompress returned\n");
} else {
/* Parent process */
if (mode == CPT_SAVE) {
close(pfd[0]);
return fdopen(pfd[1], "w");
} else {
close(pfd[1]);
return fdopen(pfd[0], "r");
}
}
return NULL;
}
static CptCompression
GetCompression(char *filename, char *pathname)
{
CptCompression c;
for (c = NO_COMPRESSION; c < NUM_COMPRESS_TYPES; c++) {
bool found;
if (cptRemote) {
sprintf(pathname, "%s%s", filename, compression[c].extender);
found = Simrmt_access(pathname);
} else {
sprintf(pathname, "%s%s", filename, compression[c].extender);
found = (access(pathname, R_OK) == 0);
}
if (found)
break;
}
return c;
}
static FILE *
OpenFile(CptMode mode, char *filename, CptCompression compress)
{
FILE *fp;
char pathname[MAX_PATH_LENGTH];
if (mode == CPT_RESTORE) {
compress = GetCompression(filename, pathname);
if (compress == NUM_COMPRESS_TYPES) {
CPUError("Could not find checkpoint file %s -- "
"all known extensions tried\n", filename);
return NULL;
}
if (cptRemote) {
CPUPrint("CPT: Reading %s from remote access server\n", pathname);
fp = Simrmt_fopen(pathname);
} else {
SIM_DEBUG(('g', "CPT: Reading from file %s....\n", pathname));
fp = fopen(pathname, "r");
}
} else {
if (cptRemote) {
CPUWarning("CPT: Not allowed to write checkpoint to remote access server\n");
return NULL;
} else {
sprintf(pathname, "%s/%s%s", cptSaveDir, filename,
compression[compress].extender);
fp = fopen(pathname, "w");
SIM_DEBUG(('g', "CPT: Writing to file %s....\n", pathname));
}
}
if (fp == NULL) {
CPUWarning("CPT: Couldn't open checkpoint file %s.\n", filename);
return fp;
} else if ((compress == NO_COMPRESSION) && (outputFD < 0)) {
return fp;
} else {
FILE *cfp = OpenCompressedFile(mode, compress, fp);
fclose(fp);
return cfp;
}
}
static void
OpenBinFile(CptMode mode, CptGroup *group)
{
char filename[MAX_PATH_LENGTH];
/* Do not open if file is already open -- this is necessary to make
Simcpt_CheckpointRmtDisk work on the rmtaccess servers */
if (group->binfd >= 0) return;
sprintf(filename, "%s.%s.bin", cptName, group->tag);
group->binfd = fileno(OpenFile(mode, filename, group->compressBin[mode]));
group->binpos = 0;
}
static void
OpenCptFile(CptMode mode, CptGroup *group)
{
char filename[MAX_PATH_LENGTH];
int length;
/* Do not open if file is already open -- this is necessary to make
Simcpt_CheckpointRmtDisk work on the rmtaccess servers */
if (group->stream != NULL) return;
/* Strip the ending period off of the checkpoint name if it's there. */
length = strlen(cptName);
if (cptName[length-1] == '.') {
cptName[length-1] = '\000';
}
sprintf(filename, "%s.%s", cptName, group->tag);
group->stream = OpenFile(mode, filename, group->compressCpt[mode]);
}
static void
CloseBinFile(CptGroup *group)
{
close(group->binfd);
group->binfd = -1;
group->binpos = 0;
}
static void
CloseCptFile(CptGroup *group)
{
fclose(group->stream);
group->stream = 0;
}
/* Functions to save and restore values */
bool
ParseInteger(char *str, cptlong *val)
{
/* Must do hex before int, or else sscanf will see 0x... and scan the 0 */
if (sscanf(str, HEX_FORMAT, val) == 1) return TRUE;
if (sscanf(str, INT_FORMAT, val) == 1) return TRUE;
return FALSE;
}
bool
ParseBlock(char *str, cptlong *ptr, cptlong *val)
{
return (sscanf(str, BLOCK_FORMAT, ptr, val) == 2);
}
bool
ParseString(char *str, char *val)
{
return (sscanf(str, STRING_FORMAT, val) == 1);
}
CptEntry *
FindEntry(CptGroup *group, char *name, int i1, int i2)
{
/* We check the file first, then the group list, because there could be
and optional parameter in the group list which is overridden in the file.
This could be inefficient once we actually have optional parameters,
because the whole file will be read as soon as the first optional value
is accessed. */
CptEntry *entry = FindEntryInFile(group, name, i1, i2);
if (entry != NULL)
return entry;
entry = FindEntryInList(group, name, i1, i2);
if (entry != NULL)
return entry;
CPUWarning("CPT: Could not find entry %s [%d,%d] in %s checkpoint file\n",
name, i1, i2, group->tag);
return NULL;
}
static int64
ReadInteger(CptGroup *group, char *name, int i1, int i2)
{
CptEntry *entry = FindEntry(group, name, i1, i2);
int64 val;
if (entry == NULL) {
group->failure = TRUE;
return 0;
}
if (entry->type != CPT_INTEGER) {
CPUWarning("CPT: Could not parse value for entry %s [%d,%d] as an integer\n",
name, i1, i2);
group->failure = TRUE;
return 0;
}
val = entry->intval;
FreeEntry(entry);
return val;
}
static int64
ReadBlock(CptGroup *group, char *name, int i1, int i2, int *size)
{
CptEntry *entry = FindEntry(group, name, i1, i2);
int64 ptr;
if (entry == NULL) {
group->failure = TRUE;
return 0;
}
if (entry->type != CPT_BLOCK) {
CPUWarning("CPT: Could not parse value for entry [%d,%d] as a binary block\n",
i1, i2);
group->failure = TRUE;
return 0;
}
ptr = entry->ptrval;
*size = entry->intval;
FreeEntry(entry);
return ptr;
}
char *
ReadString(CptGroup *group, char *name, int i1, int i2)
{
CptEntry *entry = FindEntry(group, name, i1, i2);
char *ptr;
if (entry == NULL) {
group->failure = TRUE;
return 0;
}
if (entry->type != CPT_STRING) {
CPUWarning("CPT: Could not parse value for entry %s [%d,%d] as a string\n",
name, i1, i2);
group->failure = TRUE;
return 0;
}
ptr = UINT64_TO_PTR(entry->ptrval);
FreeEntry(entry);
return ptr;
}
void
WriteInteger(CptGroup *group, char *name, int i1, int i2, int64 val)
{
char valstr[MAX_LENGTH];
sprintf(valstr, INT_FORMAT, (cptlong)val);
SaveLine(group, name, i1, i2, valstr);
}
void
WriteHexInteger(CptGroup *group, char *name, int i1, int i2, int64 val)
{
char valstr[MAX_LENGTH];
sprintf(valstr, HEX_FORMAT, (cptlong)val);
SaveLine(group, name, i1, i2, valstr);
}
void
WriteString(CptGroup *group, char *name, int i1, int i2, char *val)
{
char valstr[MAX_LENGTH];
sprintf(valstr, STRING_FORMAT, val);
SaveLine(group, name, i1, i2, valstr);
}
void
WriteBlock(CptGroup *group, char *name, int i1, int i2,
int64 pos, int64 val)
{
char valstr[MAX_LENGTH];
sprintf(valstr, BLOCK_FORMAT,(cptlong) pos, (cptlong)val);
SaveLine(group, name, i1, i2, valstr);
}
int
Simcpt_CptInt(CptDescriptor *cptd, char *name, int i1, int i2, int *val)
{
if (cptd->mode == CPT_SAVE)
WriteInteger(cptd->group, name, i1, i2, (int64) *val);
else
*val = (int) ReadInteger(cptd->group, name, i1, i2);
return 0;
}
int
Simcpt_CptUint(CptDescriptor *cptd, char *name, int i1, int i2, uint *val)
{
if (cptd->mode == CPT_SAVE)
WriteInteger(cptd->group, name, i1, i2, (int64) *val);
else
*val = (uint) ReadInteger(cptd->group, name, i1, i2);
return 0;
}
int
Simcpt_CptHex(CptDescriptor *cptd, char *name, int i1, int i2, uint *val)
{
if (cptd->mode == CPT_SAVE)
WriteHexInteger(cptd->group, name, i1, i2, (int64) *val);
else
*val = (uint) ReadInteger(cptd->group, name, i1, i2);
return 0;
}
int
Simcpt_CptChar(CptDescriptor *cptd, char *name, int i1, int i2, char *val)
{
if (cptd->mode == CPT_SAVE)
WriteInteger(cptd->group, name, i1, i2, (int64) *val);
else
*val = (char) ReadInteger(cptd->group, name, i1, i2);
return 0;
}
int
Simcpt_CptUchar(CptDescriptor *cptd, char *name, int i1, int i2, unsigned char *val)
{
if (cptd->mode == CPT_SAVE)
WriteHexInteger(cptd->group, name, i1, i2, (int64) *val);
else
*val = (unsigned char) ReadInteger(cptd->group, name, i1, i2);
return 0;
}
int
Simcpt_CptLong(CptDescriptor *cptd, char *name, int i1, int i2, long *val)
{
if (cptd->mode == CPT_SAVE)
WriteInteger(cptd->group, name, i1, i2, (int64) *val);
else
*val = (long) ReadInteger(cptd->group, name, i1, i2);
return 0;
}
int
Simcpt_CptUlong(CptDescriptor *cptd, char *name, int i1, int i2, unsigned long *val)
{
if (cptd->mode == CPT_SAVE)
WriteInteger(cptd->group, name, i1, i2, (int64) *val);
else
*val = (long) ReadInteger(cptd->group, name, i1, i2);
return 0;
}
int
Simcpt_CptULL(CptDescriptor *cptd, char *name, int i1, int i2, uint64 *val)
{
if (cptd->mode == CPT_SAVE)
WriteInteger(cptd->group, name, i1, i2, (int64) *val);
else
*val = (uint64) ReadInteger(cptd->group, name, i1, i2);
return 0;
}
int
Simcpt_CptParamInt(CptDescriptor *cptd, char *name, int i1, int i2,
int *paramAddr)
{
if (cptd->mode == CPT_SAVE)
WriteInteger(cptd->group, name, i1, i2, (int64) *paramAddr);
else
*paramAddr = (int) ReadInteger(cptd->group, name, i1, i2);
return 0;
}
int
Simcpt_CptString(CptDescriptor *cptd, char *name, int i1, int i2, char **val)
{
if (cptd->mode == CPT_SAVE)
WriteString(cptd->group, name, i1, i2, *val);
else {
char *ptr = ReadString(cptd->group, name, i1, i2);
if (ptr == NULL) return 1;
if (*val == NULL)
*val = SaveString(ptr);
else
strcpy(*val, ptr);
}
return 0;
}
int
Simcpt_CptBlock(CptDescriptor *cptd, char *name, int i1, int i2, void *val,
int size, int outputFDesc)
{
if (cptd->mode == CPT_SAVE) {
WriteBlock(cptd->group, name, i1, i2, (int64) cptd->group->binpos,
size);
SaveBlock(cptd->group, (caddr_t) val, size);
} else {
int64 pos;
int bsize;
pos = ReadBlock(cptd->group, name, i1, i2, &bsize);
if (size != bsize) {
CPUWarning("CPT: Binary data size difference in %s:\n"
"CPT: SimOS expected a data block of %d bytes\n"
"CPT: Checkpoint file provided %d bytes\n",
cptd->group->tag, size, bsize);
}
outputFD = outputFDesc;
RestoreBlock(cptd->group, (caddr_t) val, pos, bsize);
outputFD = -1;
}
return 0;
}
int
Simcpt_Comment(CptDescriptor *cptd, char *comment)
{
if (cptd->mode == CPT_SAVE) {
fprintf(cptd->group->stream, "# %s\n", comment);
}
return 0;
}
/* Calls to provide optional parameters */
int
Simcpt_OptionalInteger(CptDescriptor *cptd, char *name, int i1, int i2, int64 val)
{
CptGroup *group = cptd->group;
CptEntry *entry = NewEntry(name, i1, i2, CPT_INTEGER);
if (entry == NULL) return -1;
entry->optional = TRUE;
entry->type = CPT_INTEGER;
entry->intval = val;
AddEntry(group, entry);
return 0;
}
int
Simcpt_OptionalString(CptDescriptor *cptd, char *name, int i1, int i2, char *val)
{
char valstring[MAX_LENGTH];
CptGroup *group = cptd->group;
CptEntry *entry = NewEntry(name, i1, i2, CPT_STRING);
if (entry == NULL) return -1;
entry->optional = TRUE;
entry->ptrval = (int64)PTR_TO_UINT64(SaveString(valstring));
AddEntry(group, entry);
return 0;
}
bool
Simcpt_IsRemote(void)
{
return cptRemote;
}
int
Simcpt_GetMemoryCptFile(void)
{
char filename[MAX_PATH_LENGTH];
char pathname[MAX_PATH_LENGTH];
int memfd;
CptGroup *group = FindGroup("memory");
CptCompression compress;
if (group == NULL) {
CPUWarning("CPT: Couldn't open memory checkpoint file for mmap\n");
return -1;
}
if (cptRemote) {
CPUWarning("CPT: Cannot mmap memory checkpoint file on rmtaccess server\n");
return -1;
}
sprintf(filename, "%s.%s.bin", cptName, group->tag);
compress = GetCompression(filename, pathname);
if (compress != NO_COMPRESSION) {
if (compress != NUM_COMPRESS_TYPES)
CPUWarning("CPT: Cannot mmap file %s -- it is compressed.\n", pathname);
else
CPUWarning("CPT: Cannot find file %s for mmap.\n", filename);
return -1;
}
memfd = open(pathname, O_RDONLY);
if (memfd < 0)
CPUWarning("CPT: Cannot open file %s for mmap.\n", pathname);
return memfd;
}
int
Simcpt_CanMmapMemoryCheckpoint( void )
{
if (cptRemote) {
return FALSE;
} else {
CptGroup *group = FindGroup("memory");
char filename[MAX_PATH_LENGTH];
char pathname[MAX_PATH_LENGTH];
ASSERT(group != NULL);
sprintf(filename, "%s.%s.bin", cptName, group->tag);
return (GetCompression(filename, pathname) == NO_COMPRESSION);
}
}
void
Simcpt_Preread_Numcells(void)
{
CptGroup *group;
if (cptVersion.ver == 3) {
group = NewGroup("firewall");
OpenCptFile(CPT_RESTORE, group);
NUM_CELLS(0) = ReadInteger(group, "NumCells", NO_INDEX, NO_INDEX);
CloseCptFile(group);
group->tag = "HOHA";
}
}
void
Simcpt_Preread_Numether(void)
{
CptGroup *group;
if (cptVersion.ver == 3) {
group = NewGroup("ether");
OpenCptFile(CPT_RESTORE, group);
NUM_ETHER_CONTROLLERS(0) = ReadInteger(group, "NumControllers",
NO_INDEX, NO_INDEX);
CloseCptFile(group);
group->tag = "HOHA";
}
}