Changeset 55095 in vbox for trunk/src/VBox/Runtime/r3
- Timestamp:
- Apr 2, 2015 4:52:46 PM (10 years ago)
- svn:sync-xref-src-repo-rev:
- 99390
- File:
-
- 1 copied
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/VBox/Runtime/r3/freebsd/systemmem-freebsd.cpp
r55093 r55095 36 36 #include <iprt/string.h> 37 37 38 #include <stdio.h> 38 #include <sys/types.h> 39 #include <sys/sysctl.h> 39 40 #include <errno.h> 40 41 /* Satisfy compiller warning */42 #define __EXPORTED_HEADERS__43 #include <sys/sysinfo.h>44 #undef __EXPORTED_HEADERS__45 41 46 42 47 43 RTDECL(int) RTSystemQueryTotalRam(uint64_t *pcb) 48 44 { 45 int rc = VINF_SUCCESS; 46 u_long cbMemPhys = 0; 47 size_t cbParameter = sizeof(cbMemPhys); 48 49 49 AssertPtrReturn(pcb, VERR_INVALID_POINTER); 50 50 51 struct sysinfo info; 52 int rc = sysinfo(&info); 53 if (rc == 0) 51 if (!sysctlbyname("hw.physmem", &cbMemPhys, &cbParameter, NULL, 0)) 54 52 { 55 *pcb = (uint64_t)info.totalram * info.mem_unit;53 *pcb = cbMemPhys; 56 54 return VINF_SUCCESS; 57 55 } … … 64 62 AssertPtrReturn(pcb, VERR_INVALID_POINTER); 65 63 66 FILE *pFile = fopen("/proc/meminfo", "r"); 67 if (pFile) 68 { 69 int rc = VERR_NOT_FOUND; 70 uint64_t cbTotal = 0; 71 uint64_t cbFree = 0; 72 uint64_t cbBuffers = 0; 73 uint64_t cbCached = 0; 74 char sz[256]; 75 while (fgets(sz, sizeof(sz), pFile)) 76 { 77 if (!strncmp(sz, RT_STR_TUPLE("MemTotal:"))) 78 rc = RTStrToUInt64Ex(RTStrStripL(&sz[sizeof("MemTotal:")]), NULL, 0, &cbTotal); 79 else if (!strncmp(sz, RT_STR_TUPLE("MemFree:"))) 80 rc = RTStrToUInt64Ex(RTStrStripL(&sz[sizeof("MemFree:")]), NULL, 0, &cbFree); 81 else if (!strncmp(sz, RT_STR_TUPLE("Buffers:"))) 82 rc = RTStrToUInt64Ex(RTStrStripL(&sz[sizeof("Buffers:")]), NULL, 0, &cbBuffers); 83 else if (!strncmp(sz, RT_STR_TUPLE("Cached:"))) 84 rc = RTStrToUInt64Ex(RTStrStripL(&sz[sizeof("Cached:")]), NULL, 0, &cbCached); 85 if (RT_FAILURE(rc)) 86 break; 87 } 88 fclose(pFile); 89 if (RT_SUCCESS(rc)) 90 { 91 *pcb = (cbFree + cbBuffers + cbCached) * _1K; 92 return VINF_SUCCESS; 93 } 94 } 95 /* 96 * Fallback (e.g. /proc not mapped) to sysinfo. Less accurat because there 97 * is no information about the cached memory. 'Cached:' from above is only 98 * accessible through proc :-( 99 */ 100 struct sysinfo info; 101 int rc = sysinfo(&info); 102 if (rc == 0) 103 { 104 *pcb = ((uint64_t)info.freeram + info.bufferram) * info.mem_unit; 105 return VINF_SUCCESS; 106 } 107 return RTErrConvertFromErrno(errno); 64 int rc = VINF_SUCCESS; 65 u_int cPagesMemFree = 0; 66 u_int cPagesMemInactive = 0; 67 u_int cPagesMemCached = 0; 68 u_int cPagesMemUsed = 0; 69 int cbPage = 0; 70 size_t cbParameter; 71 int cProcessed = 0; 72 73 cbParameter = sizeof(cPagesMemFree); 74 if (sysctlbyname("vm.stats.vm.v_free_count", &cPagesMemFree, &cbParameter, NULL, 0)) 75 rc = RTErrConvertFromErrno(errno); 76 cbParameter = sizeof(cPagesMemUsed); 77 if ( RT_SUCCESS(rc) 78 && sysctlbyname("vm.stats.vm.v_active_count", &cPagesMemUsed, &cbParameter, NULL, 0)) 79 rc = RTErrConvertFromErrno(errno); 80 cbParameter = sizeof(cPagesMemInactive); 81 if ( RT_SUCCESS(rc) 82 && sysctlbyname("vm.stats.vm.v_inactive_count", &cPagesMemInactive, &cbParameter, NULL, 0)) 83 rc = RTErrConvertFromErrno(errno); 84 cbParameter = sizeof(cPagesMemCached); 85 if ( RT_SUCCESS(rc) 86 && sysctlbyname("vm.stats.vm.v_cache_count", &cPagesMemCached, &cbParameter, NULL, 0)) 87 rc = RTErrConvertFromErrno(errno); 88 cbParameter = sizeof(cbPage); 89 if ( RT_SUCCESS(rc) 90 && sysctlbyname("hw.pagesize", &cbPage, &cbParameter, NULL, 0)) 91 rc = RTErrConvertFromErrno(errno); 92 93 if (RT_SUCCESS(rc)) 94 *pcb = (cPagesMemFree + cPagesMemInactive + cPagesMemCached ) * cbPage; 95 96 return rc; 108 97 } 109 98
Note:
See TracChangeset
for help on using the changeset viewer.