7 #include "concretepredicate.h"
11 #include "execution.h"
12 #include "newfuzzer.h"
14 /** @brief Constructor */
15 ModelHistory::ModelHistory() :
16 func_counter(1), /* function id starts with 1 */
22 /* The following are snapshot data structures */
23 write_history = new HashTable<void *, value_set_t *, uintptr_t, 0>();
24 loc_rd_func_nodes_map = new HashTable<void *, SnapVector<FuncNode *> *, uintptr_t, 0>();
25 loc_wr_func_nodes_map = new HashTable<void *, SnapVector<FuncNode *> *, uintptr_t, 0>();
26 loc_waiting_writes_map = new HashTable<void *, SnapVector<ConcretePredicate *> *, uintptr_t, 0>();
27 thrd_func_list = new SnapVector<func_id_list_t>();
28 thrd_last_entered_func = new SnapVector<uint32_t>();
29 thrd_waiting_write = new SnapVector<ConcretePredicate *>();
30 thrd_wait_obj = new SnapVector<WaitObj *>();
31 func_inst_act_maps = new HashTable<uint32_t, SnapVector<inst_act_map_t *> *, int, 0>(128);
34 ModelHistory::~ModelHistory()
36 // TODO: complete deconstructor; maybe not needed
37 for (uint i = 0;i < thrd_wait_obj->size();i++)
38 delete (*thrd_wait_obj)[i];
41 void ModelHistory::enter_function(const uint32_t func_id, thread_id_t tid)
43 //model_print("thread %d entering func %d\n", tid, func_id);
44 uint id = id_to_int(tid);
46 if ( thrd_func_list->size() <= id ) {
47 uint oldsize = thrd_func_list->size();
48 thrd_func_list->resize( id + 1 );
50 for (uint i = oldsize;i < id + 1;i++) {
51 // push 0 as a dummy function id to a void seg fault
52 new (&(*thrd_func_list)[i]) func_id_list_t();
53 (*thrd_func_list)[i].push_back(0);
54 thrd_last_entered_func->push_back(0);
58 uint32_t last_entered_func_id = (*thrd_last_entered_func)[id];
59 (*thrd_last_entered_func)[id] = func_id;
60 (*thrd_func_list)[id].push_back(func_id);
62 if ( func_nodes.size() <= func_id )
63 resize_func_nodes( func_id + 1 );
65 FuncNode * func_node = func_nodes[func_id];
66 func_node->function_entry_handler(tid);
68 /* Add edges between FuncNodes */
69 if (last_entered_func_id != 0) {
70 FuncNode * last_func_node = func_nodes[last_entered_func_id];
71 last_func_node->add_out_edge(func_node);
74 /* Monitor the statuses of threads waiting for tid */
75 // monitor_waiting_thread(func_id, tid);
78 /* @param func_id a non-zero value */
79 void ModelHistory::exit_function(const uint32_t func_id, thread_id_t tid)
81 uint32_t id = id_to_int(tid);
82 uint32_t last_func_id = (*thrd_func_list)[id].back();
84 if (last_func_id == func_id) {
85 FuncNode * func_node = func_nodes[func_id];
86 func_node->function_exit_handler(tid);
88 (*thrd_func_list)[id].pop_back();
90 model_print("trying to exit with a wrong function id\n");
91 model_print("--- last_func: %d, func_id: %d\n", last_func_id, func_id);
93 //model_print("thread %d exiting func %d\n", tid, func_id);
96 void ModelHistory::resize_func_nodes(uint32_t new_size)
98 uint32_t old_size = func_nodes.size();
100 if ( old_size < new_size )
101 func_nodes.resize(new_size);
103 for (uint32_t id = old_size;id < new_size;id++) {
104 const char * func_name = func_map_rev[id];
105 FuncNode * func_node = new FuncNode(this);
106 func_node->set_func_id(id);
107 func_node->set_func_name(func_name);
108 func_nodes[id] = func_node;
112 void ModelHistory::process_action(ModelAction *act, thread_id_t tid)
114 uint32_t thread_id = id_to_int(tid);
115 /* Return if thread tid has not entered any function that contains atomics */
116 if ( thrd_func_list->size() <= thread_id )
119 /* Monitor the statuses of threads waiting for tid */
120 // monitor_waiting_thread_counter(tid);
122 /* Every write action should be processed, including
123 * nonatomic writes (which have no position) */
124 if (act->is_write()) {
125 void * location = act->get_location();
126 uint64_t value = act->get_write_value();
127 update_write_history(location, value);
129 /* Notify FuncNodes that may read from this location */
130 SnapVector<FuncNode *> * func_node_list = getRdFuncNodes(location);
131 for (uint i = 0;i < func_node_list->size();i++) {
132 FuncNode * func_node = (*func_node_list)[i];
133 func_node->add_to_val_loc_map(value, location);
136 // check_waiting_write(act);
139 uint32_t func_id = (*thrd_func_list)[thread_id].back();
141 /* The following does not care about actions that are not inside
142 * any function that contains atomics or actions without a position */
143 if (func_id == 0 || act->get_position() == NULL)
146 if (skip_action(act))
149 FuncNode * func_node = func_nodes[func_id];
150 func_node->add_inst(act);
152 if (act->is_read()) {
153 func_node->update_inst_act_map(tid, act);
155 // Fuzzer * fuzzer = model->get_execution()->getFuzzer();
156 // Predicate * selected_branch = ((NewFuzzer *)fuzzer)->get_selected_child_branch(tid);
157 // func_node->set_predicate_tree_position(tid, selected_branch);
160 if (act->is_write()) {
161 Predicate * curr_pred = func_node->get_predicate_tree_position(tid);
162 FuncInst * curr_inst = func_node->get_inst(act);
166 curr_pred = curr_pred->follow_write_child(curr_inst);
168 func_node->set_predicate_tree_position(tid, curr_pred);
172 func_node->update_tree(act);
176 /* Return the FuncNode given its func_id */
177 FuncNode * ModelHistory::get_func_node(uint32_t func_id)
182 // This node has not been added to func_nodes
183 if (func_nodes.size() <= func_id)
186 return func_nodes[func_id];
189 /* Return the current FuncNode when given a thread id */
190 FuncNode * ModelHistory::get_curr_func_node(thread_id_t tid)
192 int thread_id = id_to_int(tid);
193 uint32_t func_id = (*thrd_func_list)[thread_id].back();
196 return func_nodes[func_id];
202 void ModelHistory::update_write_history(void * location, uint64_t write_val)
204 value_set_t * write_set = write_history->get(location);
206 if (write_set == NULL) {
207 write_set = new value_set_t();
208 write_history->put(location, write_set);
211 write_set->add(write_val);
214 void ModelHistory::update_loc_rd_func_nodes_map(void * location, FuncNode * node)
216 SnapVector<FuncNode *> * func_node_list = getRdFuncNodes(location);
217 func_node_list->push_back(node);
220 void ModelHistory::update_loc_wr_func_nodes_map(void * location, FuncNode * node)
222 SnapVector<FuncNode *> * func_node_list = getWrFuncNodes(location);
223 func_node_list->push_back(node);
226 SnapVector<FuncNode *> * ModelHistory::getRdFuncNodes(void * location)
228 SnapVector<FuncNode *> * func_node_list = loc_rd_func_nodes_map->get(location);
229 if (func_node_list == NULL) {
230 func_node_list = new SnapVector<FuncNode *>();
231 loc_rd_func_nodes_map->put(location, func_node_list);
234 return func_node_list;
237 SnapVector<FuncNode *> * ModelHistory::getWrFuncNodes(void * location)
239 SnapVector<FuncNode *> * func_node_list = loc_wr_func_nodes_map->get(location);
240 if (func_node_list == NULL) {
241 func_node_list = new SnapVector<FuncNode *>();
242 loc_wr_func_nodes_map->put(location, func_node_list);
245 return func_node_list;
248 /* When a thread is paused by Fuzzer, keep track of the condition it is waiting for */
249 void ModelHistory::add_waiting_write(ConcretePredicate * concrete)
251 void * location = concrete->get_location();
252 SnapVector<ConcretePredicate *> * waiting_conditions = loc_waiting_writes_map->get(location);
253 if (waiting_conditions == NULL) {
254 waiting_conditions = new SnapVector<ConcretePredicate *>();
255 loc_waiting_writes_map->put(location, waiting_conditions);
258 /* waiting_conditions should not have duplications */
259 waiting_conditions->push_back(concrete);
261 int thread_id = id_to_int(concrete->get_tid());
262 if (thrd_waiting_write->size() <= (uint) thread_id) {
263 thrd_waiting_write->resize(thread_id + 1);
266 (*thrd_waiting_write)[thread_id] = concrete;
269 void ModelHistory::remove_waiting_write(thread_id_t tid)
271 ConcretePredicate * concrete = (*thrd_waiting_write)[ id_to_int(tid) ];
272 void * location = concrete->get_location();
273 SnapVector<ConcretePredicate *> * concrete_preds = loc_waiting_writes_map->get(location);
275 /* Linear search should be fine because presumably not many ConcretePredicates
276 * are at the same memory location */
277 for (uint i = 0;i < concrete_preds->size();i++) {
278 ConcretePredicate * current = (*concrete_preds)[i];
279 if (concrete == current) {
280 (*concrete_preds)[i] = concrete_preds->back();
281 concrete_preds->pop_back();
286 int thread_id = id_to_int( concrete->get_tid() );
287 (*thrd_waiting_write)[thread_id] = NULL;
291 /* Check if any other thread is waiting for this write action. If so, "notify" them */
292 void ModelHistory::check_waiting_write(ModelAction * write_act)
294 void * location = write_act->get_location();
295 uint64_t value = write_act->get_write_value();
296 SnapVector<ConcretePredicate *> * concrete_preds = loc_waiting_writes_map->get(location);
297 if (concrete_preds == NULL)
301 while (index < concrete_preds->size()) {
302 ConcretePredicate * concrete_pred = (*concrete_preds)[index];
303 SnapVector<struct concrete_pred_expr> * concrete_exprs = concrete_pred->getExpressions();
304 bool satisfy_predicate = true;
305 /* Check if the written value satisfies every predicate expression */
306 for (uint i = 0;i < concrete_exprs->size();i++) {
307 struct concrete_pred_expr concrete = (*concrete_exprs)[i];
308 bool equality = false;
309 switch (concrete.token) {
311 equality = (value == concrete.value);
314 equality = ((void*)value == NULL);
317 model_print("unknown predicate token");
321 if (equality != concrete.equality) {
322 satisfy_predicate = false;
327 if (satisfy_predicate) {
328 /* Wake up threads */
329 thread_id_t tid = concrete_pred->get_tid();
330 Thread * thread = model->get_thread(tid);
332 //model_print("** thread %d is woken up\n", thread->get_id());
333 ((NewFuzzer *)model->get_execution()->getFuzzer())->notify_paused_thread(thread);
340 WaitObj * ModelHistory::getWaitObj(thread_id_t tid)
342 int thread_id = id_to_int(tid);
343 int old_size = thrd_wait_obj->size();
344 if (old_size <= thread_id) {
345 thrd_wait_obj->resize(thread_id + 1);
346 for (int i = old_size;i < thread_id + 1;i++) {
347 (*thrd_wait_obj)[i] = new WaitObj( int_to_id(i) );
351 return (*thrd_wait_obj)[thread_id];
354 void ModelHistory::add_waiting_thread(thread_id_t self_id,
355 thread_id_t waiting_for_id, FuncNode * target_node, int dist)
357 WaitObj * self_wait_obj = getWaitObj(self_id);
358 self_wait_obj->add_waiting_for(waiting_for_id, target_node, dist);
360 /* Update waited-by relation */
361 WaitObj * other_wait_obj = getWaitObj(waiting_for_id);
362 other_wait_obj->add_waited_by(self_id);
365 /* Thread tid is woken up (or notified), so it is not waiting for others anymore */
366 void ModelHistory::remove_waiting_thread(thread_id_t tid)
368 WaitObj * self_wait_obj = getWaitObj(tid);
369 thrd_id_set_t * waiting_for = self_wait_obj->getWaitingFor();
371 /* Remove tid from waited_by's */
372 thrd_id_set_iter * iter = waiting_for->iterator();
373 while (iter->hasNext()) {
374 thread_id_t other_id = iter->next();
375 WaitObj * other_wait_obj = getWaitObj(other_id);
376 other_wait_obj->remove_waited_by(tid);
379 self_wait_obj->clear_waiting_for();
383 void ModelHistory::stop_waiting_for_node(thread_id_t self_id,
384 thread_id_t waiting_for_id, FuncNode * target_node)
386 WaitObj * self_wait_obj = getWaitObj(self_id);
387 bool thread_removed = self_wait_obj->remove_waiting_for_node(waiting_for_id, target_node);
389 // model_print("\t%d gives up %d on node %d\n", self_id, waiting_for_id, target_node->get_func_id());
391 /* If thread self_id is not waiting for waiting_for_id anymore */
392 if (thread_removed) {
393 WaitObj * other_wait_obj = getWaitObj(waiting_for_id);
394 other_wait_obj->remove_waited_by(self_id);
396 thrd_id_set_t * self_waiting_for = self_wait_obj->getWaitingFor();
397 if ( self_waiting_for->isEmpty() ) {
398 // model_print("\tthread %d waits for nobody, wake up\n", self_id);
399 ModelExecution * execution = model->get_execution();
400 Thread * thread = execution->get_thread(self_id);
401 ((NewFuzzer *)execution->getFuzzer())->notify_paused_thread(thread);
406 SnapVector<inst_act_map_t *> * ModelHistory::getThrdInstActMap(uint32_t func_id)
408 ASSERT(func_id != 0);
410 SnapVector<inst_act_map_t *> * maps = func_inst_act_maps->get(func_id);
412 maps = new SnapVector<inst_act_map_t *>();
413 func_inst_act_maps->put(func_id, maps);
419 bool ModelHistory::skip_action(ModelAction * act)
421 bool second_part_of_rmw = act->is_rmwc() || act->is_rmw();
422 modelclock_t curr_seq_number = act->get_seq_number();
424 /* Skip actions that are second part of a read modify write */
425 if (second_part_of_rmw)
428 /* Skip actions with the same sequence number */
429 if (last_action != NULL) {
430 if (last_action->get_seq_number() == curr_seq_number)
434 /* Skip actions that are paused by fuzzer (sequence number is 0) */
435 if (curr_seq_number == 0)
441 /* Monitor thread tid and decide whether other threads (that are waiting for tid)
442 * should keep waiting for this thread or not. Shall only be called when a thread
445 * Heuristics: If the distance from the current FuncNode to some target node
446 * ever increases, stop waiting for this thread on this target node.
448 void ModelHistory::monitor_waiting_thread(uint32_t func_id, thread_id_t tid)
450 WaitObj * wait_obj = getWaitObj(tid);
451 thrd_id_set_t * waited_by = wait_obj->getWaitedBy();
452 FuncNode * curr_node = func_nodes[func_id];
454 /* For each thread waiting for tid */
455 thrd_id_set_iter * tid_iter = waited_by->iterator();
456 while (tid_iter->hasNext()) {
457 thread_id_t waited_by_id = tid_iter->next();
458 WaitObj * other_wait_obj = getWaitObj(waited_by_id);
460 node_set_t * target_nodes = other_wait_obj->getTargetNodes(tid);
461 node_set_iter * node_iter = target_nodes->iterator();
462 while (node_iter->hasNext()) {
463 FuncNode * target = node_iter->next();
464 int old_dist = other_wait_obj->lookup_dist(tid, target);
465 int new_dist = curr_node->compute_distance(target, old_dist);
467 if (new_dist == -1) {
468 stop_waiting_for_node(waited_by_id, tid, target);
478 void ModelHistory::monitor_waiting_thread_counter(thread_id_t tid)
480 WaitObj * wait_obj = getWaitObj(tid);
481 thrd_id_set_t * waited_by = wait_obj->getWaitedBy();
483 // Thread tid has taken an action, update the counter for threads waiting for tid
484 thrd_id_set_iter * tid_iter = waited_by->iterator();
485 while (tid_iter->hasNext()) {
486 thread_id_t waited_by_id = tid_iter->next();
487 WaitObj * other_wait_obj = getWaitObj(waited_by_id);
489 bool expire = other_wait_obj->incr_counter(tid);
491 // model_print("thread %d stops waiting for thread %d\n", waited_by_id, tid);
492 wait_obj->remove_waited_by(waited_by_id);
493 other_wait_obj->remove_waiting_for(tid);
495 thrd_id_set_t * other_waiting_for = other_wait_obj->getWaitingFor();
496 if ( other_waiting_for->isEmpty() ) {
497 // model_print("\tthread %d waits for nobody, wake up\n", self_id);
498 ModelExecution * execution = model->get_execution();
499 Thread * thread = execution->get_thread(waited_by_id);
500 ((NewFuzzer *)execution->getFuzzer())->notify_paused_thread(thread);
508 /* Reallocate some snapshotted memories when new executions start */
509 void ModelHistory::set_new_exec_flag()
511 for (uint i = 1;i < func_nodes.size();i++) {
512 FuncNode * func_node = func_nodes[i];
513 func_node->set_new_exec_flag();
517 void ModelHistory::dump_func_node_graph()
519 model_print("digraph func_node_graph {\n");
520 for (uint i = 1;i < func_nodes.size();i++) {
521 FuncNode * node = func_nodes[i];
522 ModelList<FuncNode *> * out_edges = node->get_out_edges();
524 model_print("\"%p\" [label=\"%s\"]\n", node, node->get_func_name());
525 mllnode<FuncNode *> * it;
526 for (it = out_edges->begin();it != NULL;it = it->getNext()) {
527 FuncNode * other = it->getVal();
528 model_print("\"%p\" -> \"%p\"\n", node, other);
534 void ModelHistory::print_func_node()
536 /* function id starts with 1 */
537 for (uint32_t i = 1;i < func_nodes.size();i++) {
538 FuncNode * func_node = func_nodes[i];
539 func_node->print_predicate_tree();
542 func_inst_list_mt * entry_insts = func_node->get_entry_insts();
543 model_print("function %s has entry actions\n", func_node->get_func_name());
545 mllnode<FuncInst*>* it;
546 for (it = entry_insts->begin();it != NULL;it=it->getNext()) {
547 FuncInst *inst = it->getVal();
548 model_print("type: %d, at: %s\n", inst->get_type(), inst->get_position());
554 void ModelHistory::print_waiting_threads()
556 ModelExecution * execution = model->get_execution();
557 for (unsigned int i = 0;i < execution->get_num_threads();i++) {
558 thread_id_t tid = int_to_id(i);
559 WaitObj * wait_obj = getWaitObj(tid);
560 wait_obj->print_waiting_for();
563 for (unsigned int i = 0;i < execution->get_num_threads();i++) {
564 thread_id_t tid = int_to_id(i);
565 WaitObj * wait_obj = getWaitObj(tid);
566 wait_obj->print_waited_by();