goroutineAffinity

package module
Version: v0.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 1, 2021 License: MIT Imports: 3 Imported by: 0

README

goroutineAffinity

inspired by CPU Affinity in Go

usage

set goroutine affinity to specified cpu core

support

  • Linux

todo

  • support MacOS
  • support Windows

How to use

package main

import (
	"fmt"
	"math/rand"
	"runtime"
	"time"

	"github.com/glennWang/goroutineAffinity"
)

func randSleep() {
	time.Sleep(time.Duration(rand.Intn(300)) * time.Millisecond)
}

func worker(id int, lock bool) {
	if lock {
		goroutineAffinity.SetAffinity(id)
	}

	for {
		fmt.Printf("worker: %d, CPU: %d\n", id, goroutineAffinity.GetAffinityCPU())
		randSleep()
	}
}

func main() {

	fmt.Println("NumCPU:", runtime.NumCPU())

	for i := 0; i < runtime.NumCPU(); i++ {
		go worker(i, true)
	}
	time.Sleep(2 * time.Second)
}

⚠️ License

Source code in goroutineAffinity is available under the MIT License.

🤝 Welcome pr

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetAffinityCPU

func GetAffinityCPU() int

func SetAffinity

func SetAffinity(cpuID int) int

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
t or T : Toggle theme light dark auto
y or Y : Canonical URL