aboutsummaryrefslogtreecommitdiffstats
path: root/lib/private/Profiler
diff options
context:
space:
mode:
Diffstat (limited to 'lib/private/Profiler')
-rw-r--r--lib/private/Profiler/FileProfilerStorage.php286
-rw-r--r--lib/private/Profiler/Profile.php168
-rw-r--r--lib/private/Profiler/Profiler.php105
-rw-r--r--lib/private/Profiler/RoutingDataCollector.php55
4 files changed, 614 insertions, 0 deletions
diff --git a/lib/private/Profiler/FileProfilerStorage.php b/lib/private/Profiler/FileProfilerStorage.php
new file mode 100644
index 00000000000..ce09ed51ed9
--- /dev/null
+++ b/lib/private/Profiler/FileProfilerStorage.php
@@ -0,0 +1,286 @@
+<?php
+
+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/>.
+ *
+ */
+
+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) && false === @mkdir($this->folder, 0777, true) && !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 && false === strpos($csvUrl, $url) || $method && false === strpos($csvMethod, $method) || $statusCode && false === strpos($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) {
+ if (is_file($file)) {
+ unlink($file);
+ } else {
+ rmdir($file);
+ }
+ }
+ }
+
+ 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) && false === @mkdir($dir, 0777, true) && !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 (false === file_put_contents($file, serialize($data), 0, $context)) {
+ 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 (0 === $position) {
+ return null;
+ }
+
+ while (true) {
+ $chunkSize = min($position, 1024);
+ $position -= $chunkSize;
+ fseek($file, $position);
+
+ if (0 === $chunkSize) {
+ // 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..648c49c0330
--- /dev/null
+++ b/lib/private/Profiler/Profile.php
@@ -0,0 +1,168 @@
+<?php
+
+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/>.
+ *
+ */
+
+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..8aa800fbc6d
--- /dev/null
+++ b/lib/private/Profiler/Profiler.php
@@ -0,0 +1,105 @@
+<?php
+
+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/>.
+ *
+ */
+
+namespace OC\Profiler;
+
+use OC\AppFramework\Http\Request;
+use OCP\AppFramework\Http\Response;
+use OCP\DataCollector\IDataCollector;
+use OCP\Profiler\IProfiler;
+use OCP\Profiler\IProfile;
+use OC\SystemConfig;
+
+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 {
+ return $this->storage->read($token);
+ }
+
+ public function saveProfile(IProfile $profile): bool {
+ return $this->storage->write($profile);
+ }
+
+ 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 {
+ return $this->storage->find($url, $limit, $method, $start, $end, $statusCode);
+ }
+
+ 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;
+ }
+}
diff --git a/lib/private/Profiler/RoutingDataCollector.php b/lib/private/Profiler/RoutingDataCollector.php
new file mode 100644
index 00000000000..e6659230879
--- /dev/null
+++ b/lib/private/Profiler/RoutingDataCollector.php
@@ -0,0 +1,55 @@
+<?php
+
+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/>.
+ *
+ */
+
+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';
+ }
+}