public
Last active

A lightweight set of routines for your on-device iPhone performance tuning needs. Enjoy!

  • Download Gist
profiler.h
C++
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
//
// profiler.h
// Quick and dirty profiler
//
// Created by Niels Gabel on 9/1/08.
//
// Copyright 2010 Niels Gabel
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
 
// To use:
// At function start, call ProfilerEnter() and pass your function name.
// Call ProfilerEnd() and pass your function name before your function returns
// ProfilerInit() installs an atexit() handler and will dump the profiler results to file
// when the program exits...
 
#ifdef PROFILE
#define PROFILING PROFILE
#endif
 
#if defined( PROFILING ) && PROFILING
# warning Profiling ON!
 
//#define PROFILER_OUTPUT_TO_FILE YES
 
#import <pthread.h>
 
struct ProfilerDataCounter
{
struct ProfilerDataCounter * parent ;
clock_t func_time ;
unsigned long count ;
} ;
 
struct ProfilerData
{
char * name ;
struct ProfilerDataCounter counters[10] ;
} ;
 
 
extern pthread_key_t gProfilerDataPThreadKey ;
extern struct ProfilerData gProfilerTable[100] ;
extern struct ProfilerData * gProfilerTableLastUsedEntry ;
extern
#ifdef __cplusplus
"C"
#endif
 
void _ProfilerInit() ;
 
extern
#ifdef __cplusplus
"C"
#endif
void ProfilerOutputTree( FILE * file ) ;
 
//extern clock_t gProfilerOverhead ;
//extern unsigned long gProfilerCallCount ;
 
#define ProfilerGetCurrent() (struct ProfilerDataCounter*)pthread_getspecific( gProfilerDataPThreadKey )
#define ProfilerSetCurrent( __profiler_data ) pthread_setspecific( gProfilerDataPThreadKey, (const void*)__profiler_data )
 
//#define ProfilerInit() { _ProfilerInit() ; }
#define ProfilerStackPush( ) \
struct ProfilerDataCounter * __profiler_parent = ProfilerGetCurrent() ; \
struct ProfilerDataCounter * __profiler_counter = & __profiler_data->counters[ 0 ] ; \
if ( __profiler_parent ) { \
while( __profiler_counter < & __profiler_data->counters[ 10 ] ) { \
++__profiler_counter ; \
if ( __profiler_counter->parent == __profiler_parent ) { break ; } \
else if ( !__profiler_counter->parent ) { \
__profiler_counter->parent = __profiler_parent ; \
__profiler_counter->func_time = 0 ; \
__profiler_counter->count = 0 ; \
break ; \
} \
} \
} \
ProfilerSetCurrent( __profiler_counter ) ;
 
#define ProfilerStackPop() ProfilerSetCurrent( __profiler_parent ) ;
#define ProfilerEnter( __function_name ) \
static struct ProfilerData * __profiler_data = NULL ; \
clock_t __profiler_start_time = clock() ; \
if ( !__profiler_data ) { __profiler_data = gProfilerTableLastUsedEntry++ ; __profiler_data->name = (char*)__function_name ; } \
ProfilerStackPush() ;
 
#define ProfilerExit( __function_name )\
__profiler_counter->func_time += clock() - __profiler_start_time ;\
++__profiler_counter->count ;\
ProfilerStackPop() ;
#else
#define ProfilerEnter( __function_name ) {}
#define ProfilerExit( __function_name ) {}
#endif
profiler.mm
Objective-C
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
//
// profiler.m
// Quick and dirty profiler
//
// Created by Niels Gabel on 9/1/08.
//
// Copyright 2010 Niels Gabel
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
 
#import "profiler.h"
 
#if defined( PROFILING ) && PROFILING
 
