github.com/lrita/numa@v1.0.2/getcpu_linux_amd64_test.go (about)

     1  package numa
     2  
     3  import (
     4  	"runtime"
     5  	"sync"
     6  	"testing"
     7  
     8  	"github.com/stretchr/testify/require"
     9  )
    10  
    11  func TestGetNodeAndCPU2(t *testing.T) {
    12  	if !Available() || !fastway {
    13  		t.Skip("not available or fastwway")
    14  	}
    15  
    16  	var (
    17  		nodem  = NewBitmask(NodePossibleCount())
    18  		mu     sync.Mutex
    19  		wg     sync.WaitGroup
    20  		assert = require.New(t)
    21  	)
    22  
    23  	fastway = false
    24  	defer func() { fastway = true }()
    25  
    26  	cpum := make([]Bitmask, NodePossibleCount())
    27  	for i := 0; i < len(cpum); i++ {
    28  		cpum[i] = NewBitmask(CPUPossibleCount())
    29  	}
    30  	for i := 0; i < CPUCount(); i++ {
    31  		wg.Add(1)
    32  		go func() {
    33  			defer wg.Done()
    34  			for i := 0; i < 100; i++ {
    35  				cpu, node := GetCPUAndNode()
    36  				mu.Lock()
    37  				cpum[node].Set(cpu, true)
    38  				nodem.Set(node, true)
    39  				mu.Unlock()
    40  				runtime.Gosched()
    41  			}
    42  		}()
    43  	}
    44  	wg.Wait()
    45  
    46  	nmask := NodeMask()
    47  	for i := 0; i < nodem.Len(); i++ {
    48  		if !nodem.Get(i) {
    49  			continue
    50  		}
    51  		assert.True(nmask.Get(i), "node %d", i)
    52  		cpumask, err := NodeToCPUMask(i)
    53  		assert.NoError(err)
    54  		cmask := cpum[i]
    55  		for j := 0; j < cmask.Len(); j++ {
    56  			if !cmask.Get(j) {
    57  				continue
    58  			}
    59  			assert.True(cpumask.Get(j), "cpu %d @ node %d", j, i)
    60  		}
    61  	}
    62  }