discord-jadx/app/src/main/java/com/discord/utilities/system/DeviceResourceUsageMonitor.java
2021-08-18 09:29:27 +02:00

209 lines
7.1 KiB
Java

package com.discord.utilities.system;
import a0.a.a.b;
import android.system.Os;
import android.system.OsConstants;
import androidx.annotation.AnyThread;
import c.d.b.a.a;
import com.discord.api.permission.Permission;
import com.discord.utilities.system.ProcfsReader;
import com.discord.utilities.time.Clock;
import com.discord.utilities.time.TimeSpan;
import d0.k;
import d0.l;
import d0.z.d.m;
import kotlin.Unit;
import kotlin.jvm.functions.Function1;
import kotlin.jvm.internal.DefaultConstructorMarker;
/* compiled from: DeviceResourceUsageMonitor.kt */
public final class DeviceResourceUsageMonitor {
public static final Companion Companion = new Companion(null);
private static final long SC_CLK_TCK;
private static final long SC_PAGE_SIZE;
private Long activeThreadId;
private final Function1<ResourceUsage, Unit> callback;
private final Clock clock;
private Thread thread;
private final Object threadSync = new Object();
private final TimeSpan timeSpan;
/* compiled from: DeviceResourceUsageMonitor.kt */
public static final class Companion {
private Companion() {
}
public /* synthetic */ Companion(DefaultConstructorMarker defaultConstructorMarker) {
this();
}
}
/* compiled from: DeviceResourceUsageMonitor.kt */
public static final class ResourceUsage {
private final int cpuUsagePercent;
private final long memoryRssBytes;
public ResourceUsage(int i, long j) {
this.cpuUsagePercent = i;
this.memoryRssBytes = j;
}
public static /* synthetic */ ResourceUsage copy$default(ResourceUsage resourceUsage, int i, long j, int i2, Object obj) {
if ((i2 & 1) != 0) {
i = resourceUsage.cpuUsagePercent;
}
if ((i2 & 2) != 0) {
j = resourceUsage.memoryRssBytes;
}
return resourceUsage.copy(i, j);
}
public final int component1() {
return this.cpuUsagePercent;
}
public final long component2() {
return this.memoryRssBytes;
}
public final ResourceUsage copy(int i, long j) {
return new ResourceUsage(i, j);
}
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (!(obj instanceof ResourceUsage)) {
return false;
}
ResourceUsage resourceUsage = (ResourceUsage) obj;
return this.cpuUsagePercent == resourceUsage.cpuUsagePercent && this.memoryRssBytes == resourceUsage.memoryRssBytes;
}
public final int getCpuUsagePercent() {
return this.cpuUsagePercent;
}
public final long getMemoryRssBytes() {
return this.memoryRssBytes;
}
public int hashCode() {
return b.a(this.memoryRssBytes) + (this.cpuUsagePercent * 31);
}
public String toString() {
StringBuilder L = a.L("ResourceUsage(cpuUsagePercent=");
L.append(this.cpuUsagePercent);
L.append(", memoryRssBytes=");
return a.y(L, this.memoryRssBytes, ")");
}
}
static {
Long l;
Long l2;
try {
k.a aVar = k.i;
l = k.m75constructorimpl(Long.valueOf(Os.sysconf(OsConstants._SC_CLK_TCK)));
} catch (Throwable th) {
k.a aVar2 = k.i;
l = k.m75constructorimpl(l.createFailure(th));
}
if (k.m79isFailureimpl(l)) {
l = 100L;
}
SC_CLK_TCK = ((Number) l).longValue();
try {
k.a aVar3 = k.i;
l2 = k.m75constructorimpl(Long.valueOf(Os.sysconf(OsConstants._SC_PAGE_SIZE)));
} catch (Throwable th2) {
k.a aVar4 = k.i;
l2 = k.m75constructorimpl(l.createFailure(th2));
}
Long valueOf = Long.valueOf((long) Permission.SEND_TTS_MESSAGES);
if (k.m79isFailureimpl(l2)) {
l2 = valueOf;
}
SC_PAGE_SIZE = ((Number) l2).longValue();
}
/* JADX DEBUG: Multi-variable search result rejected for r4v0, resolved type: kotlin.jvm.functions.Function1<? super com.discord.utilities.system.DeviceResourceUsageMonitor$ResourceUsage, kotlin.Unit> */
/* JADX WARN: Multi-variable type inference failed */
public DeviceResourceUsageMonitor(TimeSpan timeSpan, Clock clock, Function1<? super ResourceUsage, Unit> function1) {
m.checkNotNullParameter(timeSpan, "timeSpan");
m.checkNotNullParameter(clock, "clock");
m.checkNotNullParameter(function1, "callback");
this.timeSpan = timeSpan;
this.clock = clock;
this.callback = function1;
}
public static final /* synthetic */ void access$monitor(DeviceResourceUsageMonitor deviceResourceUsageMonitor) {
deviceResourceUsageMonitor.monitor();
}
private final void monitor() {
ProcfsReader.Stat stat = null;
Long l = null;
while (true) {
synchronized (this.threadSync) {
Long l2 = this.activeThreadId;
Thread currentThread = Thread.currentThread();
m.checkNotNullExpressionValue(currentThread, "Thread.currentThread()");
long id2 = currentThread.getId();
if (l2 != null) {
if (l2.longValue() != id2) {
break;
}
} else {
break;
}
}
if (!Thread.interrupted()) {
ProcfsReader.Stat readStatFile = ProcfsReader.INSTANCE.readStatFile();
long currentTimeMillis = this.clock.currentTimeMillis();
if (!(stat == null || l == null)) {
this.callback.invoke(new ResourceUsage((int) (((double) ((readStatFile.getTotalTime() - stat.getTotalTime()) * ((long) 100))) / (((double) SC_CLK_TCK) * (((double) (currentTimeMillis - l.longValue())) / 1000.0d))), readStatFile.getRssPages() * SC_PAGE_SIZE));
}
l = Long.valueOf(currentTimeMillis);
Thread.sleep(this.timeSpan.toMillis());
stat = readStatFile;
} else {
return;
}
}
}
@AnyThread
public final void finalize() {
stop();
}
@AnyThread
public final void start() {
Thread thread;
synchronized (this.threadSync) {
thread = this.thread;
Thread thread$default = d0.v.a.thread$default(true, true, null, "DeviceResourceUsageMonitor", 2, new DeviceResourceUsageMonitor$start$$inlined$synchronized$lambda$1(this), 4, null);
this.activeThreadId = Long.valueOf(thread$default.getId());
this.thread = thread$default;
}
if (thread != null) {
thread.interrupt();
}
}
@AnyThread
public final void stop() {
Thread thread;
synchronized (this.threadSync) {
thread = this.thread;
this.thread = null;
this.activeThreadId = null;
}
if (thread != null) {
thread.interrupt();
}
}
}