blob: 6be7fd3e067b4e00c0e59e66c2eb0fe88db437bf [file] [log] [blame]
Mike Frysinger57538c02016-10-06 23:01:33 -04001// Copyright 2016 The Chromium OS Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "debugd/src/swap_tool.h"
6
7#include <string>
8
Brian Geffon8a87b402019-10-10 14:55:51 -07009#include <base/files/file_path.h>
10#include <base/files/file_util.h>
Mike Frysinger57538c02016-10-06 23:01:33 -040011#include <base/strings/stringprintf.h>
12
Brian Geffon8a87b402019-10-10 14:55:51 -070013#include "debugd/src/error_utils.h"
Mike Frysinger57538c02016-10-06 23:01:33 -040014#include "debugd/src/process_with_output.h"
15
16namespace debugd {
17
18namespace {
19
20// This script holds the bulk of the real logic.
21const char kSwapHelperScript[] = "/usr/share/cros/init/swap.sh";
Brian Geffon8a87b402019-10-10 14:55:51 -070022// The path of the kstaled ratio file.
23const char kKstaledRatioPath[] = "/sys/kernel/mm/kstaled/ratio";
24const char kSwapToolErrorString[] = "org.chromium.debugd.error.Swap";
Mike Frysinger57538c02016-10-06 23:01:33 -040025
26std::string RunSwapHelper(const ProcessWithOutput::ArgList& arguments,
27 int* result) {
28 std::string stdout, stderr;
29 *result = ProcessWithOutput::RunProcessFromHelper(
30 kSwapHelperScript, arguments, nullptr, &stdout, &stderr);
31 return *result ? stderr : stdout;
32}
33
34} // namespace
35
Luigi Semenzato3421c412017-10-24 14:56:26 -070036std::string SwapTool::SwapEnable(int32_t size, bool change_now) const {
Mike Frysinger57538c02016-10-06 23:01:33 -040037 int result;
38 std::string output, buf;
39
Luigi Semenzato3421c412017-10-24 14:56:26 -070040 buf = base::StringPrintf("%d", size);
Mike Frysinger57538c02016-10-06 23:01:33 -040041 output = RunSwapHelper({"enable", buf}, &result);
42 if (result != EXIT_SUCCESS)
43 return output;
44
45 if (change_now)
Eric Carusoc93a15c2017-04-24 16:15:12 -070046 output = SwapStartStop(true);
Mike Frysinger57538c02016-10-06 23:01:33 -040047
48 return output;
49}
50
Eric Carusoc93a15c2017-04-24 16:15:12 -070051std::string SwapTool::SwapDisable(bool change_now) const {
Mike Frysinger57538c02016-10-06 23:01:33 -040052 int result;
53 std::string output;
54
Tom Hughesd6c2d392020-08-24 18:12:11 -070055 output = RunSwapHelper({"disable"}, &result);
Mike Frysinger57538c02016-10-06 23:01:33 -040056 if (result != EXIT_SUCCESS)
57 return output;
58
59 if (change_now)
Eric Carusoc93a15c2017-04-24 16:15:12 -070060 output = SwapStartStop(false);
Mike Frysinger57538c02016-10-06 23:01:33 -040061
62 return output;
63}
64
Eric Carusoc93a15c2017-04-24 16:15:12 -070065std::string SwapTool::SwapStartStop(bool on) const {
Mike Frysinger57538c02016-10-06 23:01:33 -040066 int result;
67 std::string output;
68
69 // We always turn off swap because the config might have changed.
70 // Also because the code doesn't like to turn on twice.
Tom Hughesd6c2d392020-08-24 18:12:11 -070071 output = RunSwapHelper({"stop"}, &result);
Mike Frysinger57538c02016-10-06 23:01:33 -040072 if (result != EXIT_SUCCESS)
73 return output;
74
75 if (on)
Tom Hughesd6c2d392020-08-24 18:12:11 -070076 output = RunSwapHelper({"start"}, &result);
Mike Frysinger57538c02016-10-06 23:01:33 -040077
78 return output;
79}
80
Eric Carusoc93a15c2017-04-24 16:15:12 -070081std::string SwapTool::SwapStatus() const {
Mike Frysinger57538c02016-10-06 23:01:33 -040082 int result;
Tom Hughesd6c2d392020-08-24 18:12:11 -070083 return RunSwapHelper({"status"}, &result);
Mike Frysinger57538c02016-10-06 23:01:33 -040084}
85
Luigi Semenzato1caa11f2017-04-25 12:49:46 -070086std::string SwapTool::SwapSetParameter(const std::string& parameter_name,
Luigi Semenzato3421c412017-10-24 14:56:26 -070087 int32_t parameter_value) const {
Luigi Semenzatoe8b0cda2017-03-20 10:37:53 -070088 int result;
Luigi Semenzato1caa11f2017-04-25 12:49:46 -070089 std::string buf;
Luigi Semenzatoe8b0cda2017-03-20 10:37:53 -070090
Luigi Semenzato3421c412017-10-24 14:56:26 -070091 buf = base::StringPrintf("%d", parameter_value);
Luigi Semenzato1caa11f2017-04-25 12:49:46 -070092 return RunSwapHelper({"set_parameter", parameter_name, buf}, &result);
Luigi Semenzatoe8b0cda2017-03-20 10:37:53 -070093}
94
Brian Geffon8a87b402019-10-10 14:55:51 -070095bool SwapTool::KstaledSetRatio(brillo::ErrorPtr* error,
96 uint8_t kstaled_ratio) const {
97 std::string buf = std::to_string(kstaled_ratio);
98
99 errno = 0;
Tom Hughesd6c2d392020-08-24 18:12:11 -0700100 size_t res = base::WriteFile(base::FilePath(kKstaledRatioPath), buf.c_str(),
101 buf.size());
Brian Geffon8a87b402019-10-10 14:55:51 -0700102 if (res != buf.size()) {
103 DEBUGD_ADD_ERROR(error, kSwapToolErrorString, strerror(errno));
104 return false;
105 }
106
107 return true;
108}
109
Mike Frysinger57538c02016-10-06 23:01:33 -0400110} // namespace debugd