1 #include "scanalysis.h"
3 #include "threads-model.h"
4 #include "clockvector.h"
7 SCAnalysis::SCAnalysis() :
19 SCAnalysis::~SCAnalysis() {
22 void SCAnalysis::setExecution(ModelExecution * execution) {
23 this->execution=execution;
26 const char * SCAnalysis::name() {
27 const char * name = "SC";
31 bool SCAnalysis::option(char * opt) {
32 if (strcmp(opt, "verbose")==0) {
35 } else if (strcmp(opt, "buggy")==0) {
37 } else if (strcmp(opt, "quiet")==0) {
40 } if (strcmp(opt, "help") != 0) {
41 model_print("Unrecognized option: %s\n", opt);
44 model_print("SC Analysis options\n");
45 model_print("verbose -- print all feasible executions\n");
46 model_print("buggy -- print only buggy executions (default)\n");
47 model_print("quiet -- print nothing\n");
53 void SCAnalysis::print_list(action_list_t *list) {
54 model_print("---------------------------------------------------------------------\n");
56 model_print("Not SC\n");
57 unsigned int hash = 0;
59 for (action_list_t::iterator it = list->begin(); it != list->end(); it++) {
60 const ModelAction *act = *it;
61 if (act->get_seq_number() > 0) {
62 if (badrfset.contains(act))
65 if (badrfset.contains(act)) {
66 model_print("Desired Rf: %u \n", badrfset.get(act)->get_seq_number());
69 hash = hash ^ (hash << 3) ^ ((*it)->hash());
71 model_print("HASH %u\n", hash);
72 model_print("---------------------------------------------------------------------\n");
75 void SCAnalysis::analyze(action_list_t *actions) {
76 action_list_t *list = generateSC(actions);
78 if (print_always || (print_buggy && execution->have_bug_reports()))
82 void SCAnalysis::check_rf(action_list_t *list) {
83 for (action_list_t::iterator it = list->begin(); it != list->end(); it++) {
84 const ModelAction *act = *it;
86 if (act->get_reads_from() != lastwrmap.get(act->get_location()))
87 badrfset.put(act, lastwrmap.get(act->get_location()));
90 lastwrmap.put(act->get_location(), act);
94 bool SCAnalysis::merge(ClockVector *cv, const ModelAction *act, const ModelAction *act2) {
95 ClockVector *cv2 = cvmap.get(act2);
98 if (cv2->getClock(act->get_tid()) >= act->get_seq_number() && act->get_seq_number() != 0) {
100 //refuse to introduce cycles into clock vectors
104 return cv->merge(cv2);
107 int SCAnalysis::getNextActions(ModelAction ** array) {
110 for (int t = 0; t <= maxthreads; t++) {
111 action_list_t *tlt = &threadlists[t];
114 ModelAction *act = tlt->front();
115 ClockVector *cv = cvmap.get(act);
117 /* Find the earliest in SC ordering */
118 for (int i = 0; i <= maxthreads; i++) {
121 action_list_t *threadlist = &threadlists[i];
122 if (threadlist->empty())
124 ModelAction *first = threadlist->front();
125 if (cv->synchronized_since(first)) {
136 for (int t = 0; t <= maxthreads; t++) {
137 action_list_t *tlt = &threadlists[t];
140 ModelAction *act = tlt->front();
141 ClockVector *cv = act->get_cv();
143 /* Find the earliest in SC ordering */
144 for (int i = 0; i <= maxthreads; i++) {
147 action_list_t *threadlist = &threadlists[i];
148 if (threadlist->empty())
150 ModelAction *first = threadlist->front();
151 if (cv->synchronized_since(first)) {
161 ASSERT(count==0 || cyclic);
166 ModelAction * SCAnalysis::pruneArray(ModelAction **array,int count) {
171 /* Choose first non-write action */
172 ModelAction *nonwrite=NULL;
173 for(int i=0;i<count;i++) {
174 if (!array[i]->is_write())
175 if (nonwrite==NULL || nonwrite->get_seq_number() > array[i]->get_seq_number())
178 if (nonwrite != NULL)
181 /* Look for non-conflicting action */
182 ModelAction *nonconflict=NULL;
183 for(int a=0;a<count;a++) {
184 ModelAction *act=array[a];
185 for (int i = 0; i <= maxthreads && act != NULL; i++) {
186 thread_id_t tid = int_to_id(i);
187 if (tid == act->get_tid())
190 action_list_t *list = &threadlists[id_to_int(tid)];
191 for (action_list_t::iterator rit = list->begin(); rit != list->end(); rit++) {
192 ModelAction *write = *rit;
193 if (!write->is_write())
195 ClockVector *writecv = cvmap.get(write);
196 if (writecv->synchronized_since(act))
198 if (write->get_location() == act->get_location()) {
199 //write is sc after act
206 if (nonconflict == NULL || nonconflict->get_seq_number() > act->get_seq_number())
213 action_list_t * SCAnalysis::generateSC(action_list_t *list) {
214 int numactions=buildVectors(list);
217 action_list_t *sclist = new action_list_t();
218 ModelAction **array = (ModelAction **)model_calloc(1, (maxthreads + 1) * sizeof(ModelAction *));
219 int * choices = (int *) model_calloc(1, sizeof(int)*numactions);
224 int numActions = getNextActions(array);
227 ModelAction * act=pruneArray(array, numActions);
229 if (currchoice < endchoice) {
230 act = array[choices[currchoice]];
231 //check whether there is still another option
232 if ((choices[currchoice]+1)<numActions)
233 lastchoice=currchoice;
237 choices[currchoice]=0;
239 lastchoice=currchoice;
243 thread_id_t tid = act->get_tid();
245 threadlists[id_to_int(tid)].pop_front();
246 //add ordering constraints from this choice
247 if (updateConstraints(act)) {
248 //propagate changes if we have them
251 if (!prevc && cyclic) {
252 model_print("ROLLBACK in SC\n");
253 //check whether we have another choice
254 if (lastchoice != -1) {
255 //have to reset everything
256 choices[lastchoice]++;
257 endchoice=lastchoice+1;
269 sclist->push_back(act);
275 int SCAnalysis::buildVectors(action_list_t *list) {
278 for (action_list_t::iterator it = list->begin(); it != list->end(); it++) {
279 ModelAction *act = *it;
281 int threadid = id_to_int(act->get_tid());
282 if (threadid > maxthreads) {
283 threadlists.resize(threadid + 1);
284 maxthreads = threadid;
286 threadlists[threadid].push_back(act);
291 void SCAnalysis::reset(action_list_t *list) {
292 for (int t = 0; t <= maxthreads; t++) {
293 action_list_t *tlt = &threadlists[t];
296 for (action_list_t::iterator it = list->begin(); it != list->end(); it++) {
297 ModelAction *act = *it;
298 delete cvmap.get(act);
299 cvmap.put(act, NULL);
305 bool SCAnalysis::updateConstraints(ModelAction *act) {
306 bool changed = false;
307 for (int i = 0; i <= maxthreads; i++) {
308 thread_id_t tid = int_to_id(i);
309 if (tid == act->get_tid())
312 action_list_t *list = &threadlists[id_to_int(tid)];
313 for (action_list_t::iterator rit = list->begin(); rit != list->end(); rit++) {
314 ModelAction *write = *rit;
315 if (!write->is_write())
317 ClockVector *writecv = cvmap.get(write);
318 if (writecv->synchronized_since(act))
320 if (write->get_location() == act->get_location()) {
321 //write is sc after act
322 merge(writecv, write, act);
331 bool SCAnalysis::processRead(ModelAction *read, ClockVector *cv) {
332 bool changed = false;
334 /* Merge in the clock vector from the write */
335 const ModelAction *write = read->get_reads_from();
336 ClockVector *writecv = cvmap.get(write);
337 changed |= merge(cv, read, write) && (*read < *write);
339 for (int i = 0; i <= maxthreads; i++) {
340 thread_id_t tid = int_to_id(i);
341 if (tid == read->get_tid())
343 if (tid == write->get_tid())
345 action_list_t *list = execution->get_actions_on_obj(read->get_location(), tid);
348 for (action_list_t::reverse_iterator rit = list->rbegin(); rit != list->rend(); rit++) {
349 ModelAction *write2 = *rit;
350 if (!write2->is_write())
353 ClockVector *write2cv = cvmap.get(write2);
354 if (write2cv == NULL)
357 /* write -sc-> write2 &&
360 if (write2cv->synchronized_since(write)) {
361 changed |= merge(write2cv, write2, read);
364 //looking for earliest write2 in iteration to satisfy this
367 write2 -sc-> write */
368 if (cv->synchronized_since(write2)) {
369 changed |= writecv == NULL || merge(writecv, write, write2);
377 void SCAnalysis::computeCV(action_list_t *list) {
379 bool firsttime = true;
380 ModelAction **last_act = (ModelAction **)model_calloc(1, (maxthreads + 1) * sizeof(ModelAction *));
382 changed = changed&firsttime;
385 for (action_list_t::iterator it = list->begin(); it != list->end(); it++) {
386 ModelAction *act = *it;
387 ModelAction *lastact = last_act[id_to_int(act->get_tid())];
388 if (act->is_thread_start())
389 lastact = execution->get_thread(act)->get_creation();
390 last_act[id_to_int(act->get_tid())] = act;
391 ClockVector *cv = cvmap.get(act);
393 cv = new ClockVector(NULL, act);
396 if (lastact != NULL) {
397 merge(cv, act, lastact);
399 if (act->is_thread_join()) {
400 Thread *joinedthr = act->get_thread_operand();
401 ModelAction *finish = execution->get_last_action(joinedthr->get_id());
402 changed |= merge(cv, act, finish);
404 if (act->is_read()) {
405 changed |= processRead(act, cv);
408 /* Reset the last action array */
410 bzero(last_act, (maxthreads + 1) * sizeof(ModelAction *));
413 model_free(last_act);