Measure CPU utilization in ServiceQueue

This commit is contained in:
Vinnie Falco
2013-10-04 11:46:10 -07:00
parent ca47d72aee
commit 93e9d8622e
6 changed files with 46 additions and 36 deletions

View File

@@ -337,7 +337,6 @@
<ClInclude Include="..\..\modules\beast_core\threads\TimeSliceThread.h" />
<ClInclude Include="..\..\modules\beast_core\thread\DeadlineTimer.h" />
<ClInclude Include="..\..\modules\beast_core\thread\Semaphore.h" />
<ClInclude Include="..\..\modules\beast_core\thread\ServiceQueue.h" />
<ClInclude Include="..\..\modules\beast_core\thread\Stoppable.h" />
<ClInclude Include="..\..\modules\beast_core\thread\Workers.h" />
<ClInclude Include="..\..\modules\beast_core\thread\detail\ScopedLock.h" />
@@ -1190,12 +1189,6 @@
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">true</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|x64'">true</ExcludedFromBuild>
</ClCompile>
<ClCompile Include="..\..\modules\beast_core\thread\ServiceQueue.cpp">
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">true</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">true</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">true</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|x64'">true</ExcludedFromBuild>
</ClCompile>
<ClCompile Include="..\..\modules\beast_core\thread\Stoppable.cpp">
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">true</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">true</ExcludedFromBuild>

View File

@@ -1139,9 +1139,6 @@
<ClInclude Include="..\..\modules\beast_asio\async\AbstractHandler.h">
<Filter>beast_asio\async</Filter>
</ClInclude>
<ClInclude Include="..\..\modules\beast_core\thread\ServiceQueue.h">
<Filter>beast_core\thread</Filter>
</ClInclude>
<ClInclude Include="..\..\beast\Chrono.h">
<Filter>beast</Filter>
</ClInclude>
@@ -1757,9 +1754,6 @@
<ClCompile Include="..\..\beast\crypto\impl\Sha256.cpp">
<Filter>beast\crypto\impl</Filter>
</ClCompile>
<ClCompile Include="..\..\modules\beast_core\thread\ServiceQueue.cpp">
<Filter>beast_core\thread</Filter>
</ClCompile>
<ClCompile Include="..\..\beast\chrono\impl\RelativeTime.cpp">
<Filter>beast\chrono\impl</Filter>
</ClCompile>

View File

@@ -51,22 +51,6 @@ private:
CPUMeter* m_meter;
};
public:
/** The type of container that measures idle time. */
typedef ScopedTimeInterval <MeasureIdle> ScopedIdleTime;
typedef ScopedTimeInterval <MeasureActive> ScopedActiveTime;
/** Returns the fraction of time that the CPU is being used. */
double getCpuUsage () const
{
SharedState::ConstAccess state (m_state);
double const seconds (state->usage.seconds());
if (seconds > 0)
return (state->usage.active.inSeconds() / seconds);
return 0;
}
private:
enum
{
// The amount of time an aggregate must accrue before a swap
@@ -152,6 +136,21 @@ private:
state->front().active += interval;
state->update();
}
public:
/** The type of container that measures idle time. */
typedef ScopedTimeInterval <MeasureIdle> ScopedIdleTime;
typedef ScopedTimeInterval <MeasureActive> ScopedActiveTime;
/** Returns the fraction of time that the CPU is being used. */
double getUtilizaton () const
{
SharedState::ConstAccess state (m_state);
double const seconds (state->usage.seconds());
if (seconds > 0)
return (state->usage.active.inSeconds() / seconds);
return 0;
}
};
}

View File

@@ -41,7 +41,7 @@ public:
/** Create the measurement with UnaryFunction constructed from one argument. */
template <typename Arg>
explicit ScopedTimeInterval (Arg arg)
explicit ScopedTimeInterval (Arg& arg)
: m_func (arg)
, m_start (RelativeTime::fromStartup ())
{

View File

@@ -20,6 +20,7 @@
#ifndef BEAST_THREAD_SERVICEQUEUE_H_INCLUDED
#define BEAST_THREAD_SERVICEQUEUE_H_INCLUDED
#include "../chrono/CPUMeter.h"
#include "../intrusive/List.h"
#include "../intrusive/LockFreeStack.h"
#include "SharedData.h"
@@ -390,6 +391,7 @@ protected:
typedef SharedData <State> SharedState;
SharedState m_state;
CPUMeter m_cpuMeter;
Atomic <int> m_stopped;
static ThreadLocalValue <ServiceQueueBase*> s_service;
@@ -459,6 +461,10 @@ public:
}
}
/** Returns the percentage of time the queue is using the CPU. */
double getUtilizaton () const
{ return m_cpuMeter.getUtilizaton(); }
/** Returns the allocator associated with the container. */
allocator_type get_allocator() const
{

View File

@@ -57,6 +57,8 @@ ServiceQueueBase::~ServiceQueueBase()
std::size_t ServiceQueueBase::poll ()
{
CPUMeter::ScopedActiveTime interval (m_cpuMeter);
std::size_t total (0);
ScopedServiceThread thread (this);
for (;;)
@@ -71,6 +73,8 @@ std::size_t ServiceQueueBase::poll ()
std::size_t ServiceQueueBase::poll_one ()
{
CPUMeter::ScopedActiveTime interval (m_cpuMeter);
ScopedServiceThread thread (this);
return dequeue();
}
@@ -81,8 +85,15 @@ std::size_t ServiceQueueBase::run ()
ScopedServiceThread thread (this);
while (! stopped())
{
total += poll ();
wait ();
{
CPUMeter::ScopedActiveTime interval (m_cpuMeter);
total += poll ();
}
{
CPUMeter::ScopedIdleTime interval (m_cpuMeter);
wait ();
}
}
return total;
}
@@ -93,10 +104,17 @@ std::size_t ServiceQueueBase::run_one ()
ScopedServiceThread (this);
for (;;)
{
n = poll_one();
if (n != 0)
break;
wait();
{
CPUMeter::ScopedActiveTime interval (m_cpuMeter);
n = poll_one();
if (n != 0)
break;
}
{
CPUMeter::ScopedIdleTime interval (m_cpuMeter);
wait();
}
}
return n;
}