Compare commits

...

4 commits

4 changed files with 28 additions and 4 deletions

View file

@ -7,6 +7,18 @@ Which is based on [Keep A Changelog](http://keepachangelog.com/)
## Unreleased
### Added
- A comment for all functions
### Changed
- Use fmt.Sprintf to generate file paths
### Removed
- Duplicate array interval
## v1.0.0 - 2019-06-25
### Added

View file

@ -29,6 +29,12 @@ Output with the swap usage, pid and the command:
1716 kB [275] /lib/systemd/systemd-udevd
```
For more options:
```
swapus -help
```
## License
```

View file

@ -18,18 +18,21 @@
package main
import (
"fmt"
"strings"
"regexp"
"io/ioutil"
"strconv"
)
// Proc is a struct with process information
type Proc struct {
Pid int
CmdLine string
Swap int
}
// NewProc initialize a new Proc struct
func NewProc(pid int) *Proc {
proc := new(Proc)
proc.Pid = pid
@ -39,8 +42,9 @@ func NewProc(pid int) *Proc {
return proc
}
// CmdLine return the command line for a process
func CmdLine(pid int) string {
data, err := ioutil.ReadFile("/proc/" + strconv.Itoa(pid) + "/cmdline")
data, err := ioutil.ReadFile(fmt.Sprintf("/proc/%d/cmdline", pid))
if err != nil {
return ""
}
@ -49,8 +53,9 @@ func CmdLine(pid int) string {
return cmdline
}
// Swap return the swap usage for a process
func Swap(pid int) int {
data, err := ioutil.ReadFile("/proc/" + strconv.Itoa(pid) + "/status")
data, err := ioutil.ReadFile(fmt.Sprintf("/proc/%d/status", pid))
if err != nil {
return 0
}

View file

@ -26,6 +26,7 @@ import (
"path/filepath"
)
// Variable setted by option
var (
LIMIT = flag.Int("limit", 20, "limit the number of processes to print")
REVERSE = flag.Bool("reverse", false, "reverse the list of processes to print")
@ -63,10 +64,10 @@ func main() {
})
}
for _, proc := range procs[:*LIMIT] {
for _, proc := range procs {
if proc.Swap == 0 || proc.CmdLine == "" {
continue
}
fmt.Printf("%d kB [%d] %v\n", proc.Swap, proc.Pid, proc.CmdLine)
fmt.Printf("%d kB [%d] %s\n", proc.Swap, proc.Pid, proc.CmdLine)
}
}