Skip to content

Commit bf20b75

Browse files
authored
Merge pull request #1006 from dcantah/cpugroup-onstart
Add support for assigning cpu group on creation
2 parents 8656c9b + 377e39a commit bf20b75

File tree

9 files changed

+45
-36
lines changed

9 files changed

+45
-36
lines changed

internal/cpugroup/cpugroup.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ package cpugroup
33
import (
44
"context"
55
"encoding/json"
6-
"errors"
76
"fmt"
87
"strings"
98

109
"github.com/Microsoft/hcsshim/internal/hcs"
1110
hcsschema "github.com/Microsoft/hcsshim/internal/hcs/schema2"
11+
"github.com/pkg/errors"
1212
)
1313

1414
const NullGroupID = "00000000-0000-0000-0000-000000000000"
@@ -48,7 +48,7 @@ func Create(ctx context.Context, id string, logicalProcessors []uint32) error {
4848
LogicalProcessorCount: uint32(len(logicalProcessors)),
4949
}
5050
if err := modifyCPUGroupRequest(ctx, operation, details); err != nil {
51-
return fmt.Errorf("failed to make cpugroups CreateGroup request for details %+v with: %s", details, err)
51+
return errors.Wrapf(err, "failed to make cpugroups CreateGroup request for details %+v", details)
5252
}
5353
return nil
5454
}
@@ -65,7 +65,7 @@ func getCPUGroupConfig(ctx context.Context, id string) (*hcsschema.CpuGroupConfi
6565
}
6666
groupConfigs := &hcsschema.CpuGroupConfigurations{}
6767
if err := json.Unmarshal(cpuGroupsPresent.Properties[0], groupConfigs); err != nil {
68-
return nil, fmt.Errorf("failed to unmarshal host cpugroups: %v", err)
68+
return nil, errors.Wrap(err, "failed to unmarshal host cpugroups")
6969
}
7070

