From aaf4187631e6ebfeeed8439fdc4f71cd1603cb9b Mon Sep 17 00:00:00 2001 From: M66B Date: Sun, 5 Sep 2021 18:33:16 +0200 Subject: [PATCH] Added debug option to configure sqlite cache size --- .../java/eu/faircode/email/ApplicationEx.java | 8 +-- app/src/main/java/eu/faircode/email/DB.java | 6 ++- .../faircode/email/FragmentOptionsMisc.java | 52 ++++++++++++++++--- .../main/res/layout/fragment_options_misc.xml | 46 +++++++++++++++- app/src/main/res/values/strings.xml | 1 + 5 files changed, 99 insertions(+), 14 deletions(-) diff --git a/app/src/main/java/eu/faircode/email/ApplicationEx.java b/app/src/main/java/eu/faircode/email/ApplicationEx.java index 09da844119..3f7b57b4d6 100644 --- a/app/src/main/java/eu/faircode/email/ApplicationEx.java +++ b/app/src/main/java/eu/faircode/email/ApplicationEx.java @@ -245,7 +245,7 @@ public class ApplicationEx extends Application case "query_threads": // misc case "wal": // misc // Should be excluded for import - restart(); + restart(this); break; case "debug": case "log_level": @@ -254,10 +254,10 @@ public class ApplicationEx extends Application } } - void restart() { - Intent intent = new Intent(this, ActivityMain.class); + static void restart(Context context) { + Intent intent = new Intent(context, ActivityMain.class); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); - startActivity(intent); + context.startActivity(intent); Runtime.getRuntime().exit(0); } diff --git a/app/src/main/java/eu/faircode/email/DB.java b/app/src/main/java/eu/faircode/email/DB.java index 329f155ebe..09b63079be 100644 --- a/app/src/main/java/eu/faircode/email/DB.java +++ b/app/src/main/java/eu/faircode/email/DB.java @@ -117,9 +117,10 @@ public abstract class DB extends RoomDatabase { private static Context sContext; private static DB sInstance; + static final int DB_DEFAULT_CACHE = 5; // percentage + private static final String DB_NAME = "fairemail"; private static final int DB_CHECKPOINT = 1000; // requery/sqlite-android default - private static final int DB_CACHE_PERCENTAGE = 3; private static final String[] DB_TABLES = new String[]{ "identity", "account", "folder", "message", "attachment", "operation", "contact", "certificate", "answer", "rule", "log"}; @@ -374,6 +375,7 @@ public abstract class DB extends RoomDatabase { SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); int threads = prefs.getInt("query_threads", 4); // AndroidX default thread count: 4 boolean wal = prefs.getBoolean("wal", true); + int sqlite_cache = prefs.getInt("sqlite_cache", DB.DB_DEFAULT_CACHE); Log.i("DB query threads=" + threads + " wal=" + wal); ExecutorService executorQuery = Helper.getBackgroundExecutor(threads, "query"); ExecutorService executorTransaction = Helper.getBackgroundExecutor(0, "transaction"); @@ -404,7 +406,7 @@ public abstract class DB extends RoomDatabase { try { ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); int class_mb = am.getMemoryClass(); - int cache_size = DB_CACHE_PERCENTAGE * class_mb * 1024 / 100; + int cache_size = sqlite_cache * class_mb * 1024 / 100; if (cache_size > 2000) { // https://www.sqlite.org/pragma.html#pragma_cache_size cache_size = -cache_size; // kibibytes diff --git a/app/src/main/java/eu/faircode/email/FragmentOptionsMisc.java b/app/src/main/java/eu/faircode/email/FragmentOptionsMisc.java index 5f6890889e..8d0de44c2c 100644 --- a/app/src/main/java/eu/faircode/email/FragmentOptionsMisc.java +++ b/app/src/main/java/eu/faircode/email/FragmentOptionsMisc.java @@ -124,6 +124,9 @@ public class FragmentOptionsMisc extends FragmentBase implements SharedPreferenc private SwitchCompat swDebug; private SwitchCompat swQueries; private SwitchCompat swWal; + private TextView tvSqliteCache; + private SeekBar sbSqliteCache; + private ImageButton ibSqliteCache; private SwitchCompat swModSeq; private SwitchCompat swExpunge; private SwitchCompat swAuthPlain; @@ -154,7 +157,7 @@ public class FragmentOptionsMisc extends FragmentBase implements SharedPreferenc "shortcuts", "fts", "classification", "class_min_probability", "class_min_difference", "language", "deepl_enabled", "watchdog", "updates", "weekly", - "experiments", "wal", "query_threads", "crash_reports", "cleanup_attachments", + "experiments", "wal", "query_threads", "sqlite_cache", "crash_reports", "cleanup_attachments", "protocol", "debug", "log_level", "use_modseq", "perform_expunge", "auth_plain", "auth_login", "auth_ntlm", "auth_sasl", @@ -236,6 +239,9 @@ public class FragmentOptionsMisc extends FragmentBase implements SharedPreferenc swDebug = view.findViewById(R.id.swDebug); swQueries = view.findViewById(R.id.swQueries); swWal = view.findViewById(R.id.swWal); + tvSqliteCache = view.findViewById(R.id.tvSqliteCache); + sbSqliteCache = view.findViewById(R.id.sbSqliteCache); + ibSqliteCache = view.findViewById(R.id.ibSqliteCache); swModSeq = view.findViewById(R.id.swModSeq); swExpunge = view.findViewById(R.id.swExpunge); swAuthPlain = view.findViewById(R.id.swAuthPlain); @@ -608,6 +614,30 @@ public class FragmentOptionsMisc extends FragmentBase implements SharedPreferenc } }); + sbSqliteCache.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { + @Override + public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { + prefs.edit().putInt("sqlite_cache", progress).apply(); + } + + @Override + public void onStartTrackingTouch(SeekBar seekBar) { + // Do nothing + } + + @Override + public void onStopTrackingTouch(SeekBar seekBar) { + // Do nothing + } + }); + + ibSqliteCache.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + ApplicationEx.restart(v.getContext()); + } + }); + swProtocol.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton compoundButton, boolean checked) { @@ -1010,6 +1040,12 @@ public class FragmentOptionsMisc extends FragmentBase implements SharedPreferenc private void setOptions() { SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext()); + ActivityManager am = (ActivityManager) getContext().getSystemService(Context.ACTIVITY_SERVICE); + int class_mb = am.getMemoryClass(); + int class_large_mb = am.getLargeMemoryClass(); + ActivityManager.MemoryInfo mi = new ActivityManager.MemoryInfo(); + am.getMemoryInfo(mi); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) swPowerMenu.setChecked(Helper.isComponentEnabled(getContext(), ServicePowerControl.class)); swExternalSearch.setChecked(Helper.isComponentEnabled(getContext(), ActivitySearch.class)); @@ -1061,6 +1097,14 @@ public class FragmentOptionsMisc extends FragmentBase implements SharedPreferenc swDebug.setChecked(prefs.getBoolean("debug", false)); swQueries.setChecked(prefs.getInt("query_threads", 4) < 4); swWal.setChecked(prefs.getBoolean("wal", true)); + + int sqlite_cache = prefs.getInt("sqlite_cache", DB.DB_DEFAULT_CACHE); + int cache_size = sqlite_cache * class_mb * 1024 / 100; + tvSqliteCache.setText(getString(R.string.title_advanced_sqlite_cache, + NF.format(sqlite_cache), + Helper.humanReadableByteCount(cache_size * 1024L))); + sbSqliteCache.setProgress(sqlite_cache); + swModSeq.setChecked(prefs.getBoolean("use_modseq", true)); swExpunge.setChecked(prefs.getBoolean("perform_expunge", true)); swAuthPlain.setChecked(prefs.getBoolean("auth_plain", true)); @@ -1071,12 +1115,6 @@ public class FragmentOptionsMisc extends FragmentBase implements SharedPreferenc swTestIab.setChecked(prefs.getBoolean("test_iab", false)); tvProcessors.setText(getString(R.string.title_advanced_processors, Runtime.getRuntime().availableProcessors())); - - ActivityManager am = (ActivityManager) getContext().getSystemService(Context.ACTIVITY_SERVICE); - int class_mb = am.getMemoryClass(); - int class_large_mb = am.getLargeMemoryClass(); - ActivityManager.MemoryInfo mi = new ActivityManager.MemoryInfo(); - am.getMemoryInfo(mi); tvMemoryClass.setText(getString(R.string.title_advanced_memory_class, class_mb + " MB", class_large_mb + " MB", diff --git a/app/src/main/res/layout/fragment_options_misc.xml b/app/src/main/res/layout/fragment_options_misc.xml index 610ab50db0..2b6ebda138 100644 --- a/app/src/main/res/layout/fragment_options_misc.xml +++ b/app/src/main/res/layout/fragment_options_misc.xml @@ -640,6 +640,50 @@ app:layout_constraintTop_toBottomOf="@id/tvQueriesRemark" app:switchPadding="12dp" /> + + + + + + + + Debug mode Limit parallel database access WAL + Sqlite cache %1$s %% - %2$s MODSEQ EXPUNGE PLAIN