Debian Codegraph @

extensions/Scribunto/includes/Engines/LuaStandalone/LuaStandaloneEngine.php

raw symbols SCIP
1 <?php
2
3 namespace MediaWiki\Extension\Scribunto\Engines\LuaStandalone;
4
5 use Exception;
6 use MediaWiki\Extension\Scribunto\Engines\LuaCommon\LuaEngine;
7 use MediaWiki\Logger\LoggerFactory;
8 use MediaWiki\Parser\ParserOutput;
9
10 class LuaStandaloneEngine extends LuaEngine {
11 /** @var int|null */
12 protected static $clockTick;
13 /** @var array|false */
14 public $initialStatus;
15
16 /**
17 * @var LuaStandaloneInterpreter
18 */
19 protected $interpreter;
20
21 public function load() {
22 parent::load();
23 if ( php_uname( 's' ) === 'Linux' ) {
24 $this->initialStatus = $this->interpreter->getStatus();
25 } else {
26 $this->initialStatus = false;
27 }
28 }
29
30 /** @inheritDoc */
31 public function getPerformanceCharacteristics() {
32 return [
33 'phpCallsRequireSerialization' => true,
34 ];
35 }
36
37 /** @inheritDoc */
38 public function reportLimitData( ParserOutput $parserOutput ) {
39 try {
40 $this->load();
41 } catch ( Exception $e ) {
42 return;
43 }
44 if ( $this->initialStatus ) {
45 $status = $this->interpreter->getStatus();
46 $parserOutput->setLimitReportData( 'scribunto-limitreport-timeusage',
47 [
48 sprintf( "%.3f", $status['time'] / $this->getClockTick() ),
49 // Strip trailing .0s
50 rtrim( rtrim( sprintf( "%.3f", $this->options['cpuLimit'] ), '0' ), '.' )
51 ]
52 );
53 $parserOutput->setLimitReportData( 'scribunto-limitreport-virtmemusage',
54 [
55 $status['vsize'],
56 $this->options['memoryLimit']
57 ]
58 );
59 $parserOutput->setLimitReportData( 'scribunto-limitreport-estmemusage',
60 $status['vsize'] - $this->initialStatus['vsize']
61 );
62 }
63 $logs = $this->getLogBuffer();
64 if ( $logs !== '' ) {
65 $parserOutput->addModules( [ 'ext.scribunto.logs' ] );
66 $parserOutput->setLimitReportData( 'scribunto-limitreport-logs', $logs );
67 }
68 }
69
70 /** @inheritDoc */
71 public function formatLimitData( $key, &$value, &$report, $isHTML, $localize ) {
72 switch ( $key ) {
73 case 'scribunto-limitreport-logs':
74 if ( $isHTML ) {
75 $report .= $this->formatHtmlLogs( $value, $localize );
76 }
77 return false;
78 }
79 return true;
80 }
81
82 /**
83 * @return int
84 */
85 protected function getClockTick() {
86 if ( self::$clockTick === null ) {
87 // phpcs:ignore Generic.PHP.NoSilencedErrors.Discouraged,MediaWiki.Usage.ForbiddenFunctions.shell_exec
88 self::$clockTick = intval( @shell_exec( 'getconf CLK_TCK' ) );
89 if ( !self::$clockTick ) {
90 self::$clockTick = 100;
91 }
92 }
93 return self::$clockTick;
94 }
95
96 /**
97 * @return LuaStandaloneInterpreter
98 */
99 protected function newInterpreter() {
100 return new LuaStandaloneInterpreter( $this, $this->options + [
101 'logger' => LoggerFactory::getInstance( 'Scribunto' )
102 ] );
103 }
104
105 /** @inheritDoc */
106 public function getSoftwareInfo( array &$software ) {
107 $ver = LuaStandaloneInterpreter::getLuaVersion( $this->options );
108 if ( $ver !== null ) {
109 if ( substr( $ver, 0, 6 ) === 'LuaJIT' ) {
110 $software['[http://luajit.org/ LuaJIT]'] = str_replace( 'LuaJIT ', '', $ver );
111 } else {
112 $software['[http://www.lua.org/ Lua]'] = str_replace( 'Lua ', '', $ver );
113 }
114 }
115 }
116 }