Skip to content

Commit

Permalink
be less crazy with getrusage
Browse files Browse the repository at this point in the history
  • Loading branch information
Eric Robert committed Jul 15, 2015
1 parent a25defb commit f1453a5
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 9 deletions.
26 changes: 17 additions & 9 deletions service/endpoint.cc
Original file line number Diff line number Diff line change
Expand Up @@ -660,16 +660,24 @@ void
EndpointBase::
doMinLatencyPolling(int threadNum, int numThreads)
{
int epoch = 0;

while (!shutdown_) {
// query the kernel for performance metrics
rusage now;
getrusage(RUSAGE_THREAD, &now);

// if we're just started, assume we know nothing and don't update the usage
long s = now.ru_utime.tv_sec+now.ru_stime.tv_sec;
if(s > 1) {
std::lock_guard<std::mutex> guard(usageLock);
resourceUsage[threadNum] = now;
// sync with the loop monitor request
int i = resourceEpoch;
if(i != epoch) {
// query the kernel for performance metrics
rusage now;
getrusage(RUSAGE_THREAD, &now);

// if we're just started, assume we know nothing and don't update the usage
long s = now.ru_utime.tv_sec+now.ru_stime.tv_sec;
if(s > 1) {
std::lock_guard<std::mutex> guard(usageLock);
resourceUsage[threadNum] = now;
}

epoch = i;
}

handleEvents(0, 1, handleEvent);
Expand Down
3 changes: 3 additions & 0 deletions service/endpoint.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "soa/service/epoller.h"
#include <map>
#include <mutex>
#include <atomic>


namespace Datacratic {
Expand Down Expand Up @@ -108,6 +109,7 @@ struct EndpointBase : public Epoller {
*/
std::vector<double> totalSleepSeconds() const { return totalSleepTime; }
std::vector<rusage> getResourceUsage() const {
resourceEpoch++;
std::vector<rusage> result;
std::lock_guard<std::mutex> guard(usageLock);
result = resourceUsage;
Expand Down Expand Up @@ -280,6 +282,7 @@ struct EndpointBase : public Epoller {
std::vector<double> totalSleepTime;
std::vector<rusage> resourceUsage;
mutable std::mutex usageLock;
mutable std::atomic<int> resourceEpoch;

/** Run a thread to handle events. */
void runEventThread(int threadNum, int numThreads);
Expand Down

0 comments on commit f1453a5

Please sign in to comment.