1 | /* $Id: HostPower.cpp 76592 2019-01-01 20:13:07Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VirtualBox interface to host's power notification service
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-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 |
|
---|
19 | /*********************************************************************************************************************************
|
---|
20 | * Header Files *
|
---|
21 | *********************************************************************************************************************************/
|
---|
22 | #define LOG_GROUP LOG_GROUP_MAIN_HOST
|
---|
23 | #include "HostPower.h"
|
---|
24 | #include "LoggingNew.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 |
|
---|
34 | HostPowerService::HostPowerService(VirtualBox *aVirtualBox)
|
---|
35 | {
|
---|
36 | AssertPtr(aVirtualBox);
|
---|
37 | mVirtualBox = aVirtualBox;
|
---|
38 | }
|
---|
39 |
|
---|
40 | HostPowerService::~HostPowerService()
|
---|
41 | {
|
---|
42 | }
|
---|
43 |
|
---|
44 | void HostPowerService::notify(Reason_T aReason)
|
---|
45 | {
|
---|
46 | SessionMachinesList machines;
|
---|
47 | VirtualBox::InternalControlList controls;
|
---|
48 |
|
---|
49 | HRESULT rc = S_OK;
|
---|
50 |
|
---|
51 | switch (aReason)
|
---|
52 | {
|
---|
53 | case Reason_HostSuspend:
|
---|
54 | {
|
---|
55 | LogFunc(("HOST SUSPEND\n"));
|
---|
56 |
|
---|
57 | #ifdef VBOX_WITH_RESOURCE_USAGE_API
|
---|
58 | /* Suspend performance sampling to avoid unnecessary callbacks due to jumps in time. */
|
---|
59 | PerformanceCollector *perfcollector = mVirtualBox->i_performanceCollector();
|
---|
60 |
|
---|
61 | if (perfcollector)
|
---|
62 | perfcollector->suspendSampling();
|
---|
63 | #endif
|
---|
64 | mVirtualBox->i_getOpenedMachines(machines, &controls);
|
---|
65 |
|
---|
66 | /* pause running VMs */
|
---|
67 | for (VirtualBox::InternalControlList::const_iterator it = controls.begin();
|
---|
68 | it != controls.end();
|
---|
69 | ++it)
|
---|
70 | {
|
---|
71 | ComPtr<IInternalSessionControl> pControl = *it;
|
---|
72 |
|
---|
73 | /* PauseWithReason() will simply return a failure if
|
---|
74 | * the VM is in an inappropriate state */
|
---|
75 | rc = pControl->PauseWithReason(Reason_HostSuspend);
|
---|
76 | if (FAILED(rc))
|
---|
77 | continue;
|
---|
78 |
|
---|
79 | /* save the control to un-pause the VM later */
|
---|
80 | mSessionControls.push_back(pControl);
|
---|
81 | }
|
---|
82 |
|
---|
83 | LogRel(("Host suspending: Paused %d VMs\n", mSessionControls.size()));
|
---|
84 | break;
|
---|
85 | }
|
---|
86 |
|
---|
87 | case Reason_HostResume:
|
---|
88 | {
|
---|
89 | LogFunc(("HOST RESUME\n"));
|
---|
90 |
|
---|
91 | size_t resumed = 0;
|
---|
92 |
|
---|
93 | /* go through VMs we paused on Suspend */
|
---|
94 | for (size_t i = 0; i < mSessionControls.size(); ++i)
|
---|
95 | {
|
---|
96 | /* note that Resume() will simply return a failure if the VM is
|
---|
97 | * in an inappropriate state (it will also fail if the VM has
|
---|
98 | * been somehow closed by this time already so that the
|
---|
99 | * console reference we have is dead) */
|
---|
100 | rc = mSessionControls[i]->ResumeWithReason(Reason_HostResume);
|
---|
101 | if (FAILED(rc))
|
---|
102 | continue;
|
---|
103 |
|
---|
104 | ++resumed;
|
---|
105 | }
|
---|
106 |
|
---|
107 | LogRel(("Host resumed: Resumed %d VMs\n", resumed));
|
---|
108 |
|
---|
109 | #ifdef VBOX_WITH_RESOURCE_USAGE_API
|
---|
110 | /* Resume the performance sampling. */
|
---|
111 | PerformanceCollector *perfcollector = mVirtualBox->i_performanceCollector();
|
---|
112 |
|
---|
113 | if (perfcollector)
|
---|
114 | perfcollector->resumeSampling();
|
---|
115 | #endif
|
---|
116 |
|
---|
117 | mSessionControls.clear();
|
---|
118 | break;
|
---|
119 | }
|
---|
120 |
|
---|
121 | case Reason_HostBatteryLow:
|
---|
122 | {
|
---|
123 | LogFunc(("BATTERY LOW\n"));
|
---|
124 |
|
---|
125 | Bstr value;
|
---|
126 | rc = mVirtualBox->GetExtraData(Bstr("VBoxInternal2/SavestateOnBatteryLow").raw(),
|
---|
127 | value.asOutParam());
|
---|
128 | int fGlobal = 0;
|
---|
129 | if (SUCCEEDED(rc) && !value.isEmpty())
|
---|
130 | {
|
---|
131 | if (value != "0")
|
---|
132 | fGlobal = 1;
|
---|
133 | else if (value == "0")
|
---|
134 | fGlobal = -1;
|
---|
135 | }
|
---|
136 |
|
---|
137 | mVirtualBox->i_getOpenedMachines(machines, &controls);
|
---|
138 | size_t saved = 0;
|
---|
139 |
|
---|
140 | /* save running VMs */
|
---|
141 | for (SessionMachinesList::const_iterator it = machines.begin();
|
---|
142 | it != machines.end();
|
---|
143 | ++it)
|
---|
144 | {
|
---|
145 | ComPtr<SessionMachine> pMachine = *it;
|
---|
146 | rc = pMachine->GetExtraData(Bstr("VBoxInternal2/SavestateOnBatteryLow").raw(),
|
---|
147 | value.asOutParam());
|
---|
148 | int fPerVM = 0;
|
---|
149 | if (SUCCEEDED(rc) && !value.isEmpty())
|
---|
150 | {
|
---|
151 | /* per-VM overrides global */
|
---|
152 | if (value != "0")
|
---|
153 | fPerVM = 2;
|
---|
154 | else if (value == "0")
|
---|
155 | fPerVM = -2;
|
---|
156 | }
|
---|
157 |
|
---|
158 | /* default is true */
|
---|
159 | if (fGlobal + fPerVM >= 0)
|
---|
160 | {
|
---|
161 | ComPtr<IProgress> progress;
|
---|
162 |
|
---|
163 | /* SessionMachine::i_saveStateWithReason() will return
|
---|
164 | * a failure if the VM is in an inappropriate state */
|
---|
165 | rc = pMachine->i_saveStateWithReason(Reason_HostBatteryLow, progress);
|
---|
166 | if (FAILED(rc))
|
---|
167 | {
|
---|
168 | LogRel(("SaveState '%s' failed with %Rhrc\n", pMachine->i_getName().c_str(), rc));
|
---|
169 | continue;
|
---|
170 | }
|
---|
171 |
|
---|
172 | /* Wait until the operation has been completed. */
|
---|
173 | rc = progress->WaitForCompletion(-1);
|
---|
174 | if (SUCCEEDED(rc))
|
---|
175 | {
|
---|
176 | LONG iRc;
|
---|
177 | progress->COMGETTER(ResultCode)(&iRc);
|
---|
178 | rc = iRc;
|
---|
179 | }
|
---|
180 |
|
---|
181 | AssertMsg(SUCCEEDED(rc), ("SaveState WaitForCompletion failed with %Rhrc (%#08X)\n", rc, rc));
|
---|
182 |
|
---|
183 | if (SUCCEEDED(rc))
|
---|
184 | {
|
---|
185 | LogRel(("SaveState '%s' succeeded\n", pMachine->i_getName().c_str()));
|
---|
186 | ++saved;
|
---|
187 | }
|
---|
188 | }
|
---|
189 | }
|
---|
190 | LogRel(("Battery Low: saved %d VMs\n", saved));
|
---|
191 | break;
|
---|
192 | }
|
---|
193 |
|
---|
194 | default:
|
---|
195 | /* nothing */;
|
---|
196 | }
|
---|
197 | }
|
---|
198 | /* vi: set tabstop=4 shiftwidth=4 expandtab: */
|
---|