These are chat archives for gin-gonic/gin

24th
Oct 2016
Rafael Bertelli
@zfael
Oct 24 2016 18:38

Guys, I know this room is about gin, however I have a question about golang language and I couldn't found a place to ask this.
There are someone here that faced a highest cpu usage when using infinity loops in go? (using linux and command top)

Like
func main() {
for true {
time.Sleep(time.Millisecond)
}
}

Javier Provecho Fernandez
@javierprovecho
Oct 24 2016 18:44
why do you want to do that?
Rafael Bertelli
@zfael
Oct 24 2016 18:46
It is just a example, but i am using for with select/channel
Javier Provecho Fernandez
@javierprovecho
Oct 24 2016 18:53
well, time.Sleep makes a call to the scheduler for pausing the current goroutine, making it so fast may add a lot of overhead to the cpu
as you said, a better solution is to use a select with a timer
or sleep in the default clause
that depends on your app
Rafael Bertelli
@zfael
Oct 24 2016 19:05

but its normal get 99% usage of CPU just with this code?

This issue here is similar golang/go#15129
But the guy said that is a problem with top command, but i am not sure

Javier Provecho Fernandez
@javierprovecho
Oct 24 2016 19:12
let me check it locally, but I would say yes in advance
on darwin/amd64 no more than 10% on 2 cores
htop and top and monitor shows the same
however, kernel (pid 0) has a lot of cpu, so i think it depends on the arch,
Rafael Bertelli
@zfael
Oct 24 2016 19:25

Its weird, when there isnt any process running together with go program, go process use all the CPU like 99%, however when another process start, like the mongod process, go process free the cpu usage and mongod use this freed %

When both process are running, the CPU usage seems like below

  • Go Process with 75%
  • mongod process with 25%

If go was using all 99% cpu for sure, it wont be freed the % easily, Make sense that? :worried: