Defining the interfaces to add various regions to snapshot
[model-checker.git] / snapshot-interface.h
1 #ifndef __SNAPINTERFACE_H
2 #define __SNAPINTERFACE_H
3 #include "snapshot.h"
4 #include "mymemory.h"
5 #include <vector>
6 #include <utility>
7 #include <string>
8 #include <map>
9 #include <set>
10 #include "snapshot.h"
11 #include "libthreads.h"
12 typedef std::basic_string< char, std::char_traits< char >, MyAlloc< char > > MyString;
13 namespace snapshot_utils{
14 /*
15 SnapshotTree defines the interfaces and ideal points that require snapshotting.
16 The algorithm is: When a step happens we create a new Tree node and define that node as the current scope.
17 when it is time to set a backtracking point, for each backtracking point we record its parent
18 finally when set current scope is called, we see if this state is a parent 
19 where possible the goal is speed so we use an integer comparison to make all comparison operations required for 
20 finding a given node O 1
21 */
22         typedef std::map< thrd_t, int, std::less< thrd_t >, MyAlloc< std::pair< const thrd_t, int > > > ThreadStepMap_t;
23         MyString SerializeThreadSteps( const ThreadStepMap_t & theMap );
24         class snapshotTree;
25         struct snapshotTreeComp{
26                 public:
27                         bool operator()( const std::pair< const snapshotTree*, snapshot_id > & lhs, const std::pair< const snapshotTree*, snapshot_id > & rhs );        
28         };
29         
30         typedef std::map< snapshotTree *, snapshot_id, snapshotTreeComp, MyAlloc< std::pair< const snapshotTree *, snapshot_id > > > SnapshotToStateMap_t;
31         typedef std::set< snapshotTree*, std::less< snapshotTree * >, MyAlloc< snapshotTree > > BackTrackingParents_t; 
32         typedef std::vector< snapshotTree *, MyAlloc< snapshotTree * > > SnapshotChildren_t;
33         typedef std::map< MyString, snapshotTree *, std::less<  MyString >, MyAlloc< std::pair< const MyString, snapshotTree * > > > SnapshotEdgeMap_t;
34     void SnapshotGlobalSegments();
35     class snapshotTree{
36                 friend struct snapshotTreeComp;
37         public:
38                 MEMALLOC
39                 explicit snapshotTree( );
40                 ~snapshotTree();
41                 static snapshotTree * ReturnCurrentScope();
42                 std::pair< MyString, bool > TakeStep( thrd_t which, thrd_t whoseNext );
43         private: 
44                 unsigned int mTimeStamp;
45                 ThreadStepMap_t mThreadStepsTaken;
46                 thrd_t mNextStepTaker;
47                 snapshotTree * mpParent;
48                 SnapshotChildren_t mChildren;
49                 void AddThreadStep( ThreadStepMap_t & theMap, thrd_t theThread );
50                 static snapshotTree * msCurrentScope;
51                 static BackTrackingParents_t msRecordedParents;
52                 static SnapshotToStateMap_t msSnapshottedStates;
53                 static SnapshotEdgeMap_t msSnapshotEdgesMap; //this might not actually needed, if this is blowing up memory we have to traverse and find as opposed to looking up....
54                 static unsigned int msTimeCounter;
55         void EnsureRelevantRegionsSnapshotted();
56         public:
57                 void BacktrackingPointSet();
58                 bool operator<( const snapshotTree & rhs ) const;
59                 snapshot_id ReturnEarliestSnapshotID( MyString key );
60                 snapshot_id SnapshotNow();
61         };
62 };
63 #endif