From 00349527e136f7f96502d0f61f5aaeceb8617e34 Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Sun, 28 Oct 2018 11:47:12 +0100 Subject: [PATCH 1/2] calculate IobCobThread execution time --- .../androidaps/plugins/IobCobCalculator/IobCobOref1Thread.java | 3 +++ .../androidaps/plugins/IobCobCalculator/IobCobThread.java | 3 +++ 2 files changed, 6 insertions(+) diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/IobCobCalculator/IobCobOref1Thread.java b/app/src/main/java/info/nightscout/androidaps/plugins/IobCobCalculator/IobCobOref1Thread.java index 049fb031bc..97de1927a7 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/IobCobCalculator/IobCobOref1Thread.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/IobCobCalculator/IobCobOref1Thread.java @@ -38,6 +38,7 @@ import info.nightscout.androidaps.plugins.Treatments.TreatmentsPlugin; import info.nightscout.utils.DateUtil; import info.nightscout.utils.DecimalFormatter; import info.nightscout.utils.FabricPrivacy; +import info.nightscout.utils.Profiler; import info.nightscout.utils.SP; import static info.nightscout.utils.DateUtil.now; @@ -75,6 +76,7 @@ public class IobCobOref1Thread extends Thread { @Override public final void run() { + long start = DateUtil.now(); mWakeLock.acquire(); try { if (L.isEnabled(L.AUTOSENS)) @@ -395,6 +397,7 @@ public class IobCobOref1Thread extends Thread { MainApp.bus().post(new EventIobCalculationProgress("")); if (L.isEnabled(L.AUTOSENS)) log.debug("AUTOSENSDATA thread ended: " + from); + Profiler.log(log, "IobCobOref1Thread", start); } } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/IobCobCalculator/IobCobThread.java b/app/src/main/java/info/nightscout/androidaps/plugins/IobCobCalculator/IobCobThread.java index 7229532446..80aaf9450c 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/IobCobCalculator/IobCobThread.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/IobCobCalculator/IobCobThread.java @@ -38,6 +38,7 @@ import info.nightscout.androidaps.plugins.Treatments.TreatmentsPlugin; import info.nightscout.utils.DateUtil; import info.nightscout.utils.DecimalFormatter; import info.nightscout.utils.FabricPrivacy; +import info.nightscout.utils.Profiler; import info.nightscout.utils.SP; import static info.nightscout.utils.DateUtil.now; @@ -74,6 +75,7 @@ public class IobCobThread extends Thread { @Override public final void run() { + long start = DateUtil.now(); mWakeLock.acquire(); try { if (L.isEnabled(L.AUTOSENS)) @@ -322,6 +324,7 @@ public class IobCobThread extends Thread { MainApp.bus().post(new EventIobCalculationProgress("")); if (L.isEnabled(L.AUTOSENS)) log.debug("AUTOSENSDATA thread ended: " + from); + Profiler.log(log, "IobCobThread", start); } } From d54651e916fa457fb9a30204b9dc02c9ae9efc30 Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Sun, 28 Oct 2018 12:21:35 +0100 Subject: [PATCH 2/2] cache midnight time calculation --- .../nightscout/androidaps/data/Profile.java | 30 +++++-------- .../IobCobCalculator/IobCobOref1Thread.java | 5 ++- .../IobCobCalculator/IobCobThread.java | 5 ++- .../info/nightscout/utils/MidnightTime.java | 43 +++++++++++++++++++ 4 files changed, 61 insertions(+), 22 deletions(-) create mode 100644 app/src/main/java/info/nightscout/utils/MidnightTime.java diff --git a/app/src/main/java/info/nightscout/androidaps/data/Profile.java b/app/src/main/java/info/nightscout/androidaps/data/Profile.java index 8d78ca19b6..0595c2f84d 100644 --- a/app/src/main/java/info/nightscout/androidaps/data/Profile.java +++ b/app/src/main/java/info/nightscout/androidaps/data/Profile.java @@ -23,6 +23,7 @@ import info.nightscout.androidaps.plugins.Overview.notifications.Notification; import info.nightscout.utils.DateUtil; import info.nightscout.utils.DecimalFormatter; import info.nightscout.utils.FabricPrivacy; +import info.nightscout.utils.MidnightTime; public class Profile { private static Logger log = LoggerFactory.getLogger(Profile.class); @@ -381,7 +382,7 @@ public class Profile { } public double getIsf() { - return getIsfTimeFromMidnight(secondsFromMidnight(System.currentTimeMillis())); + return getIsfTimeFromMidnight(secondsFromMidnight()); } public double getIsf(long time) { @@ -401,7 +402,7 @@ public class Profile { } public double getIc() { - return getIcTimeFromMidnight(secondsFromMidnight(System.currentTimeMillis())); + return getIcTimeFromMidnight(secondsFromMidnight()); } public double getIc(long time) { @@ -421,7 +422,7 @@ public class Profile { } public double getBasal() { - return getBasalTimeFromMidnight(secondsFromMidnight(System.currentTimeMillis())); + return getBasalTimeFromMidnight(secondsFromMidnight()); } public double getBasal(long time) { @@ -465,7 +466,7 @@ public class Profile { } public double getTarget() { - return getTarget(secondsFromMidnight(System.currentTimeMillis())); + return getTarget(secondsFromMidnight()); } protected double getTarget(int timeAsSeconds) { @@ -473,7 +474,7 @@ public class Profile { } public double getTargetLow() { - return getTargetLowTimeFromMidnight(secondsFromMidnight(System.currentTimeMillis())); + return getTargetLowTimeFromMidnight(secondsFromMidnight()); } public double getTargetLow(long time) { @@ -487,7 +488,7 @@ public class Profile { } public double getTargetHigh() { - return getTargetHighTimeFromMidnight(secondsFromMidnight(System.currentTimeMillis())); + return getTargetHighTimeFromMidnight(secondsFromMidnight()); } public double getTargetHigh(long time) { @@ -518,24 +519,13 @@ public class Profile { } public static int secondsFromMidnight() { - Calendar c = Calendar.getInstance(); - long now = c.getTimeInMillis(); - c.set(Calendar.HOUR_OF_DAY, 0); - c.set(Calendar.MINUTE, 0); - c.set(Calendar.SECOND, 0); - c.set(Calendar.MILLISECOND, 0); - long passed = now - c.getTimeInMillis(); + long passed = DateUtil.now() - MidnightTime.calc(); return (int) (passed / 1000); } public static int secondsFromMidnight(long date) { - Calendar c = Calendar.getInstance(); - c.setTimeInMillis(date); - c.set(Calendar.HOUR_OF_DAY, 0); - c.set(Calendar.MINUTE, 0); - c.set(Calendar.SECOND, 0); - c.set(Calendar.MILLISECOND, 0); - long passed = date - c.getTimeInMillis(); + long midnight = MidnightTime.calc(date); + long passed = date - midnight; return (int) (passed / 1000); } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/IobCobCalculator/IobCobOref1Thread.java b/app/src/main/java/info/nightscout/androidaps/plugins/IobCobCalculator/IobCobOref1Thread.java index 97de1927a7..172a6bbc47 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/IobCobCalculator/IobCobOref1Thread.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/IobCobCalculator/IobCobOref1Thread.java @@ -38,6 +38,7 @@ import info.nightscout.androidaps.plugins.Treatments.TreatmentsPlugin; import info.nightscout.utils.DateUtil; import info.nightscout.utils.DecimalFormatter; import info.nightscout.utils.FabricPrivacy; +import info.nightscout.utils.MidnightTime; import info.nightscout.utils.Profiler; import info.nightscout.utils.SP; @@ -395,8 +396,10 @@ public class IobCobOref1Thread extends Thread { } finally { mWakeLock.release(); MainApp.bus().post(new EventIobCalculationProgress("")); - if (L.isEnabled(L.AUTOSENS)) + if (L.isEnabled(L.AUTOSENS)) { log.debug("AUTOSENSDATA thread ended: " + from); + log.debug("Midnights: " + MidnightTime.log()); + } Profiler.log(log, "IobCobOref1Thread", start); } } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/IobCobCalculator/IobCobThread.java b/app/src/main/java/info/nightscout/androidaps/plugins/IobCobCalculator/IobCobThread.java index 80aaf9450c..aa1f09592e 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/IobCobCalculator/IobCobThread.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/IobCobCalculator/IobCobThread.java @@ -38,6 +38,7 @@ import info.nightscout.androidaps.plugins.Treatments.TreatmentsPlugin; import info.nightscout.utils.DateUtil; import info.nightscout.utils.DecimalFormatter; import info.nightscout.utils.FabricPrivacy; +import info.nightscout.utils.MidnightTime; import info.nightscout.utils.Profiler; import info.nightscout.utils.SP; @@ -322,8 +323,10 @@ public class IobCobThread extends Thread { } finally { mWakeLock.release(); MainApp.bus().post(new EventIobCalculationProgress("")); - if (L.isEnabled(L.AUTOSENS)) + if (L.isEnabled(L.AUTOSENS)) { log.debug("AUTOSENSDATA thread ended: " + from); + log.debug("Midnights: " + MidnightTime.log()); + } Profiler.log(log, "IobCobThread", start); } } diff --git a/app/src/main/java/info/nightscout/utils/MidnightTime.java b/app/src/main/java/info/nightscout/utils/MidnightTime.java new file mode 100644 index 0000000000..523fe13826 --- /dev/null +++ b/app/src/main/java/info/nightscout/utils/MidnightTime.java @@ -0,0 +1,43 @@ +package info.nightscout.utils; + +import android.util.LongSparseArray; + +import java.util.Calendar; + +public class MidnightTime { + private static LongSparseArray times = new LongSparseArray(); + + private static long hits = 0; + private static long misses = 0; + + public static long calc() { + Calendar c = Calendar.getInstance(); + c.set(Calendar.HOUR_OF_DAY, 0); + c.set(Calendar.MINUTE, 0); + c.set(Calendar.SECOND, 0); + c.set(Calendar.MILLISECOND, 0); + return c.getTimeInMillis(); + } + + public static long calc(long time) { + Long m = (Long) times.get(time); + if (m != null) { + ++hits; + return m; + } + Calendar c = Calendar.getInstance(); + c.setTimeInMillis(time); + c.set(Calendar.HOUR_OF_DAY, 0); + c.set(Calendar.MINUTE, 0); + c.set(Calendar.SECOND, 0); + c.set(Calendar.MILLISECOND, 0); + m = c.getTimeInMillis(); + times.append(time, m); + ++misses; + return m; + } + + public static String log() { + return "Hits: " + hits + " misses: " + misses + " stored: " + times.size(); + } +}