doupi6737 2012-07-24 05:18
浏览 76
已采纳

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 2012-07-25 07:57
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?