swapus-go/swapus.go

73 lines
1.8 KiB
Go
Raw Permalink 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"
)
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
})
}
for _, proc := range procs[:*LIMIT] {
if proc.Swap == 0 || proc.CmdLine == "" {
continue
}
fmt.Printf("%d kB [%d] %v\n", proc.Swap, proc.Pid, proc.CmdLine)
}
}