Merge branch 'linux-better-mem-report' into 'master'

Use MemAvailable instead

See merge request STJr/SRB2!281
This commit is contained in:
Monster Iestyn 2018-10-01 16:25:21 -04:00
commit a605ee9c11
1 changed files with 73 additions and 30 deletions

View File

@ -124,6 +124,10 @@ typedef LPVOID (WINAPI *p_MapViewOfFile) (HANDLE, DWORD, DWORD, DWORD, SIZE_T);
#include "macosx/mac_resources.h" #include "macosx/mac_resources.h"
#endif #endif
#ifndef errno
#include <errno.h>
#endif
// Locations for searching the srb2.srb // Locations for searching the srb2.srb
#if defined (__unix__) || defined(__APPLE__) || defined (UNIXCOMMON) #if defined (__unix__) || defined(__APPLE__) || defined (UNIXCOMMON)
#define DEFAULTWADLOCATION1 "/usr/local/share/games/SRB2" #define DEFAULTWADLOCATION1 "/usr/local/share/games/SRB2"
@ -1679,7 +1683,7 @@ static void I_ShutdownMouse2(void)
EscapeCommFunction(mouse2filehandle, CLRRTS); EscapeCommFunction(mouse2filehandle, CLRRTS);
PurgeComm(mouse2filehandle, PURGE_TXABORT | PURGE_RXABORT | PurgeComm(mouse2filehandle, PURGE_TXABORT | PURGE_RXABORT |
PURGE_TXCLEAR | PURGE_RXCLEAR); PURGE_TXCLEAR | PURGE_RXCLEAR);
CloseHandle(mouse2filehandle); CloseHandle(mouse2filehandle);
@ -1872,11 +1876,11 @@ void I_StartupMouse2(void)
{ {
// COM file handle // COM file handle
mouse2filehandle = CreateFileA(cv_mouse2port.string, GENERIC_READ | GENERIC_WRITE, mouse2filehandle = CreateFileA(cv_mouse2port.string, GENERIC_READ | GENERIC_WRITE,
0, // exclusive access 0, // exclusive access
NULL, // no security attrs NULL, // no security attrs
OPEN_EXISTING, OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL, FILE_ATTRIBUTE_NORMAL,
NULL); NULL);
if (mouse2filehandle == INVALID_HANDLE_VALUE) if (mouse2filehandle == INVALID_HANDLE_VALUE)
{ {
INT32 e = GetLastError(); INT32 e = GetLastError();
@ -1896,7 +1900,7 @@ void I_StartupMouse2(void)
// purge buffers // purge buffers
PurgeComm(mouse2filehandle, PURGE_TXABORT | PURGE_RXABORT PurgeComm(mouse2filehandle, PURGE_TXABORT | PURGE_RXABORT
| PURGE_TXCLEAR | PURGE_RXCLEAR); | PURGE_TXCLEAR | PURGE_RXCLEAR);
// setup port to 1200 7N1 // setup port to 1200 7N1
dcb.DCBlength = sizeof (DCB); dcb.DCBlength = sizeof (DCB);
@ -2025,7 +2029,7 @@ static void I_ShutdownTimer(void)
tic_t I_GetTime (void) tic_t I_GetTime (void)
{ {
static Uint32 basetime = 0; static Uint32 basetime = 0;
Uint32 ticks = SDL_GetTicks(); Uint32 ticks = SDL_GetTicks();
if (!basetime) if (!basetime)
basetime = ticks; basetime = ticks;
@ -2091,7 +2095,6 @@ INT32 I_StartupSystem(void)
return 0; return 0;
} }
// //
// I_Quit // I_Quit
// //
@ -2370,7 +2373,7 @@ void I_GetDiskFreeSpace(INT64 *freespace)
{ {
DWORD SectorsPerCluster, BytesPerSector, NumberOfFreeClusters, TotalNumberOfClusters; DWORD SectorsPerCluster, BytesPerSector, NumberOfFreeClusters, TotalNumberOfClusters;
GetDiskFreeSpace(NULL, &SectorsPerCluster, &BytesPerSector, GetDiskFreeSpace(NULL, &SectorsPerCluster, &BytesPerSector,
&NumberOfFreeClusters, &TotalNumberOfClusters); &NumberOfFreeClusters, &TotalNumberOfClusters);
*freespace = BytesPerSector*SectorsPerCluster*NumberOfFreeClusters; *freespace = BytesPerSector*SectorsPerCluster*NumberOfFreeClusters;
} }
#else // Dummy for platform independent; 1GB should be enough #else // Dummy for platform independent; 1GB should be enough
@ -2592,22 +2595,22 @@ static const char *locateWad(void)
#ifdef CMAKECONFIG #ifdef CMAKECONFIG
#ifndef NDEBUG #ifndef NDEBUG
I_OutputMsg(","CMAKE_ASSETS_DIR); I_OutputMsg(","CMAKE_ASSETS_DIR);
strcpy(returnWadPath, CMAKE_ASSETS_DIR); strcpy(returnWadPath, CMAKE_ASSETS_DIR);
if (isWadPathOk(returnWadPath)) if (isWadPathOk(returnWadPath))
{ {
return returnWadPath; return returnWadPath;
} }
#endif #endif
#endif #endif
#ifdef __APPLE__ #ifdef __APPLE__
OSX_GetResourcesPath(returnWadPath); OSX_GetResourcesPath(returnWadPath);
I_OutputMsg(",%s", returnWadPath); I_OutputMsg(",%s", returnWadPath);
if (isWadPathOk(returnWadPath)) if (isWadPathOk(returnWadPath))
{ {
return returnWadPath; return returnWadPath;
} }
#endif #endif
// examine default dirs // examine default dirs
@ -2712,7 +2715,30 @@ const char *I_LocateWad(void)
#ifdef __linux__ #ifdef __linux__
#define MEMINFO_FILE "/proc/meminfo" #define MEMINFO_FILE "/proc/meminfo"
#define MEMTOTAL "MemTotal:" #define MEMTOTAL "MemTotal:"
#define MEMAVAILABLE "MemAvailable:"
#define MEMFREE "MemFree:" #define MEMFREE "MemFree:"
#define CACHED "Cached:"
#define BUFFERS "Buffers:"
#define SHMEM "Shmem:"
/* Parse the contents of /proc/meminfo (in buf), return value of "name"
* (example: MemTotal) */
static long get_entry(const char* name, const char* buf)
{
long val;
char* hit = strstr(buf, name);
if (hit == NULL) {
return -1;
}
errno = 0;
val = strtol(hit + strlen(name), NULL, 10);
if (errno != 0) {
CONS_Alert(CONS_ERROR, M_GetText("get_entry: strtol() failed: %s\n"), strerror(errno));
return -1;
}
return val;
}
#endif #endif
// quick fix for compil // quick fix for compil
@ -2784,6 +2810,11 @@ UINT32 I_GetFreeMem(UINT32 *total)
UINT32 totalKBytes; UINT32 totalKBytes;
INT32 n; INT32 n;
INT32 meminfo_fd = -1; INT32 meminfo_fd = -1;
long Cached;
long MemFree;
long Buffers;
long Shmem;
long MemAvailable = -1;
meminfo_fd = open(MEMINFO_FILE, O_RDONLY); meminfo_fd = open(MEMINFO_FILE, O_RDONLY);
n = read(meminfo_fd, buf, 1023); n = read(meminfo_fd, buf, 1023);
@ -2809,16 +2840,28 @@ UINT32 I_GetFreeMem(UINT32 *total)
memTag += sizeof (MEMTOTAL); memTag += sizeof (MEMTOTAL);
totalKBytes = atoi(memTag); totalKBytes = atoi(memTag);
if ((memTag = strstr(buf, MEMFREE)) == NULL) if ((memTag = strstr(buf, MEMAVAILABLE)) == NULL)
{ {
// Error Cached = get_entry(CACHED, buf);
if (total) MemFree = get_entry(MEMFREE, buf);
*total = 0L; Buffers = get_entry(BUFFERS, buf);
return 0; Shmem = get_entry(SHMEM, buf);
} MemAvailable = Cached + MemFree + Buffers - Shmem;
memTag += sizeof (MEMFREE); if (MemAvailable == -1)
freeKBytes = atoi(memTag); {
// Error
if (total)
*total = 0L;
return 0;
}
freeKBytes = MemAvailable;
}
else
{
memTag += sizeof (MEMAVAILABLE);
freeKBytes = atoi(memTag);
}
if (total) if (total)
*total = totalKBytes << 10; *total = totalKBytes << 10;