10 #include "snapshotimp.h"
15 #include <semaphore.h>
20 //extern declaration definition
21 #define FAILURE(mesg) { printf("failed in the API: %s with errno relative message: %s\n", mesg, strerror( errno ) ); exit( -1 ); }
23 struct SnapShot * snapshotrecord = NULL;
24 struct Snapshot_t * sTheRecord = NULL;
26 struct Snapshot_t * sTheRecord = NULL;
28 void DumpIntoLog( const char * filename, const char * message ){
30 static pid_t thePID = getpid();
31 char newFn[ 1024 ] ={ 0 };
32 sprintf( newFn,"%s-%d.txt", filename, thePID );
33 FILE * myFile = fopen( newFn, "w+" );
34 fprintf( myFile, "the message %s: the process id %d\n", message, thePID );
40 #if !USE_CHECKPOINTING
41 static ucontext_t savedSnapshotContext;
42 static ucontext_t savedUserSnapshotContext;
43 static snapshot_id snapshotid = 0;
45 /* Initialize snapshot data structure */
47 void initSnapShotRecord(unsigned int numbackingpages, unsigned int numsnapshots, unsigned int nummemoryregions) {
48 snapshotrecord=( struct SnapShot * )MYMALLOC(sizeof(struct SnapShot));
49 snapshotrecord->regionsToSnapShot=( struct MemoryRegion * )MYMALLOC(sizeof(struct MemoryRegion)*nummemoryregions);
50 snapshotrecord->backingStoreBasePtr= ( struct SnapShotPage * )MYMALLOC( sizeof( struct SnapShotPage ) * (numbackingpages + 1) );
51 //Page align the backingstorepages
52 snapshotrecord->backingStore=( struct SnapShotPage * )PageAlignAddressUpward(snapshotrecord->backingStoreBasePtr);
53 snapshotrecord->backingRecords=( struct BackingPageRecord * )MYMALLOC(sizeof(struct BackingPageRecord)*numbackingpages);
54 snapshotrecord->snapShots= ( struct SnapShotRecord * )MYMALLOC(sizeof(struct SnapShotRecord)*numsnapshots);
55 snapshotrecord->lastSnapShot=0;
56 snapshotrecord->lastBackingPage=0;
57 snapshotrecord->lastRegion=0;
58 snapshotrecord->maxRegions=nummemoryregions;
59 snapshotrecord->maxBackingPages=numbackingpages;
60 snapshotrecord->maxSnapShots=numsnapshots;
62 #endif //nothing to initialize for the fork based snapshotting.
64 void HandlePF( int sig, siginfo_t *si, void * unused){
66 if( si->si_code == SEGV_MAPERR ){
67 printf("Real Fault at %p\n", si->si_addr);
70 void* addr = ReturnPageAlignedAddress(si->si_addr);
71 unsigned int backingpage=snapshotrecord->lastBackingPage++; //Could run out of pages...
72 if (backingpage==snapshotrecord->maxBackingPages) {
73 printf("Out of backing pages at %p\n", si->si_addr);
78 memcpy(&(snapshotrecord->backingStore[backingpage]), addr, sizeof(struct SnapShotPage));
79 //remember where to copy page back to
80 snapshotrecord->backingRecords[backingpage].basePtrOfPage=addr;
81 //set protection to read/write
82 if (mprotect( addr, sizeof(struct SnapShotPage), PROT_READ | PROT_WRITE )) {
84 // Handle error by quitting?
86 #endif //nothing to handle for non snapshotting case.
89 //Return a page aligned address for the address being added
90 //as a side effect the numBytes are also changed.
91 void * ReturnPageAlignedAddress(void * addr) {
92 return (void *)(((uintptr_t)addr)&~(PAGESIZE-1));
95 //Return a page aligned address for the address being added
96 //as a side effect the numBytes are also changed.
97 void * PageAlignAddressUpward(void * addr) {
98 return (void *)((((uintptr_t)addr)+PAGESIZE-1)&~(PAGESIZE-1));
103 void createSharedLibrary(){
104 #if !USE_CHECKPOINTING
105 //step 1. create shared memory.
106 if( sTheRecord ) return;
107 int fd = shm_open( "/ModelChecker-Snapshotter", O_RDWR | O_CREAT, 0777 ); //universal permissions.
108 if( -1 == fd ) FAILURE("shm_open");
109 if( -1 == ftruncate( fd, ( size_t )SHARED_MEMORY_DEFAULT + ( size_t )STACK_SIZE_DEFAULT ) ) FAILURE( "ftruncate" );
110 char * memMapBase = ( char * ) mmap( 0, ( size_t )SHARED_MEMORY_DEFAULT + ( size_t )STACK_SIZE_DEFAULT, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0 );
111 if( MAP_FAILED == memMapBase ) FAILURE("mmap");
112 sTheRecord = ( struct Snapshot_t * )memMapBase;
113 sTheRecord->mSharedMemoryBase = memMapBase + sizeof( struct Snapshot_t );
114 sTheRecord->mStackBase = ( char * )memMapBase + ( size_t )SHARED_MEMORY_DEFAULT;
115 sTheRecord->mStackSize = STACK_SIZE_DEFAULT;
116 sTheRecord->mIDToRollback = -1;
117 sTheRecord->currSnapShotID = 0;
123 void initSnapShotLibrary(unsigned int numbackingpages, unsigned int numsnapshots, unsigned int nummemoryregions, unsigned int numheappages, MyFuncPtr entryPoint){
124 #if USE_CHECKPOINTING
125 /* Setup a stack for our signal handler.... */
127 ss.ss_sp = MYMALLOC(SIGSTACKSIZE);
128 ss.ss_size = SIGSTACKSIZE;
130 sigaltstack(&ss, NULL);
133 sa.sa_flags = SA_SIGINFO | SA_NODEFER | SA_RESTART | SA_ONSTACK;
134 sigemptyset( &sa.sa_mask );
135 sa.sa_sigaction = HandlePF;
136 if( sigaction( SIGSEGV, &sa, NULL ) == -1 ){
137 printf("SIGACTION CANNOT BE INSTALLED\n");
140 initSnapShotRecord(numbackingpages, numsnapshots, nummemoryregions);
142 basemySpace=MYMALLOC((numheappages+1)*PAGESIZE);
143 void * pagealignedbase=PageAlignAddressUpward(basemySpace);
144 mySpace = create_mspace_with_base(pagealignedbase, numheappages*PAGESIZE, 1 );
145 addMemoryRegionToSnapShot(pagealignedbase, numheappages);
148 //add a signal to indicate that the process is going to terminate.
150 sa.sa_flags = SA_SIGINFO | SA_NODEFER | SA_RESTART;
151 sigemptyset( &sa.sa_mask );
152 sa.sa_sigaction = HandlePF;
153 if( sigaction( SIGUSR1, &sa, NULL ) == -1 ){
154 printf("SIGACTION CANNOT BE INSTALLED\n");
157 createSharedLibrary();
159 //step 2 setup the stack context.
161 int alreadySwapped = 0;
162 getcontext( &savedSnapshotContext );
163 if( !alreadySwapped ){
165 ucontext_t currentContext, swappedContext, newContext;
166 getcontext( &newContext );
167 newContext.uc_stack.ss_sp = sTheRecord->mStackBase;
168 newContext.uc_stack.ss_size = STACK_SIZE_DEFAULT;
169 newContext.uc_link = ¤tContext;
170 makecontext( &newContext, entryPoint, 0 );
171 swapcontext( &swappedContext, &newContext );
174 //add the code to take a snapshot here...
175 //to return to user process, do a second swapcontext...
177 snapshotid = sTheRecord->currSnapShotID;
178 bool swapContext = false;
179 while( !sTheRecord->mbFinalize ){
180 sTheRecord->currSnapShotID=snapshotid+1;
183 ucontext_t currentContext;
189 swapcontext( ¤tContext, &( sTheRecord->mContextToRollback ) );
191 swapcontext( ¤tContext, &savedUserSnapshotContext );
197 char mesg[ 1024 ] = { 0 };
198 sprintf( mesg, "The process id of child is %d and the process id of this process is %d and snapshot id is %d", forkedID, getpid(), snapshotid );
199 DumpIntoLog( "ModelSnapshot", mesg );
202 retVal=waitpid( forkedID, &status, 0 );
203 } while( -1 == retVal && errno == EINTR );
205 if( sTheRecord->mIDToRollback != snapshotid )
215 /* This function assumes that addr is page aligned */
216 void addMemoryRegionToSnapShot( void * addr, unsigned int numPages) {
217 #if USE_CHECKPOINTING
218 unsigned int memoryregion=snapshotrecord->lastRegion++;
219 if (memoryregion==snapshotrecord->maxRegions) {
220 printf("Exceeded supported number of memory regions!\n");
224 snapshotrecord->regionsToSnapShot[ memoryregion ].basePtr=addr;
225 snapshotrecord->regionsToSnapShot[ memoryregion ].sizeInPages=numPages;
226 #endif //NOT REQUIRED IN THE CASE OF FORK BASED SNAPSHOTS.
229 snapshot_id takeSnapshot( ){
230 #if USE_CHECKPOINTING
231 for(unsigned int region=0; region<snapshotrecord->lastRegion;region++) {
232 if( mprotect(snapshotrecord->regionsToSnapShot[region].basePtr, snapshotrecord->regionsToSnapShot[region].sizeInPages*sizeof(struct SnapShotPage), PROT_READ ) == -1 ){
234 printf("Failed to mprotect inside of takeSnapShot\n");
238 unsigned int snapshot=snapshotrecord->lastSnapShot++;
239 if (snapshot==snapshotrecord->maxSnapShots) {
240 printf("Out of snapshots\n");
243 snapshotrecord->snapShots[snapshot].firstBackingPage=snapshotrecord->lastBackingPage;
247 swapcontext( &savedUserSnapshotContext, &savedSnapshotContext );
251 void rollBack( snapshot_id theID ){
252 #if USE_CHECKPOINTING
253 std::map< void *, bool, std::less< void * >, MyAlloc< std::pair< const void *, bool > > > duplicateMap;
254 for(unsigned int region=0; region<snapshotrecord->lastRegion;region++) {
255 if( mprotect(snapshotrecord->regionsToSnapShot[region].basePtr, snapshotrecord->regionsToSnapShot[region].sizeInPages*sizeof(struct SnapShotPage), PROT_READ | PROT_WRITE ) == -1 ){
257 printf("Failed to mprotect inside of takeSnapShot\n");
261 for(unsigned int page=snapshotrecord->snapShots[theID].firstBackingPage; page<snapshotrecord->lastBackingPage; page++) {
263 if( duplicateMap.find( snapshotrecord->backingRecords[page].basePtrOfPage ) != duplicateMap.end() ){
267 duplicateMap[ snapshotrecord->backingRecords[page].basePtrOfPage ] = true;
270 memcpy(snapshotrecord->backingRecords[page].basePtrOfPage, &snapshotrecord->backingStore[page], sizeof(struct SnapShotPage));
273 snapshotrecord->lastSnapShot=theID;
274 snapshotrecord->lastBackingPage=snapshotrecord->snapShots[theID].firstBackingPage;
275 takeSnapshot(); //Make sure current snapshot is still good...All later ones are cleared
277 sTheRecord->mIDToRollback = theID;
279 getcontext( &sTheRecord->mContextToRollback );
283 DumpIntoLog( "ModelSnapshot", "Invoked rollback" );
291 #if !USE_CHECKPOINTING
292 sTheRecord->mbFinalize = true;