Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit d2811887 authored by Jared Duke's avatar Jared Duke
Browse files

Introduce a LazyJniRegistrar helper class for system server

Currently, system server registers all JNI native method entrypoints
eagerly in SystemServer init. This has generally been fine, but doesn't
allow for any flexibility in the existence of Java classes defining
these entrypoints; it's assumed that all such entrypoints always exist.

The LazyJniRegistrar class allows lazy registration of JNI methods
for classes or services, shifting registration responsibilty to
the Java classes that use these entrypoints. This in turn allows
optional classes or services to be stripped from the build.

Start by migrating only a small subset of classes that may be
considered optional in the near future. Eventually, we might
consider migrating more classes to further streamline init and
avoid cascading initialization overhead.

Bug: 375264322
Test: m + presubmit
Flag: EXEMPT refactor
Change-Id: Ia2a0f52575cd47b0f418bb0439ab3a02d413b6ae
parent 2a9533b2
Loading
Loading
Loading
Loading
+6 −0
Original line number Diff line number Diff line
@@ -30,6 +30,8 @@ import android.os.RemoteException;
import android.os.ServiceManager;
import android.util.Slog;

import com.android.server.utils.LazyJniRegistrar;

public class ConsumerIrService extends IConsumerIrService.Stub {
    private static final String TAG = "ConsumerIrService";

@@ -39,6 +41,10 @@ public class ConsumerIrService extends IConsumerIrService.Stub {
    private static native int halTransmit(int carrierFrequency, int[] pattern);
    private static native int[] halGetCarrierFrequencies();

    static {
        LazyJniRegistrar.registerConsumerIrService();
    }

    private final Context mContext;
    private final PowerManager.WakeLock mWakeLock;
    private final boolean mHasNativeHal;
+47 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2024 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.server.utils;

import com.android.tools.r8.keepanno.annotations.KeepItemKind;
import com.android.tools.r8.keepanno.annotations.MethodAccessFlags;
import com.android.tools.r8.keepanno.annotations.UsedByNative;

/**
 * Utility class for lazily registering native methods for a given class.
 *
 * <p><strong>Note: </strong>Most native methods are registered eagerly via the
 * native {@code JNI_OnLoad} hook when system server loads its primary native
 * lib. However, some classes within system server may be stripped if unused.
 * This class offers a way to selectively register their native methods. Such
 * register calls should typically be done from that class's {@code static {}}
 * init block.
 */
@UsedByNative(
        description = "Referenced from JNI in jni/com_android_server_utils_LazyJniRegistrar.cpp",
        kind = KeepItemKind.CLASS_AND_MEMBERS,
        methodAccess = {MethodAccessFlags.NATIVE})
public final class LazyJniRegistrar {

    // Note: {@link SystemServer#run} loads the native "android_servers" lib, so no need to do so
    // explicitly here. Classes that use this registration must not be initialized before this.

    /** Registers native methods for ConsumerIrService. */
    public static native void registerConsumerIrService();

    /** Registers native methods for VrManagerService. */
    public static native void registerVrManagerService();
}
+1 −0
Original line number Diff line number Diff line
@@ -10,6 +10,7 @@ per-file Watcher.java = file:/services/core/java/com/android/server/pm/OWNERS
per-file Watcher.java = shombert@google.com
per-file EventLogger.java = file:/platform/frameworks/av:/media/janitors/media_solutions_OWNERS
per-file EventLogger.java = jmtrivi@google.com
per-file LazyJniRegistrar.java = file:/PERFORMANCE_OWNERS

# Bug component : 158088 = per-file AnrTimer*.java
per-file AnrTimer*.java = file:/PERFORMANCE_OWNERS
+5 −0
Original line number Diff line number Diff line
@@ -67,6 +67,7 @@ import com.android.server.FgThread;
import com.android.server.LocalServices;
import com.android.server.SystemConfig;
import com.android.server.SystemService;
import com.android.server.utils.LazyJniRegistrar;
import com.android.server.utils.ManagedApplicationService;
import com.android.server.utils.ManagedApplicationService.BinderChecker;
import com.android.server.utils.ManagedApplicationService.LogEvent;
@@ -131,6 +132,10 @@ public class VrManagerService extends SystemService
    private static native void initializeNative();
    private static native void setVrModeNative(boolean enabled);

    static {
        LazyJniRegistrar.registerVrManagerService();
    }

    private final Object mLock = new Object();

    private final IBinder mOverlayToken = new Binder();
+1 −0
Original line number Diff line number Diff line
@@ -76,6 +76,7 @@ cc_library_static {
        "com_android_server_am_LowMemDetector.cpp",
        "com_android_server_pm_PackageManagerShellCommandDataLoader.cpp",
        "com_android_server_sensor_SensorService.cpp",
        "com_android_server_utils_LazyJniRegistrar.cpp",
        "com_android_server_wm_TaskFpsCallbackController.cpp",
        "onload.cpp",
        ":lib_cachedAppOptimizer_native",
Loading