mirror of
https://github.com/ossrs/srs.git
synced 2025-03-09 15:49:59 +00:00
add meminfo
This commit is contained in:
parent
15aea4d9b3
commit
a5f4f6bd14
5 changed files with 208 additions and 12 deletions
|
@ -286,6 +286,84 @@ void srs_update_proc_stat()
|
|||
}
|
||||
}
|
||||
|
||||
SrsMemInfo::SrsMemInfo()
|
||||
{
|
||||
ok = false;
|
||||
sample_time = 0;
|
||||
|
||||
percent_ram = 0;
|
||||
percent_swap = 0;
|
||||
|
||||
MemActive = 0;
|
||||
RealInUse = 0;
|
||||
NotInUse = 0;
|
||||
MemTotal = 0;
|
||||
MemFree = 0;
|
||||
Buffers = 0;
|
||||
Cached = 0;
|
||||
SwapTotal = 0;
|
||||
SwapFree = 0;
|
||||
}
|
||||
|
||||
static SrsMemInfo _srs_system_meminfo;
|
||||
|
||||
SrsMemInfo* srs_get_meminfo()
|
||||
{
|
||||
return &_srs_system_meminfo;
|
||||
}
|
||||
|
||||
void srs_update_meminfo()
|
||||
{
|
||||
FILE* f = fopen("/proc/meminfo", "r");
|
||||
if (f == NULL) {
|
||||
srs_warn("open meminfo failed, ignore");
|
||||
return;
|
||||
}
|
||||
|
||||
SrsMemInfo& r = _srs_system_meminfo;
|
||||
r.ok = false;
|
||||
|
||||
for (;;) {
|
||||
static char label[64];
|
||||
static unsigned long value;
|
||||
static char postfix[64];
|
||||
int ret = fscanf(f, "%64s %lu %64s\n", label, &value, postfix);
|
||||
|
||||
if (ret == EOF) {
|
||||
break;
|
||||
}
|
||||
|
||||
if (strcmp("MemTotal:", label) == 0) {
|
||||
r.MemTotal = value;
|
||||
} else if (strcmp("MemFree:", label) == 0) {
|
||||
r.MemFree = value;
|
||||
} else if (strcmp("Buffers:", label) == 0) {
|
||||
r.Buffers = value;
|
||||
} else if (strcmp("Cached:", label) == 0) {
|
||||
r.Cached = value;
|
||||
} else if (strcmp("SwapTotal:", label) == 0) {
|
||||
r.SwapTotal = value;
|
||||
} else if (strcmp("SwapFree:", label) == 0) {
|
||||
r.SwapFree = value;
|
||||
}
|
||||
}
|
||||
|
||||
r.sample_time = srs_get_system_time_ms();
|
||||
r.MemActive = r.MemTotal - r.MemFree;
|
||||
r.RealInUse = r.MemActive - r.Buffers - r.Cached;
|
||||
r.NotInUse = r.MemTotal - r.RealInUse;
|
||||
|
||||
r.ok = true;
|
||||
if (r.MemTotal > 0) {
|
||||
r.percent_ram = (float)(r.RealInUse / (double)r.MemTotal);
|
||||
}
|
||||
if (r.SwapTotal > 0) {
|
||||
r.percent_swap = (float)((r.SwapTotal - r.SwapFree) / (double)r.SwapTotal);
|
||||
}
|
||||
|
||||
fclose(f);
|
||||
}
|
||||
|
||||
SrsCpuInfo::SrsCpuInfo()
|
||||
{
|
||||
ok = false;
|
||||
|
|
|
@ -260,6 +260,42 @@ extern SrsProcSystemStat* srs_get_system_proc_stat();
|
|||
// the deamon st-thread will update it.
|
||||
extern void srs_update_proc_stat();
|
||||
|
||||
// @see: cat /proc/meminfo
|
||||
struct SrsMemInfo
|
||||
{
|
||||
// whether the data is ok.
|
||||
bool ok;
|
||||
// the time in ms when sample.
|
||||
int64_t sample_time;
|
||||
// the percent of usage. 0.153 is 15.3%.
|
||||
float percent_ram;
|
||||
float percent_swap;
|
||||
|
||||
// MemActive = MemTotal - MemFree
|
||||
int64_t MemActive;
|
||||
// RealInUse = MemActive - Buffers - Cached
|
||||
int64_t RealInUse;
|
||||
// NotInUse = MemTotal - RealInUse
|
||||
// = MemTotal - MemActive + Buffers + Cached
|
||||
// = MemTotal - MemTotal + MemFree + Buffers + Cached
|
||||
// = MemFree + Buffers + Cached
|
||||
int64_t NotInUse;
|
||||
|
||||
int64_t MemTotal;
|
||||
int64_t MemFree;
|
||||
int64_t Buffers;
|
||||
int64_t Cached;
|
||||
int64_t SwapTotal;
|
||||
int64_t SwapFree;
|
||||
|
||||
SrsMemInfo();
|
||||
};
|
||||
|
||||
// get system meminfo, use cache to avoid performance problem.
|
||||
extern SrsMemInfo* srs_get_meminfo();
|
||||
// the deamon st-thread will update it.
|
||||
extern void srs_update_meminfo();
|
||||
|
||||
// @see: cat /proc/cpuinfo
|
||||
struct SrsCpuInfo
|
||||
{
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue