1
# Movable Type (r) Open Source (C) 2001-2008 Six Apart, Ltd.
1
# Movable Type (r) Open Source (C) 2001-2009 Six Apart, Ltd.
2
2
# This program is distributed under the terms of the
3
3
# GNU General Public License, version 2.
5
# $Id: TheSchwartz.pm 2769 2008-07-14 19:56:22Z bchoate $
5
# $Id: TheSchwartz.pm 3733 2009-05-18 20:27:46Z jmarcotte $
7
7
package MT::TheSchwartz;
10
10
use base qw( TheSchwartz );
11
use MT::ObjectDriver::Driver::DBI;
11
use MT::ObjectDriver::Driver::Cache::RAM;
12
12
use List::Util qw( shuffle );
129
sub _has_enough_swap {
132
require Sys::MemInfo;
133
$memory_module = q{Sys::MemInfo};
136
my ($mem_limit) = @_;
137
if ( !defined($mem_limit) ) {
138
$mem_limit = MT->config('SchwartzSwapMemoryLimit');
141
if ( $mem_limit && $memory_module ) {
143
if ( $mem_limit =~ /^\d+[KGM]B?$/ ) {
145
( $mem_limit =~ /GB?$/ ) and $multiplier = 1073741824;
146
( $mem_limit =~ /MB?$/ ) and $multiplier = 1048576;
147
( $mem_limit =~ /KB?$/ ) and $multiplier = 1024;
148
$mem_limit =~ s/[KGM]B?$//;
149
$mem_limit = $mem_limit * $multiplier;
151
if ( $mem_limit =~ /\d+/ ) {
153
if ( $memory_module eq q{Sys::MemInfo} ) {
154
$swap = Sys::MemInfo::get("freeswap");
157
# not enough swap, lets get out of here!
158
if ( $swap < $mem_limit ) {
164
# default to returning true
165
# i.e., yes there is enough
169
sub _has_enough_memory {
172
require Sys::MemInfo;
173
$memory_module = q{Sys::MemInfo};
176
my ($mem_limit) = @_;
177
if ( !defined($mem_limit) ) {
178
$mem_limit = MT->config('SchwartzFreeMemoryLimit');
181
if ( $mem_limit && $memory_module ) {
183
if ( $mem_limit =~ /^\d+[KGM]B?$/ ) {
185
( $mem_limit =~ /GB?$/ ) and $multiplier = 1073741824;
186
( $mem_limit =~ /MB?$/ ) and $multiplier = 1048576;
187
( $mem_limit =~ /KB?$/ ) and $multiplier = 1024;
188
$mem_limit =~ s/[KGM]B?$//;
189
$mem_limit = $mem_limit * $multiplier;
191
if ( $mem_limit =~ /\d+/ ) {
193
if ( $memory_module eq q{Sys::MemInfo} ) {
194
$free = Sys::MemInfo::get("freemem");
197
# not enough free, lets get out of here!
198
if ( $free < $mem_limit ) {
204
# default to returning true
205
# i.e., yes there is enough
129
209
# Replacement for TheSchwartz::get_server_time
130
210
# to simply return value from dbd->sql_for_unixtime
131
211
# if it is a plain number (the driver has no function,
135
215
my($driver) = @_;
136
216
my $unixtime_sql = $driver->dbd->sql_for_unixtime;
137
217
return $unixtime_sql if $unixtime_sql =~ m/^\d+$/;
138
return $driver->rw_handle->selectrow_array("SELECT $unixtime_sql");
218
return $driver->r_handle->selectrow_array("SELECT $unixtime_sql");
221
sub work_until_done {
222
my TheSchwartz $client = shift;
226
my $cap = MT->config('SchwartzClientDeadline'); # in seconds
227
my $mem_limit = MT->config('SchwartzFreeMemoryLimit');
229
my $swap_limit = MT->config('SchwartzSwapMemoryLimit');
233
$deadline = time() + $cap;
234
while ( time() < $deadline ) {
235
$client->work_once or last;
236
last unless _has_enough_memory( $mem_limit );
237
last unless _has_enough_memory( $swap_limit );
242
$client->work_once or last;
243
last unless _has_enough_memory( $mem_limit );
244
last unless _has_enough_swap( $swap_limit );
141
249
sub work_periodically {
171
my $driver = MT::Object->driver;
173
if $driver->can('clear_cache');
280
MT::ObjectDriver::Driver::Cache::RAM->clear_cache;
174
282
MT->request->reset();
176
285
if ($OBJECT_REPORT) {
177
286
my $report = leak_report(\%obj_start, \%obj_pre, \%Devel::Leak::Object::OBJECT_COUNT);