Skip to content

Commit 57e2f88

Browse files
committed
cleanup
Signed-off-by: Jess Frazelle <acidburn@microsoft.com>
1 parent a564e20 commit 57e2f88

File tree

2 files changed

+15
-103
lines changed

2 files changed

+15
-103
lines changed

README.md

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,18 @@ $ go get github.com/jessfraz/magneto
2020

2121
```console
2222
$ sudo runc events <container_id> | magneto
23-
CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS
24-
1.84% 108.8 MB / 3.902 GB 1.38% 54.86 MB / 792.8 kB 26.64 MB / 0 B 4
23+
CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS
24+
1.84% 108.8 MiB / 3.902 GiB 1.38% 54.86 MB / 792.8 kB 26.64 MB / 0 B 4
2525
```
2626

2727
![chrome.png](chrome.png)
2828

2929
**Usage with the `docker-runc` command that ships with docker**
3030

3131
```console
32-
$ sudo docker-runc -root /run/docker/runtime-runc/moby events <container_id> \
33-
| sudo magneto -root /run/docker/runtime-runc/moby
32+
$ sudo docker-runc -root /run/docker/runtime-runc/moby events <container_id> | magneto
33+
CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS
34+
100.12% 452KiB / 8EiB 0.00% 0B / 0B 0B / 0B 2
3435
```
3536

3637
```console
@@ -47,8 +48,6 @@ $ magneto --help
4748
Build: 30036e2
4849

4950
-d run in debug mode
50-
-root string
51-
root directory of runc storage of container state (default "/run/runc")
5251
-v print version and exit (shorthand)
5352
-version
5453
print version and exit

main.go

Lines changed: 10 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@ import (
66
"flag"
77
"fmt"
88
"io"
9-
"io/ioutil"
109
"os"
11-
"path/filepath"
1210
"strconv"
1311
"strings"
1412
"sync"
@@ -19,7 +17,6 @@ import (
1917
"github.com/jessfraz/magneto/types"
2018
"github.com/jessfraz/magneto/version"
2119
"github.com/opencontainers/runc/libcontainer/system"
22-
specs "github.com/opencontainers/runtime-spec/specs-go"
2320
"github.com/sirupsen/logrus"
2421
)
2522

@@ -38,23 +35,19 @@ const (
3835
3936
`
4037

41-
specFile = "config.json"
42-
stateFile = "state.json"
43-
defaultRoot = "/run/runc"
38+
specFile = "config.json"
39+
stateFile = "state.json"
4440

4541
nanoSecondsPerSecond = 1e9
4642
)
4743

4844
var (
49-
root string
50-
5145
debug bool
5246
vrsn bool
5347
)
5448

