Skip to content

Commit

Permalink
Performance monitor scope safepointers (#94)
Browse files Browse the repository at this point in the history
* internal warning/error fixes

* performance monitor scope safepointers

---------

Co-authored-by: Viger <[email protected]>
  • Loading branch information
Vigerus and Viger authored Oct 23, 2024
1 parent 6924046 commit 2da2255
Show file tree
Hide file tree
Showing 13 changed files with 148 additions and 180 deletions.
45 changes: 24 additions & 21 deletions playerbot/PerformanceMonitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,21 @@

PerformanceMonitor::PerformanceMonitor()
{

}

PerformanceMonitor::~PerformanceMonitor()
{

}

PerformanceMonitorOperation* PerformanceMonitor::start(PerformanceMetric metric, std::string name, PerformanceStack* stack)
std::unique_ptr<PerformanceMonitorOperation> PerformanceMonitor::start(PerformanceMetric metric, std::string name, PerformanceStack* stack)
{
if (!sPlayerbotAIConfig.perfMonEnabled) return NULL;
if (!sPlayerbotAIConfig.perfMonEnabled)
{
return { };
}

std::string stackName = name;


if (stack)
{
if (!stack->empty())
Expand All @@ -45,26 +45,25 @@ PerformanceMonitorOperation* PerformanceMonitor::start(PerformanceMetric metric,
data[metric][stackName] = pd;
}

return new PerformanceMonitorOperation(pd, name, stack);
return std::make_unique<PerformanceMonitorOperation>(pd, name, stack);
#endif
}

PerformanceMonitorOperation* PerformanceMonitor::start(PerformanceMetric metric, std::string name, PlayerbotAI* ai)
std::unique_ptr<PerformanceMonitorOperation> PerformanceMonitor::start(PerformanceMetric metric, std::string name, PlayerbotAI * ai)
{
if (!sPlayerbotAIConfig.perfMonEnabled) return NULL;

if(ai->GetAiObjectContext())
if (ai->GetAiObjectContext())
return start(metric, name, &ai->GetAiObjectContext()->performanceStack);
else
return start(metric, name);
}

void PerformanceMonitor::PrintStats(bool perTick, bool fullStack)
{
if(data.empty())
if (data.empty())
return;


uint32 total = 0;

if (!perTick)
Expand Down Expand Up @@ -272,18 +271,18 @@ void PerformanceMonitor::PrintStats(bool perTick, bool fullStack)

void PerformanceMonitor::Reset()
{
for (std::map<PerformanceMetric, std::map<std::string, PerformanceData*> >::iterator i = data.begin(); i != data.end(); ++i)
{
std::map<std::string, PerformanceData*> pdMap = i->second;
for (std::map<std::string, PerformanceData*>::iterator j = pdMap.begin(); j != pdMap.end(); ++j)
{
for (std::map<PerformanceMetric, std::map<std::string, PerformanceData*> >::iterator i = data.begin(); i != data.end(); ++i)
{
std::map<std::string, PerformanceData*> pdMap = i->second;
for (std::map<std::string, PerformanceData*>::iterator j = pdMap.begin(); j != pdMap.end(); ++j)
{
#ifdef CMANGOS
PerformanceData* pd = j->second;
std::lock_guard<std::mutex> guard(pd->lock);
pd->minTime = pd->maxTime = pd->totalTime = pd->count = 0;
PerformanceData* pd = j->second;
std::lock_guard<std::mutex> guard(pd->lock);
pd->minTime = pd->maxTime = pd->totalTime = pd->count = 0;
#endif
}
}
}
}
}

PerformanceMonitorOperation::PerformanceMonitorOperation(PerformanceData* data, std::string name, PerformanceStack* stack) : data(data), name(name), stack(stack)
Expand All @@ -293,6 +292,11 @@ PerformanceMonitorOperation::PerformanceMonitorOperation(PerformanceData* data,
#endif
}

PerformanceMonitorOperation::~PerformanceMonitorOperation()
{
finish();
}

void PerformanceMonitorOperation::finish()
{
#ifdef CMANGOS
Expand All @@ -313,7 +317,6 @@ void PerformanceMonitorOperation::finish()
{
stack->erase(std::remove(stack->begin(), stack->end(), name), stack->end());
}
delete this;
}

bool ChatHandler::HandlePerfMonCommand(char* args)
Expand Down
16 changes: 10 additions & 6 deletions playerbot/PerformanceMonitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,11 @@ class PerformanceMonitorOperation
{
public:
PerformanceMonitorOperation(PerformanceData* data, std::string name, PerformanceStack* stack);
void finish();
~PerformanceMonitorOperation();

private:
void finish();

PerformanceData* data;
std::string name;
PerformanceStack* stack;
Expand All @@ -54,15 +56,17 @@ class PerformanceMonitor
return instance;
}

public:
PerformanceMonitorOperation* start(PerformanceMetric metric, std::string name, PerformanceStack* stack = nullptr);
PerformanceMonitorOperation* start(PerformanceMetric metric, std::string name, PlayerbotAI* ai);
public:
std::unique_ptr<PerformanceMonitorOperation> start(PerformanceMetric metric, std::string name, PerformanceStack* stack = nullptr);
std::unique_ptr<PerformanceMonitorOperation> start(PerformanceMetric metric, std::string name, PlayerbotAI* ai);
void PrintStats(bool perTick = false, bool fullStack = false);
void Reset();
private:

private:
std::map<PerformanceMetric, std::map<std::string, PerformanceData*> > data;

#ifdef CMANGOS
std::mutex lock;
std::mutex lock;
#endif
};

Expand Down
13 changes: 6 additions & 7 deletions playerbot/PlayerbotAI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,8 @@ PlayerbotAI::~PlayerbotAI()
void PlayerbotAI::UpdateAI(uint32 elapsed, bool minimal)
{
std::string mapString = WorldPosition(bot).isOverworld() ? std::to_string(bot->GetMapId()) : "I";
PerformanceMonitorOperation* pmo = sPerformanceMonitor.start(PERF_MON_TOTAL, "PlayerbotAI::UpdateAI " + mapString);
auto pmo = sPerformanceMonitor.start(PERF_MON_TOTAL, "PlayerbotAI::UpdateAI " + mapString);

if(aiInternalUpdateDelay > elapsed)
{
aiInternalUpdateDelay -= elapsed;
Expand Down Expand Up @@ -513,7 +514,6 @@ void PlayerbotAI::UpdateAI(uint32 elapsed, bool minimal)
// Cancel the update if the new delay increased
if (!CanUpdateAIInternal())
{
if (pmo) pmo->finish();
return;
}
}
Expand All @@ -530,16 +530,16 @@ void PlayerbotAI::UpdateAI(uint32 elapsed, bool minimal)

YieldAIInternalThread(min);
}
if (pmo) pmo->finish();
}

bool PlayerbotAI::UpdateAIReaction(uint32 elapsed, bool minimal, bool isStunned)
{
bool reactionFound;
std::string mapString = WorldPosition(bot).isOverworld() ? std::to_string(bot->GetMapId()) : "I";
PerformanceMonitorOperation* pmo = sPerformanceMonitor.start(PERF_MON_TOTAL, "PlayerbotAI::UpdateAIReaction " + mapString);

auto pmo = sPerformanceMonitor.start(PERF_MON_TOTAL, "PlayerbotAI::UpdateAIReaction " + mapString);
const bool reactionInProgress = reactionEngine->Update(elapsed, minimal, isStunned, reactionFound);
if (pmo) pmo->finish();
pmo.reset();

if(reactionFound)
{
Expand Down Expand Up @@ -1038,7 +1038,7 @@ void PlayerbotAI::UpdateAIInternal(uint32 elapsed, bool minimal)
return;

std::string mapString = WorldPosition(bot).isOverworld() ? std::to_string(bot->GetMapId()) : "I";
PerformanceMonitorOperation* pmo = sPerformanceMonitor.start(PERF_MON_TOTAL, "PlayerbotAI::UpdateAIInternal " + mapString);
auto pmo = sPerformanceMonitor.start(PERF_MON_TOTAL, "PlayerbotAI::UpdateAIInternal " + mapString);

ExternalEventHelper helper(aiObjectContext);

Expand Down Expand Up @@ -1105,7 +1105,6 @@ void PlayerbotAI::UpdateAIInternal(uint32 elapsed, bool minimal)
masterOutgoingPacketHandlers.Handle(helper);

DoNextAction(minimal);
if (pmo) pmo->finish();
}

void PlayerbotAI::HandleTeleportAck()
Expand Down
4 changes: 1 addition & 3 deletions playerbot/PlayerbotAIBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@ void PlayerbotAIBase::UpdateAIInternal(uint32 elapsed, bool minimal)

void PlayerbotAIBase::UpdateAI(uint32 elapsed)
{
if (totalPmo)
totalPmo->finish();

totalPmo.reset();
totalPmo = sPerformanceMonitor.start(PERF_MON_TOTAL, "PlayerbotAIBase::FullTick");

if (aiInternalUpdateDelay > elapsed)
Expand Down
7 changes: 5 additions & 2 deletions playerbot/PlayerbotAIBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ class PlayerbotMgr;
class ChatHandler;
class PerformanceMonitorOperation;

#include <memory>

class PlayerbotAIBase
{
public:
PlayerbotAIBase();
PlayerbotAIBase();

public:
bool IsActive() const;
Expand All @@ -26,5 +28,6 @@ class PlayerbotAIBase

protected:
uint32 aiInternalUpdateDelay;
PerformanceMonitorOperation* totalPmo = nullptr;

std::unique_ptr<PerformanceMonitorOperation> totalPmo;
};
Loading

0 comments on commit 2da2255

Please sign in to comment.