MCPcopy Create free account
hub / github.com/OpenSN-Library/OpenSN-Library / GetInstanceResourceInfo

Function GetInstanceResourceInfo

daemon/utils/monitor.go:155–210  ·  view source on GitHub ↗
(instanceID string, pid int)

Source from the content-addressed store, hash-verified

153}
154
155func GetInstanceResourceInfo(instanceID string, pid int) (*InstanceResouceRaw, error) {
156 cgroupProc, err := os.Open(fmt.Sprintf("/proc/%d/cgroup", pid))
157 defer cgroupProc.Close()
158 if err != nil {
159 errMsg := fmt.Sprintf("Open Cgroup Mount Dir Record Of %s Error: %s", instanceID, err.Error())
160 logrus.Warn(errMsg)
161 return nil, err
162 }
163 bytes, err := io.ReadAll(cgroupProc)
164
165 if err != nil {
166 errMsg := fmt.Sprintf("Read Cgroup Mount Dir Of %s Error: %s", instanceID, err.Error())
167 logrus.Warn(errMsg)
168 return nil, err
169 }
170
171 splitedStr := strings.Split(string(bytes), "::")
172 if len(splitedStr) < 2 {
173 errMsg := fmt.Sprintf("Read Cgroup Mount Dir Of %s Error: invalid format: %s", instanceID, string(bytes))
174 logrus.Warn(errMsg)
175 return nil, err
176 }
177
178 pathBase := fmt.Sprintf(
179 "/sys/fs/cgroup/%s/",
180 strings.TrimSpace(splitedStr[1]),
181 )
182 cpuPath := path.Join(pathBase, "cpu.stat")
183 memPath := path.Join(pathBase, "memory.current")
184 swapPath := path.Join(pathBase, "memory.swap.current")
185
186 mem, err := readUint64(memPath)
187 if err != nil {
188 errMsg := fmt.Sprintf("Read Memory Usage Of %s Error: %s", instanceID, err.Error())
189 logrus.Warn(errMsg)
190 return nil, err
191 }
192 swap, err := readUint64(swapPath)
193 if err != nil {
194 errMsg := fmt.Sprintf("Read Swap Usage Of %s Error: %s", instanceID, err.Error())
195 logrus.Warn(errMsg)
196 return nil, err
197 }
198 cpuUsage, err := readCpuUsage(cpuPath)
199 if err != nil {
200 errMsg := fmt.Sprintf("Read Cpu Usage Of %s Error: %s", instanceID, err.Error())
201 logrus.Warn(errMsg)
202 return nil, err
203 }
204
205 return &InstanceResouceRaw{
206 CPUBusy: cpuUsage / 1000,
207 MemByte: mem,
208 SwapMemByte: swap,
209 }, nil
210}
211
212func GetInstanceLinkResourceInfo(pid int) (map[string]*LinkResourceRaw, error) {

Callers

nothing calls this directly

Calls 2

readUint64Function · 0.85
readCpuUsageFunction · 0.85

Tested by

no test coverage detected