1 #include "scanalysis.h"
3 #include "threads-model.h"
4 #include "clockvector.h"
9 SCAnalysis::SCAnalysis() :
20 stats((struct sc_statistics *)model_calloc(1, sizeof(struct sc_statistics)))
24 SCAnalysis::~SCAnalysis() {
28 void SCAnalysis::setExecution(ModelExecution * execution) {
29 this->execution=execution;
32 const char * SCAnalysis::name() {
33 const char * name = "SC";
37 void SCAnalysis::finish() {
39 model_print("Elapsed time in usec %llu\n", stats->elapsedtime);
40 model_print("SC count: %u\n", stats->sccount);
41 model_print("Non-SC count: %u\n", stats->nonsccount);
44 bool SCAnalysis::option(char * opt) {
45 if (strcmp(opt, "verbose")==0) {
48 } else if (strcmp(opt, "buggy")==0) {
50 } else if (strcmp(opt, "quiet")==0) {
53 } else if (strcmp(opt, "nonsc")==0) {
56 } else if (strcmp(opt, "time")==0) {
59 } else if (strcmp(opt, "help") != 0) {
60 model_print("Unrecognized option: %s\n", opt);
63 model_print("SC Analysis options\n");
64 model_print("verbose -- print all feasible executions\n");
65 model_print("buggy -- print only buggy executions (default)\n");
66 model_print("nonsc -- print non-sc execution\n");
67 model_print("quiet -- print nothing\n");
68 model_print("time -- time execution of scanalysis\n");
74 void SCAnalysis::print_list(action_list_t *list) {
75 model_print("---------------------------------------------------------------------\n");
77 model_print("Not SC\n");
78 unsigned int hash = 0;
80 for (action_list_t::iterator it = list->begin(); it != list->end(); it++) {
81 const ModelAction *act = *it;
82 if (act->get_seq_number() > 0) {
83 if (badrfset.contains(act))
86 if (badrfset.contains(act)) {
87 model_print("Desired Rf: %u \n", badrfset.get(act)->get_seq_number());
90 hash = hash ^ (hash << 3) ^ ((*it)->hash());
92 model_print("HASH %u\n", hash);
93 model_print("---------------------------------------------------------------------\n");
96 void SCAnalysis::analyze(action_list_t *actions) {
99 struct timeval finish;
101 gettimeofday(&start, NULL);
102 action_list_t *list = generateSC(actions);
104 if (print_always || (print_buggy && execution->have_bug_reports())|| (print_nonsc && cyclic))
107 gettimeofday(&finish, NULL);
108 stats->elapsedtime+=((finish.tv_sec*1000000+finish.tv_usec)-(start.tv_sec*1000000+start.tv_usec));
113 void SCAnalysis::update_stats() {
121 void SCAnalysis::check_rf(action_list_t *list) {
122 for (action_list_t::iterator it = list->begin(); it != list->end(); it++) {
123 const ModelAction *act = *it;
124 if (act->is_read()) {
125 if (act->get_reads_from() != lastwrmap.get(act->get_location()))
126 badrfset.put(act, lastwrmap.get(act->get_location()));
129 lastwrmap.put(act->get_location(), act);
133 bool SCAnalysis::merge(ClockVector *cv, const ModelAction *act, const ModelAction *act2) {
134 ClockVector *cv2 = cvmap.get(act2);
137 if (cv2->getClock(act->get_tid()) >= act->get_seq_number() && act->get_seq_number() != 0) {
139 //refuse to introduce cycles into clock vectors
143 return cv->merge(cv2);
146 int SCAnalysis::getNextActions(ModelAction ** array) {
149 for (int t = 0; t <= maxthreads; t++) {
150 action_list_t *tlt = &threadlists[t];
153 ModelAction *act = tlt->front();
154 ClockVector *cv = cvmap.get(act);
156 /* Find the earliest in SC ordering */
157 for (int i = 0; i <= maxthreads; i++) {
160 action_list_t *threadlist = &threadlists[i];
161 if (threadlist->empty())
163 ModelAction *first = threadlist->front();
164 if (cv->synchronized_since(first)) {
175 for (int t = 0; t <= maxthreads; t++) {
176 action_list_t *tlt = &threadlists[t];
179 ModelAction *act = tlt->front();
180 ClockVector *cv = act->get_cv();
182 /* Find the earliest in SC ordering */
183 for (int i = 0; i <= maxthreads; i++) {
186 action_list_t *threadlist = &threadlists[i];
187 if (threadlist->empty())
189 ModelAction *first = threadlist->front();
190 if (cv->synchronized_since(first)) {
200 ASSERT(count==0 || cyclic);
205 ModelAction * SCAnalysis::pruneArray(ModelAction **array,int count) {
210 /* Choose first non-write action */
211 ModelAction *nonwrite=NULL;
212 for(int i=0;i<count;i++) {
213 if (!array[i]->is_write())
214 if (nonwrite==NULL || nonwrite->get_seq_number() > array[i]->get_seq_number())
217 if (nonwrite != NULL)
220 /* Look for non-conflicting action */
221 ModelAction *nonconflict=NULL;
222 for(int a=0;a<count;a++) {
223 ModelAction *act=array[a];
224 for (int i = 0; i <= maxthreads && act != NULL; i++) {
225 thread_id_t tid = int_to_id(i);
226 if (tid == act->get_tid())
229 action_list_t *list = &threadlists[id_to_int(tid)];
230 for (action_list_t::iterator rit = list->begin(); rit != list->end(); rit++) {
231 ModelAction *write = *rit;
232 if (!write->is_write())
234 ClockVector *writecv = cvmap.get(write);
235 if (writecv->synchronized_since(act))
237 if (write->get_location() == act->get_location()) {
238 //write is sc after act
245 if (nonconflict == NULL || nonconflict->get_seq_number() > act->get_seq_number())
252 action_list_t * SCAnalysis::generateSC(action_list_t *list) {
253 int numactions=buildVectors(list);
256 action_list_t *sclist = new action_list_t();
257 ModelAction **array = (ModelAction **)model_calloc(1, (maxthreads + 1) * sizeof(ModelAction *));
258 int * choices = (int *) model_calloc(1, sizeof(int)*numactions);
263 int numActions = getNextActions(array);
266 ModelAction * act=pruneArray(array, numActions);
268 if (currchoice < endchoice) {
269 act = array[choices[currchoice]];
270 //check whether there is still another option
271 if ((choices[currchoice]+1)<numActions)
272 lastchoice=currchoice;
276 choices[currchoice]=0;
278 lastchoice=currchoice;
282 thread_id_t tid = act->get_tid();
284 threadlists[id_to_int(tid)].pop_front();
285 //add ordering constraints from this choice
286 if (updateConstraints(act)) {
287 //propagate changes if we have them
290 if (!prevc && cyclic) {
291 model_print("ROLLBACK in SC\n");
292 //check whether we have another choice
293 if (lastchoice != -1) {
294 //have to reset everything
295 choices[lastchoice]++;
296 endchoice=lastchoice+1;
308 sclist->push_back(act);
314 int SCAnalysis::buildVectors(action_list_t *list) {
317 for (action_list_t::iterator it = list->begin(); it != list->end(); it++) {
318 ModelAction *act = *it;
320 int threadid = id_to_int(act->get_tid());
321 if (threadid > maxthreads) {
322 threadlists.resize(threadid + 1);
323 maxthreads = threadid;
325 threadlists[threadid].push_back(act);
330 void SCAnalysis::reset(action_list_t *list) {
331 for (int t = 0; t <= maxthreads; t++) {
332 action_list_t *tlt = &threadlists[t];
335 for (action_list_t::iterator it = list->begin(); it != list->end(); it++) {
336 ModelAction *act = *it;
337 delete cvmap.get(act);
338 cvmap.put(act, NULL);
344 bool SCAnalysis::updateConstraints(ModelAction *act) {
345 bool changed = false;
346 for (int i = 0; i <= maxthreads; i++) {
347 thread_id_t tid = int_to_id(i);
348 if (tid == act->get_tid())
351 action_list_t *list = &threadlists[id_to_int(tid)];
352 for (action_list_t::iterator rit = list->begin(); rit != list->end(); rit++) {
353 ModelAction *write = *rit;
354 if (!write->is_write())
356 ClockVector *writecv = cvmap.get(write);
357 if (writecv->synchronized_since(act))
359 if (write->get_location() == act->get_location()) {
360 //write is sc after act
361 merge(writecv, write, act);
370 bool SCAnalysis::processRead(ModelAction *read, ClockVector *cv) {
371 bool changed = false;
373 /* Merge in the clock vector from the write */
374 const ModelAction *write = read->get_reads_from();
375 ClockVector *writecv = cvmap.get(write);
376 changed |= merge(cv, read, write) && (*read < *write);
378 for (int i = 0; i <= maxthreads; i++) {
379 thread_id_t tid = int_to_id(i);
380 if (tid == read->get_tid())
382 if (tid == write->get_tid())
384 action_list_t *list = execution->get_actions_on_obj(read->get_location(), tid);
387 for (action_list_t::reverse_iterator rit = list->rbegin(); rit != list->rend(); rit++) {
388 ModelAction *write2 = *rit;
389 if (!write2->is_write())
392 ClockVector *write2cv = cvmap.get(write2);
393 if (write2cv == NULL)
396 /* write -sc-> write2 &&
399 if (write2cv->synchronized_since(write)) {
400 changed |= merge(write2cv, write2, read);
403 //looking for earliest write2 in iteration to satisfy this
406 write2 -sc-> write */
407 if (cv->synchronized_since(write2)) {
408 changed |= writecv == NULL || merge(writecv, write, write2);
416 void SCAnalysis::computeCV(action_list_t *list) {
418 bool firsttime = true;
419 ModelAction **last_act = (ModelAction **)model_calloc(1, (maxthreads + 1) * sizeof(ModelAction *));
421 changed = changed&firsttime;
424 for (action_list_t::iterator it = list->begin(); it != list->end(); it++) {
425 ModelAction *act = *it;
426 ModelAction *lastact = last_act[id_to_int(act->get_tid())];
427 if (act->is_thread_start())
428 lastact = execution->get_thread(act)->get_creation();
429 last_act[id_to_int(act->get_tid())] = act;
430 ClockVector *cv = cvmap.get(act);
432 cv = new ClockVector(NULL, act);
435 if (lastact != NULL) {
436 merge(cv, act, lastact);
438 if (act->is_thread_join()) {
439 Thread *joinedthr = act->get_thread_operand();
440 ModelAction *finish = execution->get_last_action(joinedthr->get_id());
441 changed |= merge(cv, act, finish);
443 if (act->is_read()) {
444 changed |= processRead(act, cv);
447 /* Reset the last action array */
449 bzero(last_act, (maxthreads + 1) * sizeof(ModelAction *));
452 model_free(last_act);