Skip to content

Commit

Permalink
[v1.2.1]
Browse files Browse the repository at this point in the history
Improving API for custom allocators.
Adding Shutdown function.
Adding support for generic FreeBSD platform - using it as a fallback if we are not on Linux or MacOS.
Fixing a problem with some templated edge-cases in the Function Shortener.
  • Loading branch information
bombomby committed May 5, 2019
1 parent e382569 commit b97a2c3
Show file tree
Hide file tree
Showing 11 changed files with 181 additions and 58 deletions.
45 changes: 27 additions & 18 deletions gui/Data/EventData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,13 @@ public String FullName
get { return fullName; }
set
{
fullName = value;
name = StripFunctionArguments(fullName);
name = StripReturnValue(name);
fullName = value;

String shortName = StripFunctionArguments(fullName);
if (shortName.Length != fullName.Length)
name = StripReturnValue(shortName);
else
name = fullName;
}
}

Expand Down Expand Up @@ -86,24 +90,29 @@ static String StripFunctionArguments(String name)
return name;
}

static String[] callConventions = { "__thiscall", "__fastcall", "__cdecl", "__clrcall", "__stdcall", "__vectorcall" };
static String StripReturnValue(String name)
{
if (name.Length == 0)
return name;

foreach (String functionCall in callConventions)
{
int index = name.IndexOf(functionCall);
if (index != -1)
{
int cutIndex = index + functionCall.Length + 1;
if (cutIndex < name.Length)
return name.Substring(cutIndex);
}
}
int bracketsDepth = 0;

return name;
for (int i = name.Length - 1; i >= 0; --i)
{
switch (name[i])
{
case '>':
++bracketsDepth;
break;

case '<':
--bracketsDepth;
break;

case ' ':
if (bracketsDepth == 0)
return name.Substring(i + 1);
break;
}
}
return name;
}

public override string ToString()
Expand Down
28 changes: 14 additions & 14 deletions gui/TimeLine/TimeLine.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,13 @@ public TimeLine()
this.InitializeComponent();
this.DataContext = frames;

statusToError.Add(ETWStatus.ETW_ERROR_ACCESS_DENIED, new KeyValuePair<string, string>("ETW can't start: launch your game (or Visual Studio) as administrator to collect context switches", "https://github.com/bombomby/optick/wiki/Event-Tracing-for-Windows"));
statusToError.Add(TracerStatus.TRACER_ERROR_ACCESS_DENIED, new KeyValuePair<string, string>("ETW can't start: launch your game (or Visual Studio) as administrator to collect context switches", "https://github.com/bombomby/optick/wiki/Event-Tracing-for-Windows"));
statusToError.Add(TracerStatus.TRACER_ERROR_ALREADY_EXISTS, new KeyValuePair<string, string>("ETW session already started (Reboot should help)", "https://github.com/bombomby/optick/wiki/Event-Tracing-for-Windows"));
statusToError.Add(TracerStatus.TRACER_FAILED, new KeyValuePair<string, string>("ETW session failed (Run your Game or Visual Studio as Administrator to get ETW data)", "https://github.com/bombomby/optick/wiki/Event-Tracing-for-Windows"));
statusToError.Add(TracerStatus.TRACER_INVALID_PASSWORD, new KeyValuePair<string, string>("Tracing session failed: invalid root password. Run the game as a root or pass a valid password through Optick GUI", "https://github.com/bombomby/optick/wiki/Event-Tracing-for-Windows"));
statusToError.Add(TracerStatus.TRACER_NOT_IMPLEMENTED, new KeyValuePair<string, string>("Tracing sessions are not supported yet on the selected platform! Stay tuned!", "https://github.com/bombomby/optick"));


statusToError.Add(ETWStatus.ETW_ERROR_ALREADY_EXISTS, new KeyValuePair<string, string>("ETW session already started (Reboot should help)", "https://github.com/bombomby/optick/wiki/Event-Tracing-for-Windows"));
statusToError.Add(ETWStatus.ETW_FAILED, new KeyValuePair<string, string>("ETW session failed (Run your Game or Visual Studio as Administrator to get ETW data)", "https://github.com/bombomby/optick/wiki/Event-Tracing-for-Windows"));
statusToError.Add(ETWStatus.TRACER_INVALID_PASSWORD, new KeyValuePair<string, string>("Tracing session failed: invalid root password. Run the game as a root or pass a valid password through Optick GUI", "https://github.com/bombomby/optick/wiki/Event-Tracing-for-Windows"));

