swapus-go/swapus.go

74 lines
1.8 KiB
Go
Raw Normal View History

2019-06-25 05:29:49 +00:00
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package main
import (
"os"
"flag"
"fmt"
"sort"
"strconv"
"path/filepath"
)
2019-06-26 19:48:13 +00:00
// Variable setted by option
2019-06-25 05:29:49 +00:00
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")
HELP = flag.Bool("help", false, "print this help message")
)
func init() {
flag.Parse()
if *HELP {
flag.PrintDefaults()
os.Exit(1)
}
}
func main() {
var procs []*Proc
folders, _ := filepath.Glob("/proc/[0-9]*")
for _, f := range folders {
pid, err := strconv.Atoi(filepath.Base(f))
if err == nil {
procs = append(procs, NewProc(pid))
}
}
sort.Slice(procs, func(i, j int) bool {
return procs[i].Swap > procs[j].Swap
})
procs = procs[:*LIMIT]
if *REVERSE {
sort.Slice(procs, func(i, j int) bool {
return procs[i].Swap < procs[j].Swap
})
}
2019-06-26 19:39:17 +00:00
for _, proc := range procs {
2019-06-25 05:29:49 +00:00
if proc.Swap == 0 || proc.CmdLine == "" {
continue
}
fmt.Printf("%d kB [%d] %v\n", proc.Swap, proc.Pid, proc.CmdLine)
}
}