summaryrefslogtreecommitdiffstats
path: root/src/VBox/Runtime/r3/solaris/systemmem-solaris.cpp
blob: db1368d57f12f5b5dde260b55fe3a0dd3ba83053 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/* $Id: systemmem-solaris.cpp $ */
/** @file
 * IPRT - RTSystemQueryTotalRam, Solaris ring-3.
 */

/*
 * Copyright (C) 2012-2019 Oracle Corporation
 *
 * This file is part of VirtualBox Open Source Edition (OSE), as
 * available from http://www.virtualbox.org. This file is free software;
 * you can redistribute it and/or modify it under the terms of the GNU
 * General Public License (GPL) as published by the Free Software
 * Foundation, in version 2 as it comes in the "COPYING" file of the
 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
 *
 * The contents of this file may alternatively be used under the terms
 * of the Common Development and Distribution License Version 1.0
 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
 * VirtualBox OSE distribution, in which case the provisions of the
 * CDDL are applicable instead of those of the GPL.
 *
 * You may elect to license modified versions of this file under the
 * terms and conditions of either the GPL or the CDDL or both.
 */


/*********************************************************************************************************************************
*   Header Files                                                                                                                 *
*********************************************************************************************************************************/
#include <iprt/system.h>
#include "internal/iprt.h"

#include <iprt/errcore.h>
#include <iprt/assert.h>
#include <iprt/critsect.h>
#include <iprt/once.h>
#include <iprt/param.h>

#include <errno.h>
#include <kstat.h>


/*********************************************************************************************************************************
*   Global Variables                                                                                                             *
*********************************************************************************************************************************/
/** Initialize globals once. */
static RTONCE           g_rtSysMemSolInitOnce = RTONCE_INITIALIZER;
/** Critical section serializing access to g_pKStatCtl and the other handles. */
static RTCRITSECT       g_rtSysMemSolCritSect;
/** The kstate control handle. */
static kstat_ctl_t     *g_pKStatCtl     = NULL;
/** The unix.system_pages handle. */
static kstat_t         *g_pUnixSysPages = NULL;
/** The zfs.archstats handle. */
static kstat_t         *g_pZfsArcStats  = NULL;


/** @callback_method_impl{FNRTONCE} */
static DECLCALLBACK(int) rtSysMemSolInit(void *pvUser)
{
    int rc = RTCritSectInit(&g_rtSysMemSolCritSect);
    if (RT_SUCCESS(rc))
    {
        g_pKStatCtl = kstat_open();
        if (g_pKStatCtl)
        {
            g_pUnixSysPages = kstat_lookup(g_pKStatCtl, (char *)"unix", 0, (char *)"system_pages");
            if (g_pUnixSysPages)
            {
                g_pZfsArcStats = kstat_lookup(g_pKStatCtl, (char *)"zfs",  0, (char *)"arcstats"); /* allow NULL */
                return VINF_SUCCESS;
            }

            rc = RTErrConvertFromErrno(errno);
            kstat_close(g_pKStatCtl);
            g_pKStatCtl = NULL;
        }
        else
            rc = RTErrConvertFromErrno(errno);
    }
    return rc;
}


/** @callback_method_impl{FNRTONCECLEANUP} */
static DECLCALLBACK(void) rtSysMemSolCleanUp(void *pvUser, bool fLazyCleanUpOk)
{
    RTCritSectDelete(&g_rtSysMemSolCritSect);

    kstat_close(g_pKStatCtl);
    g_pKStatCtl     = NULL;
    g_pUnixSysPages = NULL;
    g_pZfsArcStats  = NULL;

    NOREF(pvUser); NOREF(fLazyCleanUpOk);
}



RTDECL(int) RTSystemQueryTotalRam(uint64_t *pcb)
{
    AssertPtrReturn(pcb, VERR_INVALID_POINTER);

    int rc = RTOnceEx(&g_rtSysMemSolInitOnce, rtSysMemSolInit, rtSysMemSolCleanUp, NULL);
    if (RT_SUCCESS(rc))
    {
        rc = RTCritSectEnter(&g_rtSysMemSolCritSect);
        if (RT_SUCCESS(rc))
        {
            if (kstat_read(g_pKStatCtl, g_pUnixSysPages, NULL) != -1)
            {
                kstat_named_t *pData = (kstat_named_t *)kstat_data_lookup(g_pUnixSysPages, (char *)"physmem");
                if (pData)
                    *pcb = (uint64_t)pData->value.ul * PAGE_SIZE;
                else
                    rc = RTErrConvertFromErrno(errno);
            }
            else
                rc = RTErrConvertFromErrno(errno);
            RTCritSectLeave(&g_rtSysMemSolCritSect);
        }
    }
    return rc;
}


RTDECL(int) RTSystemQueryAvailableRam(uint64_t *pcb)
{
    AssertPtrReturn(pcb, VERR_INVALID_POINTER);

    int rc = RTOnceEx(&g_rtSysMemSolInitOnce, rtSysMemSolInit, rtSysMemSolCleanUp, NULL);
    if (RT_SUCCESS(rc))
    {
        rc = RTCritSectEnter(&g_rtSysMemSolCritSect);
        if (RT_SUCCESS(rc))
        {
            if (kstat_read(g_pKStatCtl, g_pUnixSysPages, NULL) != -1)
            {
                kstat_named_t *pData = (kstat_named_t *)kstat_data_lookup(g_pUnixSysPages, (char *)"freemem");
                if (pData)
                {
                    *pcb = (uint64_t)pData->value.ul * PAGE_SIZE;

                    /*
                     * Adjust for ZFS greedyness if possible.
                     * (c_min is the target minimum size of the cache, it is not
                     * an absolute minimum.)
                     */
                    if (   g_pZfsArcStats
                        && kstat_read(g_pKStatCtl, g_pZfsArcStats, NULL) != -1)
                    {
                        kstat_named_t *pCurSize = (kstat_named_t *)kstat_data_lookup(g_pZfsArcStats, (char *)"size");
                        kstat_named_t *pMinSize = (kstat_named_t *)kstat_data_lookup(g_pZfsArcStats, (char *)"c_min");
                        if (   pCurSize
                            && pMinSize
                            && pCurSize->value.ul > pMinSize->value.ul)
                        {
                            *pcb += pCurSize->value.ul - pMinSize->value.ul;
                        }
                    }
                }
                else
                    rc = RTErrConvertFromErrno(errno);
            }

            RTCritSectLeave(&g_rtSysMemSolCritSect);
        }
    }
    return rc;
}