Skip to content

Commit

Permalink
Merge pull request #8 from sjenning/refactor-topo
Browse files Browse the repository at this point in the history
Refactor/rename topo package
  • Loading branch information
sjenning committed Jul 5, 2017
2 parents 32ef4f2 + 77cd332 commit e426775
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 87 deletions.
40 changes: 2 additions & 38 deletions pkg/kubelet/cpumanager/cpu_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import (
internalapi "k8s.io/kubernetes/pkg/kubelet/apis/cri"
runtimeapi "k8s.io/kubernetes/pkg/kubelet/apis/cri/v1alpha1"
"k8s.io/kubernetes/pkg/kubelet/cpumanager/state"
"k8s.io/kubernetes/pkg/kubelet/cpumanager/topo"
"k8s.io/kubernetes/pkg/kubelet/cpumanager/topology"
"k8s.io/kubernetes/pkg/kubelet/status"
)

Expand Down Expand Up @@ -70,7 +70,7 @@ func NewManager(policyType string, cr internalapi.RuntimeService, kletGetter kle
if err != nil {
return nil, err
}
topo, err := discoverTopology(machinInfo)
topo, err := topology.Discover(machinInfo)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -160,42 +160,6 @@ func (m *manager) State() state.Reader {
return m.state
}

func discoverTopology(machineInfo *cadvisorapi.MachineInfo) (*topo.CPUTopology, error) {

if machineInfo.NumCores == 0 {
return nil, fmt.Errorf("could not detect number of cpus")
}

CPUtopoDetails := make(map[int]topo.CPUInfo)

numCPUs := machineInfo.NumCores
htEnabled := false
numPhysicalCores := 0
for _, socket := range machineInfo.Topology {
numPhysicalCores += len(socket.Cores)
for _, core := range socket.Cores {
for _, cpu := range core.Threads {
CPUtopoDetails[cpu] = topo.CPUInfo{
CoreId: core.Id,
SocketId: socket.Id,
}
// a little bit naive
if !htEnabled && len(core.Threads) != 1 {
htEnabled = true
}
}
}
}

return &topo.CPUTopology{
NumCPUs: numCPUs,
NumSockets: len(machineInfo.Topology),
NumCores: numPhysicalCores,
HyperThreading: htEnabled,
CPUtopoDetails: CPUtopoDetails,
}, nil
}

func (m *manager) reconcileState() {
m.Lock()
defer m.Unlock()
Expand Down
6 changes: 3 additions & 3 deletions pkg/kubelet/cpumanager/policy_static.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,21 @@ import (
"github.com/golang/glog"
"k8s.io/kubernetes/pkg/api/v1"
"k8s.io/kubernetes/pkg/kubelet/cpumanager/state"
"k8s.io/kubernetes/pkg/kubelet/cpumanager/topo"
"k8s.io/kubernetes/pkg/kubelet/cpumanager/topology"
"k8s.io/kubernetes/pkg/kubelet/cpuset"
"k8s.io/kubernetes/pkg/kubelet/qos"
)

const PolicyStatic PolicyName = "static"

type staticPolicy struct {
topology *topo.CPUTopology
topology *topology.CPUTopology
}

// NewStaticPolicy returns a cupset manager policy that does not change
// CPU assignments for exclusively pinned guaranteed containers after
// the main container process starts.
func NewStaticPolicy(topology *topo.CPUTopology) Policy {
func NewStaticPolicy(topology *topology.CPUTopology) Policy {
return &staticPolicy{
topology: topology,
}
Expand Down
33 changes: 0 additions & 33 deletions pkg/kubelet/cpumanager/topo/topo.go

This file was deleted.

File renamed without changes.
75 changes: 75 additions & 0 deletions pkg/kubelet/cpumanager/topology/topology.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
Copyright 2017 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package topology

import (
"fmt"

cadvisorapi "github.com/google/cadvisor/info/v1"
)

//CPU - logical CPU, cadvisor - thread
//Core - physical CPU, cadvisor - Core
//Socket - socket, cadvisor - Node
type CPUTopology struct {
NumCPUs int
NumCores int
HyperThreading bool
NumSockets int
CPUtopoDetails map[int]CPUInfo
}

type CPUInfo struct {
SocketId int
CoreId int
}

func Discover(machineInfo *cadvisorapi.MachineInfo) (*CPUTopology, error) {

if machineInfo.NumCores == 0 {
return nil, fmt.Errorf("could not detect number of cpus")
}

CPUtopoDetails := make(map[int]CPUInfo)

numCPUs := machineInfo.NumCores
htEnabled := false
numPhysicalCores := 0
for _, socket := range machineInfo.Topology {
numPhysicalCores += len(socket.Cores)
for _, core := range socket.Cores {
for _, cpu := range core.Threads {
CPUtopoDetails[cpu] = CPUInfo{
CoreId: core.Id,
SocketId: socket.Id,
}
// a little bit naive
if !htEnabled && len(core.Threads) != 1 {
htEnabled = true
}
}
}
}

return &CPUTopology{
NumCPUs: numCPUs,
NumSockets: len(machineInfo.Topology),
NumCores: numPhysicalCores,
HyperThreading: htEnabled,
CPUtopoDetails: CPUtopoDetails,
}, nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,30 +14,29 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package cpumanager
package topology

import (
"reflect"
"testing"

cadvisorapi "github.com/google/cadvisor/info/v1"
"k8s.io/kubernetes/pkg/kubelet/cpumanager/topo"
)

func Test_discoverTopology(t *testing.T) {
func Test_Discover(t *testing.T) {

tests := []struct {
name string
args *cadvisorapi.MachineInfo
want *topo.CPUTopology
want *CPUTopology
wantErr bool
}{
{
name: "FailNumCores",
args: &cadvisorapi.MachineInfo{
NumCores: 0,
},
want: &topo.CPUTopology{},
want: &CPUTopology{},
wantErr: true,
},
{
Expand All @@ -55,12 +54,12 @@ func Test_discoverTopology(t *testing.T) {
},
},
},
want: &topo.CPUTopology{
want: &CPUTopology{
NumCPUs: 8,
NumSockets: 1,
NumCores: 4,
HyperThreading: true,
CPUtopoDetails: map[int]topo.CPUInfo{
CPUtopoDetails: map[int]CPUInfo{
0: {CoreId: 0, SocketId: 0},
1: {CoreId: 1, SocketId: 0},
2: {CoreId: 2, SocketId: 0},
Expand Down Expand Up @@ -92,12 +91,12 @@ func Test_discoverTopology(t *testing.T) {
},
},
},
want: &topo.CPUTopology{
want: &CPUTopology{
NumCPUs: 4,
NumSockets: 2,
NumCores: 4,
HyperThreading: false,
CPUtopoDetails: map[int]topo.CPUInfo{
CPUtopoDetails: map[int]CPUInfo{
0: {CoreId: 0, SocketId: 0},
1: {CoreId: 1, SocketId: 1},
2: {CoreId: 2, SocketId: 0},
Expand All @@ -109,17 +108,17 @@ func Test_discoverTopology(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := discoverTopology(tt.args)
got, err := Discover(tt.args)
if err != nil {
if tt.wantErr {
t.Logf("discoverTopology() expected error = %v", err)
t.Logf("Discover() expected error = %v", err)
} else {
t.Errorf("discoverTopology() error = %v, wantErr %v", err, tt.wantErr)
t.Errorf("Discover() error = %v, wantErr %v", err, tt.wantErr)
}
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("discoverTopology() = %v, want %v", got, tt.want)
t.Errorf("Discover() = %v, want %v", got, tt.want)
}
})
}
Expand Down

0 comments on commit e426775

Please sign in to comment.