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 049fb031bc..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,8 @@ 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; import static info.nightscout.utils.DateUtil.now; @@ -75,6 +77,7 @@ public class IobCobOref1Thread extends Thread { @Override public final void run() { + long start = DateUtil.now(); mWakeLock.acquire(); try { if (L.isEnabled(L.AUTOSENS)) @@ -393,8 +396,11 @@ 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 7229532446..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,8 @@ 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; import static info.nightscout.utils.DateUtil.now; @@ -74,6 +76,7 @@ public class IobCobThread extends Thread { @Override public final void run() { + long start = DateUtil.now(); mWakeLock.acquire(); try { if (L.isEnabled(L.AUTOSENS)) @@ -320,8 +323,11 @@ 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(); + } +}