VirtualBox

source: vbox/trunk/src/VBox/Runtime/r3/solaris/systemmem-solaris.cpp

Last change on this file was 106061, checked in by vboxsync, 2 months ago

Copyright year updates by scm.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 6.4 KB
Line 
1/* $Id: systemmem-solaris.cpp 106061 2024-09-16 14:03:52Z vboxsync $ */
2/** @file
3 * IPRT - RTSystemQueryTotalRam, Solaris ring-3.
4 */
5
6/*
7 * Copyright (C) 2012-2024 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/critsect.h>
47#include <iprt/once.h>
48#include <iprt/param.h>
49
50#include <errno.h>
51#include <kstat.h>
52
53
54/*********************************************************************************************************************************
55* Global Variables *
56*********************************************************************************************************************************/
57/** Initialize globals once. */
58static RTONCE g_rtSysMemSolInitOnce = RTONCE_INITIALIZER;
59/** Critical section serializing access to g_pKStatCtl and the other handles. */
60static RTCRITSECT g_rtSysMemSolCritSect;
61/** The kstate control handle. */
62static kstat_ctl_t *g_pKStatCtl = NULL;
63/** The unix.system_pages handle. */
64static kstat_t *g_pUnixSysPages = NULL;
65/** The zfs.archstats handle. */
66static kstat_t *g_pZfsArcStats = NULL;
67
68
69/** @callback_method_impl{FNRTONCE} */
70static DECLCALLBACK(int) rtSysMemSolInit(void *pvUser)
71{
72 int rc = RTCritSectInit(&g_rtSysMemSolCritSect);
73 if (RT_SUCCESS(rc))
74 {
75 g_pKStatCtl = kstat_open();
76 if (g_pKStatCtl)
77 {
78 g_pUnixSysPages = kstat_lookup(g_pKStatCtl, (char *)"unix", 0, (char *)"system_pages");
79 if (g_pUnixSysPages)
80 {
81 g_pZfsArcStats = kstat_lookup(g_pKStatCtl, (char *)"zfs", 0, (char *)"arcstats"); /* allow NULL */
82 return VINF_SUCCESS;
83 }
84
85 rc = RTErrConvertFromErrno(errno);
86 kstat_close(g_pKStatCtl);
87 g_pKStatCtl = NULL;
88 }
89 else
90 rc = RTErrConvertFromErrno(errno);
91 }
92 return rc;
93}
94
95
96/** @callback_method_impl{FNRTONCECLEANUP} */
97static DECLCALLBACK(void) rtSysMemSolCleanUp(void *pvUser, bool fLazyCleanUpOk)
98{
99 RTCritSectDelete(&g_rtSysMemSolCritSect);
100
101 kstat_close(g_pKStatCtl);
102 g_pKStatCtl = NULL;
103 g_pUnixSysPages = NULL;
104 g_pZfsArcStats = NULL;
105
106 NOREF(pvUser); NOREF(fLazyCleanUpOk);
107}
108
109
110
111RTDECL(int) RTSystemQueryTotalRam(uint64_t *pcb)
112{
113 AssertPtrReturn(pcb, VERR_INVALID_POINTER);
114
115 int rc = RTOnceEx(&g_rtSysMemSolInitOnce, rtSysMemSolInit, rtSysMemSolCleanUp, NULL);
116 if (RT_SUCCESS(rc))
117 {
118 rc = RTCritSectEnter(&g_rtSysMemSolCritSect);
119 if (RT_SUCCESS(rc))
120 {
121 if (kstat_read(g_pKStatCtl, g_pUnixSysPages, NULL) != -1)
122 {
123 kstat_named_t *pData = (kstat_named_t *)kstat_data_lookup(g_pUnixSysPages, (char *)"physmem");
124 if (pData)
125 *pcb = (uint64_t)pData->value.ul * PAGE_SIZE;
126 else
127 rc = RTErrConvertFromErrno(errno);
128 }
129 else
130 rc = RTErrConvertFromErrno(errno);
131 RTCritSectLeave(&g_rtSysMemSolCritSect);
132 }
133 }
134 return rc;
135}
136
137
138RTDECL(int) RTSystemQueryAvailableRam(uint64_t *pcb)
139{
140 AssertPtrReturn(pcb, VERR_INVALID_POINTER);
141
142 int rc = RTOnceEx(&g_rtSysMemSolInitOnce, rtSysMemSolInit, rtSysMemSolCleanUp, NULL);
143 if (RT_SUCCESS(rc))
144 {
145 rc = RTCritSectEnter(&g_rtSysMemSolCritSect);
146 if (RT_SUCCESS(rc))
147 {
148 if (kstat_read(g_pKStatCtl, g_pUnixSysPages, NULL) != -1)
149 {
150 kstat_named_t *pData = (kstat_named_t *)kstat_data_lookup(g_pUnixSysPages, (char *)"freemem");
151 if (pData)
152 {
153 *pcb = (uint64_t)pData->value.ul * PAGE_SIZE;
154
155 /*
156 * Adjust for ZFS greedyness if possible.
157 * (c_min is the target minimum size of the cache, it is not
158 * an absolute minimum.)
159 */
160 if ( g_pZfsArcStats
161 && kstat_read(g_pKStatCtl, g_pZfsArcStats, NULL) != -1)
162 {
163 kstat_named_t *pCurSize = (kstat_named_t *)kstat_data_lookup(g_pZfsArcStats, (char *)"size");
164 kstat_named_t *pMinSize = (kstat_named_t *)kstat_data_lookup(g_pZfsArcStats, (char *)"c_min");
165 if ( pCurSize
166 && pMinSize
167 && pCurSize->value.ul > pMinSize->value.ul)
168 {
169 *pcb += pCurSize->value.ul - pMinSize->value.ul;
170 }
171 }
172 }
173 else
174 rc = RTErrConvertFromErrno(errno);
175 }
176
177 RTCritSectLeave(&g_rtSysMemSolCritSect);
178 }
179 }
180 return rc;
181}
182
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