使用龍芯2f性能計數(shù)器評測程序性能_第1頁
使用龍芯2f性能計數(shù)器評測程序性能_第2頁
使用龍芯2f性能計數(shù)器評測程序性能_第3頁
使用龍芯2f性能計數(shù)器評測程序性能_第4頁
使用龍芯2f性能計數(shù)器評測程序性能_第5頁
已閱讀5頁,還剩5頁未讀 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)

文檔簡介

1、使用龍芯2f性能計數(shù)器評測程序性能評測一個程序的性能有多種方法,對目前大多數(shù)benchmark,程序執(zhí)行時間是一個很重要的標(biāo)準(zhǔn)。但大多數(shù)情況下時間僅僅能給出一個結(jié)果,卻給不了更多的信息,如cache miss,cpu功能部件的利用率,訪存指令,分支預(yù)測等。這些信息對于知道一個程序的優(yōu)化往往會起到很重要的作用。很多現(xiàn)代的cpu都有一些內(nèi)置的計數(shù)器用來記錄這些事件,x86環(huán)境下可以調(diào)用一些庫對這些計數(shù)器進行設(shè)置和訪問,具體可以參考這里。關(guān)于x86下使用performance counter的方法很多,而且資料也很容易找到,大家google一下就好了。龍芯從早一個版本(loongson 2e)就提供

2、了性能計數(shù)器,龍芯2f的計數(shù)器基本同2e大概上沒有什么區(qū)別,定義了兩個性能計數(shù)器(performance counter),分別映射到cp0(協(xié)處理器)的第24、25號寄存器。25號寄存器是64位,其高32位用作counter1,對應(yīng)的event1,低32位用作counter0,對應(yīng)event0,event0和event1是由24號寄存器的5-12位控制的,其中5-8位為event0,9-12位為event1,用戶可以通過設(shè)置24號寄存器相應(yīng)的位來控制counter記錄的事件,event0和event1對應(yīng)的事件如下所示:event0:0000周期0001分支指令0010指令0011指令并且域

3、rs=31 0100一級i-cache缺失0101 alu1操作已發(fā)射0110 mem操作已發(fā)射0111 falu1操作已發(fā)射1000猜測指令1001從主存中讀1010固定發(fā)射隊列滿的次數(shù)1011重排隊列滿的次數(shù)1100 cp0隊列滿的次數(shù)1101 tlb重填例外1110例外1111內(nèi)部例外event1:0000提交操作0001分支預(yù)測失敗0010預(yù)測失敗0011 jr且rs=31預(yù)測失敗0100一級d-cache缺失0101 alu2操作已發(fā)射0110 falu2操作已發(fā)射0111 uncached訪問1000 bht猜測錯誤1001寫到主存1010浮點指針隊列滿的次數(shù)1011分支隊列滿的次

4、數(shù)1100 itlb缺失1101例外總數(shù)1110投機缺失1111隊列向前加載要使用計數(shù)器,首先要設(shè)置24號寄存器中event0和event1所對應(yīng)的域。比如你要測一級i-cache miss和一級d-cache miss,那么就要把24號寄存器設(shè)置成0x44f(0-4位以及其他位為默認值)。但是有一個問題,龍芯2f協(xié)處理器只有在內(nèi)核態(tài)下才能更改,因此我們要更改24號寄存器,需要模塊的幫助。龍芯的協(xié)處理器是通過mfc0/dmfc0來讀和通過mft0/dmft0來寫的,如果在用戶態(tài)下使用mft0/dmft0指令,會在運行時得到非法指令的錯誤信息。即便是在內(nèi)核態(tài)下,很多寄存器也是只讀的。下面我們寫一

5、個簡單的小模塊來更改event0和event1:loongson_counter.c#include linux/init.h#include linux/module.h module_license(dual bsd/gpl);static int event_init(void)unsigned int event=0x44f;asm volatile(mtc0%0,:r(event);return 0;static void event_exit(void)module_init(set_init);module_exit(set_exit);makefileifneq($(kerne

6、lrelease),)obj-m:=loongson_counter.o else kerneldir?=/lib/modules/$(shell uname-r)/build pwd:=$(shell pwd)clean:rm-rf*.o*core.depend.*.cmd*.ko*.mod.c.tmp_versions default:$(make)-c$(kerneldir)m=$(pwd)modules endif make生成模塊后,使用insmod loongson_counter.ko將模塊加載,這時就可以使用25號寄存器記錄i-cache miss和d-cache miss了。