/*
To use:
At function start, call ProfilerEnter() and pass your function name.
Call ProfilerExit() and pass your function name before your function returns
ProfilerInit() installs an atexit() handler and will dump the profiler results to file
when the program exits...
 
Define PROFILER_OUTPUT_TO_FILE if you want to append profiler output to "profiler_output.txt"
in your app's Documents folder. You can retrieve the file using the Organizer in Xcode.
(Click on your device, find it in the Applications list, turn down the disclosure triangle,
and click the download arrow on the right.)
If PROFILER_OUTPUT_TO_FILE is not defined, output written to stdout.
*/
 
#import <signal.h>
 
pthread_key_t gProfilerDataPThreadKey = 0 ;
struct ProfilerData gProfilerTable[100] ;
struct ProfilerData * gProfilerTableLastUsedEntry = & gProfilerTable[0] ;
 
static NSString * GetDocumentsFolderPath()
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *basePath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;
return basePath;
}
 
static void ProfilerOutputTreeNode( FILE * file, struct ProfilerData * data, struct ProfilerDataCounter * counter, int indent )
{
{
float time = (float)counter->func_time / (float)CLOCKS_PER_SEC ;
fprintf( file, "%*s%-*s %15.5f %15lu %10.5f\n", indent * 4, "", 80 - (indent * 4 ), data->name, time, counter->count, time / (float)counter->count ) ;
}
 
for( struct ProfilerData * testData = & gProfilerTable[0]; testData < gProfilerTableLastUsedEntry; ++testData )
{
if ( !testData->name )
{
break ;
}
 
if ( testData != data )
{
for( struct ProfilerDataCounter * testCounter = & testData->counters[1] ; testCounter < &testData->counters[10]; ++testCounter )
{
if ( !testCounter->parent )
{
break ;
}
 
if ( testCounter->parent == counter )
{
ProfilerOutputTreeNode( file, testData, testCounter, indent + 1 ) ;
}
}
}
}
}
 
void
ProfilerOutputTree( FILE * file )
{
fprintf( file, "%-80s %15s %15s %10s\n", "FUNCTION", "TOTAL TIME", "COUNT", "AVG. TIME") ;
 
for( struct ProfilerData * data = & gProfilerTable[0]; data < gProfilerTableLastUsedEntry; ++data )
{
if ( data->counters[0].count > 0 )
{
ProfilerOutputTreeNode( file, data, & data->counters[0], 0 ) ;
}
}
}
 
static void
ProfilerExitFunction()
{
DebugLog(@"%s\n", __PRETTY_FUNCTION__ ) ;
NSAutoreleasePool * pool = [ [ NSAutoreleasePool alloc ] init ] ;
 
#ifdef PROFILER_OUTPUT_TO_FILE
FILE * file = fopen( [ [ GetDocumentsFolderPath() stringByAppendingPathComponent:@"profiler_output.txt" ] UTF8String ], "a" ) ;
if ( !file )
{
file = stdout ;
}
#else
FILE * file = stderr ;
#endif
fprintf( file, "%s %s\n", __DATE__, __TIME__ ) ;
ProfilerOutputTree( file ) ;
 
[ pool release ] ;
}
 
static void ProfilerInterruptSignalHandler( int signal )
{
DebugLog(@"%s\n", __PRETTY_FUNCTION__ ) ;
ProfilerExitFunction() ;
}
 
struct ProfilerInit
{
ProfilerInit()
{
NSAutoreleasePool * pool = [ [ NSAutoreleasePool alloc ] init ] ;
DebugLog( @"Using profiling...\n" ) ;
DebugAssert( 0 == atexit( ProfilerExitFunction ) ) ;
DebugAssert( 0 == pthread_key_create( & gProfilerDataPThreadKey, NULL ) ) ;
{
const struct sigaction act =
{
{ ProfilerInterruptSignalHandler }, 0, 0
} ;
sigaction( SIGKILL, & act, NULL ) ;
}
bzero( gProfilerTable, sizeof( gProfilerTable ) ) ;
gProfilerTableLastUsedEntry = & gProfilerTable[ 0 ] ;
[ pool release ] ;
}
} ;
static struct ProfilerInit gProfilerInit = ProfilerInit() ;
 
#endif

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.