List Info

Thread: Server Stats




Server Stats
country flaguser name
United States
2007-04-12 20:42:29
I'm looking for a way to determine the in-use server stats
for a
cherrypy server.  I'd like to tune the number of threads
launched for
an app and would like to query the server to see what the
peak usage
is.  Is there a way to inspect how many threads are busy at
any given
time?  Thanks,


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "cherrypy-users" group.
To post to this group, send email to cherrypy-usersgooglegroups.com
To unsubscribe from this group, send email to
cherrypy-users-unsubscribegooglegroups.com
For more options, visit this group at h
ttp://groups.google.com/group/cherrypy-users?hl=en
-~----------~----~----~----~------~----~------~--~---


Re: Server Stats
country flaguser name
United States
2007-04-13 02:01:21
Hi got_milk,

> I'm looking for a way to determine the in-use server
stats for a
> cherrypy server.  I'd like to tune the number of
threads launched for
> an app and would like to query the server to see what
the peak usage
> is.  Is there a way to inspect how many threads are
busy at any given
> time?

I don't know if there's anything in CherryPy itself which
will do this
(I'm sure the more experienced folks can answer that), but I
actually
wrote some code this morning which you might find useful. 
This
"thread inspection" code checks all the running
threads, and prints a
stack trace from each running thread to a temporary file on
disk.
I've set this up to run automatically every second -- you
can probably
adapt this to do what you want.  For example, you could
check the last
line of the stack trace to see if it contains the text
"waiter.acquire()", indicating that it's an unused
CherryPy thread
(that may not be perfect, but based on my testing it looks
like it'd
work).  You could then count up the number of unused
threads, and
write that number to a disk file.  Leave this running while
your
server does its thing, and then scan the file to see how the
number
varies over time.  Getting the number of busy threads is
trivial --
just subtract the number of free threads from the total
number of
threads you've specified in your
"server.thread_pool" CherryPy config
setting.

Anyway, this is just something I cobbled together this
morning to
figure out why CherryPy wasn't shutting down cleanly.  Hope
you find
it useful...let me know if you need help updating it to
calculate the
number of unused CherryPy threads.

 - Erik.

#########################################################

# The following code periodically inspects the running
threads,
dumping
# a stack trace from each active thread to a temporary file
on disk
once
# a second.  This can be used to see what the various
threads are
doing.

import sys
import thread
import threading
import time
import traceback

def start_checking():
    global _stopThread
    _stopThread = False
    t = threading.Thread(target=_doThread)
    t.start()

def stop_checking():
    global _stopThread
    _stopThread = True

def _doThread():
    global _stopThread

    fName = "thread-dump.txt"

    f = file(fName, "w") # Start with an empty
file.
    f.close()

    while not _stopThread:
        f = file(fName, "a")
       
f.write("==============================================
====
n")
        f.write("n")

        for threadID,frame in
sys._current_frames().items():
            if threadID == thread.get_ident(): continue #
Ignore our
own thread
            f.write("Thread " + str(threadID) +
":n")
            f.write("n")
            traceback.print_stack(frame, None, f)
            f.write("n")

        f.close()
        time.sleep(1.0)

#########################################################


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "cherrypy-users" group.
To post to this group, send email to cherrypy-usersgooglegroups.com
To unsubscribe from this group, send email to
cherrypy-users-unsubscribegooglegroups.com
For more options, visit this group at h
ttp://groups.google.com/group/cherrypy-users?hl=en
-~----------~----~----~----~------~----~------~--~---


Re: Server Stats
country flaguser name
United States
2007-04-14 16:43:45
That's very close to what I'm looking for, but it only works
with
python 2.5  which offers sys._current_frames()  I was hoping
for
something internal to cherrypy.


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "cherrypy-users" group.
To post to this group, send email to cherrypy-usersgooglegroups.com
To unsubscribe from this group, send email to
cherrypy-users-unsubscribegooglegroups.com
For more options, visit this group at h
ttp://groups.google.com/group/cherrypy-users?hl=en
-~----------~----~----~----~------~----~------~--~---


Re: Server Stats
country flaguser name
United States
2007-04-15 16:27:05
Hi got_milk,

> That's very close to what I'm looking for, but it only
works with
> python 2.5  which offers sys._current_frames()  I was
hoping for
> something internal to cherrypy.

Ah yes, you're right: sys._current_frames only works for
Python 2.5 --
I wasn't aware of that.  I don't know if there is anything
built into
CherryPy which does what you want -- maybe someone else can
suggest
something here.

If you're not running Python 2.5, I'd say that your only
real chance
is to modify your copy of cherrypy/wsgiserver/__init__.py so
that the
WorkerThread's run() method sets an internal variable when
processing
a response, like this:

class WorkerThread(threading.Thread):
    def __init__(self, server):
        self.ready = False
        self.server = server
        threading.Thread.__init__(self)
+        self.worker_busy = threading.Event()

    def run(self):
        try:
            self.ready = True
            while True:
                conn = self.server.requests.get()
                if conn is _SHUTDOWNREQUEST:
                    return

                try:
+                    self.worker_busy.set()
                    conn.communicate()
                finally:
+                    self.worker_busy.clear()
                    conn.close()
        except (KeyboardInterrupt, SystemExit), exc:
            self.server.interrupt = exc

(I've marked the altered lines with a "+" -- hope
this comes through
okay).

With this change in place, you could then do something like
this
(warning: I haven't tested this code, so it may have syntax
errors,
etc):

    numBusy = 0
    for thread in threading.enumerate():
        if thread.getName().startswith("CP
WSGIServer"):
            if thread.worker_busy.isSet():
                numBusy = numBusy + 1

If you stick this code in a backround thread that's run
periodically,
you should be able to get an idea of how many worker threads
are being
used over time.  And it should work with any
reasonably-recent version
of Python, not just Python 2.5.

Hope this helps...

 - Erik.


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "cherrypy-users" group.
To post to this group, send email to cherrypy-usersgooglegroups.com
To unsubscribe from this group, send email to
cherrypy-users-unsubscribegooglegroups.com
For more options, visit this group at h
ttp://groups.google.com/group/cherrypy-users?hl=en
-~----------~----~----~----~------~----~------~--~---


Re: Server Stats
country flaguser name
United States
2007-04-16 01:37:36
Erik Westra wrote:
> ...modify your copy of cherrypy/wsgiserver/__init__.py
> so that the WorkerThread's run() method sets an
internal
> variable when processing a response...

That could work, but relies on the deployer using the
builtin WSGI server, so it wouldn't work if you're deploying
with mod_python, for example, or with someone else's WSGI
server.

A more portable solution would take the same approach that
engine.on_start_thread_list does: maintain a map of the form
{thread id: thread metadata}. This could be done for server
stats with a pair of on_start_resource/on_end_resource
hooks. Here's a CP3 Tool which does that: http://too
ls.cherrypy.org/wiki/StatusTool.


Robert Brewer
System Architect
Amor Ministries
fumanchuamor.org



--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "cherrypy-users" group.
To post to this group, send email to cherrypy-usersgooglegroups.com
To unsubscribe from this group, send email to
cherrypy-users-unsubscribegooglegroups.com
For more options, visit this group at h
ttp://groups.google.com/group/cherrypy-users?hl=en
-~----------~----~----~----~------~----~------~--~---


  
Re: Server Stats
country flaguser name
United States
2007-04-16 03:34:12
Hi Robert,

> That could work, but relies on the deployer using the
builtin WSGI server, so it wouldn't work if you're deploying
with mod_python, for example, or with someone else's WSGI
server.
>
> A more portable solution would take the same approach
that engine.on_start_thread_list does: maintain a map of the
form {thread id: thread metadata}. This could be done for
server stats with a pair of
on_start_resource/on_end_resource hooks. Here's a CP3 Tool
which does that:http://too
ls.cherrypy.org/wiki/StatusTool.

Ah, excellent -- I was hoping that someone who really
understood the
CherryPy internals would come up with a better solution than
my
kludge.  Thanks!

 - Erik.


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "cherrypy-users" group.
To post to this group, send email to cherrypy-usersgooglegroups.com
To unsubscribe from this group, send email to
cherrypy-users-unsubscribegooglegroups.com
For more options, visit this group at h
ttp://groups.google.com/group/cherrypy-users?hl=en
-~----------~----~----~----~------~----~------~--~---


Re: Server Stats
country flaguser name
United Kingdom
2007-04-16 03:45:52
Erik Westra wrote:
> Hi Robert,
> 
>> That could work, but relies on the deployer using
the builtin WSGI server, so it wouldn't work if you're
deploying with mod_python, for example, or with someone
else's WSGI server.
>>
>> A more portable solution would take the same
approach that engine.on_start_thread_list does: maintain a
map of the form {thread id: thread metadata}. This could be
done for server stats with a pair of
on_start_resource/on_end_resource hooks. Here's a CP3 Tool
which does that:http://too
ls.cherrypy.org/wiki/StatusTool.
> 
> Ah, excellent -- I was hoping that someone who really
understood the
> CherryPy internals would come up with a better solution
than my
> kludge.  Thanks!

That's definitely a nice tool 
Clean and simple.

- Sylvain

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "cherrypy-users" group.
To post to this group, send email to cherrypy-usersgooglegroups.com
To unsubscribe from this group, send email to
cherrypy-users-unsubscribegooglegroups.com
For more options, visit this group at h
ttp://groups.google.com/group/cherrypy-users?hl=en
-~----------~----~----~----~------~----~------~--~---


[1-7]

about | contact  Other archives ( Real Estate discussion Medical topics )