Commit b6c88e95 authored by Jan Möbius's avatar Jan Möbius

Merge branch 'fixMemoryInformation' into 'master'

Fix memory information

fixes #3 the memory display issue on linux and adds memory information for OS X

See merge request !17
parents aed9179e 72072b1e
......@@ -40,32 +40,18 @@
\*===========================================================================*/
#include "RAMInfo.hh"
#include <stdio.h>
// Main Memory information
#ifdef WIN32
#include <Windows.h>
#elif defined ARCH_DARWIN
#else
#include <sys/sysinfo.h>
//Info class returned by sysinfo
//struct sysinfo {
// long uptime; /* Sekunden seit dem letzten Systemstart */
// unsigned long loads[3]; /* 1, 5 und 15 minütige Systemlast */
// unsigned long totalram; /* nutzbare Hauptspeichergröße */
// unsigned long freeram; /* verfügbare Speichergröße */
// unsigned long sharedram; /* Größe des gemeinsamen Speichers */
// unsigned long bufferram; /* von Puffern benutzter Speicher */
// unsigned long totalswap; /* Größe des Auslagerungsspeichers */
// unsigned long freeswap; /* verfügbarer Auslagerungsspeicher */
// unsigned short procs; /* Aktuelle Prozesszahl */
// unsigned long totalhigh; /* Gesamtgröße des oberen Speicherbereichs */
// unsigned long freehigh; /* verfügbarer oberer Speicherbereich */
// unsigned int mem_unit; /* Größe der Speichereinheit in Byte */
// char _f[20-2*sizeof(long)-sizeof(int)]; /* Auffüllung auf 64 bytes */
//};
// private struct to get ram information
#include <mach/mach_host.h>
#include <mach/vm_statistics.h>
#include <sys/sysctl.h>
#include <sys/types.h>
#endif
// private struct to get ram information
namespace{
struct MemoryVacancy{
unsigned long totalRamMB;
......@@ -73,6 +59,36 @@ struct MemoryVacancy{
unsigned long bufferRamMB;
};
void parseMeminfo(int& total, int& free, int& buffer)
{
int memcache;
int memfree;
FILE* info = fopen("/proc/meminfo","r");
if(fscanf (info, "MemTotal: %d kB MemFree: %d kB Buffers: %d kB Cached: %d kB",&total, &memfree, &buffer, &memcache) < 4) //try to parse the old meminfo format
{
fclose(info);
info = fopen("/proc/meminfo","r");
//parsing the old format failed so we try to parse using the new format
if(fscanf(info, "MemTotal: %d kB MemFree: %d kB MemAvailable: %d kB Buffers: %d kB Cached: %d kB",&total, &memfree, &free, &buffer, &memcache) < 5)
{
//parsing failed overall so return -1 for all values
total = -1;
free = -1;
buffer = -1;
}
else
{
free = memfree + (buffer + memcache); //everything is fine
}
}
else //compute available memory
{
free = memfree + (buffer + memcache);
}
fclose(info);
}
}
namespace Utils
{
......@@ -98,15 +114,41 @@ namespace Utils
_outMemoryVacancy.freeRamMB = ms.ullAvailPhys / 1024 / 1024;
#elif defined ARCH_DARWIN // Apple (sadly cant query free memory)
mach_msg_type_number_t count = HOST_VM_INFO_COUNT;
vm_statistics_data_t vmstat;
if(KERN_SUCCESS != host_statistics(mach_host_self(), HOST_VM_INFO, (host_info_t)&vmstat, &count))
{ // An error occurred
}
else
{
//retrieve real physical memory
int mib[2];
uint64_t physical_memory;
size_t length;
mib[0] = CTL_HW;
mib[1] = HW_MEMSIZE;
length = sizeof(uint64_t);
if( sysctl(mib, 2, &physical_memory, &length, NULL, 0) == -1 )
{
physical_memory = 0;
}
// retrieve free memory
double total = vmstat.wire_count + vmstat.active_count + vmstat.inactive_count + vmstat.free_count;
double free = vmstat.free_count / total;
_outMemoryVacancy.totalRamMB = physical_memory / 1024 / 1024;
_outMemoryVacancy.freeRamMB = (long)(free + vmstat.inactive_count) * PAGE_SIZE / 1024/1024;
_outMemoryVacancy.bufferRamMB =0;
}
#else // Linux
struct sysinfo sys_info;
sysinfo(&sys_info);
int total, free, buffer;
parseMeminfo(total, free, buffer);
// Unit in bytes ; /1024 -> KB ; /1024 MB
_outMemoryVacancy.totalRamMB = sys_info.totalram / 1024 / 1024 * sys_info.mem_unit;
_outMemoryVacancy.freeRamMB = sys_info.freeram / 1024 / 1024 * sys_info.mem_unit;
_outMemoryVacancy.bufferRamMB = sys_info.bufferram / 1024 / 1024 * sys_info.mem_unit; // Buffers get freed, if we don't have enough free ram
// Unit in kbytes ; /1024 -> MB
_outMemoryVacancy.totalRamMB = (long)total / 1024;
_outMemoryVacancy.freeRamMB = (long)free / 1024;
_outMemoryVacancy.bufferRamMB = (long)buffer / 1024; // Buffers get freed, if we don't have enough free ram
#endif
}
......
......@@ -50,7 +50,7 @@ namespace Utils
/**
* @brief queryFreeRAM queries the os ram information and returns the amount of free ram (0 if not supported like on osx)
* @return unsigned long the free amount of ram in MB
* @return unsigned long the free amount of ram in MB (as for linux users free ram means here available ram)
*/
DLLEXPORT
unsigned long queryFreeRAM();
......
......@@ -91,9 +91,9 @@ void MemInfoPlugin::pluginsInitialized() {
}
// Main Memory information
#ifdef ARCH_DARWIN //Apple
// #ifdef ARCH_DARWIN //Apple
#else // Linux and Windows
// #else // Linux and Windows
// emit log(LOGINFO,"Main Memory monitoring supported, installing main memory monitor into status bar");
......@@ -109,7 +109,7 @@ void MemInfoPlugin::pluginsInitialized() {
cpuMemoryInfoUpdate();
#endif
//#endif
}
......@@ -147,10 +147,9 @@ void MemInfoPlugin::cpuMemoryInfoUpdate() {
if (mainMemBar_) {
unsigned long totalRamMB = Utils::Memory::queryTotalRAM();
unsigned long freeRamMB = Utils::Memory::queryFreeRAM();
unsigned long bufferRamMB = Utils::Memory::queryBufferedRAM();
mainMemBar_->setRange( 0 , totalRamMB );
mainMemBar_->setValue( totalRamMB-freeRamMB-bufferRamMB);
mainMemBar_->setValue( totalRamMB - freeRamMB );
setProgressBarStyleSheet(mainMemBar_);
}
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment