github.com/deroproject/derosuite@v2.1.6-1.0.20200307070847-0f2e589c7a2b+incompatible/cmd/dero-miner/thread_linux.go (about)

     1  package main
     2  
     3  import "runtime"
     4  import "sync/atomic"
     5  import "golang.org/x/sys/unix"
     6  
     7  var processor int32
     8  
     9  // sets thread affinity to avoid cache collision and thread migration
    10  func threadaffinity() {
    11  	var cpuset unix.CPUSet
    12  
    13  	lock_on_cpu := atomic.AddInt32(&processor, 1)
    14  	if lock_on_cpu >= int32(runtime.GOMAXPROCS(0)) { // threads are more than cpu, we do not know what to do
    15  		return
    16  	}
    17  	cpuset.Zero()
    18  	cpuset.Set(int(avoidHT(int(lock_on_cpu))))
    19  
    20  	unix.SchedSetaffinity(0, &cpuset)
    21  }
    22  
    23  func avoidHT(i int) int {
    24  	count := runtime.GOMAXPROCS(0)
    25  	if i < count/2 {
    26  		return i * 2
    27  	} else {
    28  		return (i-count/2)*2 + 1
    29  	}
    30  }