5549
func init() {
5650
// Parse flags
57-
flag.StringVar(&root, "root", defaultRoot, "root directory of runc storage of container state")
5851
flag.BoolVar(&vrsn, "version", false, "print version and exit")
5952
flag.BoolVar(&vrsn, "v", false, "print version and exit (shorthand)")
6053
flag.BoolVar(&debug, "d", false, "run in debug mode")
@@ -90,8 +83,8 @@ type containerStats struct {
9083
MemoryPercentage float64
9184
NetworkRx float64
9285
NetworkTx float64
93-
BlockRead uint64
94-
BlockWrite uint64
86+
BlockRead float64
87+
BlockWrite float64
9588
PidsCurrent uint64
9689
mu sync.RWMutex
9790
bufReader *bufio.Reader
@@ -166,12 +159,6 @@ func (s *containerStats) collect() {
166159
}
167160
v := e.Data
168161

169-
/*resources, err := getContainerResources(e.ID)
170-
if err != nil {
171-
u <- fmt.Errorf("Getting container's configured resources failed: %v", err)
172-
continue
173-
}*/
174-
175162
systemUsage, err := s.getSystemCPUUsage()
176163
if err != nil {
177164
u <- fmt.Errorf("collecting system cpu usage failed: %v", err)
@@ -195,8 +182,8 @@ func (s *containerStats) collect() {
195182
// set the stats
196183
s.mu.Lock()
197184
s.CPUPercentage = cpuPercent
198-
s.BlockRead = blkRead
199-
s.BlockWrite = blkWrite
185+
s.BlockRead = float64(blkRead)
186+
s.BlockWrite = float64(blkWrite)
200187
s.Memory = mem
201188
s.MemoryLimit = memLimit
202189
s.MemoryPercentage = memPercent
@@ -218,8 +205,6 @@ func (s *containerStats) collect() {
218205
}
219206
}
220207

221-
var it = 0
222-
223208
func (s *containerStats) Display(w io.Writer) error {
224209
s.mu.RLock()
225210
defer s.mu.RUnlock()
@@ -229,16 +214,13 @@ func (s *containerStats) Display(w io.Writer) error {
229214
return s.err
230215
}
231216

232-
fmt.Fprintf(w, "%.2f%%\t%s / %s\t%.2f%%\t%s / %s\t%d / %d\t%d\n",
217+
fmt.Fprintf(w, "%.2f%%\t%s / %s\t%.2f%%\t%s / %s\t%s / %s\t%d\n",
233218
s.CPUPercentage,
234-
units.HumanSize(s.Memory), units.HumanSize(s.MemoryLimit),
219+
units.BytesSize(s.Memory), units.BytesSize(s.MemoryLimit),
235220
s.MemoryPercentage,
236-
units.HumanSize(s.NetworkRx), units.HumanSize(s.NetworkTx),
237-
s.BlockRead, s.BlockWrite,
221+
units.HumanSizeWithPrecision(s.NetworkRx, 3), units.HumanSizeWithPrecision(s.NetworkTx, 3),
222+
units.HumanSizeWithPrecision(s.BlockRead, 3), units.HumanSizeWithPrecision(s.BlockWrite, 3),
238223
s.PidsCurrent)
239-
240-
logrus.Infof("displayed stats %d", it)
241-
it++
242224
return nil
243225
}
244226

@@ -348,72 +330,3 @@ func (s *containerStats) getSystemCPUUsage() (uint64, error) {
348330

349331
return 0, fmt.Errorf("invalid stat format. Error trying to parse the '/proc/stat' file")
350332
}
351-
352-
func getContainerResources(id string) (*specs.LinuxResources, error) {
353-
abs, err := filepath.Abs(root)
354-
if err != nil {
355-
return nil, err
356-
}
357-
358-
// check to make sure a container exists with this ID
359-
statePath := filepath.Join(abs, id, stateFile)
360-
361-
// read the state.json for the container so we can find out the bundle path
362-
f, err := os.Open(statePath)
363-
if err != nil {
364-
if os.IsNotExist(err) {
365-
return nil, fmt.Errorf("JSON runtime state file %s not found at %s", stateFile, statePath)
366-
}
367-
}
368-
defer f.Close()
369-
370-
var state types.State
371-
if err = json.NewDecoder(f).Decode(&state); err != nil {
372-
return nil, err
373-
}
374-
375-
bundle := searchLabels(state.Config.Labels, "bundle")
376-
specPath := filepath.Join(bundle, specFile)
377-
378-
// read the runtime.json for the container so we know things like limits set
379-
// this is only if a container ID is not passed we assume we are in a directory
380-
// with a config.json containing the spec
381-
f, err = os.Open(specPath)
382-
if err != nil {
383-
if os.IsNotExist(err) {
384-
return nil, fmt.Errorf("JSON runtime config file %s not found at %s", specFile, specPath)
385-
}
386-
}
387-
defer f.Close()
388-
389-
var spec specs.Spec
390-
if err = json.NewDecoder(f).Decode(&spec); err != nil {
391-
return nil, err
392-
}
393-
if spec.Linux.Resources.Memory.Limit == nil {
394-
// set the memory limit manually
395-
b, err := ioutil.ReadFile(filepath.Join(state.CgroupPaths["memory"], "memory.limit_in_bytes"))
396-
if err != nil {
397-
return nil, err
398-
}
399-
i, err := units.RAMInBytes(strings.TrimSpace(string(b)) + "b")
400-
if err != nil {
401-
return nil, err
402-
}
403-
spec.Linux.Resources.Memory.Limit = &i
404-
}
405-
return spec.Linux.Resources, nil
406-
}
407-
408-
func searchLabels(labels []string, query string) string {
409-
for _, l := range labels {
410-
parts := strings.SplitN(l, "=", 2)
411-
if len(parts) < 2 {
412-
continue
413-
}
414-
if parts[0] == query {
415-
return parts[1]
416-
}
417-
}
418-
return ""
419-
}

0 commit comments

Comments
 (0)