1 #include "scanalysis.h"
3 #include "threads-model.h"
4 #include "clockvector.h"
7 SCAnalysis::SCAnalysis() :
17 SCAnalysis::~SCAnalysis() {
20 void SCAnalysis::setExecution(ModelExecution * execution) {
21 this->execution=execution;
24 char * SCAnalysis::name() {
29 bool SCAnalysis::option(char *) {
33 void SCAnalysis::print_list(action_list_t *list) {
34 model_print("---------------------------------------------------------------------\n");
36 model_print("Not SC\n");
37 unsigned int hash = 0;
39 for (action_list_t::iterator it = list->begin(); it != list->end(); it++) {
40 const ModelAction *act = *it;
41 if (act->get_seq_number() > 0) {
42 if (badrfset.contains(act))
45 if (badrfset.contains(act)) {
46 model_print("Desired Rf: %u \n", badrfset.get(act)->get_seq_number());
49 hash = hash ^ (hash << 3) ^ ((*it)->hash());
51 model_print("HASH %u\n", hash);
52 model_print("---------------------------------------------------------------------\n");
55 void SCAnalysis::analyze(action_list_t *actions) {
56 action_list_t *list = generateSC(actions);
61 void SCAnalysis::check_rf(action_list_t *list) {
62 for (action_list_t::iterator it = list->begin(); it != list->end(); it++) {
63 const ModelAction *act = *it;
65 if (act->get_reads_from() != lastwrmap.get(act->get_location()))
66 badrfset.put(act, lastwrmap.get(act->get_location()));
69 lastwrmap.put(act->get_location(), act);
73 bool SCAnalysis::merge(ClockVector *cv, const ModelAction *act, const ModelAction *act2) {
74 ClockVector *cv2 = cvmap.get(act2);
77 if (cv2->getClock(act->get_tid()) >= act->get_seq_number() && act->get_seq_number() != 0) {
79 //refuse to introduce cycles into clock vectors
83 return cv->merge(cv2);
86 int SCAnalysis::getNextActions(ModelAction ** array) {
89 for (int t = 0; t <= maxthreads; t++) {
90 action_list_t *tlt = &threadlists[t];
93 ModelAction *act = tlt->front();
94 ClockVector *cv = cvmap.get(act);
96 /* Find the earliest in SC ordering */
97 for (int i = 0; i <= maxthreads; i++) {
100 action_list_t *threadlist = &threadlists[i];
101 if (threadlist->empty())
103 ModelAction *first = threadlist->front();
104 if (cv->synchronized_since(first)) {
115 for (int t = 0; t <= maxthreads; t++) {
116 action_list_t *tlt = &threadlists[t];
119 ModelAction *act = tlt->front();
120 ClockVector *cv = act->get_cv();
122 /* Find the earliest in SC ordering */
123 for (int i = 0; i <= maxthreads; i++) {
126 action_list_t *threadlist = &threadlists[i];
127 if (threadlist->empty())
129 ModelAction *first = threadlist->front();
130 if (cv->synchronized_since(first)) {
140 ASSERT(count==0 || cyclic);
145 ModelAction * SCAnalysis::pruneArray(ModelAction **array,int count) {
150 /* Choose first non-write action */
151 ModelAction *nonwrite=NULL;
152 for(int i=0;i<count;i++) {
153 if (!array[i]->is_write())
154 if (nonwrite==NULL || nonwrite->get_seq_number() > array[i]->get_seq_number())
157 if (nonwrite != NULL)
160 /* Look for non-conflicting action */
161 ModelAction *nonconflict=NULL;
162 for(int a=0;a<count;a++) {
163 ModelAction *act=array[a];
164 for (int i = 0; i <= maxthreads && act != NULL; i++) {
165 thread_id_t tid = int_to_id(i);
166 if (tid == act->get_tid())
169 action_list_t *list = &threadlists[id_to_int(tid)];
170 for (action_list_t::iterator rit = list->begin(); rit != list->end(); rit++) {
171 ModelAction *write = *rit;
172 if (!write->is_write())
174 ClockVector *writecv = cvmap.get(write);
175 if (writecv->synchronized_since(act))
177 if (write->get_location() == act->get_location()) {
178 //write is sc after act
185 if (nonconflict == NULL || nonconflict->get_seq_number() > act->get_seq_number())
192 action_list_t * SCAnalysis::generateSC(action_list_t *list) {
193 int numactions=buildVectors(list);
196 action_list_t *sclist = new action_list_t();
197 ModelAction **array = (ModelAction **)model_calloc(1, (maxthreads + 1) * sizeof(ModelAction *));
198 int * choices = (int *) model_calloc(1, sizeof(int)*numactions);
203 int numActions = getNextActions(array);
206 ModelAction * act=pruneArray(array, numActions);
208 if (currchoice < endchoice) {
209 act = array[choices[currchoice]];
210 //check whether there is still another option
211 if ((choices[currchoice]+1)<numActions)
212 lastchoice=currchoice;
216 choices[currchoice]=0;
218 lastchoice=currchoice;
222 thread_id_t tid = act->get_tid();
224 threadlists[id_to_int(tid)].pop_front();
225 //add ordering constraints from this choice
226 if (updateConstraints(act)) {
227 //propagate changes if we have them
230 if (!prevc && cyclic) {
231 model_print("ROLLBACK in SC\n");
232 //check whether we have another choice
233 if (lastchoice != -1) {
234 //have to reset everything
235 choices[lastchoice]++;
236 endchoice=lastchoice+1;
248 sclist->push_back(act);
254 int SCAnalysis::buildVectors(action_list_t *list) {
257 for (action_list_t::iterator it = list->begin(); it != list->end(); it++) {
258 ModelAction *act = *it;
260 int threadid = id_to_int(act->get_tid());
261 if (threadid > maxthreads) {
262 threadlists.resize(threadid + 1);
263 maxthreads = threadid;
265 threadlists[threadid].push_back(act);
270 void SCAnalysis::reset(action_list_t *list) {
271 for (int t = 0; t <= maxthreads; t++) {
272 action_list_t *tlt = &threadlists[t];
275 for (action_list_t::iterator it = list->begin(); it != list->end(); it++) {
276 ModelAction *act = *it;
277 delete cvmap.get(act);
278 cvmap.put(act, NULL);
284 bool SCAnalysis::updateConstraints(ModelAction *act) {
285 bool changed = false;
286 for (int i = 0; i <= maxthreads; i++) {
287 thread_id_t tid = int_to_id(i);
288 if (tid == act->get_tid())
291 action_list_t *list = &threadlists[id_to_int(tid)];
292 for (action_list_t::iterator rit = list->begin(); rit != list->end(); rit++) {
293 ModelAction *write = *rit;
294 if (!write->is_write())
296 ClockVector *writecv = cvmap.get(write);
297 if (writecv->synchronized_since(act))
299 if (write->get_location() == act->get_location()) {
300 //write is sc after act
301 merge(writecv, write, act);
310 bool SCAnalysis::processRead(ModelAction *read, ClockVector *cv) {
311 bool changed = false;
313 /* Merge in the clock vector from the write */
314 const ModelAction *write = read->get_reads_from();
315 ClockVector *writecv = cvmap.get(write);
316 changed |= merge(cv, read, write) && (*read < *write);
318 for (int i = 0; i <= maxthreads; i++) {
319 thread_id_t tid = int_to_id(i);
320 if (tid == read->get_tid())
322 if (tid == write->get_tid())
324 action_list_t *list = execution->get_actions_on_obj(read->get_location(), tid);
327 for (action_list_t::reverse_iterator rit = list->rbegin(); rit != list->rend(); rit++) {
328 ModelAction *write2 = *rit;
329 if (!write2->is_write())
332 ClockVector *write2cv = cvmap.get(write2);
333 if (write2cv == NULL)
336 /* write -sc-> write2 &&
339 if (write2cv->synchronized_since(write)) {
340 changed |= merge(write2cv, write2, read);
343 //looking for earliest write2 in iteration to satisfy this
346 write2 -sc-> write */
347 if (cv->synchronized_since(write2)) {
348 changed |= writecv == NULL || merge(writecv, write, write2);
356 void SCAnalysis::computeCV(action_list_t *list) {
358 bool firsttime = true;
359 ModelAction **last_act = (ModelAction **)model_calloc(1, (maxthreads + 1) * sizeof(ModelAction *));
361 changed = changed&firsttime;
364 for (action_list_t::iterator it = list->begin(); it != list->end(); it++) {
365 ModelAction *act = *it;
366 ModelAction *lastact = last_act[id_to_int(act->get_tid())];
367 if (act->is_thread_start())
368 lastact = execution->get_thread(act)->get_creation();
369 last_act[id_to_int(act->get_tid())] = act;
370 ClockVector *cv = cvmap.get(act);
372 cv = new ClockVector(NULL, act);
375 if (lastact != NULL) {
376 merge(cv, act, lastact);
378 if (act->is_thread_join()) {
379 Thread *joinedthr = act->get_thread_operand();
380 ModelAction *finish = execution->get_last_action(joinedthr->get_id());
381 changed |= merge(cv, act, finish);
383 if (act->is_read()) {
384 changed |= processRead(act, cv);
387 /* Reset the last action array */
389 bzero(last_act, (maxthreads + 1) * sizeof(ModelAction *));
392 model_free(last_act);