@@ -31,7 +31,6 @@ import (
31
31
32
32
linuxproc "github.com/c9s/goprocinfo/linux"
33
33
"github.com/gitpod-io/gitpod/common-go/cgroups"
34
- v1 "github.com/gitpod-io/gitpod/common-go/cgroups/v1"
35
34
v2 "github.com/gitpod-io/gitpod/common-go/cgroups/v2"
36
35
"github.com/gitpod-io/gitpod/common-go/log"
37
36
"github.com/gitpod-io/gitpod/common-go/tracing"
@@ -931,7 +930,11 @@ func (wbs *InWorkspaceServiceServer) WorkspaceInfo(ctx context.Context, req *api
931
930
return nil , status .Errorf (codes .FailedPrecondition , "could not determine cgroup setup" )
932
931
}
933
932
934
- resources , err := getWorkspaceResourceInfo (wbs .CGroupMountPoint , cgroupPath , unified )
933
+ if ! unified {
934
+ return nil , status .Errorf (codes .FailedPrecondition , "only cgroups v2 is supported" )
935
+ }
936
+
937
+ resources , err := getWorkspaceResourceInfo (wbs .CGroupMountPoint , cgroupPath )
935
938
if err != nil {
936
939
if ! errors .Is (err , os .ErrNotExist ) {
937
940
log .WithError (err ).Error ("could not get resource information" )
@@ -944,38 +947,21 @@ func (wbs *InWorkspaceServiceServer) WorkspaceInfo(ctx context.Context, req *api
944
947
}, nil
945
948
}
946
949
947
- func getWorkspaceResourceInfo (mountPoint , cgroupPath string , unified bool ) (* api.Resources , error ) {
948
- if unified {
949
- cpu , err := getCpuResourceInfoV2 (mountPoint , cgroupPath )
950
- if err != nil {
951
- return nil , err
952
- }
953
-
954
- memory , err := getMemoryResourceInfoV2 (mountPoint , cgroupPath )
955
- if err != nil {
956
- return nil , err
957
- }
958
-
959
- return & api.Resources {
960
- Cpu : cpu ,
961
- Memory : memory ,
962
- }, nil
963
- } else {
964
- cpu , err := getCpuResourceInfoV1 (mountPoint , cgroupPath )
965
- if err != nil {
966
- return nil , err
967
- }
968
-
969
- memory , err := getMemoryResourceInfoV1 (mountPoint , cgroupPath )
970
- if err != nil {
971
- return nil , err
972
- }
950
+ func getWorkspaceResourceInfo (mountPoint , cgroupPath string ) (* api.Resources , error ) {
951
+ cpu , err := getCpuResourceInfoV2 (mountPoint , cgroupPath )
952
+ if err != nil {
953
+ return nil , err
954
+ }
973
955
974
- return & api.Resources {
975
- Cpu : cpu ,
976
- Memory : memory ,
977
- }, nil
956
+ memory , err := getMemoryResourceInfoV2 (mountPoint , cgroupPath )
957
+ if err != nil {
958
+ return nil , err
978
959
}
960
+
961
+ return & api.Resources {
962
+ Cpu : cpu ,
963
+ Memory : memory ,
964
+ }, nil
979
965
}
980
966
981
967
func getCpuResourceInfoV2 (mountPoint , cgroupPath string ) (* api.Cpu , error ) {
@@ -1066,120 +1052,11 @@ func getMemoryResourceInfoV2(mountPoint, cgroupPath string) (*api.Memory, error)
1066
1052
}, nil
1067
1053
}
1068
1054
1069
- func getMemoryResourceInfoV1 (mountPoint , cgroupPath string ) (* api.Memory , error ) {
1070
- memory := v1 .NewMemoryControllerWithMount (mountPoint , cgroupPath )
1071
-
1072
- memoryLimit , err := memory .Limit ()
1073
- if err != nil {
1074
- return nil , err
1075
- }
1076
-
1077
- memInfo , err := linuxproc .ReadMemInfo ("/proc/meminfo" )
1078
- if err != nil {
1079
- return nil , xerrors .Errorf ("failed to read meminfo: %w" , err )
1080
- }
1081
-
1082
- // if no memory limit has been specified, use total available memory
1083
- if memoryLimit == math .MaxUint64 || memoryLimit > memInfo .MemTotal * 1024 {
1084
- // total memory is specifed on kilobytes -> convert to bytes
1085
- memoryLimit = memInfo .MemTotal * 1024
1086
- }
1087
-
1088
- usedMemory , err := memory .Usage ()
1089
- if err != nil {
1090
- return nil , xerrors .Errorf ("failed to read memory limit: %w" , err )
1091
- }
1092
-
1093
- stats , err := memory .Stat ()
1094
- if err != nil {
1095
- return nil , xerrors .Errorf ("failed to read memory stats: %w" , err )
1096
- }
1097
-
1098
- if stats .InactiveFileTotal > 0 {
1099
- if usedMemory < stats .InactiveFileTotal {
1100
- usedMemory = 0
1101
- } else {
1102
- usedMemory -= stats .InactiveFileTotal
1103
- }
1104
- }
1105
-
1106
- return & api.Memory {
1107
- Limit : int64 (memoryLimit ),
1108
- Used : int64 (usedMemory ),
1109
- }, nil
1110
- }
1111
-
1112
- func getCpuResourceInfoV1 (mountPoint , cgroupPath string ) (* api.Cpu , error ) {
1113
- cpu := v1 .NewCpuControllerWithMount (mountPoint , cgroupPath )
1114
-
1115
- t , err := resolveCPUStatV1 (cpu )
1116
- if err != nil {
1117
- return nil , err
1118
- }
1119
-
1120
- time .Sleep (time .Second )
1121
-
1122
- t2 , err := resolveCPUStatV1 (cpu )
1123
- if err != nil {
1124
- return nil , err
1125
- }
1126
-
1127
- cpuUsage := t2 .usage - t .usage
1128
- totalTime := t2 .uptime - t .uptime
1129
- used := cpuUsage / totalTime * 1000
1130
-
1131
- quota , err := cpu .Quota ()
1132
- if err != nil {
1133
- return nil , err
1134
- }
1135
-
1136
- // if no cpu limit has been specified, use the number of cores
1137
- var limit uint64
1138
- if quota == math .MaxUint64 {
1139
- content , err := os .ReadFile (filepath .Join (mountPoint , "cpu" , cgroupPath , "cpuacct.usage_percpu" ))
1140
- if err != nil {
1141
- return nil , xerrors .Errorf ("failed to read cpuacct.usage_percpu: %w" , err )
1142
- }
1143
- limit = uint64 (len (strings .Split (strings .TrimSpace (string (content )), " " ))) * 1000
1144
- } else {
1145
- period , err := cpu .Period ()
1146
- if err != nil {
1147
- return nil , err
1148
- }
1149
-
1150
- limit = quota / period * 1000
1151
- }
1152
-
1153
- return & api.Cpu {
1154
- Used : int64 (used ),
1155
- Limit : int64 (limit ),
1156
- }, nil
1157
- }
1158
-
1159
1055
type cpuStat struct {
1160
1056
usage float64
1161
1057
uptime float64
1162
1058
}
1163
1059
1164
- func resolveCPUStatV1 (cpu * v1.Cpu ) (* cpuStat , error ) {
1165
- usage_ns , err := cpu .Usage ()
1166
- if err != nil {
1167
- return nil , xerrors .Errorf ("failed to get cpu usage: %w" , err )
1168
- }
1169
-
1170
- // convert from nanoseconds to seconds
1171
- usage := float64 (usage_ns ) * 1e-9
1172
- uptime , err := readProcUptime ()
1173
- if err != nil {
1174
- return nil , err
1175
- }
1176
-
1177
- return & cpuStat {
1178
- usage : usage ,
1179
- uptime : uptime ,
1180
- }, nil
1181
- }
1182
-
1183
1060
func resolveCPUStatV2 (cpu * v2.Cpu ) (* cpuStat , error ) {
1184
1061
stats , err := cpu .Stat ()
1185
1062
if err != nil {
0 commit comments