Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 38 additions & 8 deletions lib/base.php
Original file line number Diff line number Diff line change
Expand Up @@ -915,16 +915,46 @@ public static function init(): void {
$eventLogger->end('request');
});

register_shutdown_function(function () {
register_shutdown_function(function () use ($config) {
$memoryPeak = memory_get_peak_usage();
$logLevel = match (true) {
$memoryPeak > 500_000_000 => ILogger::FATAL,
$memoryPeak > 400_000_000 => ILogger::ERROR,
$memoryPeak > 300_000_000 => ILogger::WARN,
default => null,
};
if ($logLevel !== null) {
$debugModeEnabled = $config->getSystemValueBool('debug', false);
$memoryLimit = null;

if (!$debugModeEnabled) {
// Use the memory helper to get the real memory limit in bytes if debug mode is disabled
try {
$memoryInfo = new \OC\MemoryInfo();
$memoryLimit = $memoryInfo->getMemoryLimit();
} catch (Throwable $e) {
// Ignore any errors and fall back to hardcoded thresholds
}
}

// Check if a memory limit is configured and can be retrieved and determine log level if debug mode is disabled
if (!$debugModeEnabled && $memoryLimit !== null && $memoryLimit !== -1) {
$logLevel = match (true) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
$logLevel = match (true) {
$memoryLimit = min(512_000_000, $memoryLimit);
$logLevel = match (true) {

I'd prefer this to be capped to 512M. We always want to log things when they are above ~300M, and not skip them because the developer uses 2G locally

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is how this is currently implemented. However we get a lot of complaints by admins and users. See the issue I am trying to fix: #52997

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please check 45d3c64 @nickvergessen :)

$memoryPeak > $memoryLimit * 0.9 => ILogger::FATAL,
$memoryPeak > $memoryLimit * 0.75 => ILogger::ERROR,
$memoryPeak > $memoryLimit * 0.5 => ILogger::WARN,
default => null,
};

$memoryLimitIni = @ini_get('memory_limit');
$message = 'Request used ' . Util::humanFileSize($memoryPeak) . ' of memory. Memory limit: ' . ($memoryLimitIni ?: 'unknown');
} else {
// Fall back to hardcoded thresholds if memory_limit cannot be determined or if debug mode is enabled
$logLevel = match (true) {
$memoryPeak > 500_000_000 => ILogger::FATAL,
$memoryPeak > 400_000_000 => ILogger::ERROR,
$memoryPeak > 300_000_000 => ILogger::WARN,
default => null,
};

$message = 'Request used more than 300 MB of RAM: ' . Util::humanFileSize($memoryPeak);
}

// Log the message
if ($logLevel !== null) {
$logger = Server::get(LoggerInterface::class);
$logger->log($logLevel, $message, ['app' => 'core']);
}
Expand Down
Loading