dol: initial dol commit
[jump.git] / dol / src / dol / visitor / hds / lib / functional_trace.cpp
1 #include "functional_trace.h"\r
2 \r
3 functional_trace dol_functional_trace;\r
4 \r
5 functional_trace::functional_trace()\r
6 {\r
7     //DBGPRINT;\r
8     strcpy(trace_file_name, "trace1.txt");\r
9     event_num = 0;\r
10     file_index = 1;\r
11 }\r
12 \r
13 functional_trace::~functional_trace()\r
14 {\r
15     //DBGPRINT;\r
16     char temp[NAME_LENGTH];\r
17 #ifdef INCLUDE_TRACE\r
18     strcpy(trace_file_name, "trace");\r
19     sprintf(temp, "%d", file_index);\r
20     strcat(trace_file_name, temp);\r
21     strcat(trace_file_name, ".txt");\r
22     write_to_file(trace_file_name);\r
23     free_traces();\r
24     //printf("The traces in the memory have been freed\n");\r
25 #endif\r
26     //DBGPRINT;\r
27 }\r
28 \r
29 void functional_trace::add_event_node(TRACE_EVENT &trace_event)\r
30 {\r
31     Process_Trace *process_trace_ptr;\r
32     char temp[NAME_LENGTH];\r
33 \r
34     process_trace_ptr = get_process_trace(trace_event.process_name);\r
35     if (process_trace_ptr == NULL)\r
36     {\r
37         process_trace_ptr = new Process_Trace(trace_event.process_name);\r
38         _list_process_trace.push_back(process_trace_ptr);\r
39     }\r
40 \r
41     process_trace_ptr->add_entry(&trace_event);\r
42 \r
43     event_num++;\r
44     if (event_num > 8000000)\r
45     {\r
46         strcpy(trace_file_name, "trace");\r
47         sprintf(temp, "%d", file_index);\r
48         strcat(trace_file_name, temp);\r
49         strcat(trace_file_name, ".txt");\r
50         write_to_file(trace_file_name);\r
51         free_traces();\r
52         file_index++;\r
53         event_num = 0;\r
54     }\r
55 \r
56 }\r
57 \r
58 /*\r
59 void functional_trace::add_event_node(TRACE_EVENT &trace_event)\r
60 {\r
61     XMLNode process_node;\r
62     char temp[NAME_LENGTH];\r
63     process_node = xml_main_node.getChildNode(trace_event.process_name);\r
64 \r
65     if(process_node.isEmpty())\r
66     {\r
67         process_node = xml_main_node.addChild(trace_event.process_name);\r
68     }\r
69 \r
70     XMLNode event_node = process_node.addChild("event");\r
71 \r
72     event_node.addAttribute("type", trace_event_type_string[trace_event.event_type]);\r
73 \r
74     if(trace_event.event_type == COMPUTATION_EVENT)\r
75     {\r
76 \r
77         sprintf(temp, "%d", trace_event.computation_start_line);\r
78         event_node.addAttribute("start", temp);\r
79         sprintf(temp, "%d", trace_event.computation_end_line);\r
80         event_node.addAttribute("end", temp);\r
81     }\r
82     else if((trace_event.event_type == READ_EVENT) || (trace_event.event_type == WRITE_EVENT))\r
83     {\r
84         sprintf(temp, "%d", trace_event.data_num);\r
85         event_node.addAttribute("data_num", temp);\r
86         event_node.addAttribute("channel_name", trace_event.channel_name);\r
87     }\r
88 }\r
89 */\r
90 \r
91 void functional_trace::create_computation_event(const char *process_name, int start_line, int end_line)\r
92 {\r
93     TRACE_EVENT trace_event;\r
94 \r
95     trace_event.event_type = COMPUTATION_EVENT;\r
96     strcpy(trace_event.process_name, process_name);\r
97     trace_event.computation_start_line = start_line;\r
98     trace_event.computation_end_line = end_line;\r
99     strcpy(trace_event.channel_name, "");\r
100     trace_event.data_num = 0;\r
101 \r
102     add_event_node(trace_event);\r
103 }\r
104 \r
105 void functional_trace::create_read_event(const char *process_name, int data_num, const char *channel_name)\r
106 {\r
107     //DBGPRINT;\r
108     TRACE_EVENT trace_event;\r
109     trace_event.event_type = READ_EVENT;\r
110     strcpy(trace_event.process_name, process_name);\r
111     trace_event.data_num = data_num;\r
112     strcpy(trace_event.channel_name, channel_name);\r
113 \r
114     trace_event.computation_start_line = 0;\r
115     trace_event.computation_end_line = 0;\r
116 \r
117     add_event_node(trace_event);\r
118 }\r
119 \r
120 void functional_trace::create_write_event(const char *process_name, int data_num, const char *channel_name)\r
121 {\r
122     //DBGPRINT;\r
123     TRACE_EVENT trace_event;\r
124     trace_event.event_type = WRITE_EVENT;\r
125     strcpy(trace_event.process_name, process_name);\r
126     trace_event.data_num = data_num;\r
127     strcpy(trace_event.channel_name, channel_name);\r
128 \r
129     trace_event.computation_start_line = 0;\r
130     trace_event.computation_end_line = 0;\r
131 \r
132     add_event_node(trace_event);\r
133 }\r
134 \r
135 \r
136 Process_Trace *functional_trace::get_process_trace(const char *process_name)\r
137 {\r
138     Process_Trace *process_trace_ptr;\r
139 \r
140     for (_iter_process_trace = _list_process_trace.begin();\r
141          _iter_process_trace != _list_process_trace.end();\r
142          _iter_process_trace++)\r
143     {\r
144         process_trace_ptr = *_iter_process_trace;\r
145 \r
146         if (strcmp(process_trace_ptr->get_name(), process_name) == 0)\r
147         {\r
148             break;\r
149         }\r
150     }\r
151 \r
152     if (_iter_process_trace == _list_process_trace.end())\r
153     {\r
154         process_trace_ptr = NULL;\r
155     }\r
156 \r
157     return process_trace_ptr;\r
158 }\r
159 \r
160 void functional_trace::free_traces()\r
161 {\r
162     Process_Trace *process_trace_ptr;\r
163 \r
164     for (_iter_process_trace = _list_process_trace.begin();\r
165          _iter_process_trace != _list_process_trace.end();\r
166          _iter_process_trace++)\r
167     {\r
168         process_trace_ptr = *_iter_process_trace;\r
169         delete process_trace_ptr;\r
170     }\r
171     _list_process_trace.clear();\r
172 }\r
173 \r
174 int functional_trace::write_to_file(const char *trace_file_name) {\r
175     int ret = -1;\r
176     Process_Trace *process_trace_ptr;\r
177     FILE *trace_file_handle;\r
178     EVENT_ENTRY *event_entry_ptr;\r
179 \r
180     if ((trace_file_handle = fopen(trace_file_name, "w")) == NULL) {\r
181         ret = -1;\r
182         printf("Can not create file: %s\n", trace_file_name);\r
183         goto end1;\r
184     }\r
185 \r
186     for (_iter_process_trace = _list_process_trace.begin();\r
187          _iter_process_trace != _list_process_trace.end();\r
188          _iter_process_trace++) {\r
189         process_trace_ptr = *_iter_process_trace;\r
190         fprintf(trace_file_handle, "$ %s\n", process_trace_ptr->get_name());\r
191 \r
192         event_entry_ptr = process_trace_ptr->get_head_entry();\r
193         while (event_entry_ptr != NULL) {\r
194             if (event_entry_ptr->event_type == COMPUTATION_EVENT) {\r
195                 if (event_entry_ptr->end_line!=(event_entry_ptr->start_line+1))\r
196                 fprintf(trace_file_handle, "c %d %d\n",\r
197                         event_entry_ptr->start_line,\r
198                         event_entry_ptr->end_line);\r
199             }\r
200             else if (event_entry_ptr->event_type == WRITE_EVENT) {\r
201                 fprintf(trace_file_handle, "w %d %s\n",\r
202                         event_entry_ptr->data_num,\r
203                         event_entry_ptr->channel_name);\r
204             }\r
205             else if (event_entry_ptr->event_type == READ_EVENT) {\r
206                 fprintf(trace_file_handle, "r %d %s\n",\r
207                         event_entry_ptr->data_num,\r
208                         event_entry_ptr->channel_name);\r
209             }\r
210 \r
211             event_entry_ptr = event_entry_ptr->next;\r
212         }\r
213     }\r
214 \r
215     fflush(trace_file_handle);\r
216     fseek(trace_file_handle, 0, SEEK_SET);\r
217     fclose(trace_file_handle);\r
218 \r
219 end1:\r
220     return ret;\r
221 }\r
222 \r
223 Process_Trace::Process_Trace(const char *process_name)\r
224 {\r
225     strcpy(_process_name, process_name);\r
226     _head = NULL;\r
227     _tail = NULL;\r
228 }\r
229 \r
230 Process_Trace::~Process_Trace()\r
231 {\r
232     EVENT_ENTRY *temp;\r
233 \r
234     while (_head != NULL)\r
235     {\r
236         temp = _head;\r
237         _head = _head->next;\r
238         if (temp->channel_name != NULL)\r
239             delete [] temp->channel_name;\r
240         delete temp;\r
241     }\r
242 }\r
243 \r
244 const char *Process_Trace::get_name()\r
245 {\r
246     return _process_name;\r
247 }\r
248 \r
249 EVENT_ENTRY *Process_Trace::get_head_entry()\r
250 {\r
251     return _head;\r
252 }\r
253 \r
254 int Process_Trace::add_entry(TRACE_EVENT *trace_event_ptr)\r
255 {\r
256     int ret = 0;\r
257     EVENT_ENTRY *temp;\r
258     int channel_name_length;\r
259 \r
260     temp = new EVENT_ENTRY;\r
261     memset(temp, 0, sizeof(EVENT_ENTRY));\r
262 \r
263     temp->event_type = trace_event_ptr->event_type;\r
264     temp->start_line = trace_event_ptr->computation_start_line;\r
265     temp->end_line = trace_event_ptr->computation_end_line;\r
266     temp->data_num = trace_event_ptr->data_num;\r
267 \r
268     channel_name_length = strlen(trace_event_ptr->channel_name);\r
269     if (channel_name_length != 0)\r
270     {\r
271         temp->channel_name = new char[channel_name_length + 1];\r
272         strcpy(temp->channel_name, trace_event_ptr->channel_name);\r
273     }\r
274     else\r
275     {\r
276         temp->channel_name = NULL;\r
277     }\r
278 \r
279     if (_head == NULL)\r
280     {\r
281         _head = temp;\r
282         _tail = temp;\r
283         temp->next = NULL;\r
284     }\r
285     else\r
286     {\r
287         _tail->next = temp;\r
288         _tail = temp;\r
289         temp->next = NULL;\r
290     }\r
291 \r
292     return ret;\r
293 }\r
294 \r
295 \r