Ubuntu使用top command监测CPU、内存使用情况
最近学习了几种在Ubuntu中监测CPU和内存使用情况的方法,整理一下。
%%%%%%% top命令行
top命令用于显示Linux进程。它提供了运行系统的动态实时视图。通常,这个命令显示系统的摘要信息以及当前由Linux内核管理的进程或线程的列表。
一旦运行此命令,它将打开一个交互式命令模式,其中上半部分将包含进程和资源使用情况的统计信息。下半部分包含当前正在运行的进程的列表。按下q将简单地退出命令模式。
top
Fig 1: top命令行结果示意
如果提示没有安装top则需要
sudo apt install apt-file && apt-file update
面板信息:
PID:显示任务的唯一进程id。
PR:表示任务的优先级。
SHR:表示任务使用的共享内存数量。
VIRT:任务使用的总虚拟内存。
USER:任务所有者的用户名。
%CPU:表示CPU使用量。
TIME+:CPU时间,与“TIME”相同,但通过百分之一秒反映出更细的粒度。
表示任务使用的共享内存大小(kb)。
NI:表示任务的NI值。一个负的NICE值意味着更高的优先级,而正的NICE值意味着更低的优先级。
%MEM:显示任务的内存使用情况。
top -n 10 -d 1 -H -i -b > top-output.txt
这行命令是将top的输出结果读取十次 ('-n 10'),时间间隔1s ('-d 1'),'H' 代表我们查看的Threads,'I' 表示Irix Mode,'b'表示Batch Mode : Send output from top to file or any other programs。关于这几种模式,大家可以直接在Fig 1显示的界面中用shift+i, shift+h体会一下。
这时我们就能看到输出结果了,首先在对应目录下生成txt文件。
Fig 2: top在文件目录下输出结果
在命令行显示txt文件结果。
cat top-output.txt
结果如图:
Fig 3: 利用cat在命令行显示文件内容
为了满足不同需求,又尝试了几种不同的存储方式:
for i in {1..5}; do top -n 10 -d 0.1 -H -i -b >> ${i}.txt; done
输出结果:
将top输出存储到5个txt文件中,每个文件包含10次top输出结果,采样间隔为0.1s
这样我们就存取了5s内每间隔0.1s系统CPU和内存使用情况的数据,可以看到一个变化趋势。但是,由于每次存取内容格式并不完全一致,对于后续数据处理会是个问题,所以还是存为单个文件方便处理,将command稍作处理即可。
for i in {1..50}; do top -n 1 -d 0.1 -H -i -b >> ${i}.txt; done
这样就会存为50个txt文件,内容格式基本一致,方便后续处理。
附Matlab处理数据code:
% calculate CPU usage and MEM usage
% version 0.1
%==================== settings ================
file_num = 100;
CPU_col = 9; % you should look into the file to determine the value
MEM_col = 10; % you should look into the file to determine the value
clear clc
CPU.preamble_detect = zeros(1,file_num); MEM.preamble_detect = zeros(1,file_num);
CPU.fractional_res1 = zeros(1,file_num); MEM.fractional_res1 = zeros(1,file_num);
CPU.freq_xlating_f1 = zeros(1,file_num); MEM.freq_xlating_f1 = zeros(1,file_num);
CPU.python = zeros(1,file_num); MEM.python = zeros(1,file_num);
CPU.decoder9 = zeros(1,file_num); MEM.decoder9 = zeros(1,file_num);
CPU.gr_uhd_usrp = zeros(1,file_num); MEM.gr_uhd_usrp = zeros(1,file_num);
CPU.message_socket = zeros(1,file_num); MEM.message_socket = zeros(1,file_num);
CPU.usage_sum = zeros(1,file_num); MEM.usage_sum = zeros(1,file_num);
%====================== processing =========
for file_ind = 1:file_num
filename = [num2str(file_num) '.txt'];
% load data
raw_data = function_loadfile(filename);
% get the length of the data, so we know the number of the threads there
length_here = size(raw_data, 1);
CPU.usage_sum(file_ind) = sum(str2double(raw_data(11:length_here,CPU_col)));
MEM.usage_sum(file_ind) = sum(str2double(raw_data(11:length_here,MEM_col)));
% check the threads
for threads_ind = 11:length_here
switch raw_data(threads_ind,1)
case '25481'
CPU.preamble_detect(file_ind) = str2double(raw_data(threads_ind,CPU_col));
MEM.preamble_detect(file_ind) = str2double(raw_data(threads_ind,MEM_col));
case '25480'
CPU.freq_xlating_f1(file_ind) = str2double(raw_data(threads_ind,CPU_col));
MEM.freq_xlating_f1(file_ind) = str2double(raw_data(threads_ind,MEM_col));
case '25476'
CPU.python(file_ind) = str2double(raw_data(threads_ind,CPU_col));
MEM.python(file_ind) = str2double(raw_data(threads_ind,MEM_col));
case '25483'
CPU.decoder9(file_ind) = str2double(raw_data(threads_ind,CPU_col));
MEM.decoder9(file_ind) = str2double(raw_data(threads_ind,MEM_col));
case '25482'
CPU.fractional_res1(file_ind) = str2double(raw_data(threads_ind,CPU_col));
MEM.fractional_res1(file_ind) = str2double(raw_data(threads_ind,MEM_col));
case '25479'
CPU.gr_uhd_usrp(file_ind) = str2double(raw_data(threads_ind,CPU_col));
MEM.gr_uhd_usrp(file_ind) = str2double(raw_data(threads_ind,MEM_col));
case '25478'
CPU.message_socket(file_ind) = str2double(raw_data(threads_ind,CPU_col));
MEM.message_socket(file_ind) = str2double(raw_data(threads_ind,MEM_col));
end
end
end
top还有很多其他参数,感兴趣的可以自行学习。
man top
Peace!