File: arc_vmm_manager.cc

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,811; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (413 lines) | stat: -rw-r--r-- 15,072 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/ash/arc/vmm/arc_vmm_manager.h"

#include <optional>

#include "ash/accelerators/accelerator_controller_impl.h"
#include "ash/public/cpp/accelerators.h"
#include "ash/shell.h"
#include "base/feature_list.h"
#include "base/functional/bind.h"
#include "base/functional/callback_forward.h"
#include "base/functional/callback_helpers.h"
#include "base/location.h"
#include "base/memory/raw_ptr.h"
#include "base/task/thread_pool.h"
#include "base/time/time.h"
#include "chrome/browser/ash/arc/vmm/arc_vmm_swap_scheduler.h"
#include "chrome/browser/ash/arc/vmm/arcvm_working_set_trim_executor.h"
#include "chromeos/ash/components/dbus/concierge/concierge_client.h"
#include "chromeos/ash/experiences/arc/arc_browser_context_keyed_service_factory_base.h"
#include "chromeos/ash/experiences/arc/arc_features.h"
#include "chromeos/ash/experiences/arc/arc_util.h"
#include "chromeos/ash/experiences/arc/session/arc_bridge_service.h"
#include "chromeos/ash/experiences/arc/session/arc_session.h"
#include "ui/base/accelerators/accelerator.h"

