go tool 分析內(nèi)存信息

可以使用go tool分析CPU渔彰、內(nèi)存占用情況時(shí)嵌屎,pprof進(jìn)行分析相關(guān)的cpu占用情況和內(nèi)存占用情況:
可以使用 go tool pprof binary profile 對(duì)內(nèi)存或是cpu的二進(jìn)制文件進(jìn)行分析:
舉例:

 go tool pprof memprofile 

memprofile 就是我們導(dǎo)出的內(nèi)存信息的二進(jìn)制文件,導(dǎo)出的代碼為:

// cpuFile 是cpu導(dǎo)出的使用信息的文件可帶目錄 memFile 內(nèi)存文件
util.SetupProfiling(*cpuFile, *memFile) 
 func SetupProfiling(cpuProfile, memProfile string) {
    if cpuProfile != "" {
        f, err := os.Create(cpuProfile)
        if err != nil {
            glog.Fatal(err)
        }
        pprof.StartCPUProfile(f)
        OnInterrupt(func() {
            pprof.StopCPUProfile()
        })
    }
    if memProfile != "" {
                // 此處默認(rèn)配置為512kb恍涂,即每512kb寫(xiě)一次heapprofile
        runtime.MemProfileRate = 1
        f, err := os.Create(memProfile)
        if err != nil {
            glog.Fatal(err)
        }
                // 此處封裝了一些方法會(huì)去做一次內(nèi)存的刷新宝惰,如停止服務(wù)時(shí)
        OnInterrupt(func() {
            pprof.WriteHeapProfile(f)
            f.Close()
        })
    }
}

通過(guò)命令查看:

 $ go tool pprof memprofile 
38812.64kB 73.11% 73.11% 38812.64kB 73.11%  github.com/chrislusf/seaweedfs/weed/storage/needle_map.NewCompactSection
   13720kB 25.84% 98.95%    13720kB 25.84%  github.com/chrislusf/seaweedfs/weed/storage/needle_map.(*CompactSection).setOverflowEntry
    7.22kB 0.014% 98.96% 49202.13kB 92.68%  github.com/chrislusf/seaweedfs/weed/storage.NewVolume
    1.97kB 0.0037% 98.97% 49194.91kB 92.66%  github.com/chrislusf/seaweedfs/weed/storage.(*Volume).load
    0.98kB 0.0019% 98.97% 49203.13kB 92.68%  github.com/chrislusf/seaweedfs/weed/storage.(*DiskLocation).loadExistingVolume
    0.29kB 0.00054% 98.97% 52532.93kB 98.95%  github.com/chrislusf/seaweedfs/weed/storage/needle_map.(*CompactMap).Set
    0.19kB 0.00035% 98.97% 49203.32kB 92.68%  github.com/chrislusf/seaweedfs/weed/storage.(*DiskLocation).concurrentLoadingVolumes.func2
    0.05kB 8.8e-05% 98.97%  3394.50kB  6.39%  net/http.(*conn).serve
         0     0% 98.97%  3363.91kB  6.34%  github.com/chrislusf/seaweedfs/weed/server.(*VolumeServer).PostHandler
         0     0% 98.97%  3363.91kB  6.34%  github.com/chrislusf/seaweedfs/weed/server.(*VolumeServer).privateStoreHandler

可以使用以下命令查看使用詳情:

$ go tool pprof --help
usage:

Produce output in the specified format.

   pprof <format> [options] [binary] <source> ...

Omit the format to get an interactive shell whose commands can be used
to generate various views of a profile

   pprof [options] [binary] <source> ...

Omit the format and provide the "-http" flag to get an interactive web
interface at the specified host:port that can be used to navigate through
various views of a profile.

   pprof -http [host]:[port] [options] [binary] <source> ...

