VirtualBox

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

Last change on this file since 103795 was 98103, checked in by vboxsync, 2 years ago

Copyright year updates by scm.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 4.0 KB
Line 
1/* $Id: systemmem-linux.cpp 98103 2023-01-17 14:15:46Z vboxsync $ */
2/** @file
3 * IPRT - RTSystemQueryTotalRam, Linux ring-3.
4 */
5
6/*
7 * Copyright (C) 2012-2023 Oracle and/or its affiliates.
8 *
9 * This file is part of VirtualBox base platform packages, as
10 * available from https://www.virtualbox.org.
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation, in version 3 of the
15 * License.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, see <https://www.gnu.org/licenses>.
24 *
25 * The contents of this file may alternatively be used under the terms
26 * of the Common Development and Distribution License Version 1.0
27 * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
28 * in the VirtualBox distribution, in which case the provisions of the
29 * CDDL are applicable instead of those of the GPL.
30 *
31 * You may elect to license modified versions of this file under the
32 * terms and conditions of either the GPL or the CDDL or both.
33 *
34 * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
35 */
36
37
38/*********************************************************************************************************************************
39* Header Files *
40*********************************************************************************************************************************/
41#include <iprt/system.h>
42#include "internal/iprt.h"
43
44#include <iprt/errcore.h>
45#include <iprt/assert.h>
46#include <iprt/string.h>
47
48#include <stdio.h>
49#include <errno.h>
50
51/* Satisfy compiller warning */
52#define __EXPORTED_HEADERS__
53#include <sys/sysinfo.h>
54#undef __EXPORTED_HEADERS__
55
56
57RTDECL(int) RTSystemQueryTotalRam(uint64_t *pcb)
58{
59 AssertPtrReturn(pcb, VERR_INVALID_POINTER);
60
61 struct sysinfo info;
62 int rc = sysinfo(&info);
63 if (rc == 0)
64 {
65 *pcb = (uint64_t)info.totalram * info.mem_unit;
66 return VINF_SUCCESS;
67 }
68 return RTErrConvertFromErrno(errno);
69}
70
71
72RTDECL(int) RTSystemQueryAvailableRam(uint64_t *pcb)
73{
74 AssertPtrReturn(pcb, VERR_INVALID_POINTER);
75
76 FILE *pFile = fopen("/proc/meminfo", "r");
77 if (pFile)
78 {
79 int rc = VERR_NOT_FOUND;
80 uint64_t cbTotal = 0;
81 uint64_t cbFree = 0;
82 uint64_t cbBuffers = 0;
83 uint64_t cbCached = 0;
84 char sz[256];
85 while (fgets(sz, sizeof(sz), pFile))
86 {
87 if (!strncmp(sz, RT_STR_TUPLE("MemTotal:")))
88 rc = RTStrToUInt64Ex(RTStrStripL(&sz[sizeof("MemTotal:")]), NULL, 0, &cbTotal);
89 else if (!strncmp(sz, RT_STR_TUPLE("MemFree:")))
90 rc = RTStrToUInt64Ex(RTStrStripL(&sz[sizeof("MemFree:")]), NULL, 0, &cbFree);
91 else if (!strncmp(sz, RT_STR_TUPLE("Buffers:")))
92 rc = RTStrToUInt64Ex(RTStrStripL(&sz[sizeof("Buffers:")]), NULL, 0, &cbBuffers);
93 else if (!strncmp(sz, RT_STR_TUPLE("Cached:")))
94 rc = RTStrToUInt64Ex(RTStrStripL(&sz[sizeof("Cached:")]), NULL, 0, &cbCached);
95 if (RT_FAILURE(rc))
96 break;
97 }
98 fclose(pFile);
99 if (RT_SUCCESS(rc))
100 {
101 *pcb = (cbFree + cbBuffers + cbCached) * _1K;
102 return VINF_SUCCESS;
103 }
104 }
105 /*
106 * Fallback (e.g. /proc not mapped) to sysinfo. Less accurat because there
107 * is no information about the cached memory. 'Cached:' from above is only
108 * accessible through proc :-(
109 */
110 struct sysinfo info;
111 int rc = sysinfo(&info);
112 if (rc == 0)
113 {
114 *pcb = ((uint64_t)info.freeram + info.bufferram) * info.mem_unit;
115 return VINF_SUCCESS;
116 }
117 return RTErrConvertFromErrno(errno);
118}
119
Note: See TracBrowser for help on using the repository browser.

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