Hi,
I was posting a block of code in a discussion when I saw
the code is not correctly represented in the Forum.
I put here the code in a CODE block and also in a file.
Comparing the two you will see that e.g. this part is not correct
in CODE block.
====
(0..4).each do |i|
....
end
====
I was posting a block of code in a discussion when I saw
the code is not correctly represented in the Forum.
I put here the code in a CODE block and also in a file.
Comparing the two you will see that e.g. this part is not correct
in CODE block.
====
(0..4).each do |i|
....
end
====
Code:
while true do
st1 = `sysctl kern.cp_times`
st1.chomp!
vals1 = st1.split(/ /); vals1.shift
sleep 1
st2 = `sysctl kern.cp_times`
st2.chomp!
vals2 = st2.split(/ /); vals2.shift
diffs = [-1,-1,-1,-1,-1]
(0..4).each do |i|
diffs = vals2.to_f - vals1.to_f
end
tot = diffs.sum
diffsPerc = [-1,-1,-1,-1,-1]
(0..4).each do |i|
diffsPerc = (diffs / tot) * 100.0
end
userv = "%.1f" % diffsPerc[0]; nicev = "%.1f" % diffsPerc[1]
systemv = "%.1f" % diffsPerc[2]; interv = "%.1f" % diffsPerc[3]
idlev = "%.1f" % diffsPerc[4]
puts "CPU: #{userv}% user, #{nicev}% nice, #{systemv}% system, " +
"#{interv}% interrupt, #{idlev}% idle"
end