* displaying top cpu using process
@ 2001-04-07 14:33 Dominik Vogt
2001-04-07 18:26 ` Bart Schaefer
0 siblings, 1 reply; 4+ messages in thread
From: Dominik Vogt @ 2001-04-07 14:33 UTC (permalink / raw)
To: zsh-users
I want to have a button in the button bar of my window manager
(fvwm with FvwmButtons) that displays the process that currently
uses most CPU. The information I want to display is the process
id, used CPU and the command line of the process, e.g.
12345 99.0% netscape
The part dealing with X and the window manager is not the problen,
althoug it is not easy to do. The problem is that it is
surprisingly difficult to get said information (SuSE 7.1 Linux
with 2.4.0 kernel)
I tried out about a dozen different programs that display load or
process information, and the only candidates I came across are ps
and top.
ps:
ps gives me complete control over the output format, but I can't
make it give me the CPU percentage. Although the 'C' output
modifier is documented as
"use raw CPU time for %CPU instead of decaying average"
I always get the decaying average. Because of this, most of the
time I see that X uses most CPU.
top:
Top provides all the output I need, but it is *very* cumbersome
to parse. Okay, I have an (inefficient) shell script that
extracts the information:
top -b -n 1 | sed '<script>'
But then, this only works if the terminal window from which top
was started has enough lines. If its too short, top generates
only the header or no lines at all. This is certainly a strange
notion of a batch mode (b option). To get the full output I
have to detach top from the terminal. In other words, I need
one process that generates the data and one that writes it into
the terminal window. There are several ways to do this, but
the best solution seems to be a coprocess (because the coprocess
is killed if the terminal window that runs the script is
closed).
Of course this all wastes a lot of cpu and memory and is very
ugly to write.
Can anybody think of a better solution (that does not include
writing any C code)?
Bye
Dominik ^_^ ^_^
--
Dominik Vogt, dominik.vogt@gmx.de
Reply-To: dominik.vogt@gmx.de
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: displaying top cpu using process
2001-04-07 14:33 displaying top cpu using process Dominik Vogt
@ 2001-04-07 18:26 ` Bart Schaefer
2001-04-08 12:49 ` Dominik Vogt
0 siblings, 1 reply; 4+ messages in thread
From: Bart Schaefer @ 2001-04-07 18:26 UTC (permalink / raw)
To: dominik.vogt, zsh-users
On Apr 7, 4:33pm, Dominik Vogt wrote:
} Subject: displaying top cpu using process
}
} ps gives me complete control over the output format, but I can't
} make it give me the CPU percentage. Although the 'C' output
} modifier is documented as
}
} "use raw CPU time for %CPU instead of decaying average"
}
} I always get the decaying average.
I can't even get my "ps" to accept the C modifier. It keeps interpreting
it as the "selection by command name" option, and telling me that only
root is allowed to do that.
} top -b -n 1 | sed '<script>'
}
} But then, this only works if the terminal window from which top
} was started has enough lines. [...] To get the full output I
} have to detach top from the terminal.
That's odd; on RH6.2, for me, "top -b -n 1" produces a lot more lines
of output than the height of the terminal. (procps-2.0.6-5) However,
try fooling it by stuffing the LINES and COLUMNS environment variables.
} In other words, I need one process that generates the data and one
} that writes it into the terminal window. There are several ways to
} do this, but the best solution seems to be a coprocess (because the
} coprocess is killed if the terminal window that runs the script is
} closed).
A coprocess is overkill. Just use the parameter assignment and a pipe:
topcpu {
local p=0 pid user pri ni size rss share stat lib cpu mem time command
LINES=10 COLUMNS=200 top d 2 c b n 43200 |
while read pid user pri ni size rss share stat lib cpu mem time command
do
[[ $pid == PID ]] && { p=1; continue }
(( p )) && { print $pid $cpu% $command; p=0 }
done
}
43200 iterations of 2 sec is 24 hours.
} Can anybody think of a better solution (that does not include
} writing any C code)?
Presumably it's possible to compute the CPU percentages by reading from
/proc/<->/stat plus the cpu line from /proc/stat, but I don't know the
algorithm top uses.
--
Bart Schaefer Brass Lantern Enterprises
http://www.well.com/user/barts http://www.brasslantern.com
Zsh: http://www.zsh.org | PHPerl Project: http://phperl.sourceforge.net
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: displaying top cpu using process
2001-04-07 18:26 ` Bart Schaefer
@ 2001-04-08 12:49 ` Dominik Vogt
2001-04-08 17:04 ` Bart Schaefer
0 siblings, 1 reply; 4+ messages in thread
From: Dominik Vogt @ 2001-04-08 12:49 UTC (permalink / raw)
To: zsh-users
On Sat, Apr 07, 2001 at 06:26:01PM +0000, Bart Schaefer wrote:
> On Apr 7, 4:33pm, Dominik Vogt wrote:
> } Subject: displaying top cpu using process
> }
> } ps gives me complete control over the output format, but I can't
> } make it give me the CPU percentage. Although the 'C' output
> } modifier is documented as
> }
> } "use raw CPU time for %CPU instead of decaying average"
> }
> } I always get the decaying average.
>
> I can't even get my "ps" to accept the C modifier. It keeps interpreting
> it as the "selection by command name" option, and telling me that only
> root is allowed to do that.
I remember that it was very difficult to make ps accept its
options, but it is possible. The man page is really crap. I
don't have the line handy right now, but I can look it up on
monday.
> } top -b -n 1 | sed '<script>'
> }
> } But then, this only works if the terminal window from which top
> } was started has enough lines. [...] To get the full output I
> } have to detach top from the terminal.
>
> That's odd; on RH6.2, for me, "top -b -n 1" produces a lot more lines
> of output than the height of the terminal.
Yes, it produces full output as long as the terminal window has at
least eight lines. If the window is smaller, top only writes the
headers but no process information.
> (procps-2.0.6-5) However,
> try fooling it by stuffing the LINES and COLUMNS environment variables.
Yes, that's it. Setting LINES to a big value makes top produce
full output. Now, how can I protect LINES from being reset when
the window is resized?
> } In other words, I need one process that generates the data and one
> } that writes it into the terminal window. There are several ways to
> } do this, but the best solution seems to be a coprocess (because the
> } coprocess is killed if the terminal window that runs the script is
> } closed).
>
> A coprocess is overkill. Just use the parameter assignment and a pipe:
Yes, with a way to make top produce output in a 20x1 window a
background process is not necessary.
> topcpu {
> local p=0 pid user pri ni size rss share stat lib cpu mem time command
> LINES=10 COLUMNS=200 top d 2 c b n 43200 |
> while read pid user pri ni size rss share stat lib cpu mem time command
> do
> [[ $pid == PID ]] && { p=1; continue }
> (( p )) && { print $pid $cpu% $command; p=0 }
> done
> }
Unfortunately the read command above will not work reliably which
complicates things a lot. The number of space separated words in
the top output varies:
354 luthien 8 0 2000 2000 1376 S 0.0 1.5 0:00 zsh
525 luthien 13 5 1148 1148 932 S N 0.0 0.9 0:00 startx
^^^
> 43200 iterations of 2 sec is 24 hours.
My solution looks like this:
---------------------------------------------------------------
#!/usr/bin/zsh
emulate zsh
setopt EXTENDED_GLOB
function topcpu ()
{
local p=0 pid rest
LINES=10 COLUMNS=200 top -d 2 -c -b -s -n 1 |
while read pid rest; do
[[ $pid == PID ]] && { p=1; continue }
(( p )) && {
if [ ! $rest[57,60] = "top " ]; then
print -n "\n"$pid ${rest[44,48]## ##}% ${rest[57,99]}
fi
return
}
done
}
topcpu
if [ -n $1 ]; then
while true; do
sleep $1 2> /dev/null
topcpu
done
fi
---------------------------------------------------------------
To circumvent the problem of the window being resized, the
function is called in a loop with a single pass of top each.
I call it from xterm:
xterm -g 20x1 -font 5x7 +sb +aw +ah -e topproc 15
Unfortunately I can't make the cursor the same colour as the
window background. Although xterm allows to set the colour, it
ignores the setting if it's identical to the background colour.
I guess I'll have to live with that (it *is* possible to solve,
even with the themes setup in fvwm, but it's not worth the
effort).
Bye
Dominik ^_^ ^_^
--
Dominik Vogt, dominik.vogt@gmx.de
Reply-To: dominik.vogt@gmx.de
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: displaying top cpu using process
2001-04-08 12:49 ` Dominik Vogt
@ 2001-04-08 17:04 ` Bart Schaefer
0 siblings, 0 replies; 4+ messages in thread
From: Bart Schaefer @ 2001-04-08 17:04 UTC (permalink / raw)
To: dominik.vogt, zsh-users
On Apr 8, 2:49pm, Dominik Vogt wrote:
} Subject: Re: displaying top cpu using process
}
} > LINES=10 COLUMNS=200 top d 2 c b n 43200 |
} > while read pid user pri ni size rss share stat lib cpu mem time command
}
} 525 luthien 13 5 1148 1148 932 S N 0.0 0.9 0:00 startx
} ^^^
Urk, I forgot about that. Your solution is probably the best for this
particular applicaton, but in case you actually want to parse it:
... | while read pid user pri ni size rss share stat rest
do
rest=($=rest)
[[ $rest[1] == <->* ]] || { stat="$stat $rest[1]" ; shift rest }
lib=$rest[1] # Your output does not have this item? Renumber.
cpu=$rest[2]
mem=$rest[3]
time=$rest[4]
command=$rest[5,-1]
...
done
I don't think top ever produces more than 2 words in the STAT column,
does it?
--
Bart Schaefer Brass Lantern Enterprises
http://www.well.com/user/barts http://www.brasslantern.com
Zsh: http://www.zsh.org | PHPerl Project: http://phperl.sourceforge.net
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2001-04-08 17:04 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2001-04-07 14:33 displaying top cpu using process Dominik Vogt
2001-04-07 18:26 ` Bart Schaefer
2001-04-08 12:49 ` Dominik Vogt
2001-04-08 17:04 ` Bart Schaefer
Code repositories for project(s) associated with this public inbox
https://git.vuxu.org/mirror/zsh/
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).