Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions lib/private/Preview/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use OCP\Files\SimpleFS\InMemoryFile;
use OCP\Files\SimpleFS\ISimpleFile;
use OCP\IAppConfig;
use OCP\ICacheFactory;
use OCP\IConfig;
use OCP\IImage;
use OCP\Image;
Expand All @@ -46,6 +47,7 @@ public function __construct(
private readonly PreviewMapper $previewMapper,
private readonly StorageFactory $storageFactory,
private readonly PreviewMigrationService $migrationService,
private readonly ICacheFactory $cacheFactory,
) {
}

Expand Down Expand Up @@ -261,7 +263,19 @@ public static function unguardWithSemaphore(false|\SysvSemaphore $semId): bool {
*
* @return int number of concurrent threads, or 0 if it cannot be determined
*/
private static function getHardwareConcurrency(): int {
private function getHardwareConcurrency(): int {
// /proc/cpuinfo is slow to read
$cache = $this->cacheFactory->createLocal('preview');
$cached = $cache->get('hardware_concurrency');
if (is_int($cached)) {
return $cached;
}
$concurrency = self::readHardwareConcurrency();
$cache->set('hardware_concurrency', $concurrency, 3600);
return $concurrency;
}

private static function readHardwareConcurrency(): int {
if (function_exists('ini_get')) {
$openBasedir = ini_get('open_basedir');
if (empty($openBasedir) || strpos($openBasedir, '/proc/cpuinfo') !== false) {
Expand Down Expand Up @@ -291,7 +305,7 @@ public function getNumConcurrentPreviews(string $type): int {
return $this->cachedNumConcurrentPreviews[$type];
}

$hardwareConcurrency = self::getHardwareConcurrency();
$hardwareConcurrency = $this->getHardwareConcurrency();
switch ($type) {
case 'preview_concurrency_all':
$fallback = $hardwareConcurrency > 0 ? $hardwareConcurrency * 2 : 8;
Expand Down
2 changes: 2 additions & 0 deletions lib/private/PreviewManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
use OCP\Files\SimpleFS\ISimpleFile;
use OCP\IAppConfig;
use OCP\IBinaryFinder;
use OCP\ICacheFactory;
use OCP\IConfig;
use OCP\IPreview;
use OCP\Preview\IProviderV2;
Expand Down Expand Up @@ -161,6 +162,7 @@ private function getGenerator(): Generator {
$this->container->get(PreviewMapper::class),
$this->container->get(StorageFactory::class),
$this->container->get(PreviewMigrationService::class),
$this->container->get(ICacheFactory::class),
);
}
return $this->generator;
Expand Down
22 changes: 22 additions & 0 deletions tests/lib/Preview/GeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,28 @@ public function testCorrectSize(int $maxX, int $maxY, int $reqX, int $reqY, bool
}
}

public function testHardwareConcurrencyFromCache(): void {
$this->getAutoMock(IConfig::class)->method('getSystemValueInt')
->willReturnCallback(fn ($key, $default) => $default);
$cache = $this->getCacheAutoMock('preview');
$cache->method('get')->with('hardware_concurrency')->willReturn(3);
$cache->expects($this->never())->method('set');

$this->assertSame(6, $this->generator->getNumConcurrentPreviews('preview_concurrency_all'));
$this->assertSame(3, $this->generator->getNumConcurrentPreviews('preview_concurrency_new'));
}

public function testHardwareConcurrencyIsCached(): void {
$this->getAutoMock(IConfig::class)->method('getSystemValueInt')
->willReturnCallback(fn ($key, $default) => $default);
$cache = $this->getCacheAutoMock('preview');
$cache->method('get')->with('hardware_concurrency')->willReturn(null);
$cache->expects($this->once())->method('set')
->with('hardware_concurrency', $this->isInt(), 3600);

$this->generator->getNumConcurrentPreviews('preview_concurrency_new');
}

public function testUnreadbleFile(): void {
$file = $this->createMock(File::class);
$file->method('isReadable')
Expand Down
Loading