Details:
  Output formats (select at most one):
    -callgrind       Outputs a graph in callgrind format
    -comments        Output all profile comments
    -disasm          Output assembly listings annotated with samples
    -dot             Outputs a graph in DOT format
    -eog             Visualize graph through eog
    -evince          Visualize graph through evince
    -gif             Outputs a graph image in GIF format
    -gv              Visualize graph through gv
    -kcachegrind     Visualize report in KCachegrind
    -list            Output annotated source for functions matching regexp
    -pdf             Outputs a graph in PDF format
    -peek            Output callers/callees of functions matching regexp
    -png             Outputs a graph image in PNG format
    -proto           Outputs the profile in compressed protobuf format
    -ps              Outputs a graph in PS format
    -raw             Outputs a text representation of the raw profile
    -svg             Outputs a graph in SVG format
    -tags            Outputs all tags in the profile
    -text            Outputs top entries in text form
    -top             Outputs top entries in text form
    -topproto        Outputs top entries in compressed protobuf format
    -traces          Outputs all profile samples in text form
    -tree            Outputs a text rendering of call graph
    -web             Visualize graph through web browser
    -weblist         Display annotated source in a web browser

  Options:
    -call_tree       Create a context-sensitive call tree
    -compact_labels  Show minimal headers
    -divide_by       Ratio to divide all samples before visualization
    -drop_negative   Ignore negative differences
    -edgefraction    Hide edges below <f>*total
    -focus           Restricts to samples going through a node matching regexp
    -hide            Skips nodes matching regexp
    -ignore          Skips paths going through any nodes matching regexp
    -mean            Average sample value over first value (count)
    -nodecount       Max number of nodes to show
    -nodefraction    Hide nodes below <f>*total
    -noinlines       Ignore inlines.
    -normalize       Scales profile based on the base profile.
    -output          Output filename for file-based outputs
    -prune_from      Drops any functions below the matched frame.
    -relative_percentages Show percentages relative to focused subgraph
    -sample_index    Sample value to report (0-based index or name)
    -show            Only show nodes matching regexp
    -show_from       Drops functions above the highest matched frame.
    -source_path     Search path for source files
    -tagfocus        Restricts to samples with tags in range or matched by regexp
    -taghide         Skip tags matching this regexp
    -tagignore       Discard samples with tags in range or matched by regexp
    -tagshow         Only consider tags matching this regexp
    -trim            Honor nodefraction/edgefraction/nodecount defaults
    -trim_path       Path to trim from source paths before search
    -unit            Measurement units to display

  Option groups (only set one per group):
    cumulative       
      -cum             Sort entries based on cumulative weight
      -flat            Sort entries based on own weight
    granularity      
      -addresses       Aggregate at the address level.
      -filefunctions   Aggregate at the function level.
      -files           Aggregate at the file level.
      -functions       Aggregate at the function level.
      -lines           Aggregate at the source code line level.

  Source options:
    -seconds              Duration for time-based profile collection
    -timeout              Timeout in seconds for profile collection
    -buildid              Override build id for main binary
    -add_comment          Free-form annotation to add to the profile
                          Displayed on some reports or with pprof -comments
    -diff_base source     Source of base profile for comparison
    -base source          Source of base profile for profile subtraction
    profile.pb.gz         Profile in compressed protobuf format
    legacy_profile        Profile in legacy pprof format
    http://host/profile   URL for profile handler to retrieve
    -symbolize=           Controls source of symbol information
      none                  Do not attempt symbolization
      local                 Examine only local binaries
      fastlocal             Only get function names from local binaries
      remote                Do not examine local binaries
      force                 Force re-symbolization
    Binary                  Local path or build id of binary for symbolization
    -tls_cert             TLS client certificate file for fetching profile and symbols
    -tls_key              TLS private key file for fetching profile and symbols
    -tls_ca               TLS CA certs file for fetching profile and symbols

  Misc options:
   -http              Provide web based interface at host:port.
                      Host is optional and 'localhost' by default.
                      Port is optional and a randomly available port by default.
   -tools             Search path for object tools

  Legacy convenience options:
   -inuse_space           Same as -sample_index=inuse_space
   -inuse_objects         Same as -sample_index=inuse_objects
   -alloc_space           Same as -sample_index=alloc_space
   -alloc_objects         Same as -sample_index=alloc_objects
   -total_delay           Same as -sample_index=delay
   -contentions           Same as -sample_index=contentions
   -mean_delay            Same as -mean -sample_index=delay

  Environment Variables:
   PPROF_TMPDIR       Location for saved profiles (default $HOME/pprof)
   PPROF_TOOLS        Search path for object-level tools
   PPROF_BINARY_PATH  Search path for local binary files
                      default: $HOME/pprof/binaries
                      searches $name, $path, $buildid/$name, $path/$buildid
   * On Windows, %USERPROFILE% is used instead of $HOME
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市再沧,隨后出現(xiàn)的幾起案子尼夺,更是在濱河造成了極大的恐慌,老刑警劉巖炒瘸,帶你破解...
    沈念sama閱讀 218,941評(píng)論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件淤堵,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡什燕,警方通過(guò)查閱死者的電腦和手機(jī)粘勒,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,397評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門(mén),熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)屎即,“玉大人庙睡,你說(shuō)我怎么就攤上這事〖祭” “怎么了乘陪?”我有些...
    開(kāi)封第一講書(shū)人閱讀 165,345評(píng)論 0 356
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)雕擂。 經(jīng)常有香客問(wèn)我啡邑,道長(zhǎng),這世上最難降的妖魔是什么井赌? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 58,851評(píng)論 1 295
  • 正文 為了忘掉前任谤逼,我火速辦了婚禮贵扰,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘流部。我一直安慰自己戚绕,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,868評(píng)論 6 392
  • 文/花漫 我一把揭開(kāi)白布枝冀。 她就那樣靜靜地躺著舞丛,像睡著了一般。 火紅的嫁衣襯著肌膚如雪果漾。 梳的紋絲不亂的頭發(fā)上球切,一...
    開(kāi)封第一講書(shū)人閱讀 51,688評(píng)論 1 305
  • 那天,我揣著相機(jī)與錄音绒障,去河邊找鬼吨凑。 笑死,一個(gè)胖子當(dāng)著我的面吹牛端盆,可吹牛的內(nèi)容都是我干的怀骤。 我是一名探鬼主播费封,決...
    沈念sama閱讀 40,414評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼焕妙,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來(lái)了弓摘?” 一聲冷哼從身側(cè)響起焚鹊,我...
    開(kāi)封第一講書(shū)人閱讀 39,319評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎韧献,沒(méi)想到半個(gè)月后末患,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,775評(píng)論 1 315
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡锤窑,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,945評(píng)論 3 336
  • 正文 我和宋清朗相戀三年璧针,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片渊啰。...
    茶點(diǎn)故事閱讀 40,096評(píng)論 1 350
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡探橱,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出绘证,到底是詐尸還是另有隱情隧膏,我是刑警寧澤,帶...
    沈念sama閱讀 35,789評(píng)論 5 346
  • 正文 年R本政府宣布嚷那,位于F島的核電站胞枕,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏魏宽。R本人自食惡果不足惜腐泻,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,437評(píng)論 3 331
  • 文/蒙蒙 一决乎、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧派桩,春花似錦瑞驱、人聲如沸。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 31,993評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至鸭津,卻和暖如春彤侍,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背逆趋。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 33,107評(píng)論 1 271
  • 我被黑心中介騙來(lái)泰國(guó)打工盏阶, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人闻书。 一個(gè)月前我還...
    沈念sama閱讀 48,308評(píng)論 3 372
  • 正文 我出身青樓名斟,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親魄眉。 傳聞我的和親對(duì)象是個(gè)殘疾皇子砰盐,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,037評(píng)論 2 355

