~ubuntu-branches/ubuntu/raring/virtualbox-ose/raring

« back to all changes in this revision

Viewing changes to src/VBox/Frontends/VirtualBox/src/widgets/VBoxGuestRAMSlider.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Felix Geyer
  • Date: 2009-12-18 16:44:29 UTC
  • mfrom: (0.3.3 upstream) (0.4.6 squeeze)
  • Revision ID: james.westby@ubuntu.com-20091218164429-jd34ccexpv5na11a
Tags: 3.1.2-dfsg-1ubuntu1
* Merge from Debian unstable (LP: #498219), remaining changes:
  - Disable update action
    - debian/patches/u01-disable-update-action.dpatch
  - VirtualBox should go in Accessories, not in System tools (LP: #288590)
    - debian/virtualbox-ose-qt.files/virtualbox-ose.desktop
  - Add Apport hook
    - debian/virtualbox-ose.files/source_virtualbox-ose.py
    - debian/virtualbox-ose.install
  - Add Launchpad integration
    - debian/control
    - debian/lpi-bug.xpm
    - debian/patches/u02-lp-integration.dpatch
* Fixes the following bugs:
  - Kernel module fails to build with Linux >= 2.6.32 (LP: #474625)
  - X.Org drivers need to be rebuilt against X-Server 1.7 (LP: #495935)
  - The *-source packages try to build the kernel modules even though the
    kernel headers aren't available (LP: #473334)
* Replace *-source packages with transitional packages for *-dkms.
* Adapt u01-disable-update-action.dpatch and u02-lp-integration.dpatch for
  new upstream version.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/** @file
 
2
 *
 
3
 * VBox frontends: Qt GUI ("VirtualBox"):
 
4
 * VirtualBox Qt extensions: VBoxGuestRAMSlider class implementation
 
5
 */
 
6
 
 
7
/*
 
8
 * Copyright (C) 2009 Sun Microsystems, Inc.
 
9
 *
 
10
 * This file is part of VirtualBox Open Source Edition (OSE), as
 
11
 * available from http://www.virtualbox.org. This file is free software;
 
12
 * you can redistribute it and/or modify it under the terms of the GNU
 
13
 * General Public License (GPL) as published by the Free Software
 
14
 * Foundation, in version 2 as it comes in the "COPYING" file of the
 
15
 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
 
16
 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
 
17
 *
 
18
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
 
19
 * Clara, CA 95054 USA or visit http://www.sun.com if you need
 
20
 * additional information or have any questions.
 
21
 */
 
22
 
 
23
/* VBox includes */
 
24
#include "VBoxGuestRAMSlider.h"
 
25
#include "VBoxGlobal.h"
 
26
 
 
27
VBoxGuestRAMSlider::VBoxGuestRAMSlider (QWidget *aParent /* = 0 */)
 
28
  : QIAdvancedSlider (aParent)
 
29
  , mMinRAM (0)
 
30
  , mMaxRAMOpt (0)
 
31
  , mMaxRAMAlw (0)
 
32
  , mMaxRAM (0)
 
33
{
 
34
    init();
 
35
}
 
36
 
 
37
VBoxGuestRAMSlider::VBoxGuestRAMSlider (Qt::Orientation aOrientation, QWidget *aParent /* = 0 */)
 
38
  : QIAdvancedSlider (aOrientation, aParent)
 
39
  , mMinRAM (0)
 
40
  , mMaxRAMOpt (0)
 
41
  , mMaxRAMAlw (0)
 
42
  , mMaxRAM (0)
 
43
{
 
44
    init();
 
45
}
 
46
 
 
47
uint VBoxGuestRAMSlider::minRAM() const
 
48
{
 
49
    return mMinRAM;
 
50
}
 
51
 
 
52
uint VBoxGuestRAMSlider::maxRAMOpt() const
 
53
{
 
54
    return mMaxRAMOpt;
 
55
}
 
56
 
 
57
uint VBoxGuestRAMSlider::maxRAMAlw() const
 
58
{
 
59
    return mMaxRAMAlw;
 
60
}
 
61
 
 
62
uint VBoxGuestRAMSlider::maxRAM() const
 
63
{
 
64
    return mMaxRAM;
 
65
}
 
66
 
 
67
void VBoxGuestRAMSlider::init()
 
68
{
 
69
    ulong fullSize = vboxGlobal().virtualBox().GetHost().GetMemorySize();
 
70
    CSystemProperties sys = vboxGlobal().virtualBox().GetSystemProperties();
 
71
    mMinRAM = sys.GetMinGuestRAM();
 
72
    mMaxRAM = RT_MIN (RT_ALIGN (fullSize, _1G / _1M), sys.GetMaxGuestRAM());
 
73
 
 
74
    /* Come up with some nice round percent boundaries relative to
 
75
     * the system memory. A max of 75% on a 256GB config is ridiculous,
 
76
     * even on an 8GB rig reserving 2GB for the OS is way to conservative.
 
77
     * The max numbers can be estimated using the following program:
 
78
     *
 
79
     *      double calcMaxPct(uint64_t cbRam)
 
80
     *      {
 
81
     *          double cbRamOverhead = cbRam * 0.0390625; // 160 bytes per page.
 
82
     *          double cbRamForTheOS = RT_MAX(RT_MIN(_512M, cbRam * 0.25), _64M);
 
83
     *          double OSPct  = (cbRamOverhead + cbRamForTheOS) * 100.0 / cbRam;
 
84
     *          double MaxPct = 100 - OSPct;
 
85
     *          return MaxPct;
 
86
     *      }
 
87
     *
 
88
     *      int main()
 
89
     *      {
 
90
     *          uint64_t cbRam = _1G;
 
91
     *          for (; !(cbRam >> 33); cbRam += _1G)
 
92
     *              printf("%8lluGB %.1f%% %8lluKB\n", cbRam >> 30, calcMaxPct(cbRam),
 
93
     *                     (uint64_t)(cbRam * calcMaxPct(cbRam) / 100.0) >> 20);
 
94
     *          for (; !(cbRam >> 51); cbRam <<= 1)
 
95
     *              printf("%8lluGB %.1f%% %8lluKB\n", cbRam >> 30, calcMaxPct(cbRam),
 
96
     *                     (uint64_t)(cbRam * calcMaxPct(cbRam) / 100.0) >> 20);
 
97
     *          return 0;
 
98
     *      }
 
99
     *
 
100
     * Note. We might wanna put these calculations somewhere global later. */
 
101
 
 
102
    /* System RAM amount test */
 
103
    mMaxRAMAlw  = (uint)(0.75 * fullSize);
 
104
    mMaxRAMOpt  = (uint)(0.50 * fullSize);
 
105
    if (fullSize < 3072)
 
106
        /* done */;
 
107
    else if (fullSize < 4096)   /* 3GB */
 
108
        mMaxRAMAlw = (uint)(0.80 * fullSize);
 
109
    else if (fullSize < 6144)   /* 4-5GB */
 
110
    {
 
111
        mMaxRAMAlw = (uint)(0.84 * fullSize);
 
112
        mMaxRAMOpt = (uint)(0.60 * fullSize);
 
113
    }
 
114
    else if (fullSize < 8192)   /* 6-7GB */
 
115
    {
 
116
        mMaxRAMAlw = (uint)(0.88 * fullSize);
 
117
        mMaxRAMOpt = (uint)(0.65 * fullSize);
 
118
    }
 
119
    else if (fullSize < 16384)  /* 8-15GB */
 
120
    {
 
121
        mMaxRAMAlw = (uint)(0.90 * fullSize);
 
122
        mMaxRAMOpt = (uint)(0.70 * fullSize);
 
123
    }
 
124
    else if (fullSize < 32768)  /* 16-31GB */
 
125
    {
 
126
        mMaxRAMAlw = (uint)(0.93 * fullSize);
 
127
        mMaxRAMOpt = (uint)(0.75 * fullSize);
 
128
    }
 
129
    else if (fullSize < 65536)  /* 32-63GB */
 
130
    {
 
131
        mMaxRAMAlw = (uint)(0.94 * fullSize);
 
132
        mMaxRAMOpt = (uint)(0.80 * fullSize);
 
133
    }
 
134
    else if (fullSize < 131072) /* 64-127GB */
 
135
    {
 
136
        mMaxRAMAlw = (uint)(0.95 * fullSize);
 
137
        mMaxRAMOpt = (uint)(0.85 * fullSize);
 
138
    }
 
139
    else                        /* 128GB- */
 
140
    {
 
141
        mMaxRAMAlw = (uint)(0.96 * fullSize);
 
142
        mMaxRAMOpt = (uint)(0.90 * fullSize);
 
143
    }
 
144
    /* Now check the calculated maximums are out of the range for the guest
 
145
     * RAM. If so change it accordingly. */
 
146
    mMaxRAMAlw  = RT_MIN (mMaxRAMAlw, mMaxRAM);
 
147
    mMaxRAMOpt  = RT_MIN (mMaxRAMOpt, mMaxRAM);
 
148
 
 
149
    setPageStep (calcPageStep (mMaxRAM));
 
150
    setSingleStep (pageStep() / 4);
 
151
    setTickInterval (pageStep());
 
152
    /* Setup the scale so that ticks are at page step boundaries */
 
153
    setMinimum ((mMinRAM / pageStep()) * pageStep());
 
154
    setMaximum (mMaxRAM);
 
155
    setSnappingEnabled (true);
 
156
    setOptimalHint (mMinRAM, mMaxRAMOpt);
 
157
    setWarningHint (mMaxRAMOpt, mMaxRAMAlw);
 
158
    setErrorHint (mMaxRAMAlw, mMaxRAM);
 
159
}
 
160
 
 
161
/**
 
162
 *  Calculates a suitable page step size for the given max value. The returned
 
163
 *  size is so that there will be no more than 32 pages. The minimum returned
 
164
 *  page size is 4.
 
165
 */
 
166
int VBoxGuestRAMSlider::calcPageStep (int aMax) const
 
167
{
 
168
    /* reasonable max. number of page steps is 32 */
 
169
    uint page = ((uint) aMax + 31) / 32;
 
170
    /* make it a power of 2 */
 
171
    uint p = page, p2 = 0x1;
 
172
    while ((p >>= 1))
 
173
        p2 <<= 1;
 
174
    if (page != p2)
 
175
        p2 <<= 1;
 
176
    if (p2 < 4)
 
177
        p2 = 4;
 
178
    return (int) p2;
 
179
}
 
180