0

Below UNIX command:

$ lscpu
Architecture:        x86_64
CPU op-mode(s):      32-bit, 64-bit
Byte Order:          Little Endian
CPU(s):              4
On-line CPU(s) list: 0-3
Thread(s) per core:  2
Core(s) per socket:  2
Socket(s):           1

shows,

  CPU(s):              4
  Thread(s) per core:  2

which is 4 X 2 = 8 logical processors. Correct me.


Below is another Linux command:

$ cat /proc/cpuinfo
processor   : 0
....
cpu cores   : 2
.....
processor   : 1
.....
cpu cores   : 2
.....
processor   : 2
.....
cpu cores   : 2
.....
processor   : 3
.....
cpu cores   : 2
.....
$ 

But the below program shows only 4 logical processors:

package main

import (
    "fmt"
    "runtime"
)

func main() {
    fmt.Println(runtime.GOMAXPROCS(0))   // gives 4
    fmt.Println(runtime.NumCPU())        // gives 4   
}

Output:

$ go install github.com/myhub/cs61a
$ bin/cs61a 
4
4
code$ 

More details:

$ go version
go version go1.14.1 linux/amd64
$ uname -a
Linux mohet01-ubuntu 4.15.0-99-generic #100-Ubuntu SMP Wed Apr 22 20:32:56 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux

Documentation says,

NumCPU returns the number of logical CPUs usable by the current process.


My understanding is,

Go scheduler creates OS threads(M) which will be equivalent to number of logical processors.

Why runtime api does not give value as 8?

overexchange
  • 15,768
  • 30
  • 152
  • 347
  • https://stackoverflow.com/a/13245047/3317808 – overexchange May 06 '20 at 19:42
  • What version of Go is this? It looks like this could potentially be a defect in the runtime. – Adrian May 06 '20 at 19:45
  • @Adrian `1.14` I edited query with `go version` output. – overexchange May 06 '20 at 19:47
  • 1
    The line "CPU(s): 4" in the output of lscpu is the number of logical CPUs. You can also see this in the line "On-line CPU(s) list: 0-3" which says that the logical cores 0, 1, 2 and 3 are currently online (currently available). You can also try running the following in a shell to get the number of physical cores: `grep 'cpu cores' /proc/cpuinfo | uniq` – nussjustin May 06 '20 at 19:57
  • @nussjustin Query edited with required command output – overexchange May 06 '20 at 20:03
  • To my understanding based on what you're showing it should be reporting 8 logical cores on 4 physical cores. – Adrian May 06 '20 at 20:14
  • @Adrian Further, OS threads(`M`) should be created equivalent to number of logical cores. Is that correct? – overexchange May 06 '20 at 20:49
  • What @nussjustin wrote. `lscpu` also outputs the Sockets, and Cores per socket. Multiply those to get the total number of cores. Multiply that with Threads per core. That's the number of logical processors, which is `CPU(s)`. – icza May 06 '20 at 21:14
  • @icza Query edited... `sockets:1` & `cores per socket :2` – overexchange May 06 '20 at 22:46
  • You have 1 socket with 2 cores, and 2 threads per core, which is 4 logical processors. I don't see how you get to 8. – JimB May 07 '20 at 01:20

1 Answers1

2

According to all of your listings above, you have:

  • two cores per socket
  • and one socket

which means you have only two CPU cores. However, you also have:

  • two threads per core

which means your two CPUs can run up to four threads simultaneously (with some potential drawbacks, but at least in most cases this should offer significantly more computing power than using two threads total).

Since that's the actual hardware limit, the fact that Go computes the number of threads to use as 4 seems correct.

(I think the reason you are counting to eight is that you are assuming that each of the "cpus" that Linux reports supports two threads. That is not the case: there are only two physical cores, but each supports two threads, so Linux reports this as four "cpus".)

torek
  • 448,244
  • 59
  • 642
  • 775