Measure CPU usage in Workers

This commit is contained in:
Vinnie Falco
2013-10-22 17:09:58 -07:00
parent 8604e216eb
commit 1f97a239dc
4 changed files with 47 additions and 29 deletions

View File

@@ -143,9 +143,10 @@ public:
typedef ScopedTimeInterval <MeasureActive> ScopedActiveTime; typedef ScopedTimeInterval <MeasureActive> ScopedActiveTime;
/** Returns the fraction of time that the CPU is being used. */ /** Returns the fraction of time that the CPU is being used. */
double getUtilizaton () const double getUtilization () const
{ {
SharedState::ConstAccess state (m_state); SharedState::ConstAccess state (m_state);
double const seconds (state->usage.seconds()); double const seconds (state->usage.seconds());
if (seconds > 0) if (seconds > 0)
return (state->usage.active.inSeconds() / seconds); return (state->usage.active.inSeconds() / seconds);

View File

@@ -466,8 +466,8 @@ public:
} }
/** Returns the percentage of time the queue is using the CPU. */ /** Returns the percentage of time the queue is using the CPU. */
double getUtilizaton () const double getUtilization () const
{ return m_cpuMeter.getUtilizaton(); } { return m_cpuMeter.getUtilization(); }
/** Returns the allocator associated with the container. */ /** Returns the allocator associated with the container. */
allocator_type get_allocator() const allocator_type get_allocator() const

View File

@@ -111,6 +111,11 @@ int Workers::numberOfCurrentlyRunningTasks () const noexcept
return m_runningTaskCount.get (); return m_runningTaskCount.get ();
} }
double Workers::getUtilization () const
{
return m_usage.getUtilization();
}
void Workers::deleteWorkers (LockFreeStack <Worker>& stack) void Workers::deleteWorkers (LockFreeStack <Worker>& stack)
{ {
for (;;) for (;;)
@@ -157,36 +162,44 @@ void Workers::Worker::run ()
{ {
// Acquire a task or "internal task." // Acquire a task or "internal task."
// //
m_workers.m_semaphore.wait ();
// See if there's a pause request. This
// counts as an "internal task."
//
int pauseCount = m_workers.m_pauseCount.get ();
if (pauseCount > 0)
{ {
// Try to decrement CPUMeter::ScopedIdleTime elapsed (m_workers.m_usage);
pauseCount = --m_workers.m_pauseCount;
if (pauseCount >= 0) m_workers.m_semaphore.wait ();
{
// We got paused
break;
}
else
{
// Undo our decrement
++m_workers.m_pauseCount;
}
} }
// We couldn't pause so we must have gotten {
// unblocked in order to process a task. CPUMeter::ScopedActiveTime elapsed (m_workers.m_usage);
//
++m_workers.m_runningTaskCount; // See if there's a pause request. This
m_workers.m_callback.processTask (); // counts as an "internal task."
--m_workers.m_runningTaskCount; //
int pauseCount = m_workers.m_pauseCount.get ();
if (pauseCount > 0)
{
// Try to decrement
pauseCount = --m_workers.m_pauseCount;
if (pauseCount >= 0)
{
// We got paused
break;
}
else
{
// Undo our decrement
++m_workers.m_pauseCount;
}
}
// We couldn't pause so we must have gotten
// unblocked in order to process a task.
//
++m_workers.m_runningTaskCount;
m_workers.m_callback.processTask ();
--m_workers.m_runningTaskCount;
}
// Put the name back in case the callback changed it // Put the name back in case the callback changed it
Thread::setCurrentThreadName (m_threadName); Thread::setCurrentThreadName (m_threadName);

View File

@@ -93,6 +93,9 @@ public:
*/ */
int numberOfCurrentlyRunningTasks () const noexcept; int numberOfCurrentlyRunningTasks () const noexcept;
/** Returns the fraction of time that the CPU is being used. */
double getUtilization () const;
//-------------------------------------------------------------------------- //--------------------------------------------------------------------------
private: private:
@@ -129,6 +132,7 @@ private:
private: private:
Callback& m_callback; Callback& m_callback;
CPUMeter m_usage; // CPU utilization across threads
String m_threadNames; // The name to give each thread String m_threadNames; // The name to give each thread
WaitableEvent m_allPaused; // signaled when all threads paused WaitableEvent m_allPaused; // signaled when all threads paused
Semaphore m_semaphore; // each pending task is 1 resource Semaphore m_semaphore; // each pending task is 1 resource