package com.bytedance.sysoptimizer;

import android.os.Process;
import com.bytedance.covode.number.Covode;
import com.bytedance.frameworks.apm.trace.MethodCollector;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.Timer;
import java.util.TimerTask;
import java.util.regex.Pattern;

public class HeapGCOptimizer {
    public static final Pattern numPattern = Pattern.compile("[^0-9]");
    public static boolean sInited;
    public static float sShrinkRatio = 0.0f;
    public static int sShrinkStep = 0;
    public static int sShrinkSuccessCount = 0;
    public static Timer sVssCheckTimer;

    /* renamed from: com.bytedance.sysoptimizer.HeapGCOptimizer$1  reason: invalid class name */
    public static /* synthetic */ class AnonymousClass1 {
        static {
            Covode.recordClassIndex(38400);
        }
    }

    public static native boolean init(int i);

    public static native boolean optimize(int i, float f, float f2, float f3);

    public static native boolean shrink_regionspace_vss(long j);

    public static class VssCheckAndShrinkTask extends TimerTask {
        static {
            Covode.recordClassIndex(38401);
        }

        public VssCheckAndShrinkTask() {
        }

        public void run() {
            MethodCollector.i(14775);
            if (HeapGCOptimizer.sShrinkSuccessCount > 5) {
                HeapGCOptimizer.stopVssCheckTimer();
                MethodCollector.o(14775);
                return;
            }
            float readVssSize = ((float) HeapGCOptimizer.readVssSize()) / 4.2949673E9f;
            if (((double) readVssSize) < 1.0d && readVssSize > HeapGCOptimizer.sShrinkRatio) {
                if (!HeapGCOptimizer.shrink_regionspace_vss(((long) HeapGCOptimizer.sShrinkStep) * 1048576)) {
                    HeapGCOptimizer.stopVssCheckTimer();
                    MethodCollector.o(14775);
                    return;
                }
                HeapGCOptimizer.readVssSize();
                HeapGCOptimizer.sShrinkSuccessCount++;
            }
            MethodCollector.o(14775);
        }

        public /* synthetic */ VssCheckAndShrinkTask(AnonymousClass1 r1) {
            this();
        }
    }

    public static void stopVssCheckTimer() {
        Timer timer = sVssCheckTimer;
        if (timer != null) {
            timer.cancel();
            sVssCheckTimer = null;
        }
    }

    static {
        Covode.recordClassIndex(38399);
    }

