File: profiler.c

package info (click to toggle)
covered 0.7.10-7
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 9,040 kB
  • sloc: ansic: 48,809; yacc: 11,650; xml: 8,838; tcl: 7,698; sh: 3,925; lex: 2,240; makefile: 362; perl: 329
file content (337 lines) | stat: -rw-r--r-- 11,832 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
/*
 Copyright (c) 2000-2010 Trevor Williams

 This program is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by the Free Software
 Foundation; either version 2 of the License, or (at your option) any later version.

 This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 See the GNU General Public License for more details.

 You should have received a copy of the GNU General Public License along with this program;
 if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
/*!
 \file    profiler.c
 \author  Trevor Williams  (phase1geo@gmail.com)
 \date    12/10/2007
*/

#include <stdio.h>
#include <assert.h>

#include "profiler.h"


/*! Current profiling mode value */
bool profiling_mode = TRUE;

/*! Name of output profiling file */
static char* profiling_output = NULL;

/*! Stack of profiles that have been called */
static unsigned int stack[4096];

/*! Current size of the profile stack */
static unsigned int stack_size = 0;

#ifdef HAVE_SYS_TIME_H
static timer* sim_timer = NULL;
#endif


extern char user_msg[USER_MSG_LENGTH];


/*!
 \param value  New value to set profiling mode to

 Sets the current profiling mode to the given value.
*/
void profiler_set_mode( bool value ) {

  profiling_mode = value;

#ifdef HAVE_SYS_TIME_H
  if( profiling_mode ) {
    timer_start( &sim_timer );
  }
#endif
    
}

/*!
 \param fname  Name of output profiling file.

 Sets the profiling output file to the given value.
*/
void profiler_set_filename( const char* fname ) {

  /* Deallocate profiling output name, if one was already specified */
  free_safe( profiling_output, (strlen( profiling_output ) + 1) );

  profiling_output = strdup_safe( fname );

}

/*!
 \param index  Profiler index of current function.

 Increases the current call count for the current function, stops the timer for
 the last running counter and starts the timer for the current function.
*/
void profiler_enter( unsigned int index ) {

  /* Stop the last running timer if we are going to be timed */
  if( (stack_size > 0) && profiles[index].timed && profiles[stack[stack_size-1]].timed ) {
    timer_stop( &profiles[stack[stack_size-1]].time_in );
  }

  /* Increment the calls counter */
  profiles[index].calls++;

  /* Start the timer for this function, if needed */
  if( profiles[index].timed ) {
    timer_start( &profiles[index].time_in );
    stack[stack_size] = index;
    stack_size++;
  }

}

/*!
 Gets called when leaving a profiling function.  Stops the current timer and pops the stack.
*/
void profiler_exit( unsigned int index ) {

  /* Stop the current timer */
  timer_stop( &profiles[index].time_in );

  /* Pop the stack */
  stack_size--;

  /* Start the timer, if needed */
  if( (stack_size > 0) && profiles[stack[stack_size-1]].timed ) {
    timer_start( &profiles[stack[stack_size-1]].time_in );
  }

}

/*!
 Deallocates all allocated memory for profiler.
*/
static void profiler_dealloc() {

  int i;  /* Loop iterator */

  /* Deallocate profiling output name */
  free_safe( profiling_output, (strlen( profiling_output ) + 1) );

  /* Iterate through the profiler array and deallocate all timer structures */
  for( i=0; i<NUM_PROFILES; i++ ) {
    free_safe( profiles[i].time_in, sizeof( timer ) );
  }

}

