JNI memory stats

pull/214/head
M66B 1 year ago
parent f9311f19c8
commit 1815e3f2bc

@ -65,7 +65,6 @@ import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.CancellationSignal;
import android.os.Debug;
import android.os.OperationCanceledException;
import android.os.Parcel;
import android.os.Parcelable;
@ -7118,10 +7117,16 @@ public class FragmentMessages extends FragmentBase
if (!getLifecycle().getCurrentState().isAtLeast(Lifecycle.State.STARTED))
return;
Runtime rt = Runtime.getRuntime();
long hused = rt.totalMemory() - rt.freeMemory();
long hmax = rt.maxMemory();
long nheap = Debug.getNativeHeapAllocatedSize();
long[] stats = Log.jni_safe_runtime_stats();
if (stats == null) {
tvDebug.setText("OOM");
return;
}
long hused = stats[0] - stats[1];
long hmax = stats[2];
int processors = (int) stats[3];
long nheap = stats[4];
int perc = Math.round(hused * 100f / hmax);
int utilization = 0;
@ -7131,13 +7136,12 @@ public class FragmentMessages extends FragmentBase
int cpuDelta = (int) (cpu - lastCpu);
int timeDelta = (int) (time - lastTime);
if (timeDelta != 0)
utilization = 100 * cpuDelta / timeDelta / rt.availableProcessors();
utilization = 100 * cpuDelta / timeDelta / processors;
}
lastCpu = cpu;
lastTime = time;
tvDebug.setText(utilization + "%\n" + perc + "% " + (nheap / (1024 * 1024)) + "M"); // TODO CASA
tvDebug.setText(utilization + "%\n" + perc + "% " + (nheap / (1024 * 1024)) + "M");
}
private boolean handleThreadActions(

@ -245,6 +245,8 @@ public class Log {
public static native Process jni_safe_runtime_exec(Runtime runtime, String[] cmd);
public static native long[] jni_safe_runtime_stats();
public static void setLevel(Context context) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
boolean debug = prefs.getBoolean("debug", false);

@ -74,6 +74,43 @@ Java_eu_faircode_email_Log_jni_1safe_1runtime_1exec(JNIEnv *env, jclass clazz,
return env->CallObjectMethod(runtime, mid, cmd);
}
extern "C"
JNIEXPORT jlongArray JNICALL
Java_eu_faircode_email_Log_jni_1safe_1runtime_1stats(JNIEnv *env, jclass clazz) {
jclass clsRuntime = env->FindClass("java/lang/Runtime");
jmethodID mid = env->GetStaticMethodID(clsRuntime, "getRuntime", "()Ljava/lang/Runtime;");
jobject jruntime = env->CallStaticObjectMethod(clsRuntime, mid);
jmethodID midTotalMemory = env->GetMethodID(clsRuntime, "totalMemory", "()J");
jlong totalMemory = env->CallLongMethod(jruntime, midTotalMemory);
jmethodID midFreeMemory = env->GetMethodID(clsRuntime, "freeMemory", "()J");
jlong freeMemory = env->CallLongMethod(jruntime, midFreeMemory);
jmethodID midMaxMemory = env->GetMethodID(clsRuntime, "maxMemory", "()J");
jlong maxMemory = env->CallLongMethod(jruntime, midMaxMemory);
jmethodID midAvailableProcessors = env->GetMethodID(clsRuntime, "availableProcessors", "()I");
jlong availableProcessors = env->CallIntMethod(jruntime, midAvailableProcessors);
jclass clsDebug = env->FindClass("android/os/Debug");
jmethodID midGetNativeHeapAllocatedSize = env->GetStaticMethodID(clsDebug, "getNativeHeapAllocatedSize", "()J");
jlong getNativeHeapAllocatedSize = env->CallStaticLongMethod(clsDebug, midGetNativeHeapAllocatedSize);
jlongArray result = env->NewLongArray(5);
if (result == NULL)
return NULL; /* out of memory error thrown */
env->SetLongArrayRegion(result, 0, 1, &totalMemory);
env->SetLongArrayRegion(result, 1, 1, &freeMemory);
env->SetLongArrayRegion(result, 2, 1, &maxMemory);
env->SetLongArrayRegion(result, 3, 1, &availableProcessors);
env->SetLongArrayRegion(result, 4, 1, &getNativeHeapAllocatedSize);
return result;
}
extern "C"
JNIEXPORT jobject JNICALL
Java_eu_faircode_email_CharsetHelper_jni_1detect_1charset(

Loading…
Cancel
Save