1 #include "scanalysis.h"
3 #include "threads-model.h"
4 #include "clockvector.h"
6 SCAnalysis::SCAnalysis() {
7 cvmap=new HashTable<const ModelAction *, ClockVector *, uintptr_t, 4>();
8 cycleset=new HashTable<const ModelAction *, const ModelAction *, uintptr_t, 4>();
9 threadlists=new SnapVector<action_list_t>(1);
12 SCAnalysis::~SCAnalysis() {
18 void SCAnalysis::print_list(action_list_t *list) {
19 action_list_t::iterator it;
21 model_print("---------------------------------------------------------------------\n");
23 unsigned int hash = 0;
25 for (it = list->begin(); it != list->end(); it++) {
26 const ModelAction *act = *it;
27 if (act->get_seq_number() > 0) {
28 if (cycleset->contains(act))
32 hash = hash^(hash<<3)^((*it)->hash());
34 model_print("HASH %u\n", hash);
35 model_print("---------------------------------------------------------------------\n");
38 void SCAnalysis::analyze(action_list_t * actions) {
39 buildVectors(actions);
41 action_list_t *list=generateSC(actions);
45 bool SCAnalysis::merge(ClockVector * cv, const ModelAction * act, ClockVector *cv2) {
46 if (cv2->getClock(act->get_tid())>=act->get_seq_number() && act->get_seq_number() != 0) {
47 cycleset->put(act, act);
49 return cv->merge(cv2);
52 ModelAction * SCAnalysis::getNextAction() {
53 ModelAction *act=NULL;
54 for(int i=0;i<=maxthreads;i++) {
55 action_list_t * threadlist=&(*threadlists)[i];
56 if (threadlist->empty())
58 ModelAction *first=threadlist->front();
63 ClockVector *cv=cvmap->get(act);
64 if (cv->synchronized_since(first)) {
71 action_list_t * SCAnalysis::generateSC(action_list_t *list) {
72 action_list_t *sclist=new action_list_t();
74 ModelAction * act=getNextAction();
77 thread_id_t tid=act->get_tid();
79 (*threadlists)[id_to_int(tid)].pop_front();
80 //add ordering constraints from this choice
81 if (updateConstraints(act)) {
82 //propagate changes if we have them
86 sclist->push_back(act);
91 void SCAnalysis::buildVectors(action_list_t *list) {
93 for (action_list_t::iterator it = list->begin(); it != list->end(); it++) {
94 ModelAction *act = *it;
95 int threadid=id_to_int(act->get_tid());
96 if (threadid > maxthreads) {
97 threadlists->resize(threadid+1);
100 (*threadlists)[threadid].push_back(act);
104 bool SCAnalysis::updateConstraints(ModelAction *act) {
106 ClockVector *actcv = cvmap->get(act);
107 for(int i=0;i<=maxthreads;i++) {
108 thread_id_t tid=int_to_id(i);
109 if (tid==act->get_tid())
112 action_list_t * list=&(*threadlists)[id_to_int(tid)];
113 for (action_list_t::iterator rit = list->begin(); rit != list->end(); rit++) {
114 ModelAction *write = *rit;
115 if (!write->is_write())
117 ClockVector *writecv = cvmap->get(write);
118 if (writecv->synchronized_since(act))
120 if (write->get_location() == act->get_location()) {
121 //write is sc after act
122 merge(writecv, write, actcv);
131 bool SCAnalysis::processRead(ModelAction *read, ClockVector *cv) {
134 /* Merge in the clock vector from the write */
135 const ModelAction *write=read->get_reads_from();
136 ClockVector *writecv=cvmap->get(write);
137 changed|= ( writecv == NULL || merge(cv, read, writecv) && (*read < *write));
139 for(int i=0;i<=maxthreads;i++) {
140 thread_id_t tid=int_to_id(i);
141 if (tid==read->get_tid())
143 if (tid==write->get_tid())
145 action_list_t * list=model->get_actions_on_obj(read->get_location(), tid);
148 for (action_list_t::reverse_iterator rit = list->rbegin(); rit != list->rend(); rit++) {
149 ModelAction *write2 = *rit;
150 if (!write2->is_write())
153 ClockVector *write2cv = cvmap->get(write2);
154 if (write2cv == NULL)
157 /* write -sc-> write2 &&
160 if (write2cv->synchronized_since(write)) {
161 changed |= merge(write2cv, write2, cv);
164 //looking for earliest write2 in iteration to satisfy this
167 write2 -sc-> write */
168 if (cv->synchronized_since(write2)) {
169 changed |= writecv == NULL || merge(writecv, write, write2cv);
178 void SCAnalysis::computeCV(action_list_t *list) {
181 ModelAction **last_act=(ModelAction **)model_calloc(1,(maxthreads+1)*sizeof(ModelAction *));
183 changed=changed&firsttime;
186 for (action_list_t::iterator it = list->begin(); it != list->end(); it++) {
187 ModelAction *act = *it;
188 ModelAction *lastact = last_act[id_to_int(act->get_tid())];
189 if (act->is_thread_start())
190 lastact=model->get_thread(act)->get_creation();
191 ClockVector *lastcv=(lastact != NULL) ? cvmap->get(lastact) : NULL;
192 last_act[id_to_int(act->get_tid())]=act;
193 ClockVector *cv=cvmap->get(act);
195 cv = new ClockVector(lastcv, act);
197 } else if ( lastcv != NULL ) {
198 merge(cv, act, lastcv);
200 if (act->is_thread_join()) {
201 Thread *joinedthr = act->get_thread_operand();
202 ModelAction *finish = model->get_last_action(joinedthr->get_id());
203 ClockVector *finishcv = cvmap->get(finish);
204 changed |= (finishcv == NULL) || merge(cv, act, finishcv);
206 if (act->is_thread_join()) {
207 Thread *joinedthr = act->get_thread_operand();
208 ModelAction *finish = model->get_last_action(joinedthr->get_id());
209 ClockVector *finishcv = cvmap->get(finish);
210 changed |= (finishcv == NULL) || cv->merge(finishcv);
212 if (act->is_read()) {
213 changed|=processRead(act, cv);
216 /* Reset the last action array */
218 bzero(last_act, (maxthreads+1)*sizeof(ModelAction *));
221 model_free(last_act);