VirtualBox

source: vbox/trunk/src/VBox/Runtime/r3/linux/systemmem-linux.cpp@ 76553

Last change on this file since 76553 was 76553, checked in by vboxsync, 5 years ago

scm --update-copyright-year

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 3.7 KB
Line 
1/* $Id: systemmem-linux.cpp 76553 2019-01-01 01:45:53Z vboxsync $ */
2/** @file
3 * IPRT - RTSystemQueryTotalRam, Linux ring-3.
4 */
5
6/*
7 * Copyright (C) 2012-2019 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * The contents of this file may alternatively be used under the terms
18 * of the Common Development and Distribution License Version 1.0
19 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
20 * VirtualBox OSE distribution, in which case the provisions of the
21 * CDDL are applicable instead of those of the GPL.
22 *
23 * You may elect to license modified versions of this file under the
24 * terms and conditions of either the GPL or the CDDL or both.
25 */
26
27
28/*********************************************************************************************************************************
29* Header Files *
30*********************************************************************************************************************************/
31#include <iprt/system.h>
32#include "internal/iprt.h"
33
34#include <iprt/errcore.h>
35#include <iprt/assert.h>
36#include <iprt/string.h>
37
38#include <stdio.h>
39#include <errno.h>
40
41/* Satisfy compiller warning */
42#define __EXPORTED_HEADERS__
43#include <sys/sysinfo.h>
44#undef __EXPORTED_HEADERS__
45
46
47RTDECL(int) RTSystemQueryTotalRam(uint64_t *pcb)
48{
49 AssertPtrReturn(pcb, VERR_INVALID_POINTER);
50
51 struct sysinfo info;
52 int rc = sysinfo(&info);
53 if (rc == 0)
54 {
55 *pcb = (uint64_t)info.totalram * info.mem_unit;
56 return VINF_SUCCESS;
57 }
58 return RTErrConvertFromErrno(errno);
59}
60
61
62RTDECL(int) RTSystemQueryAvailableRam(uint64_t *pcb)
63{
64 AssertPtrReturn(pcb, VERR_INVALID_POINTER);
65
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);
108}
109
Note: See TracBrowser for help on using the repository browser.

© 2023 Oracle
ContactPrivacy policyTerms of Use