...
1
2
3 package cpugroup
4
5 import (
6 "context"
7 "encoding/json"
8 "fmt"
9 "strings"
10
11 "github.com/Microsoft/hcsshim/internal/hcs"
12 hcsschema "github.com/Microsoft/hcsshim/internal/hcs/schema2"
13 "github.com/pkg/errors"
14 )
15
16 const NullGroupID = "00000000-0000-0000-0000-000000000000"
17
18
19 var ErrHVStatusInvalidCPUGroupState = errors.New("The hypervisor could not perform the operation because the CPU group is entering or in an invalid state.")
20
21
22 func Delete(ctx context.Context, id string) error {
23 operation := hcsschema.DeleteGroup
24 details := hcsschema.DeleteGroupOperation{
25 GroupId: id,
26 }
27
28 return modifyCPUGroupRequest(ctx, operation, details)
29 }
30
31
32 func modifyCPUGroupRequest(ctx context.Context, operation hcsschema.CPUGroupOperation, details interface{}) error {
33 req := hcsschema.ModificationRequest{
34 PropertyType: hcsschema.PTCPUGroup,
35 Settings: &hcsschema.HostProcessorModificationRequest{
36 Operation: operation,
37 OperationDetails: details,
38 },
39 }
40
41 return hcs.ModifyServiceSettings(ctx, req)
42 }
43
44
45 func Create(ctx context.Context, id string, logicalProcessors []uint32) error {
46 operation := hcsschema.CreateGroup
47 details := &hcsschema.CreateGroupOperation{
48 GroupId: strings.ToLower(id),
49 LogicalProcessors: logicalProcessors,
50 LogicalProcessorCount: uint32(len(logicalProcessors)),
51 }
52 if err := modifyCPUGroupRequest(ctx, operation, details); err != nil {
53 return errors.Wrapf(err, "failed to make cpugroups CreateGroup request for details %+v", details)
54 }
55 return nil
56 }
57
58
59 func GetCPUGroupConfig(ctx context.Context, id string) (*hcsschema.CpuGroupConfig, error) {
60 query := hcsschema.PropertyQuery{
61 PropertyTypes: []hcsschema.PropertyType{hcsschema.PTCPUGroup},
62 }
63 cpuGroupsPresent, err := hcs.GetServiceProperties(ctx, query)
64 if err != nil {
65 return nil, err
66 }
67 groupConfigs := &hcsschema.CpuGroupConfigurations{}
68 if err := json.Unmarshal(cpuGroupsPresent.Properties[0], groupConfigs); err != nil {
69 return nil, errors.Wrap(err, "failed to unmarshal host cpugroups")
70 }
71
72 for _, c := range groupConfigs.CpuGroups {
73 if strings.EqualFold(c.GroupId, id) {
74 return &c, nil
75 }
76 }
77 return nil, fmt.Errorf("no cpugroup exists with id %v", id)
78 }
79
View as plain text