ProfilerClient.Get().ConnectionChanged += TimeLine_ConnectionChanged;
ProfilerClient.Get().ConnectionChanged += TimeLine_ConnectionChanged;

socketThread = new Thread(RecieveMessage);
socketThread.Start();
Expand Down Expand Up @@ -148,16 +147,17 @@ public override string ToString()
}
}

enum ETWStatus
enum TracerStatus
{
ETW_OK = 0,
ETW_ERROR_ALREADY_EXISTS = 1,
ETW_ERROR_ACCESS_DENIED = 2,
ETW_FAILED = 3,
TRACER_OK = 0,
TRACER_ERROR_ALREADY_EXISTS = 1,
TRACER_ERROR_ACCESS_DENIED = 2,
TRACER_FAILED = 3,
TRACER_INVALID_PASSWORD = 4,
TRACER_NOT_IMPLEMENTED = 5,
}

Dictionary<ETWStatus, KeyValuePair<String, String>> statusToError = new Dictionary<ETWStatus, KeyValuePair<String, String>>();
Dictionary<TracerStatus, KeyValuePair<String, String>> statusToError = new Dictionary<TracerStatus, KeyValuePair<String, String>>();

private bool ApplyResponse(DataResponse response)
{
Expand All @@ -183,7 +183,7 @@ private bool ApplyResponse(DataResponse response)
break;

case DataResponse.Type.Handshake:
ETWStatus status = (ETWStatus)response.Reader.ReadUInt32();
TracerStatus status = (TracerStatus)response.Reader.ReadUInt32();

KeyValuePair<string, string> warning;
if (statusToError.TryGetValue(status, out warning))
Expand Down
1 change: 1 addition & 0 deletions gui/ViewModels/AddressBarViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ public ConnectionVM(Platform.Connection con)
Target = con.Target;
Port = con.Port;
Password = con.Password;
CanDelete = true;
}

public Platform.Connection GetConnection()
Expand Down
12 changes: 9 additions & 3 deletions samples/ConsoleApp/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,13 @@ int main()

cout << "Starting main loop update." << endl;

OPTICK_SET_MEMORY_ALLOCATOR([](size_t size) -> void* { return operator new(size); }, [](void* p) { operator delete(p); });

while( true )
OPTICK_SET_MEMORY_ALLOCATOR(
[](size_t size) -> void* { return operator new(size); },
[](void* p) { operator delete(p); },
[]() { /* Do some TLS initialization here if needed */ });

bool needExit = false;
while( !needExit )
{
OPTICK_FRAME("MainThread");

Expand All @@ -39,6 +43,8 @@ int main()
cout<<'.'<<flush;
}

OPTICK_SHUTDOWN();

return 0;
}

Expand Down
24 changes: 18 additions & 6 deletions src/optick.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@
# define OPTICK_OSX (1)
# elif defined(__linux__)
# define OPTICK_LINUX (1)
# elif defined(__ORBIS__)
# define OPTICK_PS4 (1)
# elif defined(__FreeBSD__)
# define OPTICK_FREEBSD (1)
# endif
#elif defined(_MSC_VER)
# define OPTICK_MSVC (1)
# if defined(_DURANGO)
# define OPTICK_XBOX (1)
# define OPTICK_PC (0)
# else
# define OPTICK_PC (1)
#endif
Expand Down Expand Up @@ -662,8 +662,10 @@ OPTICK_API const EventDescription* GetFrameDescription(FrameType::Type frame);
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
typedef void* (*AllocateFn)(size_t);
typedef void (*DeallocateFn)(void*);
typedef void (*InitThreadCb)(void);
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
OPTICK_API void SetAllocator(AllocateFn allocateFn, DeallocateFn deallocateFn);
OPTICK_API void SetAllocator(AllocateFn allocateFn, DeallocateFn deallocateFn, InitThreadCb initThreadCb);
OPTICK_API void Shutdown();
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
}

Expand Down Expand Up @@ -835,10 +837,19 @@ OPTICK_API void SetAllocator(AllocateFn allocateFn, DeallocateFn deallocateFn);

// Registers custom memory allocator within Optick core
// Example:
// OPTICK_SET_MEMORY_ALLOCATOR([](size_t size) -> void* { return operator new(size); }, [](void* p) { operator delete(p); });
// OPTICK_SET_MEMORY_ALLOCATOR([](size_t size) -> void* { return operator new(size); }, [](void* p) { operator delete(p); }, nullptr);
// Params:
// INIT_THREAD_CALLBACK - callback for internal Optick threads (useful if you need to setup some TLS variables related to the memory allocator for your thread)
// Notes:
// Should be called before the first call to OPTICK_FRAME
#define OPTICK_SET_MEMORY_ALLOCATOR(ALLOCATE_FUNCTION, DEALLOCATE_FUNCTION) ::Optick::SetAllocator(ALLOCATE_FUNCTION, DEALLOCATE_FUNCTION);
// Allocation and deallocation functions should be thread-safe - Optick doesn't do any synchronization for these calls
#define OPTICK_SET_MEMORY_ALLOCATOR(ALLOCATE_FUNCTION, DEALLOCATE_FUNCTION, INIT_THREAD_CALLBACK) ::Optick::SetAllocator(ALLOCATE_FUNCTION, DEALLOCATE_FUNCTION, INIT_THREAD_CALLBACK);

// Shutdown
// Clears all the internal buffers allocated by Optick
// Notes:
// You shouldn't call any Optick functions after shutting down the system (it might lead to the undefined behaviour)
#define OPTICK_SHUTDOWN() ::Optick::Shutdown();

