Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

app: add sample application #15

Merged
merged 1 commit into from
Oct 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions samples/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
FROM golang:1.20 AS builder
WORKDIR /go/src/github.com/opeshift/cluster-node-tuning-operator/samples/app
COPY . .
RUN mkdir build || true && go build -o build/sample-app ./app

FROM registry.access.redhat.com/ubi9/ubi
COPY --from=builder /go/src/github.com/opeshift/cluster-node-tuning-operator/samples/app/build/sample-app /bin/sample-app
RUN dnf install -y https://dl.fedoraproject.org/pub/epel/epel-release-latest-9.noarch.rpm && \
dnf install -y htop procps && \
dnf clean all
ENTRYPOINT ["/bin/sample-app"]
119 changes: 119 additions & 0 deletions samples/app/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
package main

import (
"bytes"
"errors"
"os"
"runtime"
"strconv"
"strings"
"sync"
"syscall"
"time"

"golang.org/x/sys/unix"

"k8s.io/klog/v2"
"k8s.io/kubernetes/pkg/kubelet/cm/cpuset"

"github.com/openshift-kni/mixed-cpu-node-plugin/pkg/deviceplugin"
)

func main() {
klog.Infof("reading /sys/fs/cgroup/cpuset/cpuset.cpus to retrieve the accessible cpus")
out, err := os.ReadFile("/sys/fs/cgroup/cpuset/cpuset.cpus")
if err != nil {
klog.Fatal(err)
}
cgroupCPUs := strings.TrimSuffix(string(out), "\n")
completeSet, err := cpuset.Parse(cgroupCPUs)
if err != nil {
klog.Fatalf("failed to parse cpuset %q; %v", cgroupCPUs, err)
}
klog.Infof("/sys/fs/cgroup/cpuset/cpuset.cpus content: %s", cgroupCPUs)

klog.Infof("reading environment variable:%q to retrieve the shared cpus", deviceplugin.EnvVarName)
cpus, ok := os.LookupEnv(deviceplugin.EnvVarName)
if !ok {
klog.Fatalf("%q environment variable not set", deviceplugin.EnvVarName)
}
sharedSet, err := cpuset.Parse(cpus)
if err != nil {
klog.Fatalf("failed to parse cpuset %q; %v", cpus, err)
}
if sharedSet.IsEmpty() {
klog.Warning("no shared cpus are configured for this process")
}
klog.Infof("%s=%s", deviceplugin.EnvVarName, cpus)
// here we extract the shared cpus from the complete set and find the isolated set
isolatedSet := completeSet.Difference(sharedSet)
klog.Infof("container finalized cpuset layout:\ncomplete-set=%q\nisolated-set=%q\nshared-set=%q", completeSet.String(), isolatedSet.String(), sharedSet.String())
var wg sync.WaitGroup
spawnLightWeightTasks(&sharedSet, &wg, 2)
spawnHeavyWeightTasks(&isolatedSet, &wg, 2)
wg.Wait()
}

func spawnLightWeightTasks(set *cpuset.CPUSet, wg *sync.WaitGroup, tasks int) {
for i := 0; i < tasks; i++ {
spawnTask(set, wg, "light weight task")
}
}

func spawnHeavyWeightTasks(set *cpuset.CPUSet, wg *sync.WaitGroup, tasks int) {
for i := 0; i < tasks; i++ {
spawnTask(set, wg, "heavy weight task")
}
}

func spawnTask(set *cpuset.CPUSet, wg *sync.WaitGroup, desc string) {
wg.Add(1)
go func() {
defer wg.Done()
runtime.LockOSThread()
unixSet := k8sCPUstoUnixCPUs(set)
tid := syscall.Gettid()
id, err := goid()
if err != nil {
klog.Fatal(err)
}
if err := unix.SchedSetaffinity(0, unixSet); err != nil {
klog.Fatal(err)
}
for {
klog.Infof("%s: thread id %d => goroutine id: %d set affinity to cores: %q", desc, tid, id, set.String())
time.Sleep(60 * time.Second)
}
}()
}

func k8sCPUstoUnixCPUs(set *cpuset.CPUSet) *unix.CPUSet {
unixSet := &unix.CPUSet{}
for _, i := range set.List() {
unixSet.Set(i)
}
return unixSet
}

var (
goroutinePrefix = []byte("goroutine ")
errBadStack = errors.New("invalid runtime.Stack output")
)

// This is terrible, slow, and should never be used in production.
func goid() (int, error) {
buf := make([]byte, 32)
n := runtime.Stack(buf, false)
buf = buf[:n]
// goroutine 1 [running]: ...
buf, ok := bytes.CutPrefix(buf, goroutinePrefix)
if !ok {
return 0, errBadStack
}

i := bytes.IndexByte(buf, ' ')
if i < 0 {
return 0, errBadStack
}
return strconv.Atoi(string(buf[:i]))
}
63 changes: 63 additions & 0 deletions samples/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
module app

go 1.20

replace (
k8s.io/api => k8s.io/api v0.27.1
k8s.io/apiserver => k8s.io/apiserver v0.27.1
k8s.io/cli-runtime => k8s.io/cli-runtime v0.27.1
k8s.io/client-go => k8s.io/client-go v0.27.1
k8s.io/cloud-provider => k8s.io/cloud-provider v0.27.1
k8s.io/cluster-bootstrap => k8s.io/cluster-bootstrap v0.27.1
k8s.io/code-generator => k8s.io/code-generator v0.27.1
k8s.io/component-base => k8s.io/component-base v0.27.1
k8s.io/component-helpers => k8s.io/component-helpers v0.27.1
k8s.io/controller-manager => k8s.io/controller-manager v0.27.1
k8s.io/csi-translation-lib => k8s.io/csi-translation-lib v0.27.1
k8s.io/dynamic-resource-allocation => k8s.io/dynamic-resource-allocation v0.27.1
k8s.io/kms => k8s.io/kms v0.27.1
k8s.io/kube-aggregator => k8s.io/kube-aggregator v0.27.1
k8s.io/kube-controller-manager => k8s.io/kube-controller-manager v0.27.1
k8s.io/kube-proxy => k8s.io/kube-proxy v0.27.1
k8s.io/kube-scheduler => k8s.io/kube-scheduler v0.27.1
k8s.io/kubectl => k8s.io/kubectl v0.27.1
k8s.io/kubelet => k8s.io/kubelet v0.27.1
k8s.io/kubernetes => k8s.io/kubernetes v1.27.1
k8s.io/legacy-cloud-providers => k8s.io/legacy-cloud-providers v0.27.1
k8s.io/metrics => k8s.io/metrics v0.27.1
k8s.io/mount-utils => k8s.io/mount-utils v0.27.1
k8s.io/pod-security-admission => k8s.io/pod-security-admission v0.27.1
k8s.io/sample-apiserver => k8s.io/sample-apiserver v0.27.1
)

require (
github.com/containerd/cgroups v1.1.0
github.com/openshift-kni/mixed-cpu-node-plugin v0.0.0-20230814112512-f1a527d0a451
k8s.io/klog/v2 v2.100.1
k8s.io/kubernetes v1.25.4
)

require (
github.com/containerd/nri v0.2.0 // indirect
github.com/containerd/ttrpc v1.1.1-0.20220420014843-944ef4a40df3 // indirect
github.com/containers/podman/v4 v4.4.2 // indirect
github.com/coreos/go-systemd/v22 v22.5.0 // indirect
github.com/docker/go-units v0.5.0 // indirect
github.com/fsnotify/fsnotify v1.6.0 // indirect
github.com/go-logr/logr v1.2.3 // indirect
github.com/godbus/dbus/v5 v5.1.1-0.20221029134443-4b691ce883d5 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/glog v1.0.0 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/kubevirt/device-plugin-manager v1.19.4 // indirect
github.com/opencontainers/runtime-spec v1.0.3-0.20220909204839-494a5a6aca78 // indirect
github.com/sirupsen/logrus v1.9.0 // indirect
golang.org/x/net v0.8.0 // indirect
golang.org/x/sys v0.6.0 // indirect
golang.org/x/text v0.8.0 // indirect
google.golang.org/genproto v0.0.0-20221227171554-f9683d7f8bef // indirect
google.golang.org/grpc v1.51.0 // indirect
google.golang.org/protobuf v1.28.1 // indirect
k8s.io/cri-api v0.25.3 // indirect
k8s.io/kubelet v0.26.0 // indirect
)
Loading
Loading