sabeechen / hassio-google-drive-backup

Automatically create and sync Home Assistant backups into Google Drive
MIT License
3.21k stars 198 forks source link

Idle CPU Usage #978

Open PuffShr1k3 opened 10 months ago

PuffShr1k3 commented 10 months ago

Is it possible the idle CPU usage could be reduced please? Albeit the usage is low, but if sitting idle, could the CPU usage be brought down a bit - Other addons use 0% if not in use. image

I have lengthened the intervals to attempt to mitigate polling being a culprit: image

Nonetheless, thank you for this extension - Having fire and forget backups is a huge stress relief.

bdraco commented 10 months ago

cpu_time.zip

Here are py-spys for where the time is spent

bdraco commented 10 months ago

Its all the enumeration in the backup models. backups.models.backups

called from

    def nextBackup(self, now: datetime, include_pending=True):
        latest = max(filter(lambda s: not s.ignore() and (not s.isPending() or include_pending), self.backups.values()),
                     default=None, key=lambda s: s.date())
        if latest:
            latest = latest.date()
        return self._nextBackup(now, latest)

I think most of them could be changed to properties and use @cached_property than the overhead would likely disappear

sabeechen commented 9 months ago

There are some subtle problems caching those properties would cause.

I can make some little fixes that should cut the time used there in half, though its kind of mysterious that its spending so much time calculating .ignore(). Do you have a lot of backups?

I'll also add a new configuration option that the performance sensitive like yourself can use. Right now the addon checks once/second if it should make a backup so new backups always happen on time. I'll make this configurable so you can sacrifice a little scheduling accuracy to do less computation.

bdraco commented 9 months ago

At least on my systems:

system 1: 87 ignored backups. 4 non-ignored backups. system 2: 92 ignored backups. 4 non-ignored backups.

PuffShr1k3 commented 9 months ago

I noticed I had 40 ignored backups, deleted older backups, so now I only have 9 ignored, which reduced the CPU usage from 0.4% to 0.3%. I did notice that Google Drive backup identified the changes in the amount of backups very quickly, which ties in with how the adon checks once a second. I'm happy to sacrifice scheduling accuracy for less computational overhead. Thank you for looking into this, let me know if I can help test, provide logs, etc.