Initial commit (code only without large binaries)
This commit is contained in:
19
EdgeNode/internal/utils/procs/id.go
Normal file
19
EdgeNode/internal/utils/procs/id.go
Normal file
@@ -0,0 +1,19 @@
|
||||
// Copyright 2024 GoEdge CDN goedge.cdn@gmail.com. All rights reserved. Official site: https://goedge.cn .
|
||||
|
||||
package procs
|
||||
|
||||
import (
|
||||
_ "unsafe"
|
||||
)
|
||||
|
||||
//go:linkname runtime_procPin runtime.procPin
|
||||
func runtime_procPin() int
|
||||
|
||||
//go:linkname runtime_procUnpin runtime.procUnpin
|
||||
func runtime_procUnpin() int
|
||||
|
||||
func Id() int {
|
||||
var pid = runtime_procPin()
|
||||
runtime_procUnpin()
|
||||
return pid
|
||||
}
|
||||
99
EdgeNode/internal/utils/procs/id_test.go
Normal file
99
EdgeNode/internal/utils/procs/id_test.go
Normal file
@@ -0,0 +1,99 @@
|
||||
// Copyright 2024 GoEdge CDN goedge.cdn@gmail.com. All rights reserved. Official site: https://goedge.cn .
|
||||
|
||||
package procs_test
|
||||
|
||||
import (
|
||||
"github.com/TeaOSLab/EdgeNode/internal/utils/procs"
|
||||
"github.com/iwind/TeaGo/lists"
|
||||
"runtime"
|
||||
"sort"
|
||||
"sync"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestId_24(t *testing.T) {
|
||||
runtime.GOMAXPROCS(24)
|
||||
|
||||
var procIds []int
|
||||
var mu = &sync.Mutex{}
|
||||
|
||||
var countProcs = runtime.GOMAXPROCS(0)
|
||||
var wg = &sync.WaitGroup{}
|
||||
wg.Add(countProcs * 4096)
|
||||
for i := 0; i < countProcs*4096; i++ {
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
|
||||
mu.Lock()
|
||||
var procId = procs.Id()
|
||||
if !lists.ContainsInt(procIds, procId) {
|
||||
procIds = append(procIds, procId)
|
||||
}
|
||||
mu.Unlock()
|
||||
}()
|
||||
}
|
||||
wg.Wait()
|
||||
|
||||
sort.Ints(procIds)
|
||||
t.Log(procIds)
|
||||
}
|
||||
|
||||
func TestId_4(t *testing.T) {
|
||||
runtime.GOMAXPROCS(4)
|
||||
|
||||
var procIds []int
|
||||
var mu = &sync.Mutex{}
|
||||
|
||||
var countProcs = runtime.GOMAXPROCS(0)
|
||||
var wg = &sync.WaitGroup{}
|
||||
wg.Add(countProcs * 4096)
|
||||
for i := 0; i < countProcs*4096; i++ {
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
|
||||
mu.Lock()
|
||||
var procId = procs.Id()
|
||||
if !lists.ContainsInt(procIds, procId) {
|
||||
procIds = append(procIds, procId)
|
||||
}
|
||||
mu.Unlock()
|
||||
}()
|
||||
}
|
||||
wg.Wait()
|
||||
|
||||
sort.Ints(procIds)
|
||||
t.Log(procIds)
|
||||
}
|
||||
|
||||
func TestId_Auto(t *testing.T) {
|
||||
var procIds []int
|
||||
var mu = &sync.Mutex{}
|
||||
|
||||
var countProcs = runtime.GOMAXPROCS(0)
|
||||
var wg = &sync.WaitGroup{}
|
||||
wg.Add(countProcs * 4096)
|
||||
for i := 0; i < countProcs*4096; i++ {
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
|
||||
mu.Lock()
|
||||
var procId = procs.Id()
|
||||
if !lists.ContainsInt(procIds, procId) {
|
||||
procIds = append(procIds, procId)
|
||||
}
|
||||
mu.Unlock()
|
||||
}()
|
||||
}
|
||||
wg.Wait()
|
||||
|
||||
sort.Ints(procIds)
|
||||
t.Log(procIds)
|
||||
}
|
||||
|
||||
func BenchmarkId(b *testing.B) {
|
||||
b.RunParallel(func(pb *testing.PB) {
|
||||
for pb.Next() {
|
||||
_ = procs.Id()
|
||||
}
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user