Documentation
¶
Overview ¶
Package workgroup provides zero-cgo bindings for macOS os_workgroup parallel workloads and pthread QoS class pinning on Apple Silicon.
On Apple Silicon, macOS schedules threads onto performance (P) or efficiency (E) cores based on their requested QoSClass and Group membership. Because Go multiplexes goroutines onto OS threads, callers that use SetThreadQoS or Group.Join directly must pin the goroutine with runtime.LockOSThread and undo the change before calling runtime.UnlockOSThread. RunPinned handles locking, QoS set/restore, workgroup join/leave, and panic-safe cleanup automatically.
The zero value of Group is not usable; create one with New.
Basic usage:
g, err := workgroup.New("decode")
if err != nil {
log.Fatal(err)
}
defer g.Close()
err = workgroup.RunPinned(ctx, workgroup.UserInteractive, g, func(ctx context.Context) error {
// Runs on a locked OS thread at UserInteractive QoS in g.
return nil
})
Example ¶
package main
import (
"context"
"fmt"
"log"
"github.com/tmc/apple/x/workgroup"
)
func main() {
g, err := workgroup.New("example.decode")
if err != nil {
log.Fatal(err)
}
defer g.Close()
err = workgroup.RunPinned(context.Background(), workgroup.UserInteractive, g, func(ctx context.Context) error {
qos, _, err := workgroup.CurrentThreadQoS()
if err != nil {
return err
}
fmt.Println(qos)
return nil
})
if err != nil {
log.Fatal(err)
}
}
Output: UserInteractive
Index ¶
Examples ¶
Constants ¶
const MinRelativePriority = -15
MinRelativePriority is the lowest valid relative priority offset within a QoS class (QOS_MIN_RELATIVE_PRIORITY from <sys/qos.h>).
Variables ¶
var ErrClosed = errors.New("workgroup: group closed")
ErrClosed is returned when an operation is attempted on a closed Group.
var ErrUnsupported = errors.New("workgroup: unsupported on this system")
ErrUnsupported reports that the required libSystem symbols are unavailable on this macOS version.
Functions ¶
func RunPinned ¶
func RunPinned(ctx context.Context, qos QoSClass, group *Group, fn func(context.Context) error) error
RunPinned executes fn on a locked OS thread (runtime.LockOSThread) with the requested QoS class and optional Group membership.
If qos is Unspecified, the thread's QoS class is left unchanged. If group is nil, no workgroup is joined. Before returning — including if fn panics — RunPinned leaves the workgroup, restores the thread's prior QoS class, and calls runtime.UnlockOSThread.
If ctx is already canceled, RunPinned returns ctx.Err() without locking the OS thread.
Example ¶
package main
import (
"context"
"fmt"
"log"
"github.com/tmc/apple/x/workgroup"
)
func main() {
err := workgroup.RunPinned(context.Background(), workgroup.Background, nil, func(ctx context.Context) error {
qos, _, err := workgroup.CurrentThreadQoS()
if err != nil {
return err
}
fmt.Println(qos)
return nil
})
if err != nil {
log.Fatal(err)
}
}
Output: Background
func SetThreadQoS ¶
SetThreadQoS sets the requested QoS class and relative priority of the calling OS thread via pthread_set_qos_class_self_np.
relPriority must be in [MinRelativePriority, 0]. Callers should hold runtime.LockOSThread for the duration that the QoS class is needed.
Example ¶
package main
import (
"fmt"
"log"
"runtime"
"github.com/tmc/apple/x/workgroup"
)
func main() {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
prev, rel, err := workgroup.CurrentThreadQoS()
if err != nil {
log.Fatal(err)
}
if prev == workgroup.Unspecified {
prev, rel = workgroup.Default, 0
}
defer workgroup.SetThreadQoS(prev, rel)
if err := workgroup.SetThreadQoS(workgroup.UserInitiated, 0); err != nil {
log.Fatal(err)
}
got, _, err := workgroup.CurrentThreadQoS()
if err != nil {
log.Fatal(err)
}
fmt.Println(got)
}
Output: UserInitiated
Types ¶
type Group ¶
type Group struct {
// contains filtered or unexported fields
}
Group wraps a macOS parallel os_workgroup_t. Threads that join the same Group signal to the kernel scheduler that they are collaborating on a latency-sensitive parallel workload.
Group is safe for concurrent use by multiple goroutines. The zero value is not usable; create one with New.
func New ¶
New creates a new parallel os_workgroup with the given label. Call Group.Close when the group is no longer needed.
func (*Group) Cancel ¶
func (g *Group) Cancel()
Cancel marks the workgroup as canceled. Already-joined threads may still call Group.Leave, but subsequent Group.Join calls fail. Cancel is idempotent.
func (*Group) Close ¶
Close cancels and releases the underlying os_workgroup_t. Close is idempotent. Threads already joined via Group.Join may still safely call Group.Leave after Close.
func (*Group) Join ¶
Join adds the calling OS thread to the workgroup and returns a JoinToken that must be released with Group.Leave or JoinToken.Leave on the same OS thread. Callers must hold runtime.LockOSThread across Join and Leave.
func (*Group) Leave ¶
Leave removes the calling OS thread from the workgroup using a token previously returned by Group.Join. Leave is idempotent for a given token.
func (*Group) MaxParallelThreads ¶
MaxParallelThreads returns the system's recommended maximum number of parallel threads for this workgroup, or 0 if the group is closed or the query is unsupported.
type JoinToken ¶
type JoinToken struct {
// contains filtered or unexported fields
}
JoinToken represents an OS thread's active membership in a Group. Pass it to Group.Leave (or call JoinToken.Leave) on the same OS thread that called Group.Join, in reverse order of nested joins.
type QoSClass ¶
type QoSClass uint32
QoSClass represents a Darwin thread Quality of Service class (qos_class_t).
const ( UserInteractive QoSClass = 0x21 // QOS_CLASS_USER_INTERACTIVE (P-cores, highest priority) UserInitiated QoSClass = 0x19 // QOS_CLASS_USER_INITIATED (P-cores) Default QoSClass = 0x15 // QOS_CLASS_DEFAULT Utility QoSClass = 0x11 // QOS_CLASS_UTILITY (energy efficient) Background QoSClass = 0x09 // QOS_CLASS_BACKGROUND (E-cores on Apple Silicon) Unspecified QoSClass = 0x00 // QOS_CLASS_UNSPECIFIED )
Darwin QoS classes from <sys/qos.h>.
func CurrentThreadQoS ¶
CurrentThreadQoS returns the requested QoS class and relative priority of the calling OS thread via pthread_get_qos_class_np.