APIs in go for CPU and memory usage

5,408 views
Skip to first unread message

ISockGo

unread,
Sep 25, 2012, 11:05:12 AM9/25/12
to golan...@googlegroups.com
Hi,

     I am almost done with a chat server in Go, it's going good I guess. Now I am required to do some manipulation at runtime according to CPU and memory usage. Can one suggest me the apis in Go to check the CPU and memory usage of system at the run time and right way to use it. 

Steven Rice

unread,
Sep 25, 2012, 11:18:35 AM9/25/12
to golan...@googlegroups.com
You probably need the runtime package from the standard library.

ISockGo

unread,
Sep 25, 2012, 12:35:39 PM9/25/12
to golan...@googlegroups.com
I will really appreciate if you can elaborate it little more, which API can give me CPU or memory usage at the run time. I have checked the runtime package, but didn't figure out a api, which can help me in this scenario.

Thanks,
Chuck james

minux

unread,
Sep 25, 2012, 1:16:38 PM9/25/12
to ISockGo, golan...@googlegroups.com

On Wednesday, September 26, 2012, ISockGo wrote:
I will really appreciate if you can elaborate it little more, which API can give me CPU or memory usage at the run time. I have checked the runtime package, but didn't figure out a api, which can help me in this scenario.
Do you want CPU/memory usage for your program or the whole system?
For the first case, runtime (ReadMemStats for example) and possibly runtime/pprof
will help you.

For the latter case, you need to use OS-specific ways to get that info.

ISockGo

unread,
Sep 25, 2012, 1:25:09 PM9/25/12
to golan...@googlegroups.com, ISockGo
No, not for my program but for complete system (server) so that I can manage connection acception or rejection or data transfer rate accordingly. 

minux

unread,
Sep 25, 2012, 1:27:50 PM9/25/12
to ISockGo, golan...@googlegroups.com

On Wednesday, September 26, 2012, ISockGo wrote:
No, not for my program but for complete system (server) so that I can manage connection acception or rejection or data transfer rate accordingly. 
Ok, then you need to research what other programs do to get this
as Go standard library won't help you (directly).

ISockGo

unread,
Sep 25, 2012, 2:48:24 PM9/25/12
to golan...@googlegroups.com, ISockGo
Any other open source way/lib to get this fixed. Any suggestion will be helpful.. 

minux

unread,
Sep 25, 2012, 2:53:18 PM9/25/12
to ISockGo, golan...@googlegroups.com

On Wednesday, September 26, 2012, ISockGo wrote:
Any other open source way/lib to get this fixed. Any suggestion will be helpful.. 
Which OS do you use?

ISockGo

unread,
Sep 25, 2012, 3:03:04 PM9/25/12
to golan...@googlegroups.com, ISockGo
Linux and Mac (priority for linux)

Rémy Oudompheng

unread,
Sep 25, 2012, 3:48:43 PM9/25/12
to ISockGo, golan...@googlegroups.com
On 2012/9/25 ISockGo <amitab...@gmail.com> wrote:
> Linux and Mac (priority for linux)

Reading /proc/loadavg and /proc/meminfo can be a first step.

Rémy.

Xing Xing

unread,
Sep 26, 2012, 3:02:06 AM9/26/12
to golan...@googlegroups.com
于 Tue, 25 Sep 2012 12:03:04 -0700 (PDT)
ISockGo <amitab...@gmail.com> 写道:

> Linux and Mac (priority for linux)

https://github.com/alouca/gosnmp

The pkg gosnmp should fit for you.

minux

unread,
Sep 26, 2012, 7:07:33 AM9/26/12
to ISockGo, golan...@googlegroups.com
On Wed, Sep 26, 2012 at 3:03 AM, ISockGo <amitab...@gmail.com> wrote:
Linux and Mac (priority for linux)
For Mac, try sysctl(3). 

Or, if you're willing to use cgo, you can call getloadavg(3).

Doug MacEachern

unread,
Sep 26, 2012, 9:05:55 PM9/26/12
to golang-nuts
Sounds like sigar may have the functionality you're looking for:
https://github.com/cloudfoundry/gosigar

dharma...@gmail.com

unread,
Mar 5, 2017, 12:25:29 AM3/5/17
to golang-nuts
On linux, you can read in /proc/stat for cpu specific and aggregated data. Other info in /proc/stat can get you info on each running process. Google for procfs and /proc/stat... 

Anurag Rao

unread,
Jul 5, 2024, 9:41:04 PM (2 days ago) Jul 5
to golang-nuts
You can use gopsutil
Reply all
Reply to author
Forward
0 new messages