snapuserd: Factor setpriority/settid calls into a helper.

This allows disabling the code in host builds, since settid() isn't
available in the glibc prebuilt.

Bug: 288273605
Test: snapuserd_test
Change-Id: Ifddb5cb8b04484a1ab0a29794d65c9839759a919
This commit is contained in:
David Anderson 2023-08-03 16:53:55 -07:00
parent fe032d0391
commit dba77ad737
7 changed files with 70 additions and 6 deletions

View file

@ -72,6 +72,7 @@ cc_library_static {
"user-space-merge/snapuserd_transitions.cpp",
"user-space-merge/snapuserd_verify.cpp",
"user-space-merge/worker.cpp",
"utility.cpp",
],
static_libs: [
"libbase",

View file

@ -16,6 +16,7 @@
#include "merge_worker.h"
#include "snapuserd_core.h"
#include "utility.h"
namespace android {
namespace snapshot {
@ -550,8 +551,8 @@ bool MergeWorker::Run() {
return true;
}
if (setpriority(PRIO_PROCESS, gettid(), kNiceValueForMergeThreads)) {
SNAP_PLOG(ERROR) << "Failed to set priority for TID: " << gettid();
if (!SetThreadPriority(kNiceValueForMergeThreads)) {
SNAP_PLOG(ERROR) << "Failed to set thread priority";
}
SNAP_LOG(INFO) << "Merge starting..";

View file

@ -17,6 +17,7 @@
#include "read_worker.h"
#include "snapuserd_core.h"
#include "utility.h"
namespace android {
namespace snapshot {
@ -208,8 +209,8 @@ bool ReadWorker::Init() {
bool ReadWorker::Run() {
SNAP_LOG(INFO) << "Processing snapshot I/O requests....";
if (setpriority(PRIO_PROCESS, gettid(), kNiceValueForMergeThreads)) {
SNAP_PLOG(ERROR) << "Failed to set priority for TID: " << gettid();
if (!SetThreadPriority(kNiceValueForMergeThreads)) {
SNAP_PLOG(ERROR) << "Failed to set thread priority";
}
// Start serving IO

View file

@ -17,6 +17,7 @@
#include "snapuserd_readahead.h"
#include "snapuserd_core.h"
#include "utility.h"
namespace android {
namespace snapshot {
@ -765,8 +766,8 @@ bool ReadAhead::RunThread() {
InitializeIouring();
if (setpriority(PRIO_PROCESS, gettid(), kNiceValueForMergeThreads)) {
SNAP_PLOG(ERROR) << "Failed to set priority for TID: " << gettid();
if (!SetThreadPriority(kNiceValueForMergeThreads)) {
SNAP_PLOG(ERROR) << "Failed to set thread priority";
}
while (!RAIterDone()) {

View file

@ -43,6 +43,7 @@
#include "snapuserd_core.h"
#include "testing/dm_user_harness.h"
#include "testing/temp_device.h"
#include "utility.h"
DEFINE_string(force_config, "", "Force testing mode with iouring disabled");

View file

@ -0,0 +1,36 @@
// Copyright (C) 2023 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.
#include "utility.h"
#include <sys/resource.h>
#include <unistd.h>
#include <android-base/file.h>
namespace android {
namespace snapshot {
using android::base::unique_fd;
bool SetThreadPriority([[maybe_unused]] int priority) {
#ifdef __ANDROID__
return setpriority(PRIO_PROCESS, gettid(), priority) != -1;
#else
return true;
#endif
}
} // namespace snapshot
} // namespace android

View file

@ -0,0 +1,23 @@
// Copyright (C) 2023 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.
#pragma once
namespace android {
namespace snapshot {
bool SetThreadPriority(int priority);
} // namespace snapshot
} // namespace android