From 1c23fc14be8a0ac9542f1412448c4d896756ba01 Mon Sep 17 00:00:00 2001 From: Keuin Date: Fri, 22 Jan 2021 18:59:47 +0800 Subject: Speed up the incremental backup by using multiple CPU cores if available (use multiple threads to calculate the hash). --- .../keuin/kbackupfabric/util/ThreadingUtil.java | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 src/main/java/com/keuin/kbackupfabric/util/ThreadingUtil.java (limited to 'src/main/java/com/keuin/kbackupfabric/util/ThreadingUtil.java') diff --git a/src/main/java/com/keuin/kbackupfabric/util/ThreadingUtil.java b/src/main/java/com/keuin/kbackupfabric/util/ThreadingUtil.java new file mode 100644 index 0000000..b270895 --- /dev/null +++ b/src/main/java/com/keuin/kbackupfabric/util/ThreadingUtil.java @@ -0,0 +1,22 @@ +package com.keuin.kbackupfabric.util; + +public class ThreadingUtil { + public static int getRecommendedThreadCount() { + int coreCount = Runtime.getRuntime().availableProcessors(); + + // if the cores are too few, we regress to single thread + if (coreCount <= 2) + return 1; + + // we have multiple cores, but not too many + if (coreCount == 3) + return 2; + + // if we have multiple core, but not too many, we use a half + if (coreCount <= 6) // 4, 5, 6 -> 3, 3, 4 + return (coreCount + 2) / 2; + + // cores are sufficient, we use almost all of them, except a fixed count remained for the OS + return coreCount - 2; + } +} -- cgit v1.2.3