You are viewing a plain text version of this content. The canonical link for it is here.
Posted to modperl@perl.apache.org by EXP <ex...@3dwars.de> on 2001/09/26 20:41:27 UTC

Restricting cpu time for mod_perl scripts? AND Reevaluating perl scripts under mod_perl.

Hi

I am currently using Apache::Resource to limit the maximum amount of ram 
the apache childs are allowed to use.
However, I can't really use PERL_RLIMIT_CPU because it is kind of pointless 
to kill every apache child that reaches
this limit. I need a way to restrict cpu time on a per script or per run 
basis. But I am not sure if this is possible as
far as I understand the mod_perl layout.
Anybody did this? Or any suggestions?

My second problem is related to this one. How can I add perlcode via 
httpd.conf (PerlModule, ...) which is
reevaluated at EVERY request? I am thinking about adding some own perl code 
which sets an alarm an
checks every now an than how much cpu time the currently running perl 
script allready got (by substracting
the value of consumed cpu time at the start of the script from the current 
value) and execute a die() when
a certain maximum is reached. In theorie (if there are no mod_perl related 
problems with this, the user doesn't
use the alarm function in his scripts and doesn't specify any handlers for 
the die signal) this could work.

Some other probleme here. How do I tell mod_perl to execute abc.pl inside 
the current scope but without
saving the compiled code or any variables? I could do a system("abc.pl"); 
but that wouldn't allow me access to
the variables I need. A simple require abc.pl works but mod_perl then 
caches everything in ram.
I thought maybe an eval { require abc.pl }; would do it but it seems to be 
exactly the same as without the eval.
Is there a way to do this?
Example problem:

test.pl
...
$somepackage::somevar = 1;
...
system("abc.pl");       #doesn't cache it but doesn't allow accessing the 
var either
#OR
require abc.pl          #caches and bloats the apache process but allows 
accessing the var
#OR
eval {
         require abc.pl;
};                      #exactly the same as withoit eval
#OR
????                    #what did I miss?
...


abc.pl
...
if ($somepackage::somevar == 1) {
...

Thanks!


BYe!
EXP

3DWARS.de - Your resource for 3Dimensional Warfare
   http://www.3DWARS.de

PS: Big Brother IS Watching You!


Re: Restricting cpu time for mod_perl scripts? AND Reevaluating perl scripts under mod_perl.

Posted by Perrin Harkins <pe...@elem.com>.
> I am currently using Apache::Resource to limit the maximum amount of ram
> the apache childs are allowed to use.

It would be better to use Apache::SizeLimit for that.  It's more flexible
and allows requests to complete before shutting down the process.
Apache::Resource is more useful as a safety measure to catch runaway
processes.

> However, I can't really use PERL_RLIMIT_CPU because it is kind of
pointless
> to kill every apache child that reaches
> this limit. I need a way to restrict cpu time on a per script or per run
> basis. But I am not sure if this is possible as
> far as I understand the mod_perl layout.
> Anybody did this? Or any suggestions?

That isn't possible without some modification to apache.  You would need to
be able to check the CPU consumed in some asynchronous way as the request
was processed, and there's no provision for that in the current apache
process model.

> My second problem is related to this one. How can I add perlcode via
> httpd.conf (PerlModule, ...) which is
> reevaluated at EVERY request?

All you need is a handler.  Every request can run as many handlers as you
configure in each stage.  If you're asking how to assign a handler globally
for every request, just use <LocationMatch *> or something similar.

> I am thinking about adding some own perl code
> which sets an alarm an
> checks every now an than how much cpu time the currently running perl
> script allready got

I don't think you can do that.  There are warnings in perlipc about how
having a complex alarm handler can cause segfaults.

Honestly, if all you're trying to do is keep coding mistakes from crashing
your server with a tight loop, just set some reasonable limit with
Apache::Resource.  When there's no problem, apache children don't generally
use much CPU at all.  That means runaways really stand out and use up their
allotted CPU limit quickly.

> Some other probleme here. How do I tell mod_perl to execute abc.pl inside
> the current scope but without
> saving the compiled code or any variables?

You can't.  You can remove the entries from the symbol table after running
it (see Apache::PerlRun for an exmaple), but why do you want to do this?  If
you're just trying to save RAM, load the script at startup.  If it's huge
and you don't usually need it, tell the current process to exit when it's
finished with this request.

- Perrin