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.php269
-rw-r--r--lib/private/Profiler/Profile.php151
-rw-r--r--lib/private/Profiler/Profiler.php104
-rw-r--r--lib/private/Profiler/RoutingDataCollector.php38
5 files changed, 657 insertions, 0 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
new file mode 100644
index 00000000000..cd45090e7ca
--- /dev/null
+++ b/lib/private/Profiler/FileProfilerStorage.php
@@ -0,0 +1,269 @@
+<?php
+
+declare(strict_types = 1);
+/**
+ * SPDX-FileCopyrightText: 2022 Nextcloud GmbH and Nextcloud contributors
+ * SPDX-License-Identifier: AGPL-3.0-or-later
+ */
+
+namespace OC\Profiler;
+
+use OCP\Profiler\IProfile;
+
+/**
+ * Storage for profiler using files.
+ */
+class FileProfilerStorage {
+ // Folder where profiler data are stored.
+ private string $folder;
+
+ /**
+ * Constructs the file storage using a "dsn-like" path.
+ *
+ * Example : "file:/path/to/the/storage/folder"
+ *
+ * @throws \RuntimeException
+ */
+ public function __construct(string $folder) {
+ $this->folder = $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 {
+ $file = $this->getIndexFilename();
+
+ if (!file_exists($file)) {
+ return [];
+ }
+
+ $file = fopen($file, 'r');
+ fseek($file, 0, \SEEK_END);
+
+ $result = [];
+ while (\count($result) < $limit && $line = $this->readLineFromFile($file)) {
+ $values = str_getcsv($line);
+ [$csvToken, $csvMethod, $csvUrl, $csvTime, $csvParent, $csvStatusCode] = $values;
+ $csvTime = (int)$csvTime;
+
+ if ($url && !str_contains($csvUrl, $url) || $method && !str_contains($csvMethod, $method) || $statusCode && !str_contains($csvStatusCode, $statusCode)) {
+ continue;
+ }
+
+ if (!empty($start) && $csvTime < $start) {
+ continue;
+ }
+
+ if (!empty($end) && $csvTime > $end) {
+ continue;
+ }
+
+ $result[$csvToken] = [
+ 'token' => $csvToken,
+ 'method' => $csvMethod,
+ 'url' => $csvUrl,
+ 'time' => $csvTime,
+ 'parent' => $csvParent,
+ 'status_code' => $csvStatusCode,
+ ];
+ }
+
+ fclose($file);
+
+ return array_values($result);
+ }
+
+ public function purge(): void {
+ $flags = \FilesystemIterator::SKIP_DOTS;
+ $iterator = new \RecursiveDirectoryIterator($this->folder, $flags);
+ $iterator = new \RecursiveIteratorIterator($iterator, \RecursiveIteratorIterator::CHILD_FIRST);
+
+ foreach ($iterator as $file) {
+ $path = $file->getPathname();
+ if (is_file($path)) {
+ unlink($path);
+ } else {
+ rmdir($path);
+ }
+ }
+ }
+
+ public function read(string $token): ?IProfile {
+ if (!$token || !file_exists($file = $this->getFilename($token))) {
+ return null;
+ }
+
+ if (\function_exists('gzcompress')) {
+ $file = 'compress.zlib://' . $file;
+ }
+
+ return $this->createProfileFromData($token, unserialize(file_get_contents($file)));
+ }
+
+ /**
+ * @throws \RuntimeException
+ */
+ public function write(IProfile $profile): bool {
+ $file = $this->getFilename($profile->getToken());
+
+ $profileIndexed = is_file($file);
+ if (!$profileIndexed) {
+ // Create directory
+ $dir = \dirname($file);
+ if (!is_dir($dir) && @mkdir($dir, 0777, true) === false && !is_dir($dir)) {
+ throw new \RuntimeException(sprintf('Unable to create the storage directory (%s).', $dir));
+ }
+ }
+
+ $profileToken = $profile->getToken();
+ // when there are errors in sub-requests, the parent and/or children tokens
+ // may equal the profile token, resulting in infinite loops
+ $parentToken = $profile->getParentToken() !== $profileToken ? $profile->getParentToken() : null;
+ $childrenToken = array_filter(array_map(function (IProfile $p) use ($profileToken) {
+ return $profileToken !== $p->getToken() ? $p->getToken() : null;
+ }, $profile->getChildren()));
+
+ // Store profile
+ $data = [
+ 'token' => $profileToken,
+ 'parent' => $parentToken,
+ 'children' => $childrenToken,
+ 'data' => $profile->getCollectors(),
+ 'method' => $profile->getMethod(),
+ 'url' => $profile->getUrl(),
+ 'time' => $profile->getTime(),
+ 'status_code' => $profile->getStatusCode(),
+ ];
+
+ $context = stream_context_create();
+
+ if (\function_exists('gzcompress')) {
+ $file = 'compress.zlib://' . $file;
+ stream_context_set_option($context, 'zlib', 'level', 3);
+ }
+
+ if (file_put_contents($file, serialize($data), 0, $context) === false) {
+ return false;
+ }
+
+ if (!$profileIndexed) {
+ // Add to index
+ if (false === $file = fopen($this->getIndexFilename(), 'a')) {
+ return false;
+ }
+
+ fputcsv($file, [
+ $profile->getToken(),
+ $profile->getMethod(),
+ $profile->getUrl(),
+ $profile->getTime(),
+ $profile->getParentToken(),
+ $profile->getStatusCode(),
+ ]);
+ fclose($file);
+ }
+
+ return true;
+ }
+
+ /**
+ * Gets filename to store data, associated to the token.
+ *
+ * @return string The profile filename
+ */
+ protected function getFilename(string $token): string {
+ // Uses 4 last characters, because first are mostly the same.
+ $folderA = substr($token, -2, 2);
+ $folderB = substr($token, -4, 2);
+
+ return $this->folder . '/' . $folderA . '/' . $folderB . '/' . $token;
+ }
+
+ /**
+ * Gets the index filename.
+ *
+ * @return string The index filename
+ */
+ protected function getIndexFilename(): string {
+ return $this->folder . '/index.csv';
+ }
+
+ /**
+ * Reads a line in the file, backward.
+ *
+ * This function automatically skips the empty lines and do not include the line return in result value.
+ *
+ * @param resource $file The file resource, with the pointer placed at the end of the line to read
+ *
+ * @return ?string A string representing the line or null if beginning of file is reached
+ */
+ protected function readLineFromFile($file): ?string {
+ $line = '';
+ $position = ftell($file);
+
+ if ($position === 0) {
+ return null;
+ }
+
+ while (true) {
+ $chunkSize = min($position, 1024);
+ $position -= $chunkSize;
+ fseek($file, $position);
+
+ if ($chunkSize === 0) {
+ // bof reached
+ break;
+ }
+
+ $buffer = fread($file, $chunkSize);
+
+ if (false === ($upTo = strrpos($buffer, "\n"))) {
+ $line = $buffer . $line;
+ continue;
+ }
+
+ $position += $upTo;
+ $line = substr($buffer, $upTo + 1) . $line;
+ fseek($file, max(0, $position), \SEEK_SET);
+
+ if ($line !== '') {
+ break;
+ }
+ }
+
+ return $line === '' ? null : $line;
+ }
+
+ protected function createProfileFromData(string $token, array $data, ?IProfile $parent = null): IProfile {
+ $profile = new Profile($token);
+ $profile->setMethod($data['method']);
+ $profile->setUrl($data['url']);
+ $profile->setTime($data['time']);
+ $profile->setStatusCode($data['status_code']);
+ $profile->setCollectors($data['data']);
+
+ if (!$parent && $data['parent']) {
+ $parent = $this->read($data['parent']);
+ }
+
+ if ($parent) {
+ $profile->setParent($parent);
+ }
+
+ foreach ($data['children'] as $token) {
+ if (!$token || !file_exists($file = $this->getFilename($token))) {
+ continue;
+ }
+
+ if (\function_exists('gzcompress')) {
+ $file = 'compress.zlib://' . $file;
+ }
+
+ $profile->addChild($this->createProfileFromData($token, unserialize(file_get_contents($file)), $profile));
+ }
+
+ return $profile;
+ }
+}
diff --git a/lib/private/Profiler/Profile.php b/lib/private/Profiler/Profile.php
new file mode 100644
index 00000000000..c611d79e259
--- /dev/null
+++ b/lib/private/Profiler/Profile.php
@@ -0,0 +1,151 @@
+<?php
+
+declare(strict_types = 1);
+/**
+ * SPDX-FileCopyrightText: 2022 Nextcloud GmbH and Nextcloud contributors
+ * SPDX-License-Identifier: AGPL-3.0-or-later
+ */
+
+namespace OC\Profiler;
+
+use OCP\DataCollector\IDataCollector;
+use OCP\Profiler\IProfile;
+
+class Profile implements \JsonSerializable, IProfile {
+ private string $token;
+
+ private ?int $time = null;
+
+ private ?string $url = null;
+
+ private ?string $method = null;
+
+ private ?int $statusCode = null;
+
+ /** @var array<string, IDataCollector> */
+ private array $collectors = [];
+
+ private ?IProfile $parent = null;
+
+ /** @var IProfile[] */
+ private array $children = [];
+
+ public function __construct(string $token) {
+ $this->token = $token;
+ }
+
+ public function getToken(): string {
+ return $this->token;
+ }
+
+ public function setToken(string $token): void {
+ $this->token = $token;
+ }
+
+ public function getTime(): ?int {
+ return $this->time;
+ }
+
+ public function setTime(int $time): void {
+ $this->time = $time;
+ }
+
+ public function getUrl(): ?string {
+ return $this->url;
+ }
+
+ public function setUrl(string $url): void {
+ $this->url = $url;
+ }
+
+ public function getMethod(): ?string {
+ return $this->method;
+ }
+
+ public function setMethod(string $method): void {
+ $this->method = $method;
+ }
+
+ public function getStatusCode(): ?int {
+ return $this->statusCode;
+ }
+
+ public function setStatusCode(int $statusCode): void {
+ $this->statusCode = $statusCode;
+ }
+
+ public function addCollector(IDataCollector $collector) {
+ $this->collectors[$collector->getName()] = $collector;
+ }
+
+ public function getParent(): ?IProfile {
+ return $this->parent;
+ }
+
+ public function setParent(?IProfile $parent): void {
+ $this->parent = $parent;
+ }
+
+ public function getParentToken(): ?string {
+ return $this->parent ? $this->parent->getToken() : null;
+ }
+
+ /** @return IProfile[] */
+ public function getChildren(): array {
+ return $this->children;
+ }
+
+ /**
+ * @param IProfile[] $children
+ */
+ public function setChildren(array $children): void {
+ $this->children = [];
+ foreach ($children as $child) {
+ $this->addChild($child);
+ }
+ }
+
+ public function addChild(IProfile $profile): void {
+ $this->children[] = $profile;
+ $profile->setParent($this);
+ }
+
+ /**
+ * @return IDataCollector[]
+ */
+ public function getCollectors(): array {
+ return $this->collectors;
+ }
+
+ /**
+ * @param IDataCollector[] $collectors
+ */
+ public function setCollectors(array $collectors): void {
+ $this->collectors = $collectors;
+ }
+
+ public function __sleep(): array {
+ return ['token', 'parent', 'children', 'collectors', 'method', 'url', 'time', 'statusCode'];
+ }
+
+ #[\ReturnTypeWillChange]
+ public function jsonSerialize() {
+ // Everything but parent
+ return [
+ 'token' => $this->token,
+ 'method' => $this->method,
+ 'children' => $this->children,
+ 'url' => $this->url,
+ 'statusCode' => $this->statusCode,
+ 'time' => $this->time,
+ 'collectors' => $this->collectors,
+ ];
+ }
+
+ public function getCollector(string $collectorName): ?IDataCollector {
+ if (!array_key_exists($collectorName, $this->collectors)) {
+ return null;
+ }
+ return $this->collectors[$collectorName];
+ }
+}
diff --git a/lib/private/Profiler/Profiler.php b/lib/private/Profiler/Profiler.php
new file mode 100644
index 00000000000..84a4e3eff34
--- /dev/null
+++ b/lib/private/Profiler/Profiler.php
@@ -0,0 +1,104 @@
+<?php
+
+declare(strict_types = 1);
+
+/**
+ * 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\IProfile;
+use OCP\Profiler\IProfiler;
+
+class Profiler implements IProfiler {
+ /** @var array<string, IDataCollector> */
+ private array $dataCollectors = [];
+
+ private ?FileProfilerStorage $storage = null;
+
+ private bool $enabled = false;
+
+ 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');
+ }
+ }
+
+ public function add(IDataCollector $dataCollector): void {
+ $this->dataCollectors[$dataCollector->getName()] = $dataCollector;
+ }
+
+ public function loadProfileFromResponse(Response $response): ?IProfile {
+ if (!$token = $response->getHeaders()['X-Debug-Token']) {
+ return null;
+ }
+
+ return $this->loadProfile($token);
+ }
+
+ public function loadProfile(string $token): ?IProfile {
+ if ($this->storage) {
+ return $this->storage->read($token);
+ } else {
+ return null;
+ }
+ }
+
+ public function saveProfile(IProfile $profile): bool {
+ if ($this->storage) {
+ return $this->storage->write($profile);
+ } else {
+ return false;
+ }
+ }
+
+ public function collect(Request $request, Response $response): IProfile {
+ $profile = new Profile($request->getId());
+ $profile->setTime(time());
+ $profile->setUrl($request->getRequestUri());
+ $profile->setMethod($request->getMethod());
+ $profile->setStatusCode($response->getStatus());
+ foreach ($this->dataCollectors as $dataCollector) {
+ $dataCollector->collect($request, $response, null);
+
+ // We clone for subrequests
+ $profile->addCollector(clone $dataCollector);
+ }
+ return $profile;
+ }
+
+ /**
+ * @return array[]
+ */
+ public function find(?string $url, ?int $limit, ?string $method, ?int $start, ?int $end,
+ ?string $statusCode = null): array {
+ if ($this->storage) {
+ return $this->storage->find($url, $limit, $method, $start, $end, $statusCode);
+ } else {
+ return [];
+ }
+ }
+
+ public function dataProviders(): array {
+ return array_keys($this->dataCollectors);
+ }
+
+ public function isEnabled(): bool {
+ return $this->enabled;
+ }
+
+ public function setEnabled(bool $enabled): void {
+ $this->enabled = $enabled;
+ }
+
+ public function clear(): void {
+ $this->storage->purge();
+ }
+}
diff --git a/lib/private/Profiler/RoutingDataCollector.php b/lib/private/Profiler/RoutingDataCollector.php
new file mode 100644
index 00000000000..c8952c76a38
--- /dev/null
+++ b/lib/private/Profiler/RoutingDataCollector.php
@@ -0,0 +1,38 @@
+<?php
+
+declare(strict_types=1);
+
+/**
+ * SPDX-FileCopyrightText: 2022 Nextcloud GmbH and Nextcloud contributors
+ * SPDX-License-Identifier: AGPL-3.0-or-later
+ */
+
+namespace OC\Profiler;
+
+use OC\AppFramework\Http\Request;
+use OCP\AppFramework\Http\Response;
+use OCP\DataCollector\AbstractDataCollector;
+
+class RoutingDataCollector extends AbstractDataCollector {
+ private string $appName;
+ private string $controllerName;
+ private string $actionName;
+
+ public function __construct(string $appName, string $controllerName, string $actionName) {
+ $this->appName = $appName;
+ $this->controllerName = $controllerName;
+ $this->actionName = $actionName;
+ }
+
+ public function collect(Request $request, Response $response, ?\Throwable $exception = null): void {
+ $this->data = [
+ 'appName' => $this->appName,
+ 'controllerName' => $this->controllerName,
+ 'actionName' => $this->actionName,
+ ];
+ }
+
+ public function getName(): string {
+ return 'router';
+ }
+}