dol: initial dol commit
[jump.git] / dol / src / dol / visitor / hds / lib / ProcessWrapper.cpp
1 #include "ProcessWrapper.h"\r
2 #include "dolSupport.h"\r
3 \r
4 /**\r
5  *\r
6  */\r
7 ProcessWrapper::ProcessWrapper(\r
8         sc_module_name name = sc_gen_unique_name("process"))\r
9         : sc_module(name) {\r
10 \r
11     _name = new char[strlen(name) + 1];\r
12     strcpy(_name, name);\r
13 \r
14     _isDetached = false;\r
15     for (int i = 0; i < 4; i++) {\r
16         _iteratorIndex[i] = getIndex(_name, "_", i);\r
17     }\r
18 }\r
19 \r
20 /**\r
21  *\r
22  */\r
23 ProcessWrapper::~ProcessWrapper() {\r
24     if (_name) {\r
25         delete _name;\r
26     }\r
27 }\r
28 \r
29 /**\r
30  *\r
31  */\r
32 void ProcessWrapper::initialize() {\r
33     _process.init(&_process);\r
34 }\r
35 \r
36 /**\r
37  *\r
38  */\r
39 int ProcessWrapper::fire()\r
40 {\r
41     int returnValue;\r
42 \r
43     #ifdef INCLUDE_TRACE\r
44     start_line = -1;\r
45     #endif\r
46 \r
47     #ifdef INCLUDE_PERFORMANCE\r
48     start_line = -1;\r
49     get_current_time(&start_time);\r
50     #endif\r
51 \r
52     returnValue = _process.fire(&_process);\r
53 \r
54     #ifdef INCLUDE_TRACE\r
55     end_line = -1;\r
56     dol_functional_trace.create_computation_event(basename(),\r
57             start_line, end_line);\r
58     #endif\r
59 \r
60     #ifdef INCLUDE_PERFORMANCE\r
61     get_current_time(&end_time);\r
62     end_line = -1;\r
63     performance_extraction.add_computation_performance(basename(),\r
64             start_line, end_line, &start_time, &end_time);\r
65     #endif\r
66 \r
67     return returnValue;\r
68 }\r
69 \r
70 \r
71 /**\r
72  *\r
73  */\r
74 void ProcessWrapper::detach() {\r
75     _isDetached = true;\r
76 }\r
77 \r
78 \r
79 /**\r
80  * Gets an index of a string, where the index must be separated by\r
81  * a character specified in tokens.\r
82  * Returns -1, when an error occurs.\r
83  *\r
84  * Example:\r
85  * getIndex("name_1_2", "_", 0) will return 1.\r
86  * getIndex("name_1_2", "_", 1) will return 2.\r
87  *\r
88  * @param string string to parse\r
89  * @param tokens delimiter of indices\r
90  * @param indexNumber position of index (starting at 0)\r
91  */\r
92 int ProcessWrapper::getIndex(const char* string, char* tokens,\r
93         int indexNumber) const {\r
94     char* string_copy;\r
95     char* token_pointer;\r
96     int index = 0;\r
97 \r
98     string_copy = (char*) malloc(sizeof(char) * (strlen(string) + 1));\r
99     if (!string_copy) {\r
100         fprintf(stderr, "getIndex(): could not allocate memory.\n");\r
101         return -1;\r
102     }\r
103 \r
104     strcpy(string_copy, string);\r
105 \r
106     token_pointer = strtok(string_copy, tokens);\r
107     do {\r
108         token_pointer = strtok(NULL, tokens);\r
109         index++;\r
110     } while (index <= indexNumber && token_pointer != 0);\r
111 \r
112     if (token_pointer) {\r
113         index = atoi(token_pointer);\r
114         free(string_copy);\r
115         return index;\r
116     }\r
117 \r
118     return -1;\r
119 }\r
120 \r
121 \r
122 /**\r
123  * Get the index of this process.\r
124  * @param indexNumber position of index (starting at 0)\r
125  */\r
126 int ProcessWrapper::getIndex(unsigned indexNumber) const {\r
127     if (indexNumber < 4) {\r
128         return _iteratorIndex[indexNumber];\r
129     }\r
130     return -1;\r
131 }\r
132 \r
133 \r
134 /**\r
135  * Get the name of this process.\r
136  */\r
137 char* ProcessWrapper::getName() const {\r
138     return _name;\r
139 }\r
140 \r
141 \r
142 /**\r
143  *\r
144  */\r
145 #ifdef INCLUDE_PROFILER\r
146 void ProcessWrapper::addToProfile(const char *event, void *port,\r
147     int length) {\r
148     if (profiler_output_file != NULL) {\r
149         fprintf(profiler_output_file, "%u %s %s %p%s %d\n",\r
150                 profiler_event_counter++, _name, event, port, (event[0] == 'r') ? "I" : "O",\r
151                 length);\r
152 \r
153     } else {\r
154         printf("profiler_output_file does not exist");\r
155     }\r
156 }\r
157 #endif\r