11 /** @brief Constructor */
12 ModelHistory::ModelHistory() :
13 func_counter(1), /* function id starts with 1 */
17 write_history(), // snapshot data structure
18 loc_func_nodes_map() // shapshot data structure
21 void ModelHistory::enter_function(const uint32_t func_id, thread_id_t tid)
23 //model_print("thread %d entering func %d\n", tid, func_id);
24 uint id = id_to_int(tid);
25 SnapVector<func_id_list_t> * thrd_func_list = model->get_execution()->get_thrd_func_list();
26 SnapVector< SnapList<action_list_t *> *> *
27 thrd_func_act_lists = model->get_execution()->get_thrd_func_act_lists();
29 if ( thrd_func_list->size() <= id ) {
30 uint oldsize = thrd_func_list->size();
31 thrd_func_list->resize( id + 1 );
32 for (uint i = oldsize; i < id + 1; i++) {
33 new (&(*thrd_func_list)[i]) func_id_list_t();
34 // push 0 as a dummy function id to a void seg fault
35 (*thrd_func_list)[i].push_back(0);
38 thrd_func_act_lists->resize( id + 1 );
39 for (uint i = oldsize; i < id + 1; i++) {
40 (*thrd_func_act_lists)[i] = new SnapList<action_list_t *>();
44 SnapList<action_list_t *> * func_act_lists = (*thrd_func_act_lists)[id];
46 (*thrd_func_list)[id].push_back(func_id);
47 func_act_lists->push_back( new action_list_t() );
49 if ( func_nodes.size() <= func_id )
50 resize_func_nodes( func_id + 1 );
52 FuncNode * func_node = func_nodes[func_id];
53 func_node->init_predicate_tree_position(tid);
54 func_node->init_inst_act_map(tid);
57 /* @param func_id a non-zero value */
58 void ModelHistory::exit_function(const uint32_t func_id, thread_id_t tid)
60 uint32_t id = id_to_int(tid);
61 SnapVector<func_id_list_t> * thrd_func_list = model->get_execution()->get_thrd_func_list();
62 SnapVector< SnapList<action_list_t *> *> *
63 thrd_func_act_lists = model->get_execution()->get_thrd_func_act_lists();
65 SnapList<action_list_t *> * func_act_lists = (*thrd_func_act_lists)[id];
66 uint32_t last_func_id = (*thrd_func_list)[id].back();
68 if (last_func_id == func_id) {
69 FuncNode * func_node = func_nodes[func_id];
70 func_node->set_predicate_tree_position(tid, NULL);
71 func_node->reset_inst_act_map(tid);
72 //func_node->clear_read_map(tid);
74 action_list_t * curr_act_list = func_act_lists->back();
76 /* defer the processing of curr_act_list until the function has exits a few times
77 * (currently twice) so that more information can be gathered to infer nullity predicates.
79 func_node->incr_exit_count();
80 if (func_node->get_exit_count() >= 2) {
81 ModelList<action_list_t *> * action_list_buffer = func_node->get_action_list_buffer();
82 while (action_list_buffer->size() > 0) {
83 action_list_t * act_list = action_list_buffer->back();
84 action_list_buffer->pop_back();
85 func_node->update_tree(act_list);
88 func_node->update_tree(curr_act_list);
90 func_node->get_action_list_buffer()->push_front(curr_act_list);
92 (*thrd_func_list)[id].pop_back();
93 func_act_lists->pop_back();
95 model_print("trying to exit with a wrong function id\n");
96 model_print("--- last_func: %d, func_id: %d\n", last_func_id, func_id);
98 //model_print("thread %d exiting func %d\n", tid, func_id);
101 void ModelHistory::resize_func_nodes(uint32_t new_size)
103 uint32_t old_size = func_nodes.size();
105 if ( old_size < new_size )
106 func_nodes.resize(new_size);
108 for (uint32_t id = old_size; id < new_size; id++) {
109 const char * func_name = func_map_rev[id];
110 FuncNode * func_node = new FuncNode(this);
111 func_node->set_func_id(id);
112 func_node->set_func_name(func_name);
113 func_nodes[id] = func_node;
117 void ModelHistory::process_action(ModelAction *act, thread_id_t tid)
119 /* return if thread i has not entered any function or has exited
120 from all functions */
121 SnapVector<func_id_list_t> * thrd_func_list = model->get_execution()->get_thrd_func_list();
122 SnapVector< SnapList<action_list_t *> *> *
123 thrd_func_act_lists = model->get_execution()->get_thrd_func_act_lists();
125 uint32_t id = id_to_int(tid);
126 if ( thrd_func_list->size() <= id )
129 /* get the function id that thread i is currently in */
130 uint32_t func_id = (*thrd_func_list)[id].back();
131 SnapList<action_list_t *> * func_act_lists = (*thrd_func_act_lists)[id];
133 if (act->is_write()) {
134 void * location = act->get_location();
135 uint64_t value = act->get_write_value();
136 add_to_write_history(location, value);
138 /* update FuncNodes */
139 SnapList<FuncNode *> * func_nodes = loc_func_nodes_map.get(location);
140 sllnode<FuncNode *> * it = NULL;
141 if (func_nodes != NULL)
142 it = func_nodes->begin();
144 for (; it != NULL; it = it->getNext()) {
145 FuncNode * func_node = it->getVal();
146 func_node->add_to_val_loc_map(value, location);
150 /* the following does not care about actions without a position */
151 if (func_id == 0 || act->get_position() == NULL)
154 bool second_part_of_rmw = act->is_rmwc() || act->is_rmw();
156 action_list_t * curr_act_list = func_act_lists->back();
157 ASSERT(curr_act_list != NULL);
159 ModelAction * last_act = NULL;
160 if (curr_act_list->size() != 0)
161 last_act = curr_act_list->back();
163 /* skip actions that are paused by fuzzer (sequence number is 0),
164 * that are second part of a read modify write or actions with the same sequence number */
165 modelclock_t curr_seq_number = act->get_seq_number();
166 if (curr_seq_number == 0 || second_part_of_rmw ||
167 (last_act != NULL && last_act->get_seq_number() == curr_seq_number) )
170 if ( func_nodes.size() <= func_id )
171 resize_func_nodes( func_id + 1 );
173 FuncNode * func_node = func_nodes[func_id];
175 /* add to curr_inst_list */
176 curr_act_list->push_back(act);
177 func_node->add_inst(act);
179 if (act->is_read()) {
180 func_node->update_inst_act_map(tid, act);
182 Fuzzer * fuzzer = model->get_execution()->getFuzzer();
183 Predicate * selected_branch = fuzzer->get_selected_child_branch(tid);
184 func_node->set_predicate_tree_position(tid, selected_branch);
188 /* return the FuncNode given its func_id */
189 FuncNode * ModelHistory::get_func_node(uint32_t func_id)
191 if (func_nodes.size() <= func_id) // this node has not been added to func_nodes
194 return func_nodes[func_id];
197 void ModelHistory::add_to_write_history(void * location, uint64_t write_val)
199 value_set_t * write_set = write_history.get(location);
201 if (write_set == NULL) {
202 write_set = new value_set_t();
203 write_history.put(location, write_set);
206 write_set->add(write_val);
209 void ModelHistory::add_to_loc_func_nodes_map(void * location, FuncNode * node)
211 SnapList<FuncNode *> * func_node_list = loc_func_nodes_map.get(location);
212 if (func_node_list == NULL) {
213 func_node_list = new SnapList<FuncNode *>();
214 loc_func_nodes_map.put(location, func_node_list);
217 func_node_list->push_back(node);
220 /* Reallocate some snapshotted memories when new executions start */
221 void ModelHistory::set_new_exec_flag()
223 for (uint i = 1; i < func_nodes.size(); i++) {
224 FuncNode * func_node = func_nodes[i];
225 func_node->set_new_exec_flag();
229 void ModelHistory::print_write()
233 void ModelHistory::print_func_node()
235 /* function id starts with 1 */
236 for (uint32_t i = 1; i < func_nodes.size(); i++) {
237 FuncNode * func_node = func_nodes[i];
239 func_inst_list_mt * entry_insts = func_node->get_entry_insts();
240 model_print("function %s has entry actions\n", func_node->get_func_name());
242 mllnode<FuncInst*>* it;
243 for (it = entry_insts->begin();it != NULL;it=it->getNext()) {
244 FuncInst *inst = it->getVal();
245 model_print("type: %d, at: %s\n", inst->get_type(), inst->get_position());
248 func_inst_list_mt * inst_list = funcNode->get_inst_list();
250 model_print("function %s has following actions\n", funcNode->get_func_name());
251 func_inst_list_mt::iterator it;
252 for (it = inst_list->begin(); it != inst_list->end(); it++) {
253 FuncInst *inst = *it;
254 model_print("type: %d, at: %s\n", inst->get_type(), inst->get_position());