Loading services/camera/libcameraservice/Android.bp +3 −0 Original line number Diff line number Diff line Loading @@ -66,6 +66,8 @@ cc_defaults { "libmedia_codeclist", "libmedia_omx", "libmemunreachable", "libprocessgroup", "libprocinfo", "libsensorprivacy", "libstagefright", "libstagefright_foundation", Loading Loading @@ -185,6 +187,7 @@ cc_library { "utils/CameraThreadState.cpp", "utils/CameraTraces.cpp", "utils/AutoConditionLock.cpp", "utils/SchedulingPolicyUtils.cpp", "utils/SessionConfigurationUtils.cpp", "utils/SessionConfigurationUtilsHidl.cpp", "utils/SessionStatsBuilder.cpp", Loading services/camera/libcameraservice/CameraService.cpp +51 −36 Original line number Diff line number Diff line Loading @@ -87,6 +87,7 @@ namespace { const char* kActivityServiceName = "activity"; const char* kSensorPrivacyServiceName = "sensor_privacy"; const char* kAppopsServiceName = "appops"; const char* kProcessInfoServiceName = "processinfo"; }; // namespace anonymous namespace android { Loading Loading @@ -1589,6 +1590,30 @@ status_t CameraService::handleEvictionsLocked(const String8& cameraId, int clien } } // Get state for the given cameraId auto state = getCameraState(cameraId); if (state == nullptr) { ALOGE("CameraService::connect X (PID %d) rejected (no camera device with ID %s)", clientPid, cameraId.string()); // Should never get here because validateConnectLocked should have errored out return BAD_VALUE; } sp<IServiceManager> sm = defaultServiceManager(); sp<IBinder> binder = sm->checkService(String16(kProcessInfoServiceName)); if (!binder && isAutomotivePrivilegedClient(CameraThreadState::getCallingUid())) { // If processinfo service is not available and the client is automotive privileged // client used for safety critical uses cases such as rear-view and surround-view which // needs to be available before android boot completes, then use the hardcoded values // for the process state and priority score. As this scenario is before android system // services are up and client is native client, hence using NATIVE_ADJ as the priority // score and state as PROCESS_STATE_BOUND_TOP as such automotive apps need to be // visible on the top. clientDescriptor = CameraClientManager::makeClientDescriptor(cameraId, sp<BasicClient>{nullptr}, static_cast<int32_t>(state->getCost()), state->getConflicting(), resource_policy::NATIVE_ADJ, clientPid, ActivityManager::PROCESS_STATE_BOUND_TOP, oomScoreOffset, systemNativeClient); } else { // Get current active client PIDs std::vector<int> ownerPids(mActiveClientManager.getAllOwners()); ownerPids.push_back(clientPid); Loading @@ -1597,12 +1622,10 @@ status_t CameraService::handleEvictionsLocked(const String8& cameraId, int clien std::vector<int> states(ownerPids.size()); // Get priority scores of all active PIDs status_t err = ProcessInfoService::getProcessStatesScoresFromPids( ownerPids.size(), &ownerPids[0], /*out*/&states[0], /*out*/&priorityScores[0]); status_t err = ProcessInfoService::getProcessStatesScoresFromPids(ownerPids.size(), &ownerPids[0], /*out*/&states[0], /*out*/&priorityScores[0]); if (err != OK) { ALOGE("%s: Priority score query failed: %d", __FUNCTION__, err); ALOGE("%s: Priority score query failed: %d", __FUNCTION__, err); return err; } Loading @@ -1616,15 +1639,6 @@ status_t CameraService::handleEvictionsLocked(const String8& cameraId, int clien } mActiveClientManager.updatePriorities(pidToPriorityMap); // Get state for the given cameraId auto state = getCameraState(cameraId); if (state == nullptr) { ALOGE("CameraService::connect X (PID %d) rejected (no camera device with ID %s)", clientPid, cameraId.string()); // Should never get here because validateConnectLocked should have errored out return BAD_VALUE; } int32_t actualScore = priorityScores[priorityScores.size() - 1]; int32_t actualState = states[states.size() - 1]; Loading @@ -1635,6 +1649,7 @@ status_t CameraService::handleEvictionsLocked(const String8& cameraId, int clien sp<BasicClient>{nullptr}, static_cast<int32_t>(state->getCost()), state->getConflicting(), actualScore, clientPid, actualState, oomScoreOffset, systemNativeClient); } resource_policy::ClientPriority clientPriority = clientDescriptor->getPriority(); Loading services/camera/libcameraservice/device3/Camera3Device.cpp +3 −3 Original line number Diff line number Diff line Loading @@ -60,9 +60,9 @@ #include "device3/Camera3InputStream.h" #include "device3/Camera3OutputStream.h" #include "device3/Camera3SharedOutputStream.h" #include "mediautils/SchedulingPolicyService.h" #include "utils/CameraThreadState.h" #include "utils/CameraTraces.h" #include "utils/SchedulingPolicyUtils.h" #include "utils/SessionConfigurationUtils.h" #include "utils/TraceHFR.h" Loading Loading @@ -2616,8 +2616,8 @@ status_t Camera3Device::configureStreamsLocked(int operatingMode, if (disableFifo != 1) { // Boost priority of request thread to SCHED_FIFO. pid_t requestThreadTid = mRequestThread->getTid(); res = requestPriority(getpid(), requestThreadTid, kRequestThreadPriority, /*isForApp*/ false, /*asynchronous*/ false); res = SchedulingPolicyUtils::requestPriorityDirect(getpid(), requestThreadTid, kRequestThreadPriority); if (res != OK) { ALOGW("Can't set realtime priority for request processing thread: %s (%d)", strerror(-res), res); Loading services/camera/libcameraservice/utils/SchedulingPolicyUtils.cpp 0 → 100644 +66 −0 Original line number Diff line number Diff line /* * 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 "SchedulingPolicyUtils.h" #include <errno.h> #include <pthread.h> #include <sched.h> #include "CameraThreadState.h" #include <private/android_filesystem_config.h> #include <processgroup/processgroup.h> #include <processgroup/sched_policy.h> #include <procinfo/process.h> #include <utils/Log.h> namespace android { namespace camera3 { namespace SchedulingPolicyUtils { int requestPriorityDirect(int pid, int tid, int prio) { android::procinfo::ProcessInfo processInfo; static const int kMinPrio = 1; static const int kMaxPrio = 3; if (!android::procinfo::GetProcessInfo(tid, &processInfo)) { ALOGE("%s: Error getting process info", __FUNCTION__); return -EPERM; } if (prio < kMinPrio || prio > kMaxPrio || processInfo.pid != pid) { ALOGE("%s: Invalid parameter prio=%d pid=%d procinfo.pid=%d", __FUNCTION__, prio, pid, processInfo.pid); return -EPERM; } // Set the thread group as audio system thread group in consistent with the // implementation in SchedulingPolicyService.java when isApp is false in // requestPriority method. if (!SetTaskProfiles(tid, {get_sched_policy_profile_name(SP_AUDIO_SYS)}, /*use_fd_cache*/ true)) { ALOGE("%s:Error in SetTaskProfiles", __FUNCTION__); return -EPERM; } struct sched_param param; param.sched_priority = prio; return sched_setscheduler(tid, SCHED_FIFO | SCHED_RESET_ON_FORK, ¶m); } } // namespace SchedulingPolicyUtils } // namespace camera3 } // namespace android services/camera/libcameraservice/utils/SchedulingPolicyUtils.h 0 → 100644 +35 −0 Original line number Diff line number Diff line /* * 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. */ #ifndef ANDROID_SERVICE_CAMERA_SCHEDULING_POLICY_UTILS_H #define ANDROID_SERVICE_CAMERA_SCHEDULING_POLICY_UTILS_H namespace android { namespace camera3 { namespace SchedulingPolicyUtils { /** * Request elevated priority for thread tid, whose thread group leader must be pid. * Instead of using scheduling policy service, this method uses direct system calls. * The priority parameter is currently restricted from 1 to 3 matching * scheduling policy service implementation. */ int requestPriorityDirect(int pid, int tid, int prio); } // SchedulingPolicyUtils } // camera3 } // android #endif Loading
services/camera/libcameraservice/Android.bp +3 −0 Original line number Diff line number Diff line Loading @@ -66,6 +66,8 @@ cc_defaults { "libmedia_codeclist", "libmedia_omx", "libmemunreachable", "libprocessgroup", "libprocinfo", "libsensorprivacy", "libstagefright", "libstagefright_foundation", Loading Loading @@ -185,6 +187,7 @@ cc_library { "utils/CameraThreadState.cpp", "utils/CameraTraces.cpp", "utils/AutoConditionLock.cpp", "utils/SchedulingPolicyUtils.cpp", "utils/SessionConfigurationUtils.cpp", "utils/SessionConfigurationUtilsHidl.cpp", "utils/SessionStatsBuilder.cpp", Loading
services/camera/libcameraservice/CameraService.cpp +51 −36 Original line number Diff line number Diff line Loading @@ -87,6 +87,7 @@ namespace { const char* kActivityServiceName = "activity"; const char* kSensorPrivacyServiceName = "sensor_privacy"; const char* kAppopsServiceName = "appops"; const char* kProcessInfoServiceName = "processinfo"; }; // namespace anonymous namespace android { Loading Loading @@ -1589,6 +1590,30 @@ status_t CameraService::handleEvictionsLocked(const String8& cameraId, int clien } } // Get state for the given cameraId auto state = getCameraState(cameraId); if (state == nullptr) { ALOGE("CameraService::connect X (PID %d) rejected (no camera device with ID %s)", clientPid, cameraId.string()); // Should never get here because validateConnectLocked should have errored out return BAD_VALUE; } sp<IServiceManager> sm = defaultServiceManager(); sp<IBinder> binder = sm->checkService(String16(kProcessInfoServiceName)); if (!binder && isAutomotivePrivilegedClient(CameraThreadState::getCallingUid())) { // If processinfo service is not available and the client is automotive privileged // client used for safety critical uses cases such as rear-view and surround-view which // needs to be available before android boot completes, then use the hardcoded values // for the process state and priority score. As this scenario is before android system // services are up and client is native client, hence using NATIVE_ADJ as the priority // score and state as PROCESS_STATE_BOUND_TOP as such automotive apps need to be // visible on the top. clientDescriptor = CameraClientManager::makeClientDescriptor(cameraId, sp<BasicClient>{nullptr}, static_cast<int32_t>(state->getCost()), state->getConflicting(), resource_policy::NATIVE_ADJ, clientPid, ActivityManager::PROCESS_STATE_BOUND_TOP, oomScoreOffset, systemNativeClient); } else { // Get current active client PIDs std::vector<int> ownerPids(mActiveClientManager.getAllOwners()); ownerPids.push_back(clientPid); Loading @@ -1597,12 +1622,10 @@ status_t CameraService::handleEvictionsLocked(const String8& cameraId, int clien std::vector<int> states(ownerPids.size()); // Get priority scores of all active PIDs status_t err = ProcessInfoService::getProcessStatesScoresFromPids( ownerPids.size(), &ownerPids[0], /*out*/&states[0], /*out*/&priorityScores[0]); status_t err = ProcessInfoService::getProcessStatesScoresFromPids(ownerPids.size(), &ownerPids[0], /*out*/&states[0], /*out*/&priorityScores[0]); if (err != OK) { ALOGE("%s: Priority score query failed: %d", __FUNCTION__, err); ALOGE("%s: Priority score query failed: %d", __FUNCTION__, err); return err; } Loading @@ -1616,15 +1639,6 @@ status_t CameraService::handleEvictionsLocked(const String8& cameraId, int clien } mActiveClientManager.updatePriorities(pidToPriorityMap); // Get state for the given cameraId auto state = getCameraState(cameraId); if (state == nullptr) { ALOGE("CameraService::connect X (PID %d) rejected (no camera device with ID %s)", clientPid, cameraId.string()); // Should never get here because validateConnectLocked should have errored out return BAD_VALUE; } int32_t actualScore = priorityScores[priorityScores.size() - 1]; int32_t actualState = states[states.size() - 1]; Loading @@ -1635,6 +1649,7 @@ status_t CameraService::handleEvictionsLocked(const String8& cameraId, int clien sp<BasicClient>{nullptr}, static_cast<int32_t>(state->getCost()), state->getConflicting(), actualScore, clientPid, actualState, oomScoreOffset, systemNativeClient); } resource_policy::ClientPriority clientPriority = clientDescriptor->getPriority(); Loading
services/camera/libcameraservice/device3/Camera3Device.cpp +3 −3 Original line number Diff line number Diff line Loading @@ -60,9 +60,9 @@ #include "device3/Camera3InputStream.h" #include "device3/Camera3OutputStream.h" #include "device3/Camera3SharedOutputStream.h" #include "mediautils/SchedulingPolicyService.h" #include "utils/CameraThreadState.h" #include "utils/CameraTraces.h" #include "utils/SchedulingPolicyUtils.h" #include "utils/SessionConfigurationUtils.h" #include "utils/TraceHFR.h" Loading Loading @@ -2616,8 +2616,8 @@ status_t Camera3Device::configureStreamsLocked(int operatingMode, if (disableFifo != 1) { // Boost priority of request thread to SCHED_FIFO. pid_t requestThreadTid = mRequestThread->getTid(); res = requestPriority(getpid(), requestThreadTid, kRequestThreadPriority, /*isForApp*/ false, /*asynchronous*/ false); res = SchedulingPolicyUtils::requestPriorityDirect(getpid(), requestThreadTid, kRequestThreadPriority); if (res != OK) { ALOGW("Can't set realtime priority for request processing thread: %s (%d)", strerror(-res), res); Loading
services/camera/libcameraservice/utils/SchedulingPolicyUtils.cpp 0 → 100644 +66 −0 Original line number Diff line number Diff line /* * 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 "SchedulingPolicyUtils.h" #include <errno.h> #include <pthread.h> #include <sched.h> #include "CameraThreadState.h" #include <private/android_filesystem_config.h> #include <processgroup/processgroup.h> #include <processgroup/sched_policy.h> #include <procinfo/process.h> #include <utils/Log.h> namespace android { namespace camera3 { namespace SchedulingPolicyUtils { int requestPriorityDirect(int pid, int tid, int prio) { android::procinfo::ProcessInfo processInfo; static const int kMinPrio = 1; static const int kMaxPrio = 3; if (!android::procinfo::GetProcessInfo(tid, &processInfo)) { ALOGE("%s: Error getting process info", __FUNCTION__); return -EPERM; } if (prio < kMinPrio || prio > kMaxPrio || processInfo.pid != pid) { ALOGE("%s: Invalid parameter prio=%d pid=%d procinfo.pid=%d", __FUNCTION__, prio, pid, processInfo.pid); return -EPERM; } // Set the thread group as audio system thread group in consistent with the // implementation in SchedulingPolicyService.java when isApp is false in // requestPriority method. if (!SetTaskProfiles(tid, {get_sched_policy_profile_name(SP_AUDIO_SYS)}, /*use_fd_cache*/ true)) { ALOGE("%s:Error in SetTaskProfiles", __FUNCTION__); return -EPERM; } struct sched_param param; param.sched_priority = prio; return sched_setscheduler(tid, SCHED_FIFO | SCHED_RESET_ON_FORK, ¶m); } } // namespace SchedulingPolicyUtils } // namespace camera3 } // namespace android
services/camera/libcameraservice/utils/SchedulingPolicyUtils.h 0 → 100644 +35 −0 Original line number Diff line number Diff line /* * 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. */ #ifndef ANDROID_SERVICE_CAMERA_SCHEDULING_POLICY_UTILS_H #define ANDROID_SERVICE_CAMERA_SCHEDULING_POLICY_UTILS_H namespace android { namespace camera3 { namespace SchedulingPolicyUtils { /** * Request elevated priority for thread tid, whose thread group leader must be pid. * Instead of using scheduling policy service, this method uses direct system calls. * The priority parameter is currently restricted from 1 to 3 matching * scheduling policy service implementation. */ int requestPriorityDirect(int pid, int tid, int prio); } // SchedulingPolicyUtils } // camera3 } // android #endif