doupi6737
doupi6737
2012-07-24 05:18

Go- CPU /内存/网络

已采纳

I'm writing a cross-platform Go application that runs as a daemon. It basically listens for changes on the filesystem and copies/downloads changes to/from a server. The idea is that it should never slow down the machine, queueing tasks until the machine is idle.

To do this, I would like to monitor CPU, RAM, and network usage, but network usage (if possible). I am currently targeting Windows 7+, Mac OS 10+, and Linux 3+.

I'm primarily a Linux programmer, so I'm not familiar with Windows/BSD APIs, but I've already found some good information about BSD, so I think I can figure that out. I'm having trouble tracking down the Windows syscalls (I don't know of a way to get a Windows man page...). I've managed to find these resources (for Windows):

I would prefer to use a syscall instead of shelling out/wrapping a C++ API. Obviously, I would be interested in any other Go libraries that already do this.

Ultimately, I'm looking for something like this: http://nodejs.org/api/os.html

I'm pretty much looking for syscalls and examples of how to use them.

If I'm headed in the wrong direction, please let me know. I'm not married to the idea of polling the kernel for system information, but that seemed to me the most reliable way to get that information. I'd prefer to run the application as an underprivileged user.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答

  • dqd2800 dqd2800 9年前

    I found https://github.com/AllenDang/w32, a project that provides wrappers for a lot of Windows API calls. I added a call to GetSystemTimes in my fork, and that is what I am currently using. I've opened a pull request, so it should be merged soon.

    This allowed me to get CPU information (times for idle, kernel and user, similar to /proc/stats), and it seems to be working pretty well. Here's some example code:

    package main
    
    import (
        "w32"
        "fmt"
        "time"
    )
    
    func main() {
        var idle, kernel, user w32.FILETIME
    
        w32.GetSystemTimes(&idle, &kernel, &user)
        idleFirst   := idle.DwLowDateTime   | (idle.DwHighDateTime << 32)
        kernelFirst := kernel.DwLowDateTime | (kernel.DwHighDateTime << 32)
        userFirst   := user.DwLowDateTime   | (user.DwHighDateTime << 32)
    
        time.Sleep(time.Second)
    
        w32.GetSystemTimes(&idle, &kernel, &user)
        idleSecond   := idle.DwLowDateTime   | (idle.DwHighDateTime << 32)
        kernelSecond := kernel.DwLowDateTime | (kernel.DwHighDateTime << 32)
        userSecond   := user.DwLowDateTime   | (user.DwHighDateTime << 32)
    
        totalIdle   := float64(idleSecond - idleFirst)
        totalKernel := float64(kernelSecond - kernelFirst)
        totalUser   := float64(userSecond - userFirst)
        totalSys    := float64(totalKernel + totalUser)
    
        fmt.Printf("Idle: %f%%
    Kernel: %f%%
    User: %f%%
    ", (totalIdle / totalSys) * 100, (totalKernel / totalSys) * 100, (totalUser / totalSys) * 100)
        fmt.Printf("
    Total: %f%%
    ", (totalSys - totalIdle) * 100 / totalSys)
    }
    

    If I can find RAM/Network stats, I should be set.

    点赞 评论 复制链接分享

为你推荐