2010-08-28 14:23:21 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
2011-01-15 13:29:43 +00:00
|
|
|
* This file is part of the Symfony package.
|
2010-08-28 14:23:21 +01:00
|
|
|
*
|
2011-03-06 11:40:06 +00:00
|
|
|
* (c) Fabien Potencier <fabien@symfony.com>
|
2010-08-28 14:23:21 +01:00
|
|
|
*
|
2011-01-15 13:29:43 +00:00
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
2010-08-28 14:23:21 +01:00
|
|
|
*/
|
|
|
|
|
2011-01-15 13:29:43 +00:00
|
|
|
namespace Symfony\Component\HttpKernel\DataCollector;
|
|
|
|
|
|
|
|
use Symfony\Component\HttpFoundation\Request;
|
|
|
|
use Symfony\Component\HttpFoundation\Response;
|
|
|
|
|
2010-08-28 14:23:21 +01:00
|
|
|
/**
|
|
|
|
* MemoryDataCollector.
|
|
|
|
*
|
2011-03-06 11:40:06 +00:00
|
|
|
* @author Fabien Potencier <fabien@symfony.com>
|
2019-08-08 17:27:37 +01:00
|
|
|
*
|
2019-11-05 20:53:45 +00:00
|
|
|
* @final
|
2010-08-28 14:23:21 +01:00
|
|
|
*/
|
2013-09-30 06:59:16 +01:00
|
|
|
class MemoryDataCollector extends DataCollector implements LateDataCollectorInterface
|
2010-08-28 14:23:21 +01:00
|
|
|
{
|
2012-11-10 16:53:14 +00:00
|
|
|
public function __construct()
|
|
|
|
{
|
2017-10-05 10:48:08 +01:00
|
|
|
$this->reset();
|
2012-11-10 16:53:14 +00:00
|
|
|
}
|
|
|
|
|
2010-08-28 14:23:21 +01:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2019-11-05 20:53:45 +00:00
|
|
|
public function collect(Request $request, Response $response, \Throwable $exception = null)
|
2010-08-28 14:23:21 +01:00
|
|
|
{
|
2012-11-10 16:53:14 +00:00
|
|
|
$this->updateMemoryUsage();
|
2010-08-28 14:23:21 +01:00
|
|
|
}
|
|
|
|
|
2017-10-05 10:48:08 +01:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function reset()
|
|
|
|
{
|
2019-01-16 09:39:14 +00:00
|
|
|
$this->data = [
|
2017-10-05 10:48:08 +01:00
|
|
|
'memory' => 0,
|
|
|
|
'memory_limit' => $this->convertToBytes(ini_get('memory_limit')),
|
2019-01-16 09:39:14 +00:00
|
|
|
];
|
2017-10-05 10:48:08 +01:00
|
|
|
}
|
|
|
|
|
2013-09-30 06:59:16 +01:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function lateCollect()
|
|
|
|
{
|
|
|
|
$this->updateMemoryUsage();
|
|
|
|
}
|
|
|
|
|
2010-08-28 14:23:21 +01:00
|
|
|
/**
|
|
|
|
* Gets the memory.
|
|
|
|
*
|
2014-11-30 13:33:44 +00:00
|
|
|
* @return int The memory
|
2010-08-28 14:23:21 +01:00
|
|
|
*/
|
|
|
|
public function getMemory()
|
|
|
|
{
|
|
|
|
return $this->data['memory'];
|
|
|
|
}
|
|
|
|
|
2013-02-10 09:40:27 +00:00
|
|
|
/**
|
|
|
|
* Gets the PHP memory limit.
|
|
|
|
*
|
2014-11-30 13:33:44 +00:00
|
|
|
* @return int The memory limit
|
2013-02-10 09:40:27 +00:00
|
|
|
*/
|
|
|
|
public function getMemoryLimit()
|
|
|
|
{
|
|
|
|
return $this->data['memory_limit'];
|
|
|
|
}
|
|
|
|
|
2012-11-10 16:53:14 +00:00
|
|
|
/**
|
|
|
|
* Updates the memory usage data.
|
|
|
|
*/
|
|
|
|
public function updateMemoryUsage()
|
|
|
|
{
|
|
|
|
$this->data['memory'] = memory_get_peak_usage(true);
|
|
|
|
}
|
|
|
|
|
2010-08-28 14:23:21 +01:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function getName()
|
|
|
|
{
|
|
|
|
return 'memory';
|
|
|
|
}
|
2013-03-15 16:55:21 +00:00
|
|
|
|
2019-11-22 13:14:59 +00:00
|
|
|
/**
|
|
|
|
* @return int|float
|
|
|
|
*/
|
|
|
|
private function convertToBytes(string $memoryLimit)
|
2013-03-15 16:55:21 +00:00
|
|
|
{
|
2013-03-23 06:20:57 +00:00
|
|
|
if ('-1' === $memoryLimit) {
|
|
|
|
return -1;
|
2013-03-15 16:55:21 +00:00
|
|
|
}
|
|
|
|
|
2013-10-28 14:46:04 +00:00
|
|
|
$memoryLimit = strtolower($memoryLimit);
|
2013-09-16 10:12:56 +01:00
|
|
|
$max = strtolower(ltrim($memoryLimit, '+'));
|
|
|
|
if (0 === strpos($max, '0x')) {
|
2018-07-05 12:24:53 +01:00
|
|
|
$max = \intval($max, 16);
|
2013-09-16 10:12:56 +01:00
|
|
|
} elseif (0 === strpos($max, '0')) {
|
2018-07-05 12:24:53 +01:00
|
|
|
$max = \intval($max, 8);
|
2013-09-16 10:12:56 +01:00
|
|
|
} else {
|
2015-03-07 19:12:23 +00:00
|
|
|
$max = (int) $max;
|
2013-09-16 10:12:56 +01:00
|
|
|
}
|
2013-03-23 06:20:57 +00:00
|
|
|
|
2013-09-16 10:12:56 +01:00
|
|
|
switch (substr($memoryLimit, -1)) {
|
|
|
|
case 't': $max *= 1024;
|
2017-09-15 11:36:22 +01:00
|
|
|
// no break
|
2013-09-16 10:12:56 +01:00
|
|
|
case 'g': $max *= 1024;
|
2017-09-15 11:36:22 +01:00
|
|
|
// no break
|
2013-09-16 10:12:56 +01:00
|
|
|
case 'm': $max *= 1024;
|
2017-09-15 11:36:22 +01:00
|
|
|
// no break
|
2013-09-16 10:12:56 +01:00
|
|
|
case 'k': $max *= 1024;
|
2013-03-23 06:20:57 +00:00
|
|
|
}
|
|
|
|
|
2013-09-16 10:12:56 +01:00
|
|
|
return $max;
|
2013-03-15 16:55:21 +00:00
|
|
|
}
|
2010-08-28 14:23:21 +01:00
|
|
|
}
|