flatten 20260225
This commit is contained in:
28
tjp/core/system_usage/CpuUsage.h
Normal file
28
tjp/core/system_usage/CpuUsage.h
Normal file
@@ -0,0 +1,28 @@
|
||||
// TJP COPYRIGHT HEADER
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <cstddef>
|
||||
|
||||
namespace tjp {
|
||||
namespace core {
|
||||
|
||||
struct CpuUsage {
|
||||
size_t total;
|
||||
size_t idle;
|
||||
} ;
|
||||
|
||||
CpuUsage getCpuUsage ();
|
||||
|
||||
inline
|
||||
CpuUsage operator-(const CpuUsage &lhs, const CpuUsage &rhs)
|
||||
{
|
||||
return CpuUsage {
|
||||
.total = lhs.total - rhs.total,
|
||||
.idle = lhs.idle - rhs.idle
|
||||
};
|
||||
}
|
||||
|
||||
} // namespace
|
||||
} // namespace
|
||||
|
||||
28
tjp/core/system_usage/NetworkUsage.h
Normal file
28
tjp/core/system_usage/NetworkUsage.h
Normal file
@@ -0,0 +1,28 @@
|
||||
// TJP COPYRIGHT HEADER
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <cstddef>
|
||||
|
||||
namespace tjp {
|
||||
namespace core {
|
||||
|
||||
struct NetworkUsage {
|
||||
size_t total;
|
||||
size_t used;
|
||||
} ;
|
||||
|
||||
NetworkUsage getNetworkUsage ();
|
||||
|
||||
inline
|
||||
NetworkUsage operator-(const NetworkUsage &lhs, const NetworkUsage &rhs)
|
||||
{
|
||||
return NetworkUsage {
|
||||
.total = lhs.total - rhs.total,
|
||||
.used = lhs.used - rhs.used
|
||||
};
|
||||
}
|
||||
|
||||
} // namespace
|
||||
} // namespace
|
||||
|
||||
60
tjp/core/system_usage/ProcessUsage.cpp
Normal file
60
tjp/core/system_usage/ProcessUsage.cpp
Normal file
@@ -0,0 +1,60 @@
|
||||
// TJP COPYRIGHT HEADER
|
||||
|
||||
#include "ProcessUsage.h"
|
||||
|
||||
#include <ctime>
|
||||
#include <limits>
|
||||
|
||||
#include <chrono>
|
||||
|
||||
#include <tjp/core/types/Types.h>
|
||||
|
||||
namespace tjp {
|
||||
namespace core {
|
||||
|
||||
template<typename T>
|
||||
T difference_wrapped(T l, T r)
|
||||
{
|
||||
auto max = std::numeric_limits<T>::max();
|
||||
|
||||
return
|
||||
(l >= r) ?
|
||||
l - r :
|
||||
max - r + l + 1;
|
||||
}
|
||||
|
||||
|
||||
ProcessUsage operator-(const ProcessUsage &lhs, const ProcessUsage &rhs)
|
||||
{
|
||||
auto ticks = difference_wrapped((clock_t)lhs.ticks, (clock_t)rhs.ticks);
|
||||
auto millis = difference_wrapped(lhs.total, rhs.total);
|
||||
|
||||
static_assert((u64)60 * 1000 * CLOCKS_PER_SEC < std::numeric_limits<u64>::max());
|
||||
|
||||
static_assert(std::numeric_limits<size_t>::max() >= std::numeric_limits<clock_t>::max());
|
||||
|
||||
return ProcessUsage {
|
||||
.ticks = (size_t)ticks,
|
||||
.total = millis * CLOCKS_PER_SEC / 1000
|
||||
} ;
|
||||
} ;
|
||||
|
||||
ProcessUsage getProcessUsage ()
|
||||
{
|
||||
static auto start = std::chrono::high_resolution_clock::now();
|
||||
auto now = std::chrono::high_resolution_clock::now();
|
||||
|
||||
auto totalTime = std::chrono::duration_cast<std::chrono::milliseconds>(now - start).count();
|
||||
|
||||
auto ticks = clock();
|
||||
|
||||
return ProcessUsage {
|
||||
.ticks = (size_t)ticks,
|
||||
.total = (size_t)totalTime
|
||||
} ;
|
||||
|
||||
}
|
||||
|
||||
} // namespace
|
||||
} // namespace
|
||||
|
||||
22
tjp/core/system_usage/ProcessUsage.h
Normal file
22
tjp/core/system_usage/ProcessUsage.h
Normal file
@@ -0,0 +1,22 @@
|
||||
// TJP COPYRIGHT HEADER
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <cstddef>
|
||||
|
||||
namespace tjp {
|
||||
namespace core {
|
||||
|
||||
struct ProcessUsage {
|
||||
size_t ticks;
|
||||
size_t total;
|
||||
} ;
|
||||
|
||||
ProcessUsage getProcessUsage ();
|
||||
|
||||
ProcessUsage operator-(const ProcessUsage &lhs, const ProcessUsage &rhs);
|
||||
|
||||
|
||||
} // namespace
|
||||
} // namespace
|
||||
|
||||
52
tjp/core/system_usage/SystemUsage.cpp
Normal file
52
tjp/core/system_usage/SystemUsage.cpp
Normal file
@@ -0,0 +1,52 @@
|
||||
// TJP COPYRIGHT HEADER
|
||||
|
||||
#include "SystemUsage.hpp"
|
||||
|
||||
#include <tjp/core/time/Time.h>
|
||||
#include <tjp/core/time/Time+chrono.hpp>
|
||||
#include <thread>
|
||||
#include <chrono>
|
||||
|
||||
namespace tjp {
|
||||
namespace core {
|
||||
|
||||
SystemUsage::SystemUsage ()
|
||||
{
|
||||
poll();
|
||||
}
|
||||
|
||||
void SystemUsage::poll ()
|
||||
{
|
||||
Poll poll {
|
||||
.time = time::now(),
|
||||
.cpu = getCpuUsage(),
|
||||
.network = getNetworkUsage(),
|
||||
.process = getProcessUsage()
|
||||
};
|
||||
|
||||
if (previous.time != time::Time::Zero)
|
||||
{
|
||||
usage.cpu = poll.cpu - previous.cpu;
|
||||
usage.network = poll.network - previous.network;
|
||||
usage.duration = poll.time - previous.time;
|
||||
usage.process = poll.process - previous.process;
|
||||
}
|
||||
|
||||
previous = poll;
|
||||
}
|
||||
|
||||
void SystemUsage::sleepAndPoll(time::Duration duration)
|
||||
{
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(duration.v));
|
||||
poll();
|
||||
}
|
||||
|
||||
SystemUsage::Poll SystemUsage::getUsage () const
|
||||
{
|
||||
return usage;
|
||||
}
|
||||
|
||||
|
||||
} // namespace
|
||||
} // namespace
|
||||
|
||||
12
tjp/core/system_usage/SystemUsage.h
Normal file
12
tjp/core/system_usage/SystemUsage.h
Normal file
@@ -0,0 +1,12 @@
|
||||
// TJP COPYRIGHT HEADER
|
||||
|
||||
#pragma once
|
||||
|
||||
namespace tjp {
|
||||
namespace core {
|
||||
|
||||
struct SystemUsage;
|
||||
|
||||
} // namespace
|
||||
} // namespace
|
||||
|
||||
35
tjp/core/system_usage/SystemUsage.hpp
Normal file
35
tjp/core/system_usage/SystemUsage.hpp
Normal file
@@ -0,0 +1,35 @@
|
||||
// TJP COPYRIGHT HEADER
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "SystemUsage.h"
|
||||
#include <tjp/core/time/Time.hpp>
|
||||
#include "CpuUsage.h"
|
||||
#include "NetworkUsage.h"
|
||||
#include "ProcessUsage.h"
|
||||
|
||||
namespace tjp {
|
||||
namespace core {
|
||||
|
||||
struct SystemUsage
|
||||
{
|
||||
struct Poll {
|
||||
time::Time time;
|
||||
CpuUsage cpu;
|
||||
NetworkUsage network;
|
||||
ProcessUsage process;
|
||||
time::Duration duration;
|
||||
};
|
||||
|
||||
Poll usage, previous;
|
||||
void poll();
|
||||
void sleepAndPoll(time::Duration duration = time::Milliseconds(10));
|
||||
|
||||
SystemUsage();
|
||||
|
||||
Poll getUsage () const;
|
||||
} ;
|
||||
|
||||
} // namespace
|
||||
} // namespace
|
||||
|
||||
36
tjp/core/system_usage/apple/CpuUsage.cpp
Normal file
36
tjp/core/system_usage/apple/CpuUsage.cpp
Normal file
@@ -0,0 +1,36 @@
|
||||
// TJP COPYRIGHT HEADER
|
||||
|
||||
#include "../CpuUsage.h"
|
||||
|
||||
#include <mach/mach_init.h>
|
||||
#include <mach/mach_error.h>
|
||||
#include <mach/mach_host.h>
|
||||
#include <mach/vm_map.h>
|
||||
|
||||
namespace tjp {
|
||||
namespace core {
|
||||
|
||||
CpuUsage getCpuUsage ()
|
||||
{
|
||||
host_cpu_load_info_data_t cpuinfo;
|
||||
mach_msg_type_number_t count = HOST_CPU_LOAD_INFO_COUNT;
|
||||
if (host_statistics(mach_host_self(), HOST_CPU_LOAD_INFO, (host_info_t)&cpuinfo, &count) == KERN_SUCCESS)
|
||||
{
|
||||
unsigned long long totalTicks = 0;
|
||||
for(int i=0; i<CPU_STATE_MAX; i++)
|
||||
totalTicks += cpuinfo.cpu_ticks[i];
|
||||
|
||||
auto idleTicks = cpuinfo.cpu_ticks[CPU_STATE_IDLE];
|
||||
|
||||
return {
|
||||
.total = totalTicks,
|
||||
.idle = idleTicks
|
||||
};
|
||||
}
|
||||
|
||||
return {0, 0};
|
||||
}
|
||||
|
||||
} // namespace
|
||||
} // namespace
|
||||
|
||||
15
tjp/core/system_usage/apple/NetworkUsage.cpp
Normal file
15
tjp/core/system_usage/apple/NetworkUsage.cpp
Normal file
@@ -0,0 +1,15 @@
|
||||
// TJP COPYRIGHT HEADER
|
||||
|
||||
#include "../NetworkUsage.h"
|
||||
|
||||
namespace tjp {
|
||||
namespace core {
|
||||
|
||||
NetworkUsage getNetworkUsage ()
|
||||
{
|
||||
return { 0, 0 };
|
||||
}
|
||||
|
||||
} // namespace
|
||||
} // namespace
|
||||
|
||||
54
tjp/core/system_usage/linux/CpuUsage.cpp
Normal file
54
tjp/core/system_usage/linux/CpuUsage.cpp
Normal file
@@ -0,0 +1,54 @@
|
||||
// TJP COPYRIGHT HEADER
|
||||
|
||||
#include "../CpuUsage.h"
|
||||
|
||||
#include <tjp/core/log/Log.h>
|
||||
#include <tjp/core/log/LogOf.h>
|
||||
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
|
||||
#include <tjp/core/string/split.hpp>
|
||||
#include <tjp/core/string/from_string.hpp>
|
||||
|
||||
namespace tjp {
|
||||
namespace core {
|
||||
|
||||
CpuUsage getCpuUsage ()
|
||||
{
|
||||
std::ifstream in("/proc/stat");
|
||||
|
||||
std::string line;
|
||||
|
||||
std::getline(in, line);
|
||||
std::istringstream iss(line);
|
||||
|
||||
std::string label;
|
||||
iss >> label;
|
||||
|
||||
std::vector<size_t> values;
|
||||
while (iss)
|
||||
{
|
||||
size_t value;
|
||||
iss >> value;
|
||||
|
||||
if (!iss.fail())
|
||||
values.push_back(value);
|
||||
}
|
||||
|
||||
sLogDebug("core::system_usage", "[" << line << "]" << logVar(values));
|
||||
|
||||
auto &idle = values[3];
|
||||
size_t total = 0;
|
||||
for (auto &v : values)
|
||||
total += v;
|
||||
|
||||
return {
|
||||
.total = total,
|
||||
.idle = idle
|
||||
};
|
||||
}
|
||||
|
||||
} // namespace
|
||||
} // namespace
|
||||
|
||||
15
tjp/core/system_usage/linux/NetworkUsage.cpp
Normal file
15
tjp/core/system_usage/linux/NetworkUsage.cpp
Normal file
@@ -0,0 +1,15 @@
|
||||
// TJP COPYRIGHT HEADER
|
||||
|
||||
#include "../NetworkUsage.h"
|
||||
|
||||
namespace tjp {
|
||||
namespace core {
|
||||
|
||||
NetworkUsage getNetworkUsage ()
|
||||
{
|
||||
return { 0, 0 };
|
||||
}
|
||||
|
||||
} // namespace
|
||||
} // namespace
|
||||
|
||||
Reference in New Issue
Block a user