blob: b323ca95fe88d2ef885143b70b1580b73929e2a5 [file] [log] [blame]
Seungjae Yoofd9a0622022-10-14 10:01:29 +09001// Copyright 2022, The Android Open Source Project
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use anyhow::{Context, Result};
16use libc::{sysconf, _SC_CLK_TCK};
17use regex::Regex;
18use std::fs::{self, File};
19use std::io::{BufRead, BufReader};
20
21const MILLIS_PER_SEC: i64 = 1000;
22
23pub struct CpuTime {
24 pub user: i64,
25 pub nice: i64,
26 pub sys: i64,
27 pub idle: i64,
28}
29
30pub struct MemInfo {
31 pub total: i64,
32 pub free: i64,
33 pub available: i64,
34 pub buffer: i64,
35 pub cached: i64,
36}
37
38// Get CPU time information from /proc/stat
39//
40// /proc/stat example(omitted):
41// cpu 24790952 21104390 10771070 10480973587 1700955 0 410931 0 316532 0
42// cpu0 169636 141307 61153 81785791 9605 0 183524 0 1345 0
43// cpu1 182431 198327 68273 81431817 10445 0 32392 0 2616 0
44// cpu2 183209 174917 68591 81933935 12239 0 10042 0 2415 0
45// cpu3 183413 177758 69908 81927474 13354 0 5853 0 2491 0
46// intr 7913477443 39 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
47// ctxt 10326710014
48// btime 1664123605
49// processes 9225712
50// procs_running 1
51// procs_blocked 0
52// softirq 2683914305 14595298 304837101 1581 327291100 16397051 0 208857783 1024640365 787932 786506094
53//
54// expected output:
55// user: 24790952
56// nice: 21104390
57// sys: 10771070
58// idle: 10480973587
59pub fn get_cpu_time() -> Result<CpuTime> {
60 let re = Regex::new(r"^cpu\s+([\d]+)\s([\d]+)\s([\d]+)\s([\d]+)").unwrap();
61
62 let mut proc_stat = BufReader::new(File::open("/proc/stat")?);
63 let mut line = String::new();
64 proc_stat.read_line(&mut line)?;
65 let data_list = re.captures(&line).context("Failed to capture values")?;
66
67 let ticks_per_sec = unsafe { sysconf(_SC_CLK_TCK) } as i64;
68 let cpu_time = CpuTime {
69 user: data_list.get(1).unwrap().as_str().parse::<i64>()? * MILLIS_PER_SEC / ticks_per_sec,
70 nice: data_list.get(2).unwrap().as_str().parse::<i64>()? * MILLIS_PER_SEC / ticks_per_sec,
71 sys: data_list.get(3).unwrap().as_str().parse::<i64>()? * MILLIS_PER_SEC / ticks_per_sec,
72 idle: data_list.get(4).unwrap().as_str().parse::<i64>()? * MILLIS_PER_SEC / ticks_per_sec,
73 };
74 Ok(cpu_time)
75}
76
77// Get memory information from /proc/meminfo
78//
79// /proc/meminfo example(omitted):
80// MemTotal: 263742736 kB
81// MemFree: 37144204 kB
82// MemAvailable: 249168700 kB
83// Buffers: 10231296 kB
84// Cached: 189502836 kB
85// SwapCached: 113848 kB
86// Active: 132266424 kB
87// Inactive: 73587504 kB
88// Active(anon): 1455240 kB
89// Inactive(anon): 6993584 kB
90// Active(file): 130811184 kB
91// Inactive(file): 66593920 kB
92// Unevictable: 56436 kB
93// Mlocked: 56436 kB
94// SwapTotal: 255123452 kB
95// SwapFree: 254499068 kB
96// Dirty: 596 kB
97// Writeback: 0 kB
98// AnonPages: 5295864 kB
99// Mapped: 3512608 kB
100//
101// expected output:
102// total: 263742736
103// free: 37144204
104// available: 249168700
105// buffer: 10231296
106// cached: 189502836
107pub fn get_mem_info() -> Result<MemInfo> {
108 let re = Regex::new(r"^.*?:\s+([0-9]+)\skB").unwrap();
109
110 let proc_mem_info = fs::read_to_string("/proc/meminfo")?;
111 let data_list: Vec<_> = proc_mem_info
112 .trim()
113 .splitn(6, '\n')
114 .map(|s| re.captures(s).context("Failed to capture values").ok()?.get(1))
115 .collect();
116
117 let mem_info = MemInfo {
118 total: data_list[0].unwrap().as_str().parse::<i64>()?,
119 free: data_list[1].unwrap().as_str().parse::<i64>()?,
120 available: data_list[2].unwrap().as_str().parse::<i64>()?,
121 buffer: data_list[3].unwrap().as_str().parse::<i64>()?,
122 cached: data_list[4].unwrap().as_str().parse::<i64>()?,
123 };
124 Ok(mem_info)
125}