aboutsummaryrefslogtreecommitdiffstats
path: root/lib/private/Profiler
diff options
context:
space:
mode:
Diffstat (limited to 'lib/private/Profiler')
-rw-r--r--lib/private/Profiler/BuiltInProfiler.php95
-rw-r--r--lib/private/Profiler/FileProfilerStorage.php46
-rw-r--r--lib/private/Profiler/Profile.php21
-rw-r--r--lib/private/Profiler/Profiler.php21
-rw-r--r--lib/private/Profiler/RoutingDataCollector.php21
5 files changed, 115 insertions, 89 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 79b17d520c6..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;
@@ -64,7 +46,7 @@ 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 && !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 = (string)$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)));
@@ -158,7 +140,7 @@ 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);
}
@@ -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';
}
/**
@@ -238,12 +220,12 @@ 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 !== '') {
@@ -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 10cae8a96a9..84a4e3eff34 100644
--- a/lib/private/Profiler/Profiler.php
+++ b/lib/private/Profiler/Profiler.php
@@ -3,25 +3,8 @@
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;
diff --git a/lib/private/Profiler/RoutingDataCollector.php b/lib/private/Profiler/RoutingDataCollector.php
index 8769615a37b..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;