-
Notifications
You must be signed in to change notification settings - Fork 248
/
types.go
107 lines (96 loc) · 2.91 KB
/
types.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/*
* Tencent is pleased to support the open source community by making Blueking Container Service available.
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
* Licensed under the MIT License (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
* http://opensource.org/licenses/MIT
* 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 types xxx
package types
import (
"fmt"
"sync"
"time"
"github.com/Tencent/bk-bcs/bcs-common/common/blog"
)
// CpusetNode struct for cpuset node
type CpusetNode struct {
sync.RWMutex
// cpuset node id
// show available: 2 nodes (0-1)
Id string
// the node include cpusets
// show node 0 cpus: 0 1 2 3 4 5 6 7 8 9 10 11 24 25 26 27 28 29 30 31 32 33 34 35
Cpuset []string
// allocated cpuset of container
// the cpuset belongs to only one container
AllocatedCpuset []string
AllocatedCpusetTime map[string]time.Time
}
// Capacity cpuset capacity
func (c *CpusetNode) Capacity() int {
return len(c.Cpuset) - len(c.AllocatedCpuset)
}
// AllocateCpuset allocate cpuset
func (c *CpusetNode) AllocateCpuset(number int) ([]string, error) {
c.Lock()
defer c.Unlock()
if c.Capacity() < number {
return nil, fmt.Errorf("Cpuset node %s Capacity %d, then can't allocate %d cpuset", c.Id, c.Capacity(), number)
}
cpuset := make([]string, 0, number)
for _, o := range c.Cpuset {
allocated := false
for _, set := range c.AllocatedCpuset {
if o == set {
allocated = true
break
}
}
if allocated {
continue
}
cpuset = append(cpuset, o)
c.AllocatedCpuset = append(c.AllocatedCpuset, o)
c.AllocatedCpusetTime[o] = time.Now()
if len(cpuset) == number {
break
}
}
blog.Infof("node %s allocate cpuset(%v), and AllocatedCpuset(%v) AllCpuset(%v)",
c.Id, cpuset, c.AllocatedCpuset, c.Cpuset)
return cpuset, nil
}
// ReleaseCpuset release cpuset
func (c *CpusetNode) ReleaseCpuset(cpuset []string) {
c.Lock()
defer c.Unlock()
c.ReleaseCpusetWithoutLock(cpuset)
}
// ReleaseCpusetWithoutLock release cpuset without lock
func (c *CpusetNode) ReleaseCpusetWithoutLock(cpuset []string) {
// just keep the element which is in allocatedCpuset but is not in cpuset
allocated := make([]string, 0)
for _, o := range c.AllocatedCpuset {
release := false
for _, r := range cpuset {
if o == r {
release = true
break
}
}
if !release {
allocated = append(allocated, o)
} else {
delete(c.AllocatedCpusetTime, o)
}
}
c.AllocatedCpuset = allocated
blog.Infof("node %s release cpuset(%v), and AllocatedCpuset(%v) AllCpuset(%v)",
c.Id, cpuset, c.AllocatedCpuset, c.Cpuset)
}