Ralf Fassel <
[email protected]> writes:
* Cecil Westerhof <[email protected]>
| I am thinking about writing a little program to show processes that
| use swap.
| For this I will get name and amount of swap of every process that uses
| swap.
| I want to show them with biggest swap first. When the same amount is
| used it should be sorted on name.
| What would be the best data-structure for this?
A list with {name size} elements. Since lsort has stable sort, you
first sort on name (lsort -index 0), then on size (lsort -index 1).
I was already walking on this path, but the missing piece was that
lsort has a stable sort.
I now have:
package require fileutil
set regex [string cat \
{^Name:[[:blank:]]+([^\n]+)\n} \
{.*} \
{\nVmSwap:[[:blank:]]+([[:digit:]]+)[[:blank:]]+kB\n} \
]
set swapDict [dict create]
foreach file [glob /proc/*/status] {
try {
set statusStr [::fileutil::cat $file]
} on error msg {
# Process does not exist anymore
if {${::errorCode} == {NONE}} {
continue
}
puts "errorInfo: ${::errorInfo}"
puts "errorCode: ${::errorCode}"
puts "msg: ${msg}"
exit
}
set result [regexp ${regex} ${statusStr} match name swap]
if {${result} && ($swap != 0)} {
dict incr swapDict $name $swap
}
}
set swapList {}
dict for {name swap} ${swapDict} {
lappend swapList [list $name $swap]
}
set swapList [lsort -index 0 ${swapList}]
set swapList [lsort -index 1 -integer -decreasing ${swapList}]
foreach swapElement ${swapList} {
puts [format "%-15s: %8d" {*}${swapElement}]
}
Thanks.
--
Cecil Westerhof
Senior Software Engineer
LinkedIn:
http://www.linkedin.com/in/cecilwesterhof
--- SoupGate-Win32 v1.05
* Origin: fsxNet Usenet Gateway (21:1/5)