-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
164 lines (132 loc) · 4.15 KB
/
main.go
File metadata and controls
164 lines (132 loc) · 4.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/*
psmaps reports memory usage of Linux processes, including USS, PSS, and RSS.
The memory metrics are defined as follows:
- USS - unique set size, the amount of memory unique to a process, i.e. not shared with any other process.
- PSS - proportional set size, the process’s unshared memory plus a proportional share of memory shared with other processes.
- RSS - resident set size, the total memory resident in RAM for a process, including all private pages and all shared pages.
All values represent memory resident in RAM (not swapped).
Values are shown in KiB by default.
Usage:
psmaps [flags] [pid ...]
Flags:
--help
Print help information.
-w, --wide
Always print the full command line, even if it exceeds the screen width.
-k, --key
Select field to sort output on.
-r, --reverse
Sort in reverse order.
-h, --human-readable
Print sizes in human readable format (e.g. MiB, GiB).
*/
package main
import (
"flag"
"fmt"
"log"
"os"
"strconv"
"strings"
)
const procDir = "/proc"
// returns the list of PIDs of all processes
func allProcesses() []int {
files, err := os.ReadDir(procDir + "/")
if err != nil {
log.Fatal(err)
}
var processes []int
for _, file := range files {
if pid, err := strconv.Atoi(file.Name()); err == nil {
processes = append(processes, pid)
}
}
return processes
}
const flagHelpDescription = "print help information"
const flagWideDescription = "always print full command line"
const flagSortKeyDescription = "field to sort output on"
const flagReverseSortDescription = "sort in reverse order"
const flagHumanReadableDescription = "print sizes in human readable format"
func printUsage() {
fmt.Fprintf(flag.CommandLine.Output(), "Usage: %s [OPTION]... [PID]...\n", os.Args[0])
fmt.Fprintf(flag.CommandLine.Output(), `Options:
--help %s
-w, --wide %s
-k, --key %s
-r, --reverse %s
-h, --human-readable %s
`,
flagHelpDescription,
flagWideDescription,
flagSortKeyDescription,
flagReverseSortDescription,
flagHumanReadableDescription)
}
const (
ExitSuccess = 0
ExitInvalidArguments = 1
)
func main() {
//trace.Start(os.Stderr)
//defer trace.Stop()
// parse command line arguments
var help, wideOutput, reverseOrder, humanReadable bool
var sortKey string
flag.BoolVar(&help, "help", false, flagHelpDescription)
flag.BoolVar(&wideOutput, "wide", false, flagWideDescription)
flag.BoolVar(&wideOutput, "w", false, flagWideDescription)
flag.StringVar(&sortKey, "key", "pid", flagSortKeyDescription)
flag.StringVar(&sortKey, "k", "pid", flagSortKeyDescription)
flag.BoolVar(&reverseOrder, "reverse", false, flagReverseSortDescription)
flag.BoolVar(&reverseOrder, "r", false, flagReverseSortDescription)
flag.BoolVar(&humanReadable, "human-readable", false, flagWideDescription)
flag.BoolVar(&humanReadable, "h", false, flagWideDescription)
flag.Usage = printUsage
flag.Parse()
if help {
printUsage()
os.Exit(ExitSuccess)
}
// validate sort key
allowedSortKeys := map[string]bool{
"pid": true,
"rss": true,
"pss": true,
"uss": true,
"user": true,
"command": true,
}
if !allowedSortKeys[strings.ToLower(sortKey)] {
fmt.Fprintf(os.Stderr, "error: unknown sort key: %s\n", sortKey)
os.Exit(ExitInvalidArguments)
}
// select PIDs
pids := []int{}
args := flag.Args()
if len(args) > 0 {
for i := range args {
pid, err := strconv.Atoi(args[i])
if err == nil && pid > 0 {
pids = append(pids, pid)
}
}
} else {
pids = allProcesses()
}
// dispatch goroutines
pidSmemRollupParserChannelMap := dispatchSmemRollupParsers(pids)
pidOwnerChannelMap := dispatchPidOwners(pids)
comdlineChannelMap := dispatchCmdLineReaders(pids)
// collect results
//rollups := reduceSmemRollupParsers(pidSmemRollupParserChannelMap)
rollups := reduceSmemRollupParsersSelect(pidSmemRollupParserChannelMap)
pidOwnersMap := reducePidOwners(pidOwnerChannelMap)
//pidOwnersMap := reducePidOwnersSelect(pidOwnerChannelMap)
cmdlineMap := reduceCmdLines(comdlineChannelMap)
// sort
sortRollups(rollups, pidOwnersMap, cmdlineMap, sortKey, reverseOrder)
// output
render(rollups, pidOwnersMap, cmdlineMap, wideOutput, humanReadable)
}