Created
April 6, 2011 19:29
-
-
Save PlacePop/906347 to your computer and use it in GitHub Desktop.
Quick and Dirty Profiler for iPhone
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// profiler.h | |
// Quick and dirty profiler | |
// | |
// Created by Niels Gabel on 9/1/08. | |
// | |
// Copyright 208-2011 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... | |
#if defined( PROFILING ) && PROFILING | |
# warning Profiling ON! | |
#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() ; | |
// gProfilerOverhead += clock() - __profiler_start_time ; \ | |
// ++gProfilerCallCount ; | |
#define ProfilerExit( __function_name )\ | |
__profiler_counter->func_time += clock() - __profiler_start_time ;\ | |
++__profiler_counter->count ;\ | |
ProfilerStackPop() ; | |
// gProfilerOverhead += clock() - __profiler_end_time ; \ | |
// ++gProfilerCallCount ; | |
#else | |
#define ProfilerEnter( __function_name ) {} | |
#define ProfilerExit( __function_name ) {} | |
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// profiler.m | |
// Quick and dirty profiler | |
// | |
// Created by Niels Gabel on 9/1/08. | |
// | |
// Copyright 208-2011 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 "utils.h" | |
//#import "debug.h" | |
#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; | |
} | |
//clock_t gProfilerOverhead = 0 ; | |
//unsigned long gProfilerCallCount = 0 ; | |
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 = stdout ; | |
#endif | |
fprintf( file, "%s %s\n", __DATE__, __TIME__ ) ; | |
// { | |
// float time = (float)gProfilerOverhead / (float)CLOCKS_PER_SEC ; | |
// | |
// fprintf( file, "profiler time used: %.5f\n", time ) ; | |
// fprintf( file, "profiler avg. time: %.2f\n", time / (float)gProfilerCallCount ) ; | |
// } | |
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