in cgroup/reader.go [95:178]
func (r *Reader) GetStatsForProcess(pid int) (*Stats, error) {
// Read /proc/[pid]/cgroup to get the paths to the cgroup metrics.
paths, err := ProcessCgroupPaths(r.rootfsMountpoint, pid)
if err != nil {
return nil, err
}
// Build the full path for the subsystems we are interested in.
mounts := map[string]mount{}
for _, interestedSubsystem := range []string{"blkio", "cpu", "cpuacct", "memory"} {
path, found := paths[interestedSubsystem]
if !found {
continue
}
if path == "/" && r.ignoreRootCgroups {
continue
}
subsystemMount, found := r.cgroupMountpoints[interestedSubsystem]
if !found {
continue
}
id := filepath.Base(path)
if r.cgroupsHierarchyOverride != "" {
path = r.cgroupsHierarchyOverride
}
mounts[interestedSubsystem] = mount{
subsystem: interestedSubsystem,
mountpoint: subsystemMount,
id: id,
path: path,
fullPath: filepath.Join(subsystemMount, path),
}
}
stats := Stats{Metadata: getCommonCgroupMetadata(mounts)}
// Collect stats from each cgroup subsystem associated with the task.
if mount, found := mounts["blkio"]; found {
stats.BlockIO = &BlockIOSubsystem{}
err := stats.BlockIO.get(mount.fullPath)
if err != nil {
return nil, err
}
stats.BlockIO.Metadata.ID = mount.id
stats.BlockIO.Metadata.Path = mount.path
}
if mount, found := mounts["cpu"]; found {
stats.CPU = &CPUSubsystem{}
err := stats.CPU.get(mount.fullPath)
if err != nil {
return nil, err
}
stats.CPU.Metadata.ID = mount.id
stats.CPU.Metadata.Path = mount.path
}
if mount, found := mounts["cpuacct"]; found {
stats.CPUAccounting = &CPUAccountingSubsystem{}
err := stats.CPUAccounting.get(mount.fullPath)
if err != nil {
return nil, err
}
stats.CPUAccounting.Metadata.ID = mount.id
stats.CPUAccounting.Metadata.Path = mount.path
}
if mount, found := mounts["memory"]; found {
stats.Memory = &MemorySubsystem{}
err := stats.Memory.get(mount.fullPath)
if err != nil {
return nil, err
}
stats.Memory.Metadata.ID = mount.id
stats.Memory.Metadata.Path = mount.path
}
// Return nil if no metrics were collected.
if stats.BlockIO == nil && stats.CPU == nil && stats.CPUAccounting == nil && stats.Memory == nil {
return nil, nil
}
return &stats, nil
}