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)) {
70 //print cycles in a nice way to avoid confusion
71 //make sure thread starts appear after the create
72 if (act->is_thread_start()) {
73 ModelAction *createact=model->get_thread(act)->get_creation();
75 action_list_t *threadlist=&(*threadlists)[id_to_int(createact->get_tid())];
76 if (!threadlist->empty()) {
77 ModelAction *first=threadlist->front();
78 if (first->get_seq_number() <= createact->get_seq_number())
84 //make sure that joins appear after the thread is finished
85 if (act->is_thread_join()) {
86 int jointhread=id_to_int(act->get_thread_operand()->get_id());
87 action_list_t *threadlist=&(*threadlists)[jointhread];
88 if (!threadlist->empty()) {
89 act=threadlist->front();
96 action_list_t * SCAnalysis::generateSC(action_list_t *list) {
97 action_list_t *sclist=new action_list_t();
99 ModelAction * act=getNextAction();
102 thread_id_t tid=act->get_tid();
104 (*threadlists)[id_to_int(tid)].pop_front();
105 //add ordering constraints from this choice
106 if (updateConstraints(act)) {
107 //propagate changes if we have them
111 sclist->push_back(act);
116 void SCAnalysis::buildVectors(action_list_t *list) {
118 for (action_list_t::iterator it = list->begin(); it != list->end(); it++) {
119 ModelAction *act = *it;
120 int threadid=id_to_int(act->get_tid());
121 if (threadid > maxthreads) {
122 threadlists->resize(threadid+1);
125 (*threadlists)[threadid].push_back(act);
129 bool SCAnalysis::updateConstraints(ModelAction *act) {
131 ClockVector *actcv = cvmap->get(act);
132 for(int i=0;i<=maxthreads;i++) {
133 thread_id_t tid=int_to_id(i);
134 if (tid==act->get_tid())
137 action_list_t * list=&(*threadlists)[id_to_int(tid)];
138 for (action_list_t::iterator rit = list->begin(); rit != list->end(); rit++) {
139 ModelAction *write = *rit;
140 if (!write->is_write())
142 ClockVector *writecv = cvmap->get(write);
143 if (writecv->synchronized_since(act))
145 if (write->get_location() == act->get_location()) {
146 //write is sc after act
147 merge(writecv, write, actcv);
156 bool SCAnalysis::processRead(ModelAction *read, ClockVector *cv) {
159 /* Merge in the clock vector from the write */
160 const ModelAction *write=read->get_reads_from();
161 ClockVector *writecv=cvmap->get(write);
162 changed |= writecv == NULL || (merge(cv, read, writecv) && (*read < *write));
164 for(int i=0;i<=maxthreads;i++) {
165 thread_id_t tid=int_to_id(i);
166 if (tid==read->get_tid())
168 if (tid==write->get_tid())
170 action_list_t * list=model->get_actions_on_obj(read->get_location(), tid);
173 for (action_list_t::reverse_iterator rit = list->rbegin(); rit != list->rend(); rit++) {
174 ModelAction *write2 = *rit;
175 if (!write2->is_write())
178 ClockVector *write2cv = cvmap->get(write2);
179 if (write2cv == NULL)
182 /* write -sc-> write2 &&
185 if (write2cv->synchronized_since(write)) {
186 changed |= merge(write2cv, write2, cv);
189 //looking for earliest write2 in iteration to satisfy this
192 write2 -sc-> write */
193 if (cv->synchronized_since(write2)) {
194 changed |= writecv == NULL || merge(writecv, write, write2cv);
203 void SCAnalysis::computeCV(action_list_t *list) {
206 ModelAction **last_act=(ModelAction **)model_calloc(1,(maxthreads+1)*sizeof(ModelAction *));
208 changed=changed&firsttime;
211 for (action_list_t::iterator it = list->begin(); it != list->end(); it++) {
212 ModelAction *act = *it;
213 ModelAction *lastact = last_act[id_to_int(act->get_tid())];
214 if (act->is_thread_start())
215 lastact=model->get_thread(act)->get_creation();
216 ClockVector *lastcv=(lastact != NULL) ? cvmap->get(lastact) : NULL;
217 last_act[id_to_int(act->get_tid())]=act;
218 ClockVector *cv=cvmap->get(act);
220 cv = new ClockVector(lastcv, act);
222 } else if ( lastcv != NULL ) {
223 merge(cv, act, lastcv);
225 if (act->is_thread_join()) {
226 Thread *joinedthr = act->get_thread_operand();
227 ModelAction *finish = model->get_last_action(joinedthr->get_id());
228 ClockVector *finishcv = cvmap->get(finish);
229 changed |= (finishcv == NULL) || merge(cv, act, finishcv);
231 if (act->is_thread_join()) {
232 Thread *joinedthr = act->get_thread_operand();
233 ModelAction *finish = model->get_last_action(joinedthr->get_id());
234 ClockVector *finishcv = cvmap->get(finish);
235 changed |= (finishcv == NULL) || cv->merge(finishcv);
237 if (act->is_read()) {
238 changed|=processRead(act, cv);
241 /* Reset the last action array */
243 bzero(last_act, (maxthreads+1)*sizeof(ModelAction *));
246 model_free(last_act);