Mike Frysinger | 57538c0 | 2016-10-06 23:01:33 -0400 | [diff] [blame] | 1 | // 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 Geffon | 8a87b40 | 2019-10-10 14:55:51 -0700 | [diff] [blame^] | 9 | #include <base/files/file_path.h> |
| 10 | #include <base/files/file_util.h> |
Mike Frysinger | 57538c0 | 2016-10-06 23:01:33 -0400 | [diff] [blame] | 11 | #include <base/strings/stringprintf.h> |
| 12 | |
Brian Geffon | 8a87b40 | 2019-10-10 14:55:51 -0700 | [diff] [blame^] | 13 | #include "debugd/src/error_utils.h" |
Mike Frysinger | 57538c0 | 2016-10-06 23:01:33 -0400 | [diff] [blame] | 14 | #include "debugd/src/process_with_output.h" |
| 15 | |
| 16 | namespace debugd { |
| 17 | |
| 18 | namespace { |
| 19 | |
| 20 | // This script holds the bulk of the real logic. |
| 21 | const char kSwapHelperScript[] = "/usr/share/cros/init/swap.sh"; |
Brian Geffon | 8a87b40 | 2019-10-10 14:55:51 -0700 | [diff] [blame^] | 22 | // The path of the kstaled ratio file. |
| 23 | const char kKstaledRatioPath[] = "/sys/kernel/mm/kstaled/ratio"; |
| 24 | const char kSwapToolErrorString[] = "org.chromium.debugd.error.Swap"; |
Mike Frysinger | 57538c0 | 2016-10-06 23:01:33 -0400 | [diff] [blame] | 25 | |
| 26 | std::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 Semenzato | 3421c41 | 2017-10-24 14:56:26 -0700 | [diff] [blame] | 36 | std::string SwapTool::SwapEnable(int32_t size, bool change_now) const { |
Mike Frysinger | 57538c0 | 2016-10-06 23:01:33 -0400 | [diff] [blame] | 37 | int result; |
| 38 | std::string output, buf; |
| 39 | |
Luigi Semenzato | 3421c41 | 2017-10-24 14:56:26 -0700 | [diff] [blame] | 40 | buf = base::StringPrintf("%d", size); |
Mike Frysinger | 57538c0 | 2016-10-06 23:01:33 -0400 | [diff] [blame] | 41 | output = RunSwapHelper({"enable", buf}, &result); |
| 42 | if (result != EXIT_SUCCESS) |
| 43 | return output; |
| 44 | |
| 45 | if (change_now) |
Eric Caruso | c93a15c | 2017-04-24 16:15:12 -0700 | [diff] [blame] | 46 | output = SwapStartStop(true); |
Mike Frysinger | 57538c0 | 2016-10-06 23:01:33 -0400 | [diff] [blame] | 47 | |
| 48 | return output; |
| 49 | } |
| 50 | |
Eric Caruso | c93a15c | 2017-04-24 16:15:12 -0700 | [diff] [blame] | 51 | std::string SwapTool::SwapDisable(bool change_now) const { |
Mike Frysinger | 57538c0 | 2016-10-06 23:01:33 -0400 | [diff] [blame] | 52 | int result; |
| 53 | std::string output; |
| 54 | |
| 55 | output = RunSwapHelper({"disable", }, &result); |
| 56 | if (result != EXIT_SUCCESS) |
| 57 | return output; |
| 58 | |
| 59 | if (change_now) |
Eric Caruso | c93a15c | 2017-04-24 16:15:12 -0700 | [diff] [blame] | 60 | output = SwapStartStop(false); |
Mike Frysinger | 57538c0 | 2016-10-06 23:01:33 -0400 | [diff] [blame] | 61 | |
| 62 | return output; |
| 63 | } |
| 64 | |
Eric Caruso | c93a15c | 2017-04-24 16:15:12 -0700 | [diff] [blame] | 65 | std::string SwapTool::SwapStartStop(bool on) const { |
Mike Frysinger | 57538c0 | 2016-10-06 23:01:33 -0400 | [diff] [blame] | 66 | 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. |
| 71 | output = RunSwapHelper({"stop", }, &result); |
| 72 | if (result != EXIT_SUCCESS) |
| 73 | return output; |
| 74 | |
| 75 | if (on) |
| 76 | output = RunSwapHelper({"start", }, &result); |
| 77 | |
| 78 | return output; |
| 79 | } |
| 80 | |
Eric Caruso | c93a15c | 2017-04-24 16:15:12 -0700 | [diff] [blame] | 81 | std::string SwapTool::SwapStatus() const { |
Mike Frysinger | 57538c0 | 2016-10-06 23:01:33 -0400 | [diff] [blame] | 82 | int result; |
| 83 | return RunSwapHelper({"status", }, &result); |
| 84 | } |
| 85 | |
Luigi Semenzato | 1caa11f | 2017-04-25 12:49:46 -0700 | [diff] [blame] | 86 | std::string SwapTool::SwapSetParameter(const std::string& parameter_name, |
Luigi Semenzato | 3421c41 | 2017-10-24 14:56:26 -0700 | [diff] [blame] | 87 | int32_t parameter_value) const { |
Luigi Semenzato | e8b0cda | 2017-03-20 10:37:53 -0700 | [diff] [blame] | 88 | int result; |
Luigi Semenzato | 1caa11f | 2017-04-25 12:49:46 -0700 | [diff] [blame] | 89 | std::string buf; |
Luigi Semenzato | e8b0cda | 2017-03-20 10:37:53 -0700 | [diff] [blame] | 90 | |
Luigi Semenzato | 3421c41 | 2017-10-24 14:56:26 -0700 | [diff] [blame] | 91 | buf = base::StringPrintf("%d", parameter_value); |
Luigi Semenzato | 1caa11f | 2017-04-25 12:49:46 -0700 | [diff] [blame] | 92 | return RunSwapHelper({"set_parameter", parameter_name, buf}, &result); |
Luigi Semenzato | e8b0cda | 2017-03-20 10:37:53 -0700 | [diff] [blame] | 93 | } |
| 94 | |
Brian Geffon | 8a87b40 | 2019-10-10 14:55:51 -0700 | [diff] [blame^] | 95 | bool SwapTool::KstaledSetRatio(brillo::ErrorPtr* error, |
| 96 | uint8_t kstaled_ratio) const { |
| 97 | std::string buf = std::to_string(kstaled_ratio); |
| 98 | |
| 99 | errno = 0; |
| 100 | size_t res = base::WriteFile(base::FilePath(kKstaledRatioPath), |
| 101 | buf.c_str(), buf.size()); |
| 102 | if (res != buf.size()) { |
| 103 | DEBUGD_ADD_ERROR(error, kSwapToolErrorString, strerror(errno)); |
| 104 | return false; |
| 105 | } |
| 106 | |
| 107 | return true; |
| 108 | } |
| 109 | |
Mike Frysinger | 57538c0 | 2016-10-06 23:01:33 -0400 | [diff] [blame] | 110 | } // namespace debugd |