# Displaying the load the server is under?

**URL:** <https://www.ruby-forum.com/t/displaying-the-load-the-server-is-under/98543>\
**Category:** Ruby\
**Created:** [July 15, 2006, 10:34pm UTC](https://www.ruby-forum.com/t/displaying-the-load-the-server-is-under/98543 "2006-07-15T22:34:08Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![benjohnson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/3ec8ea/32.png) [@benjohnson](https://www.ruby-forum.com/u/benjohnson)\
**Post date:** [July 15, 2006, 10:34pm UTC](https://www.ruby-forum.com/t/displaying-the-load-the-server-is-under/98543/1 "2006-07-15T22:34:08Z")

</div>

On my program I want to display the load the server is currently  
under. The application I’m writing can get pretty intense and it  
based on a number of factors. I want the user to be able to tell when  
they are using the program beyond the server’s limits. Is this  
possible? Maybe show the percent of the CPU being used, how much ram  
is being used, etc.

Any ideas how the user can go “whoa, maybe I should stop because I’m  
maxing out the server”.

Thanks for your help.

Thank You,  
Ben J.  
E: removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![benjohnson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/3ec8ea/32.png) [@benjohnson](https://www.ruby-forum.com/u/benjohnson)\
**Post date:** [July 15, 2006, 10:44pm UTC](https://www.ruby-forum.com/t/displaying-the-load-the-server-is-under/98543/2 "2006-07-15T22:44:21Z")

</div>

On 15-Jul-06, at 4:31 PM, Ben J. wrote:

> On my program I want to display the load the server is currently  
> under. The application I’m writing can get pretty intense and it  
> based on a number of factors. I want the user to be able to tell  
> when they are using the program beyond the server’s limits. Is this  
> possible? Maybe show the percent of the CPU being used, how much  
> ram is being used, etc.

Not really… If you limit CPU usage to a certain percentage,  
percentage of CPU usage isn’t going to be helpful. If you don’t,  
other factors could (and will) almost always taint that result. The  
same caveat applies to testing memory usage.

Also, the load displayed by the unix ‘w’ program (uptime is usually a  
hard link to this) will display differently based on different  
factors on different unix operating systems. FreeBSD for example uses  
the total number of waiting processes to gauge system load, which may  
not be the most accurate. Calculating load is a difficult beast. If  
perhaps you could tell us what exactly your program does, we may be  
able to offer you better solutions.

> Ben J.

–  
Jeremy T.  
removed\_email\_address@domain.invalid

“One serious obstacle to the adoption of good programming languages  
is the notion that everything has to be sacrificed for speed. In  
computer languages as in life, speed kills.” – Mike Vanier

---

<div class="post-metadata">

**Author:** ![benjohnson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/3ec8ea/32.png) [@benjohnson](https://www.ruby-forum.com/u/benjohnson)\
**Post date:** [July 15, 2006, 11:28pm UTC](https://www.ruby-forum.com/t/displaying-the-load-the-server-is-under/98543/3 "2006-07-15T23:28:46Z")

</div>

The sad thing is that I can’t really say what my program is for,  
because it is confidential. On the other hand, the reason I want to  
do this is because I have a background process running that handles  
the core work of the program. What this background process does is  
create threads that contain while loops that could run forever  
depending on the parameters. What I want to make sure they don’t do  
is create too many threads in this process that they are exceeding  
what the CPU can handle, because if this happens the programs  
performance will suffer and performance is the backbone of this  
software. Also if it gets to that point I’d like to inform them that  
their hardware is constraining the performance of the program.

I wish there was a way to tell them “This program is not optimal  
because the hardware is constraining the performance of this  
application.” Is there any possible way to do this?

Running the “top” command tells me all kinds of information about the  
computer, could something similar to this be used in a ruby application?

Thank You,  
Ben J.  
E: removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![benjohnson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/3ec8ea/32.png) [@benjohnson](https://www.ruby-forum.com/u/benjohnson)\
**Post date:** [July 18, 2006, 7:04pm UTC](https://www.ruby-forum.com/t/displaying-the-load-the-server-is-under/98543/4 "2006-07-18T19:04:12Z")

</div>

On 7/15/06, Ben J. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> [SNIP]
> 
> Running the “top” command tells me all kinds of information about the  
> computer, could something similar to this be used in a ruby application?

Well if top information is all you want ruby can certainly call top as  
a  
system call  
I wrote a little class that might be useful, be careful about  
synchronization though, I have 0 experience with that in ruby! I  
immagine  
one might implement consumer and producer threads with classical CondVar  
design  
anyway it seems to work well 😉

Cheers  
Robert  
----------------------------- 8\< -------------------------------------  
#!/usr/bin/ruby -w

require ‘thread’

class TopData

```
def initialize sleepy=60
    @sleepy = sleepy
    @new = false
    @m = Mutex.new
    @top = Thread.new {
        loop {
            @m.synchronize {
                @data = IO.popen( '/usr/bin/top -bn 1' ).readlines
                @new = true
            }
            sleep @sleepy
        }
    }
end

def new_data?; @new; end

def get_data
    @m.synchronize {
        @new = false
        @data
    }
end

```

end

t = TopData.new 2  
100.times do  
puts t.get\_data  
sleep 2  
end

---

<div class="post-metadata">

**Author:** ![benjohnson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/3ec8ea/32.png) [@benjohnson](https://www.ruby-forum.com/u/benjohnson)\
**Post date:** [July 19, 2006, 6:04am UTC](https://www.ruby-forum.com/t/displaying-the-load-the-server-is-under/98543/5 "2006-07-19T06:04:38Z")

</div>

Ben J. wrote:

> Thanks for your help.
> 
> Thank You,  
> Ben J.  
> E: removed\_email\_address@domain.invalid

See sys-proctable and/or sys-cpu, available on the RAA.

Regards,

Dan
