VirtualBox

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

Last change on this file since 49875 was 46775, checked in by vboxsync, 11 years ago

Main/Host(HostPower)+Session+Console: convert HostPower code to signal pause/resume/savestate through internal methods, conveying information why the method was called, preparing for VM/PDM passing this information to devices and drivers

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 5.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
33HostPowerService::HostPowerService(VirtualBox *aVirtualBox)
34{
35 Assert(aVirtualBox != NULL);
36 mVirtualBox = aVirtualBox;
37}
38
39HostPowerService::~HostPowerService()
40{
41}
42
43void HostPowerService::notify(Reason_T aReason)
44{
45 SessionMachinesList machines;
46 VirtualBox::InternalControlList controls;
47
48 HRESULT rc = S_OK;
49
50 switch (aReason)
51 {
52 case Reason_HostSuspend:
53 {
54 LogFunc(("SUSPEND\n"));
55
56#ifdef VBOX_WITH_RESOURCE_USAGE_API
57 /* Suspend performance sampling to avoid unnecessary callbacks due to jumps in time. */
58 PerformanceCollector *perfcollector = mVirtualBox->performanceCollector();
59
60 if (perfcollector)
61 perfcollector->suspendSampling();
62#endif
63 mVirtualBox->getOpenedMachines(machines, &controls);
64
65 /* pause running VMs */
66 for (VirtualBox::InternalControlList::const_iterator it = controls.begin();
67 it != controls.end();
68 ++it)
69 {
70 ComPtr<IInternalSessionControl> pControl = *it;
71
72 /* PauseWithReason() will simply return a failure if
73 * the VM is in an inappropriate state */
74 rc = pControl->PauseWithReason(Reason_HostSuspend);
75 if (FAILED(rc))
76 continue;
77
78 /* save the control to un-pause the VM later */
79 mSessionControls.push_back(pControl);
80 }
81
82 LogFunc(("Suspended %d VMs\n", mSessionControls.size()));
83
84 break;
85 }
86
87 case Reason_HostResume:
88 {
89 LogFunc(("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 LogFunc(("Resumed %d VMs\n", resumed));
108
109#ifdef VBOX_WITH_RESOURCE_USAGE_API
110 /* Resume the performance sampling. */
111 PerformanceCollector *perfcollector = mVirtualBox->performanceCollector();
112
113 if (perfcollector)
114 perfcollector->resumeSampling();
115#endif
116
117 mSessionControls.clear();
118
119 break;
120 }
121
122 case Reason_HostBatteryLow:
123 {
124 LogFunc(("BATTERY LOW\n"));
125
126 mVirtualBox->getOpenedMachines(machines, &controls);
127
128 size_t saved = 0;
129
130 /* save running VMs */
131 for (VirtualBox::InternalControlList::const_iterator it = controls.begin();
132 it != controls.end();
133 ++it)
134 {
135 ComPtr<IInternalSessionControl> pControl = *it;
136
137 ComPtr<IProgress> progress;
138
139 /* note that SaveStateWithReason() will simply return a failure
140 * if the VM is in an inappropriate state */
141 rc = pControl->SaveStateWithReason(Reason_HostBatteryLow, progress.asOutParam());
142 if (FAILED(rc))
143 continue;
144
145 /* Wait until the operation has been completed. */
146 rc = progress->WaitForCompletion(-1);
147 if (SUCCEEDED(rc))
148 {
149 LONG iRc;
150 progress->COMGETTER(ResultCode)(&iRc);
151 rc = iRc;
152 }
153
154 AssertMsg(SUCCEEDED(rc), ("SaveState WaitForCompletion failed with %Rhrc (%#08X)\n", rc, rc));
155
156 if (SUCCEEDED(rc))
157 ++saved;
158 }
159
160 LogFunc(("Saved %d VMs\n", saved));
161
162 break;
163 }
164
165 default:
166 /* nothing */;
167 }
168}
169/* 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