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