From dba77ad7377d18eefeaa2cdb661660e39e02e595 Mon Sep 17 00:00:00 2001 From: David Anderson Date: Thu, 3 Aug 2023 16:53:55 -0700 Subject: [PATCH] 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 --- fs_mgr/libsnapshot/snapuserd/Android.bp | 1 + .../user-space-merge/merge_worker.cpp | 5 +-- .../user-space-merge/read_worker.cpp | 5 +-- .../user-space-merge/snapuserd_readahead.cpp | 5 +-- .../user-space-merge/snapuserd_test.cpp | 1 + fs_mgr/libsnapshot/snapuserd/utility.cpp | 36 +++++++++++++++++++ fs_mgr/libsnapshot/snapuserd/utility.h | 23 ++++++++++++ 7 files changed, 70 insertions(+), 6 deletions(-) create mode 100644 fs_mgr/libsnapshot/snapuserd/utility.cpp create mode 100644 fs_mgr/libsnapshot/snapuserd/utility.h diff --git a/fs_mgr/libsnapshot/snapuserd/Android.bp b/fs_mgr/libsnapshot/snapuserd/Android.bp index 2368f8f36..6251e5aea 100644 --- a/fs_mgr/libsnapshot/snapuserd/Android.bp +++ b/fs_mgr/libsnapshot/snapuserd/Android.bp @@ -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", diff --git a/fs_mgr/libsnapshot/snapuserd/user-space-merge/merge_worker.cpp b/fs_mgr/libsnapshot/snapuserd/user-space-merge/merge_worker.cpp index a83cab8d8..2d6972171 100644 --- a/fs_mgr/libsnapshot/snapuserd/user-space-merge/merge_worker.cpp +++ b/fs_mgr/libsnapshot/snapuserd/user-space-merge/merge_worker.cpp @@ -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.."; diff --git a/fs_mgr/libsnapshot/snapuserd/user-space-merge/read_worker.cpp b/fs_mgr/libsnapshot/snapuserd/user-space-merge/read_worker.cpp index ce4be4348..e2c292bd8 100644 --- a/fs_mgr/libsnapshot/snapuserd/user-space-merge/read_worker.cpp +++ b/fs_mgr/libsnapshot/snapuserd/user-space-merge/read_worker.cpp @@ -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 diff --git a/fs_mgr/libsnapshot/snapuserd/user-space-merge/snapuserd_readahead.cpp b/fs_mgr/libsnapshot/snapuserd/user-space-merge/snapuserd_readahead.cpp index 8755820e1..8b799ea93 100644 --- a/fs_mgr/libsnapshot/snapuserd/user-space-merge/snapuserd_readahead.cpp +++ b/fs_mgr/libsnapshot/snapuserd/user-space-merge/snapuserd_readahead.cpp @@ -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()) { diff --git a/fs_mgr/libsnapshot/snapuserd/user-space-merge/snapuserd_test.cpp b/fs_mgr/libsnapshot/snapuserd/user-space-merge/snapuserd_test.cpp index 4ea1d5d08..c40783499 100644 --- a/fs_mgr/libsnapshot/snapuserd/user-space-merge/snapuserd_test.cpp +++ b/fs_mgr/libsnapshot/snapuserd/user-space-merge/snapuserd_test.cpp @@ -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"); diff --git a/fs_mgr/libsnapshot/snapuserd/utility.cpp b/fs_mgr/libsnapshot/snapuserd/utility.cpp new file mode 100644 index 000000000..a84a7c14f --- /dev/null +++ b/fs_mgr/libsnapshot/snapuserd/utility.cpp @@ -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 +#include + +#include + +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 diff --git a/fs_mgr/libsnapshot/snapuserd/utility.h b/fs_mgr/libsnapshot/snapuserd/utility.h new file mode 100644 index 000000000..58ec3e63a --- /dev/null +++ b/fs_mgr/libsnapshot/snapuserd/utility.h @@ -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