-func OpenAndReadAll(filename string, log_chan chan<- string) ([]byte, error) {
- in, err := os.Open(filename)
- if err != nil {
- if log_chan != nil {
- log_chan <- fmt.Sprintf("crunchstat: open %s: %s", filename, err)
- }
- return nil, err
- }
- defer in.Close()
- {
- content, err := ioutil.ReadAll(in)
- if err != nil && log_chan != nil {
- log_chan <- fmt.Sprintf("crunchstat: read %s: %s", filename, err)
- }
- return content, err
- }
-}
-
-var reportedStatFile map[string]bool
-var reportedNoStatFile map[string]bool
-
-func FindStat(stderr chan<- string, cgroup Cgroup, statgroup string, stat string) string {
- if reportedStatFile == nil {
- reportedStatFile = make(map[string]bool)
- reportedNoStatFile = make(map[string]bool)
- }
-
- var path string
- path = fmt.Sprintf("%s/%s/%s/%s/%s", cgroup.root, statgroup, cgroup.parent, cgroup.cid, stat)
- if _, err := os.Stat(path); err != nil {
- path = fmt.Sprintf("%s/%s/%s/%s", cgroup.root, cgroup.parent, cgroup.cid, stat)
- }
- if _, err := os.Stat(path); err != nil {
- path = fmt.Sprintf("%s/%s/%s", cgroup.root, statgroup, stat)
- }
- if _, err := os.Stat(path); err != nil {
- path = fmt.Sprintf("%s/%s", cgroup.root, stat)
- }
- if _, err := os.Stat(path); err != nil {
- if _, ok := reportedNoStatFile[path]; !ok {
- stderr <- fmt.Sprintf("crunchstat: did not find stats file (root %s, parent %s, cid %s, statgroup %s, stat %s)", cgroup.root, cgroup.parent, cgroup.cid, statgroup, stat)
- reportedNoStatFile[path] = true
- }
- return ""
- }
- if _, ok := reportedStatFile[path]; !ok {
- stderr <- fmt.Sprintf("crunchstat: reading stats from %s", path)
- reportedStatFile[path] = true
- }
- return path
-}
-
-func GetContainerNetStats(stderr chan<- string, cgroup Cgroup) (io.Reader, error) {
- procsFilename := FindStat(stderr, cgroup, "cpuacct", "cgroup.procs")
- procsFile, err := os.Open(procsFilename)
- if err != nil {
- stderr <- fmt.Sprintf("crunchstat: open %s: %s", procsFilename, err)
- return nil, err
- }
- defer procsFile.Close()
- reader := bufio.NewScanner(procsFile)
- for reader.Scan() {
- taskPid := reader.Text()
- statsFilename := fmt.Sprintf("/proc/%s/net/dev", taskPid)
- stats, err := OpenAndReadAll(statsFilename, stderr)
- if err != nil {
- continue
- }
- return strings.NewReader(string(stats)), nil
- }
- return nil, errors.New("Could not read stats for any proc in container")
-}
-
-type Disk struct {
- last_read int64
- next_read int64
- last_write int64
- next_write int64
-}
-var disk map[string]*Disk
-
-func DoBlkIoStats(stderr chan<- string, cgroup Cgroup) {
- blkio_io_service_bytes := FindStat(stderr, cgroup, "blkio", "blkio.io_service_bytes")
- if blkio_io_service_bytes == "" {
- return
- }
-
- if disk == nil {
- disk = make(map[string]*Disk)
- }