From 2a817e21cb3b047a3b8c9909b072892506763bc1 Mon Sep 17 00:00:00 2001 From: Nick Craig-Wood Date: Thu, 28 Jul 2022 17:43:05 +0100 Subject: [PATCH] vfs: fix excess CPU used by VFS cache cleaner looping Before this change the VFS cache cleaner would loop indefinitely while the cache was above quota. This used up all the CPU. This fix prevents the cache cleaner from looping. It will be kicked on ENOSPACE and run in its scheduled time otherwise so this should be sufficient. See: https://forum.rclone.org/t/vfs-keeps-checking-same-files/32120 --- vfs/vfscache/cache.go | 20 +++++--------------- 1 file changed, 5 insertions(+), 15 deletions(-) diff --git a/vfs/vfscache/cache.go b/vfs/vfscache/cache.go index 8e5209a79..9c0ec0bf0 100644 --- a/vfs/vfscache/cache.go +++ b/vfs/vfscache/cache.go @@ -739,27 +739,17 @@ func (c *Cache) clean(kicked bool) { oldItems, oldUsed := len(c.item), fs.SizeSuffix(c.used) c.mu.Unlock() - // loop cleaning the cache until we reach below cache quota - for { - // Remove any files that are over age - c.purgeOld(c.opt.CacheMaxAge) + // Remove any files that are over age + c.purgeOld(c.opt.CacheMaxAge) - if int64(c.opt.CacheMaxSize) <= 0 { - break - } - - // Now remove files not in use until cache size is below quota starting from the - // oldest first + // If have a maximum cache size... + if int64(c.opt.CacheMaxSize) > 0 { + // Remove files not in use until cache size is below quota starting from the oldest first c.purgeOverQuota(int64(c.opt.CacheMaxSize)) // Remove cache files that are not dirty if we are still above the max cache size c.purgeClean(int64(c.opt.CacheMaxSize)) c.retryFailedResets() - - used := c.updateUsed() - if used <= int64(c.opt.CacheMaxSize) && len(c.errItems) == 0 { - break - } } // Was kicked?