推薦閱讀更多精彩內(nèi)容

  • 在計(jì)算機(jī)性能調(diào)試領(lǐng)域里,profiling 是指對(duì)應(yīng)用程序的畫(huà)像坑律,畫(huà)像就是應(yīng)用程序使用 CPU 和內(nèi)存的情況岩梳。 G...
    雪上霜閱讀 913評(píng)論 0 1
  • 概要 profile就是定時(shí)采樣,收集cpu晃择,內(nèi)存等信息冀值,進(jìn)而給出性能優(yōu)化指導(dǎo),golang 官方提供了golan...
    糖果果老師閱讀 8,995評(píng)論 0 6
  • 本文作者歐長(zhǎng)坤宫屠,德國(guó)慕尼黑大學(xué)在讀博士列疗,Go/etcd/Tensorflow contributor,開(kāi)源書(shū)籍《G...
    水表學(xué)Java閱讀 448評(píng)論 0 0
  • 1. 概述 在 go 語(yǔ)言中浪蹂,主要關(guān)注的應(yīng)用運(yùn)行情況主要包括以下幾種: CPU Profiling:CPU 分析抵栈,...
    northking閱讀 553評(píng)論 0 0
  • 久違的晴天,家長(zhǎng)會(huì)乌逐。 家長(zhǎng)大會(huì)開(kāi)好到教室時(shí)竭讳,離放學(xué)已經(jīng)沒(méi)多少時(shí)間了。班主任說(shuō)已經(jīng)安排了三個(gè)家長(zhǎng)分享經(jīng)驗(yàn)浙踢。 放學(xué)鈴聲...
    飄雪兒5閱讀 7,523評(píng)論 16 22