1 | /* $Id: UIGuestRAMSlider.cpp 76553 2019-01-01 01:45:53Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBox Qt GUI - UIGuestRAMSlider class implementation.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2009-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 |
|
---|
18 | #ifdef VBOX_WITH_PRECOMPILED_HEADERS
|
---|
19 | # include <precomp.h>
|
---|
20 | #else /* !VBOX_WITH_PRECOMPILED_HEADERS */
|
---|
21 |
|
---|
22 | /* GUI includes: */
|
---|
23 | # include "VBoxGlobal.h"
|
---|
24 | # include "UIGuestRAMSlider.h"
|
---|
25 |
|
---|
26 | /* COM includes: */
|
---|
27 | # include "CSystemProperties.h"
|
---|
28 |
|
---|
29 | #endif /* !VBOX_WITH_PRECOMPILED_HEADERS */
|
---|
30 |
|
---|
31 |
|
---|
32 | UIGuestRAMSlider::UIGuestRAMSlider(QWidget *pParent /* = 0 */)
|
---|
33 | : QIAdvancedSlider(pParent)
|
---|
34 | , m_uMinRAM(0)
|
---|
35 | , m_uMaxRAMOpt(0)
|
---|
36 | , m_uMaxRAMAlw(0)
|
---|
37 | , m_uMaxRAM(0)
|
---|
38 | {
|
---|
39 | /* Prepare: */
|
---|
40 | prepare();
|
---|
41 | }
|
---|
42 |
|
---|
43 | UIGuestRAMSlider::UIGuestRAMSlider(Qt::Orientation enmOrientation, QWidget *pParent /* = 0 */)
|
---|
44 | : QIAdvancedSlider(enmOrientation, pParent)
|
---|
45 | , m_uMinRAM(0)
|
---|
46 | , m_uMaxRAMOpt(0)
|
---|
47 | , m_uMaxRAMAlw(0)
|
---|
48 | , m_uMaxRAM(0)
|
---|
49 | {
|
---|
50 | /* Prepare: */
|
---|
51 | prepare();
|
---|
52 | }
|
---|
53 |
|
---|
54 | uint UIGuestRAMSlider::minRAM() const
|
---|
55 | {
|
---|
56 | return m_uMinRAM;
|
---|
57 | }
|
---|
58 |
|
---|
59 | uint UIGuestRAMSlider::maxRAMOpt() const
|
---|
60 | {
|
---|
61 | return m_uMaxRAMOpt;
|
---|
62 | }
|
---|
63 |
|
---|
64 | uint UIGuestRAMSlider::maxRAMAlw() const
|
---|
65 | {
|
---|
66 | return m_uMaxRAMAlw;
|
---|
67 | }
|
---|
68 |
|
---|
69 | uint UIGuestRAMSlider::maxRAM() const
|
---|
70 | {
|
---|
71 | return m_uMaxRAM;
|
---|
72 | }
|
---|
73 |
|
---|
74 | void UIGuestRAMSlider::prepare()
|
---|
75 | {
|
---|
76 | ulong uFullSize = vboxGlobal().host().GetMemorySize();
|
---|
77 | CSystemProperties sys = vboxGlobal().virtualBox().GetSystemProperties();
|
---|
78 | m_uMinRAM = sys.GetMinGuestRAM();
|
---|
79 | m_uMaxRAM = RT_MIN(RT_ALIGN(uFullSize, _1G / _1M), sys.GetMaxGuestRAM());
|
---|
80 |
|
---|
81 | /* Come up with some nice round percent boundaries relative to
|
---|
82 | * the system memory. A max of 75% on a 256GB config is ridiculous,
|
---|
83 | * even on an 8GB rig reserving 2GB for the OS is way to conservative.
|
---|
84 | * The max numbers can be estimated using the following program:
|
---|
85 | *
|
---|
86 | * double calcMaxPct(uint64_t cbRam)
|
---|
87 | * {
|
---|
88 | * double cbRamOverhead = cbRam * 0.0390625; // 160 bytes per page.
|
---|
89 | * double cbRamForTheOS = RT_MAX(RT_MIN(_512M, cbRam * 0.25), _64M);
|
---|
90 | * double OSPct = (cbRamOverhead + cbRamForTheOS) * 100.0 / cbRam;
|
---|
91 | * double MaxPct = 100 - OSPct;
|
---|
92 | * return MaxPct;
|
---|
93 | * }
|
---|
94 | *
|
---|
95 | * int main()
|
---|
96 | * {
|
---|
97 | * uint64_t cbRam = _1G;
|
---|
98 | * for (; !(cbRam >> 33); cbRam += _1G)
|
---|
99 | * printf("%8lluGB %.1f%% %8lluKB\n", cbRam >> 30, calcMaxPct(cbRam),
|
---|
100 | * (uint64_t)(cbRam * calcMaxPct(cbRam) / 100.0) >> 20);
|
---|
101 | * for (; !(cbRam >> 51); cbRam <<= 1)
|
---|
102 | * printf("%8lluGB %.1f%% %8lluKB\n", cbRam >> 30, calcMaxPct(cbRam),
|
---|
103 | * (uint64_t)(cbRam * calcMaxPct(cbRam) / 100.0) >> 20);
|
---|
104 | * return 0;
|
---|
105 | * }
|
---|
106 | *
|
---|
107 | * Note. We might wanna put these calculations somewhere global later. */
|
---|
108 |
|
---|
109 | /* System RAM amount test: */
|
---|
110 | m_uMaxRAMAlw = (uint)(0.75 * uFullSize);
|
---|
111 | m_uMaxRAMOpt = (uint)(0.50 * uFullSize);
|
---|
112 | if (uFullSize < 3072)
|
---|
113 | /* done */;
|
---|
114 | else if (uFullSize < 4096) /* 3GB */
|
---|
115 | m_uMaxRAMAlw = (uint)(0.80 * uFullSize);
|
---|
116 | else if (uFullSize < 6144) /* 4-5GB */
|
---|
117 | {
|
---|
118 | m_uMaxRAMAlw = (uint)(0.84 * uFullSize);
|
---|
119 | m_uMaxRAMOpt = (uint)(0.60 * uFullSize);
|
---|
120 | }
|
---|
121 | else if (uFullSize < 8192) /* 6-7GB */
|
---|
122 | {
|
---|
123 | m_uMaxRAMAlw = (uint)(0.88 * uFullSize);
|
---|
124 | m_uMaxRAMOpt = (uint)(0.65 * uFullSize);
|
---|
125 | }
|
---|
126 | else if (uFullSize < 16384) /* 8-15GB */
|
---|
127 | {
|
---|
128 | m_uMaxRAMAlw = (uint)(0.90 * uFullSize);
|
---|
129 | m_uMaxRAMOpt = (uint)(0.70 * uFullSize);
|
---|
130 | }
|
---|
131 | else if (uFullSize < 32768) /* 16-31GB */
|
---|
132 | {
|
---|
133 | m_uMaxRAMAlw = (uint)(0.93 * uFullSize);
|
---|
134 | m_uMaxRAMOpt = (uint)(0.75 * uFullSize);
|
---|
135 | }
|
---|
136 | else if (uFullSize < 65536) /* 32-63GB */
|
---|
137 | {
|
---|
138 | m_uMaxRAMAlw = (uint)(0.94 * uFullSize);
|
---|
139 | m_uMaxRAMOpt = (uint)(0.80 * uFullSize);
|
---|
140 | }
|
---|
141 | else if (uFullSize < 131072) /* 64-127GB */
|
---|
142 | {
|
---|
143 | m_uMaxRAMAlw = (uint)(0.95 * uFullSize);
|
---|
144 | m_uMaxRAMOpt = (uint)(0.85 * uFullSize);
|
---|
145 | }
|
---|
146 | else /* 128GB- */
|
---|
147 | {
|
---|
148 | m_uMaxRAMAlw = (uint)(0.96 * uFullSize);
|
---|
149 | m_uMaxRAMOpt = (uint)(0.90 * uFullSize);
|
---|
150 | }
|
---|
151 | /* Now check the calculated maximums are out of the range for the guest
|
---|
152 | * RAM. If so change it accordingly. */
|
---|
153 | m_uMaxRAMAlw = RT_MIN(m_uMaxRAMAlw, m_uMaxRAM);
|
---|
154 | m_uMaxRAMOpt = RT_MIN(m_uMaxRAMOpt, m_uMaxRAM);
|
---|
155 |
|
---|
156 | setPageStep(calcPageStep(m_uMaxRAM));
|
---|
157 | setSingleStep(pageStep() / 4);
|
---|
158 | setTickInterval(pageStep());
|
---|
159 | /* Setup the scale so that ticks are at page step boundaries */
|
---|
160 | if (m_uMinRAM >= static_cast<uint>(pageStep()))
|
---|
161 | setMinimum((m_uMinRAM / pageStep()) * pageStep());
|
---|
162 | else
|
---|
163 | setMinimum(m_uMinRAM);
|
---|
164 |
|
---|
165 | setMaximum(m_uMaxRAM);
|
---|
166 | setSnappingEnabled(true);
|
---|
167 | setOptimalHint(m_uMinRAM, m_uMaxRAMOpt);
|
---|
168 | setWarningHint(m_uMaxRAMOpt, m_uMaxRAMAlw);
|
---|
169 | setErrorHint(m_uMaxRAMAlw, m_uMaxRAM);
|
---|
170 | }
|
---|
171 |
|
---|
172 | int UIGuestRAMSlider::calcPageStep(int iMaximum) const
|
---|
173 | {
|
---|
174 | /* Calculate a suitable page step size for the given max value.
|
---|
175 | * The returned size is so that there will be no more than 32
|
---|
176 | * pages. The minimum returned page size is 4. */
|
---|
177 |
|
---|
178 | /* Reasonable max. number of page steps is 32: */
|
---|
179 | uint uPage = ((uint)iMaximum + 31) / 32;
|
---|
180 | /* Make it a power of 2: */
|
---|
181 | uint p = uPage, p2 = 0x1;
|
---|
182 | while ((p >>= 1))
|
---|
183 | p2 <<= 1;
|
---|
184 | if (uPage != p2)
|
---|
185 | p2 <<= 1;
|
---|
186 | if (p2 < 4)
|
---|
187 | p2 = 4;
|
---|
188 | return (int) p2;
|
---|
189 | }
|
---|