Skip to content

device/telemetry: optimize TWAMP using CPU affinity/pinning #790

@snormore

Description

@snormore

An optimization we could make is to configure CPU affinity/pinning for the TWAMP sender/receiver.

This would require the TWAMP sender/reflector code in Go to be locked to the OS thread, which has some caveats/footguns, but it can work if careful. Alternatively, it could be in a separate process entirely.

Example

package main

import (
	"fmt"
	"runtime"

	"golang.org/x/sys/unix"
)

func pinToCPU(cpu int) error {
	runtime.LockOSThread()

	var mask unix.CPUSet
	mask.Zero()
	mask.Set(cpu)

	err := unix.SchedSetaffinity(0, &mask) // 0 = current thread
	if err != nil {
		return fmt.Errorf("failed to set CPU affinity: %w", err)
	}
	return nil
}

func main() {
	go func() {
		if err := pinToCPU(2); err != nil {
			panic(err)
		}
		// Do work pinned to CPU 2
		select {}
	}()

	select {}
}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions