VirtualBox

source: vbox/trunk/src/VBox/Main/src-server/HostPower.cpp@ 50213

Last change on this file since 50213 was 50175, checked in by vboxsync, 11 years ago

Main/HostPower: Introduced global extradata item HostPower/SavestateOnBatteryLow which can be set to "0" to disable automatic VM savestate if the battery reached a critical level

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 6.0 KB
Line 
1/** @file
2 *
3 * VirtualBox interface to host's power notification service
4 */
5
6/*
7 * Copyright (C) 2006-2013 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
19/*******************************************************************************
20* Header Files *
21*******************************************************************************/
22
23#include "HostPower.h"
24#include "Logging.h"
25
26#include <VBox/com/ptr.h>
27
28#include "VirtualBoxImpl.h"
29#include "MachineImpl.h"
30
31#include <iprt/mem.h>
32#include <iprt/cpp/utils.h>
33
34HostPowerService::HostPowerService(VirtualBox *aVirtualBox)
35{
36 AssertPtr(aVirtualBox);
37 mVirtualBox = aVirtualBox;
38
39 // keep this in sync with the host-specific implementations
40#if defined(RT_OS_WINDOWS) || defined(RT_OS_DARWIN)
41 Bstr bstrValue;
42 HRESULT hrc = mVirtualBox->GetExtraData(Bstr("HostPower/SavestateOnBatteryLow").raw(),
43 bstrValue.asOutParam());
44 if ( SUCCEEDED(hrc)
45 && ( bstrValue.isEmpty()
46 || bstrValue != "0"))
47 {
48 fSavestateOnBatteryLow = true;
49 LogRel(("Power: BatteryLow event will trigger VM savestate\n"));
50 }
51 else
52#endif
53 {
54 fSavestateOnBatteryLow = false;
55 LogRel(("Power: BatteryLow will be ignored\n"));
56 }
57}
58
59HostPowerService::~HostPowerService()
60{
61}
62
63void HostPowerService::notify(Reason_T aReason)
64{
65 SessionMachinesList machines;
66 VirtualBox::InternalControlList controls;
67
68 HRESULT rc = S_OK;
69
70 switch (aReason)
71 {
72 case Reason_HostSuspend:
73 {
74 LogFunc(("SUSPEND\n"));
75
76#ifdef VBOX_WITH_RESOURCE_USAGE_API
77 /* Suspend performance sampling to avoid unnecessary callbacks due to jumps in time. */
78 PerformanceCollector *perfcollector = mVirtualBox->performanceCollector();
79
80 if (perfcollector)
81 perfcollector->suspendSampling();
82#endif
83 mVirtualBox->getOpenedMachines(machines, &controls);
84
85 /* pause running VMs */
86 for (VirtualBox::InternalControlList::const_iterator it = controls.begin();
87 it != controls.end();
88 ++it)
89 {
90 ComPtr<IInternalSessionControl> pControl = *it;
91
92 /* PauseWithReason() will simply return a failure if
93 * the VM is in an inappropriate state */
94 rc = pControl->PauseWithReason(Reason_HostSuspend);
95 if (FAILED(rc))
96 continue;
97
98 /* save the control to un-pause the VM later */
99 mSessionControls.push_back(pControl);
100 }
101
102 LogFunc(("Suspended %d VMs\n", mSessionControls.size()));
103
104 break;
105 }
106
107 case Reason_HostResume:
108 {
109 LogFunc(("RESUME\n"));
110
111 size_t resumed = 0;
112
113 /* go through VMs we paused on Suspend */
114 for (size_t i = 0; i < mSessionControls.size(); ++i)
115 {
116 /* note that Resume() will simply return a failure if the VM is
117 * in an inappropriate state (it will also fail if the VM has
118 * been somehow closed by this time already so that the
119 * console reference we have is dead) */
120 rc = mSessionControls[i]->ResumeWithReason(Reason_HostResume);
121 if (FAILED(rc))
122 continue;
123
124 ++resumed;
125 }
126
127 LogFunc(("Resumed %d VMs\n", resumed));
128
129#ifdef VBOX_WITH_RESOURCE_USAGE_API
130 /* Resume the performance sampling. */
131 PerformanceCollector *perfcollector = mVirtualBox->performanceCollector();
132
133 if (perfcollector)
134 perfcollector->resumeSampling();
135#endif
136
137 mSessionControls.clear();
138
139 break;
140 }
141
142 case Reason_HostBatteryLow:
143 {
144 if (fSavestateOnBatteryLow)
145 {
146 LogFunc(("BATTERY LOW -- savestate running VMs\n"));
147
148 mVirtualBox->getOpenedMachines(machines, &controls);
149 size_t saved = 0;
150
151 /* save running VMs */
152 for (VirtualBox::InternalControlList::const_iterator it = controls.begin();
153 it != controls.end();
154 ++it)
155 {
156 ComPtr<IInternalSessionControl> pControl = *it;
157 ComPtr<IProgress> progress;
158
159 /* note that SaveStateWithReason() will simply return a failure
160 * if the VM is in an inappropriate state */
161 rc = pControl->SaveStateWithReason(Reason_HostBatteryLow, progress.asOutParam());
162 if (FAILED(rc))
163 continue;
164
165 /* Wait until the operation has been completed. */
166 rc = progress->WaitForCompletion(-1);
167 if (SUCCEEDED(rc))
168 {
169 LONG iRc;
170 progress->COMGETTER(ResultCode)(&iRc);
171 rc = iRc;
172 }
173
174 AssertMsg(SUCCEEDED(rc), ("SaveState WaitForCompletion failed with %Rhrc (%#08X)\n", rc, rc));
175
176 if (SUCCEEDED(rc))
177 ++saved;
178 }
179 LogFunc(("Saved %d VMs\n", saved));
180 }
181 else
182 {
183 LogFunc(("BATTERY LOW -- no action\n"));
184 }
185
186 break;
187 }
188
189 default:
190 /* nothing */;
191 }
192}
193/* vi: set tabstop=4 shiftwidth=4 expandtab: */
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