// GPU events
#define OPTICK_GPU_INIT_D3D12(DEVICE, CMD_QUEUES, NUM_CMD_QUEUS) ::Optick::InitGpuD3D12(DEVICE, CMD_QUEUES, NUM_CMD_QUEUS);
Expand Down Expand Up @@ -880,6 +891,7 @@ OPTICK_API void SetAllocator(AllocateFn allocateFn, DeallocateFn deallocateFn);
#define OPTICK_STORAGE_POP(STORAGE, CPU_TIMESTAMP_FINISH)
#define OPTICK_SET_STATE_CHANGED_CALLBACK(CALLBACK)
#define OPTICK_SET_MEMORY_ALLOCATOR(ALLOCATE_FUNCTION, DEALLOCATE_FUNCTION)
#define OPTICK_SHUTDOWN()
#define OPTICK_GPU_INIT_D3D12(DEVICE, CMD_QUEUES, NUM_CMD_QUEUS)
#define OPTICK_GPU_INIT_VULKAN(DEVICES, PHYSICAL_DEVICES, CMD_QUEUES, CMD_QUEUES_FAMILY, NUM_CMD_QUEUS)
#define OPTICK_GPU_CONTEXT(...)
Expand Down
43 changes: 31 additions & 12 deletions src/optick_core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,14 @@
//////////////////////////////////////////////////////////////////////////
#if defined(OPTICK_MSVC)
#include "optick_core.win.h"
#endif
#if defined(OPTICK_LINUX)
#elif defined(OPTICK_LINUX)
#include "optick_core.linux.h"
#endif
#if defined(OPTICK_OSX)
#elif defined(OPTICK_OSX)
#include "optick_core.macos.h"
#endif
#if defined(OPTICK_PS4)
#elif defined(OPTICK_PS4)
#include "optick_core.ps4.h"
#elif defined(OPTICK_FREEBSD)
#include "optick_core.freebsd.h"
#endif
//////////////////////////////////////////////////////////////////////////
// End of the Platform-specific stuff
Expand All @@ -40,8 +39,9 @@ extern "C" Optick::EventData* NextEvent()
namespace Optick
{
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void* (*Memory::allocate)(size_t) = operator new;
void (*Memory::deallocate)(void* p) = operator delete;
void* (*Memory::allocate)(size_t) = [](size_t size)->void* { return operator new(size); };
void (*Memory::deallocate)(void* p) = [](void* p) { operator delete(p); };
void (*Memory::initThread)(void) = nullptr;
std::atomic<uint64_t> Memory::memAllocated;
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
uint64_t MurmurHash64A(const void * key, int len, uint64_t seed)
Expand Down Expand Up @@ -419,6 +419,13 @@ const EventDescriptionList& EventDescriptionBoard::GetEvents() const
return boardDescriptions;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void EventDescriptionBoard::Shutdown()
{
boardDescriptions.Clear(false);
sharedNames.Clear(false);
sharedDescriptions.clear();
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
EventDescription* EventDescriptionBoard::CreateDescription(const char* name, const char* file /*= nullptr*/, uint32_t line /*= 0*/, uint32_t color /*= Color::Null*/, uint32_t filter /*= 0*/)
{
std::lock_guard<std::mutex> lock(GetBoardLock());
Expand Down Expand Up @@ -1177,7 +1184,7 @@ void Core::Activate(Mode::Type mode)

if (mode != Mode::OFF)
{
CaptureStatus::Type status = CaptureStatus::ERR_TRACER_FAILED;
CaptureStatus::Type status = CaptureStatus::ERR_TRACER_NOT_IMPLEMENTED;

#if OPTICK_ENABLE_TRACING
if (mode & Mode::TRACER)
Expand Down Expand Up @@ -1418,7 +1425,7 @@ const EventDescription* Core::GetFrameDescription(FrameType::Type frame) const
return frameDescriptions[frame];
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Core::~Core()
void Core::Shutdown()
{
std::lock_guard<std::recursive_mutex> lock(threadsLock);

Expand All @@ -1433,6 +1440,13 @@ Core::~Core()
Memory::Delete(*it);
}
fibers.clear();

EventDescriptionBoard::Get().Shutdown();
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Core::~Core()
{
Shutdown();
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
const vector<ThreadEntry*>& Core::GetThreads() const
Expand Down Expand Up @@ -1586,9 +1600,14 @@ OPTICK_API const EventDescription* GetFrameDescription(FrameType::Type frame)
return Core::Get().GetFrameDescription(frame);
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
OPTICK_API void SetAllocator(AllocateFn allocateFn, DeallocateFn deallocateFn)
OPTICK_API void SetAllocator(AllocateFn allocateFn, DeallocateFn deallocateFn, InitThreadCb initThreadCb)
{
Memory::SetAllocator(allocateFn, deallocateFn, initThreadCb);
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
OPTICK_API void Shutdown()
{
Memory::SetAllocator(allocateFn, deallocateFn);
Core::Get().Shutdown();
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
EventStorage::EventStorage(): currentMode(Mode::OFF), pushPopEventStackIndex(0), isFiberStorage(false)
Expand Down
55 changes: 55 additions & 0 deletions src/optick_core.freebsd.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#pragma once
#if defined(__FreeBSD__)

#include "optick.config.h"
#if USE_OPTICK

#include "optick_core.platform.h"

#include <sys/time.h>
#include <sys/types.h>
#include <pthread.h>
#include <unistd.h>

namespace Optick
{
const char* Platform::GetName()
{
return "PS4";
}

ThreadID Platform::GetThreadID()
{
return (uint64_t)pthread_self();
}

ProcessID Platform::GetProcessID()
{
return (ProcessID)getpid();
}

int64 Platform::GetFrequency()
{
return 1000000000;
}

int64 Platform::GetTime()
{
struct timespec ts;
clock_gettime(CLOCK_REALTIME, &ts);
return ts.tv_sec * 1000000000LL + ts.tv_nsec;
}

Trace* Platform::CreateTrace()
{
return nullptr;
}

SymbolEngine* Platform::CreateSymbolEngine()
{
return nullptr;
}
}

#endif //USE_OPTICK
#endif //__FreeBSD__
6 changes: 6 additions & 0 deletions src/optick_core.h
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,8 @@ class EventDescriptionBoard

const EventDescriptionList& GetEvents() const;

void Shutdown();

friend OutputDataStream& operator << (OutputDataStream& stream, const EventDescriptionBoard& ob);
};
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -391,6 +393,7 @@ struct CaptureStatus
ERR_TRACER_ACCESS_DENIED = 2,
ERR_TRACER_FAILED = 3,
ERR_TRACER_INVALID_PASSWORD = 4,
ERR_TRACER_NOT_IMPLEMENTED = 5,
};
};
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -555,6 +558,9 @@ class Core
// Returns Frame Description
const EventDescription* GetFrameDescription(FrameType::Type frame) const;

// Full Destruction
void Shutdown();

// NOT Thread Safe singleton (performance)
static Core& Get();

Expand Down
Loading

0 comments on commit b97a2c3

Please sign in to comment.