diff --git a/OAT.xml b/OAT.xml new file mode 100644 index 0000000000000000000000000000000000000000..6c9cf8b131fddb0d26d6b6a864ad6bb2dd049c2f --- /dev/null +++ b/OAT.xml @@ -0,0 +1,76 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/RELEASE-NOTES.md b/RELEASE-NOTES.md new file mode 100644 index 0000000000000000000000000000000000000000..80de028df148207f5e1a4845d04610fd30120140 --- /dev/null +++ b/RELEASE-NOTES.md @@ -0,0 +1,4 @@ +# RELEASE-NOTES + +# 1.0.0 +1. Initial version \ No newline at end of file diff --git a/bundle.json b/bundle.json new file mode 100644 index 0000000000000000000000000000000000000000..80a435b320d19c7e1097c0adc58c9d7fd595f0b8 --- /dev/null +++ b/bundle.json @@ -0,0 +1,61 @@ +{ + "name": "@ohos/resourceschedule_qos_manager", + "description": "resourceschedule_qos_manager", + "version": "3.1", + "license": "Apache License 2.0", + "publishAs": "code-segment", + "segment": { + "destPath": "foundation/resourceschedule/resourceschedule_qos_manager" + }, + "dirs": {}, + "scripts": {}, + "component": { + "name": "resourceschedule_qos_manager", + "subsystem": "resourceschedule", + "syscap": [], + "features": [], + "adapted_system_type": [ + "mini", + "small", + "standard" + ], + "rom": "2048KB", + "ram": "10240KB", + "deps": { + "components": [ + "ability_base", + "ability_runtime", + "libeventhandler", + "ipc_single", + "samgr", + "system_ability_fwk" + ], + "third_party": [ + "xml2", + "json" + ] + }, + "build": { + "sub_component": [ + "//foundation/resourceschedule/resourceschedule_qos_manager/etc/init:concurrent_task_service.cfg", + "//foundation/resourceschedule/resourceschedule_qos_manager/sa_profile:concurrent_task_sa_profile", + "//foundation/resourceschedule/resourceschedule_qos_manager/services:concurrentsvc", + "//foundation/resourceschedule/resourceschedule_qos_manager/frameworks/concurrent_task_client:concurrent_task_client" + ], + "inner_kits": [ + { + "header": { + "header_base": "//foundation/resourceschedule/resourceschedule_qos_manager/interfaces/inner_api/", + "header_files": [ + "concurrent_task_client.h" + ] + }, + "name": "//foundation/resourceschedule/resourceschedule_qos_manager/frameworks/concurrent_task_client:concurrent_task_client" + } + ], + "test": [ + "//foundation/resourceschedule/resourceschedule_qos_manager/test:concurrent_unittest" + ] + } + } +} diff --git a/etc/init/BUILD.gn b/etc/init/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..4fc262a52816fb29c0b9dab675c817b68fc1d8fe --- /dev/null +++ b/etc/init/BUILD.gn @@ -0,0 +1,27 @@ +# Copyright (c) 2022 Huawei Device Co., Ltd. +# 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. + +import("//build/ohos.gni") + +################################################################################# + +group("etc") { + deps = [ ":concurrent_task_service.cfg" ] +} + +ohos_prebuilt_etc("concurrent_task_service.cfg") { + source = "concurrent_task_service.cfg" + relative_install_dir = "init" + subsystem_name = "resourceschedule" + part_name = "resourceschedule_qos_manager" +} diff --git a/etc/init/concurrent_task_service.cfg b/etc/init/concurrent_task_service.cfg new file mode 100644 index 0000000000000000000000000000000000000000..4453863f420e6a088f6c99bf524fe66fb3a9de4a --- /dev/null +++ b/etc/init/concurrent_task_service.cfg @@ -0,0 +1,18 @@ +{ + "jobs" : [{ + "name" : "post-fs-data", + "cmds" : [ + "start concurrent_task_service" + ] + } + ], + "services" : [{ + "name" : "concurrent_task_service", + "path" : ["/system/bin/sa_main", "/system/profile/concurrent_task_service.xml"], + "importance" : -20, + "uid" : "system", + "gid" : ["system", "shell"], + "secon" : "u:r:concurrent_task_service:s0" + } + ] +} diff --git a/frameworks/concurrent_task_client/BUILD.gn b/frameworks/concurrent_task_client/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..4a9949e19f5c5878f62df03857e173cb0a7690cf --- /dev/null +++ b/frameworks/concurrent_task_client/BUILD.gn @@ -0,0 +1,62 @@ +# Copyright (c) 2022 Huawei Device Co., Ltd. +# 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. + +import("//build/ohos.gni") +import("//build/test.gni") + +config("client_private_config") { + cflags_cc = [ "-fexceptions" ] + include_dirs = [ + "./include", + "../../include", + "../../services/include", + "../../interfaces/inner_api/", + "//commonlibrary/c_utils/base/include", + "//utils/system/safwk/native/include", + "//third_party/jsoncpp/include", + ] +} + +config("client_public_config") { + visibility = [":*"] + cflags = [ "-fstack-protector-strong" ] + include_dirs = [ "include" ] +} + +ohos_shared_library("concurrent_task_client") { + configs = [ + ":client_private_config", + ] + + public_configs = [ ":client_public_config" ] + + sources = [ + "src/concurrent_task_client.cpp", + "src/concurrent_task_service_proxy.cpp", + ] + + deps = [ + "//third_party/jsoncpp:jsoncpp", + ] + + external_deps = [ + "c_utils:utils", + "eventhandler:libeventhandler", + "hiviewdfx_hilog_native:libhilog", + "ipc:ipc_single", + "samgr:samgr_proxy", + ] + + subsystem_name = "resourceschedule" + part_name = "resourceschedule_qos_manager" +} diff --git a/frameworks/concurrent_task_client/include/concurrent_task_errors.h b/frameworks/concurrent_task_client/include/concurrent_task_errors.h new file mode 100644 index 0000000000000000000000000000000000000000..89df25c2837bc91edc01f055032681c1b1a38070 --- /dev/null +++ b/frameworks/concurrent_task_client/include/concurrent_task_errors.h @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * 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 CONCURRENT_TASK_SERVICE_INTERFACES_INNERAPI_CONCURRENT_TASK_CLIENT_INCLUDE_CONCURRENT_TASK_ERRORS_H +#define CONCURRENT_TASK_SERVICE_INTERFACES_INNERAPI_CONCURRENT_TASK_CLIENT_INCLUDE_CONCURRENT_TASK_ERRORS_H + +#include "errors.h" + +namespace OHOS { +namespace ConcurrentTask { +enum { + CONCURRENT_TASK_MODULE_COMMON = 0x00, + CONCURRENT_TASK_MODULE_SERVICE = 0x01, +}; + +constexpr ErrCode CONCURRENT_TASK_COMMON_ERR_OFFSET = ErrCodeOffset(SUBSYS_IAWARE, CONCURRENT_TASK_MODULE_COMMON); +enum { + ERR_CONCURRENT_TASK_INVALID_PARAM = CONCURRENT_TASK_COMMON_ERR_OFFSET + 1, + GET_CONCURRENT_TASK_SERVICE_FAILED, +}; + +constexpr ErrCode CONCURRENT_TASK_SERVICE_ERR_OFFSET = ErrCodeOffset(SUBSYS_IAWARE, CONCURRENT_TASK_MODULE_SERVICE); +enum { + ERR_CONCURRENT_TASK_PARCEL_ERROR = CONCURRENT_TASK_SERVICE_ERR_OFFSET + 1, + ERR_CONCURRENT_TASK_PERMISSION_DENIED, + ERR_CONCURRENT_TASK_WRITE_FILE_FAILED, +}; +} // namespace ConcurrentTask +} // namespace OHOS + +#endif // CONCURRENT_TASK_SERVICE_INTERFACES_INNERAPI_CONCURRENT_TASK_CLIENT_INCLUDE_CONCURRENT_TASK_ERRORS_H diff --git a/frameworks/concurrent_task_client/include/concurrent_task_service_proxy.h b/frameworks/concurrent_task_client/include/concurrent_task_service_proxy.h new file mode 100644 index 0000000000000000000000000000000000000000..bd07586d87523eea52e558574957dd93d846f9a9 --- /dev/null +++ b/frameworks/concurrent_task_client/include/concurrent_task_service_proxy.h @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * 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 CONCUURENT_TASK_INTERFACES_INNERAPI_CONCURRENT_TASK_CLIENT_INCLUDE_CONCUURENT_TASK_SERVICE_PROXY_H +#define CONCUURENT_TASK_INTERFACES_INNERAPI_CONCURRENT_TASK_CLIENT_INCLUDE_CONCUURENT_TASK_SERVICE_PROXY_H + +#include "iremote_proxy.h" +#include "iremote_object.h" +#include "iconcurrent_task_service.h" + +namespace OHOS { +namespace ConcurrentTask { +class ConcurrentTaskServiceProxy : public IRemoteProxy { +public: + explicit ConcurrentTaskServiceProxy(const sptr& impl) : IRemoteProxy(impl) {} + virtual ~ConcurrentTaskServiceProxy() {} + + void ReportData(uint32_t resType, int64_t value, const Json::Value& payload) override; + void QueryInterval(int queryItem, IntervalReply& queryRs) override; + +private: + DISALLOW_COPY_AND_MOVE(ConcurrentTaskServiceProxy); + static inline BrokerDelegator delegator_; +}; +} // namespace ConcurrentTask +} // namespace OHOS + +#endif // CONCUURENT_TASK_INTERFACES_INNERAPI_CONCURRENT_TASK_CLIENT_INCLUDE_CONCUURENT_TASK_SERVICE_PROXY_H \ No newline at end of file diff --git a/frameworks/concurrent_task_client/include/concurrent_task_type.h b/frameworks/concurrent_task_client/include/concurrent_task_type.h new file mode 100644 index 0000000000000000000000000000000000000000..70d5105d57af8fddf888867630de8b7540120f8e --- /dev/null +++ b/frameworks/concurrent_task_client/include/concurrent_task_type.h @@ -0,0 +1,58 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * 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 CONCURRENT_TASK_SERVICES_CONCURRENTSEVICE_INCLUDE_CONCURRENT_TASK_TYPE_H +#define CONCURRENT_TASK_SERVICES_CONCURRENTSEVICE_INCLUDE_CONCURRENT_TASK_TYPE_H +namespace OHOS { +namespace ConcurrentTask { +constexpr int MAX_KEY_THREADS = 5; + +enum MsgType { + MSG_FOREGROUND = 0, + MSG_BACKGROUND, + MSG_APP_START, + MSG_APP_KILLED, + MSG_FOCUS, + MSG_SYSTEM_MAX, + MSG_APP_START_TYPE = 100, + MSG_REG_RENDER = MSG_APP_START_TYPE, + MSG_REG_UI, + MSG_REG_KEY_THERAD, + MSG_TYPE_MAX +}; + +enum PrioType { + PRIO_RT = 0, + RPIO_IN = 1, + PRIO_NORMAL = 2, +}; + +enum QueryIntervalItem { + QUERY_UI = 0, + QUERY_RENDER = 1, + QUERY_RENDER_SERVICE = 2, + QUERY_COMPOSER = 3, + QURRY_TYPE_MAX +}; + +struct IntervalReply { + int rtgId; + int paramA; + int paramB; + int paramC; +}; +} +} +#endif // CONCURRENT_TASK_SERVICES_CONCURRENTSEVICE_INCLUDE_CONCURRENT_TASK_TYPE_H \ No newline at end of file diff --git a/frameworks/concurrent_task_client/include/iconcurrent_task_service.h b/frameworks/concurrent_task_client/include/iconcurrent_task_service.h new file mode 100644 index 0000000000000000000000000000000000000000..126f892abe650e8142063deaad276a98be23ac43 --- /dev/null +++ b/frameworks/concurrent_task_client/include/iconcurrent_task_service.h @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * 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 CONCURRENT_TASK_SEVICES_INTERFACES_INNERAPI_CONCURRENT_TASK_CLIENT_INCLUDE_ICONCURRENT_TASK_SERVICE_H +#define CONCURRENT_TASK_SEVICES_INTERFACES_INNERAPI_CONCURRENT_TASK_CLIENT_INCLUDE_ICONCURRENT_TASK_SERVICE_H + +#include "iremote_broker.h" +#include "json/json.h" +#include "concurrent_task_type.h" + +namespace OHOS { +namespace ConcurrentTask { +class IConcurrentTaskService : public IRemoteBroker { +public: + enum : uint32_t { + REPORT_DATA = 1, + QUERY_INTERVAL = 2, + }; + + DECLARE_INTERFACE_DESCRIPTOR(u"OHOS.ResourceSchedule.ConcurrentTaskService"); + + virtual void ReportData(uint32_t resType, int64_t value, const Json::Value& payload) = 0; + virtual void QueryInterval(int queryItem, IntervalReply& queryRs) = 0; +}; +} // namespace ConcurrentTask +} // namespace OHOS + +#endif // CONCURRENT_TASK_SEVICES_INTERFACES_INNERAPI_CONCURRENT_TASK_CLIENT_INCLUDE_ICONCURRENT_TASK_SERVICE_H diff --git a/frameworks/concurrent_task_client/src/concurrent_task_client.cpp b/frameworks/concurrent_task_client/src/concurrent_task_client.cpp new file mode 100644 index 0000000000000000000000000000000000000000..03c3296ffc9994fcf32f4c25982c34fd44eae092 --- /dev/null +++ b/frameworks/concurrent_task_client/src/concurrent_task_client.cpp @@ -0,0 +1,110 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * 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 "concurrent_task_client.h" +#include +#include "if_system_ability_manager.h" +#include "iservice_registry.h" +#include "concurrent_task_log.h" +#include "concurrent_task_errors.h" +#include "system_ability_definition.h" + +namespace OHOS { +namespace ConcurrentTask { +ConcurrentTaskClient& ConcurrentTaskClient::GetInstance() +{ + static ConcurrentTaskClient instance; + return instance; +} + +void ConcurrentTaskClient::ReportData(uint32_t resType, int64_t value, + const std::unordered_map& mapPayload) +{ + CONCUR_LOGD("ConcurrentTaskClient::ReportData receive resType = %{public}u, value = %{public}" PRId64 ".", + resType, value); + if (TryConnect() != ERR_OK) { + return; + } + Json::Value payload; + for (auto it = mapPayload.begin(); it != mapPayload.end(); ++it) { + payload[it->first] = it->second; + } + clientService_->ReportData(resType, value, payload); +} + +void ConcurrentTaskClient::QueryInterval(int queryItem, IntervalReply& queryRs) +{ + if (TryConnect() != ERR_OK) { + CONCUR_LOGE("QueryInterval connnect fail"); + return; + } + clientService_->QueryInterval(queryItem, queryRs); + return; +} + +ErrCode ConcurrentTaskClient::TryConnect() +{ + std::lock_guard lock(mutex_); + if (clientService_) { + return ERR_OK; + } + + sptr systemManager = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); + if (!systemManager) { + CONCUR_LOGE("ConcurrentTaskClient::Fail to get registry."); + return GET_CONCURRENT_TASK_SERVICE_FAILED; + } + + remoteObject_ = systemManager->GetSystemAbility(CONCURRENT_TASK_SERVICE_ID); + if (!remoteObject_) { + CONCUR_LOGE("ConcurrentTaskClient::Fail to connect concurrent task schedule service."); + return GET_CONCURRENT_TASK_SERVICE_FAILED; + } + + clientService_ = iface_cast(remoteObject_); + if (!clientService_) { + return GET_CONCURRENT_TASK_SERVICE_FAILED; + } + try { + recipient_ = new ConcurrentTaskDeathRecipient(*this); + } catch (const std::bad_alloc& e) { + CONCUR_LOGE("ConcurrentTaskClient::New ConcurrentTaskDeathRecipient fail."); + } + if (!recipient_) { + return GET_CONCURRENT_TASK_SERVICE_FAILED; + } + clientService_->AsObject()->AddDeathRecipient(recipient_); + CONCUR_LOGD("ConcurrentTaskClient::Connect concurrent task service success."); + return ERR_OK; +} + +void ConcurrentTaskClient::StopRemoteObject() +{ + if (clientService_ && clientService_->AsObject()) { + clientService_->AsObject()->RemoveDeathRecipient(recipient_); + } + clientService_ = nullptr; +} + +ConcurrentTaskClient::ConcurrentTaskDeathRecipient::ConcurrentTaskDeathRecipient( + ConcurrentTaskClient& concurrentTaskClient) : concurrentTaskClient_(concurrentTaskClient) {} + +ConcurrentTaskClient::ConcurrentTaskDeathRecipient::~ConcurrentTaskDeathRecipient() {} + +void ConcurrentTaskClient::ConcurrentTaskDeathRecipient::OnRemoteDied(const wptr& object) +{ + concurrentTaskClient_.StopRemoteObject(); +} +} // namespace ConcurrentTask +} // namespace OHOS \ No newline at end of file diff --git a/frameworks/concurrent_task_client/src/concurrent_task_service_proxy.cpp b/frameworks/concurrent_task_client/src/concurrent_task_service_proxy.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a417ee8dfa1fc652b998524ea424e826679ce171 --- /dev/null +++ b/frameworks/concurrent_task_client/src/concurrent_task_service_proxy.cpp @@ -0,0 +1,84 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * 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 "concurrent_task_service_proxy.h" +#include "concurrent_task_log.h" +#include "concurrent_task_errors.h" + +namespace OHOS { +namespace ConcurrentTask { +void ConcurrentTaskServiceProxy::ReportData(uint32_t resType, int64_t value, const Json::Value& payload) +{ + int32_t error; + MessageParcel data; + MessageParcel reply; + MessageOption option = { MessageOption::TF_ASYNC }; + if (!data.WriteInterfaceToken(ConcurrentTaskServiceProxy::GetDescriptor())) { + return; + } + if (!data.WriteUint32(resType)) { + return; + } + if (!data.WriteInt64(value)) { + return; + } + if (!data.WriteString(payload.toStyledString())) { + return; + } + error = Remote()->SendRequest(IConcurrentTaskService::REPORT_DATA, data, reply, option); + if (error != NO_ERROR) { + CONCUR_LOGE("Send request error: %{public}d", error); + return; + } + CONCUR_LOGD("ConcurrentTaskServiceProxy::ReportData success."); +} + +void ConcurrentTaskServiceProxy::QueryInterval(int queryItem, IntervalReply& queryRs) +{ + int32_t error; + MessageParcel data; + MessageParcel reply; + MessageOption option = { MessageOption::TF_SYNC }; + queryRs.rtgId = -1; + queryRs.paramA = -1; + queryRs.paramB = -1; + queryRs.paramC = -1; + if (!data.WriteInterfaceToken(ConcurrentTaskServiceProxy::GetDescriptor())) { + return; + } + if (!data.WriteInt64(queryItem)) { + return; + } + error = Remote()->SendRequest(IConcurrentTaskService::QUERY_INTERVAL, data, reply, option); + if (error != NO_ERROR) { + CONCUR_LOGE("QueryInterval error: %{public}d", error); + return; + } + if (!reply.ReadInt32(queryRs.rtgId)) { + return; + } + if (!reply.ReadInt32(queryRs.paramA)) { + return; + } + if (!reply.ReadInt32(queryRs.paramB)) { + return; + } + if (!reply.ReadInt32(queryRs.paramC)) { + return; + } + return; +} +} // namespace ConcurrentTask +} // namespace OHOS \ No newline at end of file diff --git a/include/concurrent_task_log.h b/include/concurrent_task_log.h new file mode 100644 index 0000000000000000000000000000000000000000..716af45cb2084d2a89316869da5322016415d7ca --- /dev/null +++ b/include/concurrent_task_log.h @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * 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 CONCURRENT_TASK_COMMON_INCLUDE_CONCURRENT_TASK_LOG_H +#define CONCURRENT_TASK_COMMON_INCLUDE_CONCURRENT_TASK_LOG_H + +#include "hilog/log.h" + +namespace OHOS { +namespace ConcurrentTask { +constexpr OHOS::HiviewDFX::HiLogLabel CONCUR_LABEL = { + LOG_CORE, + 0xD001707, + "CONCUR" +}; + +#define CONCUR_LOGF(...) (void)OHOS::HiviewDFX::HiLog::Fatal(CONCUR_LABEL, __VA_ARGS__) +#define CONCUR_LOGE(...) (void)OHOS::HiviewDFX::HiLog::Error(CONCUR_LABEL, __VA_ARGS__) +#define CONCUR_LOGW(...) (void)OHOS::HiviewDFX::HiLog::Warn(CONCUR_LABEL, __VA_ARGS__) +#define CONCUR_LOGI(...) (void)OHOS::HiviewDFX::HiLog::Info(CONCUR_LABEL, __VA_ARGS__) +#define CONCUR_LOGD(...) (void)OHOS::HiviewDFX::HiLog::Debug(CONCUR_LABEL, __VA_ARGS__) +} // namespace ConcurrentTask +} // namespace OHOS + +#endif // CONCURRENT_TASK diff --git a/interfaces/inner_api/concurrent_task_client.h b/interfaces/inner_api/concurrent_task_client.h new file mode 100644 index 0000000000000000000000000000000000000000..5e2732d83b496bd29a558a81d6fb883a5201ef62 --- /dev/null +++ b/interfaces/inner_api/concurrent_task_client.h @@ -0,0 +1,72 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * 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 CONCURRENT_TASK_SERVICE_INTERFACES_INNERAPI_CONCURRENT_TASK_CLIENT_INCLUDE_CONCURRENT_TASK_CLIENT_H +#define CONCURRENT_TASK_SERVICE_INTERFACES_INNERAPI_CONCURRENT_TASK_CLIENT_INCLUDE_CONCURRENT_TASK_CLIENT_H + +#include +#include "iremote_object.h" +#include "iconcurrent_task_service.h" + +namespace OHOS { +namespace ConcurrentTask { +/* + * this class wraped the functions of IConcurrentTaskService,effect is the same. + * but through ConcurrentTaskClient, you don't need to get IConcurrentTaskService from samgr, + * just use the functions is ok. + */ + +class ConcurrentTaskClient { +public: + /** + * Only need one client connect to ConcurrentTaskService, singleton pattern. + * + * @return Returns the only one implement of ConcurrentTaskClient. + */ + static ConcurrentTaskClient& GetInstance(); + + void ReportData(uint32_t resType, int64_t value, const std::unordered_map& mapPayload); + void QueryInterval(int queryItem, IntervalReply& queryRs); + + void StopRemoteObject(); + +protected: + ConcurrentTaskClient() = default; + virtual ~ConcurrentTaskClient() = default; + +private: + class ConcurrentTaskDeathRecipient : public IRemoteObject::DeathRecipient { + public: + explicit ConcurrentTaskDeathRecipient(ConcurrentTaskClient& concurrentTaskClient); + + ~ConcurrentTaskDeathRecipient() override; + + void OnRemoteDied(const wptr& object) override; + + private: + ConcurrentTaskClient& concurrentTaskClient_; + }; + ErrCode TryConnect(); + + std::mutex mutex_; + sptr recipient_; + sptr remoteObject_; + sptr clientService_; + DISALLOW_COPY_AND_MOVE(ConcurrentTaskClient); +}; +} // namespace ConcurrentTask +} // namespace OHOS + +#endif // CONCURRENT_TASK_SERVICE_INTERFACES_INNERAPI_ConcurrentTask_CLIENT_INCLUDE_CONCURRENT_TASK_CLIENT_H diff --git a/sa_profile/1912.xml b/sa_profile/1912.xml new file mode 100644 index 0000000000000000000000000000000000000000..e520da5d66e7d84ee849d81d693d5eb8069a8e5d --- /dev/null +++ b/sa_profile/1912.xml @@ -0,0 +1,28 @@ + + + + concurrent_task_service + + libconcurrentsvc.z.so + + + 1912 + libconcurrentsvc.z.so + true + false + 1 + + diff --git a/sa_profile/BUILD.gn b/sa_profile/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..725f2f18fe77a49d27a0b8fb2bfb514c1d5a2d17 --- /dev/null +++ b/sa_profile/BUILD.gn @@ -0,0 +1,20 @@ +# Copyright (c) 2022 Huawei Device Co., Ltd. +# 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. + +import("//build/ohos/sa_profile/sa_profile.gni") + +ohos_sa_profile("concurrent_task_sa_profile") { + sources = [ "1912.xml" ] + + part_name = "resourceschedule_qos_manager" +} diff --git a/services/BUILD.gn b/services/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..123c5f1d774e01b2aaf41fd494976135935c9087 --- /dev/null +++ b/services/BUILD.gn @@ -0,0 +1,64 @@ +# Copyright (c) 2022 Huawei Device Co., Ltd. +# 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. + +import("//build/ohos.gni") + +config("concurrent_task_config") { + visibility = [":*"] + cflags_cc = [ "-fexceptions" ] + include_dirs = [ + "include", + "../include", + "../frameworks/concurrent_task_client/include/", + "../interfaces/inner_api/", + "//foundation/resourceschedule/frame_aware_sched/common/include/", + "//utils/system/safwk/native/include", + "//commonlibrary/c_utils/base/include", + "//third_party/libxml2/include", + "//third_party/jsoncpp/include", + ] +} + +ohos_shared_library("concurrentsvc") { + + public_configs = [ + ":concurrent_task_config", + ] + + sources = [ + "src/concurrent_task_service.cpp", + "src/concurrent_task_service_ability.cpp", + "src/concurrent_task_service_stub.cpp", + "src/concurrent_task_controller.cpp", + "src/qos_interface.cpp", + "src/qos_manager.cpp", + ] + + deps = [ + "//foundation/resourceschedule/frame_aware_sched/interfaces/innerkits/frameintf:rtg_interface", + "//third_party/jsoncpp:jsoncpp", + "//third_party/libxml2:xml2", + ] + + external_deps = [ + "c_utils:utils", + "eventhandler:libeventhandler", + "hiviewdfx_hilog_native:libhilog", + "ipc:ipc_single", + "safwk:system_ability_fwk", + "samgr:samgr_proxy", + ] + + subsystem_name = "resourceschedule" + part_name = "resourceschedule_qos_manager" +} diff --git a/services/include/concurrent_task_controller.h b/services/include/concurrent_task_controller.h new file mode 100644 index 0000000000000000000000000000000000000000..4c7ed16ba609602835d3c97cd14984521c2df542 --- /dev/null +++ b/services/include/concurrent_task_controller.h @@ -0,0 +1,92 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * 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 CONCURRENT_TASK_SERVICES_CONCURRENTSEVICE_INCLUDE_CONCURRENT_TASK_CONTROLLER_H +#define CONCURRENT_TASK_SERVICES_CONCURRENTSEVICE_INCLUDE_CONCURRENT_TASK_CONTROLLER_H + +#include +#include +#include +#include +#include +#include "json/json.h" +#include "concurrent_task_type.h" +#include "qos_manager.h" + +namespace OHOS { +namespace ConcurrentTask { +class ForegroundAppRecord; + +class TaskController { +public: + static TaskController& GetInstance(); + TaskController() = default; + virtual ~TaskController() = default; + void ReportData(uint32_t resType, int64_t value, const Json::Value& payload); + void QueryInterval(int queryItem, IntervalReply& queryRs); + void Init(); + void Release(); + +private: + bool CheckUid(pid_t uid); + void TypeMapInit(); + void QosApplyInit(); + void SetHwcAuth(bool status); + void TryCreateRsGroup(); + void QueryUi(pid_t uid, IntervalReply& queryRs); + void QueryRender(pid_t uid, IntervalReply& queryRs); + void QueryRenderService(pid_t uid, IntervalReply& queryRs); + void QueryHwc(pid_t uid, IntervalReply& queryRs); + int GetRequestType(std::string strRequstType); + void DealSystemRequest(int requestType, const Json::Value& payload); + void DealAppRequest(int requestType, const Json::Value& payload, pid_t uid); + void NewForeground(int uid); + void NewBackground(int uid); + void NewAppStart(int uid); + void AppKilled(int uid); + std::list::iterator GetRecordOfUid(int uid); + void PrintInfo(); + + std::mutex appInfoLock_; + std::list foregroundApp_ = {}; + std::unordered_map msgType_ = {}; + QosManager qosManager_; + std::vector authApps_; + int renderServiceGrpId_ = -1; + bool rtgEnabled_ = false; +}; + +class ForegroundAppRecord { +public: + explicit ForegroundAppRecord(int uid); + ~ForegroundAppRecord(); + + void AddKeyThread(int tid, int prio = PRIO_NORMAL); + bool BeginScene(); + bool EndScene(); + int GetUid(); + int GetGrpId(); + bool IsValid(); + void PrintKeyThreads(); + +private: + int uid_ = 0; + int grpId_ = 0; + std::unordered_set keyThreads_; +}; +} // namespace ConcurrentTask +} // namespace OHOS + +#endif // CONCURRENT_TASK_SERVICES_CONCURRENTSEVICE_INCLUDE_CONCURRENT_TASK_CONTROLLER_H \ No newline at end of file diff --git a/services/include/concurrent_task_service.h b/services/include/concurrent_task_service.h new file mode 100644 index 0000000000000000000000000000000000000000..3c8f5aca82014b2fb2a7a426a21b187a229ef4a7 --- /dev/null +++ b/services/include/concurrent_task_service.h @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * 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 CONCURRENT_TASK_SERVICES_CONCURRENTSEVICE_INCLUDE_CONCURRENT_TASK_SEVICE_H +#define CONCURRENT_TASK_SERVICES_CONCURRENTSEVICE_INCLUDE_CONCURRENT_TASK_SEVICE_H + +#include "concurrent_task_service_stub.h" +#include "concurrent_task_log.h" + +namespace OHOS { +namespace ConcurrentTask { +class ConcurrentTaskService : public ConcurrentTaskServiceStub { +public: + ConcurrentTaskService() {} + ~ConcurrentTaskService() override = default; + + void ReportData(uint32_t resType, int64_t value, const Json::Value& payload) override; + void QueryInterval(int queryItem, IntervalReply& queryRs) override; +private: + DISALLOW_COPY_AND_MOVE(ConcurrentTaskService); +}; +} // namespace ConcurrentTask +} // namespace OHOS + +#endif // CONCURRENT_TASK_SERVICES_CONCURRENTSEVICE_INCLUDE_CONCURRENT_TASK_SEVICE_H diff --git a/services/include/concurrent_task_service_ability.h b/services/include/concurrent_task_service_ability.h new file mode 100644 index 0000000000000000000000000000000000000000..8a9a33b619c939d267336907303e33d3d6bfafe4 --- /dev/null +++ b/services/include/concurrent_task_service_ability.h @@ -0,0 +1,47 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * 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 CONCURRENT_TASK_SEVICES_INCLUDE_CONCURRENT_TASK_SERVICE_ABILITY_H +#define CONCURRENT_TASK_SEVICES_INCLUDE_CONCURRENT_TASK_SERVICE_ABILITY_H + +#include "system_ability.h" +#include "concurrent_task_service.h" + +namespace OHOS { +namespace ConcurrentTask { +class ConcurrentTaskServiceAbility : public SystemAbility { + DECLARE_SYSTEM_ABILITY(ConcurrentTaskServiceAbility); + +public: + ConcurrentTaskServiceAbility(int32_t sysAbilityId, bool runOnCreate) : SystemAbility(sysAbilityId, runOnCreate) {} + ~ConcurrentTaskServiceAbility() override = default; + +private: + void OnStart() override; + + void OnStop() override; + + void OnAddSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override; + + void OnRemoveSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override; + + sptr service_; + + DISALLOW_COPY_AND_MOVE(ConcurrentTaskServiceAbility); +}; +} // namespace ConcurrentTask +} // namespace OHOS + +#endif // CONCURRENT_TASK_SEVICES_INCLUDE_CONCURRENT_TASK_SERVICE_ABILITY_H diff --git a/services/include/concurrent_task_service_stub.h b/services/include/concurrent_task_service_stub.h new file mode 100644 index 0000000000000000000000000000000000000000..9b4a62c5a9b00c83b48610ad685bbbb45c2b9a96 --- /dev/null +++ b/services/include/concurrent_task_service_stub.h @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * 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 CONCURRENT_TASK_SEVICES_INCLUDE_CONCURRENT_TASK_SERVICE_STUB_H +#define CONCURRENT_TASK_SEVICES_INCLUDE_CONCURRENT_TASK_SERVICE_STUB_H + +#include +#include "iremote_stub.h" +#include "iconcurrent_task_service.h" + +namespace OHOS { +namespace ConcurrentTask { +class ConcurrentTaskServiceStub : public IRemoteStub { +public: + ConcurrentTaskServiceStub(); + ~ConcurrentTaskServiceStub(); + int32_t OnRemoteRequest(uint32_t code, MessageParcel& data, MessageParcel& reply, MessageOption& option) override; + +private: + int32_t ReportDataInner(MessageParcel& data, MessageParcel& reply); + int32_t QueryIntervalInner(MessageParcel& data, MessageParcel& reply); + Json::Value StringToJson(const std::string& str); + + void Init(); + + using RequestFuncType = std::function; + std::map funcMap_; +}; +} // namespace ConcurrentTask +} // namespace OHOS + +#endif // CONCURRENT_TASK_SEVICES_INCLUDE_CONCURRENT_TASK_SERVICE_STUB_H diff --git a/services/include/qos_interface.h b/services/include/qos_interface.h new file mode 100644 index 0000000000000000000000000000000000000000..70daa1fefec857566653a7c2e4d0a326103caa0d --- /dev/null +++ b/services/include/qos_interface.h @@ -0,0 +1,155 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * 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 QOS_INTERFACE_H +#define QOS_INTERFACE_H + +#ifdef __cplusplus +extern "C" { +#endif + +/* + * generic + */ +#define SYSTEM_UID 1000 +#define ROOT_UID 0 + +/* + * auth_ctrl + */ +struct AuthCtrlData { + unsigned int uid; + unsigned int type; + unsigned int rtgUaFlag; + unsigned int qosUaFlag; + unsigned int status; +}; + +enum class AuthManipulateType { + AUTH_ENABLE = 1, + AUTH_DELETE, + AUTH_GET, + AUTH_SWITCH, + AUTH_MAX_NR, +}; + +enum class AuthStatus { + AUTH_STATUS_DISABLED = 1, + AUTH_STATUS_SYSTEM_SERVER = 2, + AUTH_STATUS_FOREGROUND = 3, + AUTH_STATUS_BACKGROUND = 4, + AUTH_STATUS_DEAD, +}; + +enum class QosClassLevel { + qos_unspecified = 0, + qos_background = 1, + qos_utility = 2, + qos_default = 3, + qos_user_initiated = 4, + qos_deadline_request = 5, + qos_user_interactive = 6, + qos_max, +}; + +#define BASIC_AUTH_CTRL_OPERATION \ + _IOWR(0xCD, 1, struct AuthCtrlData) + +/* + * qos ctrl + */ +enum class QosManipulateType { + QOS_APPLY = 1, + QOS_LEAVE, + QOS_MAX_NR, +}; + +struct QosCtrlData { + int pid; + unsigned int type; + unsigned int level; +}; + +struct QosPolicyData { + int nice; + int latencyNice; + int uclampMin; + int uclampMax; + int rtSchedPriority; +}; + +enum class QosPolicyType { + QOS_POLICY_DEFAULT = 1, + QOS_POLICY_SYSTEM_SERVER = 2, + QOS_POLICY_FRONT = 3, + QOS_POLICY_BACK = 4, + QOS_POLICY_MAX_NR, +}; + +#define QOS_FLAG_NICE 0X01 +#define QOS_FLAG_LATENCY_NICE 0X02 +#define QOS_FLAG_UCLAMP 0x04 +#define QOS_FLAG_RT 0x08 + +#define QOS_FLAG_ALL (QOS_FLAG_NICE | \ + QOS_FLAG_LATENCY_NICE | \ + QOS_FLAG_UCLAMP | \ + QOS_FLAG_RT) + +struct QosPolicyDatas { + int policyType; + unsigned int policyFlag; + struct QosPolicyData policys[static_cast(QosClassLevel::qos_max)]; +}; + +#define QOS_CTRL_BASIC_OPERATION \ + _IOWR(0xCC, 1, struct QosCtrlData) +#define QOS_CTRL_POLICY_OPERATION \ + _IOWR(0xCC, 2, struct QosPolicyDatas) + +/* + * RTG + */ +#define AF_RTG_ALL 0x1fff +#define AF_RTG_DELEGATED 0x1fff + +struct RtgEnableData { + int enable; + int len; + char *data; +}; + +#define CMD_ID_SET_ENABLE \ + _IOWR(0xAB, 1, struct RtgEnableData) + +/* + * interface + */ +int EnableRtg(bool flag); +int AuthEnable(unsigned int uid, unsigned int uaFlag, unsigned int status); +int AuthPause(unsigned int uid); +int AuthDelete(unsigned int uid); +int AuthGet(unsigned int uid, unsigned int *uaFlag, unsigned int *status); +int AuthSwitch(unsigned int uid, unsigned int rtgFlag, unsigned int qosFlag, unsigned int status); +int QosApply(unsigned int level); +int QosApplyForOther(unsigned int level, int tid); +int QosLeave(void); +int QosLeaveForOther(int tid); +int QosPolicy(struct QosPolicyDatas *policyDatas); + +#ifdef __cplusplus +} +#endif +#endif /* OQS_INTERFACE_H */ \ No newline at end of file diff --git a/services/include/qos_manager.h b/services/include/qos_manager.h new file mode 100644 index 0000000000000000000000000000000000000000..a22fa42e7ac3bea77eb0b97f30d61084ea18599b --- /dev/null +++ b/services/include/qos_manager.h @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * 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 QOS_MANAGER_H +#define QOS_MANAGER_H + +#include "qos_interface.h" + +namespace OHOS { +namespace ConcurrentTask { +class QosManager { +public: + void Init(); + int SetQosPolicy(struct QosPolicyDatas *policyDatas); +}; +} // namespace ConcurrentTask +} // namespace OHOS + +#endif diff --git a/services/src/concurrent_task_controller.cpp b/services/src/concurrent_task_controller.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9d2f0828d9be2cda4d638e3394aada7850aaf171 --- /dev/null +++ b/services/src/concurrent_task_controller.cpp @@ -0,0 +1,520 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * 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 +#include +#include +#include +#include "rtg_interface.h" +#include "ipc_skeleton.h" +#include "concurrent_task_log.h" +#include "concurrent_task_controller.h" +constexpr int TARGET_UID = 3039; +using namespace OHOS::RME; + +namespace OHOS { +namespace ConcurrentTask { +TaskController& TaskController::GetInstance() +{ + static TaskController instance; + return instance; +} + +void TaskController::ReportData(uint32_t resType, int64_t value, const Json::Value& payload) +{ + pid_t uid = IPCSkeleton::GetInstance().GetCallingUid(); + if (!CheckUid(uid)) { + CONCUR_LOGE("only system call can be allowed"); + return; + } + Json::ValueType type = payload.type(); + if (type != Json::objectValue) { + CONCUR_LOGE("error payload"); + return; + } + if (payload.empty()) { + CONCUR_LOGE("payload is empty"); + return; + } + std::string strRequstType = ""; + try { + strRequstType = payload["type"].asString(); + } catch (...) { + CONCUR_LOGE("Unexpected type format"); + return; + } + if (strRequstType.length() == 0) { + CONCUR_LOGE("Get payload type err"); + return; + } + int requstType = GetRequestType(strRequstType); + DealSystemRequest(requstType, payload); + PrintInfo(); +} + +void TaskController::QueryInterval(int queryItem, IntervalReply& queryRs) +{ + pid_t uid = IPCSkeleton::GetInstance().GetCallingUid(); + if (uid == 0) { + CONCUR_LOGE("Uid is 0, error query"); + return; + } + switch (queryItem) { + case QUERY_UI: + QueryUi(uid, queryRs); + break; + case QUERY_RENDER: + QueryRender(uid, queryRs); + break; + case QUERY_RENDER_SERVICE: + QueryRenderService(uid, queryRs); + break; + case QUERY_COMPOSER: + QueryHwc(uid, queryRs); + break; + default: + break; + } +} + +void TaskController::QueryUi(int uid, IntervalReply& queryRs) +{ + if (uid == SYSTEM_UID) { + return; + } + pid_t pid = IPCSkeleton::GetInstance().GetCallingPid(); + auto iter = GetRecordOfUid(uid); + if (iter == foregroundApp_.end()) { + CONCUR_LOGD("Query ui with uid %{public}d failed: pid %{public}d", uid, pid); + return; + } + int grpId = iter->GetGrpId(); + if (grpId <= 0) { + CONCUR_LOGI("%{public}d Query ui with none grpid", uid); + queryRs.rtgId = -1; + } else { + queryRs.rtgId = grpId; + } +} + +void TaskController::QueryRender(int uid, IntervalReply& queryRs) +{ + if (uid == SYSTEM_UID) { + return; + } + pid_t pid = IPCSkeleton::GetInstance().GetCallingPid(); + auto iter = GetRecordOfUid(uid); + if (iter == foregroundApp_.end()) { + CONCUR_LOGD("Query render with uid %{public}d failed, pid %{public}d", uid, pid); + return; + } + int grpId = iter->GetGrpId(); + if (grpId <= 0) { + CONCUR_LOGI("%{public}d Query render with none grpid", uid); + queryRs.rtgId = -1; + } else { + queryRs.rtgId = grpId; + } +} + +void TaskController::QueryRenderService(int uid, IntervalReply& queryRs) +{ + if (renderServiceGrpId_ > 0) { + CONCUR_LOGD("uid %{public}d query rs group %{public}d.", uid, renderServiceGrpId_); + queryRs.rtgId = renderServiceGrpId_; + return; + } + TryCreateRsGroup(); + queryRs.rtgId = renderServiceGrpId_; + CONCUR_LOGE("uid %{public}d query rs group failed and create %{public}d.", uid, renderServiceGrpId_); +} + +void TaskController::QueryHwc(int uid, IntervalReply& queryRs) +{ + if (uid == SYSTEM_UID) { + return; + } + pid_t pid = IPCSkeleton::GetInstance().GetCallingPid(); + auto iter = GetRecordOfUid(uid); + if (iter == foregroundApp_.end()) { + CONCUR_LOGD("Query ipc thread with uid %{public}d failed, pid %{public}d", uid, pid); + return; + } + int grpId = iter->GetGrpId(); + if (grpId <= 0) { + CONCUR_LOGI("%{public}d Query ipc thread with none grpid", uid); + queryRs.rtgId = -1; + } else { + queryRs.rtgId = grpId; + } +} + +void TaskController::SetHwcAuth(bool status) +{ + int ret; + if (status) { + ret = AuthEnable(TARGET_UID, AF_RTG_ALL, static_cast(AuthStatus::AUTH_STATUS_FOREGROUND)); + } else { + ret = AuthDelete(TARGET_UID); + } + + if (ret == 0) { + CONCUR_LOGI("set auth status(%{public}d) for %{public}d success", status, TARGET_UID); + } else { + CONCUR_LOGE("set auth status(%{public}d) for %{public}d fail with ret %{public}d ", status, TARGET_UID, ret); + } +} + +void TaskController::Init() +{ + SetHwcAuth(true); + TypeMapInit(); + qosManager_.Init(); + TryCreateRsGroup(); +} + +void TaskController::Release() +{ + SetHwcAuth(false); + msgType_.clear(); + if (renderServiceGrpId_ <= 0) { + return; + } + DestroyRtgGrp(renderServiceGrpId_); + renderServiceGrpId_ = -1; +} + +void TaskController::TypeMapInit() +{ + msgType_.clear(); + msgType_.insert(pair("foreground", MSG_FOREGROUND)); + msgType_.insert(pair("background", MSG_BACKGROUND)); + msgType_.insert(pair("appStart", MSG_APP_START)); + msgType_.insert(pair("appKilled", MSG_APP_KILLED)); +} + +void TaskController::TryCreateRsGroup() +{ + if (!rtgEnabled_) { + rtgEnabled_ = EnableRtg(true) < 0 ? false : true; + if (!rtgEnabled_) { + CONCUR_LOGE("Rtg enable failed"); + return; + } + CONCUR_LOGI("Enable Rtg"); + } + renderServiceGrpId_ = CreateNewRtgGrp(PRIO_RT, MAX_KEY_THREADS); + if (renderServiceGrpId_ <= 0) { + CONCUR_LOGI("CreateRsRtgGroup with RT failed, try change to normal type."); + renderServiceGrpId_ = CreateNewRtgGrp(PRIO_NORMAL, MAX_KEY_THREADS); + } + if (renderServiceGrpId_ <= 0) { + CONCUR_LOGI("CreateRsRtgGroup failed! rtGrp:%{public}d", renderServiceGrpId_); + } +} + +int TaskController::GetRequestType(std::string strRequstType) +{ + auto iter = msgType_.find(strRequstType); + if (iter == msgType_.end()) { + return MSG_TYPE_MAX; + } + return msgType_[strRequstType]; +} + +bool TaskController::CheckUid(pid_t uid) +{ + if ((uid != SYSTEM_UID) && (uid != 0)) { + return false; + } + return true; +} + +void TaskController::DealSystemRequest(int requestType, const Json::Value& payload) +{ + int appUid = 0; + try { + appUid = stoi(payload["uid"].asString()); + } catch (...) { + CONCUR_LOGE("Unexpected uid format"); + } + if (appUid < 0) { + CONCUR_LOGE("appUid error:%d", appUid); + return; + } + switch (requestType) { + case MSG_FOREGROUND: + NewForeground(appUid); + break; + case MSG_BACKGROUND: + NewBackground(appUid); + break; + case MSG_APP_START: + NewAppStart(appUid); + break; + case MSG_APP_KILLED: + AppKilled(appUid); + break; + default: + CONCUR_LOGE("Unknown system request"); + break; + } +} + +void TaskController::DealAppRequest(int requestType, const Json::Value& payload, pid_t uid) +{ + if (uid <= SYSTEM_UID) { + CONCUR_LOGE("Unexpected uid in app req"); + return; + } + int tid = 0; + try { + tid = stoi(payload["tid"].asString()); + } catch (...) { + CONCUR_LOGE("Unexpected tid format"); + return; + } + if ((requestType >= MSG_REG_RENDER) && (requestType <= MSG_REG_KEY_THERAD)) { + int prioType = PRIO_NORMAL; + auto record = GetRecordOfUid(uid); + if (record == foregroundApp_.end()) { + return; + } + if (requestType != MSG_REG_KEY_THERAD) { + prioType = PRIO_RT; + } + record->AddKeyThread(tid, prioType); + } +} + +std::list::iterator TaskController::GetRecordOfUid(int uid) +{ + for (auto iter = foregroundApp_.begin(); iter != foregroundApp_.end(); iter++) { + if (iter->GetUid() == uid) { + return iter; + } + } + return foregroundApp_.end(); +} + +void TaskController::NewForeground(int uid) +{ + auto it = find(authApps_.begin(), authApps_.end(), uid); + if (it == authApps_.end()) { + CONCUR_LOGI("un-authed uid %{public}d", uid); + return; + } + unsigned int uidParam = static_cast(uid); + unsigned int uaFlag = AF_RTG_ALL; + unsigned int status = static_cast(AuthStatus::AUTH_STATUS_FOREGROUND); + + int ret = AuthEnable(uidParam, uaFlag, status); + if (ret == 0) { + CONCUR_LOGI("auth_enable %{public}d success", uid); + } else { + CONCUR_LOGE("auth_enable %{public}d fail with ret %{public}d", uid, ret); + } + bool found = false; + for (auto iter = foregroundApp_.begin(); iter != foregroundApp_.end(); iter++) { + if (iter->GetUid() == uid) { + found = true; + CONCUR_LOGI("uid %{public}d is already in foreground.", uid); + iter->BeginScene(); + } + } + CONCUR_LOGI("uid %{public}d change to foreground.", uid); + if (!found) { + ForegroundAppRecord *tempRecord = new ForegroundAppRecord(uid); + if (tempRecord->IsValid()) { + foregroundApp_.push_back(*tempRecord); + tempRecord->BeginScene(); + } else { + delete tempRecord; + } + } +} + +void TaskController::NewBackground(int uid) +{ + auto it = find(authApps_.begin(), authApps_.end(), uid); + if (it == authApps_.end()) { + CONCUR_LOGI("un-authed uid %{public}d", uid); + return; + } + CONCUR_LOGI("uid %{public}d change to background.", uid); + unsigned int uidParam = static_cast(uid); + + int ret = AuthPause(uidParam); + if (ret == 0) { + CONCUR_LOGI("auth_pause %{public}d success", ret); + } else { + CONCUR_LOGI("auth_pause %{public}d fail with %{public}d", uid, ret); + } + for (auto iter = foregroundApp_.begin(); iter != foregroundApp_.end(); iter++) { + if (iter->GetUid() == uid) { + iter->EndScene(); + return; + } + } +} + +void TaskController::NewAppStart(int uid) +{ + CONCUR_LOGI("uid %{public}d start.", uid); + unsigned int uidParam = static_cast(uid); + unsigned int uaFlag = AF_RTG_ALL; + unsigned int status = static_cast(AuthStatus::AUTH_STATUS_BACKGROUND); + + int ret = AuthEnable(uidParam, uaFlag, status); + if (ret == 0) { + CONCUR_LOGI("auth_enable %{public}d success", uid); + } else { + CONCUR_LOGE("auth_enable %{public}d fail with ret %{public}d", uid, ret); + } + authApps_.push_back(uid); +} + +void TaskController::AppKilled(int uid) +{ + CONCUR_LOGI("uid %{public}d killed.", uid); + unsigned int uidParam = static_cast(uid); + int ret = AuthDelete(uidParam); + if (ret == 0) { + CONCUR_LOGI("auth_delete %{public}d success", uid); + } else { + CONCUR_LOGE("auth_delete %{public}d fail with %{public}d", uid, ret); + } + std::lock_guard lock(appInfoLock_); + for (auto iter = foregroundApp_.begin(); iter != foregroundApp_.end(); iter++) { + if (iter->GetUid() == uid) { + foregroundApp_.erase(iter++); + break; + } + } + for (auto iter = authApps_.begin(); iter != authApps_.end(); iter++) { + if (*iter == uid) { + authApps_.erase(iter++); + break; + } + } +} + +void TaskController::PrintInfo() +{ + for (auto iter = foregroundApp_.begin(); iter != foregroundApp_.end(); iter++) { + iter->PrintKeyThreads(); + } +} + +ForegroundAppRecord::ForegroundAppRecord(int uid) +{ + uid_ = uid; + grpId_ = CreateNewRtgGrp(PRIO_RT, MAX_KEY_THREADS); + if (grpId_ <= 0) { + CONCUR_LOGI("CreateNewRtgGroup with RT failed, try change to normal type."); + grpId_ = CreateNewRtgGrp(PRIO_NORMAL, MAX_KEY_THREADS); + } + if (grpId_ <= 0) { + CONCUR_LOGI("CreateNewRtgGroup failed! rtGrp:%{public}d, pid: %{public}d", grpId_, uid); + } +} + +ForegroundAppRecord::~ForegroundAppRecord() +{ + if (grpId_ > 0) { + DestroyRtgGrp(grpId_); + } +} + +void ForegroundAppRecord::AddKeyThread(int tid, int prio) +{ + int rtgPrio = (prio >= PRIO_NORMAL) ? PRIO_NORMAL : PRIO_RT; + if (keyThreads_.find(tid) != keyThreads_.end()) { + return; + } + if (grpId_ <= 0) { + CONCUR_LOGI("Add key thread fail: Grp id not been created success."); + return; + } + if (keyThreads_.size() >= MAX_KEY_THREADS) { + CONCUR_LOGI("Add key thread fail: Key threads num limit."); + return; + } + if (prio == RPIO_IN) { + setpriority(PRIO_PROCESS, tid, -13); // -13 represent spcial nice in qos + } else { + int ret = AddThreadToRtg(tid, grpId_, rtgPrio); + if (ret != 0) { + CONCUR_LOGI("Add key thread fail: Kernel err report."); + } else { + CONCUR_LOGI("Add key thread %{public}d", tid); + } + keyThreads_.insert(tid); + } +} + +bool ForegroundAppRecord::BeginScene() +{ + if (grpId_ <= 0) { + CONCUR_LOGI("Error begin scene in uid %{public}d", uid_); + return false; + } + OHOS::RME::BeginFrameFreq(grpId_, 0); + OHOS::RME::EndFrameFreq(grpId_); + return true; +} + +bool ForegroundAppRecord::EndScene() +{ + if (grpId_ <= 0) { + CONCUR_LOGI("Error end scene in uid %{public}d", uid_); + return false; + } + OHOS::RME::EndScene(grpId_); + return true; +} + +int ForegroundAppRecord::GetUid() +{ + return uid_; +} + +int ForegroundAppRecord::GetGrpId() +{ + return grpId_; +} + +bool ForegroundAppRecord::IsValid() +{ + if (uid_ > 0 && grpId_ > 0) { + return true; + } + return false; +} + +void ForegroundAppRecord::PrintKeyThreads() +{ + std::string strLog = "pid "; + strLog.append(std::to_string(uid_)); + strLog.append(" has key threads: "); + for (auto iter = keyThreads_.begin(); iter != keyThreads_.end(); iter++) { + std::string temp = std::to_string(*iter); + strLog.append(temp); + strLog.append(", "); + } + CONCUR_LOGD("%{public}s", strLog.c_str()); +} +} // namespace ConcurrentTask +} // namespace OHOS diff --git a/services/src/concurrent_task_service.cpp b/services/src/concurrent_task_service.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1173d1ef77c8d8c48a75854ed2a1ca729555e809 --- /dev/null +++ b/services/src/concurrent_task_service.cpp @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * 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 "concurrent_task_service.h" +#include "concurrent_task_controller.h" + +namespace OHOS { +namespace ConcurrentTask { +void ConcurrentTaskService::ReportData(uint32_t resType, int64_t value, const Json::Value& payload) +{ + TaskController::GetInstance().ReportData(resType, value, payload); +} + +void ConcurrentTaskService::QueryInterval(int queryItem, IntervalReply& queryRs) +{ + TaskController::GetInstance().QueryInterval(queryItem, queryRs); +} +} // namespace ConcurrentTask +} // namespace OHOS diff --git a/services/src/concurrent_task_service_ability.cpp b/services/src/concurrent_task_service_ability.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e3edd1f4ee00a5cfe72ebaa358cca948ffb0701d --- /dev/null +++ b/services/src/concurrent_task_service_ability.cpp @@ -0,0 +1,58 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * 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 "concurrent_task_service_ability.h" +#include "concurrent_task_log.h" +#include "concurrent_task_controller.h" +#include "concurrent_task_service.h" +#include "system_ability_definition.h" + +namespace OHOS { +namespace ConcurrentTask { +REGISTER_SYSTEM_ABILITY_BY_ID(ConcurrentTaskServiceAbility, CONCURRENT_TASK_SERVICE_ID, true); + +void ConcurrentTaskServiceAbility::OnStart() +{ + TaskController::GetInstance().Init(); + if (!service_) { + try { + service_ = new ConcurrentTaskService(); + } catch (const std::bad_alloc& e) { + CONCUR_LOGE("ConcurrentTaskServiceAbility:: new ConcurentTaskService failed."); + } + } + if (!Publish(service_)) { + CONCUR_LOGE("ConcurrentTaskServiceAbility:: Register service failed."); + } + CONCUR_LOGI("ConcurrentTaskServiceAbility ::OnStart."); +} + +void ConcurrentTaskServiceAbility::OnStop() +{ + TaskController::GetInstance().Release(); + CONCUR_LOGI("ConcurrentTaskServiceAbility::OnStop!"); +} + +void ConcurrentTaskServiceAbility::OnAddSystemAbility(int32_t systemAbilityId, const std::string& deviceId) +{ + CONCUR_LOGI("ConcurrentTaskServiceAbility::Add"); +} + +void ConcurrentTaskServiceAbility::OnRemoveSystemAbility(int32_t systemAbilityId, const std::string& deviceId) +{ + CONCUR_LOGI("ConcurrentTaskServiceAbility::Remove"); +} +} // namespace ConcurrentTask +} // namespace OHOS diff --git a/services/src/concurrent_task_service_stub.cpp b/services/src/concurrent_task_service_stub.cpp new file mode 100644 index 0000000000000000000000000000000000000000..24f6cfc7521f133b1d07c13bc6432e588883c18c --- /dev/null +++ b/services/src/concurrent_task_service_stub.cpp @@ -0,0 +1,150 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * 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 "concurrent_task_service_stub.h" +#include "concurrent_task_log.h" +#include "concurrent_task_errors.h" +#include "string_ex.h" +#include "ipc_skeleton.h" + +namespace OHOS { +namespace ConcurrentTask { +namespace { + bool IsValidToken(MessageParcel& data) + { + std::u16string descriptor = ConcurrentTaskServiceStub::GetDescriptor(); + std::u16string remoteDescriptor = data.ReadInterfaceToken(); + return descriptor == remoteDescriptor; + } +} + +ConcurrentTaskServiceStub::ConcurrentTaskServiceStub() +{ + Init(); +} + +ConcurrentTaskServiceStub::~ConcurrentTaskServiceStub() +{ + funcMap_.clear(); +} + +int32_t ConcurrentTaskServiceStub::ReportDataInner(MessageParcel& data, [[maybe_unused]] MessageParcel& reply) +{ + if (!IsValidToken(data)) { + return ERR_CONCURRENT_TASK_PARCEL_ERROR; + } + uint32_t type = 0; + if (!data.ReadUint32(type)) { + return ERR_CONCURRENT_TASK_PARCEL_ERROR; + } + + int64_t value = 0; + if (!data.ReadInt64(value)) { + return ERR_CONCURRENT_TASK_PARCEL_ERROR; + } + + std::string payload; + if (!data.ReadString(payload)) { + return ERR_CONCURRENT_TASK_PARCEL_ERROR; + } + if (payload.empty()) { + return ERR_OK; + } + ReportData(type, value, StringToJson(payload)); + return ERR_OK; +} + +int32_t ConcurrentTaskServiceStub::QueryIntervalInner(MessageParcel& data, [[maybe_unused]] MessageParcel& reply) +{ + if (!IsValidToken(data)) { + return ERR_CONCURRENT_TASK_PARCEL_ERROR; + } + int item; + if (!data.ReadInt32(item)) { + return ERR_CONCURRENT_TASK_PARCEL_ERROR; + } + IntervalReply queryRs; + queryRs.rtgId = -1; + queryRs.paramA = -1; + queryRs.paramB = -1; + queryRs.paramC = -1; + QueryInterval(item, queryRs); + if (!reply.WriteInt32(queryRs.rtgId)) { + return ERR_CONCURRENT_TASK_PARCEL_ERROR; + } + if (!reply.WriteInt32(queryRs.paramA)) { + return ERR_CONCURRENT_TASK_PARCEL_ERROR; + } + if (!reply.WriteInt32(queryRs.paramB)) { + return ERR_CONCURRENT_TASK_PARCEL_ERROR; + } + if (!reply.WriteInt32(queryRs.paramC)) { + return ERR_CONCURRENT_TASK_PARCEL_ERROR; + } + return ERR_OK; +} + +int32_t ConcurrentTaskServiceStub::OnRemoteRequest(uint32_t code, MessageParcel& data, + MessageParcel& reply, MessageOption& option) +{ + auto uid = IPCSkeleton::GetCallingUid(); + auto pid = IPCSkeleton::GetCallingPid(); + CONCUR_LOGD("ConcurrentTaskServiceStub::OnRemoteRequest, code = %{public}u, flags = %{public}d," + " uid = %{public}d pid = %{public}d", code, option.GetFlags(), uid, pid); + + auto itFunc = funcMap_.find(code); + if (itFunc != funcMap_.end()) { + auto requestFunc = itFunc->second; + if (requestFunc) { + return requestFunc(data, reply); + } + } + return IPCObjectStub::OnRemoteRequest(code, data, reply, option); +} + +Json::Value ConcurrentTaskServiceStub::StringToJson(const std::string& payload) +{ + bool res; + Json::CharReaderBuilder readerBuilder; + JSONCPP_STRING errs; + std::unique_ptr const jsonReader(readerBuilder.newCharReader()); + Json::Value root; + if (!IsAsciiString(payload)) { + CONCUR_LOGE("Payload is not ascii string"); + return root; + } + try { + res = jsonReader->parse(payload.c_str(), payload.c_str() + payload.length(), &root, &errs); + } catch (...) { + CONCUR_LOGE("Unexpected json parse"); + return root; + } + if (!res || !errs.empty()) { + CONCUR_LOGE("ConcurentTaskServiceStub::payload = %{public}s Incorrect JSON format ", payload.c_str()); + } + return root; +} + +void ConcurrentTaskServiceStub::Init() +{ + funcMap_ = { + { REPORT_DATA, + [this](auto& data, auto& reply) {return ReportDataInner(data, reply); } }, + { QUERY_INTERVAL, + [this](auto& data, auto& reply) {return QueryIntervalInner(data, reply); } }, + }; +} +} // namespace ResourceSchedule +} // namespace OHOS diff --git a/services/src/qos_interface.cpp b/services/src/qos_interface.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e5d2fb6020ed1395e4c867fac763949a60f0379b --- /dev/null +++ b/services/src/qos_interface.cpp @@ -0,0 +1,324 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * 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 GNU_SOURCE +#define GNU_SOURCE +#endif +#include +#include +#include +#include +#include + +#include "../include/qos_interface.h" + +constexpr unsigned int AF_QOS_ALL = 0x0003; +constexpr unsigned int AF_QOS_DELEGATED = 0x0001; + +static int TrivalOpenRtgNode(void) +{ + char fileName[] = "/proc/self/sched_rtg_ctrl"; + int fd = open(fileName, O_RDWR); +#ifdef QOS_DEBUG + if (fd < 0) { + printf("task %d belong to user %d open rtg node failed\n", getpid(), getuid()); + } +#endif + return fd; +} + +static int TrivalOpenAuthCtrlNode(void) +{ + char fileName[] = "/dev/auth_ctrl"; + int fd = open(fileName, O_RDWR); +#ifdef QOS_DEBUG + if (fd < 0) { + printf("task %d belong to user %d open auth node failed\n", getpid(), getuid()); + } +#endif + return fd; +} + +static int TrivalOpenQosCtrlNode(void) +{ + char fileName[] = "/proc/thread-self/sched_qos_ctrl"; + int fd = open(fileName, O_RDWR); +#ifdef QOS_DEBUG + if (fd < 0) { + printf("task %d belong to user %d open qos node failed\n", getpid(), getuid()); + } +#endif + return fd; +} + +int EnableRtg(bool flag) +{ + struct RtgEnableData enableData; + char configStr[] = "load_freq_switch:1;sched_cycle:1;frame_max_util:750"; + int ret; + + enableData.enable = flag; + enableData.len = sizeof(configStr); + enableData.data = configStr; + int fd = TrivalOpenRtgNode(); + if (fd < 0) { + return fd; + } + + ret = ioctl(fd, CMD_ID_SET_ENABLE, &enableData); + if (ret < 0) { + printf("set rtg config enable failed.\n"); + } + + close(fd); + + return 0; +}; + +int AuthEnable(unsigned int uid, unsigned int uaFlag, unsigned int status) +{ + struct AuthCtrlData data; + int fd; + int ret; + + fd = TrivalOpenAuthCtrlNode(); + if (fd < 0) { + return fd; + } + + data.uid = uid; + data.rtgUaFlag = uaFlag; + data.qosUaFlag = AF_QOS_ALL; + data.status = status; + data.type = static_cast(AuthManipulateType::AUTH_ENABLE); + + ret = ioctl(fd, BASIC_AUTH_CTRL_OPERATION, &data); +#ifdef QOS_DEBUG + if (ret < 0) { + printf("auth enable failed for uid %u with status %u\n", uid, status); + } +#endif + close(fd); + return ret; +} + +int AuthSwitch(unsigned int uid, unsigned int rtgFlag, unsigned int qosFlag, unsigned int status) +{ + struct AuthCtrlData data; + int fd; + int ret; + + fd = TrivalOpenAuthCtrlNode(); + if (fd < 0) { + return fd; + } + + data.uid = uid; + data.rtgUaFlag = rtgFlag; + data.qosUaFlag = qosFlag; + data.status = status; + data.type = static_cast(AuthManipulateType::AUTH_SWITCH); + + ret = ioctl(fd, BASIC_AUTH_CTRL_OPERATION, &data); +#ifdef QOS_DEBUG + if (ret < 0) { + printf("auth switch failed for uid %u with status %u\n", uid, status); + } +#endif + close(fd); + return ret; +} + +int AuthDelete(unsigned int uid) +{ + struct AuthCtrlData data; + int fd; + int ret; + + fd = TrivalOpenAuthCtrlNode(); + if (fd < 0) { + return fd; + } + + data.uid = uid; + data.type = static_cast(AuthManipulateType::AUTH_DELETE); + + ret = ioctl(fd, BASIC_AUTH_CTRL_OPERATION, &data); +#ifdef QOS_DEBUG + if (ret < 0) { + printf("auth delete failed for uid %u\n", uid); + } +#endif + close(fd); + return ret; +} + +int AuthPause(unsigned int uid) +{ + struct AuthCtrlData data; + int fd; + int ret; + + fd = TrivalOpenAuthCtrlNode(); + if (fd < 0) { + return fd; + } + + data.uid = uid; + data.type = static_cast(AuthManipulateType::AUTH_SWITCH); + data.rtgUaFlag = 0; + data.qosUaFlag = AF_QOS_DELEGATED; + data.status = static_cast(AuthStatus::AUTH_STATUS_BACKGROUND); + + ret = ioctl(fd, BASIC_AUTH_CTRL_OPERATION, &data); +#ifdef QOS_DEBUG + if (ret < 0) { + printf("auth pause failed for uid %u\n", uid); + } +#endif + close(fd); + return ret; +} + +int AuthGet(unsigned int uid, unsigned int *uaFlag, unsigned int *status) +{ + struct AuthCtrlData data; + int fd; + int ret; + + fd = TrivalOpenAuthCtrlNode(); + if (fd < 0) { + return fd; + } + + data.uid = uid; + data.type = static_cast(AuthManipulateType::AUTH_GET); + + ret = ioctl(fd, BASIC_AUTH_CTRL_OPERATION, &data); +#ifdef QOS_DEBUG + if (ret < 0) { + printf("auth get failed for uid %u\n", uid); + } +#endif + close(fd); + + *uaFlag = data.rtgUaFlag; + *status = data.status; + + return ret; +} + +int QosApply(unsigned int level) +{ + int tid = gettid(); + int ret; + + ret = QosApplyForOther(level, tid); + return ret; +} + +int QosApplyForOther(unsigned int level, int tid) +{ + struct QosCtrlData data; + int fd; + + int ret; + + fd = TrivalOpenQosCtrlNode(); + if (fd < 0) { + return fd; + } + + data.level = level; + data.type = static_cast(QosManipulateType::QOS_APPLY); + data.pid = tid; + + ret = ioctl(fd, QOS_CTRL_BASIC_OPERATION, &data); +#ifdef QOS_DEBUG + if (ret < 0) { + printf("qos apply failed for task %d\n", tid); + } +#endif + close(fd); + return ret; +} + +int QosLeave(void) +{ + struct QosCtrlData data; + int fd; + int ret; + + fd = TrivalOpenQosCtrlNode(); + if (fd < 0) { + return fd; + } + + data.type = static_cast(QosManipulateType::QOS_LEAVE); + data.pid = gettid(); + + ret = ioctl(fd, QOS_CTRL_BASIC_OPERATION, &data); +#ifdef QOS_DEBUG + if (ret < 0) { + printf("qos leave failed for task %d\n", getpid()); + } +#endif + close(fd); + return ret; +} + +int QosLeaveForOther(int tid) +{ + struct QosCtrlData data; + int fd; + int ret; + + fd = TrivalOpenQosCtrlNode(); + if (fd < 0) { + return fd; + } + + data.type = static_cast(QosManipulateType::QOS_LEAVE); + data.pid = tid; + + ret = ioctl(fd, QOS_CTRL_BASIC_OPERATION, &data); +#ifdef QOS_DEBUG + if (ret < 0) { + printf("qos leave failed for task %d\n", tid); + } +#endif + close(fd); + return ret; +} + +int QosPolicy(struct QosPolicyDatas *policyDatas) +{ + int fd; + int ret; + + fd = TrivalOpenQosCtrlNode(); + if (fd < 0) { + return fd; + } + + ret = ioctl(fd, QOS_CTRL_POLICY_OPERATION, policyDatas); +#ifdef QOS_DEBUG + if (ret < 0) { + printf("set qos policy failed for task %d\n", getpid()); + } +#endif + close(fd); + return ret; +} diff --git a/services/src/qos_manager.cpp b/services/src/qos_manager.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e9fe5ae60d5903bf236185e1a63ff622c600fe93 --- /dev/null +++ b/services/src/qos_manager.cpp @@ -0,0 +1,109 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * 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 "qos_manager.h" +#include +#include "concurrent_task_log.h" + +static struct QosPolicyDatas g_defaultQosPolicy = { + .policyType = static_cast(QosPolicyType::QOS_POLICY_DEFAULT), + .policyFlag = QOS_FLAG_ALL, + .policys = { + {0, 0, 0, 1024, 0}, + {0, 0, 0, 1024, 0}, + {0, 0, 0, 1024, 0}, + {0, 0, 0, 1024, 0}, + {0, 0, 0, 1024, 0}, + {0, 0, 0, 1024, 0}, + {0, 0, 0, 1024, 0}, + } +}; + +static struct QosPolicyDatas g_foregroundQosPolicy = { + .policyType = static_cast(QosPolicyType::QOS_POLICY_FRONT), + .policyFlag = QOS_FLAG_ALL, + .policys = { + {0, 0, 0, 1024, 0}, + {10, 10, 0, 200, 0}, + {5, 5, 0, 250, 0}, + {0, 0, 0, 1024, 0}, + {-5, -5, 300, 1024, 0}, + {-10, -10, 500, 1024, 0}, + {-10, -10, 500, 1024, 2}, + } +}; + +static struct QosPolicyDatas g_backgroundQosPolicy = { + .policyType = static_cast(QosPolicyType::QOS_POLICY_BACK), + .policyFlag = QOS_FLAG_ALL & ~QOS_FLAG_RT, + .policys = { + {0, 0, 0, 1024, 0}, + {15, 15, 0, 150, 0}, + {10, 10, 0, 200, 0}, + {5, 5, 0, 250, 0}, + {0, 0, 0, 300, 0}, + {-5, -5, 0, 350, 0}, + {-5, -5, 0, 350, 3}, + } +}; + +static struct QosPolicyDatas g_systemServerQosPolicy = { + .policyType = static_cast(QosPolicyType::QOS_POLICY_SYSTEM_SERVER), + .policyFlag = QOS_FLAG_ALL, + .policys = { + {0, 0, 0, 1024, 0}, + {10, 10, 0, 200, 0}, + {5, 5, 0, 250, 0}, + {0, 0, 0, 1024, 0}, + {-5, -5, 300, 1024, 0}, + {-10, -10, 500, 1024, 0}, + {-10, -10, 500, 1024, 2}, + } +}; + +namespace OHOS { +namespace ConcurrentTask { +int QosManager::SetQosPolicy(struct QosPolicyDatas *policyDatas) +{ + return QosPolicy(policyDatas); +} + +void QosManager::Init() +{ + int ret; + + ret = SetQosPolicy(&g_defaultQosPolicy); + if (ret) { + CONCUR_LOGE("%{public}d set g_defaultQosPolicy failed", getuid()); + } + + ret = SetQosPolicy(&g_foregroundQosPolicy); + if (ret) { + CONCUR_LOGE("%{public}d set g_foregroundQosPolicy failed", getuid()); + } + + ret = SetQosPolicy(&g_backgroundQosPolicy); + if (ret) { + CONCUR_LOGE("%{public}d set g_backgroundQosPolicy failed", getuid()); + } + + ret = SetQosPolicy(&g_systemServerQosPolicy); + if (ret) { + CONCUR_LOGE("%{public}d set g_systemServerQosPolicy failed", getuid()); + } + CONCUR_LOGI("set qos policy finish"); +} +} +} \ No newline at end of file diff --git a/test/BUILD.gn b/test/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..225b1323986d037a6cae18735390df7c15872fda --- /dev/null +++ b/test/BUILD.gn @@ -0,0 +1,291 @@ +# Copyright (C) 2022 Huawei Device Co., Ltd. +# 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. + +import("//build/ohos.gni") +import("//build/ohos_var.gni") +import("//build/test.gni") + +module_output_path = "qosmanagertest/" + +gtest_public_deps = [ "//third_party/googletest:gtest_main" ] + +config("test_config") { + include_dirs = [ + "../include/", + "../frameworks/concurrent_task_client/include", + "../interfaces/inner_api/", + "../services/include/", + "//third_party/jsoncpp/include/", + "//foundation/resourceschedule/frame_aware_sched/common/include/", + ] +} + +config("ffrt_test_config") { + include_dirs = [ + "//foundation/resourceschedule/resourceschedule_qos_manager/interfaces/inner_api/", + "//foundation/resourceschedule/resourceschedule_qos_manager/include", + ] + + cflags = [ + "-Wno-unused-variable", + "-Wno-unused-function", + ] + + defines = [ + "QOS_INTERVAL", + "QOS_DISPATCH", + "QOS_RTG", + "QOS_MULTI_RTG", + "QOS_RTG_RT", + "QOS_CHECKPOINT", + "QOS_SUBMIT", + "QOS_DEMO", + "ATRACE_MODE=0", # 0 is off, 5 is bytrace, 6 is pmu + "ATRACE_LEVEL=0", + "ATRACE_LOGI_ENABLE=0", + "ATRACE_LOGD_ENABLE=0", + "ATRACE_SCOPE_LOG_ENABLE=0", + "ATRACE_DURATION_LOG_ENABLE=0", + "FFRT_RELEASE", + "DISABLE_MONITOR" + ] + if (use_musl) { + defines += [ "TDD_MUSL" ] + } +} + +ohos_unittest("concurrent_svc_intf_test") { + module_out_path = module_output_path + + configs = [ + ":test_config", + ] + + sources = [ + "unittest/phone/concurrent_svc_intf_test.cpp", + ] + deps = [ + "../frameworks/concurrent_task_client:concurrent_task_client", + ] + external_deps = [ + "c_utils:utils", + "eventhandler:libeventhandler", + "safwk:system_ability_fwk", + "samgr:samgr_proxy", + "ipc:ipc_single", + "hiviewdfx_hilog_native:libhilog", + ] + + if (is_standard_system) { + public_deps = gtest_public_deps + } + + subsystem_name = "resourceschedule" + part_name = "resourceschedule_qos_manager" +} + +ohos_unittest("concurrent_task_client_test") { + module_out_path = module_output_path + + configs = [ + ":ffrt_test_config", + ":test_config", + ] + + sources = [ + "unittest/phone/concurrent_task_client_test.cpp", + ] + deps = [ + "../frameworks/concurrent_task_client:concurrent_task_client", + ] + + if (is_standard_system) { + public_deps = gtest_public_deps + } + + external_deps = [ + "ipc:ipc_single", + "hiviewdfx_hilog_native:libhilog", + ] + + subsystem_name = "resourceschedule" + part_name = "resourceschedule_qos_manager" +} + +ohos_unittest("concurrent_task_controller_test") { + module_out_path = module_output_path + + configs = [ + ":ffrt_test_config", + ":test_config", + ] + + sources = [ + "unittest/phone/concurrent_task_controller_test.cpp", + ] + deps = [ + "../services:concurrentsvc", + "../frameworks/concurrent_task_client:concurrent_task_client", + "//third_party/jsoncpp:jsoncpp", + ] + + external_deps = [ + "c_utils:utils", + "hiviewdfx_hilog_native:libhilog", + "safwk:system_ability_fwk", + "samgr:samgr_proxy", + ] + + if (is_standard_system) { + public_deps = gtest_public_deps + } + + subsystem_name = "resourceschedule" + part_name = "resourceschedule_qos_manager" +} + +ohos_unittest("concurrent_task_service_ability_test") { + module_out_path = module_output_path + + configs = [ + ":ffrt_test_config", + ":test_config", + ] + + sources = [ + "unittest/phone/concurrent_task_service_ability_test.cpp", + ] + deps = [ + "../services:concurrentsvc", + "../frameworks/concurrent_task_client:concurrent_task_client", + ] + external_deps = [ + "c_utils:utils", + "hiviewdfx_hilog_native:libhilog", + "safwk:system_ability_fwk", + "samgr:samgr_proxy", + ] + + if (is_standard_system) { + public_deps = gtest_public_deps + } + + subsystem_name = "resourceschedule" + part_name = "resourceschedule_qos_manager" +} + +ohos_unittest("qos_interface_test") { + module_out_path = module_output_path + + configs = [ + ":ffrt_test_config", + ":test_config", + ] + + sources = [ + "unittest/phone/qos_interface_test.cpp", + ] + deps = [ + "../services:concurrentsvc", + "../frameworks/concurrent_task_client:concurrent_task_client", + ] + external_deps = [ + "c_utils:utils", + "safwk:system_ability_fwk", + "samgr:samgr_proxy", + "hiviewdfx_hilog_native:libhilog", + ] + + if (is_standard_system) { + public_deps = gtest_public_deps + } + + subsystem_name = "resourceschedule" + part_name = "resourceschedule_qos_manager" +} + +ohos_unittest("qos_manager_test") { + module_out_path = module_output_path + + configs = [ + ":ffrt_test_config", + ":test_config", + ] + + sources = [ + "unittest/phone/qos_manager_test.cpp", + ] + deps = [ + "../services:concurrentsvc", + "../frameworks/concurrent_task_client:concurrent_task_client", + ] + external_deps = [ + "c_utils:utils", + "safwk:system_ability_fwk", + "samgr:samgr_proxy", + "hiviewdfx_hilog_native:libhilog", + ] + + if (is_standard_system) { + public_deps = gtest_public_deps + } + + subsystem_name = "resourceschedule" + part_name = "resourceschedule_qos_manager" +} + +ohos_unittest("concurrent_task_service_test") { + module_out_path = module_output_path + + configs = [ + ":ffrt_test_config", + ":test_config", + ] + + sources = [ + "unittest/phone/concurrent_task_service_test.cpp", + ] + deps = [ + "../services:concurrentsvc", + "../frameworks/concurrent_task_client:concurrent_task_client", + ] + external_deps = [ + "c_utils:utils", + "safwk:system_ability_fwk", + "samgr:samgr_proxy", + "hiviewdfx_hilog_native:libhilog", + ] + + if (is_standard_system) { + public_deps = gtest_public_deps + } + + subsystem_name = "resourceschedule" + part_name = "resourceschedule_qos_manager" +} + +group("concurrent_unittest") { + testonly = true + deps = [] + if (!is_asan) { + deps += [ + ":qos_interface_test", + ":qos_manager_test", + ":concurrent_svc_intf_test", + ":concurrent_task_client_test", + ":concurrent_task_controller_test", + ":concurrent_task_service_ability_test", + ":concurrent_task_service_test", + ] + } +} diff --git a/test/unittest/phone/concurrent_svc_intf_test.cpp b/test/unittest/phone/concurrent_svc_intf_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d63e605f66cc463e574016e2a78996d6213d964f --- /dev/null +++ b/test/unittest/phone/concurrent_svc_intf_test.cpp @@ -0,0 +1,126 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * 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 +#include +#include +#include +#include "gtest/gtest.h" +#include "concurrent_task_log.h" +#include "concurrent_task_client.h" + +namespace OHOS { +namespace ConcurrentTask { +using namespace testing; +using namespace testing::ext; +using namespace OHOS::ConcurrentTask; +using namespace std; + +constexpr int HUGE_ITEM = 1000000; + +class ConcurrentSvcIntfTest : public testing::Test { +public: + static void SetUpTestCase(); + static void TearDownTestCase(); + void SetUp(); + void TearDown(); +}; + +static int QueryInterval(int item) +{ + IntervalReply rs; + rs.rtgId = -1; + cout << "start to query renderService" < payload; + payload["uid"] = "3587"; + payload["type"] = "appStart"; + ConcurrentTaskClient::GetInstance().ReportData(resType, value, payload); +} + +/** + * @tc.name: PushTaskTest + * @tc.desc: Test whether the PushTask interface are normal. + * @tc.type: FUNC + */ +HWTEST_F(ConcurrentTaskClientTest, QueryIntervalTest, TestSize.Level1) +{ + int queryItem = 3; + IntervalReply queryRs = {87, 657, 357, 214}; + ConcurrentTaskClient::GetInstance().QueryInterval(queryItem, queryRs); +} + +/** + * @tc.name: PushTaskTest + * @tc.desc: Test whether the PushTask interface are normal. + * @tc.type: FUNC + */ +HWTEST_F(ConcurrentTaskClientTest, StopRemoteObjectTest, TestSize.Level1) +{ + ConcurrentTaskClient::GetInstance().StopRemoteObject(); +} +} +} \ No newline at end of file diff --git a/test/unittest/phone/concurrent_task_controller_test.cpp b/test/unittest/phone/concurrent_task_controller_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..fda34038f1dda02ccbb88b9e37e25abf424c325c --- /dev/null +++ b/test/unittest/phone/concurrent_task_controller_test.cpp @@ -0,0 +1,339 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * 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 +#include +#include +#include +#include "gtest/gtest.h" +#define private public +#include "concurrent_task_controller.h" +#include "rtg_interface.h" +#include "ipc_skeleton.h" +#include "concurrent_task_log.h" +#undef private + +namespace OHOS { +namespace FFRT_TEST { +using namespace testing; +using namespace testing::ext; +using namespace OHOS::ConcurrentTask; +using namespace std; + + +class ConcurrentTaskControllerTest : public testing::Test { +public: + static void SetUpTestCase(); + static void TearDownTestCase(); + void SetUp(); + void TearDown(); +}; + +void ConcurrentTaskControllerTest::SetUpTestCase() +{ +} + +void ConcurrentTaskControllerTest::TearDownTestCase() +{ +} + +void ConcurrentTaskControllerTest::SetUp() +{ +} + +void ConcurrentTaskControllerTest::TearDown() +{ +} + +/** + * @tc.name: ReportDataTest + * @tc.desc: Test whether the ReportDataTest interface are normal. + * @tc.type: FUNC + */ +HWTEST_F(ConcurrentTaskControllerTest, ReportDataTest, TestSize.Level1) +{ + uint32_t resType = 0; + int64_t value = 0; + const Json::Value payload; + TaskController repData; + repData.ReportData(resType, value, payload); +} + +/** + * @tc.name: PushTaskTest + * @tc.desc: Test whether the PushTask interface are normal. + * @tc.type: FUNC + */ +HWTEST_F(ConcurrentTaskControllerTest, QueryIntervalTest, TestSize.Level1) +{ + TaskController queInt; + int queryItem = QUERY_UI; + IntervalReply queryRs = {87, 657, 357, 214}; + queInt.QueryInterval(queryItem, queryRs); + queryItem = QUERY_RENDER; + queInt.QueryInterval(queryItem, queryRs); + queryItem = QUERY_RENDER_SERVICE; + queInt.QueryInterval(queryItem, queryRs); + queryItem = QUERY_COMPOSER; + queInt.QueryInterval(queryItem, queryRs); + queryItem = QURRY_TYPE_MAX; + queInt.QueryInterval(queryItem, queryRs); +} + +/** + * @tc.name: PushTaskTest + * @tc.desc: Test whether the PushTask interface are normal. + * @tc.type: FUNC + */ +HWTEST_F(ConcurrentTaskControllerTest, InitTest, TestSize.Level1) +{ + TaskController::GetInstance().Init(); +} + +/** + * @tc.name: PushTaskTest + * @tc.desc: Test whether the PushTask interface are normal. + * @tc.type: FUNC + */ +HWTEST_F(ConcurrentTaskControllerTest, AuthHwcTest, TestSize.Level1) +{ + TaskController::GetInstance().SetHwcAuth(true); + TaskController::GetInstance().SetHwcAuth(false); + TaskController::GetInstance().SetHwcAuth(true); +} + +/** + * @tc.name: PushTaskTest + * @tc.desc: Test whether the PushTask interface are normal. + * @tc.type: FUNC + */ +HWTEST_F(ConcurrentTaskControllerTest, CheckUidTest, TestSize.Level1) +{ + int uid = SYSTEM_UID; + bool ret = TaskController::GetInstance().CheckUid(uid); + EXPECT_EQ(ret, true); + uid = 0; + ret = TaskController::GetInstance().CheckUid(uid); + EXPECT_EQ(ret, true); + uid = 100; + ret = TaskController::GetInstance().CheckUid(uid); + EXPECT_EQ(ret, false); +} + +/** + * @tc.name: PushTaskTest + * @tc.desc: Test whether the PushTask interface are normal. + * @tc.type: FUNC + */ +HWTEST_F(ConcurrentTaskControllerTest, TypeMapInitTest, TestSize.Level1) +{ + TaskController::GetInstance().TypeMapInit(); +} + +/** + * @tc.name: PushTaskTest + * @tc.desc: Test whether the PushTask interface are normal. + * @tc.type: FUNC + */ +HWTEST_F(ConcurrentTaskControllerTest, TryCreateRsGroupTest, TestSize.Level1) +{ + TaskController::GetInstance().rtgEnabled_ = false; + TaskController::GetInstance().TryCreateRsGroup(); + TaskController::GetInstance().rtgEnabled_ = true; + TaskController::GetInstance().TryCreateRsGroup(); +} + +/** + * @tc.name: PushTaskTest + * @tc.desc: Test whether the PushTask interface are normal. + * @tc.type: FUNC + */ +HWTEST_F(ConcurrentTaskControllerTest, QueryRenderServiceTest, TestSize.Level1) +{ + int uid = SYSTEM_UID; + IntervalReply queryRs = {87, 657, 357, 214}; + TaskController::GetInstance().QueryRenderService(uid, queryRs); + int flag = TaskController::GetInstance().renderServiceGrpId_; + TaskController::GetInstance().renderServiceGrpId_ = 1; + TaskController::GetInstance().QueryRenderService(uid, queryRs); + TaskController::GetInstance().renderServiceGrpId_ = -1; + TaskController::GetInstance().QueryRenderService(uid, queryRs); + TaskController::GetInstance().renderServiceGrpId_ = flag; + TaskController::GetInstance().QueryRenderService(uid, queryRs); +} + +/** + * @tc.name: PushTaskTest + * @tc.desc: Test whether the PushTask interface are normal. + * @tc.type: FUNC + */ +HWTEST_F(ConcurrentTaskControllerTest, GetRequestTypeTest, TestSize.Level1) +{ + std::string strRequstType = "test"; + int ret = TaskController::GetInstance().GetRequestType(strRequstType); + EXPECT_EQ(ret, MSG_TYPE_MAX); + TaskController::GetInstance().msgType_["test"] = 8; + ret = TaskController::GetInstance().GetRequestType(strRequstType); + EXPECT_EQ(ret, 8); + TaskController::GetInstance().msgType_.erase("test"); + ret = TaskController::GetInstance().GetRequestType(strRequstType); + EXPECT_EQ(ret, MSG_TYPE_MAX); +} + +/** + * @tc.name: PushTaskTest + * @tc.desc: Test whether the PushTask interface are normal. + * @tc.type: FUNC + */ +HWTEST_F(ConcurrentTaskControllerTest, NewForegroundTest, TestSize.Level1) +{ + TaskController fore; + int uid = 0; + fore.NewForeground(uid); + fore.NewBackground(uid); + fore.NewAppStart(uid); + fore.NewForeground(uid); + fore.NewBackground(uid); + fore.AppKilled(uid); + uid = 574; + fore.foregroundApp_.push_back(ForegroundAppRecord(574)); + fore.foregroundApp_.push_back(ForegroundAppRecord(1)); + fore.foregroundApp_.push_back(ForegroundAppRecord(3)); + auto iter = fore.foregroundApp_.begin(); + EXPECT_EQ(iter->GetUid(), uid); + fore.NewForeground(uid); + fore.NewBackground(uid); + fore.NewAppStart(uid); + fore.NewForeground(uid); + fore.NewBackground(uid); + fore.AppKilled(uid); +} + +/** + * @tc.name: PushTaskTest + * @tc.desc: Test whether the PushTask interface are normal. + * @tc.type: FUNC + */ +HWTEST_F(ConcurrentTaskControllerTest, PrintInfoTest, TestSize.Level1) +{ + TaskController print; + print.foregroundApp_.push_back(ForegroundAppRecord(1)); + print.foregroundApp_.push_back(ForegroundAppRecord(3)); + print.foregroundApp_.push_back(ForegroundAppRecord(5)); + auto iter = print.foregroundApp_.begin(); + EXPECT_NE(iter, print.foregroundApp_.end()); + print.PrintInfo(); +} + +/** + * @tc.name: PushTaskTest + * @tc.desc: Test whether the PushTask interface are normal. + * @tc.type: FUNC + */ +HWTEST_F(ConcurrentTaskControllerTest, AddKeyThreadTest, TestSize.Level1) +{ + int uid = 758; + int tid = 45; + int tid2 = 46; + int tid3 = 47; + int tid4 = 48; + int tid5 = 49; + int prio = PRIO_NORMAL; + ForegroundAppRecord foregroundapprecord = ForegroundAppRecord(uid); + foregroundapprecord.AddKeyThread(tid, prio); + foregroundapprecord.keyThreads_.insert(tid); + foregroundapprecord.AddKeyThread(tid, prio); + foregroundapprecord.grpId_ = -1; + foregroundapprecord.AddKeyThread(tid, prio); + foregroundapprecord.grpId_ = 1; + foregroundapprecord.AddKeyThread(tid, prio); + foregroundapprecord.keyThreads_.insert(tid2); + foregroundapprecord.keyThreads_.insert(tid3); + foregroundapprecord.keyThreads_.insert(tid4); + foregroundapprecord.keyThreads_.insert(tid5); + foregroundapprecord.keyThreads_.insert(tid5); + foregroundapprecord.AddKeyThread(tid, prio); + prio = RPIO_IN; + foregroundapprecord.keyThreads_.insert(tid); + prio = PRIO_RT; + foregroundapprecord.keyThreads_.insert(tid); + foregroundapprecord.AddKeyThread(tid, prio); + foregroundapprecord.keyThreads_.clear(); +} + +/** + * @tc.name: PushTaskTest + * @tc.desc: Test whether the PushTask interface are normal. + * @tc.type: FUNC + */ +HWTEST_F(ConcurrentTaskControllerTest, BeginSceneTest, TestSize.Level1) +{ + int uid = 758; + ForegroundAppRecord foregroundapprecord = ForegroundAppRecord(uid); + foregroundapprecord.BeginScene(); + foregroundapprecord.EndScene(); + foregroundapprecord.grpId_ = -1; + foregroundapprecord.BeginScene(); + foregroundapprecord.EndScene(); + foregroundapprecord.grpId_ = 1; + foregroundapprecord.BeginScene(); + foregroundapprecord.EndScene(); +} + +/** + * @tc.name: PushTaskTest + * @tc.desc: Test whether the PushTask interface are normal. + * @tc.type: FUNC + */ +HWTEST_F(ConcurrentTaskControllerTest, IsValidTest, TestSize.Level1) +{ + int uid = 758; + ForegroundAppRecord foregroundapprecord = ForegroundAppRecord(uid); + EXPECT_EQ(foregroundapprecord.GetUid(), foregroundapprecord.uid_); + EXPECT_EQ(foregroundapprecord.GetGrpId(), foregroundapprecord.grpId_); + foregroundapprecord.uid_ = -1; + foregroundapprecord.grpId_ = 1; + EXPECT_EQ(foregroundapprecord.IsValid(), false); + foregroundapprecord.uid_ = -1; + foregroundapprecord.grpId_ = -1; + EXPECT_EQ(foregroundapprecord.IsValid(), false); + foregroundapprecord.uid_ = 1; + foregroundapprecord.grpId_ = -1; + EXPECT_EQ(foregroundapprecord.IsValid(), false); + foregroundapprecord.uid_ = 1; + foregroundapprecord.grpId_ = 1; + EXPECT_EQ(foregroundapprecord.IsValid(), true); +} + +/** + * @tc.name: PushTaskTest + * @tc.desc: Test whether the PushTask interface are normal. + * @tc.type: FUNC + */ +HWTEST_F(ConcurrentTaskControllerTest, PrintKeyThreadsTest, TestSize.Level1) +{ + int uid = 758; + ForegroundAppRecord foregroundapprecord = ForegroundAppRecord(uid); + foregroundapprecord.keyThreads_.insert(1); + foregroundapprecord.keyThreads_.insert(3); + foregroundapprecord.keyThreads_.insert(5); + foregroundapprecord.keyThreads_.insert(7); + foregroundapprecord.keyThreads_.insert(9); + auto iter = foregroundapprecord.keyThreads_.begin(); + EXPECT_NE(iter, foregroundapprecord.keyThreads_.end()); + foregroundapprecord.PrintKeyThreads(); +} +} +} \ No newline at end of file diff --git a/test/unittest/phone/concurrent_task_service_ability_test.cpp b/test/unittest/phone/concurrent_task_service_ability_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e1047f5d35ed4185d8fb68ed6bfd9213a5bd09bd --- /dev/null +++ b/test/unittest/phone/concurrent_task_service_ability_test.cpp @@ -0,0 +1,85 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * 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 "gtest/gtest.h" +#define private public +#include "concurrent_task_service_ability.h" +#undef private + +namespace OHOS { +namespace FFRT_TEST { +using namespace testing; +using namespace testing::ext; +using namespace OHOS::ConcurrentTask; + + +class ConcurrentTaskServiceAbilityTest : public testing::Test { +public: + static void SetUpTestCase(); + static void TearDownTestCase(); + void SetUp(); + void TearDown(); +}; + +void ConcurrentTaskServiceAbilityTest::SetUpTestCase() +{ +} + +void ConcurrentTaskServiceAbilityTest::TearDownTestCase() +{ +} + +void ConcurrentTaskServiceAbilityTest::SetUp() +{ +} + +void ConcurrentTaskServiceAbilityTest::TearDown() +{ +} + +/** + * @tc.name: PushTaskTest + * @tc.desc: Test whether the PushTask interface are normal. + * @tc.type: FUNC + */ +HWTEST_F(ConcurrentTaskServiceAbilityTest, OnStartTest, TestSize.Level1) +{ + int32_t sysAbilityId = 8745; + bool runOnCreate = true; + ConcurrentTaskServiceAbility concurrenttaskserviceability = ConcurrentTaskServiceAbility(sysAbilityId, runOnCreate); + concurrenttaskserviceability.OnStart(); + concurrenttaskserviceability.OnStart(); + concurrenttaskserviceability.OnStop(); +} + +/** + * @tc.name: PushTaskTest + * @tc.desc: Test whether the PushTask interface are normal. + * @tc.type: FUNC + */ +HWTEST_F(ConcurrentTaskServiceAbilityTest, OnAddSystemAbilityTest, TestSize.Level1) +{ + int32_t sysAbilityId = 8745; + bool runOnCreate = true; + int32_t systemAbilityId = 6587; + std::string deviceId = "test"; + ConcurrentTaskServiceAbility concurrenttaskserviceability = ConcurrentTaskServiceAbility(sysAbilityId, runOnCreate); + concurrenttaskserviceability.OnStart(); + concurrenttaskserviceability.OnAddSystemAbility(systemAbilityId, deviceId); + concurrenttaskserviceability.OnRemoveSystemAbility(systemAbilityId, deviceId); + concurrenttaskserviceability.OnStop(); +} +} +} \ No newline at end of file diff --git a/test/unittest/phone/concurrent_task_service_test.cpp b/test/unittest/phone/concurrent_task_service_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..31a8dee7fd6124792cdb20d9473c846955d56694 --- /dev/null +++ b/test/unittest/phone/concurrent_task_service_test.cpp @@ -0,0 +1,64 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * 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 "gtest/gtest.h" +#include "concurrent_task_service.h" +#include "concurrent_task_controller.h" + +namespace OHOS { +namespace FFRT_TEST { +using namespace testing; +using namespace testing::ext; +using namespace OHOS::ConcurrentTask; + + +class ConcurrentTaskServiceTest : public testing::Test { +public: + static void SetUpTestCase(); + static void TearDownTestCase(); + void SetUp(); + void TearDown(); +}; + +void ConcurrentTaskServiceTest::SetUpTestCase() +{ +} + +void ConcurrentTaskServiceTest::TearDownTestCase() +{ +} + +void ConcurrentTaskServiceTest::SetUp() +{ +} + +void ConcurrentTaskServiceTest::TearDown() +{ +} + +/** + * @tc.name: QueryIntervalTest + * @tc.desc: Test whether the QueryInterval interface are normal. + * @tc.type: FUNC + */ +HWTEST_F(ConcurrentTaskServiceTest, QueryIntervalTest, TestSize.Level1) +{ + int queryItem = 0; + IntervalReply queryRs = {87, 657, 357, 214}; + ConcurrentTaskService queInt; + queInt.QueryInterval(queryItem, queryRs); +} +} +} \ No newline at end of file diff --git a/test/unittest/phone/qos_interface_test.cpp b/test/unittest/phone/qos_interface_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2aa840901c85cfa10c0bd977fabed53616f6bedd --- /dev/null +++ b/test/unittest/phone/qos_interface_test.cpp @@ -0,0 +1,250 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * 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 "gtest/gtest.h" + +#include "../include/qos_interface.h" + +namespace OHOS { +namespace FFRT_TEST { +using namespace testing; +using namespace testing::ext; +using namespace OHOS::FFRT_TEST; +using namespace std; + + +class QosInterfaceTest : public testing::Test { +public: + static void SetUpTestCase(); + static void TearDownTestCase(); + void SetUp(); + void TearDown(); +}; + +void QosInterfaceTest::SetUpTestCase() +{ +} + +void QosInterfaceTest::TearDownTestCase() +{ +} + +void QosInterfaceTest::SetUp() +{ +} + +void QosInterfaceTest::TearDown() +{ +} + +extern "C" { +/** + * @tc.name: EnableRtgTest + * @tc.desc: Test whether the OnRemoteRequest interface are normal. + * @tc.type: FUNC + */ +HWTEST_F(QosInterfaceTest, EnableRtgTest, TestSize.Level1) +{ + bool flag = true; + int ret = EnableRtg(flag); +#if TDD_MUSL + EXPECT_EQ(ret, 0); +#else + (void)ret; +#endif +} + +/** + * @tc.name: AuthEnableTest + * @tc.desc: Test whether the OnRemoteRequest interface are normal. + * @tc.type: FUNC + */ +HWTEST_F(QosInterfaceTest, AuthEnableTest, TestSize.Level1) +{ + unsigned int uid = 1; + unsigned int uaFlag = 1; + unsigned int status = 1; + int ret = AuthEnable(uid, uaFlag, status); +#if TDD_MUSL + EXPECT_EQ(ret, 0); +#else + (void)ret; +#endif +} + +/** + * @tc.name: AuthSwitchTest + * @tc.desc: Test whether the AuthSwitch interface are normal. + * @tc.type: FUNC + */ +HWTEST_F(QosInterfaceTest, AuthSwitchTest, TestSize.Level1) +{ + unsigned int uid = 1; + unsigned int rtgFlag = 1; + unsigned int qosFlag = 1; + unsigned int status = 1; + int ret = AuthSwitch(uid, rtgFlag, qosFlag, status); +#if TDD_MUSL + EXPECT_EQ(ret, 0); +#else + (void)ret; +#endif +} + +/** + * @tc.name: AuthDeleteTest + * @tc.desc: Test whether the AuthDelete interface are normal. + * @tc.type: FUNC + */ +HWTEST_F(QosInterfaceTest, AuthDeleteTest, TestSize.Level1) +{ + unsigned int uid = 1; + int ret = AuthDelete(uid); +#if TDD_MUSL + EXPECT_EQ(ret, 0); +#else + (void)ret; +#endif +} + +/** + * @tc.name: AuthPauseTest + * @tc.desc: Test whether the AuthPause interface are normal. + * @tc.type: FUNC + */ +HWTEST_F(QosInterfaceTest, AuthPauseTest, TestSize.Level1) +{ + unsigned int uid = 1; + int ret = -1; + ret = AuthPause(uid); +#if TDD_MUSL + EXPECT_EQ(ret, -1); +#else + (void)ret; +#endif +} + +/** + * @tc.name: QosApplyTest + * @tc.desc: Test whether the QosApply interface are normal. + * @tc.type: FUNC + */ +HWTEST_F(QosInterfaceTest, QosApplyTest, TestSize.Level1) +{ + unsigned int level = 1; + int ret = -1; + ret = QosApply(level); +#if TDD_MUSL + EXPECT_EQ(ret, -1); +#else + (void)ret; +#endif +} + +/** + * @tc.name: AuthGetTest + * @tc.desc: Test whether the AuthGet interface are normal. + * @tc.type: FUNC + */ +HWTEST_F(QosInterfaceTest, AuthGetTest, TestSize.Level1) +{ + unsigned int uid = 1000; + unsigned int uaFlag1 = 0; + unsigned int *uaFlag = &uaFlag1; + unsigned int status1 = 0; + unsigned int *status = &status1; + int ret = AuthGet(uid, uaFlag, status); +#if TDD_MUSL + EXPECT_GE(ret, 0); +#endif + uid = -1; + ret = AuthGet(uid, uaFlag, status); +#if TDD_MUSL + EXPECT_EQ(ret, -1); +#else + (void)ret; +#endif +} + +/** + * @tc.name: QosApplyForOtherTest + * @tc.desc: Test whether the QosApplyForOther interface are normal. + * @tc.type: FUNC + */ +HWTEST_F(QosInterfaceTest, QosApplyForOtherTest, TestSize.Level1) +{ + unsigned int level = 1; + int tid = 1; + int ret = -1; + ret = QosApplyForOther(level, tid); +#if TDD_MUSL + EXPECT_EQ(ret, 0); +#else + (void)ret; +#endif +} + +/** + * @tc.name: QosLeaveTest + * @tc.desc: Test whether the QosLeave interface are normal. + * @tc.type: FUNC + */ +HWTEST_F(QosInterfaceTest, QosLeaveTest, TestSize.Level1) +{ + int ret = -1; + ret = QosLeave(); +#if TDD_MUSL + EXPECT_EQ(ret, -1); +#else + (void)ret; +#endif +} + +/** + * @tc.name: QosLeaveForOtherTest + * @tc.desc: Test whether the QosLeaveForOther interface are normal. + * @tc.type: FUNC + */ +HWTEST_F(QosInterfaceTest, QosLeaveForOtherTest, TestSize.Level1) +{ + int ret = -1; + int tid = 1; + ret = QosLeaveForOther(tid); +#if TDD_MUSL + EXPECT_EQ(ret, 0); +#else + (void)ret; +#endif +} + +/** + * @tc.name: QosPolicyTest + * @tc.desc: Test whether the QosPolicy interface are normal. + * @tc.type: FUNC + */ +HWTEST_F(QosInterfaceTest, QosPolicyTest, TestSize.Level1) +{ + int ret = -1; + struct QosPolicyDatas *policyDatas = nullptr; + ret = QosPolicy(policyDatas); +#if TDD_MUSL + EXPECT_EQ(ret, -1); +#else + (void)ret; +#endif +} +} +} +} \ No newline at end of file diff --git a/test/unittest/phone/qos_manager_test.cpp b/test/unittest/phone/qos_manager_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7514ca7d31cf583dbdf4b89ade07fda4abc910e5 --- /dev/null +++ b/test/unittest/phone/qos_manager_test.cpp @@ -0,0 +1,63 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * 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 "gtest/gtest.h" +#include "qos_manager.h" + +namespace OHOS { +namespace FFRT_TEST { +using namespace testing; +using namespace testing::ext; +using namespace OHOS::FFRT_TEST; +using namespace ConcurrentTask; +using namespace std; + + +class QosManagerTest : public testing::Test { +public: + static void SetUpTestCase(); + static void TearDownTestCase(); + void SetUp(); + void TearDown(); +}; + +void QosManagerTest::SetUpTestCase() +{ +} + +void QosManagerTest::TearDownTestCase() +{ +} + +void QosManagerTest::SetUp() +{ +} + +void QosManagerTest::TearDown() +{ +} + +/** + * @tc.name: DecDepRefTest + * @tc.desc: Confirm the test binary can execute in root priv. + * @tc.type: FUNC + */ +HWTEST_F(QosManagerTest, TaskHandleTest, TestSize.Level1) +{ + QosManager qosManager; + qosManager.Init(); +} +} +} \ No newline at end of file