af710487 |
1 | /////////////////////////////////////////////////////////////////////////////// |
2 | // Copyright (c) 2000-2015 Ericsson Telecom AB |
3 | // All rights reserved. This program and the accompanying materials |
4 | // are made available under the terms of the Eclipse Public License v1.0 |
5 | // which accompanies this distribution, and is available at |
6 | // http://www.eclipse.org/legal/epl-v10.html |
7 | /////////////////////////////////////////////////////////////////////////////// |
8 | |
9 | #ifndef PROFILER_HH |
10 | #define PROFILER_HH |
11 | |
3abe9331 |
12 | #include "ProfilerTools.hh" |
af710487 |
13 | |
14 | /** This class performs profiling and code coverage on lines and functions in |
15 | * TTCN-3 code (requires the -z compiler option). |
16 | * Customizable through the configuration file's [PROFILER] section. */ |
17 | class TTCN3_Profiler { |
18 | public: |
19 | |
af710487 |
20 | /** Constructor */ |
21 | TTCN3_Profiler(); |
a38c6d4c |
22 | /** Destructor |
23 | * In single mode and in the Host Controller's process in parallel mode: |
24 | * - imports data gathered on the previous run (if data aggregation is set) |
25 | * - imports data gathered by all other processes (only in parallel mode) |
26 | * - prints statistics (if necessary) |
27 | * Afterwards, in all cases: |
28 | * - exports data gathered in this process (including any imported data) |
29 | * - frees allocated memory */ |
af710487 |
30 | ~TTCN3_Profiler(); |
31 | |
a38c6d4c |
32 | /** Reactivates the profiler if it was stopped before, data gathering will resume */ |
33 | void start(); |
34 | /** Deactivates the profiler, no more data will be gathered until it is reactivated */ |
35 | void stop(); |
36 | |
af710487 |
37 | /** Enables or disables profiling - called by the config file parser */ |
38 | void set_disable_profiler(boolean p_disable_profiler); |
39 | /** Enables or disables code coverage - called by the config file parser */ |
40 | void set_disable_coverage(boolean p_disable_coverage); |
41 | /** Sets the database file name (default is "profiler.db" - called by the config file parser */ |
42 | void set_database_filename(const char* p_database_filename); |
43 | /** Enables or disables data aggregation - called by the config file parser */ |
44 | void set_aggregate_data(boolean p_aggregate_data); |
45 | /** Sets the statistics file name (default is "profiler.stats" - called by the config file parser */ |
46 | void set_stats_filename(const char* p_stats_filename); |
47 | /** Enables or disables the printing of statistics - called by the config file parser */ |
48 | void set_disable_stats(boolean p_disable_stats); |
a38c6d4c |
49 | /** Disables all statistics entry flags - called by the config file parser */ |
50 | void reset_stats_flags(); |
51 | /** Enables the specified statistics entry flags - called by the config file parser */ |
52 | void add_stats_flags(unsigned int p_flags); |
af710487 |
53 | |
54 | /** Returns true if profiling is disabled */ |
55 | boolean is_profiler_disabled() const; |
a38c6d4c |
56 | /** Returns true if the profiler is currently running (not stopped) */ |
57 | boolean is_running() const; |
af710487 |
58 | |
3abe9331 |
59 | /** Stores the component reference of a newly created PTC or MTC (in parallel mode only) */ |
60 | void add_component(component p_comp_ref); |
a38c6d4c |
61 | |
af710487 |
62 | /** Adds the data from the database file to the local database */ |
a38c6d4c |
63 | void import_data(component p_comp_ref = NULL_COMPREF); |
af710487 |
64 | /** Writes the local database to the database file (overwrites the file) */ |
65 | void export_data(); |
66 | |
67 | /** Calculates and prints statistics from the gathered data */ |
68 | void print_stats(); |
69 | |
70 | /** Resets data related to the previous location and time (the local database is not changed) */ |
71 | void reset(); |
72 | /** Returns the current time (in seconds) */ |
a38c6d4c |
73 | static timeval get_time(); |
af710487 |
74 | /** Called when a TTCN-3 function starts execution - stores data */ |
a38c6d4c |
75 | void enter_function(const char* filename, int lineno); |
af710487 |
76 | /** Called when a TTCN-3 code line starts execution - stores data */ |
77 | void execute_line(const char* filename, int lineno); |
78 | /** Returns the index of a TTCN-3 file's entry in the local database */ |
79 | int get_element(const char* filename); |
80 | /** Returns the index of a TTCN-3 function's entry in the database |
81 | * @param element index of the file (where the function is declared) |
82 | * @param lineno function start line */ |
83 | int get_function(int element, int lineno); |
84 | /** Creates a new TTCN-3 function entry and inserts it in the database |
85 | * @param element file entry's index |
86 | * @param lineno function start line |
87 | * @param function_name name of the function */ |
88 | void create_function(int element, int lineno, const char* function_name); |
a38c6d4c |
89 | /** Returns the index of a TTCN-3 code line's entry in the database */ |
90 | int get_line(int element, int lineno); |
91 | /** Creates a new TTCN-3 code line entry and inserts it into the database */ |
92 | void create_line(int element, int lineno); |
af710487 |
93 | /** Adds elapsed time to the specified TTCN-3 code line's total time */ |
a38c6d4c |
94 | void add_line_time(timeval elapsed, int element, int lineno); |
3abe9331 |
95 | /** Adds elapsed time to the specified TTCN-3 function's total time */ |
a38c6d4c |
96 | void add_function_time(timeval elapsed, int element, int lineno); |
af710487 |
97 | /** Called when a TTCN-3 function's execution ends - stores data */ |
98 | void update_last(); |
99 | /** Stores data related to the previous location */ |
100 | void set_prev(int stack_len, const char* filename, int lineno); |
101 | |
102 | private: |
a38c6d4c |
103 | /** If true, the profiler ignores execute_line, enter_function and update_last calls */ |
104 | boolean stopped; |
af710487 |
105 | /** Profiling is disabled if true */ |
106 | boolean disable_profiler; |
107 | /** Code coverage is disabled if true */ |
108 | boolean disable_coverage; |
109 | /** Contains the database file name */ |
110 | char* database_filename; |
111 | /** If true, data gathered by previous runs will be added to the data gathered |
112 | * in this run */ |
113 | boolean aggregate_data; |
114 | /** Contains the statistics file name */ |
115 | char* stats_filename; |
116 | /** Statistics will not be calculated and printed if true */ |
117 | boolean disable_stats; |
a38c6d4c |
118 | /** Flags that determine which statistics entries are displayed */ |
119 | unsigned int stats_flags; |
af710487 |
120 | /** The time measured at the previous TTCN-3 code line */ |
a38c6d4c |
121 | timeval prev_time; |
af710487 |
122 | /** Name of the TTCN-3 file, where the last executed line is (not owned) */ |
123 | const char* prev_file; |
124 | /** The number of the previously executed line */ |
125 | int prev_line; |
126 | /** The local database */ |
3abe9331 |
127 | Profiler_Tools::profiler_db_t profiler_db; |
af710487 |
128 | /** The stack length at the previously executed line */ |
129 | int prev_stack_len; |
3abe9331 |
130 | /** Contains the component references of the other processes (only relevant in the Host |
a38c6d4c |
131 | * Controller's process, in parallel mode) */ |
3abe9331 |
132 | Vector<component> component_list; |
af710487 |
133 | }; |
134 | |
135 | /** The global TTCN3_Profiler object |
136 | * |
137 | * One instance is created in each process (in parallel mode). |
138 | * After construction the configuration file parser may change the profiler's settings. |
139 | * The destructor merges its data with that of other processes (and possibly with previous runs) |
140 | * through the database file. The last destructor (the one in the Host Controller's process) |
141 | * prints the statistics (if enabled). */ |
142 | extern TTCN3_Profiler ttcn3_prof; |
143 | |
144 | /** Helper class for profiling |
145 | * |
146 | * Its instances depict the current call stack. One instance is created at the start |
147 | * of each TTCN-3 function execution, and it's destroyed at the function's end. */ |
148 | class TTCN3_Stack_Depth { |
a38c6d4c |
149 | public: |
af710487 |
150 | /** Entry for one function call in the call stack */ |
151 | struct call_stack_timer_item_t { |
152 | /** Stack length before the function call */ |
153 | int stack_len; |
154 | /** File name, where the calling function is declared (not owned) */ |
155 | const char* caller_file; |
a38c6d4c |
156 | /** File name, where the called function is declared (not owned) */ |
af710487 |
157 | const char* func_file; |
158 | /** Calling function's start line */ |
159 | int caller_line; |
160 | /** Called function's start line */ |
161 | int start_line; |
162 | /** Time elapsed in this function call */ |
a38c6d4c |
163 | timeval elapsed; |
164 | /** If true, then this is the first entry of this function and caller pair |
165 | * (only used in case of gross line times) */ |
166 | boolean first_call; |
167 | /** If true, then this function has appeared before in the call stack |
168 | * (only used in case of gross function times)*/ |
169 | boolean recursive_call; |
af710487 |
170 | }; |
171 | |
172 | /** Constructor - increases the stack depth */ |
173 | TTCN3_Stack_Depth(); |
174 | /** Destructor - decreases the stack depth, updates call times in the profiler */ |
175 | ~TTCN3_Stack_Depth(); |
176 | |
a38c6d4c |
177 | /** Sets whether line times should include function call times - called by the config file parser */ |
178 | static void set_net_line_times(boolean p_net_line_times); |
179 | /** Sets whether function times should include embedded function times - called by the config file parser */ |
180 | static void set_net_func_times(boolean p_net_func_times); |
181 | |
af710487 |
182 | /** Returns the current stack depth */ |
183 | static int depth() { return current_depth; } |
184 | /** Inserts a new function call entry into the call stack database */ |
185 | static void add_stack(int stack_len, const char* caller_file, const char* func_file, |
186 | int caller_line, int start_line); |
187 | /** Removes the last entry from the call stack database */ |
188 | static void remove_stack(); |
189 | /** Adds the elapsed time to all entries in the call stack database */ |
a38c6d4c |
190 | static void update_stack_elapsed(timeval elapsed); |
af710487 |
191 | private: |
192 | /** The current stack depth (starts from 0)*/ |
193 | static int current_depth; |
194 | /** The call stack database */ |
195 | static Vector<call_stack_timer_item_t> call_stack_timer_db; |
a38c6d4c |
196 | /** If true, line times will not include the execution times of functions called |
197 | * in that line */ |
198 | static boolean net_line_times; |
199 | /** If true, function times will not include the execution times of functions |
200 | * called in that function */ |
201 | static boolean net_func_times; |
af710487 |
202 | }; |
203 | |
204 | #endif /* PROFILER_HH */ |
205 | |