diff options
Diffstat (limited to 'lib/private/Profiler')
-rw-r--r-- | lib/private/Profiler/BuiltInProfiler.php | 95 | ||||
-rw-r--r-- | lib/private/Profiler/FileProfilerStorage.php | 66 | ||||
-rw-r--r-- | lib/private/Profiler/Profile.php | 21 | ||||
-rw-r--r-- | lib/private/Profiler/Profiler.php | 47 | ||||
-rw-r--r-- | lib/private/Profiler/RoutingDataCollector.php | 23 |
5 files changed, 145 insertions, 107 deletions
diff --git a/lib/private/Profiler/BuiltInProfiler.php b/lib/private/Profiler/BuiltInProfiler.php new file mode 100644 index 00000000000..0a62365e901 --- /dev/null +++ b/lib/private/Profiler/BuiltInProfiler.php @@ -0,0 +1,95 @@ +<?php + +declare(strict_types=1); +/** + * SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-only + */ + +namespace OC\Profiler; + +use DateTime; +use OCP\IConfig; +use OCP\IRequest; + +class BuiltInProfiler { + private \ExcimerProfiler $excimer; + + public function __construct( + private IConfig $config, + private IRequest $request, + ) { + } + + public function start(): void { + if (!extension_loaded('excimer')) { + return; + } + + $shouldProfileSingleRequest = $this->shouldProfileSingleRequest(); + $shouldSample = $this->config->getSystemValueBool('profiling.sample') && !$shouldProfileSingleRequest; + + + if (!$shouldProfileSingleRequest && !$shouldSample) { + return; + } + + $requestRate = $this->config->getSystemValue('profiling.request.rate', 0.001); + $sampleRate = $this->config->getSystemValue('profiling.sample.rate', 1.0); + $eventType = $this->config->getSystemValue('profiling.event_type', EXCIMER_REAL); + + + $this->excimer = new \ExcimerProfiler(); + $this->excimer->setPeriod($shouldProfileSingleRequest ? $requestRate : $sampleRate); + $this->excimer->setEventType($eventType); + $this->excimer->setMaxDepth(250); + + if ($shouldSample) { + $this->excimer->setFlushCallback([$this, 'handleSampleFlush'], 1); + } + + $this->excimer->start(); + register_shutdown_function([$this, 'handleShutdown']); + } + + public function handleSampleFlush(\ExcimerLog $log): void { + file_put_contents($this->getSampleFilename(), $log->formatCollapsed(), FILE_APPEND); + } + + public function handleShutdown(): void { + $this->excimer->stop(); + + if (!$this->shouldProfileSingleRequest()) { + $this->excimer->flush(); + return; + } + + $request = \OCP\Server::get(IRequest::class); + $data = $this->excimer->getLog()->getSpeedscopeData(); + + $data['profiles'][0]['name'] = $request->getMethod() . ' ' . $request->getRequestUri() . ' ' . $request->getId(); + + file_put_contents($this->getProfileFilename(), json_encode($data, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE)); + } + + private function shouldProfileSingleRequest(): bool { + $shouldProfileSingleRequest = $this->config->getSystemValueBool('profiling.request', false); + $profileSecret = $this->config->getSystemValueString('profiling.secret', ''); + $secretParam = $this->request->getParam('profile_secret') ?? null; + return $shouldProfileSingleRequest || (!empty($profileSecret) && $profileSecret === $secretParam); + } + + private function getSampleFilename(): string { + $profilePath = $this->config->getSystemValueString('profiling.path', '/tmp'); + $sampleRotation = $this->config->getSystemValueInt('profiling.sample.rotation', 60); + $timestamp = floor(time() / ($sampleRotation * 60)) * ($sampleRotation * 60); + $sampleName = date('Y-m-d_Hi', (int)$timestamp); + return $profilePath . '/sample-' . $sampleName . '.log'; + } + + private function getProfileFilename(): string { + $profilePath = $this->config->getSystemValueString('profiling.path', '/tmp'); + $requestId = $this->request->getId(); + return $profilePath . '/profile-' . (new DateTime)->format('Y-m-d_His_v') . '-' . $requestId . '.json'; + } +} diff --git a/lib/private/Profiler/FileProfilerStorage.php b/lib/private/Profiler/FileProfilerStorage.php index d583549332e..cd45090e7ca 100644 --- a/lib/private/Profiler/FileProfilerStorage.php +++ b/lib/private/Profiler/FileProfilerStorage.php @@ -2,26 +2,8 @@ declare(strict_types = 1); /** - * @copyright 2022 Carl Schwan <carl@carlschwan.eu> - * - * @author Carl Schwan <carl@carlschwan.eu> - * @author Alexandre Salomé <alexandre.salome@gmail.com> - * - * @license AGPL-3.0-or-later AND MIT - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see <http://www.gnu.org/licenses/>. - * + * SPDX-FileCopyrightText: 2022 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later */ namespace OC\Profiler; @@ -45,12 +27,12 @@ class FileProfilerStorage { public function __construct(string $folder) { $this->folder = $folder; - if (!is_dir($this->folder) && false === @mkdir($this->folder, 0777, true) && !is_dir($this->folder)) { + if (!is_dir($this->folder) && @mkdir($this->folder, 0777, true) === false && !is_dir($this->folder)) { throw new \RuntimeException(sprintf('Unable to create the storage directory (%s).', $this->folder)); } } - public function find(?string $url, ?int $limit, ?string $method, int $start = null, int $end = null, string $statusCode = null): array { + public function find(?string $url, ?int $limit, ?string $method, ?int $start = null, ?int $end = null, ?string $statusCode = null): array { $file = $this->getIndexFilename(); if (!file_exists($file)) { @@ -64,9 +46,9 @@ class FileProfilerStorage { while (\count($result) < $limit && $line = $this->readLineFromFile($file)) { $values = str_getcsv($line); [$csvToken, $csvMethod, $csvUrl, $csvTime, $csvParent, $csvStatusCode] = $values; - $csvTime = (int) $csvTime; + $csvTime = (int)$csvTime; - if ($url && false === strpos($csvUrl, $url) || $method && false === strpos($csvMethod, $method) || $statusCode && false === strpos($csvStatusCode, $statusCode)) { + if ($url && !str_contains($csvUrl, $url) || $method && !str_contains($csvMethod, $method) || $statusCode && !str_contains($csvStatusCode, $statusCode)) { continue; } @@ -99,11 +81,11 @@ class FileProfilerStorage { $iterator = new \RecursiveIteratorIterator($iterator, \RecursiveIteratorIterator::CHILD_FIRST); foreach ($iterator as $file) { - $file = $file->getPathInfo(); - if (is_file($file)) { - unlink($file); + $path = $file->getPathname(); + if (is_file($path)) { + unlink($path); } else { - rmdir($file); + rmdir($path); } } } @@ -114,7 +96,7 @@ class FileProfilerStorage { } if (\function_exists('gzcompress')) { - $file = 'compress.zlib://'.$file; + $file = 'compress.zlib://' . $file; } return $this->createProfileFromData($token, unserialize(file_get_contents($file))); @@ -130,7 +112,7 @@ class FileProfilerStorage { if (!$profileIndexed) { // Create directory $dir = \dirname($file); - if (!is_dir($dir) && false === @mkdir($dir, 0777, true) && !is_dir($dir)) { + if (!is_dir($dir) && @mkdir($dir, 0777, true) === false && !is_dir($dir)) { throw new \RuntimeException(sprintf('Unable to create the storage directory (%s).', $dir)); } } @@ -158,11 +140,11 @@ class FileProfilerStorage { $context = stream_context_create(); if (\function_exists('gzcompress')) { - $file = 'compress.zlib://'.$file; + $file = 'compress.zlib://' . $file; stream_context_set_option($context, 'zlib', 'level', 3); } - if (false === file_put_contents($file, serialize($data), 0, $context)) { + if (file_put_contents($file, serialize($data), 0, $context) === false) { return false; } @@ -196,7 +178,7 @@ class FileProfilerStorage { $folderA = substr($token, -2, 2); $folderB = substr($token, -4, 2); - return $this->folder.'/'.$folderA.'/'.$folderB.'/'.$token; + return $this->folder . '/' . $folderA . '/' . $folderB . '/' . $token; } /** @@ -205,7 +187,7 @@ class FileProfilerStorage { * @return string The index filename */ protected function getIndexFilename(): string { - return $this->folder.'/index.csv'; + return $this->folder . '/index.csv'; } /** @@ -221,7 +203,7 @@ class FileProfilerStorage { $line = ''; $position = ftell($file); - if (0 === $position) { + if ($position === 0) { return null; } @@ -230,7 +212,7 @@ class FileProfilerStorage { $position -= $chunkSize; fseek($file, $position); - if (0 === $chunkSize) { + if ($chunkSize === 0) { // bof reached break; } @@ -238,23 +220,23 @@ class FileProfilerStorage { $buffer = fread($file, $chunkSize); if (false === ($upTo = strrpos($buffer, "\n"))) { - $line = $buffer.$line; + $line = $buffer . $line; continue; } $position += $upTo; - $line = substr($buffer, $upTo + 1).$line; + $line = substr($buffer, $upTo + 1) . $line; fseek($file, max(0, $position), \SEEK_SET); - if ('' !== $line) { + if ($line !== '') { break; } } - return '' === $line ? null : $line; + return $line === '' ? null : $line; } - protected function createProfileFromData(string $token, array $data, IProfile $parent = null): IProfile { + protected function createProfileFromData(string $token, array $data, ?IProfile $parent = null): IProfile { $profile = new Profile($token); $profile->setMethod($data['method']); $profile->setUrl($data['url']); @@ -276,7 +258,7 @@ class FileProfilerStorage { } if (\function_exists('gzcompress')) { - $file = 'compress.zlib://'.$file; + $file = 'compress.zlib://' . $file; } $profile->addChild($this->createProfileFromData($token, unserialize(file_get_contents($file)), $profile)); diff --git a/lib/private/Profiler/Profile.php b/lib/private/Profiler/Profile.php index 648c49c0330..c611d79e259 100644 --- a/lib/private/Profiler/Profile.php +++ b/lib/private/Profiler/Profile.php @@ -2,25 +2,8 @@ declare(strict_types = 1); /** - * @copyright 2022 Carl Schwan <carl@carlschwan.eu> - * - * @author Carl Schwan <carl@carlschwan.eu> - * - * @license GNU AGPL version 3 or any later version - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see <http://www.gnu.org/licenses/>. - * + * SPDX-FileCopyrightText: 2022 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later */ namespace OC\Profiler; diff --git a/lib/private/Profiler/Profiler.php b/lib/private/Profiler/Profiler.php index 9fd5e76d592..84a4e3eff34 100644 --- a/lib/private/Profiler/Profiler.php +++ b/lib/private/Profiler/Profiler.php @@ -3,35 +3,18 @@ declare(strict_types = 1); /** - * @copyright 2021 Carl Schwan <carl@carlschwan.eu> - * - * @author Carl Schwan <carl@carlschwan.eu> - * - * @license GNU AGPL version 3 or any later version - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see <http://www.gnu.org/licenses/>. - * + * SPDX-FileCopyrightText: 2021 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later */ namespace OC\Profiler; use OC\AppFramework\Http\Request; +use OC\SystemConfig; use OCP\AppFramework\Http\Response; use OCP\DataCollector\IDataCollector; -use OCP\Profiler\IProfiler; use OCP\Profiler\IProfile; -use OC\SystemConfig; +use OCP\Profiler\IProfiler; class Profiler implements IProfiler { /** @var array<string, IDataCollector> */ @@ -44,7 +27,7 @@ class Profiler implements IProfiler { public function __construct(SystemConfig $config) { $this->enabled = $config->getValue('profiler', false); if ($this->enabled) { - $this->storage = new FileProfilerStorage($config->getValue('datadirectory', \OC::$SERVERROOT . '/data') . '/profiler'); + $this->storage = new FileProfilerStorage($config->getValue('datadirectory', \OC::$SERVERROOT . '/data') . '/__profiler'); } } @@ -61,11 +44,19 @@ class Profiler implements IProfiler { } public function loadProfile(string $token): ?IProfile { - return $this->storage->read($token); + if ($this->storage) { + return $this->storage->read($token); + } else { + return null; + } } public function saveProfile(IProfile $profile): bool { - return $this->storage->write($profile); + if ($this->storage) { + return $this->storage->write($profile); + } else { + return false; + } } public function collect(Request $request, Response $response): IProfile { @@ -87,8 +78,12 @@ class Profiler implements IProfiler { * @return array[] */ public function find(?string $url, ?int $limit, ?string $method, ?int $start, ?int $end, - string $statusCode = null): array { - return $this->storage->find($url, $limit, $method, $start, $end, $statusCode); + ?string $statusCode = null): array { + if ($this->storage) { + return $this->storage->find($url, $limit, $method, $start, $end, $statusCode); + } else { + return []; + } } public function dataProviders(): array { diff --git a/lib/private/Profiler/RoutingDataCollector.php b/lib/private/Profiler/RoutingDataCollector.php index e6659230879..c8952c76a38 100644 --- a/lib/private/Profiler/RoutingDataCollector.php +++ b/lib/private/Profiler/RoutingDataCollector.php @@ -3,25 +3,8 @@ declare(strict_types=1); /** - * @copyright 2022 Carl Schwan <carl@carlschwan.eu> - * - * @author Carl Schwan <carl@carlschwan.eu> - * - * @license GNU AGPL version 3 or any later version - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see <http://www.gnu.org/licenses/>. - * + * SPDX-FileCopyrightText: 2022 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later */ namespace OC\Profiler; @@ -41,7 +24,7 @@ class RoutingDataCollector extends AbstractDataCollector { $this->actionName = $actionName; } - public function collect(Request $request, Response $response, \Throwable $exception = null): void { + public function collect(Request $request, Response $response, ?\Throwable $exception = null): void { $this->data = [ 'appName' => $this->appName, 'controllerName' => $this->controllerName, |