namespace arc {

namespace {
class ArcVmmManagerFactory
    : public internal::ArcBrowserContextKeyedServiceFactoryBase<
          ArcVmmManager,
          ArcVmmManagerFactory> {
 public:
  static constexpr const char* kName = "ArcVmmManagerFactory";
  static ArcVmmManagerFactory* GetInstance() {
    static base::NoDestructor<ArcVmmManagerFactory> instance;
    return instance.get();
  }

 private:
  friend class base::NoDestructor<ArcVmmManagerFactory>;

  ArcVmmManagerFactory() = default;
  ~ArcVmmManagerFactory() override = default;
};

}  // namespace

// static
ArcVmmManager* ArcVmmManager::GetForBrowserContext(
    content::BrowserContext* context) {
  return ArcVmmManagerFactory::GetForBrowserContext(context);
}

// static
void ArcVmmManager::EnsureFactoryBuilt() {
  ArcVmmManagerFactory::GetInstance();
}

// static
ArcVmmManager* ArcVmmManager::GetForBrowserContextForTesting(
    content::BrowserContext* context) {
  return ArcVmmManagerFactory::GetForBrowserContextForTesting(context);
}

ArcVmmManager::ArcVmmManager(content::BrowserContext* context,
                             ArcBridgeService* bridge)
    : context_(context), bridge_service_(bridge) {
  app_instance_observation_.Observe(bridge_service_->app());

  auto* client = ash::ConciergeClient::Get();
  DCHECK(client);
  if (client) {
    concierge_observation_.Observe(client);
  } else {
    LOG(FATAL) << "ArcVmmManager initialized but failed to register observer "
                  "on Concierge.";
  }

  if (base::FeatureList::IsEnabled(kVmmSwapKeyboardShortcut)) {
    accelerator_ = std::make_unique<AcceleratorTarget>(this);
  }
  if (base::FeatureList::IsEnabled(kVmmSwapPolicy)) {
    swap_out_delay_ = base::Seconds(kVmmSwapOutDelaySecond.Get());
    scheduler_ = std::make_unique<ArcVmmSwapScheduler>(
        base::BindRepeating(
            [](base::WeakPtr<ArcVmmManager> manager, bool enable) {
              if (manager) {
                manager->SetSwapState(enable ? SwapState::ENABLE
                                             : SwapState::DISABLE);
              }
            },
            weak_ptr_factory_.GetWeakPtr()),
        /* minimum_swapout_interval= */
        base::Seconds(kVmmSwapOutTimeIntervalSecond.Get()),
        /* swappable_checking_period= */
        base::Seconds(kVmmSwapArcSilenceIntervalSecond.Get()),
        std::make_unique<ArcSystemStateObservation>(context));
  }
  trim_call_ =
      base::BindRepeating(&ArcVmWorkingSetTrimExecutor::Trim, context_);
}

ArcVmmManager::~ArcVmmManager() = default;

void ArcVmmManager::SetSwapState(SwapState state) {
  if (!IsArcVmEnabled() || !arc_connected_) {
    LOG(ERROR) << "Failed to SetSwapState, ARCVM not enabled or connected.";
    return;
  }
  DVLOG(1) << "SetSwapState " << static_cast<int>(state);
  vm_tools::concierge::SwapOperation op;
  switch (state) {
    case SwapState::ENABLE:
      op = vm_tools::concierge::SwapOperation::ENABLE;
      break;
    case SwapState::FORCE_ENABLE:
      op = vm_tools::concierge::SwapOperation::FORCE_ENABLE;
      break;
    case SwapState::DISABLE:
      op = vm_tools::concierge::SwapOperation::DISABLE;
      break;
  }

  if (state == SwapState::DISABLE) {
    if (latest_swap_state_ == state) {
      return;
    }
    latest_swap_state_ = state;
    // The disable request will be sent immediately so the verify is
    // unnecessarily.
    SendSwapRequest(op, base::DoNothing());
    enabled_state_heartbeat_timer_.Stop();
    return;
  }

  // Do not re-send "enable" signal if the timer is waiting for resend it. But
  // allow "force-enable" bypass this restriction and redo the entire swap
  // process.
  if (latest_swap_state_ == SwapState::ENABLE && latest_swap_state_ == state &&
      enabled_state_heartbeat_timer_.IsRunning()) {
    // The state is not update, do not send request now but leave it to heart
    // beat timer.
    return;
  }

  latest_swap_state_ = state;

  // Reset the timer anyway since the enable state and force enable state may
  // overwrite each other.
  enabled_state_heartbeat_timer_.Start(
      FROM_HERE, kVmmSwapTrimInterval.Get(),
      base::BindRepeating(&ArcVmmManager::SetSwapState,
                          weak_ptr_factory_.GetWeakPtr(), state));

  // Enable or ForceEnable need shrink ARCVM memory first.
  if (!last_shrink_timestamp_ ||
      base::Time::Now() - last_shrink_timestamp_.value() >
          kVmmSwapMinShrinkInterval.Get()) {
    last_shrink_timestamp_ = base::Time::Now();
    last_shrink_result_ = false;
    // Following attempts to enable vmm-swap will be ignored until
    // `ShrinkArcVmMemoryAndEnableSwap()` finish. As a result, it will send an
    // enable swap request as a coalesced request if it succeeds to shrink the
    // ARCVM memory.
    ShrinkArcVmMemoryAndEnableSwap(op);
  } else {
    if (last_shrink_result_.value_or(false)) {
      // If recently the memory shrinking succeed, just send enable request
      // rather than shrink memory again.
      VerifyThenSendSwapRequest(op, base::DoNothing());
    } else {
      // If recently the memory failed to shrink, skip the request.
      VLOG(0) << "Skip enable swap request due to last arcvm memory shrink "
                 "failure";
    }
  }
}

bool ArcVmmManager::IsSwapped() const {
  // Currently ArcVmmManager assume after set vmm swap enabled, the system
  // under the "swapped" state.
  // In the future, is should be replaced by real swap state from the concierge,
  // because only the memory swapped and has been written to the disk can be
  // assumed as "swapped".
  return latest_swap_state_ != SwapState::DISABLE;
}

void ArcVmmManager::AddObserver(Observer* observer) {
  observer_list_.AddObserver(observer);
}
void ArcVmmManager::RemoveObserver(Observer* observer) {
  observer_list_.RemoveObserver(observer);
}

void ArcVmmManager::OnConnectionReady() {
  arc_connected_ = true;
}
void ArcVmmManager::OnConnectionClosed() {
  arc_connected_ = false;
}

void ArcVmmManager::OnVmSwapping(
    const vm_tools::concierge::VmSwappingSignal& signal) {
  if (signal.name() != kArcVmName) {
    return;
  }
  if (signal.state() == vm_tools::concierge::SWAPPING_OUT) {
    VLOG(1) << "ArcVm swapping out.";
    for (auto& observer : observer_list_) {
      observer.OnArcVmSwappingOut();
    }
  } else if (signal.state() == vm_tools::concierge::SWAPPING_IN) {
    VLOG(1) << "ArcVm swapping in.";
    for (auto& observer : observer_list_) {
      observer.OnArcVmSwappingIn();
    }
  }
}

void ArcVmmManager::SendSwapRequest(
    vm_tools::concierge::SwapOperation operation,
    base::OnceClosure success_callback) {
  auto* client = ash::ConciergeClient::Get();
  if (!client) {
    LOG(ERROR) << "Cannot find concierge client to swap ARCVM";
    return;
  }

  VLOG(0) << "SendSwapRequest " << static_cast<int>(operation)
          << " to concierge.";
  vm_tools::concierge::SwapVmRequest request;
  request.set_name(kArcVmName);
  request.set_owner_id(user_id_hash_);
  request.set_operation(operation);
  client->SwapVm(
      request,
      base::BindOnce(
          [](vm_tools::concierge::SwapOperation op, base::OnceClosure cb,
             std::optional<vm_tools::concierge::SuccessFailureResponse>
                 response) {
            if (!response.has_value()) {
              LOG(ERROR) << "Failed to receive SwapVm response.";
            } else if (!response->success()) {
              LOG(ERROR) << "Failed to send request: "
                         << vm_tools::concierge::SwapOperation_Name(op)
                         << ". Reason: " << response->failure_reason();
            } else {
              std::move(cb).Run();
            }
          },
          operation, std::move(success_callback)));
}

void ArcVmmManager::VerifyThenSendSwapRequest(
    vm_tools::concierge::SwapOperation operation,
    base::OnceClosure success_callback) {
  auto request_disable = latest_swap_state_ == SwapState::DISABLE;
  auto going_to_disable =
      operation == vm_tools::concierge::SwapOperation::DISABLE;
  if (request_disable != going_to_disable) {
    LOG(WARNING) << "Vmm swap request conflict in callback chain, ignored. "
                    "latest state: "
                 << static_cast<int>(latest_swap_state_)
                 << ", pending operation: " << static_cast<int>(operation);
    return;
  }
  SendSwapRequest(operation, std::move(success_callback));
}

void ArcVmmManager::SendAggressiveBalloonRequest(
    bool enable,
    base::OnceClosure success_callback) {
  auto* client = ash::ConciergeClient::Get();
  if (!client) {
    LOG(ERROR) << "Cannot find concierge client to swap ARCVM";
    return;
  }

  DVLOG(1) << "SendAggressiveBalloon state change " << enable
           << " request to concierge";
  vm_tools::concierge::AggressiveBalloonRequest request;
  request.set_name(kArcVmName);
  request.set_owner_id(user_id_hash_);
  request.set_enable(enable);
  client->AggressiveBalloon(
      request,
      base::BindOnce(
          [](bool enabled, base::OnceClosure cb,
             std::optional<vm_tools::concierge::SuccessFailureResponse>
                 response) {
            if (!response.has_value()) {
              LOG(ERROR) << "Failed to receive aggressive ballon response.";
            } else if (!response->success()) {
              LOG(ERROR) << "Failed to send aggressive balloon request: "
                         << enabled
                         << ". Reason: " << response->failure_reason();
            } else {
              std::move(cb).Run();
            }
          },
          enable, std::move(success_callback)));
}

void ArcVmmManager::VerifyThenSendAggressiveBalloonRequest(
    bool enable,
    base::OnceClosure success_callback) {
  auto request_disable = latest_swap_state_ == SwapState::DISABLE;
  if (request_disable == enable) {
    LOG(WARNING) << "Vmm swap request conflict in callback chain, ignored. "
                    "latest state: "
                 << static_cast<int>(latest_swap_state_)
                 << ", pending aggressive balloon: " << enable;
    return;
  }
  SendAggressiveBalloonRequest(enable, std::move(success_callback));
}

void ArcVmmManager::PostWithSwapDelay(base::OnceClosure callback) {
  base::SingleThreadTaskRunner::GetCurrentDefault()->PostDelayedTask(
      FROM_HERE, std::move(callback), swap_out_delay_);
}

void ArcVmmManager::ShrinkArcVmMemoryAndEnableSwap(
    vm_tools::concierge::SwapOperation requested_operation) {
  // Trim ARCVM memory before enable vmm swap in order to squeeze the vm
  // memory. Send enable operation if trim success.
  DCHECK(!trim_call_.is_null());
  DVLOG(1) << "ShrinkArcVmMemoryAndEnableSwap with request "
           << static_cast<int>(requested_operation);
  trim_call_.Run(
      base::BindOnce(
          [](base::OnceClosure success_closure, bool success,
             const std::string& failure_reason) {
            if (success) {
              std::move(success_closure).Run();
            } else {
              LOG(ERROR) << "Failed to trim ARCVM memory when enable vmm "
                            "swap, reason: "
                         << failure_reason;
            }
          },
          // If successfully execute trim, request enable aggressive balloon.
          base::BindOnce(
              &ArcVmmManager::VerifyThenSendAggressiveBalloonRequest,
              weak_ptr_factory_.GetWeakPtr(), true,
              // If enable aggressive balloon successful, set shrink
              // result and re-send enable swap request.
              base::BindOnce(&ArcVmmManager::SetShrinkResult,
                             weak_ptr_factory_.GetWeakPtr(), true)
                  .Then(base::BindOnce(
                      &ArcVmmManager::VerifyThenSendSwapRequest,
                      weak_ptr_factory_.GetWeakPtr(), requested_operation,
                      // Drop ARCVM page cache after successful enable swap.
                      base::BindOnce(
                          trim_call_, base::DoNothing(),
                          arc::ArcVmReclaimType::kReclaimGuestPageCaches,
                          arc::ArcSession::kNoPageLimit))))),
      arc::ArcVmReclaimType::kReclaimAllGuestOnly,
      arc::ArcSession::kNoPageLimit);
}

void ArcVmmManager::SetShrinkResult(bool success) {
  last_shrink_result_ = success;
}

// ArcVmmManager::AcceleratorTarget --------------------------------------------

class ArcVmmManager::AcceleratorTarget : public ui::AcceleratorTarget {
 public:
  explicit AcceleratorTarget(ArcVmmManager* manager)
      : manager_(manager),
        vmm_swap_enabled_(ui::VKEY_O, ash::kDebugModifier),
        vmm_swap_disabled_(ui::VKEY_P, ash::kDebugModifier) {
    ash::Shell::Get()->accelerator_controller()->Register(
        {vmm_swap_enabled_, vmm_swap_disabled_}, this);
  }
  AcceleratorTarget(const AcceleratorTarget&) = delete;
  AcceleratorTarget& operator=(const AcceleratorTarget&) = delete;
  ~AcceleratorTarget() override = default;

 private:
  // ui::AcceleratorTarget:
  bool AcceleratorPressed(const ui::Accelerator& accelerator) override {
    if (accelerator == vmm_swap_enabled_) {
      DVLOG(1) << "Set force enable vmm swap state by keyboard shortcut.";
      manager_->SetSwapState(SwapState::FORCE_ENABLE);
    } else if (accelerator == vmm_swap_disabled_) {
      DVLOG(1) << "Set diable vmm swap state by keyboard shortcut.";
      manager_->SetSwapState(SwapState::DISABLE);
    } else {
      NOTREACHED();
    }
    return true;
  }

  bool CanHandleAccelerators() const override { return true; }

  // The manager responsible for executing vmm commands.
  const raw_ptr<ArcVmmManager> manager_;

  // The accelerator to enable vmm swap for ARCVM.
  const ui::Accelerator vmm_swap_enabled_;

  // The accelerator to disable vmm swap for ARCVM.
  const ui::Accelerator vmm_swap_disabled_;
};

}  // namespace arc