VirtualBox

Ignore:
Timestamp:
May 30, 2013 12:03:16 PM (12 years ago)
Author:
vboxsync
Message:

Runtime/linux/RTSystemQueryAvailableRam: also consider the cached memory

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Runtime/r3/linux/systemmem-linux.cpp

    r45104 r46324  
    3434#include <iprt/err.h>
    3535#include <iprt/assert.h>
     36#include <iprt/string.h>
    3637
     38#include <stdio.h>
    3739#include <errno.h>
    3840
     
    6264    AssertPtrReturn(pcb, VERR_INVALID_POINTER);
    6365
     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     */
    64100    struct sysinfo info;
    65101    int rc = sysinfo(&info);
    66102    if (rc == 0)
    67103    {
    68         /** @todo Actually this is not quite correct. We would also need to add the
    69          * cached RAM but this information is not available in sysinfo. */
    70104        *pcb = ((uint64_t)info.freeram + info.bufferram) * info.mem_unit;
    71105        return VINF_SUCCESS;
Note: See TracChangeset for help on using the changeset viewer.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette