2012-04-13 10:38:12 +00:00
|
|
|
<?php
|
|
|
|
|
2022-07-30 18:52:37 +00:00
|
|
|
namespace MediaWiki\Extension\Scribunto\Engines\LuaStandalone;
|
|
|
|
|
|
|
|
use Exception;
|
2022-08-03 12:31:15 +00:00
|
|
|
use MediaWiki\Extension\Scribunto\Engines\LuaCommon\LuaEngine;
|
2018-02-04 18:57:59 +00:00
|
|
|
use MediaWiki\Logger\LoggerFactory;
|
2022-07-30 18:52:37 +00:00
|
|
|
use ParserOutput;
|
2022-02-24 21:16:46 +00:00
|
|
|
use Wikimedia\AtEase\AtEase;
|
2018-02-04 18:57:59 +00:00
|
|
|
|
2022-08-03 12:31:15 +00:00
|
|
|
class LuaStandaloneEngine extends LuaEngine {
|
2020-12-18 19:04:17 +00:00
|
|
|
/** @var int|null */
|
2016-05-17 14:52:05 +00:00
|
|
|
protected static $clockTick;
|
2020-12-18 19:04:17 +00:00
|
|
|
/** @var array|false */
|
2014-08-17 21:11:26 +00:00
|
|
|
public $initialStatus;
|
2012-04-13 10:38:12 +00:00
|
|
|
|
2018-04-09 04:39:06 +00:00
|
|
|
/**
|
2022-07-30 18:52:37 +00:00
|
|
|
* @var LuaStandaloneInterpreter
|
2018-04-09 04:39:06 +00:00
|
|
|
*/
|
|
|
|
protected $interpreter;
|
|
|
|
|
2012-04-13 10:38:12 +00:00
|
|
|
public function load() {
|
|
|
|
parent::load();
|
|
|
|
if ( php_uname( 's' ) === 'Linux' ) {
|
|
|
|
$this->initialStatus = $this->interpreter->getStatus();
|
|
|
|
} else {
|
|
|
|
$this->initialStatus = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-14 18:50:34 +00:00
|
|
|
/** @inheritDoc */
|
2013-01-25 17:53:18 +00:00
|
|
|
public function getPerformanceCharacteristics() {
|
2017-06-15 17:19:00 +00:00
|
|
|
return [
|
2013-01-25 17:53:18 +00:00
|
|
|
'phpCallsRequireSerialization' => true,
|
2017-06-15 17:19:00 +00:00
|
|
|
];
|
2013-01-25 17:53:18 +00:00
|
|
|
}
|
|
|
|
|
2020-01-14 18:50:34 +00:00
|
|
|
/** @inheritDoc */
|
2022-01-12 16:31:31 +00:00
|
|
|
public function reportLimitData( ParserOutput $parserOutput ) {
|
2013-03-14 12:42:42 +00:00
|
|
|
try {
|
|
|
|
$this->load();
|
|
|
|
} catch ( Exception $e ) {
|
|
|
|
return;
|
|
|
|
}
|
2014-07-09 14:48:59 +00:00
|
|
|
if ( $this->initialStatus ) {
|
|
|
|
$status = $this->interpreter->getStatus();
|
2022-01-12 16:31:31 +00:00
|
|
|
$parserOutput->setLimitReportData( 'scribunto-limitreport-timeusage',
|
2017-06-15 17:19:00 +00:00
|
|
|
[
|
2014-07-09 14:48:59 +00:00
|
|
|
sprintf( "%.3f", $status['time'] / $this->getClockTick() ),
|
2018-05-30 15:31:12 +00:00
|
|
|
// Strip trailing .0s
|
|
|
|
rtrim( rtrim( sprintf( "%.3f", $this->options['cpuLimit'] ), '0' ), '.' )
|
2017-06-15 17:19:00 +00:00
|
|
|
]
|
2014-07-09 14:48:59 +00:00
|
|
|
);
|
2022-01-12 16:31:31 +00:00
|
|
|
$parserOutput->setLimitReportData( 'scribunto-limitreport-virtmemusage',
|
2017-06-15 17:19:00 +00:00
|
|
|
[
|
2014-07-09 14:48:59 +00:00
|
|
|
$status['vsize'],
|
|
|
|
$this->options['memoryLimit']
|
2017-06-15 17:19:00 +00:00
|
|
|
]
|
2014-07-09 14:48:59 +00:00
|
|
|
);
|
2022-01-12 16:31:31 +00:00
|
|
|
$parserOutput->setLimitReportData( 'scribunto-limitreport-estmemusage',
|
2014-07-09 14:48:59 +00:00
|
|
|
$status['vsize'] - $this->initialStatus['vsize']
|
|
|
|
);
|
2013-03-14 12:42:42 +00:00
|
|
|
}
|
2013-03-25 15:56:38 +00:00
|
|
|
$logs = $this->getLogBuffer();
|
|
|
|
if ( $logs !== '' ) {
|
2022-01-12 16:31:31 +00:00
|
|
|
$parserOutput->addModules( [ 'ext.scribunto.logs' ] );
|
|
|
|
$parserOutput->setLimitReportData( 'scribunto-limitreport-logs', $logs );
|
2013-03-25 15:56:38 +00:00
|
|
|
}
|
2013-03-14 12:42:42 +00:00
|
|
|
}
|
|
|
|
|
2020-01-14 18:50:34 +00:00
|
|
|
/** @inheritDoc */
|
2018-11-09 19:31:08 +00:00
|
|
|
public function formatLimitData( $key, &$value, &$report, $isHTML, $localize ) {
|
2013-03-14 12:42:42 +00:00
|
|
|
switch ( $key ) {
|
2013-03-25 15:56:38 +00:00
|
|
|
case 'scribunto-limitreport-logs':
|
|
|
|
if ( $isHTML ) {
|
|
|
|
$report .= $this->formatHtmlLogs( $value, $localize );
|
|
|
|
}
|
|
|
|
return false;
|
2013-03-14 12:42:42 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-06-15 23:34:35 +00:00
|
|
|
/**
|
2020-12-18 19:04:17 +00:00
|
|
|
* @return int
|
2012-06-15 23:34:35 +00:00
|
|
|
*/
|
2018-11-09 19:31:08 +00:00
|
|
|
protected function getClockTick() {
|
2012-04-13 10:38:12 +00:00
|
|
|
if ( self::$clockTick === null ) {
|
2022-02-24 21:16:46 +00:00
|
|
|
AtEase::suppressWarnings();
|
2012-04-13 10:38:12 +00:00
|
|
|
self::$clockTick = intval( shell_exec( 'getconf CLK_TCK' ) );
|
2022-02-24 21:16:46 +00:00
|
|
|
AtEase::restoreWarnings();
|
2012-04-13 10:38:12 +00:00
|
|
|
if ( !self::$clockTick ) {
|
|
|
|
self::$clockTick = 100;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return self::$clockTick;
|
|
|
|
}
|
|
|
|
|
2012-06-15 23:34:35 +00:00
|
|
|
/**
|
2022-07-30 18:52:37 +00:00
|
|
|
* @return LuaStandaloneInterpreter
|
2012-06-15 23:34:35 +00:00
|
|
|
*/
|
2018-11-09 19:31:08 +00:00
|
|
|
protected function newInterpreter() {
|
2022-07-30 18:52:37 +00:00
|
|
|
return new LuaStandaloneInterpreter( $this, $this->options + [
|
2018-02-04 18:57:59 +00:00
|
|
|
'logger' => LoggerFactory::getInstance( 'Scribunto' )
|
|
|
|
] );
|
2012-04-13 10:38:12 +00:00
|
|
|
}
|
2013-03-20 16:30:26 +00:00
|
|
|
|
2020-01-14 18:50:34 +00:00
|
|
|
/** @inheritDoc */
|
2014-10-09 19:49:40 +00:00
|
|
|
public function getSoftwareInfo( array &$software ) {
|
2022-07-30 18:52:37 +00:00
|
|
|
$ver = LuaStandaloneInterpreter::getLuaVersion( $this->options );
|
2013-03-20 16:30:26 +00:00
|
|
|
if ( $ver !== null ) {
|
|
|
|
if ( substr( $ver, 0, 6 ) === 'LuaJIT' ) {
|
|
|
|
$software['[http://luajit.org/ LuaJIT]'] = str_replace( 'LuaJIT ', '', $ver );
|
|
|
|
} else {
|
|
|
|
$software['[http://www.lua.org/ Lua]'] = str_replace( 'Lua ', '', $ver );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-04-13 10:38:12 +00:00
|
|
|
}
|