Honglin Yu | 1cd2507 | 2019-07-09 11:54:14 +1000 | [diff] [blame] | 1 | // Copyright 2019 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 | #ifndef ML_UTIL_H_ |
| 6 | #define ML_UTIL_H_ |
| 7 | |
| 8 | #include <base/files/file_path.h> |
| 9 | |
| 10 | namespace ml { |
| 11 | |
| 12 | // The memory usage (typically of a process). |
| 13 | // One can extend this struct to include more terms. Currently, it only |
| 14 | // includes `VmSwap` and `VmRSS` to fulfill the needs. |
| 15 | struct MemoryUsage { |
| 16 | size_t VmRSSKb; |
| 17 | size_t VmSwapKb; |
| 18 | |
| 19 | bool operator==(const MemoryUsage& other) const; |
| 20 | }; |
| 21 | |
| 22 | // Gets the memory usage by parsing a file (typically `/proc/[pid]/status`) |
| 23 | // This function assumes that the memory unit used in /proc/[pid]/status is |
| 24 | // "kB". |
| 25 | // Return true if successful, false otherwise. |
| 26 | bool GetProcessMemoryUsageFromFile(MemoryUsage* memory_usage, |
| 27 | const base::FilePath& file_path); |
| 28 | |
Honglin Yu | 7b6c119 | 2020-09-16 10:07:17 +1000 | [diff] [blame] | 29 | // Get the memory usage of a process whose PID is `pid`. |
| 30 | // Return true if successful, false otherwise. |
| 31 | bool GetProcessMemoryUsage(MemoryUsage* memory_usage, pid_t pid); |
| 32 | |
Honglin Yu | 1cd2507 | 2019-07-09 11:54:14 +1000 | [diff] [blame] | 33 | // Same as GetProcessMemoryUsageFromFile(memory_usage, "/prod/[pid]/status") |
| 34 | // for the calling process's pid. |
| 35 | // Return true if successful, false otherwise. |
| 36 | bool GetProcessMemoryUsage(MemoryUsage* memory_usage); |
| 37 | |
| 38 | // Gets the total memory usage for this process, which we define as VmSwap+VmRSS |
| 39 | // extracted from the /proc/pid/status file. |
| 40 | // Return true if successful, false otherwise. |
| 41 | bool GetTotalProcessMemoryUsage(size_t* total_memory); |
| 42 | |
Honglin Yu | 3e360a0 | 2020-08-27 10:30:01 +1000 | [diff] [blame] | 43 | // "dlopen() with RTLD_DEEPBIND" does not work with ASAN. So currently we only |
| 44 | // support services using this (e.g. HandwritingLibrary) when the "sanitizer" is |
| 45 | // not enabled (see https://crbug.com/1082632). |
| 46 | constexpr bool IsAsan() { |
| 47 | return __has_feature(address_sanitizer); |
| 48 | } |
| 49 | |
Honglin Yu | 1cd2507 | 2019-07-09 11:54:14 +1000 | [diff] [blame] | 50 | } // namespace ml |
| 51 | |
| 52 | #endif // ML_UTIL_H_ |