summaryrefslogtreecommitdiff
path: root/Runtime/Profiler/SharkProfiler.cpp
blob: 1b3d592971d910bc94b14906fb33cd09cdc62afb (plain)
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
#include "UnityPrefix.h"
#include "SharkProfiler.h"
#include "Configuration/UnityConfigure.h"

#define ENABLE_SHARK_PROFILER ENABLE_PROFILER && UNITY_OSX

#if ENABLE_SHARK_PROFILE
#include <mach-o/dyld.h>

typedef int (*fpChudAcquireRemoteAccess) ( void );
typedef int (*fpChudReleaseRemoteAccess) ( void );
typedef int (*fpChudStartRemotePerfMonitor) ( const char *label );
typedef int (*fpChudStopRemotePerfMonitor) ( void );
typedef int (*fpChudInitialize) ( void );
typedef int (*fpChudCleanup) ( void );
typedef int (*fpChudIsInitialized) ( void );

// declare storage for each API's function pointers
static int gChudLoaded = -1;
static fpChudAcquireRemoteAccess gChudAcquireRemoteAccess = NULL;
static fpChudReleaseRemoteAccess gChudReleaseRemoteAccess = NULL;
static fpChudStartRemotePerfMonitor gChudStartRemotePerfMonitor = NULL;
static fpChudStopRemotePerfMonitor gChudStopRemotePerfMonitor = NULL;
static fpChudInitialize gChudInitialize = NULL;
static fpChudCleanup gChudCleanup = NULL;
static fpChudIsInitialized gChudIsInitialized = NULL;

static bool LoadChudFunctionPointers ()
{
	if (gChudLoaded == 1)
		return true;
	if (gChudLoaded == 0)
		return false;
	
	CFStringRef bundlePath = CFSTR("/System/Library/PrivateFrameworks/CHUD.framework/Versions/A/Frameworks/CHUDCore.framework");
	CFURLRef bundleURL = CFURLCreateWithFileSystemPath (kCFAllocatorDefault, bundlePath, kCFURLPOSIXPathStyle, true);
	CFBundleRef bundle = CFBundleCreate (kCFAllocatorDefault, bundleURL);
	
	if (bundle == NULL)
	{
		ErrorString("Failed loading Shark system library");
		gChudLoaded = 0;
		return false;
	}
	
	gChudAcquireRemoteAccess=(fpChudAcquireRemoteAccess)CFBundleGetFunctionPointerForName (bundle, CFSTR("chudAcquireRemoteAccess"));
	gChudReleaseRemoteAccess=(fpChudReleaseRemoteAccess)CFBundleGetFunctionPointerForName (bundle, CFSTR("chudReleaseRemoteAccess"));
	gChudStartRemotePerfMonitor=(fpChudStartRemotePerfMonitor)CFBundleGetFunctionPointerForName (bundle, CFSTR("chudStartRemotePerfMonitor"));
	gChudStopRemotePerfMonitor=(fpChudStopRemotePerfMonitor)CFBundleGetFunctionPointerForName (bundle, CFSTR("chudStopRemotePerfMonitor"));
	gChudInitialize=(fpChudInitialize)CFBundleGetFunctionPointerForName (bundle, CFSTR("chudInitialize"));
	gChudCleanup=(fpChudCleanup)CFBundleGetFunctionPointerForName (bundle, CFSTR("chudCleanup"));
	gChudIsInitialized=(fpChudIsInitialized)CFBundleGetFunctionPointerForName (bundle, CFSTR("chudIsInitialized"));
	
	if (gChudAcquireRemoteAccess == NULL || gChudReleaseRemoteAccess == NULL || gChudStartRemotePerfMonitor == NULL
		|| gChudStartRemotePerfMonitor == NULL || gChudStopRemotePerfMonitor == NULL || gChudInitialize == NULL || gChudCleanup == NULL || gChudIsInitialized == NULL)
	{
		ErrorString("Failed loading Shark system library function");
		gChudLoaded = 0;
		return false;
	}
	else
	{
		gChudLoaded = 1;
		return true;
	}
}
#endif

void SharkBeginRemoteProfiling ()
{
#if ENABLE_SHARK_PROFILER
	if (!LoadChudFunctionPointers ())
		return;
	
	if (gChudInitialize() != 0)
		ErrorString("Launching Shark in SharkBeginProfile failed");
	if (gChudAcquireRemoteAccess() != 0)
		ErrorString("Launching Shark in SharkBeginProfile failed");
	if (gChudStartRemotePerfMonitor("Unity") != 0)
		ErrorString("Launching Shark in SharkBeginProfile failed");
#endif
}

void SharkEndRemoteProfiling ()
{
#if ENABLE_SHARK_PROFILER
	if (!LoadChudFunctionPointers ())
		return;
	
	gChudStopRemotePerfMonitor();
	gChudReleaseRemoteAccess();
#endif
}