static void profiler_sort_by_calls( FILE* ofile ) {

  int largest;             /* Index of largest calls profile */
  int i;                   /* Loop iterator */
  int j;                   /* Loop iterator */
  int list[NUM_PROFILES];  /* List of indices that can be used to sort */
  int tmp;                 /* Used for value swapping */

  /* Prepare a list of key/value pairs */
  for( i=0; i<NUM_PROFILES; i++ ) {
    list[i] = i;
  }

  /* Display header for this section */
  fprintf( ofile, "==============================================================================\n" );
  fprintf( ofile, "=                           Sort by calls Profile                            =\n" );
  fprintf( ofile, "==============================================================================\n" );
  fprintf( ofile, "\n" );
  fprintf( ofile, "Total simulation time: %ld\n", (long int)sim_timer->total );
  fprintf( ofile, "\n" );
  fprintf( ofile, "------------------------------------------------------------------------------------------------------\n" );
  fprintf( ofile, "Function Name                               calls       time        avg. time   mallocs     frees\n" );
  fprintf( ofile, "------------------------------------------------------------------------------------------------------\n" );

  /* Output them in order of most to least */
  for( i=(NUM_PROFILES-1); i>=0; i-- ) {
    largest = 0;
    for( j=0; j<i; j++ ) {
      if( profiles[list[j]].calls > profiles[list[largest]].calls ) {
        largest = j;
      }
    }
    tmp           = list[j];
    list[j]       = list[largest];
    list[largest] = tmp;
    if( profiles[list[j]].calls > 0 ) {
      if( profiles[list[j]].time_in == NULL ) {
        fprintf( ofile, "  %-40.40s  %10d          NA          NA  %10d  %10d\n",
                 profiles[list[j]].func_name, profiles[list[j]].calls, profiles[list[j]].mallocs, profiles[list[j]].frees );
      } else {
        fprintf( ofile, "  %-40.40s  %10d  %10ld  %10.3f  %10d  %10d\n",
                 profiles[list[j]].func_name, profiles[list[j]].calls, (long int)profiles[list[j]].time_in->total,
                 (float)(profiles[list[j]].time_in->total / (profiles[list[j]].calls * 1.0)), profiles[list[j]].mallocs, profiles[list[j]].frees );
      }
    }
  }

  fprintf( ofile, "\n\n\n" );
    
}

static void profiler_sort_by_time( FILE* ofile ) {

  int largest;             /* Index of largest calls profile */
  int i;                   /* Loop iterator */
  int j;                   /* Loop iterator */
  int list[NUM_PROFILES];  /* List of indices that can be used to sort */
  int tmp;                 /* Used for value swapping */

  /* Prepare a list of key/value pairs */
  for( i=0; i<NUM_PROFILES; i++ ) {
    list[i] = i;
  }

  /* Display header for this section */
  fprintf( ofile, "==============================================================================\n" );
  fprintf( ofile, "=                           Sort by time Profile                             =\n" );
  fprintf( ofile, "==============================================================================\n" );
  fprintf( ofile, "\n" );
  fprintf( ofile, "Total simulation time: %ld\n", (long int)sim_timer->total );
  fprintf( ofile, "\n" );
  fprintf( ofile, "------------------------------------------------------------------------------------------------------\n" );
  fprintf( ofile, "Function Name                               calls       time        avg. time   mallocs     frees\n" );
  fprintf( ofile, "------------------------------------------------------------------------------------------------------\n" );

  /* Output them in order of most to least */
  for( i=(NUM_PROFILES-1); i>=0; i-- ) {
    largest = 0;
    for( j=0; j<i; j++ ) {
      if( (profiles[list[j]].time_in != NULL) && ((profiles[list[largest]].time_in == NULL) || (profiles[list[j]].time_in->total > profiles[list[largest]].time_in->total)) ) {
        largest = j;
      }
    }
    tmp           = list[j];
    list[j]       = list[largest];
    list[largest] = tmp;
    if( profiles[list[j]].calls > 0 ) {
      if( profiles[list[j]].time_in == NULL ) {
        fprintf( ofile, "  %-40.40s  %10d          NA          NA  %10d  %10d\n",
                 profiles[list[j]].func_name, profiles[list[j]].calls, profiles[list[j]].mallocs, profiles[list[j]].frees );
      } else {
        fprintf( ofile, "  %-40.40s  %10d  %10ld  %10.3f  %10d  %10d\n",
                 profiles[list[j]].func_name, profiles[list[j]].calls, (long int)profiles[list[j]].time_in->total,
                 (float)(profiles[list[j]].time_in->total / (profiles[list[j]].calls * 1.0)), profiles[list[j]].mallocs, profiles[list[j]].frees );
      }
    }
  } 
    
  fprintf( ofile, "\n\n\n" );
    
}   

