blob: f7dc55119eaba2a1aa27fbc25e436139aa08d3c7 [file] [log] [blame]
Honglin Yu1cd25072019-07-09 11:54:14 +10001// 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
10namespace 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.
15struct 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.
26bool GetProcessMemoryUsageFromFile(MemoryUsage* memory_usage,
27 const base::FilePath& file_path);
28
Honglin Yu7b6c1192020-09-16 10:07:17 +100029// Get the memory usage of a process whose PID is `pid`.
30// Return true if successful, false otherwise.
31bool GetProcessMemoryUsage(MemoryUsage* memory_usage, pid_t pid);
32
Honglin Yu1cd25072019-07-09 11:54:14 +100033// Same as GetProcessMemoryUsageFromFile(memory_usage, "/prod/[pid]/status")
34// for the calling process's pid.
35// Return true if successful, false otherwise.
36bool 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.
41bool GetTotalProcessMemoryUsage(size_t* total_memory);
42
Honglin Yu3e360a02020-08-27 10:30:01 +100043// "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).
46constexpr bool IsAsan() {
47 return __has_feature(address_sanitizer);
48}
49
Honglin Yu1cd25072019-07-09 11:54:14 +100050} // namespace ml
51
52#endif // ML_UTIL_H_