1 #include "scanalysis.h"
3 #include "threads-model.h"
4 #include "clockvector.h"
7 SCAnalysis::SCAnalysis(const ModelExecution *execution) :
10 cvmap=new HashTable<const ModelAction *, ClockVector *, uintptr_t, 4>();
11 cycleset=new HashTable<const ModelAction *, const ModelAction *, uintptr_t, 4>();
12 threadlists=new SnapVector<action_list_t>(1);
15 SCAnalysis::~SCAnalysis() {
21 void SCAnalysis::print_list(action_list_t *list) {
22 action_list_t::iterator it;
24 model_print("---------------------------------------------------------------------\n");
26 unsigned int hash = 0;
28 for (it = list->begin(); it != list->end(); it++) {
29 const ModelAction *act = *it;
30 if (act->get_seq_number() > 0) {
31 if (cycleset->contains(act))
35 hash = hash^(hash<<3)^((*it)->hash());
37 model_print("HASH %u\n", hash);
38 model_print("---------------------------------------------------------------------\n");
41 void SCAnalysis::analyze(action_list_t * actions) {
42 buildVectors(actions);
44 action_list_t *list=generateSC(actions);
48 bool SCAnalysis::merge(ClockVector * cv, const ModelAction * act, ClockVector *cv2) {
49 if (cv2->getClock(act->get_tid())>=act->get_seq_number() && act->get_seq_number() != 0) {
50 cycleset->put(act, act);
52 return cv->merge(cv2);
55 ModelAction * SCAnalysis::getNextAction() {
56 ModelAction *act=NULL;
57 for(int i=0;i<=maxthreads;i++) {
58 action_list_t * threadlist=&(*threadlists)[i];
59 if (threadlist->empty())
61 ModelAction *first=threadlist->front();
66 ClockVector *cv=cvmap->get(act);
67 if (cv->synchronized_since(first)) {
73 //print cycles in a nice way to avoid confusion
74 //make sure thread starts appear after the create
75 if (act->is_thread_start()) {
76 ModelAction *createact=execution->get_thread(act)->get_creation();
78 action_list_t *threadlist=&(*threadlists)[id_to_int(createact->get_tid())];
79 if (!threadlist->empty()) {
80 ModelAction *first=threadlist->front();
81 if (first->get_seq_number() <= createact->get_seq_number())
87 //make sure that joins appear after the thread is finished
88 if (act->is_thread_join()) {
89 int jointhread=id_to_int(act->get_thread_operand()->get_id());
90 action_list_t *threadlist=&(*threadlists)[jointhread];
91 if (!threadlist->empty()) {
92 act=threadlist->front();
99 action_list_t * SCAnalysis::generateSC(action_list_t *list) {
100 action_list_t *sclist=new action_list_t();
102 ModelAction * act=getNextAction();
105 thread_id_t tid=act->get_tid();
107 (*threadlists)[id_to_int(tid)].pop_front();
108 //add ordering constraints from this choice
109 if (updateConstraints(act)) {
110 //propagate changes if we have them
114 sclist->push_back(act);
119 void SCAnalysis::buildVectors(action_list_t *list) {
121 for (action_list_t::iterator it = list->begin(); it != list->end(); it++) {
122 ModelAction *act = *it;
123 int threadid=id_to_int(act->get_tid());
124 if (threadid > maxthreads) {
125 threadlists->resize(threadid+1);
128 (*threadlists)[threadid].push_back(act);
132 bool SCAnalysis::updateConstraints(ModelAction *act) {
134 ClockVector *actcv = cvmap->get(act);
135 for(int i=0;i<=maxthreads;i++) {
136 thread_id_t tid=int_to_id(i);
137 if (tid==act->get_tid())
140 action_list_t * list=&(*threadlists)[id_to_int(tid)];
141 for (action_list_t::iterator rit = list->begin(); rit != list->end(); rit++) {
142 ModelAction *write = *rit;
143 if (!write->is_write())
145 ClockVector *writecv = cvmap->get(write);
146 if (writecv->synchronized_since(act))
148 if (write->get_location() == act->get_location()) {
149 //write is sc after act
150 merge(writecv, write, actcv);
159 bool SCAnalysis::processRead(ModelAction *read, ClockVector *cv) {
162 /* Merge in the clock vector from the write */
163 const ModelAction *write=read->get_reads_from();
164 ClockVector *writecv=cvmap->get(write);
165 changed |= writecv == NULL || (merge(cv, read, writecv) && (*read < *write));
167 for(int i=0;i<=maxthreads;i++) {
168 thread_id_t tid=int_to_id(i);
169 if (tid==read->get_tid())
171 if (tid==write->get_tid())
173 action_list_t * list=execution->get_actions_on_obj(read->get_location(), tid);
176 for (action_list_t::reverse_iterator rit = list->rbegin(); rit != list->rend(); rit++) {
177 ModelAction *write2 = *rit;
178 if (!write2->is_write())
181 ClockVector *write2cv = cvmap->get(write2);
182 if (write2cv == NULL)
185 /* write -sc-> write2 &&
188 if (write2cv->synchronized_since(write)) {
189 changed |= merge(write2cv, write2, cv);
192 //looking for earliest write2 in iteration to satisfy this
195 write2 -sc-> write */
196 if (cv->synchronized_since(write2)) {
197 changed |= writecv == NULL || merge(writecv, write, write2cv);
206 void SCAnalysis::computeCV(action_list_t *list) {
209 ModelAction **last_act=(ModelAction **)model_calloc(1,(maxthreads+1)*sizeof(ModelAction *));
211 changed=changed&firsttime;
214 for (action_list_t::iterator it = list->begin(); it != list->end(); it++) {
215 ModelAction *act = *it;
216 ModelAction *lastact = last_act[id_to_int(act->get_tid())];
217 if (act->is_thread_start())
218 lastact=execution->get_thread(act)->get_creation();
219 ClockVector *lastcv=(lastact != NULL) ? cvmap->get(lastact) : NULL;
220 last_act[id_to_int(act->get_tid())]=act;
221 ClockVector *cv=cvmap->get(act);
223 cv = new ClockVector(lastcv, act);
225 } else if ( lastcv != NULL ) {
226 merge(cv, act, lastcv);
228 if (act->is_thread_join()) {
229 Thread *joinedthr = act->get_thread_operand();
230 ModelAction *finish = execution->get_last_action(joinedthr->get_id());
231 ClockVector *finishcv = cvmap->get(finish);
232 changed |= (finishcv == NULL) || merge(cv, act, finishcv);
234 if (act->is_read()) {
235 changed|=processRead(act, cv);
238 /* Reset the last action array */
240 bzero(last_act, (maxthreads+1)*sizeof(ModelAction *));
243 model_free(last_act);