83a82275098d3a6b6e9ebd2b7f03cb1999cec27e
[model-checker.git] / scanalysis.cc
1 #include "scanalysis.h"
2 #include "action.h"
3 #include "threads-model.h"
4 #include "clockvector.h"
5
6 SCAnalysis::SCAnalysis() {
7         cvmap=new HashTable<const ModelAction *, ClockVector *, uintptr_t, 4>();
8         cycleset=new HashTable<const ModelAction *, const ModelAction *, uintptr_t, 4>();
9         threadlists=new SnapVector<action_list_t>(1);
10 }
11
12 SCAnalysis::~SCAnalysis() {
13         delete cvmap;
14         delete cycleset;
15         delete threadlists;
16 }
17
18 void SCAnalysis::print_list(action_list_t *list) {
19         action_list_t::iterator it;
20
21         model_print("---------------------------------------------------------------------\n");
22
23         unsigned int hash = 0;
24
25         for (it = list->begin(); it != list->end(); it++) {
26                 const ModelAction *act = *it;
27                 if (act->get_seq_number() > 0) {
28                         if (cycleset->contains(act))
29                                 model_print("CYC");
30                         act->print();
31                 }
32                 hash = hash^(hash<<3)^((*it)->hash());
33         }
34         model_print("HASH %u\n", hash);
35         model_print("---------------------------------------------------------------------\n");
36 }
37
38 void SCAnalysis::analyze(action_list_t * actions) {
39         buildVectors(actions);
40         computeCV(actions);
41         action_list_t *list=generateSC(actions);
42         print_list(list);
43 }
44
45 bool SCAnalysis::merge(ClockVector * cv, const ModelAction * act, ClockVector *cv2) {
46         if (cv2->getClock(act->get_tid())>=act->get_seq_number() && act->get_seq_number() != 0) {
47                 cycleset->put(act, act);
48         }
49         return cv->merge(cv2);
50 }
51
52 ModelAction * SCAnalysis::getNextAction() {
53         ModelAction *act=NULL;
54         for(int i=0;i<=maxthreads;i++) {
55                 action_list_t * threadlist=&(*threadlists)[i];
56                 if (threadlist->empty())
57                         continue;
58                 ModelAction *first=threadlist->front();
59                 if (act==NULL) {
60                         act=first;
61                         continue;
62                 }
63                 ClockVector *cv=cvmap->get(act);
64                 if (cv->synchronized_since(first)) {
65                         act=first;
66                 }
67         }
68         return act;
69 }
70
71 action_list_t * SCAnalysis::generateSC(action_list_t *list) {   
72         action_list_t *sclist=new action_list_t();
73         while (true) {
74                 ModelAction * act=getNextAction();
75                 if (act==NULL)
76                         break;
77                 thread_id_t tid=act->get_tid();
78                 //remove action
79                 (*threadlists)[id_to_int(tid)].pop_front();
80                 //add ordering constraints from this choice
81                 if (updateConstraints(act)) {
82                         //propagate changes if we have them
83                         computeCV(list);
84                 }
85                 //add action to end
86                 sclist->push_back(act);
87         }
88         return sclist;
89 }
90
91 void SCAnalysis::buildVectors(action_list_t *list) {
92         maxthreads=0;
93         for (action_list_t::iterator it = list->begin(); it != list->end(); it++) {
94                 ModelAction *act = *it;
95                 int threadid=id_to_int(act->get_tid());
96                 if (threadid > maxthreads) {
97                         threadlists->resize(threadid+1);
98                         maxthreads=threadid;
99                 }
100                 (*threadlists)[threadid].push_back(act);
101         }
102 }
103
104 bool SCAnalysis::updateConstraints(ModelAction *act) {
105         bool changed=false;
106         ClockVector *actcv = cvmap->get(act);
107         for(int i=0;i<=maxthreads;i++) {
108                 thread_id_t tid=int_to_id(i);
109                 if (tid==act->get_tid())
110                         continue;
111
112                 action_list_t * list=&(*threadlists)[id_to_int(tid)];
113                 for (action_list_t::iterator rit = list->begin(); rit != list->end(); rit++) {
114                         ModelAction *write = *rit;
115                         if (!write->is_write())
116                                 continue;
117                         ClockVector *writecv = cvmap->get(write);
118                         if (writecv->synchronized_since(act))
119                                 break;
120                         if (write->get_location() == act->get_location()) {
121                                 //write is sc after act
122                                 merge(writecv, write, actcv);
123                                 changed=true;
124                                 break;
125                         }
126                 }               
127         }
128         return changed;
129 }
130
131 bool SCAnalysis::processRead(ModelAction *read, ClockVector *cv) {
132         bool changed=false;
133
134         /* Merge in the clock vector from the write */
135         const ModelAction *write=read->get_reads_from();
136         ClockVector *writecv=cvmap->get(write);
137         changed|= ( writecv == NULL || merge(cv, read, writecv) && (*read < *write));
138
139         for(int i=0;i<=maxthreads;i++) {
140                 thread_id_t tid=int_to_id(i);
141                 if (tid==read->get_tid())
142                         continue;
143                 if (tid==write->get_tid())
144                         continue;
145                 action_list_t * list=model->get_actions_on_obj(read->get_location(), tid);
146                 if (list==NULL)
147                         continue;
148                 for (action_list_t::reverse_iterator rit = list->rbegin(); rit != list->rend(); rit++) {
149                         ModelAction *write2 = *rit;
150                         if (!write2->is_write())
151                                 continue;
152
153                         ClockVector *write2cv = cvmap->get(write2);
154                         if (write2cv == NULL)
155                                 continue;
156                         
157                         /* write -sc-> write2 &&
158                                  write -rf-> R =>
159                                  R -sc-> write2 */
160                         if (write2cv->synchronized_since(write)) {
161                                 changed |= merge(write2cv, write2, cv);
162                         }
163                 
164                         //looking for earliest write2 in iteration to satisfy this
165                         /* write2 -sc-> R &&
166                                  write -rf-> R =>
167                                  write2 -sc-> write */
168                         if (cv->synchronized_since(write2)) {
169                                 changed |= writecv == NULL || merge(writecv, write, write2cv);
170                                 break;
171                         }
172                 }
173         }
174         return changed;
175 }
176
177
178 void SCAnalysis::computeCV(action_list_t *list) {
179         bool changed=true;
180         bool firsttime=true;
181         ModelAction **last_act=(ModelAction **)model_calloc(1,(maxthreads+1)*sizeof(ModelAction *));
182         while(changed) {
183                 changed=changed&firsttime;
184                 firsttime=false;
185
186                 for (action_list_t::iterator it = list->begin(); it != list->end(); it++) {
187                         ModelAction *act = *it;
188                         ModelAction *lastact = last_act[id_to_int(act->get_tid())];
189                         if (act->is_thread_start())
190                                 lastact=model->get_thread(act)->get_creation();
191                         ClockVector *lastcv=(lastact != NULL) ? cvmap->get(lastact) : NULL;
192                         last_act[id_to_int(act->get_tid())]=act;
193                         ClockVector *cv=cvmap->get(act);
194                         if ( cv == NULL ) {
195                                 cv = new ClockVector(lastcv, act);
196                                 cvmap->put(act, cv);
197                         } else if ( lastcv != NULL ) {
198                                 merge(cv, act, lastcv);
199                         }
200                         if (act->is_thread_join()) {
201                                 Thread *joinedthr = act->get_thread_operand();
202                                 ModelAction *finish = model->get_last_action(joinedthr->get_id());
203                                 ClockVector *finishcv = cvmap->get(finish);
204                                 changed |= (finishcv == NULL) || merge(cv, act, finishcv);
205                         }
206                         if (act->is_thread_join()) {
207                                 Thread *joinedthr = act->get_thread_operand();
208                                 ModelAction *finish = model->get_last_action(joinedthr->get_id());
209                                 ClockVector *finishcv = cvmap->get(finish);
210                                 changed |= (finishcv == NULL) || cv->merge(finishcv);
211                         }
212                         if (act->is_read()) {
213                                 changed|=processRead(act, cv);
214                         }
215                 }
216                 /* Reset the last action array */
217                 if (changed) {
218                         bzero(last_act, (maxthreads+1)*sizeof(ModelAction *));
219                 }
220         }
221         model_free(last_act);
222 }