In linux you find in /proc/PID/status:
Name: exim4
.
.
.
VmSwap: 980 kB
I want to extract the name and the kB's of swap.
For this I use something like:
set regex {}
append regex {^Name:[[:blank:]]+([^\n]+)\n}
append regex {.*}
append regex {\nVmSwap:[[:blank:]]+([[:digit:]]+)[[:blank:]]+kB\n}
set statusF [open /proc/1002/status]
set statusStr [read ${statusF}]
close ${statusF}
set result [regexp ${regex} ${statusStr} match name swap]
Is that a good idea, or could I better implement it in a different
way?
On 2/20/2022 4:17 PM, Rich wrote:
There are also multiple other ways.
As to which is /better/, I suspect that is going to be largely a matter
of taste.
If better means easiest, then this comes to mind:
$ rlwrap -pred tclsh
% set line {}
% catch {set line [exec grep -s VmSwap /proc/1351/status]}
0
% set line
VmSwap: 44852 kB
% lindex $line 1
There are also multiple other ways.
As to which is /better/, I suspect that is going to be largely a matter
of taste.
Cecil Westerhof wrote:
In linux you find in /proc/PID/status:
Name: exim4
.
.
.
VmSwap: 980 kB
I want to extract the name and the kB's of swap.
For this I use something like:[...]
set regex {}
append regex {^Name:[[:blank:]]+([^\n]+)\n}
append regex {.*}
append regex {\nVmSwap:[[:blank:]]+([[:digit:]]+)[[:blank:]]+kB\n}
set statusF [open /proc/1002/status]
set statusStr [read ${statusF}]
close ${statusF}
set result [regexp ${regex} ${statusStr} match name swap]
[...]
There are also multiple other ways.
As to which is /better/, I suspect that is going to be largely a matter
of taste.
Cecil Westerhof <[email protected]> wrote:
In linux you find in /proc/PID/status:
Name: exim4
.
.
.
VmSwap: 980 kB
I want to extract the name and the kB's of swap.
For this I use something like:
set regex {}
append regex {^Name:[[:blank:]]+([^\n]+)\n}
append regex {.*}
append regex {\nVmSwap:[[:blank:]]+([[:digit:]]+)[[:blank:]]+kB\n} >> set statusF [open /proc/1002/status]
set statusStr [read ${statusF}]
close ${statusF}
set result [regexp ${regex} ${statusStr} match name swap]
Is that a good idea, or could I better implement it in a different
way?
That's one way, but not the only way. Another way is something like
this:
proc procget {file} {
set fd [open $file RDONLY]
while {[gets $fd line] != -1} {
switch -regexp -matchvar mv -- $line {
{^Name:[[:blank:]]+([^\n]+)} { set name [lindex $mv 1] }
{^VmSwap:[[:blank:]]+([[:digit:]]+)[[:blank:]]+kB} {
set swap [lindex $mv 1]
close $fd
return [list $name $swap]
}
}
}
close $fd
error "Did not find expected data"
}
There are also multiple other ways.
As to which is /better/, I suspect that is going to be largely a matter
of taste.
In linux you find in /proc/PID/status:
Name: exim4
.
.
.
VmSwap: 980 kB
Is that a good idea, or could I better implement it in a different
way?
On Monday, February 21, 2022 at 1:17:30 AM UTC+1, Rich wrote:
Cecil Westerhof wrote:
In linux you find in /proc/PID/status:[...]
Name: exim4
.
.
.
VmSwap: 980 kB
I want to extract the name and the kB's of swap.
For this I use something like:
set regex {}
append regex {^Name:[[:blank:]]+([^\n]+)\n}
append regex {.*}
append regex {\nVmSwap:[[:blank:]]+([[:digit:]]+)[[:blank:]]+kB\n}
set statusF [open /proc/1002/status]
set statusStr [read ${statusF}]
close ${statusF}
set result [regexp ${regex} ${statusStr} match name swap]
[...]
There are also multiple other ways.
As to which is /better/, I suspect that is going to be largely a matter
of taste.
A few suggestions (beyond RDONLY that Rich suggested):
* there is [fileutil::cat]
* you have not stated which process - /proc/[pid]/status ?
* there is [string cat] instead of multiple [append]s
* depending on future plans, you might want to parse the
key-value-pairs to a dictionary ...
* there is [fileutil::cat]
I will look into that.
* there is [string cat] instead of multiple [append]s
I look into that also.
At the moment I have:
set regex {}
append regex {^Name:[[:blank:]]+([^\n]+)\n}
append regex {.*}
append regex {\nVmSwap:[[:blank:]]+([[:digit:]]+)[[:blank:]]+kB\n}
foreach file [glob /proc/*/status] {
try {
set statusF [open $file RDONLY]
} on error msg {
# Process does not exist anymore
if {${::errorCode} == {POSIX ENOENT {no such file or directory}}} {
continue
}
puts "errorInfo: ${::errorInfo}"
puts "errorCode: ${::errorCode}"
puts "msg: ${msg}"
exit
}
set statusStr [read ${statusF}]
close ${statusF}
set result [regexp ${regex} ${statusStr} match name swap]
if {${result} && ($swap != 0)} {
puts "$name: $swap"
}
}
Am 21.02.22 um 00:48 schrieb Cecil Westerhof:
In linux you find in /proc/PID/status:Use the System, Luke!
Name: exim4
.
.
.
VmSwap: 980 kB
Is that a good idea, or could I better implement it in a different
way?
set swapinfo [ exec grep VmSwap: /proc/$PID/status ]
you work in a known system ( linux : has grep .. )
and it is obvious what the code wants to achieve.
and kiss : one line.
Am 21.02.22 um 12:49 schrieb Cecil Westerhof:
set swapinfo [ exec grep VmSwap: /proc/$PID/status ]
% set swapinfo [ exec egrep VmSwap:\|Name /proc/6239/status ]
Name: lyx
VmSwap: 45868 kB
%
a Billion ways to go from there :-)
Uwe
set swapinfo [ exec grep VmSwap: /proc/$PID/status ]
* Uwe Klein <[email protected]>
| % time { set swapinfo [ exec egrep VmSwap:\|Name /proc/6239/status ]} 100 | 4527.38 microseconds per iteration
% time { set swapinfo [ exec egrep VmSwap:\|Name /proc/3640/status ]} 100 2132.79 microseconds per iteration
proc swapinfo {} {
set res [list]
set pid [pid]
set fd [open /proc/$pid/status]
while {[gets $fd line] >= 0} {
if {[string range $line 0 4] eq "Name:"
|| [string range $line 0 6] eq "VmSwap:"} {
lappend res $line
if {[llength $res] == 2} {
break
}
}
}
close $fd
return $res
}
% time swapinfo 100
59.26 microseconds per iteration
:-)
R'
| Sysop: | Keyop |
|---|---|
| Location: | Huddersfield, West Yorkshire, UK |
| Users: | 715 |
| Nodes: | 16 (2 / 14) |
| Uptime: | 18:41:58 |
| Calls: | 12,103 |
| Calls today: | 3 |
| Files: | 15,004 |
| Messages: | 6,518,083 |