7、在具體程序中,如果你要計算一個函數(shù)帶來的cache miss,可以在這個函數(shù)開頭通過mfc0/dmfc0指令讀取計數(shù)器中的初始值,然后在函數(shù)結(jié)尾,再讀取一次,兩次相減就得到了該函數(shù)造成的cache miss。注意硬件性能計數(shù)器是一個全局的計數(shù)器,即如果你在運行你要測試的函數(shù)同時運行了其他程序,其他程序造成的cache miss也會記錄在計數(shù)器里。所以在保持cpu獨占的情況下可以測試幾次取平均值。下面以一個sort()函數(shù)為例示例怎么在函數(shù)中應(yīng)用:sort.cvoid sort()usigned int icache_init,dcache_initi,cache_miss,dcache_mis

8、s;asm volatile(.set mips3ntdmfc0%0,ntdsra%0,32ntmfc0%0,nt:=r(dcache_init),=r(icache_init);/把counter的初始值記錄下來qsort(.);.;asm volatile(.set mips3ntdmfc0%0,ntdsra%0,32ntmfc0%0,nt:=r(dcache_miss),=r(icache_miss);/記錄函數(shù)運行后計數(shù)器的值fprintf(stderr,i cache miss is:%dn,icache_miss-icache_init);fprintf(stderr,d cach

9、e miss is:%dn,dcache_miss-dcache_init);/紅色部分為使用performance counter所加入的代碼現(xiàn)在我們已經(jīng)可以使龍芯2f的計數(shù)器起作用了,但是還存在一個問題,如果我有一個代碼量相對比較多的程序,想做一個工具來多方面的評測這個程序,許要使用到各種不同的計數(shù)器事件,而又不想每次都重新編譯加載一個內(nèi)核來改變這個事件,這時候就需要寫一個相對稍復(fù)雜一些的模塊用來設(shè)置事件,下面是我寫的一個模塊,可以通過write系統(tǒng)調(diào)用來改變事件。loongson_counter.c#include linux/module.h#include linux/init.h#

10、include linux/fs.h#include linux/cdev.h module_license(dual bsd/gpl);module_author(sponge);module_description(change event of performance counter);module_alias(set_event);/function convert char to int int convert_event(char*array)int ret=0;int i;for(i=0;i=3;i+)/printk(ret is:%x,i is%x,arrayiis%cn,re

11、t,i,arrayi);if(arrayi=1)ret=ret+(1(3-i);else if(arrayi=0)continue;else return-einval;return ret;/function used to modify the register int event_write(struct file*filp,const char _user*buff,size_t count,loff_t*f_pos)int e0,e1;/variables hold the event number char buff_k9;/buffer holds date transform

12、from user space char event05;char event15;/these two array hold event number in form of char int i;/loop counter int regval;/the value that will transform to if(count!=9)return-einval;/copy user date if(copy_from_user(buff_k,buff,9)return-efault;/convert user date to array for(i=0;i=3;i+)event0i=buf

13、f_ki;event1i=buff_ki+4;/convert char to int if(e0=convert_event(event0)=-einval)return-einval;if(e1=convert_event(event1)=-einval)return-einval;/printk(event0 is%x,event1 is%xn,e0,e1);/compute regval=(e0 5)|(e1 9)|0xf;asm volatile(mtc0%0,:r(regval);/unsigned int val=0;/asm volatile(mfc0%0,:=r(val);/

14、printk(kern_emergcurrent cp0_24 value is 0x%xn,val);return 0;struct cdev event;struct file_operations event_ops;/init function static int init_event(void)/register the device event if(register_chrdev_region(mkdev(33,0),1,pcounter)return-efault;/register operations event_ops.write=event_write;cdev_init(&event,&event_ops);cdev_add(&event,mkdev(33,0),1);return 0;/clean function static void exit_event(void)/unregister device cdev_del(&event);unregister_chrdev_region(mkdev(33,0),1);module_init(init_event);module_exit(exit_event);makefile跟上一個例子的基本一致。使用該模塊的方法為:1、在root下使

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論