    public static long readVssSize() {
        MethodCollector.i(15918);
        long j = -1;
        try {
            FileInputStream fileInputStream = new FileInputStream("/proc/" + Process.myPid() + "/status");
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(fileInputStream));
            while (true) {
                String readLine = bufferedReader.readLine();
                if (readLine == null) {
                    break;
                }
                String lowerCase = readLine.toLowerCase();
                if (lowerCase.contains("vmsize")) {
                    j = ((long) Integer.parseInt(numPattern.matcher(lowerCase).replaceAll("").trim())) * 1024;
                    break;
                }
            }
            fileInputStream.close();
            bufferedReader.close();
        } catch (Exception unused) {
        }
        MethodCollector.o(15918);
        return j;
    }

    public static void startVssCheckTimer(int i) {
        if (sVssCheckTimer != null) {
            stopVssCheckTimer();
        }
        Timer timer = new Timer();
        sVssCheckTimer = timer;
        long j = (long) (i * 1000);
        timer.schedule(new VssCheckAndShrinkTask((AnonymousClass1) null), j, j);
    }

    /* JADX WARNING: Code restructure failed: missing block: B:20:?, code lost:
        com.bytedance.frameworks.apm.trace.MethodCollector.o(15536);
     */
    /* JADX WARNING: Code restructure failed: missing block: B:22:0x002d, code lost:
        return;
     */
    /* JADX WARNING: Exception block dominator not found, dom blocks: [] */
    /* JADX WARNING: Missing exception handler attribute for start block: B:19:0x0029 */
    /* JADX WARNING: Missing exception handler attribute for start block: B:23:0x002e */
    /* Code decompiled incorrectly, please refer to instructions dump. */
    public static synchronized void optimize(android.content.Context r4, float r5, float r6, float r7) {
        /*
            java.lang.Class<com.bytedance.sysoptimizer.HeapGCOptimizer> r3 = com.bytedance.sysoptimizer.HeapGCOptimizer.class
            monitor-enter(r3)
            r2 = 15536(0x3cb0, float:2.177E-41)
            com.bytedance.frameworks.apm.trace.MethodCollector.i(r2)     // Catch:{ all -> 0x0033 }
            boolean r0 = com.bytedance.sysoptimizer.SysOptimizer.loadOptimizerLibrary(r4)     // Catch:{ all -> 0x0033 }
            if (r0 == 0) goto L_0x002e
            int r1 = android.os.Build.VERSION.SDK_INT     // Catch:{ UnsatisfiedLinkError -> 0x0029, NoSuchMethodError -> 0x002e }
            boolean r0 = sInited     // Catch:{ UnsatisfiedLinkError -> 0x0029, NoSuchMethodError -> 0x002e }
            if (r0 != 0) goto L_0x001d
            boolean r0 = init(r1)     // Catch:{ UnsatisfiedLinkError -> 0x0029, NoSuchMethodError -> 0x002e }
            if (r0 == 0) goto L_0x001d
            r0 = 1
            sInited = r0     // Catch:{ UnsatisfiedLinkError -> 0x0029, NoSuchMethodError -> 0x002e }
        L_0x001d:
            boolean r0 = sInited     // Catch:{ UnsatisfiedLinkError -> 0x0029, NoSuchMethodError -> 0x002e }
            if (r0 == 0) goto L_0x0024
            optimize((int) r1, (float) r5, (float) r6, (float) r7)     // Catch:{ UnsatisfiedLinkError -> 0x0029, NoSuchMethodError -> 0x002e }
        L_0x0024:
            com.bytedance.frameworks.apm.trace.MethodCollector.o(r2)     // Catch:{ all -> 0x0033 }
            monitor-exit(r3)
            return
        L_0x0029:
            com.bytedance.frameworks.apm.trace.MethodCollector.o(r2)     // Catch:{ all -> 0x0033 }
            monitor-exit(r3)
            return
        L_0x002e:
            com.bytedance.frameworks.apm.trace.MethodCollector.o(r2)     // Catch:{ all -> 0x0033 }
            monitor-exit(r3)
            return
        L_0x0033:
            r0 = move-exception
            monitor-exit(r3)
            throw r0
        */
        throw new UnsupportedOperationException("Method not decompiled: com.bytedance.sysoptimizer.HeapGCOptimizer.optimize(android.content.Context, float, float, float):void");
    }

    /* JADX WARNING: Code restructure failed: missing block: B:26:?, code lost:
        com.bytedance.frameworks.apm.trace.MethodCollector.o(15737);
     */
    /* JADX WARNING: Code restructure failed: missing block: B:28:0x0041, code lost:
        return;
     */
    /* JADX WARNING: Exception block dominator not found, dom blocks: [B:25:0x003d, B:29:0x0042] */
    /* JADX WARNING: Missing exception handler attribute for start block: B:25:0x003d */
    /* JADX WARNING: Missing exception handler attribute for start block: B:29:0x0042 */
    /* JADX WARNING: Unknown top exception splitter block from list: {B:29:0x0042=Splitter:B:29:0x0042, B:21:0x0038=Splitter:B:21:0x0038, B:25:0x003d=Splitter:B:25:0x003d} */
    /* Code decompiled incorrectly, please refer to instructions dump. */
    public static synchronized void optimizeRegionSpaceVSS(android.content.Context r6, float r7, int r8, int r9) {
        /*
            java.lang.Class<com.bytedance.sysoptimizer.HeapGCOptimizer> r5 = com.bytedance.sysoptimizer.HeapGCOptimizer.class
            monitor-enter(r5)
            r4 = 15737(0x3d79, float:2.2052E-41)
            com.bytedance.frameworks.apm.trace.MethodCollector.i(r4)     // Catch:{ all -> 0x0047 }
            boolean r0 = com.bytedance.sysoptimizer.SysOptimizer.loadOptimizerLibrary(r6)     // Catch:{ all -> 0x0047 }
            if (r0 == 0) goto L_0x0042
            int r1 = android.os.Build.VERSION.SDK_INT     // Catch:{ UnsatisfiedLinkError -> 0x003d, NoSuchMethodError -> 0x0042 }
            boolean r0 = sInited     // Catch:{ UnsatisfiedLinkError -> 0x003d, NoSuchMethodError -> 0x0042 }
            if (r0 != 0) goto L_0x001d
            boolean r0 = init(r1)     // Catch:{ UnsatisfiedLinkError -> 0x003d, NoSuchMethodError -> 0x0042 }
            if (r0 == 0) goto L_0x001d
            r0 = 1
            sInited = r0     // Catch:{ UnsatisfiedLinkError -> 0x003d, NoSuchMethodError -> 0x0042 }
        L_0x001d:
            boolean r0 = sInited     // Catch:{ UnsatisfiedLinkError -> 0x003d, NoSuchMethodError -> 0x0042 }
            if (r0 == 0) goto L_0x0038
            sShrinkRatio = r7     // Catch:{ UnsatisfiedLinkError -> 0x003d, NoSuchMethodError -> 0x0042 }
            sShrinkStep = r9     // Catch:{ UnsatisfiedLinkError -> 0x003d, NoSuchMethodError -> 0x0042 }
            r0 = -1
            if (r8 != r0) goto L_0x0035
            long r2 = (long) r9     // Catch:{ UnsatisfiedLinkError -> 0x003d, NoSuchMethodError -> 0x0042 }
            r0 = 1048576(0x100000, double:5.180654E-318)
            long r2 = r2 * r0
            shrink_regionspace_vss(r2)     // Catch:{ UnsatisfiedLinkError -> 0x003d, NoSuchMethodError -> 0x0042 }
            com.bytedance.frameworks.apm.trace.MethodCollector.o(r4)     // Catch:{ UnsatisfiedLinkError -> 0x003d, NoSuchMethodError -> 0x0042 }
            monitor-exit(r5)
            return
        L_0x0035:
            startVssCheckTimer(r8)     // Catch:{ UnsatisfiedLinkError -> 0x003d, NoSuchMethodError -> 0x0042 }
        L_0x0038:
            com.bytedance.frameworks.apm.trace.MethodCollector.o(r4)     // Catch:{ all -> 0x0047 }
            monitor-exit(r5)
            return
        L_0x003d:
            com.bytedance.frameworks.apm.trace.MethodCollector.o(r4)     // Catch:{ all -> 0x0047 }
            monitor-exit(r5)
            return
        L_0x0042:
            com.bytedance.frameworks.apm.trace.MethodCollector.o(r4)     // Catch:{ all -> 0x0047 }
            monitor-exit(r5)
            return
        L_0x0047:
            r0 = move-exception
            monitor-exit(r5)
            throw r0
        */
        throw new UnsupportedOperationException("Method not decompiled: com.bytedance.sysoptimizer.HeapGCOptimizer.optimizeRegionSpaceVSS(android.content.Context, float, int, int):void");
    }
}
