Skip to content

Instantly share code, notes, and snippets.

@ngabel
Created May 2, 2011 21:58
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ngabel/952456 to your computer and use it in GitHub Desktop.
Save ngabel/952456 to your computer and use it in GitHub Desktop.
A lightweight set of routines for your on-device iPhone performance tuning needs. Enjoy!
//
// 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.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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment