+ delete(stats);
+}
+
+void SCAnalysis::setExecution(ModelExecution * execution) {
+ this->execution=execution;
+}
+
+const char * SCAnalysis::name() {
+ const char * name = "SC";
+ return name;
+}
+
+void SCAnalysis::finish() {
+ if (time)
+ model_print("Elapsed time in usec %llu\n", stats->elapsedtime);
+ model_print("SC count: %u\n", stats->sccount);
+ model_print("Non-SC count: %u\n", stats->nonsccount);
+}
+
+bool SCAnalysis::option(char * opt) {
+ if (strcmp(opt, "verbose")==0) {
+ print_always=true;
+ return false;
+ } else if (strcmp(opt, "buggy")==0) {
+ return false;
+ } else if (strcmp(opt, "quiet")==0) {
+ print_buggy=false;
+ return false;
+ } else if (strcmp(opt, "nonsc")==0) {
+ print_nonsc=true;
+ return false;
+ } else if (strcmp(opt, "time")==0) {
+ time=true;
+ return false;
+ } else if (strcmp(opt, "help") != 0) {
+ model_print("Unrecognized option: %s\n", opt);
+ }
+
+ model_print("SC Analysis options\n");
+ model_print("verbose -- print all feasible executions\n");
+ model_print("buggy -- print only buggy executions (default)\n");
+ model_print("nonsc -- print non-sc execution\n");
+ model_print("quiet -- print nothing\n");
+ model_print("time -- time execution of scanalysis\n");
+ model_print("\n");
+
+ return true;
+}
+
+void SCAnalysis::print_list(action_list_t *list) {
+ model_print("---------------------------------------------------------------------\n");
+ if (cyclic)
+ model_print("Not SC\n");
+ unsigned int hash = 0;
+
+ for (action_list_t::iterator it = list->begin(); it != list->end(); it++) {
+ const ModelAction *act = *it;
+ if (act->get_seq_number() > 0) {
+ if (badrfset.contains(act))
+ model_print("BRF ");
+ act->print();
+ if (badrfset.contains(act)) {
+ model_print("Desired Rf: %u \n", badrfset.get(act)->get_seq_number());
+ }
+ }
+ hash = hash ^ (hash << 3) ^ ((*it)->hash());
+ }
+ model_print("HASH %u\n", hash);
+ model_print("---------------------------------------------------------------------\n");
+}
+
+void SCAnalysis::analyze(action_list_t *actions) {
+
+ struct timeval start;
+ struct timeval finish;
+ if (time)
+ gettimeofday(&start, NULL);
+ action_list_t *list = generateSC(actions);
+ check_rf(list);
+ if (print_always || (print_buggy && execution->have_bug_reports())|| (print_nonsc && cyclic))
+ print_list(list);
+ if (time) {
+ gettimeofday(&finish, NULL);
+ stats->elapsedtime+=((finish.tv_sec*1000000+finish.tv_usec)-(start.tv_sec*1000000+start.tv_usec));
+ }
+ update_stats();
+}
+
+void SCAnalysis::update_stats() {
+ if (cyclic) {
+ stats->nonsccount++;
+ } else {
+ stats->sccount++;
+ }
+}
+
+void SCAnalysis::check_rf(action_list_t *list) {
+ for (action_list_t::iterator it = list->begin(); it != list->end(); it++) {
+ const ModelAction *act = *it;
+ if (act->is_read()) {
+ if (act->get_reads_from() != lastwrmap.get(act->get_location()))
+ badrfset.put(act, lastwrmap.get(act->get_location()));
+ }
+ if (act->is_write())
+ lastwrmap.put(act->get_location(), act);
+ }
+}
+
+bool SCAnalysis::merge(ClockVector *cv, const ModelAction *act, const ModelAction *act2) {
+ ClockVector *cv2 = cvmap.get(act2);
+ if (cv2 == NULL)
+ return true;
+ if (cv2->getClock(act->get_tid()) >= act->get_seq_number() && act->get_seq_number() != 0) {
+ cyclic = true;
+ //refuse to introduce cycles into clock vectors
+ return false;
+ }
+
+ return cv->merge(cv2);
+}
+
+int SCAnalysis::getNextActions(ModelAction ** array) {
+ int count=0;
+
+ for (int t = 0; t <= maxthreads; t++) {
+ action_list_t *tlt = &threadlists[t];
+ if (tlt->empty())
+ continue;
+ ModelAction *act = tlt->front();
+ ClockVector *cv = cvmap.get(act);
+
+ /* Find the earliest in SC ordering */
+ for (int i = 0; i <= maxthreads; i++) {
+ if ( i == t )
+ continue;
+ action_list_t *threadlist = &threadlists[i];
+ if (threadlist->empty())
+ continue;
+ ModelAction *first = threadlist->front();
+ if (cv->synchronized_since(first)) {
+ act = NULL;
+ break;
+ }
+ }
+ if (act != NULL) {
+ array[count++]=act;
+ }
+ }
+ if (count != 0)
+ return count;
+ for (int t = 0; t <= maxthreads; t++) {
+ action_list_t *tlt = &threadlists[t];
+ if (tlt->empty())
+ continue;
+ ModelAction *act = tlt->front();
+ ClockVector *cv = act->get_cv();
+
+ /* Find the earliest in SC ordering */
+ for (int i = 0; i <= maxthreads; i++) {
+ if ( i == t )
+ continue;
+ action_list_t *threadlist = &threadlists[i];
+ if (threadlist->empty())
+ continue;
+ ModelAction *first = threadlist->front();
+ if (cv->synchronized_since(first)) {
+ act = NULL;
+ break;
+ }
+ }
+ if (act != NULL) {
+ array[count++]=act;
+ }
+ }
+
+ ASSERT(count==0 || cyclic);
+
+ return count;
+}
+
+ModelAction * SCAnalysis::pruneArray(ModelAction **array,int count) {
+ /* No choice */
+ if (count == 1)
+ return array[0];
+
+ /* Choose first non-write action */
+ ModelAction *nonwrite=NULL;
+ for(int i=0;i<count;i++) {
+ if (!array[i]->is_write())
+ if (nonwrite==NULL || nonwrite->get_seq_number() > array[i]->get_seq_number())
+ nonwrite = array[i];
+ }
+ if (nonwrite != NULL)
+ return nonwrite;
+
+ /* Look for non-conflicting action */
+ ModelAction *nonconflict=NULL;
+ for(int a=0;a<count;a++) {
+ ModelAction *act=array[a];
+ for (int i = 0; i <= maxthreads && act != NULL; i++) {
+ thread_id_t tid = int_to_id(i);
+ if (tid == act->get_tid())
+ continue;
+
+ action_list_t *list = &threadlists[id_to_int(tid)];
+ for (action_list_t::iterator rit = list->begin(); rit != list->end(); rit++) {
+ ModelAction *write = *rit;
+ if (!write->is_write())
+ continue;
+ ClockVector *writecv = cvmap.get(write);
+ if (writecv->synchronized_since(act))
+ break;
+ if (write->get_location() == act->get_location()) {
+ //write is sc after act
+ act = NULL;
+ break;
+ }
+ }
+ }
+ if (act != NULL) {
+ if (nonconflict == NULL || nonconflict->get_seq_number() > act->get_seq_number())
+ nonconflict=act;
+ }
+ }
+ return nonconflict;