Browse Source

Added getProcessMemoryUsed.

Branimir Karadžić 10 years ago
parent
commit
6a4b0e18da
2 changed files with 51 additions and 3 deletions
  1. 43 3
      include/bx/os.h
  2. 8 0
      tests/misc.cpp

+ 43 - 3
include/bx/os.h

@@ -42,9 +42,9 @@
 #	if BX_PLATFORM_LINUX || BX_PLATFORM_RPI
 #		include <unistd.h> // syscall
 #		include <sys/syscall.h>
-#	endif // BX_PLATFORM_LINUX || BX_PLATFORM_RPI
-
-#	if BX_PLATFORM_ANDROID
+#	elif BX_PLATFORM_OSX
+#		include <mach/mach.h> // mach_task_basic_info
+#	elif BX_PLATFORM_ANDROID
 #		include "debug.h" // getTid is not implemented...
 #	endif // BX_PLATFORM_ANDROID
 #endif // BX_PLATFORM_
@@ -110,6 +110,46 @@ namespace bx
 #endif //
 	}
 
+	inline size_t getProcessMemoryUsed()
+	{
+#if BX_PLATFORM_LINUX
+		FILE* file = fopen("/proc/self/statm", "r");
+		if (NULL == file)
+		{
+			return 0;
+		}
+
+		long pages = 0;
+		fscanf(file, "%*s%ld", &pages)
+		fclose(file);
+		return pages * sysconf(_SC_PAGESIZE);
+#elif BX_PLATFORM_OSX
+		mach_task_basic_info info;
+		mach_msg_type_number_t infoCount = MACH_TASK_BASIC_INFO_COUNT;
+
+		int result = task_info(mach_task_self()
+				, MACH_TASK_BASIC_INFO
+				, (task_info_t)&info
+				, &infoCount
+				);
+		if (KERN_SUCCESS != result)
+		{
+			return 0;
+		}
+
+		return info.resident_size;
+#elif BX_PLATFORM_WINDOWS
+		PROCESS_MEMORY_COUNTERS pmc;
+		GetProcessMemoryInfo(GetCurrentProcess()
+			, &pmc
+			, sizeof(pmc)
+			);
+		return pmc.WorkingSetSize;
+#else
+		return 0;
+#endif // BX_PLATFORM_*
+	}
+
 	inline void* dlopen(const char* _filePath)
 	{
 #if BX_PLATFORM_WINDOWS

+ 8 - 0
tests/misc.cpp

@@ -0,0 +1,8 @@
+#include "test.h"
+#include <bx/os.h>
+
+TEST(getProcessMemoryUsed)
+{
+	CHECK(0 != bx::getProcessMemoryUsed() );
+//	DBG("bx::getProcessMemoryUsed %d", bx::getProcessMemoryUsed() );
+}