func New() (CpuLoadReader, error) { reader, err := netlink.New() if err != nil { return nil, fmt.Errorf("failed to create a netlink based cpuload reader: %v", err) } glog.V(3).Info("Using a netlink-based load reader") return reader, nil }
func New() (CpuLoadReader, error) { // First try to create a scheddebug based load reader. schedReader, schedErr := scheddebug.New() if schedErr == nil { glog.Info("Using a sched debug based load reader") return schedReader, nil } glog.V(1).Infof("failed to create a scheddebug-based cpu load reader: %v", schedErr) // netlink gives us more data than scheddebug, but it doesn't work inside network namespaces. It also needs to be hierarchical. reader, err := netlink.New() if err != nil { return nil, fmt.Errorf("failed to create any cpu load reader - netlink based (%v), scheddebug based (%v)", err, schedErr) } glog.Info("Using a netlink-based load reader") return reader, nil }
func main() { n, err := netlink.New() if err != nil { log.Printf("Failed to create cpu load util: %s", err) return } defer n.Stop() paths := []string{"/sys/fs/cgroup/cpu", "/sys/fs/cgroup/cpu/docker"} names := []string{"/", "/docker"} for i, path := range paths { stats, err := n.GetCpuLoad(names[i], path) if err != nil { log.Printf("Error getting cpu load for %q: %s", path, err) } log.Printf("Task load for %s: %+v", path, stats) } }