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 | |
12 | #include "Vector.hh" |
13 | #include "Types.h" |
14 | |
15 | /** This class performs profiling and code coverage on lines and functions in |
16 | * TTCN-3 code (requires the -z compiler option). |
17 | * Customizable through the configuration file's [PROFILER] section. */ |
18 | class TTCN3_Profiler { |
19 | public: |
20 | |
21 | /** Database entry for one file */ |
22 | struct profiler_db_item_t { |
23 | /** Database entry for one line */ |
24 | struct profiler_line_data_t { |
25 | /** The line's total execution time */ |
26 | double total_time; |
27 | /** The number of times this line was executed */ |
28 | int exec_count; |
29 | }; |
30 | /** Database entry for one function (including test cases, alt steps, the control part, etc.) */ |
31 | struct profiler_function_data_t { |
32 | /** Function name (owned) */ |
33 | char* name; |
34 | /** Function starting line */ |
35 | int lineno; |
36 | /** The function's total execution time */ |
37 | double total_time; |
38 | /** The number of times this function was executed */ |
39 | int exec_count; |
40 | }; |
41 | /** TTCN-3 File name (relative path, owned) */ |
42 | char* filename; |
43 | /** Contains database entries for all the lines in this file (its index is |
44 | * the line number, so there may be empty elements) */ |
45 | Vector<profiler_line_data_t> lines; |
46 | /** Contains database entries for all the functions in this file (one entry |
47 | * for each function) */ |
48 | Vector<profiler_function_data_t> functions; |
49 | }; |
50 | |
51 | /** Constructor */ |
52 | TTCN3_Profiler(); |
53 | /** Destructor - adds all gathered data to the database file and prints |
54 | * statistics if necessary */ |
55 | ~TTCN3_Profiler(); |
56 | |
57 | /** Enables or disables profiling - called by the config file parser */ |
58 | void set_disable_profiler(boolean p_disable_profiler); |
59 | /** Enables or disables code coverage - called by the config file parser */ |
60 | void set_disable_coverage(boolean p_disable_coverage); |
61 | /** Sets the database file name (default is "profiler.db" - called by the config file parser */ |
62 | void set_database_filename(const char* p_database_filename); |
63 | /** Enables or disables data aggregation - called by the config file parser */ |
64 | void set_aggregate_data(boolean p_aggregate_data); |
65 | /** Sets the statistics file name (default is "profiler.stats" - called by the config file parser */ |
66 | void set_stats_filename(const char* p_stats_filename); |
67 | /** Enables or disables the printing of statistics - called by the config file parser */ |
68 | void set_disable_stats(boolean p_disable_stats); |
69 | |
70 | /** Returns true if profiling is disabled */ |
71 | boolean is_profiler_disabled() const; |
72 | |
73 | /** Deletes the database file if data aggregation is not set */ |
74 | void init_data_file(); |
75 | /** Adds the data from the database file to the local database */ |
76 | void import_data(); |
77 | /** Writes the local database to the database file (overwrites the file) */ |
78 | void export_data(); |
79 | |
80 | /** Calculates and prints statistics from the gathered data */ |
81 | void print_stats(); |
82 | |
83 | /** Resets data related to the previous location and time (the local database is not changed) */ |
84 | void reset(); |
85 | /** Returns the current time (in seconds) */ |
86 | static double get_time(); |
87 | /** Called when a TTCN-3 function starts execution - stores data */ |
88 | void enter_function(const char* filename, int lineno, const char* function_name); |
89 | /** Called when a TTCN-3 code line starts execution - stores data */ |
90 | void execute_line(const char* filename, int lineno); |
91 | /** Returns the index of a TTCN-3 file's entry in the local database */ |
92 | int get_element(const char* filename); |
93 | /** Returns the index of a TTCN-3 function's entry in the database |
94 | * @param element index of the file (where the function is declared) |
95 | * @param lineno function start line */ |
96 | int get_function(int element, int lineno); |
97 | /** Creates a new TTCN-3 function entry and inserts it in the database |
98 | * @param element file entry's index |
99 | * @param lineno function start line |
100 | * @param function_name name of the function */ |
101 | void create_function(int element, int lineno, const char* function_name); |
102 | /** Creates TTCN-3 code line entries up to the given line number */ |
103 | void create_lines(int element, int lineno); |
104 | /** Adds elapsed time to the specified TTCN-3 code line's total time */ |
105 | void add_line_time(double elapsed, int element, int lineno); |
106 | /** Adds elapsed time to the specified TTCN-3 function's total time*/ |
107 | void add_function_time(double elapsed, int element, int lineno); |
108 | /** Called when a TTCN-3 function's execution ends - stores data */ |
109 | void update_last(); |
110 | /** Stores data related to the previous location */ |
111 | void set_prev(int stack_len, const char* filename, int lineno); |
112 | |
113 | private: |
114 | /** Profiling is disabled if true */ |
115 | boolean disable_profiler; |
116 | /** Code coverage is disabled if true */ |
117 | boolean disable_coverage; |
118 | /** Contains the database file name */ |
119 | char* database_filename; |
120 | /** If true, data gathered by previous runs will be added to the data gathered |
121 | * in this run */ |
122 | boolean aggregate_data; |
123 | /** Contains the statistics file name */ |
124 | char* stats_filename; |
125 | /** Statistics will not be calculated and printed if true */ |
126 | boolean disable_stats; |
127 | |
128 | /** The time measured at the previous TTCN-3 code line */ |
129 | double prev_time; |
130 | /** Name of the TTCN-3 file, where the last executed line is (not owned) */ |
131 | const char* prev_file; |
132 | /** The number of the previously executed line */ |
133 | int prev_line; |
134 | /** The local database */ |
135 | Vector<profiler_db_item_t> profiler_db; |
136 | /** The stack length at the previously executed line */ |
137 | int prev_stack_len; |
138 | }; |
139 | |
140 | /** The global TTCN3_Profiler object |
141 | * |
142 | * One instance is created in each process (in parallel mode). |
143 | * After construction the configuration file parser may change the profiler's settings. |
144 | * The destructor merges its data with that of other processes (and possibly with previous runs) |
145 | * through the database file. The last destructor (the one in the Host Controller's process) |
146 | * prints the statistics (if enabled). */ |
147 | extern TTCN3_Profiler ttcn3_prof; |
148 | |
149 | /** Helper class for profiling |
150 | * |
151 | * Its instances depict the current call stack. One instance is created at the start |
152 | * of each TTCN-3 function execution, and it's destroyed at the function's end. */ |
153 | class TTCN3_Stack_Depth { |
154 | public: |
155 | /** Entry for one function call in the call stack */ |
156 | struct call_stack_timer_item_t { |
157 | /** Stack length before the function call */ |
158 | int stack_len; |
159 | /** File name, where the calling function is declared (not owned) */ |
160 | const char* caller_file; |
161 | /** File name, where the called function is declared (not owned)*/ |
162 | const char* func_file; |
163 | /** Calling function's start line */ |
164 | int caller_line; |
165 | /** Called function's start line */ |
166 | int start_line; |
167 | /** Time elapsed in this function call */ |
168 | double elapsed; |
169 | }; |
170 | |
171 | /** Constructor - increases the stack depth */ |
172 | TTCN3_Stack_Depth(); |
173 | /** Destructor - decreases the stack depth, updates call times in the profiler */ |
174 | ~TTCN3_Stack_Depth(); |
175 | |
176 | /** Returns the current stack depth */ |
177 | static int depth() { return current_depth; } |
178 | /** Inserts a new function call entry into the call stack database */ |
179 | static void add_stack(int stack_len, const char* caller_file, const char* func_file, |
180 | int caller_line, int start_line); |
181 | /** Removes the last entry from the call stack database */ |
182 | static void remove_stack(); |
183 | /** Adds the elapsed time to all entries in the call stack database */ |
184 | static void update_stack_elapsed(double elapsed); |
185 | private: |
186 | /** The current stack depth (starts from 0)*/ |
187 | static int current_depth; |
188 | /** The call stack database */ |
189 | static Vector<call_stack_timer_item_t> call_stack_timer_db; |
190 | }; |
191 | |
192 | #endif /* PROFILER_HH */ |
193 | |