1 #include "scanalysis.h"
3 #include "threads-model.h"
4 #include "clockvector.h"
9 SCAnalysis::SCAnalysis() :
19 stats((struct sc_statistics *)model_calloc(1, sizeof(struct sc_statistics)))
23 SCAnalysis::~SCAnalysis() {
27 void SCAnalysis::setExecution(ModelExecution * execution) {
28 this->execution=execution;
31 const char * SCAnalysis::name() {
32 const char * name = "SC";
36 void SCAnalysis::finish() {
38 model_print("Elapsed time in usec %llu\n", stats->elapsedtime);
41 bool SCAnalysis::option(char * opt) {
42 if (strcmp(opt, "verbose")==0) {
45 } else if (strcmp(opt, "buggy")==0) {
47 } else if (strcmp(opt, "quiet")==0) {
50 } else if (strcmp(opt, "time")==0) {
53 } else if (strcmp(opt, "help") != 0) {
54 model_print("Unrecognized option: %s\n", opt);
57 model_print("SC Analysis options\n");
58 model_print("verbose -- print all feasible executions\n");
59 model_print("buggy -- print only buggy executions (default)\n");
60 model_print("quiet -- print nothing\n");
61 model_print("time -- time execution of scanalysis\n");
67 void SCAnalysis::print_list(action_list_t *list) {
68 model_print("---------------------------------------------------------------------\n");
70 model_print("Not SC\n");
71 unsigned int hash = 0;
73 for (action_list_t::iterator it = list->begin(); it != list->end(); it++) {
74 const ModelAction *act = *it;
75 if (act->get_seq_number() > 0) {
76 if (badrfset.contains(act))
79 if (badrfset.contains(act)) {
80 model_print("Desired Rf: %u \n", badrfset.get(act)->get_seq_number());
83 hash = hash ^ (hash << 3) ^ ((*it)->hash());
85 model_print("HASH %u\n", hash);
86 model_print("---------------------------------------------------------------------\n");
89 void SCAnalysis::analyze(action_list_t *actions) {
92 struct timeval finish;
94 gettimeofday(&start, NULL);
95 action_list_t *list = generateSC(actions);
97 if (print_always || (print_buggy && execution->have_bug_reports()))
100 gettimeofday(&finish, NULL);
101 stats->elapsedtime+=((finish.tv_sec*1000000+finish.tv_usec)-(start.tv_sec*1000000+start.tv_usec));
105 void SCAnalysis::check_rf(action_list_t *list) {
106 for (action_list_t::iterator it = list->begin(); it != list->end(); it++) {
107 const ModelAction *act = *it;
108 if (act->is_read()) {
109 if (act->get_reads_from() != lastwrmap.get(act->get_location()))
110 badrfset.put(act, lastwrmap.get(act->get_location()));
113 lastwrmap.put(act->get_location(), act);
117 bool SCAnalysis::merge(ClockVector *cv, const ModelAction *act, const ModelAction *act2) {
118 ClockVector *cv2 = cvmap.get(act2);
121 if (cv2->getClock(act->get_tid()) >= act->get_seq_number() && act->get_seq_number() != 0) {
123 //refuse to introduce cycles into clock vectors
127 return cv->merge(cv2);
130 int SCAnalysis::getNextActions(ModelAction ** array) {
133 for (int t = 0; t <= maxthreads; t++) {
134 action_list_t *tlt = &threadlists[t];
137 ModelAction *act = tlt->front();
138 ClockVector *cv = cvmap.get(act);
140 /* Find the earliest in SC ordering */
141 for (int i = 0; i <= maxthreads; i++) {
144 action_list_t *threadlist = &threadlists[i];
145 if (threadlist->empty())
147 ModelAction *first = threadlist->front();
148 if (cv->synchronized_since(first)) {
159 for (int t = 0; t <= maxthreads; t++) {
160 action_list_t *tlt = &threadlists[t];
163 ModelAction *act = tlt->front();
164 ClockVector *cv = act->get_cv();
166 /* Find the earliest in SC ordering */
167 for (int i = 0; i <= maxthreads; i++) {
170 action_list_t *threadlist = &threadlists[i];
171 if (threadlist->empty())
173 ModelAction *first = threadlist->front();
174 if (cv->synchronized_since(first)) {
184 ASSERT(count==0 || cyclic);
189 ModelAction * SCAnalysis::pruneArray(ModelAction **array,int count) {
194 /* Choose first non-write action */
195 ModelAction *nonwrite=NULL;
196 for(int i=0;i<count;i++) {
197 if (!array[i]->is_write())
198 if (nonwrite==NULL || nonwrite->get_seq_number() > array[i]->get_seq_number())
201 if (nonwrite != NULL)
204 /* Look for non-conflicting action */
205 ModelAction *nonconflict=NULL;
206 for(int a=0;a<count;a++) {
207 ModelAction *act=array[a];
208 for (int i = 0; i <= maxthreads && act != NULL; i++) {
209 thread_id_t tid = int_to_id(i);
210 if (tid == act->get_tid())
213 action_list_t *list = &threadlists[id_to_int(tid)];
214 for (action_list_t::iterator rit = list->begin(); rit != list->end(); rit++) {
215 ModelAction *write = *rit;
216 if (!write->is_write())
218 ClockVector *writecv = cvmap.get(write);
219 if (writecv->synchronized_since(act))
221 if (write->get_location() == act->get_location()) {
222 //write is sc after act
229 if (nonconflict == NULL || nonconflict->get_seq_number() > act->get_seq_number())
236 action_list_t * SCAnalysis::generateSC(action_list_t *list) {
237 int numactions=buildVectors(list);
240 action_list_t *sclist = new action_list_t();
241 ModelAction **array = (ModelAction **)model_calloc(1, (maxthreads + 1) * sizeof(ModelAction *));
242 int * choices = (int *) model_calloc(1, sizeof(int)*numactions);
247 int numActions = getNextActions(array);
250 ModelAction * act=pruneArray(array, numActions);
252 if (currchoice < endchoice) {
253 act = array[choices[currchoice]];
254 //check whether there is still another option
255 if ((choices[currchoice]+1)<numActions)
256 lastchoice=currchoice;
260 choices[currchoice]=0;
262 lastchoice=currchoice;
266 thread_id_t tid = act->get_tid();
268 threadlists[id_to_int(tid)].pop_front();
269 //add ordering constraints from this choice
270 if (updateConstraints(act)) {
271 //propagate changes if we have them
274 if (!prevc && cyclic) {
275 model_print("ROLLBACK in SC\n");
276 //check whether we have another choice
277 if (lastchoice != -1) {
278 //have to reset everything
279 choices[lastchoice]++;
280 endchoice=lastchoice+1;
292 sclist->push_back(act);
298 int SCAnalysis::buildVectors(action_list_t *list) {
301 for (action_list_t::iterator it = list->begin(); it != list->end(); it++) {
302 ModelAction *act = *it;
304 int threadid = id_to_int(act->get_tid());
305 if (threadid > maxthreads) {
306 threadlists.resize(threadid + 1);
307 maxthreads = threadid;
309 threadlists[threadid].push_back(act);
314 void SCAnalysis::reset(action_list_t *list) {
315 for (int t = 0; t <= maxthreads; t++) {
316 action_list_t *tlt = &threadlists[t];
319 for (action_list_t::iterator it = list->begin(); it != list->end(); it++) {
320 ModelAction *act = *it;
321 delete cvmap.get(act);
322 cvmap.put(act, NULL);
328 bool SCAnalysis::updateConstraints(ModelAction *act) {
329 bool changed = false;
330 for (int i = 0; i <= maxthreads; i++) {
331 thread_id_t tid = int_to_id(i);
332 if (tid == act->get_tid())
335 action_list_t *list = &threadlists[id_to_int(tid)];
336 for (action_list_t::iterator rit = list->begin(); rit != list->end(); rit++) {
337 ModelAction *write = *rit;
338 if (!write->is_write())
340 ClockVector *writecv = cvmap.get(write);
341 if (writecv->synchronized_since(act))
343 if (write->get_location() == act->get_location()) {
344 //write is sc after act
345 merge(writecv, write, act);
354 bool SCAnalysis::processRead(ModelAction *read, ClockVector *cv) {
355 bool changed = false;
357 /* Merge in the clock vector from the write */
358 const ModelAction *write = read->get_reads_from();
359 ClockVector *writecv = cvmap.get(write);
360 changed |= merge(cv, read, write) && (*read < *write);
362 for (int i = 0; i <= maxthreads; i++) {
363 thread_id_t tid = int_to_id(i);
364 if (tid == read->get_tid())
366 if (tid == write->get_tid())
368 action_list_t *list = execution->get_actions_on_obj(read->get_location(), tid);
371 for (action_list_t::reverse_iterator rit = list->rbegin(); rit != list->rend(); rit++) {
372 ModelAction *write2 = *rit;
373 if (!write2->is_write())
376 ClockVector *write2cv = cvmap.get(write2);
377 if (write2cv == NULL)
380 /* write -sc-> write2 &&
383 if (write2cv->synchronized_since(write)) {
384 changed |= merge(write2cv, write2, read);
387 //looking for earliest write2 in iteration to satisfy this
390 write2 -sc-> write */
391 if (cv->synchronized_since(write2)) {
392 changed |= writecv == NULL || merge(writecv, write, write2);
400 void SCAnalysis::computeCV(action_list_t *list) {
402 bool firsttime = true;
403 ModelAction **last_act = (ModelAction **)model_calloc(1, (maxthreads + 1) * sizeof(ModelAction *));
405 changed = changed&firsttime;
408 for (action_list_t::iterator it = list->begin(); it != list->end(); it++) {
409 ModelAction *act = *it;
410 ModelAction *lastact = last_act[id_to_int(act->get_tid())];
411 if (act->is_thread_start())
412 lastact = execution->get_thread(act)->get_creation();
413 last_act[id_to_int(act->get_tid())] = act;
414 ClockVector *cv = cvmap.get(act);
416 cv = new ClockVector(NULL, act);
419 if (lastact != NULL) {
420 merge(cv, act, lastact);
422 if (act->is_thread_join()) {
423 Thread *joinedthr = act->get_thread_operand();
424 ModelAction *finish = execution->get_last_action(joinedthr->get_id());
425 changed |= merge(cv, act, finish);
427 if (act->is_read()) {
428 changed |= processRead(act, cv);
431 /* Reset the last action array */
433 bzero(last_act, (maxthreads + 1) * sizeof(ModelAction *));
436 model_free(last_act);