23 #include <sys/types.h>
29 // The above block of magic allows us to look up user_hz via _SC_CLK_TCK.
37 var childLog = log.New(os.Stderr, "", 0)
38 var statLog = log.New(os.Stderr, "crunchstat: ", 0)
41 MaxLogLine = 1 << 14 // Child stderr lines >16KiB will be split
44 func CopyPipeToChildLog(in io.ReadCloser, done chan<- bool) {
45 reader := bufio.NewReaderSize(in, MaxLogLine)
48 line, isPrefix, err := reader.ReadLine()
51 } else if err != nil {
52 statLog.Fatal("error reading child stderr:", err)
58 childLog.Print(prefix, string(line), suffix)
59 // Set up prefix for following line
70 func ReadAllOrWarn(in *os.File) ([]byte, error) {
71 content, err := ioutil.ReadAll(in)
73 statLog.Printf("error reading %s: %s\n", in.Name(), err)
78 var reportedStatFile = map[string]string{}
80 // Open the cgroup stats file in /sys/fs corresponding to the target
81 // cgroup, and return an *os.File. If no stats file is available,
84 // TODO: Instead of trying all options, choose a process in the
85 // container, and read /proc/PID/cgroup to determine the appropriate
86 // cgroup root for the given statgroup. (This will avoid falling back
87 // to host-level stats during container setup and teardown.)
88 func OpenStatFile(cgroup Cgroup, statgroup string, stat string) (*os.File, error) {
90 fmt.Sprintf("%s/%s/%s/%s/%s", cgroup.root, statgroup, cgroup.parent, cgroup.cid, stat),
91 fmt.Sprintf("%s/%s/%s/%s", cgroup.root, cgroup.parent, cgroup.cid, stat),
92 fmt.Sprintf("%s/%s/%s", cgroup.root, statgroup, stat),
93 fmt.Sprintf("%s/%s", cgroup.root, stat),
98 for _, path = range paths {
99 file, err = os.Open(path)
106 if pathWas, ok := reportedStatFile[stat]; !ok || pathWas != path {
107 // Log whenever we start using a new/different cgroup
108 // stat file for a given statistic. This typically
109 // happens 1 to 3 times per statistic, depending on
110 // whether we happen to collect stats [a] before any
111 // processes have been created in the container and
112 // [b] after all contained processes have exited.
114 statLog.Printf("notice: stats not available: stat %s, statgroup %s, cid %s, parent %s, root %s\n", stat, statgroup, cgroup.cid, cgroup.parent, cgroup.root)
116 statLog.Printf("notice: stats moved from %s to %s\n", reportedStatFile[stat], path)
118 statLog.Printf("notice: reading stats from %s\n", path)
120 reportedStatFile[stat] = path
125 func GetContainerNetStats(cgroup Cgroup) (io.Reader, error) {
126 procsFile, err := OpenStatFile(cgroup, "cpuacct", "cgroup.procs")
130 defer procsFile.Close()
131 reader := bufio.NewScanner(procsFile)
133 taskPid := reader.Text()
134 statsFilename := fmt.Sprintf("/proc/%s/net/dev", taskPid)
135 stats, err := ioutil.ReadFile(statsFilename)
137 statLog.Printf("read %s: %s\n", statsFilename, err)
140 return strings.NewReader(string(stats)), nil
142 return nil, errors.New("Could not read stats for any proc in container")
145 type IoSample struct {
151 func DoBlkIoStats(cgroup Cgroup, lastSample map[string]IoSample) {
152 c, err := OpenStatFile(cgroup, "blkio", "blkio.io_service_bytes")
157 b := bufio.NewScanner(c)
158 var sampleTime = time.Now()
159 newSamples := make(map[string]IoSample)
161 var device, op string
163 if _, err := fmt.Sscanf(string(b.Text()), "%s %s %d", &device, &op, &val); err != nil {
166 var thisSample IoSample
168 if thisSample, ok = newSamples[device]; !ok {
169 thisSample = IoSample{sampleTime, -1, -1}
173 thisSample.rxBytes = val
175 thisSample.txBytes = val
177 newSamples[device] = thisSample
179 for dev, sample := range newSamples {
180 if sample.txBytes < 0 || sample.rxBytes < 0 {
184 if prev, ok := lastSample[dev]; ok {
185 delta = fmt.Sprintf(" -- interval %.4f seconds %d write %d read",
186 sample.sampleTime.Sub(prev.sampleTime).Seconds(),
187 sample.txBytes-prev.txBytes,
188 sample.rxBytes-prev.rxBytes)
190 statLog.Printf("blkio:%s %d write %d read%s\n", dev, sample.txBytes, sample.rxBytes, delta)
191 lastSample[dev] = sample
195 type MemSample struct {
197 memStat map[string]int64
200 func DoMemoryStats(cgroup Cgroup) {
201 c, err := OpenStatFile(cgroup, "memory", "memory.stat")
206 b := bufio.NewScanner(c)
207 thisSample := MemSample{time.Now(), make(map[string]int64)}
208 wantStats := [...]string{"cache", "swap", "pgmajfault", "rss"}
212 if _, err := fmt.Sscanf(string(b.Text()), "%s %d", &stat, &val); err != nil {
215 thisSample.memStat[stat] = val
217 var outstat bytes.Buffer
218 for _, key := range wantStats {
219 if val, ok := thisSample.memStat[key]; ok {
220 outstat.WriteString(fmt.Sprintf(" %d %s", val, key))
223 statLog.Printf("mem%s\n", outstat.String())
226 func DoNetworkStats(cgroup Cgroup, lastSample map[string]IoSample) {
227 sampleTime := time.Now()
228 stats, err := GetContainerNetStats(cgroup)
233 scanner := bufio.NewScanner(stats)
237 words := strings.Fields(scanner.Text())
238 if len(words) != 17 {
239 // Skip lines with wrong format
242 ifName = strings.TrimRight(words[0], ":")
243 if ifName == "lo" || ifName == "" {
244 // Skip loopback interface and lines with wrong format
247 if tx, err = strconv.ParseInt(words[9], 10, 64); err != nil {
250 if rx, err = strconv.ParseInt(words[1], 10, 64); err != nil {
253 nextSample := IoSample{}
254 nextSample.sampleTime = sampleTime
255 nextSample.txBytes = tx
256 nextSample.rxBytes = rx
258 if prev, ok := lastSample[ifName]; ok {
259 interval := nextSample.sampleTime.Sub(prev.sampleTime).Seconds()
260 delta = fmt.Sprintf(" -- interval %.4f seconds %d tx %d rx",
265 statLog.Printf("net:%s %d tx %d rx%s\n", ifName, tx, rx, delta)
266 lastSample[ifName] = nextSample
270 type CpuSample struct {
271 hasData bool // to distinguish the zero value from real data
278 // Return the number of CPUs available in the container. Return 0 if
279 // we can't figure out the real number of CPUs.
280 func GetCpuCount(cgroup Cgroup) int64 {
281 cpusetFile, err := OpenStatFile(cgroup, "cpuset", "cpuset.cpus")
285 defer cpusetFile.Close()
286 b, err := ReadAllOrWarn(cpusetFile)
287 sp := strings.Split(string(b), ",")
289 for _, v := range sp {
291 n, _ := fmt.Sscanf(v, "%d-%d", &min, &max)
293 cpus += (max - min) + 1
301 func DoCpuStats(cgroup Cgroup, lastSample *CpuSample) {
302 statFile, err := OpenStatFile(cgroup, "cpuacct", "cpuacct.stat")
306 defer statFile.Close()
307 b, err := ReadAllOrWarn(statFile)
312 nextSample := CpuSample{true, time.Now(), 0, 0, GetCpuCount(cgroup)}
313 var userTicks, sysTicks int64
314 fmt.Sscanf(string(b), "user %d\nsystem %d", &userTicks, &sysTicks)
315 user_hz := float64(C.sysconf(C._SC_CLK_TCK))
316 nextSample.user = float64(userTicks) / user_hz
317 nextSample.sys = float64(sysTicks) / user_hz
320 if lastSample.hasData {
321 delta = fmt.Sprintf(" -- interval %.4f seconds %.4f user %.4f sys",
322 nextSample.sampleTime.Sub(lastSample.sampleTime).Seconds(),
323 nextSample.user-lastSample.user,
324 nextSample.sys-lastSample.sys)
326 statLog.Printf("cpu %.4f user %.4f sys %d cpus%s\n",
327 nextSample.user, nextSample.sys, nextSample.cpus, delta)
328 *lastSample = nextSample
331 func PollCgroupStats(cgroup Cgroup, poll int64, stop_poll_chan <-chan bool) {
332 var lastNetSample = map[string]IoSample{}
333 var lastDiskSample = map[string]IoSample{}
334 var lastCpuSample = CpuSample{}
336 poll_chan := make(chan bool, 1)
338 // Send periodic poll events.
341 time.Sleep(time.Duration(poll) * time.Millisecond)
347 case <-stop_poll_chan:
350 // Emit stats, then select again.
352 DoMemoryStats(cgroup)
353 DoCpuStats(cgroup, &lastCpuSample)
354 DoBlkIoStats(cgroup, lastDiskSample)
355 DoNetworkStats(cgroup, lastNetSample)
359 func run(logger *log.Logger) error {
364 cgroup_cidfile string
369 flag.StringVar(&cgroup_root, "cgroup-root", "", "Root of cgroup tree")
370 flag.StringVar(&cgroup_parent, "cgroup-parent", "", "Name of container parent under cgroup")
371 flag.StringVar(&cgroup_cidfile, "cgroup-cid", "", "Path to container id file")
372 flag.Int64Var(&wait, "wait", 5, "Maximum time (in seconds) to wait for cid file to show up")
373 flag.Int64Var(&poll, "poll", 1000, "Polling frequency, in milliseconds")
377 if cgroup_root == "" {
378 statLog.Fatal("error: must provide -cgroup-root")
381 finish_chan := make(chan bool)
382 defer close(finish_chan)
386 if len(flag.Args()) > 0 {
388 cmd = exec.Command(flag.Args()[0], flag.Args()[1:]...)
390 childLog.Println("Running", flag.Args())
392 // Child process will use our stdin and stdout pipes
393 // (we close our copies below)
395 cmd.Stdout = os.Stdout
397 // Forward SIGINT and SIGTERM to inner process
398 sigChan := make(chan os.Signal, 1)
399 go func(sig <-chan os.Signal) {
401 if cmd.Process != nil {
402 cmd.Process.Signal(catch)
404 statLog.Println("caught signal:", catch)
406 signal.Notify(sigChan, syscall.SIGTERM)
407 signal.Notify(sigChan, syscall.SIGINT)
409 // Funnel stderr through our channel
410 stderr_pipe, err := cmd.StderrPipe()
412 statLog.Fatalln("error in StderrPipe:", err)
414 go CopyPipeToChildLog(stderr_pipe, finish_chan)
417 if err := cmd.Start(); err != nil {
418 statLog.Fatalln("error in cmd.Start:", err)
421 // Close stdin/stdout in this (parent) process
427 var container_id string
428 if cgroup_cidfile != "" {
429 // wait up to 'wait' seconds for the cid file to appear
432 for i = 0; i < time.Duration(wait)*time.Second; i += (100 * time.Millisecond) {
433 cid, err := ioutil.ReadFile(cgroup_cidfile)
434 if err == nil && len(cid) > 0 {
436 container_id = string(cid)
439 time.Sleep(100 * time.Millisecond)
442 statLog.Println("error reading cid file:", cgroup_cidfile)
446 stop_poll_chan := make(chan bool, 1)
447 cgroup := Cgroup{cgroup_root, cgroup_parent, container_id}
448 go PollCgroupStats(cgroup, poll, stop_poll_chan)
450 // When the child exits, tell the polling goroutine to stop.
451 defer func() { stop_poll_chan <- true }()
453 // Wait for CopyPipeToChan to consume child's stderr pipe
460 logger := log.New(os.Stderr, "crunchstat: ", 0)
461 if err := run(logger); err != nil {
462 if exiterr, ok := err.(*exec.ExitError); ok {
463 // The program has exited with an exit code != 0
465 // This works on both Unix and
466 // Windows. Although package syscall is
467 // generally platform dependent, WaitStatus is
468 // defined for both Unix and Windows and in
469 // both cases has an ExitStatus() method with
470 // the same signature.
471 if status, ok := exiterr.Sys().(syscall.WaitStatus); ok {
472 os.Exit(status.ExitStatus())
475 statLog.Fatalln("error in cmd.Wait:", err)