Skip to content

Commit 4418a39

Browse files
author
Dominik Laskowski
committed
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
1 parent de2cdcf commit 4418a39

3 files changed

Lines changed: 25 additions & 49 deletions

File tree

services/surfaceflinger/SurfaceFlinger.cpp

Lines changed: 19 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1009,9 +1009,8 @@ void SurfaceFlinger::init() FTL_FAKE_GUARD(kMainThreadContext) {
10091009
mPowerAdvisor->init();
10101010

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

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

1050-
if (setSchedFifo(true) != NO_ERROR) {
1051-
ALOGW("Can't set SCHED_FIFO after primeCache");
1052-
}
1049+
setSchedFifo(true, kWhence);
10531050
}
10541051

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

56925689
if (displayId == mActiveDisplayId) {
5693-
// TODO(b/281692563): Merge the syscalls. For now, keep uclamp in a separate syscall and
5694-
// set it before SCHED_FIFO due to b/190237315.
5695-
if (setSchedAttr(true) != NO_ERROR) {
5696-
ALOGW("Failed to set uclamp.min after powering on active display: %s",
5697-
strerror(errno));
5698-
}
5699-
if (setSchedFifo(true) != NO_ERROR) {
5700-
ALOGW("Failed to set SCHED_FIFO after powering on active display: %s",
5701-
strerror(errno));
5702-
}
5690+
// TODO: b/281692563 - Merge the syscalls. For now, keep uclamp in a separate syscall
5691+
// and set it before SCHED_FIFO due to b/190237315.
5692+
constexpr const char* kWhence = "setPowerMode(ON)";
5693+
setSchedAttr(true, kWhence);
5694+
setSchedFifo(true, kWhence);
57035695
}
57045696

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

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

7199-
status_t SurfaceFlinger::setSchedFifo(bool enabled) {
7186+
void SurfaceFlinger::setSchedFifo(bool enabled, const char* whence) {
72007187
static constexpr int kFifoPriority = 2;
72017188
static constexpr int kOtherPriority = 0;
72027189

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

72137200
if (sched_setscheduler(0, sched_policy, &param) != 0) {
7214-
return -errno;
7201+
const char* kPolicy[] = {"SCHED_OTHER", "SCHED_FIFO"};
7202+
ALOGW("%s: Failed to set %s: %s", whence, kPolicy[sched_policy == SCHED_FIFO],
7203+
strerror(errno));
72157204
}
7216-
7217-
return NO_ERROR;
72187205
}
72197206

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

72247211
if (!kUclampMin) {
72257212
// uclamp.min set to 0 (default), skip setting
7226-
return NO_ERROR;
7213+
return;
72277214
}
72287215

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

72367223
if (syscall(__NR_sched_setattr, 0, &attr, 0)) {
7237-
return -errno;
7224+
const char* kAction[] = {"disable", "enable"};
7225+
ALOGW("%s: Failed to %s uclamp.min: %s", whence, kAction[enabled], strerror(errno));
72387226
}
7239-
7240-
return NO_ERROR;
72417227
}
72427228

72437229
namespace {

services/surfaceflinger/SurfaceFlinger.h

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -211,11 +211,9 @@ class SurfaceFlinger : public BnSurfaceComposer,
211211
SurfaceFlinger(surfaceflinger::Factory&, SkipInitializationTag) ANDROID_API;
212212
explicit SurfaceFlinger(surfaceflinger::Factory&) ANDROID_API;
213213

214-
// set main thread scheduling policy
215-
static status_t setSchedFifo(bool enabled) ANDROID_API;
216-
217-
// set main thread scheduling attributes
218-
static status_t setSchedAttr(bool enabled);
214+
// Set scheduling policy and attributes of main thread.
215+
static void setSchedFifo(bool enabled, const char* whence);
216+
static void setSchedAttr(bool enabled, const char* whence);
219217

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

services/surfaceflinger/main_surfaceflinger.cpp

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ static void startDisplayService() {
7777
}
7878
}
7979

80-
int main(int, char**) {
80+
int main() {
8181
signal(SIGPIPE, SIG_IGN);
8282

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

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

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

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

163-
if (SurfaceFlinger::setSchedFifo(true) != NO_ERROR) {
164-
ALOGW("Failed to set SCHED_FIFO during boot: %s", strerror(errno));
165-
}
166-
167-
// run surface flinger in this thread
161+
SurfaceFlinger::setSchedFifo(true, __func__);
168162
flinger->run();
169-
170-
return 0;
171163
}
172164

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

0 commit comments

Comments
 (0)