VirtualBox

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

Last change on this file since 98103 was 98103, checked in by vboxsync, 16 months ago

Copyright year updates by scm.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 6.9 KB
RevLine 
[55401]1/* $Id: HostPower.cpp 98103 2023-01-17 14:15:46Z vboxsync $ */
[15454]2/** @file
3 * VirtualBox interface to host's power notification service
4 */
5
6/*
[98103]7 * Copyright (C) 2006-2023 Oracle and/or its affiliates.
[15454]8 *
[96407]9 * This file is part of VirtualBox base platform packages, as
10 * available from https://www.virtualbox.org.
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation, in version 3 of the
15 * License.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, see <https://www.gnu.org/licenses>.
24 *
25 * SPDX-License-Identifier: GPL-3.0-only
[15454]26 */
27
28
[57358]29/*********************************************************************************************************************************
30* Header Files *
31*********************************************************************************************************************************/
[76592]32#define LOG_GROUP LOG_GROUP_MAIN_HOST
[15454]33#include "HostPower.h"
[76592]34#include "LoggingNew.h"
[15454]35
36#include <VBox/com/ptr.h>
37
[23223]38#include "VirtualBoxImpl.h"
[46775]39#include "MachineImpl.h"
[23223]40
[15454]41#include <iprt/mem.h>
[50174]42#include <iprt/cpp/utils.h>
[15454]43
[46775]44HostPowerService::HostPowerService(VirtualBox *aVirtualBox)
[15454]45{
[50174]46 AssertPtr(aVirtualBox);
[15454]47 mVirtualBox = aVirtualBox;
48}
49
50HostPowerService::~HostPowerService()
51{
52}
53
[46775]54void HostPowerService::notify(Reason_T aReason)
[15454]55{
[27796]56 SessionMachinesList machines;
[23327]57 VirtualBox::InternalControlList controls;
[15454]58
59 HRESULT rc = S_OK;
60
[46775]61 switch (aReason)
[15454]62 {
[46775]63 case Reason_HostSuspend:
[15454]64 {
[50220]65 LogFunc(("HOST SUSPEND\n"));
[15454]66
[21395]67#ifdef VBOX_WITH_RESOURCE_USAGE_API
68 /* Suspend performance sampling to avoid unnecessary callbacks due to jumps in time. */
[50355]69 PerformanceCollector *perfcollector = mVirtualBox->i_performanceCollector();
[21395]70
71 if (perfcollector)
72 perfcollector->suspendSampling();
73#endif
[50355]74 mVirtualBox->i_getOpenedMachines(machines, &controls);
[15454]75
76 /* pause running VMs */
[23327]77 for (VirtualBox::InternalControlList::const_iterator it = controls.begin();
78 it != controls.end();
79 ++it)
[15454]80 {
[23327]81 ComPtr<IInternalSessionControl> pControl = *it;
82
[46775]83 /* PauseWithReason() will simply return a failure if
84 * the VM is in an inappropriate state */
85 rc = pControl->PauseWithReason(Reason_HostSuspend);
[26186]86 if (FAILED(rc))
[15454]87 continue;
88
89 /* save the control to un-pause the VM later */
[46775]90 mSessionControls.push_back(pControl);
[15454]91 }
92
[50220]93 LogRel(("Host suspending: Paused %d VMs\n", mSessionControls.size()));
[15454]94 break;
95 }
96
[46775]97 case Reason_HostResume:
[15454]98 {
[50220]99 LogFunc(("HOST RESUME\n"));
[15454]100
101 size_t resumed = 0;
102
103 /* go through VMs we paused on Suspend */
[46775]104 for (size_t i = 0; i < mSessionControls.size(); ++i)
[15454]105 {
106 /* note that Resume() will simply return a failure if the VM is
107 * in an inappropriate state (it will also fail if the VM has
108 * been somehow closed by this time already so that the
109 * console reference we have is dead) */
[46775]110 rc = mSessionControls[i]->ResumeWithReason(Reason_HostResume);
[26186]111 if (FAILED(rc))
[15454]112 continue;
113
[46775]114 ++resumed;
[15454]115 }
116
[50220]117 LogRel(("Host resumed: Resumed %d VMs\n", resumed));
[15454]118
[21395]119#ifdef VBOX_WITH_RESOURCE_USAGE_API
120 /* Resume the performance sampling. */
[50355]121 PerformanceCollector *perfcollector = mVirtualBox->i_performanceCollector();
[21395]122
123 if (perfcollector)
124 perfcollector->resumeSampling();
125#endif
126
[46775]127 mSessionControls.clear();
[15454]128 break;
129 }
130
[46775]131 case Reason_HostBatteryLow:
[15454]132 {
[50220]133 LogFunc(("BATTERY LOW\n"));
134
[50216]135 Bstr value;
[50220]136 rc = mVirtualBox->GetExtraData(Bstr("VBoxInternal2/SavestateOnBatteryLow").raw(),
137 value.asOutParam());
138 int fGlobal = 0;
[50221]139 if (SUCCEEDED(rc) && !value.isEmpty())
[50175]140 {
[50220]141 if (value != "0")
142 fGlobal = 1;
143 else if (value == "0")
144 fGlobal = -1;
145 }
[15454]146
[50355]147 mVirtualBox->i_getOpenedMachines(machines, &controls);
[50220]148 size_t saved = 0;
[15454]149
[50220]150 /* save running VMs */
[55214]151 for (SessionMachinesList::const_iterator it = machines.begin();
152 it != machines.end();
153 ++it)
[50220]154 {
[55214]155 ComPtr<SessionMachine> pMachine = *it;
[50220]156 rc = pMachine->GetExtraData(Bstr("VBoxInternal2/SavestateOnBatteryLow").raw(),
157 value.asOutParam());
158 int fPerVM = 0;
[50221]159 if (SUCCEEDED(rc) && !value.isEmpty())
[50175]160 {
[50220]161 /* per-VM overrides global */
162 if (value != "0")
163 fPerVM = 2;
164 else if (value == "0")
165 fPerVM = -2;
166 }
167
168 /* default is true */
169 if (fGlobal + fPerVM >= 0)
170 {
[50175]171 ComPtr<IProgress> progress;
[15454]172
[55214]173 /* SessionMachine::i_saveStateWithReason() will return
174 * a failure if the VM is in an inappropriate state */
175 rc = pMachine->i_saveStateWithReason(Reason_HostBatteryLow, progress);
[50175]176 if (FAILED(rc))
[50220]177 {
[51498]178 LogRel(("SaveState '%s' failed with %Rhrc\n", pMachine->i_getName().c_str(), rc));
[50175]179 continue;
[50220]180 }
[15454]181
[50175]182 /* Wait until the operation has been completed. */
183 rc = progress->WaitForCompletion(-1);
184 if (SUCCEEDED(rc))
185 {
186 LONG iRc;
187 progress->COMGETTER(ResultCode)(&iRc);
[85280]188 rc = (HRESULT)iRc;
[50175]189 }
[15454]190
[50175]191 AssertMsg(SUCCEEDED(rc), ("SaveState WaitForCompletion failed with %Rhrc (%#08X)\n", rc, rc));
[15454]192
[50175]193 if (SUCCEEDED(rc))
[50220]194 {
[51498]195 LogRel(("SaveState '%s' succeeded\n", pMachine->i_getName().c_str()));
[50175]196 ++saved;
[50220]197 }
[36433]198 }
[15454]199 }
[50227]200 LogRel(("Battery Low: saved %d VMs\n", saved));
[15454]201 break;
202 }
[46775]203
204 default:
205 /* nothing */;
[15454]206 }
207}
[14772]208/* vi: set tabstop=4 shiftwidth=4 expandtab: */
Note: See TracBrowser for help on using the repository browser.

© 2023 Oracle
ContactPrivacy policyTerms of Use