Skip to content

Commit

Permalink
fix: remove cpu core detection for preview semaphore
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
kesselb committed May 21, 2023
1 parent 96f0118 commit cb3861c
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 62 deletions.
6 changes: 3 additions & 3 deletions config/config.sample.php
Original file line number Diff line number Diff line change
Expand Up @@ -1146,7 +1146,7 @@
* 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,

Expand All @@ -1155,9 +1155,9 @@
*
* 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,

Expand Down
60 changes: 2 additions & 58 deletions lib/private/Preview/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -292,62 +292,6 @@ public static function unguardWithSemaphore($semId): bool {
return sem_release($semId);
}

/**
* 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
Expand Down Expand Up @@ -391,7 +335,7 @@ private function generateProviderPreview(ISimpleFolder $previewFolder, File $fil
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);
Expand Down Expand Up @@ -562,7 +506,7 @@ private function generatePreview(ISimpleFolder $previewFolder, IImage $maxPrevie
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) {
Expand Down
2 changes: 1 addition & 1 deletion lib/private/PreviewManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ private function getGenerator(): Generator {
* @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);
Expand Down

0 comments on commit cb3861c

Please sign in to comment.