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 ## 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 ## v1.0.0 - 2019-06-25
### Added ### Added

View file

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

View file

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

View file

@ -26,6 +26,7 @@ import (
"path/filepath" "path/filepath"
) )
// Variable setted by option
var ( var (
LIMIT = flag.Int("limit", 20, "limit the number of processes to print") LIMIT = flag.Int("limit", 20, "limit the number of processes to print")
REVERSE = flag.Bool("reverse", false, "reverse the list 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 == "" { if proc.Swap == 0 || proc.CmdLine == "" {
continue 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)
} }
} }