diff options
author | Daniel Kesselberg <mail@danielkesselberg.de> | 2023-05-21 21:55:09 +0200 |
---|---|---|
committer | Daniel <mail@danielkesselberg.de> | 2023-07-21 17:21:55 +0200 |
commit | 30f8c07c6e2a05d0c5f1c40f1e93d805a2cd434f (patch) | |
tree | 290e3c46ee142d319d7834df4601bef55f1b0033 | |
parent | d0957ffd977839367b376fae55b48ae34440350e (diff) | |
download | nextcloud-server-fix-remove-auto-guessing-for-preview-semaphore.tar.gz nextcloud-server-fix-remove-auto-guessing-for-preview-semaphore.zip |
fix: remove cpu core detection for preview semaphorefix-remove-auto-guessing-for-preview-semaphore
The idea of automatically limiting the number of concurrent preview requests to the number of CPU cores is nice.
Unfortunately it's difficult to determinate the number of CPU cores (e.g. open_basedir, freebsd, etc.).
This patch removes the detection and set the default for requests (existing + new) to 8 and generation to 4.
Signed-off-by: Daniel Kesselberg <mail@danielkesselberg.de>
-rw-r--r-- | config/config.sample.php | 6 | ||||
-rw-r--r-- | lib/private/Preview/Generator.php | 60 | ||||
-rw-r--r-- | lib/private/PreviewManager.php | 2 |
3 files changed, 6 insertions, 62 deletions
diff --git a/config/config.sample.php b/config/config.sample.php index 210d0a8e8ce..7de904d7c87 100644 --- a/config/config.sample.php +++ b/config/config.sample.php @@ -1161,7 +1161,7 @@ $CONFIG = [ * been generated. * * This should be greater than 'preview_concurrency_new'. - * If unspecified, defaults to twice the value of 'preview_concurrency_new'. + * Defaults to 8 */ 'preview_concurrency_all' => 8, @@ -1170,9 +1170,9 @@ $CONFIG = [ * * Depending on the max preview size set by 'preview_max_x' and 'preview_max_y', * the generation process can consume considerable CPU and memory resources. + * * It's recommended to limit this to be no greater than the number of CPU cores. - * If unspecified, defaults to the number of CPU cores, or 4 if that cannot - * be determined. + * Defaults to 4 */ 'preview_concurrency_new' => 4, diff --git a/lib/private/Preview/Generator.php b/lib/private/Preview/Generator.php index c9949c82a97..f6d3792e337 100644 --- a/lib/private/Preview/Generator.php +++ b/lib/private/Preview/Generator.php @@ -297,62 +297,6 @@ class Generator { } /** - * Get the number of concurrent threads supported by the host. - * - * @return int number of concurrent threads, or 0 if it cannot be determined - */ - public static function getHardwareConcurrency(): int { - static $width; - if (!isset($width)) { - if (is_file("/proc/cpuinfo")) { - $width = substr_count(file_get_contents("/proc/cpuinfo"), "processor"); - } else { - $width = 0; - } - } - return $width; - } - - /** - * Get number of concurrent preview generations from system config - * - * Two config entries, `preview_concurrency_new` and `preview_concurrency_all`, - * are available. If not set, the default values are determined with the hardware concurrency - * of the host. In case the hardware concurrency cannot be determined, or the user sets an - * invalid value, fallback values are: - * For new images whose previews do not exist and need to be generated, 4; - * For all preview generation requests, 8. - * Value of `preview_concurrency_all` should be greater than or equal to that of - * `preview_concurrency_new`, otherwise, the latter is returned. - * - * @param string $type either `preview_concurrency_new` or `preview_concurrency_all` - * @return int number of concurrent preview generations, or -1 if $type is invalid - */ - public function getNumConcurrentPreviews(string $type): int { - static $cached = array(); - if (array_key_exists($type, $cached)) { - return $cached[$type]; - } - - $hardwareConcurrency = self::getHardwareConcurrency(); - switch ($type) { - case "preview_concurrency_all": - $fallback = $hardwareConcurrency > 0 ? $hardwareConcurrency * 2 : 8; - $concurrency_all = $this->config->getSystemValueInt($type, $fallback); - $concurrency_new = $this->getNumConcurrentPreviews("preview_concurrency_new"); - $cached[$type] = max($concurrency_all, $concurrency_new); - break; - case "preview_concurrency_new": - $fallback = $hardwareConcurrency > 0 ? $hardwareConcurrency : 4; - $cached[$type] = $this->config->getSystemValueInt($type, $fallback); - break; - default: - return -1; - } - return $cached[$type]; - } - - /** * @param ISimpleFolder $previewFolder * @param ISimpleFile[] $previewFiles * @param File $file @@ -395,7 +339,7 @@ class Generator { continue; } - $previewConcurrency = $this->getNumConcurrentPreviews('preview_concurrency_new'); + $previewConcurrency = $this->config->getSystemValueInt('preview_concurrency_new', 4); $sem = self::guardWithSemaphore(self::SEMAPHORE_ID_NEW, $previewConcurrency); try { $preview = $this->helper->getThumbnail($provider, $file, $width, $height); @@ -566,7 +510,7 @@ class Generator { throw new \InvalidArgumentException('Failed to generate preview, failed to load image'); } - $previewConcurrency = $this->getNumConcurrentPreviews('preview_concurrency_new'); + $previewConcurrency = $this->config->getSystemValueInt('preview_concurrency_new', 4); $sem = self::guardWithSemaphore(self::SEMAPHORE_ID_NEW, $previewConcurrency); try { if ($crop) { diff --git a/lib/private/PreviewManager.php b/lib/private/PreviewManager.php index 814235f4212..647acd9376e 100644 --- a/lib/private/PreviewManager.php +++ b/lib/private/PreviewManager.php @@ -186,7 +186,7 @@ class PreviewManager implements IPreview { * @since 11.0.0 - \InvalidArgumentException was added in 12.0.0 */ public function getPreview(File $file, $width = -1, $height = -1, $crop = false, $mode = IPreview::MODE_FILL, $mimeType = null) { - $previewConcurrency = $this->getGenerator()->getNumConcurrentPreviews('preview_concurrency_all'); + $previewConcurrency = $this->config->getSystemValueInt('preview_concurrency_all', 8); $sem = Generator::guardWithSemaphore(Generator::SEMAPHORE_ID_ALL, $previewConcurrency); try { $preview = $this->getGenerator()->getPreview($file, $width, $height, $crop, $mode, $mimeType); |