尝试同步

This commit is contained in:
2025-11-27 16:46:51 +08:00
parent 6818de280b
commit 4ad23a3800
5 changed files with 51 additions and 610 deletions

View File

@@ -15,13 +15,8 @@
#include "tracy/Tracy.hpp"
#include <string>
#include <cstring>
#ifdef ADVANCED_ZONE_MANAGEMENT
#include <unordered_map>
#include <map>
#include <stack>
#include <mutex>
#include <thread>
#endif
// Platform specific export definition
#if defined(_WIN32) || defined(_WIN64)
@@ -32,22 +27,10 @@
#define UNITY_PLUGIN_EXPORT
#endif
// Simple Zone Manager for basic Zone tracking
#ifndef ADVANCED_ZONE_MANAGEMENT
namespace {
struct SimpleZone {
const char* name;
int64_t startTime;
};
struct ThreadZones {
std::stack<SimpleZone> zones;
};
std::unordered_map<std::thread::id, ThreadZones> g_threadZones;
std::mutex g_zonesMutex;
}
#endif
std::map<std::string, tracy::SourceLocationData> sourceLocationCache;
std::map<std::string, std::string> zoneFunctionNameCache;
std::map<std::string, std::string> zoneFilePathCache;
std::map<std::string, std::stack<tracy::ScopedZone*>> zoneStackCache;
// C export functions (Unity requires C linkage)
extern "C"
@@ -67,10 +50,7 @@ extern "C"
*/
UNITY_PLUGIN_EXPORT void TracyShutdown()
{
#ifndef ADVANCED_ZONE_MANAGEMENT
std::lock_guard<std::mutex> lock(g_zonesMutex);
g_threadZones.clear();
#endif
}
/**
@@ -118,51 +98,6 @@ extern "C"
}
}
/**
* Begin a named Zone
* Unity C# call: [DllImport] private static extern void TracyZoneBegin(string name);
*
* Note: This is a simplified implementation. For production use, consider using
* ADVANCED_ZONE_MANAGEMENT or a different Zone management approach.
*/
UNITY_PLUGIN_EXPORT void TracyZoneBegin(const char* name)
{
#ifndef ADVANCED_ZONE_MANAGEMENT
if (name != nullptr)
{
std::lock_guard<std::mutex> lock(g_zonesMutex);
auto threadId = std::this_thread::get_id();
SimpleZone zone;
zone.name = name;
zone.startTime = tracy::GetTime();
g_threadZones[threadId].zones.push(zone);
// Send zone begin event to Tracy
TracyMessageL(name);
}
#endif
}
/**
* End current Zone
* Unity C# call: [DllImport] private static extern void TracyZoneEnd();
*/
UNITY_PLUGIN_EXPORT void TracyZoneEnd()
{
#ifndef ADVANCED_ZONE_MANAGEMENT
std::lock_guard<std::mutex> lock(g_zonesMutex);
auto threadId = std::this_thread::get_id();
auto it = g_threadZones.find(threadId);
if (it != g_threadZones.end() && !it->second.zones.empty())
{
it->second.zones.pop();
}
#endif
}
/**
* Unity plugin lifecycle function - called on load
*/
@@ -180,117 +115,33 @@ extern "C"
TracyShutdown();
}
} // extern "C"
/*
* Advanced Implementation Example - Zone Management
*
* Below is a more complete Zone management implementation example
* Can be extended as needed
*/
#ifdef ADVANCED_ZONE_MANAGEMENT
// Zone Manager (Thread-safe)
class ZoneManager
{
private:
struct ThreadZones
UNITY_PLUGIN_EXPORT void TracyZoneScopedBegin(
const char* name,
const char* functionName,
const char* filePath,
int lineNumber
)
{
std::stack<tracy::ScopedZone*> zones;
};
std::unordered_map<std::thread::id, ThreadZones> threadZones;
std::mutex mutex;
public:
void BeginZone(const char* name, const char* function, const char* file, uint32_t line)
{
std::lock_guard<std::mutex> lock(mutex);
auto threadId = std::this_thread::get_id();
auto& zones = threadZones[threadId].zones;
// Create source location info
static const tracy::SourceLocationData loc{name, function, file, line, 0};
// Create Zone (note: must be heap allocated)
auto* zone = new tracy::ScopedZone(&loc, true);
zones.push(zone);
}
void EndZone()
{
std::lock_guard<std::mutex> lock(mutex);
auto threadId = std::this_thread::get_id();
auto it = threadZones.find(threadId);
if (it != threadZones.end() && !it->second.zones.empty())
std::string nameStr(name);
auto iter = sourceLocationCache.find(nameStr);
if (iter == sourceLocationCache.end())
{
auto* zone = it->second.zones.top();
it->second.zones.pop();
delete zone;
zoneFunctionNameCache[nameStr] = functionName;
zoneFilePathCache[nameStr] = filePath;
iter = sourceLocationCache.insert(sourceLocationCache.end(), std::make_pair(nameStr, tracy::SourceLocationData{}));
iter->second.name = iter->first.c_str();
iter->second.function = zoneFunctionNameCache[nameStr].c_str();
iter->second.file = zoneFilePathCache[nameStr].c_str();
iter->second.line = static_cast<uint32_t>(lineNumber);
iter->second.color = 0;
}
zoneStackCache[nameStr].push(new tracy::ScopedZone(&iter->second, 0, true));
}
void ClearThread()
UNITY_PLUGIN_EXPORT void TracyZoneScopedEnd(const char* name)
{
std::lock_guard<std::mutex> lock(mutex);
auto threadId = std::this_thread::get_id();
auto it = threadZones.find(threadId);
if (it != threadZones.end())
{
// Clean up all unfinished Zones
while (!it->second.zones.empty())
{
delete it->second.zones.top();
it->second.zones.pop();
}
threadZones.erase(it);
}
delete zoneStackCache[name].top();
zoneStackCache[name].pop();
}
};
// Global Zone Manager instance
static ZoneManager g_zoneManager;
extern "C" {
UNITY_PLUGIN_EXPORT void TracyZoneBeginAdvanced(const char* name, const char* function, const char* file, int line)
{
g_zoneManager.BeginZone(name, function, file, static_cast<uint32_t>(line));
}
UNITY_PLUGIN_EXPORT void TracyZoneEndAdvanced()
{
g_zoneManager.EndZone();
}
UNITY_PLUGIN_EXPORT void TracyClearThreadZones()
{
g_zoneManager.ClearThread();
}
} // extern "C"
#endif // ADVANCED_ZONE_MANAGEMENT
/*
* Build and Deployment Instructions:
*
* Use CMake for all platforms (see CMakeLists.txt)
*
* Quick build:
* 1. mkdir build && cd build
* 2. cmake .. -DTRACY_ROOT="/path/to/tracy"
* 3. cmake --build . --config Release
*
* Deploy to Unity:
* - Copy compiled library to Unity project Assets/Plugins/ directory
* - Place in appropriate subdirectory based on platform
* - Configure Plugin Import Settings to match target platform
*/