• Sorting on size and then name

    From Cecil Westerhof@21:1/5 to All on Mon Feb 21 10:01:24 2022
    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?

    --
    Cecil Westerhof
    Senior Software Engineer
    LinkedIn: http://www.linkedin.com/in/cecilwesterhof

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Cecil Westerhof@21:1/5 to Cecil Westerhof on Mon Feb 21 11:23:58 2022
    Cecil Westerhof <Cecil@decebal.nl> writes:

    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?

    To make things a bit more clear, I have the following program:
    package require fileutil


    set regex [string cat \
    {^Name:[[:blank:]]+([^\n]+)\n} \
    {.*} \
    {\nVmSwap:[[:blank:]]+([[:digit:]]+)[[:blank:]]+kB\n} \
    ]


    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)} {
    puts "$name: $swap"
    }
    }

    Now I output the values 'randomly'.
    I would prefer them to be sorted.

    --
    Cecil Westerhof
    Senior Software Engineer
    LinkedIn: http://www.linkedin.com/in/cecilwesterhof

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Ralf Fassel@21:1/5 to All on Mon Feb 21 11:37:47 2022
    * Cecil Westerhof <Cecil@decebal.nl>
    | 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).

    R'

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Cecil Westerhof@21:1/5 to Ralf Fassel on Mon Feb 21 13:05:44 2022
    Ralf Fassel <ralfixx@gmx.de> writes:

    * Cecil Westerhof <Cecil@decebal.nl>
    | 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)