1 | /* $Id: systemmem-freebsd.cpp 62477 2016-07-22 18:27:37Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - RTSystemQueryTotalRam, Linux ring-3.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2012-2016 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/err.h>
|
---|
35 | #include <iprt/assert.h>
|
---|
36 | #include <iprt/string.h>
|
---|
37 |
|
---|
38 | #include <sys/types.h>
|
---|
39 | #include <sys/sysctl.h>
|
---|
40 | #include <errno.h>
|
---|
41 |
|
---|
42 |
|
---|
43 | RTDECL(int) RTSystemQueryTotalRam(uint64_t *pcb)
|
---|
44 | {
|
---|
45 | int rc = VINF_SUCCESS;
|
---|
46 | u_long cbMemPhys = 0;
|
---|
47 | size_t cbParameter = sizeof(cbMemPhys);
|
---|
48 |
|
---|
49 | AssertPtrReturn(pcb, VERR_INVALID_POINTER);
|
---|
50 |
|
---|
51 | if (!sysctlbyname("hw.physmem", &cbMemPhys, &cbParameter, NULL, 0))
|
---|
52 | {
|
---|
53 | *pcb = cbMemPhys;
|
---|
54 | return VINF_SUCCESS;
|
---|
55 | }
|
---|
56 | return RTErrConvertFromErrno(errno);
|
---|
57 | }
|
---|
58 |
|
---|
59 |
|
---|
60 | RTDECL(int) RTSystemQueryAvailableRam(uint64_t *pcb)
|
---|
61 | {
|
---|
62 | AssertPtrReturn(pcb, VERR_INVALID_POINTER);
|
---|
63 |
|
---|
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;
|
---|
97 | }
|
---|
98 |
|
---|