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
19 thrd_last_entered_func() // snapshot data structure
22 void ModelHistory::enter_function(const uint32_t func_id, thread_id_t tid)
24 //model_print("thread %d entering func %d\n", tid, func_id);
25 uint id = id_to_int(tid);
26 SnapVector<func_id_list_t> * thrd_func_list = model->get_execution()->get_thrd_func_list();
27 SnapVector< SnapList<action_list_t *> *> *
28 thrd_func_act_lists = model->get_execution()->get_thrd_func_act_lists();
30 if ( thrd_func_list->size() <= id ) {
31 uint oldsize = thrd_func_list->size();
32 thrd_func_list->resize( id + 1 );
33 thrd_func_act_lists->resize( id + 1 );
35 for (uint i = oldsize; i < id + 1; i++) {
36 new (&(*thrd_func_list)[i]) func_id_list_t();
37 // push 0 as a dummy function id to a void seg fault
38 (*thrd_func_list)[i].push_back(0);
40 (*thrd_func_act_lists)[i] = new SnapList<action_list_t *>();
44 while ( thrd_last_entered_func.size() <= id ) {
45 thrd_last_entered_func.push_back(0); // 0 is a dummy function id
48 SnapList<action_list_t *> * func_act_lists = (*thrd_func_act_lists)[id];
49 func_act_lists->push_back( new action_list_t() );
51 uint32_t last_entered_func_id = thrd_last_entered_func[id];
52 thrd_last_entered_func[id] = func_id;
53 (*thrd_func_list)[id].push_back(func_id);
55 if ( func_nodes.size() <= func_id )
56 resize_func_nodes( func_id + 1 );
58 FuncNode * func_node = func_nodes[func_id];
59 func_node->init_predicate_tree_position(tid);
60 func_node->init_inst_act_map(tid);
62 /* Add edges between FuncNodes */
63 if (last_entered_func_id != 0) {
64 FuncNode * last_func_node = func_nodes[last_entered_func_id];
65 add_edges_between(last_func_node, func_node);
69 /* @param func_id a non-zero value */
70 void ModelHistory::exit_function(const uint32_t func_id, thread_id_t tid)
72 uint32_t id = id_to_int(tid);
73 SnapVector<func_id_list_t> * thrd_func_list = model->get_execution()->get_thrd_func_list();
74 SnapVector< SnapList<action_list_t *> *> *
75 thrd_func_act_lists = model->get_execution()->get_thrd_func_act_lists();
77 SnapList<action_list_t *> * func_act_lists = (*thrd_func_act_lists)[id];
78 uint32_t last_func_id = (*thrd_func_list)[id].back();
80 if (last_func_id == func_id) {
81 FuncNode * func_node = func_nodes[func_id];
82 func_node->set_predicate_tree_position(tid, NULL);
83 func_node->reset_inst_act_map(tid);
85 action_list_t * curr_act_list = func_act_lists->back();
87 /* defer the processing of curr_act_list until the function has exits a few times
88 * (currently twice) so that more information can be gathered to infer nullity predicates.
90 func_node->incr_exit_count();
91 if (func_node->get_exit_count() >= 2) {
92 SnapList<action_list_t *> * action_list_buffer = func_node->get_action_list_buffer();
93 while (action_list_buffer->size() > 0) {
94 action_list_t * act_list = action_list_buffer->back();
95 action_list_buffer->pop_back();
96 func_node->update_tree(act_list);
99 func_node->update_tree(curr_act_list);
101 func_node->get_action_list_buffer()->push_front(curr_act_list);
103 (*thrd_func_list)[id].pop_back();
104 func_act_lists->pop_back();
106 model_print("trying to exit with a wrong function id\n");
107 model_print("--- last_func: %d, func_id: %d\n", last_func_id, func_id);
109 //model_print("thread %d exiting func %d\n", tid, func_id);
112 void ModelHistory::resize_func_nodes(uint32_t new_size)
114 uint32_t old_size = func_nodes.size();
116 if ( old_size < new_size )
117 func_nodes.resize(new_size);
119 for (uint32_t id = old_size; id < new_size; id++) {
120 const char * func_name = func_map_rev[id];
121 FuncNode * func_node = new FuncNode(this);
122 func_node->set_func_id(id);
123 func_node->set_func_name(func_name);
124 func_nodes[id] = func_node;
128 void ModelHistory::process_action(ModelAction *act, thread_id_t tid)
130 /* return if thread i has not entered any function or has exited
131 from all functions */
132 SnapVector<func_id_list_t> * thrd_func_list = model->get_execution()->get_thrd_func_list();
133 SnapVector< SnapList<action_list_t *> *> *
134 thrd_func_act_lists = model->get_execution()->get_thrd_func_act_lists();
136 uint32_t id = id_to_int(tid);
137 if ( thrd_func_list->size() <= id )
140 /* get the function id that thread i is currently in */
141 uint32_t func_id = (*thrd_func_list)[id].back();
142 SnapList<action_list_t *> * func_act_lists = (*thrd_func_act_lists)[id];
144 if (act->is_write()) {
145 void * location = act->get_location();
146 uint64_t value = act->get_write_value();
147 update_write_history(location, value);
149 /* Update FuncNodes that may read from this location */
150 SnapList<FuncNode *> * func_nodes = loc_func_nodes_map.get(location);
151 if (func_nodes != NULL) {
152 sllnode<FuncNode *> * it = func_nodes->begin();
153 for (; it != NULL; it = it->getNext()) {
154 FuncNode * func_node = it->getVal();
155 func_node->add_to_val_loc_map(value, location);
160 /* the following does not care about actions without a position */
161 if (func_id == 0 || act->get_position() == NULL)
164 bool second_part_of_rmw = act->is_rmwc() || act->is_rmw();
166 action_list_t * curr_act_list = func_act_lists->back();
167 ASSERT(curr_act_list != NULL);
169 modelclock_t curr_seq_number = act->get_seq_number();
170 /* Skip actions that are second part of a read modify write or actions with the same sequence number */
171 if (curr_act_list->size() != 0) {
172 ModelAction * last_act = curr_act_list->back();
173 if (second_part_of_rmw || last_act->get_seq_number() == curr_seq_number)
177 /* skip actions that are paused by fuzzer (sequence number is 0) */
178 if (curr_seq_number == 0)
181 FuncNode * func_node = func_nodes[func_id];
183 /* add to curr_inst_list */
184 curr_act_list->push_back(act);
185 func_node->add_inst(act);
187 if (act->is_read()) {
188 func_node->update_inst_act_map(tid, act);
190 // Update predicate tree position
191 Fuzzer * fuzzer = model->get_execution()->getFuzzer();
192 Predicate * selected_branch = fuzzer->get_selected_child_branch(tid);
193 func_node->set_predicate_tree_position(tid, selected_branch);
197 /* return the FuncNode given its func_id */
198 FuncNode * ModelHistory::get_func_node(uint32_t func_id)
200 if (func_nodes.size() <= func_id) // this node has not been added to func_nodes
203 return func_nodes[func_id];
206 void ModelHistory::update_write_history(void * location, uint64_t write_val)
208 value_set_t * write_set = write_history.get(location);
210 if (write_set == NULL) {
211 write_set = new value_set_t();
212 write_history.put(location, write_set);
215 write_set->add(write_val);
218 void ModelHistory::update_loc_func_nodes_map(void * location, FuncNode * node)
220 SnapList<FuncNode *> * func_node_list = loc_func_nodes_map.get(location);
221 if (func_node_list == NULL) {
222 func_node_list = new SnapList<FuncNode *>();
223 loc_func_nodes_map.put(location, func_node_list);
226 func_node_list->push_back(node);
229 void ModelHistory::update_loc_wr_func_nodes_map(void * location, FuncNode * node)
231 SnapList<FuncNode *> * func_node_list = loc_wr_func_nodes_map.get(location);
232 if (func_node_list == NULL) {
233 func_node_list = new SnapList<FuncNode *>();
234 loc_func_nodes_map.put(location, func_node_list);
237 func_node_list->push_back(node);
240 /* Reallocate some snapshotted memories when new executions start */
241 void ModelHistory::set_new_exec_flag()
243 for (uint i = 1; i < func_nodes.size(); i++) {
244 FuncNode * func_node = func_nodes[i];
245 func_node->set_new_exec_flag();
249 /* Add edges between FuncNodes */
250 void ModelHistory::add_edges_between(FuncNode * prev_node, FuncNode * next_node)
252 prev_node->add_out_edge(next_node);
255 void ModelHistory::dump_func_node_graph()
257 model_print("digraph func_node_graph {\n");
258 for (uint i = 1; i < func_nodes.size(); i++) {
259 FuncNode * node = func_nodes[i];
260 ModelList<FuncNode *> * out_edges = node->get_out_edges();
262 model_print("\"%p\" [label=\"%s\"]\n", node, node->get_func_name());
263 mllnode<FuncNode *> * it;
264 for (it = out_edges->begin(); it != NULL; it = it->getNext()) {
265 FuncNode * other = it->getVal();
266 model_print("\"%p\" -> \"%p\"\n", node, other);
272 void ModelHistory::print_func_node()
274 /* function id starts with 1 */
275 for (uint32_t i = 1; i < func_nodes.size(); i++) {
276 FuncNode * func_node = func_nodes[i];
278 func_inst_list_mt * entry_insts = func_node->get_entry_insts();
279 model_print("function %s has entry actions\n", func_node->get_func_name());
281 mllnode<FuncInst*>* it;
282 for (it = entry_insts->begin();it != NULL;it=it->getNext()) {
283 FuncInst *inst = it->getVal();
284 model_print("type: %d, at: %s\n", inst->get_type(), inst->get_position());