J
Joe Van Dyk
It should be fairly obvious as to what I'm doing (getting information
about a process -- right now just cpu percentage).
Any ideas on how I can improve this code? I'm not a big fan of how
the scanf stuff works.
This is also heavily *nix-biased, and I'm not sure how to properly
work with Windows.
require 'scanf'
class ProcessInfo
def initialize pid
@pid =3D pid
end
def cpu_percentage
# if we don't know the previous time, return 0.0
if @previous_cpu_time.nil? || @previous_time.nil?
@previous_cpu_time =3D get_process_cpu_time
@previous_time =3D Time.now
0.0
else
# Get current and elapsed time
current_time =3D Time.now
elapsed_time =3D current_time - @previous_time
# Get current and elapsed cpu time
current_cpu_time =3D get_process_cpu_time
elapsed_cpu_time =3D current_cpu_time - @previous_cpu_time
# Calculate the percentage used
percentage =3D elapsed_cpu_time / elapsed_time
# Save the current time
@previous_time =3D current_time
@previous_cpu_time =3D current_cpu_time
percentage
end
end
private
# Need to get the process stats
def get_process_cpu_time
stats =3D get_process_stats
stats[:system_cpu_time] + stats[:user_cpu_time]
end
# See 'man proc' for details.
def get_process_stats
proc_stats =3D File.read("/proc/#{ @pid }/stat")
# EEWWWW -- gets the stuff from /proc/<pid>/stat and puts it into vars
pid, comm, state, ppid, pgrp, session, tty_nr, tpgid, flags,
minflt, cminflt, majflt, cmajflt, utime, stime, cutime,
cstime, priority, nice, who_cares, itrealvade,
starttime, vsize, rss, rlim, startcode, endcode, startstack,
kstkesp, kstkeip, signal, blocked, sigignore, sigcatch,
wchan, nswap, cnswap, exit_signal, processor =3D
proc_stats.scanf("%d %s %c %d %d %d %d %d %d %d \
%d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d \
%d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d")
# Just put the ones I need into the hash
stats =3D {}
stats[:system_cpu_time] =3D stime
stats[:user_cpu_time] =3D utime
stats[rocessor] =3D processor
stats
end
end
# example usage
fail "hey, specify a process id please!" if ARGV.size !=3D 1
pid =3D ARGV.shift
c =3D ProcessInfo.new pid
loop do
puts "current cpu percentage =3D #{ c.cpu_percentage }"
sleep 3
end
about a process -- right now just cpu percentage).
Any ideas on how I can improve this code? I'm not a big fan of how
the scanf stuff works.
This is also heavily *nix-biased, and I'm not sure how to properly
work with Windows.
require 'scanf'
class ProcessInfo
def initialize pid
@pid =3D pid
end
def cpu_percentage
# if we don't know the previous time, return 0.0
if @previous_cpu_time.nil? || @previous_time.nil?
@previous_cpu_time =3D get_process_cpu_time
@previous_time =3D Time.now
0.0
else
# Get current and elapsed time
current_time =3D Time.now
elapsed_time =3D current_time - @previous_time
# Get current and elapsed cpu time
current_cpu_time =3D get_process_cpu_time
elapsed_cpu_time =3D current_cpu_time - @previous_cpu_time
# Calculate the percentage used
percentage =3D elapsed_cpu_time / elapsed_time
# Save the current time
@previous_time =3D current_time
@previous_cpu_time =3D current_cpu_time
percentage
end
end
private
# Need to get the process stats
def get_process_cpu_time
stats =3D get_process_stats
stats[:system_cpu_time] + stats[:user_cpu_time]
end
# See 'man proc' for details.
def get_process_stats
proc_stats =3D File.read("/proc/#{ @pid }/stat")
# EEWWWW -- gets the stuff from /proc/<pid>/stat and puts it into vars
pid, comm, state, ppid, pgrp, session, tty_nr, tpgid, flags,
minflt, cminflt, majflt, cmajflt, utime, stime, cutime,
cstime, priority, nice, who_cares, itrealvade,
starttime, vsize, rss, rlim, startcode, endcode, startstack,
kstkesp, kstkeip, signal, blocked, sigignore, sigcatch,
wchan, nswap, cnswap, exit_signal, processor =3D
proc_stats.scanf("%d %s %c %d %d %d %d %d %d %d \
%d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d \
%d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d")
# Just put the ones I need into the hash
stats =3D {}
stats[:system_cpu_time] =3D stime
stats[:user_cpu_time] =3D utime
stats[rocessor] =3D processor
stats
end
end
# example usage
fail "hey, specify a process id please!" if ARGV.size !=3D 1
pid =3D ARGV.shift
c =3D ProcessInfo.new pid
loop do
puts "current cpu percentage =3D #{ c.cpu_percentage }"
sleep 3
end