Skip to content

Commit

Permalink
accurate memory counter
Browse files Browse the repository at this point in the history
  • Loading branch information
Joalor64GH authored Nov 26, 2024
1 parent c35cc21 commit 7f46db2
Show file tree
Hide file tree
Showing 4 changed files with 144 additions and 8 deletions.
10 changes: 2 additions & 8 deletions source/debug/FPS.hx
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,15 @@ class FPS extends TextField {
width = 1280;
height = 720;
selectable = false;
defaultTextFormat = new TextFormat(Paths.font((font != null) ? font : 'vcr.ttf'), 16, color);
defaultTextFormat = new TextFormat(Paths.font(font ?? 'vcr.ttf'), 16, color);

addEventListener(Event.ENTER_FRAME, (_) -> {
final now:Float = Timer.stamp() * 1000;
times.push(now);
while (times[0] < now - 1000)
times.shift();

var mem:Float = System.totalMemory;
var memPeak:Float = 0;
if (mem > memPeak)
memPeak = mem;

text = (visible) ? 'FPS: ${times.length}\nMEM: ${FlxStringUtil.formatBytes(mem)} / ${FlxStringUtil.formatBytes(memPeak)}' : '';

text = (visible) ? 'FPS: ${times.length}\nMEM: ${FlxStringUtil.formatBytes(Memory.getCurrentUsage())} / ${FlxStringUtil.formatBytes(Memory.getPeakUsage())}' : '';
textColor = (times.length < FlxG.drawFramerate * 0.5) ? 0xFFFF0000 : 0xFFFFFFFF;
});

Expand Down
21 changes: 21 additions & 0 deletions source/external/memory/Memory.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package external.memory;

#if cpp
@:buildXml('<include name="../../../../source/external/memory/build.xml" />')
@:include("memory.h")
extern class Memory {
@:native("getPeakRSS")
public static function getPeakUsage():Float;

@:native("getCurrentRSS")
public static function getCurrentUsage():Float;
}
#else
class Memory {
public static function getPeakUsage():Float
return 0.0;

public static function getCurrentUsage():Float
return 0.0;
}
#end
7 changes: 7 additions & 0 deletions source/external/memory/build.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<xml>
<set name="PROJECT_DIR" value="${this_dir}" />

<files id='haxe'>
<compilervalue name="-I" value="${PROJECT_DIR}/include/" />
</files>
</xml>
114 changes: 114 additions & 0 deletions source/external/memory/include/memory.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
/*
* Author: David Robert Nadeau
* Site: http://NadeauSoftware.com/
* License: Creative Commons Attribution 3.0 Unported License
* http://creativecommons.org/licenses/by/3.0/deed.en_US
*/

#if defined(_WIN32)
#include <windows.h>
#include <psapi.h>

#elif defined(__unix__) || defined(__unix) || defined(unix) || (defined(__APPLE__) && defined(__MACH__))
#include <unistd.h>
#include <sys/resource.h>

#if defined(__APPLE__) && defined(__MACH__)
#include <mach/mach.h>

#elif (defined(_AIX) || defined(__TOS__AIX__)) || (defined(__sun__) || defined(__sun) || defined(sun) && (defined(__SVR4) || defined(__svr4__)))
#include <fcntl.h>
#include <procfs.h>

#elif defined(__linux__) || defined(__linux) || defined(linux) || defined(__gnu_linux__)
#include <stdio.h>

#endif

#else
#error "Cannot define getPeakRSS( ) or getCurrentRSS( ) for an unknown OS."
#endif

/**
* Returns the peak (maximum so far) resident set size (physical
* memory use) measured in bytes, or zero if the value cannot be
* determined on this OS.
*/
size_t getPeakRSS( )
{
#if defined(_WIN32)
/* Windows -------------------------------------------------- */
PROCESS_MEMORY_COUNTERS info;
GetProcessMemoryInfo( GetCurrentProcess( ), &info, sizeof(info) );
return (size_t)info.PeakWorkingSetSize;

#elif (defined(_AIX) || defined(__TOS__AIX__)) || (defined(__sun__) || defined(__sun) || defined(sun) && (defined(__SVR4) || defined(__svr4__)))
/* AIX and Solaris ------------------------------------------ */
struct psinfo psinfo;
int fd = -1;
if ( (fd = open( "/proc/self/psinfo", O_RDONLY )) == -1 )
return (size_t)0L; /* Can't open? */
if ( read( fd, &psinfo, sizeof(psinfo) ) != sizeof(psinfo) )
{
close( fd );
return (size_t)0L; /* Can't read? */
}
close( fd );
return (size_t)(psinfo.pr_rssize * 1024L);

#elif defined(__unix__) || defined(__unix) || defined(unix) || (defined(__APPLE__) && defined(__MACH__))
/* BSD, Linux, and OSX -------------------------------------- */
struct rusage rusage;
getrusage( RUSAGE_SELF, &rusage );
#if defined(__APPLE__) && defined(__MACH__)
return (size_t)rusage.ru_maxrss;
#else
return (size_t)(rusage.ru_maxrss * 1024L);
#endif

#else
/* Unknown OS ----------------------------------------------- */
return (size_t)0L; /* Unsupported. */
#endif
}

/**
* Returns the current resident set size (physical memory use) measured
* in bytes, or zero if the value cannot be determined on this OS.
*/
size_t getCurrentRSS( )
{
#if defined(_WIN32)
/* Windows -------------------------------------------------- */
PROCESS_MEMORY_COUNTERS info;
GetProcessMemoryInfo( GetCurrentProcess( ), &info, sizeof(info) );
return (size_t)info.WorkingSetSize;

#elif defined(__APPLE__) && defined(__MACH__)
/* OSX ------------------------------------------------------ */
struct mach_task_basic_info info;
mach_msg_type_number_t infoCount = MACH_TASK_BASIC_INFO_COUNT;
if ( task_info( mach_task_self( ), MACH_TASK_BASIC_INFO,
(task_info_t)&info, &infoCount ) != KERN_SUCCESS )
return (size_t)0L; /* Can't access? */
return (size_t)info.resident_size;

#elif defined(__linux__) || defined(__linux) || defined(linux) || defined(__gnu_linux__)
/* Linux ---------------------------------------------------- */
long rss = 0L;
FILE* fp = NULL;
if ( (fp = fopen( "/proc/self/statm", "r" )) == NULL )
return (size_t)0L; /* Can't open? */
if ( fscanf( fp, "%*s%ld", &rss ) != 1 )
{
fclose( fp );
return (size_t)0L; /* Can't read? */
}
fclose( fp );
return (size_t)rss * (size_t)sysconf( _SC_PAGESIZE);

#else
/* AIX, BSD, Solaris, and Unknown OS ------------------------ */
return (size_t)0L; /* Unsupported. */
#endif
}

0 comments on commit 7f46db2

Please sign in to comment.