7171
for _, c := range groupConfigs.CpuGroups {

internal/cpugroup/cpugroup_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,17 @@ func TestCPUGroupCreateWithIDAndDelete(t *testing.T) {
1414
lps := []uint32{0, 1}
1515
ctx, cancel := context.WithCancel(context.Background())
1616
defer cancel()
17+
1718
id, err := guid.NewV4()
1819
if err != nil {
1920
t.Fatalf("failed to create cpugroup guid with: %v", err)
2021
}
22+
2123
err = Create(ctx, id.String(), lps)
2224
if err != nil {
2325
t.Fatalf("failed to create cpugroup %s with: %v", id.String(), err)
2426
}
27+
2528
defer func() {
2629
if err := Delete(ctx, id.String()); err != nil {
2730
t.Fatalf("failed to delete cpugroup %s with: %v", id.String(), err)

internal/hcs/schema2/processor_2.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*
44
* No description provided (generated by Swagger Codegen https://github.com/swagger-api/swagger-codegen)
55
*
6-
* API version: 2.1
6+
* API version: 2.5
77
* Generated by: Swagger Codegen (https://github.com/swagger-api/swagger-codegen.git)
88
*/
99

@@ -17,4 +17,7 @@ type Processor2 struct {
1717
Weight int32 `json:"Weight,omitempty"`
1818

1919
ExposeVirtualizationExtensions bool `json:"ExposeVirtualizationExtensions,omitempty"`
20+
21+
// An optional object that configures the CPU Group to which a Virtual Machine is going to bind to.
22+
CpuGroup *CpuGroup `json:"CpuGroup,omitempty"`
2023
}

internal/uvm/cpugroups.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,15 @@ import (
1010
hcsschema "github.com/Microsoft/hcsshim/internal/hcs/schema2"
1111
)
1212

13+
// Build that assigning a cpu group on creation of a vm is supported
14+
const cpuGroupCreateBuild = 20124
15+
16+
var errCPUGroupCreateNotSupported = fmt.Errorf("cpu group assignment on create requires a build of %d or higher", cpuGroupCreateBuild)
17+
1318
// ReleaseCPUGroup unsets the cpugroup from the VM
1419
func (uvm *UtilityVM) ReleaseCPUGroup(ctx context.Context) error {
15-
groupID := uvm.cpuGroupID
16-
if groupID == "" {
17-
// not set, don't try to do anything
18-
return nil
19-
}
2020
if err := uvm.unsetCPUGroup(ctx); err != nil {
21-
return fmt.Errorf("failed to remove VM %s from cpugroup %s", uvm.ID(), groupID)
21+
return fmt.Errorf("failed to remove VM %s from cpugroup", uvm.ID())
2222
}
2323
return nil
2424
}
@@ -42,7 +42,6 @@ func (uvm *UtilityVM) setCPUGroup(ctx context.Context, id string) error {
4242
if err := uvm.modify(ctx, req); err != nil {
4343
return err
4444
}
45-
uvm.cpuGroupID = id
4645
return nil
4746
}
4847

internal/uvm/create.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -244,9 +244,6 @@ func (uvm *UtilityVM) Close() (err error) {
244244
windows.Close(uvm.vmmemProcess)
245245

246246
if uvm.hcsSystem != nil {
247-
if err := uvm.ReleaseCPUGroup(ctx); err != nil {
248-
log.G(ctx).WithError(err).Warn("failed to release VM resource")
249-
}
250247
_ = uvm.hcsSystem.Terminate(ctx)
251248
_ = uvm.Wait()
252249
}

internal/uvm/create_lcow.go

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,6 @@ func CreateLCOW(ctx context.Context, opts *OptionsLCOW) (_ *UtilityVM, err error
169169
vpciDevices: make(map[string]*VPCIDevice),
170170
physicallyBacked: !opts.AllowOvercommit,
171171
devicesPhysicallyBacked: opts.FullyPhysicallyBacked,
172-
cpuGroupID: opts.CPUGroupID,
173172
createOpts: opts,
174173
}
175174

@@ -204,6 +203,19 @@ func CreateLCOW(ctx context.Context, opts *OptionsLCOW) (_ *UtilityVM, err error
204203
// Align the requested memory size.
205204
memorySizeInMB := uvm.normalizeMemorySize(ctx, opts.MemorySizeInMB)
206205

206+
processor := &hcsschema.Processor2{
207+
Count: uvm.processorCount,
208+
Limit: opts.ProcessorLimit,
209+
Weight: opts.ProcessorWeight,
210+
}
211+
// We can set a cpu group for the VM at creation time in recent builds.
212+
if opts.CPUGroupID != "" {
213+
if osversion.Build() < cpuGroupCreateBuild {
214+
return nil, errCPUGroupCreateNotSupported
215+
}
216+
processor.CpuGroup = &hcsschema.CpuGroup{Id: opts.CPUGroupID}
217+
}
218+
207219
doc := &hcsschema.ComputeSystem{
208220
Owner: uvm.owner,
209221
SchemaVersion: schemaversion.SchemaV21(),
@@ -221,11 +233,7 @@ func CreateLCOW(ctx context.Context, opts *OptionsLCOW) (_ *UtilityVM, err error
221233
HighMMIOBaseInMB: opts.HighMMIOBaseInMB,
222234
HighMMIOGapInMB: opts.HighMMIOGapInMB,
223235
},
224-
Processor: &hcsschema.Processor2{
225-
Count: uvm.processorCount,
226-
Limit: opts.ProcessorLimit,
227-
Weight: opts.ProcessorWeight,
228-
},
236+
Processor: processor,
229237
},
230238
Devices: &hcsschema.Devices{
231239
HvSocket: &hcsschema.HvSocket2{

internal/uvm/create_wcow.go

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"github.com/Microsoft/hcsshim/internal/uvmfolder"
2020
"github.com/Microsoft/hcsshim/internal/wclayer"
2121
"github.com/Microsoft/hcsshim/internal/wcow"
22+
"github.com/Microsoft/hcsshim/osversion"
2223
"github.com/containerd/ttrpc"
2324
"github.com/pkg/errors"
2425
"go.opencensus.io/trace"
@@ -122,6 +123,19 @@ func prepareConfigDoc(ctx context.Context, uvm *UtilityVM, opts *OptionsWCOW, uv
122123
}
123124
}
124125

126+
processor := &hcsschema.Processor2{
127+
Count: uvm.processorCount,
128+
Limit: opts.ProcessorLimit,
129+
Weight: opts.ProcessorWeight,
130+
}
131+
// We can set a cpu group for the VM at creation time in recent builds.
132+
if opts.CPUGroupID != "" {
133+
if osversion.Build() < cpuGroupCreateBuild {
134+
return nil, errCPUGroupCreateNotSupported
135+
}
136+
processor.CpuGroup = &hcsschema.CpuGroup{Id: opts.CPUGroupID}
137+
}
138+
125139
doc := &hcsschema.ComputeSystem{
126140
Owner: uvm.owner,
127141
SchemaVersion: schemaversion.SchemaV21(),
@@ -148,11 +162,7 @@ func prepareConfigDoc(ctx context.Context, uvm *UtilityVM, opts *OptionsWCOW, uv
148162
HighMMIOBaseInMB: opts.HighMMIOBaseInMB,
149163
HighMMIOGapInMB: opts.HighMMIOGapInMB,
150164
},
151-
Processor: &hcsschema.Processor2{
152-
Count: uvm.processorCount,
153-
Limit: opts.ProcessorLimit,
154-
Weight: opts.ProcessorWeight,
155-
},
165+
Processor: processor,
156166
},
157167
Devices: &hcsschema.Devices{
158168
HvSocket: &hcsschema.HvSocket2{
@@ -215,7 +225,6 @@ func CreateWCOW(ctx context.Context, opts *OptionsWCOW) (_ *UtilityVM, err error
215225
vpciDevices: make(map[string]*VPCIDevice),
216226
physicallyBacked: !opts.AllowOvercommit,
217227
devicesPhysicallyBacked: opts.FullyPhysicallyBacked,
218-
cpuGroupID: opts.CPUGroupID,
219228
createOpts: *opts,
220229
}
221230

internal/uvm/start.go

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -205,13 +205,6 @@ func (uvm *UtilityVM) Start(ctx context.Context) (err error) {
205205
}
206206
}()
207207

208-
// assign the VM to the cpugroup specified, if any
209-
if uvm.cpuGroupID != "" {
210-
if err := uvm.SetCPUGroup(ctx, uvm.cpuGroupID); err != nil {
211-
return err
212-
}
213-
}
214-
215208
// Start waiting on the utility VM.
216209
uvm.exitCh = make(chan struct{})
217210
go func() {

internal/uvm/types.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,9 +115,6 @@ type UtilityVM struct {
115115
// Access to this variable should be done atomically.
116116
mountCounter uint64
117117

118-
// cpuGroupID is the ID of the cpugroup on the host that this UVM is assigned to
119-
cpuGroupID string
120-
121118
// specifies if this UVM is created to be saved as a template
122119
IsTemplate bool
123120

0 commit comments

Comments
 (0)