Skip to content

Commit

Permalink
CpuUsage
Browse files Browse the repository at this point in the history
  • Loading branch information
jhanssen committed Sep 4, 2014
1 parent b9e57d1 commit d562b68
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 0 deletions.
1 change: 1 addition & 0 deletions rct.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ set(RCT_SOURCES
${CMAKE_CURRENT_LIST_DIR}/rct/Buffer.cpp
${CMAKE_CURRENT_LIST_DIR}/rct/Config.cpp
${CMAKE_CURRENT_LIST_DIR}/rct/Connection.cpp
${CMAKE_CURRENT_LIST_DIR}/rct/CpuUsage.cpp
${CMAKE_CURRENT_LIST_DIR}/rct/EventLoop.cpp
${CMAKE_CURRENT_LIST_DIR}/rct/FileSystemWatcher.cpp
${CMAKE_CURRENT_LIST_DIR}/rct/Log.cpp
Expand Down
95 changes: 95 additions & 0 deletions rct/CpuUsage.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
#include "CpuUsage.h"
#include "Rct.h"
#include <thread>
#include <mutex>
#include <unistd.h>
#include <assert.h>

#define SLEEP_TIME 1000000 // one second

struct CpuData
{
std::mutex mutex;
std::thread thread;

uint32_t lastUsage;
uint64_t lastTime;

float usage;

#ifdef OS_Linux
float hz;
uint32_t cores;
#endif
};

static CpuData sData;
static std::once_flag sFlag;

static int64_t currentUsage()
{
#ifdef OS_Linux
FILE* f = fopen("/proc/stat", "r");
if (!f)
return -1;
char cpu[20];
uint32_t user, nice, system, idle;
if (fscanf(f, "%s\t%u\t%u\t%u\t%u\t", cpu, &user, &nice, &system, &idle) != 5) {
fclose(f);
return -1;
}
fclose(f);
return idle;
#else
#warning "CpuUsage not implemented for this platform"
return -1;
#endif
}

static void collectData()
{
for (;;) {
const int64_t usage = currentUsage();
if (usage == -1)
break;
const uint64_t time = Rct::monoMs();

{
std::lock_guard<std::mutex> locker(sData.mutex);
assert(sData.lastTime < time);
if (sData.lastTime > 0) {
// did we wrap? if so, make load be 1 for now
if (sData.lastUsage > usage) {
sData.usage = 0;
} else {
const uint32_t deltaUsage = usage - sData.lastUsage;
const uint64_t deltaTime = time - sData.lastTime;
const float timeRatio = deltaTime / (SLEEP_TIME / 1000);
sData.usage = (deltaUsage / sData.hz / sData.cores) / timeRatio;
}
}
sData.lastUsage = usage;
sData.lastTime = time;
}

usleep(SLEEP_TIME);
}
}

float CpuUsage::usage()
{
std::call_once(sFlag, []() {
std::lock_guard<std::mutex> locker(sData.mutex);
sData.usage = 0;
sData.lastUsage = 0;
sData.lastTime = 0;
#ifdef OS_Linux
sData.hz = sysconf(_SC_CLK_TCK);
sData.cores = sysconf(_SC_NPROCESSORS_ONLN);
#endif
sData.thread = std::thread(collectData);
});

std::lock_guard<std::mutex> locker(sData.mutex);
return 1. - sData.usage;
}
19 changes: 19 additions & 0 deletions rct/CpuUsage.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#ifndef CPUUSAGE_H
#define CPUUSAGE_H

#include <cstdint>

// CPU usage over the last couple of seconds, range from 0 (idle) to 1 (100%)

class CpuUsage
{
public:
static float usage();

private:
CpuUsage() = delete;
CpuUsage(const CpuUsage&) = delete;
CpuUsage& operator=(const CpuUsage&) = delete;
};

#endif

0 comments on commit d562b68

Please sign in to comment.