Sleds/K2Daemon/LogMemstats.h

66 lines
2.4 KiB
C++

/*
Copyright (c) 2013 IOnU Security Inc. All rights reserved
Created October 2013 by Kendrick Webster
K2Daemon/LogMemstats.h - header for memory and CPU stats logging module
This module periodically obtains and logs memory and CPU usage statistics.
It serves three main purposes:
1). Make it easier to detect memory leaks of the type that valgrind et. al.
cannot detect. For instance: If a std library container singleton is
used to store a collection of objects, the container's dtor will run
when the program exits and free all of the contained objects, thus
preventing valgrind from detecting a leak. Unused objects accumulating
in the container will cause the memory usage to grow over time. Such a
trend will be visible in the log.
2). Provide an indication of system load and efficiency. Memory and CPU
usage for known numbers of typical users could be extrapolated to
determine roughly how many users a system could handle before it runs
out of memory or CPU time. These metrics also give developer(s)
feedback for design decisions.
The statistics are also exposed via accessor functions for system health
monitoring, etc.
*/
#pragma once
namespace LogMemstats
{
const int log_summary_interval_seconds = 60;
const int log_details_interval_seconds = 600;
const int first_log_delay_seconds = 30;
// call periodically (i.e. at TIMER_TICKS_PER_SECOND or similar rate)
void Timer(void);
}
namespace Memstats
{
static_assert(8 == sizeof(long unsigned int), "Must compile as 64-bit");
typedef long unsigned int uint_t;
// system info
uint_t CpuCount(void);
uint_t TotalVirtualMemory(void);
uint_t TotalMemory(void);
uint_t SystemWideVirtualMemoryUsed(void);
uint_t SystemWideMemoryUsed(void);
double SystemWideCpuLoadPercent(void); // percentage of all cores
// daemon instance info
uint_t UptimeSeconds(void);
uint_t ConnectedClients(void);
uint_t SubscribedOffices(void);
uint_t OfficeSubscriptions(void);
uint_t VirtualMemoryUsed(void);
uint_t MemoryUsed(void);
uint_t HeapAllocated(void);
double CpuLoadPercent(void); // percentage of a single core (same for next 4 functions below)
double CpuUserPercent(void);
double CpuSystemPercent(void);
double CpuChildUserPercent(void);
double CpuChildSystemPercent(void);
}