dskzap8756 2013-11-01 00:16
浏览 52
已采纳

使用GO时如何测量系统过载

I am rewriting an old system in GO, and in the old system I was measuring the system load average to know if I should increase the number of thread in my thread-pool.

In go people are not using threadpool or pool of goroutine because starting a goroutine is very cheap. But still running too many goroutine is less efficient then just enough to keep the cpu usage near 100%

Thus is there a way to know how many goroutine are ready to run (not blocked) but not currently running. Or is there a way to get the number of scheduled runnable goroutine "Run queue".

  • 写回答

2条回答 默认 最新

  • douhao2721 2013-11-01 01:03
    关注

    Check out the runtime/pprof package.

    To print "stack traces of all current goroutines" use:

    pprof.Lookup("goroutine").WriteTo(os.Stdout, 1)
    

    To print "stack traces that led to blocking on synchronization primitives" use:

    pprof.Lookup("block").WriteTo(os.Stdout, 1)
    

    You can combine these with the functions in the runtime package such as runtime.NumGoroutine to get some basic reporting.

    This example deliberately creates many blocked goroutines and waits for them to complete. Every 5 seconds it prints the output of the block pprof profile, as well as the number of goroutines still in existence:

    package main
    
    import (
        "fmt"
        "math/rand"
        "os"
        "runtime"
        "runtime/pprof"
        "strconv"
        "sync"
        "time"
    )
    
    var (
        wg sync.WaitGroup
        m  sync.Mutex
    )
    
    func randWait() {
        defer wg.Done()
        m.Lock()
        defer m.Unlock()
        interval, err := time.ParseDuration(strconv.Itoa(rand.Intn(499)+1) + "ms")
        if err != nil {
            fmt.Errorf("%s
    ", err)
        }
        time.Sleep(interval)
        return
    }
    
    func blockStats() {
        for {
            pprof.Lookup("block").WriteTo(os.Stdout, 1)
            fmt.Println("# Goroutines:", runtime.NumGoroutine())
            time.Sleep(5 * time.Second)
        }
    }
    
    func main() {
        rand.Seed(time.Now().Unix())
        runtime.SetBlockProfileRate(1)
        fmt.Println("Running...")
        for i := 0; i < 100; i++ {
            wg.Add(1)
            go randWait()
        }
        go blockStats()
        wg.Wait()
        fmt.Println("Finished.")
    }
    

    I'm not sure if that's what you're after, but you may be able to modify it to suit your needs.

    Playground

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥60 pb数据库修改或者求完整pb库存系统,需为pb自带数据库
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路