1 | /** @file
|
---|
2 | *
|
---|
3 | * VirtualBox interface to host's power notification service
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2007 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.215389.xyz. 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 |
|
---|
30 | #include <iprt/mem.h>
|
---|
31 |
|
---|
32 | #ifdef VBOX_WITH_RESOURCE_USAGE_API
|
---|
33 | #include "Performance.h"
|
---|
34 | #include "PerformanceImpl.h"
|
---|
35 | #endif /* VBOX_WITH_RESOURCE_USAGE_API */
|
---|
36 |
|
---|
37 | HostPowerService::HostPowerService (VirtualBox *aVirtualBox)
|
---|
38 | {
|
---|
39 | Assert(aVirtualBox != NULL);
|
---|
40 | mVirtualBox = aVirtualBox;
|
---|
41 | }
|
---|
42 |
|
---|
43 | HostPowerService::~HostPowerService()
|
---|
44 | {
|
---|
45 | }
|
---|
46 |
|
---|
47 | void HostPowerService::notify(HostPowerEvent aEvent)
|
---|
48 | {
|
---|
49 | SessionMachinesList machines;
|
---|
50 | VirtualBox::InternalControlList controls;
|
---|
51 |
|
---|
52 | HRESULT rc = S_OK;
|
---|
53 |
|
---|
54 | switch (aEvent)
|
---|
55 | {
|
---|
56 | case HostPowerEvent_Suspend:
|
---|
57 | {
|
---|
58 | LogFunc (("SUSPEND\n"));
|
---|
59 |
|
---|
60 | #ifdef VBOX_WITH_RESOURCE_USAGE_API
|
---|
61 | /* Suspend performance sampling to avoid unnecessary callbacks due to jumps in time. */
|
---|
62 | PerformanceCollector *perfcollector = mVirtualBox->performanceCollector();
|
---|
63 |
|
---|
64 | if (perfcollector)
|
---|
65 | perfcollector->suspendSampling();
|
---|
66 | #endif
|
---|
67 | mVirtualBox->getOpenedMachines(machines, &controls);
|
---|
68 |
|
---|
69 | /* pause running VMs */
|
---|
70 | for (VirtualBox::InternalControlList::const_iterator it = controls.begin();
|
---|
71 | it != controls.end();
|
---|
72 | ++it)
|
---|
73 | {
|
---|
74 | ComPtr<IInternalSessionControl> pControl = *it;
|
---|
75 |
|
---|
76 | /* get the remote console */
|
---|
77 | ComPtr<IConsole> console;
|
---|
78 | rc = pControl->GetRemoteConsole(console.asOutParam());
|
---|
79 | /* the VM could have been powered down and closed or whatever */
|
---|
80 | if (FAILED(rc))
|
---|
81 | continue;
|
---|
82 |
|
---|
83 | /* note that Pause() will simply return a failure if the VM is
|
---|
84 | * in an inappropriate state */
|
---|
85 | rc = console->Pause();
|
---|
86 | if (FAILED(rc))
|
---|
87 | continue;
|
---|
88 |
|
---|
89 | /* save the control to un-pause the VM later */
|
---|
90 | mConsoles.push_back(console);
|
---|
91 | }
|
---|
92 |
|
---|
93 | LogFunc (("Suspended %d VMs\n", mConsoles.size()));
|
---|
94 |
|
---|
95 | break;
|
---|
96 | }
|
---|
97 |
|
---|
98 | case HostPowerEvent_Resume:
|
---|
99 | {
|
---|
100 | LogFunc (("RESUME\n"));
|
---|
101 |
|
---|
102 | size_t resumed = 0;
|
---|
103 |
|
---|
104 | /* go through VMs we paused on Suspend */
|
---|
105 | for (size_t i = 0; i < mConsoles.size(); ++i)
|
---|
106 | {
|
---|
107 | /* note that Resume() will simply return a failure if the VM is
|
---|
108 | * in an inappropriate state (it will also fail if the VM has
|
---|
109 | * been somehow closed by this time already so that the
|
---|
110 | * console reference we have is dead) */
|
---|
111 | rc = mConsoles[i]->Resume();
|
---|
112 | if (FAILED(rc))
|
---|
113 | continue;
|
---|
114 |
|
---|
115 | ++ resumed;
|
---|
116 | }
|
---|
117 |
|
---|
118 | LogFunc (("Resumed %d VMs\n", resumed));
|
---|
119 |
|
---|
120 | #ifdef VBOX_WITH_RESOURCE_USAGE_API
|
---|
121 | /* Resume the performance sampling. */
|
---|
122 | PerformanceCollector *perfcollector = mVirtualBox->performanceCollector();
|
---|
123 |
|
---|
124 | if (perfcollector)
|
---|
125 | perfcollector->resumeSampling();
|
---|
126 | #endif
|
---|
127 |
|
---|
128 | mConsoles.clear();
|
---|
129 |
|
---|
130 | break;
|
---|
131 | }
|
---|
132 |
|
---|
133 | case HostPowerEvent_BatteryLow:
|
---|
134 | {
|
---|
135 | LogFunc (("BATTERY LOW\n"));
|
---|
136 |
|
---|
137 | mVirtualBox->getOpenedMachines(machines, &controls);
|
---|
138 |
|
---|
139 | size_t saved = 0;
|
---|
140 |
|
---|
141 | /* save running VMs */
|
---|
142 | for (VirtualBox::InternalControlList::const_iterator it = controls.begin();
|
---|
143 | it != controls.end();
|
---|
144 | ++it)
|
---|
145 | {
|
---|
146 | ComPtr<IInternalSessionControl> pControl = *it;
|
---|
147 | /* get the remote console */
|
---|
148 | ComPtr<IConsole> console;
|
---|
149 | rc = pControl->GetRemoteConsole (console.asOutParam());
|
---|
150 | /* the VM could have been powered down and closed or whatever */
|
---|
151 | if (FAILED(rc))
|
---|
152 | continue;
|
---|
153 |
|
---|
154 | ComPtr<IProgress> progress;
|
---|
155 |
|
---|
156 | /* note that SaveState() will simply return a failure if the VM
|
---|
157 | * is in an inappropriate state */
|
---|
158 | rc = console->SaveState (progress.asOutParam());
|
---|
159 | if (FAILED(rc))
|
---|
160 | continue;
|
---|
161 |
|
---|
162 | /* Wait until the operation has been completed. */
|
---|
163 | LONG iRc;
|
---|
164 | rc = progress->WaitForCompletion(-1);
|
---|
165 | if (SUCCEEDED(rc))
|
---|
166 | progress->COMGETTER(ResultCode) (&iRc);
|
---|
167 | rc = iRc;
|
---|
168 |
|
---|
169 | AssertMsg (SUCCEEDED(rc), ("SaveState WaitForCompletion "
|
---|
170 | "failed with %Rhrc (%#08X)\n", rc, rc));
|
---|
171 |
|
---|
172 | if (SUCCEEDED(rc))
|
---|
173 | ++ saved;
|
---|
174 | }
|
---|
175 |
|
---|
176 | LogFunc (("Saved %d VMs\n", saved));
|
---|
177 |
|
---|
178 | break;
|
---|
179 | }
|
---|
180 | }
|
---|
181 | }
|
---|
182 | /* vi: set tabstop=4 shiftwidth=4 expandtab: */
|
---|