最近學(xué)習(xí)了幾種在Ubuntu中監(jiān)測(cè)CPU和內(nèi)存使用情況的方法矛洞,整理一下。
%%%%%%% top命令行
top命令用于顯示Linux進(jìn)程烫映。它提供了運(yùn)行系統(tǒng)的動(dòng)態(tài)實(shí)時(shí)視圖沼本。通常,這個(gè)命令顯示系統(tǒng)的摘要信息以及當(dāng)前由Linux內(nèi)核管理的進(jìn)程或線程的列表锭沟。
一旦運(yùn)行此命令抽兆,它將打開一個(gè)交互式命令模式,其中上半部分將包含進(jìn)程和資源使用情況的統(tǒng)計(jì)信息族淮。下半部分包含當(dāng)前正在運(yùn)行的進(jìn)程的列表辫红。按下q將簡(jiǎn)單地退出命令模式凭涂。
top
如果提示沒有安裝top則需要
sudo apt install apt-file && apt-file update
面板信息:
PID:顯示任務(wù)的唯一進(jìn)程id。
PR:表示任務(wù)的優(yōu)先級(jí)贴妻。
SHR:表示任務(wù)使用的共享內(nèi)存數(shù)量切油。
VIRT:任務(wù)使用的總虛擬內(nèi)存。
USER:任務(wù)所有者的用戶名名惩。
%CPU:表示CPU使用量澎胡。
TIME+:CPU時(shí)間,與“TIME”相同绢片,但通過百分之一秒反映出更細(xì)的粒度滤馍。
表示任務(wù)使用的共享內(nèi)存大小(kb)。
NI:表示任務(wù)的NI值底循。一個(gè)負(fù)的NICE值意味著更高的優(yōu)先級(jí)巢株,而正的NICE值意味著更低的優(yōu)先級(jí)。
%MEM:顯示任務(wù)的內(nèi)存使用情況熙涤。
top -n 10 -d 1 -H -i -b > top-output.txt
這行命令是將top的輸出結(jié)果讀取十次 ('-n 10')阁苞,時(shí)間間隔1s ('-d 1'),'H' 代表我們查看的Threads祠挫,'I' 表示Irix Mode那槽,'b'表示Batch Mode : Send output from top to file or any other programs。關(guān)于這幾種模式等舔,大家可以直接在Fig 1顯示的界面中用shift+i, shift+h體會(huì)一下骚灸。
這時(shí)我們就能看到輸出結(jié)果了,首先在對(duì)應(yīng)目錄下生成txt文件慌植。
在命令行顯示txt文件結(jié)果甚牲。
cat top-output.txt
結(jié)果如圖:
為了滿足不同需求,又嘗試了幾種不同的存儲(chǔ)方式:
for i in {1..5}; do top -n 10 -d 0.1 -H -i -b >> ${i}.txt; done
輸出結(jié)果:
這樣我們就存取了5s內(nèi)每間隔0.1s系統(tǒng)CPU和內(nèi)存使用情況的數(shù)據(jù),可以看到一個(gè)變化趨勢(shì)交汤。但是雏赦,由于每次存取內(nèi)容格式并不完全一致,對(duì)于后續(xù)數(shù)據(jù)處理會(huì)是個(gè)問題芙扎,所以還是存為單個(gè)文件方便處理星岗,將command稍作處理即可。
for i in {1..50}; do top -n 1 -d 0.1 -H -i -b >> ${i}.txt; done
這樣就會(huì)存為50個(gè)txt文件纵顾,內(nèi)容格式基本一致伍茄,方便后續(xù)處理。
附Matlab處理數(shù)據(jù)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還有很多其他參數(shù)施逾,感興趣的可以自行學(xué)習(xí)敷矫。
man top
Peace例获!