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

Commit 4418a39c authored by Dominik Laskowski's avatar Dominik Laskowski
Browse files

SF: Clean up helpers for thread priority

Dedupe error logs and remove otherwise unused return values.

Bug: 342681202
Flag: EXEMPT refactor
Test: m surfaceflinger
Change-Id: I6f68b884c1409f73127c06ed3d091dcbd7bdc264
parent de2cdcf1
Loading
Loading
Loading
Loading
+19 −33
Original line number Diff line number Diff line
@@ -1009,9 +1009,8 @@ void SurfaceFlinger::init() FTL_FAKE_GUARD(kMainThreadContext) {
    mPowerAdvisor->init();

    if (base::GetBoolProperty("service.sf.prime_shader_cache"s, true)) {
        if (setSchedFifo(false) != NO_ERROR) {
            ALOGW("Can't set SCHED_OTHER for primeCache");
        }
        constexpr const char* kWhence = "primeCache";
        setSchedFifo(false, kWhence);

        mRenderEnginePrimeCacheFuture.callOnce([this] {
            renderengine::PrimeCacheConfig config;
@@ -1047,9 +1046,7 @@ void SurfaceFlinger::init() FTL_FAKE_GUARD(kMainThreadContext) {
            return getRenderEngine().primeCache(config);
        });

        if (setSchedFifo(true) != NO_ERROR) {
            ALOGW("Can't set SCHED_FIFO after primeCache");
        }
        setSchedFifo(true, kWhence);
    }

    // Avoid blocking the main thread on `init` to set properties.
@@ -5690,16 +5687,11 @@ void SurfaceFlinger::setPowerModeInternal(const sp<DisplayDevice>& display, hal:
        }

        if (displayId == mActiveDisplayId) {
            // TODO(b/281692563): Merge the syscalls. For now, keep uclamp in a separate syscall and
            // set it before SCHED_FIFO due to b/190237315.
            if (setSchedAttr(true) != NO_ERROR) {
                ALOGW("Failed to set uclamp.min after powering on active display: %s",
                      strerror(errno));
            }
            if (setSchedFifo(true) != NO_ERROR) {
                ALOGW("Failed to set SCHED_FIFO after powering on active display: %s",
                      strerror(errno));
            }
            // TODO: b/281692563 - Merge the syscalls. For now, keep uclamp in a separate syscall
            // and set it before SCHED_FIFO due to b/190237315.
            constexpr const char* kWhence = "setPowerMode(ON)";
            setSchedAttr(true, kWhence);
            setSchedFifo(true, kWhence);
        }

        getHwComposer().setPowerMode(displayId, mode);
@@ -5726,14 +5718,9 @@ void SurfaceFlinger::setPowerModeInternal(const sp<DisplayDevice>& display, hal:
            if (const auto display = getActivatableDisplay()) {
                onActiveDisplayChangedLocked(activeDisplay.get(), *display);
            } else {
                if (setSchedFifo(false) != NO_ERROR) {
                    ALOGW("Failed to set SCHED_OTHER after powering off active display: %s",
                          strerror(errno));
                }
                if (setSchedAttr(false) != NO_ERROR) {
                    ALOGW("Failed set uclamp.min after powering off active display: %s",
                          strerror(errno));
                }
                constexpr const char* kWhence = "setPowerMode(OFF)";
                setSchedFifo(false, kWhence);
                setSchedAttr(false, kWhence);

                if (currentModeNotDozeSuspend) {
                    if (!FlagManager::getInstance().multithreaded_present()) {
@@ -7196,7 +7183,7 @@ static status_t validateScreenshotPermissions(const CaptureArgs& captureArgs) {
    return PERMISSION_DENIED;
}

status_t SurfaceFlinger::setSchedFifo(bool enabled) {
void SurfaceFlinger::setSchedFifo(bool enabled, const char* whence) {
    static constexpr int kFifoPriority = 2;
    static constexpr int kOtherPriority = 0;

@@ -7211,19 +7198,19 @@ status_t SurfaceFlinger::setSchedFifo(bool enabled) {
    }

    if (sched_setscheduler(0, sched_policy, &param) != 0) {
        return -errno;
        const char* kPolicy[] = {"SCHED_OTHER", "SCHED_FIFO"};
        ALOGW("%s: Failed to set %s: %s", whence, kPolicy[sched_policy == SCHED_FIFO],
              strerror(errno));
    }

    return NO_ERROR;
}

status_t SurfaceFlinger::setSchedAttr(bool enabled) {
void SurfaceFlinger::setSchedAttr(bool enabled, const char* whence) {
    static const unsigned int kUclampMin =
            base::GetUintProperty<unsigned int>("ro.surface_flinger.uclamp.min"s, 0U);

    if (!kUclampMin) {
        // uclamp.min set to 0 (default), skip setting
        return NO_ERROR;
        return;
    }

    sched_attr attr = {};
@@ -7234,10 +7221,9 @@ status_t SurfaceFlinger::setSchedAttr(bool enabled) {
    attr.sched_util_max = 1024;

    if (syscall(__NR_sched_setattr, 0, &attr, 0)) {
        return -errno;
        const char* kAction[] = {"disable", "enable"};
        ALOGW("%s: Failed to %s uclamp.min: %s", whence, kAction[enabled], strerror(errno));
    }

    return NO_ERROR;
}

namespace {
+3 −5
Original line number Diff line number Diff line
@@ -211,11 +211,9 @@ public:
    SurfaceFlinger(surfaceflinger::Factory&, SkipInitializationTag) ANDROID_API;
    explicit SurfaceFlinger(surfaceflinger::Factory&) ANDROID_API;

    // set main thread scheduling policy
    static status_t setSchedFifo(bool enabled) ANDROID_API;

    // set main thread scheduling attributes
    static status_t setSchedAttr(bool enabled);
    // Set scheduling policy and attributes of main thread.
    static void setSchedFifo(bool enabled, const char* whence);
    static void setSchedAttr(bool enabled, const char* whence);

    static char const* getServiceName() ANDROID_API { return "SurfaceFlinger"; }

+3 −11
Original line number Diff line number Diff line
@@ -77,7 +77,7 @@ static void startDisplayService() {
    }
}

int main(int, char**) {
int main() {
    signal(SIGPIPE, SIG_IGN);

    hardware::configureRpcThreadpool(1 /* maxThreads */,
@@ -91,9 +91,7 @@ int main(int, char**) {

    // Set uclamp.min setting on all threads, maybe an overkill but we want
    // to cover important threads like RenderEngine.
    if (SurfaceFlinger::setSchedAttr(true) != NO_ERROR) {
        ALOGW("Failed to set uclamp.min during boot: %s", strerror(errno));
    }
    SurfaceFlinger::setSchedAttr(true, __func__);

    // The binder threadpool we start will inherit sched policy and priority
    // of (this) creating thread. We want the binder thread pool to have
@@ -160,14 +158,8 @@ int main(int, char**) {

    startDisplayService(); // dependency on SF getting registered above

    if (SurfaceFlinger::setSchedFifo(true) != NO_ERROR) {
        ALOGW("Failed to set SCHED_FIFO during boot: %s", strerror(errno));
    }

    // run surface flinger in this thread
    SurfaceFlinger::setSchedFifo(true, __func__);
    flinger->run();

    return 0;
}

// TODO(b/129481165): remove the #pragma below and fix conversion issues