static void profiler_sort_by_avg_time( FILE* ofile ) {

  int largest;             /* Index of largest calls profile */
  int i;                   /* Loop iterator */
  int j;                   /* Loop iterator */
  int list[NUM_PROFILES];  /* List of indices that can be used to sort */
  int tmp;                 /* Used for value swapping */

  /* Prepare a list of key/value pairs */
  for( i=0; i<NUM_PROFILES; i++ ) {
    list[i] = i;
  }

  /* Display header for this section */
  fprintf( ofile, "==============================================================================\n" );
  fprintf( ofile, "=                           Sort by avg. time Profile                        =\n" );
  fprintf( ofile, "==============================================================================\n" );
  fprintf( ofile, "\n" );
  fprintf( ofile, "Total simulation time: %ld\n", (long int)sim_timer->total );
  fprintf( ofile, "\n" );
  fprintf( ofile, "------------------------------------------------------------------------------------------------------\n" );
  fprintf( ofile, "Function Name                               calls       time        avg. time   mallocs     frees\n" );
  fprintf( ofile, "------------------------------------------------------------------------------------------------------\n" );

  /* Output them in order of most to least */
  for( i=(NUM_PROFILES-1); i>=0; i-- ) {
    largest = 0;
    for( j=0; j<i; j++ ) {
      if( (profiles[list[j]].time_in != NULL) &&
          ((profiles[list[largest]].time_in == NULL) ||
           ((profiles[list[j]].time_in->total / profiles[list[j]].calls) > (profiles[list[largest]].time_in->total / profiles[list[largest]].calls))) ) {
        largest = j;
      }
    }
    tmp           = list[j];
    list[j]       = list[largest];
    list[largest] = tmp;
    if( profiles[list[j]].calls > 0 ) {
      if( profiles[list[j]].time_in == NULL ) {
        fprintf( ofile, "  %-40.40s  %10d          NA          NA  %10d  %10d\n",
                 profiles[list[j]].func_name, profiles[list[j]].calls, profiles[list[j]].mallocs, profiles[list[j]].frees );
      } else {
        fprintf( ofile, "  %-40.40s  %10d  %10ld  %10.3f  %10d  %10d\n",
                 profiles[list[j]].func_name, profiles[list[j]].calls, (long int)profiles[list[j]].time_in->total,
                 (float)(profiles[list[j]].time_in->total / (profiles[list[j]].calls * 1.0)), profiles[list[j]].mallocs, profiles[list[j]].frees );
      }
    }
  } 

  fprintf( ofile, "\n\n\n" );
    
}   

/*!
 Generates profiling report if the profiling mode is set to TRUE.
*/
void profiler_report() {

  FILE* ofile;  /* File stream pointer to output file */

  if( profiling_mode ) {

    assert( profiling_output != NULL );

    if( (ofile = fopen( profiling_output, "w" )) != NULL ) {

      unsigned int rv;

      /* Stop the simulation timer and deallocate it */
      timer_stop( &sim_timer );

      /* Output profiling results */
      profiler_sort_by_time( ofile );
      profiler_sort_by_avg_time( ofile );
      profiler_sort_by_calls( ofile );

      /* Close the output file */
      rv = fclose( ofile );
      assert( rv == 0 );

    } else {

      unsigned int rv = snprintf( user_msg, USER_MSG_LENGTH, "Unable to open profiling output file \"%s\" for writing", profiling_output );
      assert( rv < USER_MSG_LENGTH );
      print_output( user_msg, FATAL, __FILE__, __LINE__ );

    }

  }

  /* Deallocate sim_timer */
  free_safe( sim_timer, sizeof( timer ) );

  /* Delete memory associated with the profiler */
  profiler_dealloc();

}