From 8288004b667b3c320cb3093c171bbcf2c76df895 Mon Sep 17 00:00:00 2001 From: ChenJie Date: Tue, 12 Apr 2022 23:30:00 +0800 Subject: [PATCH] add innerkits to list all apps' priority info Signed-off-by: ChenJie --- bundle.json | 14 ++- interface/innerkits/BUILD.gn | 50 ++++++++ interface/innerkits/include/bundle_priority.h | 37 ++++++ .../innerkits/include/bundle_priority_list.h | 51 ++++++++ interface/innerkits/include/i_mem_mgr.h | 38 ++++++ interface/innerkits/include/mem_mgr_client.h | 37 ++++++ interface/innerkits/include/mem_mgr_proxy.h | 33 +++++ .../innerkits/src/bundle_priority_list.cpp | 116 ++++++++++++++++++ interface/innerkits/src/mem_mgr_client.cpp | 66 ++++++++++ interface/innerkits/src/mem_mgr_proxy.cpp | 54 ++++++++ memmgr.gni | 2 + services/memmgrservice/BUILD.gn | 3 + .../memmgrservice/include/mem_mgr_service.h | 4 +- services/memmgrservice/include/mem_mgr_stub.h | 46 +++++++ .../memmgrservice/src/mem_mgr_service.cpp | 21 ++++ services/memmgrservice/src/mem_mgr_stub.cpp | 71 +++++++++++ test/BUILD.gn | 18 +++ test/unittest/phone/innerkits_test.cpp | 64 ++++++++++ 18 files changed, 723 insertions(+), 2 deletions(-) create mode 100644 interface/innerkits/BUILD.gn create mode 100644 interface/innerkits/include/bundle_priority.h create mode 100644 interface/innerkits/include/bundle_priority_list.h create mode 100644 interface/innerkits/include/i_mem_mgr.h create mode 100644 interface/innerkits/include/mem_mgr_client.h create mode 100644 interface/innerkits/include/mem_mgr_proxy.h create mode 100644 interface/innerkits/src/bundle_priority_list.cpp create mode 100644 interface/innerkits/src/mem_mgr_client.cpp create mode 100644 interface/innerkits/src/mem_mgr_proxy.cpp create mode 100644 services/memmgrservice/include/mem_mgr_stub.h create mode 100644 services/memmgrservice/src/mem_mgr_stub.cpp create mode 100644 test/unittest/phone/innerkits_test.cpp diff --git a/bundle.json b/bundle.json index 9300377..0c1df2d 100644 --- a/bundle.json +++ b/bundle.json @@ -40,7 +40,19 @@ "//foundation/resourceschedule/memmgr/services/memmgrservice:memmgrservice_init", "//foundation/resourceschedule/memmgr/profile:memmgr_config" ], - "inner_kits": [], + "inner_kits": [ + { + "name": "//foundation/resourceschedule/memmgr/interface/innerkits:memmgrclient", + "header": { + "header_files": [ + "mem_mgr_client.h", + "i_mem_mgr.h", + "mem_mgr_proxy.h" + ], + "header_base": "//foundation/resourceschedule/memmgr/interface/innerkits/include/" + } + } + ], "test": [ "//foundation/resourceschedule/memmgr/test:memmgr_unittest" ] diff --git a/interface/innerkits/BUILD.gn b/interface/innerkits/BUILD.gn new file mode 100644 index 0000000..83ca7dd --- /dev/null +++ b/interface/innerkits/BUILD.gn @@ -0,0 +1,50 @@ +# 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("//foundation/resourceschedule/memmgr/memmgr.gni") + +config("memmgr_client_config") { + visibility = [ ":*" ] + include_dirs = [ + "include", + "${memmgr_common_path}/include", + "//utils/system/safwk/native/include/", + ] +} + +ohos_shared_library("memmgrclient") { + install_enable = true + sources = [ + "src/bundle_priority_list.cpp", + "src/mem_mgr_client.cpp", + "src/mem_mgr_proxy.cpp", + ] + + configs = [ ":memmgr_client_config" ] + + deps = [ + "//foundation/distributedschedule/safwk/interfaces/innerkits/safwk:system_ability_fwk", + "//foundation/distributedschedule/samgr/interfaces/innerkits/samgr_proxy:samgr_proxy", + "//utils/native/base:utils", + ] + + external_deps = [ + "hiviewdfx_hilog_native:libhilog", + "ipc:ipc_core", + ] + + part_name = "memmgr" + subsystem_name = "resourceschedule" +} diff --git a/interface/innerkits/include/bundle_priority.h b/interface/innerkits/include/bundle_priority.h new file mode 100644 index 0000000..d997f6e --- /dev/null +++ b/interface/innerkits/include/bundle_priority.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 OHOS_MEMORY_MEMMGR_INNERKITS_BUNDLE_INFO_H +#define OHOS_MEMORY_MEMMGR_INNERKITS_BUNDLE_INFO_H + +#include + +#include "parcel.h" + +namespace OHOS { +namespace Memory { +class BundlePriority { +public: + BundlePriority(); + BundlePriority(int32_t uid, std::string name, int32_t priority, int32_t accountId) : uid_(uid), name_(name), + priority_(priority), accountId_(accountId) {}; + int32_t uid_ {0}; + std::string name_ {""}; + int32_t priority_ {0}; + int32_t accountId_ {0}; +}; +} // namespace Memory +} // namespace OHOS +#endif // OHOS_MEMORY_MEMMGR_INNERKITS_BUNDLE_INFO_H diff --git a/interface/innerkits/include/bundle_priority_list.h b/interface/innerkits/include/bundle_priority_list.h new file mode 100644 index 0000000..ddcc772 --- /dev/null +++ b/interface/innerkits/include/bundle_priority_list.h @@ -0,0 +1,51 @@ +/* + * 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 OHOS_MEMORY_MEMMGR_INNERKITS_BUNDLE_PRIORITY_LIST_H +#define OHOS_MEMORY_MEMMGR_INNERKITS_BUNDLE_PRIORITY_LIST_H + +#include + +#include "bundle_priority.h" +#include "parcel.h" + +namespace OHOS { +namespace Memory { +class BundlePriorityList : public Parcelable { +public: + /** + * @brief Marshals a purpose into a parcel. + * + * @param parcel Indicates the parcel object for marshalling. + * @return True if success, else false. + */ + bool Marshalling(Parcel &parcel) const; + static BundlePriorityList *Unmarshalling(Parcel &parcel); + + int32_t GetCount() const; + void SetCount(int32_t count); + const std::vector& GetList(); + int32_t Size() const; + void AddBundleInfo(BundlePriority &bundleInfo); + void Show() const; +private: + bool ReadFromParcel(Parcel &parcel); + + int32_t count_ {0}; + std::vector list_; +}; +} // namespace Memory +} // namespace OHOS +#endif // OHOS_MEMORY_MEMMGR_INNERKITS_BUNDLE_PRIORITY_LIST_H diff --git a/interface/innerkits/include/i_mem_mgr.h b/interface/innerkits/include/i_mem_mgr.h new file mode 100644 index 0000000..62d74de --- /dev/null +++ b/interface/innerkits/include/i_mem_mgr.h @@ -0,0 +1,38 @@ +/* + * 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 OHOS_MEMORY_MEMMGR_INTERFACES_INNERKITS_INCLUDE_MEM_MGR_INTERFACE_H +#define OHOS_MEMORY_MEMMGR_INTERFACES_INNERKITS_INCLUDE_MEM_MGR_INTERFACE_H + +#include +#include "iremote_broker.h" +#include "iremote_object.h" +#include "bundle_priority_list.h" + +namespace OHOS { +namespace Memory { +class IMemMgr : public IRemoteBroker { +public: + DECLARE_INTERFACE_DESCRIPTOR(u"ohos.memory.MemMgr"); + + virtual int32_t GetBundlePriorityList(BundlePriorityList &bundlePrioList) = 0; + + enum { + MEM_MGR_GET_BUNDLE_PRIORITY_LIST = 1, + }; +}; +} // namespace Memory +} // namespace OHOS +#endif // OHOS_MEMORY_MEMMGR_INTERFACES_INNERKITS_INCLUDE_MEM_MGR_INTERFACE_H diff --git a/interface/innerkits/include/mem_mgr_client.h b/interface/innerkits/include/mem_mgr_client.h new file mode 100644 index 0000000..49786d0 --- /dev/null +++ b/interface/innerkits/include/mem_mgr_client.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 OHOS_MEMORY_MEMMGR_INTERFACES_INNERKITS_INCLUDE_MEM_MGR_CLIENT_H +#define OHOS_MEMORY_MEMMGR_INTERFACES_INNERKITS_INCLUDE_MEM_MGR_CLIENT_H + +#include "i_mem_mgr.h" +#include "single_instance.h" + +namespace OHOS { +namespace Memory { +class MemMgrClient { + DECLARE_SINGLE_INSTANCE(MemMgrClient); + +public: + int32_t GetBundlePriorityList(BundlePriorityList &bundlePrioList); + +private: + sptr GetMemMgrService(); + std::mutex mutex_; + sptr dpProxy_; +}; +} // namespace Memory +} // namespace OHOS +#endif // OHOS_MEMORY_MEMMGR_INTERFACES_INNERKITS_INCLUDE_MEM_MGR_CLIENT_H diff --git a/interface/innerkits/include/mem_mgr_proxy.h b/interface/innerkits/include/mem_mgr_proxy.h new file mode 100644 index 0000000..987ac66 --- /dev/null +++ b/interface/innerkits/include/mem_mgr_proxy.h @@ -0,0 +1,33 @@ +/* + * 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 OHOS_MEMORY_MEMMGR_INTERFACES_INNERKITS_INCLUDE_MEM_MGR_PROXY_H +#define OHOS_MEMORY_MEMMGR_INTERFACES_INNERKITS_INCLUDE_MEM_MGR_PROXY_H + +#include "i_mem_mgr.h" +#include "iremote_proxy.h" + +namespace OHOS { +namespace Memory { +class MemMgrProxy : public IRemoteProxy { +public: + explicit MemMgrProxy(const sptr& impl) : IRemoteProxy(impl) {} + ~MemMgrProxy() = default; + int32_t GetBundlePriorityList(BundlePriorityList &bundlePrioList) override; +private: + static inline BrokerDelegator delegator_; +}; +} // namespace Memory +} // namespace OHOS +#endif // OHOS_MEMORY_MEMMGR_INTERFACES_INNERKITS_INCLUDE_MEM_MGR_PROXY_H diff --git a/interface/innerkits/src/bundle_priority_list.cpp b/interface/innerkits/src/bundle_priority_list.cpp new file mode 100644 index 0000000..6512393 --- /dev/null +++ b/interface/innerkits/src/bundle_priority_list.cpp @@ -0,0 +1,116 @@ +/* + * 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 "bundle_priority_list.h" +#include "memmgr_log.h" + +namespace OHOS { +namespace Memory { +namespace { +const std::string TAG = "MemMgrClient"; +} + +int32_t BundlePriorityList::GetCount() const +{ + return count_; +} + +void BundlePriorityList::SetCount(int32_t count) +{ + count_ = count; +} + +const std::vector& BundlePriorityList::GetList() +{ + return list_; +} + +int32_t BundlePriorityList::Size() const +{ + return list_.size(); +} + +void BundlePriorityList::AddBundleInfo(BundlePriority &bundleInfo) +{ + list_.push_back(bundleInfo); +} + +void BundlePriorityList::Show() const +{ + HILOGI(" uid name priority accountId"); + for (auto bi : list_) { + HILOGI("%{public}8d\t%{public}42s\t%{public}5d\t%{public}3d", + bi.uid_, bi.name_.c_str(), bi.priority_, bi.accountId_); + } + HILOGI("-------------------------------------------------------------------------------"); +} + +bool BundlePriorityList::Marshalling(Parcel &parcel) const +{ + if (!parcel.WriteInt32(count_)) { + HILOGE("Failed to write count_"); + return false; + } + for (auto i = 0; i < count_; ++i) { + if (!parcel.WriteInt32(list_[i].uid_)) { + HILOGE("Failed to write uid_"); + return false; + } + if (!parcel.WriteString(list_[i].name_)) { + HILOGE("Failed to write ability name"); + return false; + } + if (!parcel.WriteInt32(list_[i].priority_)) { + HILOGE("Failed to write priority_"); + return false; + } + if (!parcel.WriteInt32(list_[i].accountId_)) { + HILOGE("Failed to write accountId_"); + return false; + } + } + return true; +} + +BundlePriorityList* BundlePriorityList::Unmarshalling(Parcel &parcel) +{ + auto object = new (std::nothrow) BundlePriorityList(); + if ((object != nullptr) && !object->ReadFromParcel(parcel)) { + delete object; + object = nullptr; + } + + return object; +} + +bool BundlePriorityList::ReadFromParcel(Parcel &parcel) +{ + count_ = parcel.ReadInt32(); + for (auto i = 0; i < count_; ++i) { + int32_t uid = parcel.ReadInt32(); + std::string name; + if (!parcel.ReadString(name)) { + HILOGE("Failed to read creator ability name"); + return false; + } + int32_t priority = parcel.ReadInt32(); + int32_t accountId = parcel.ReadInt32(); + list_.push_back(BundlePriority(uid, name, priority, accountId)); + } + return true; +} +} +} + diff --git a/interface/innerkits/src/mem_mgr_client.cpp b/interface/innerkits/src/mem_mgr_client.cpp new file mode 100644 index 0000000..eb1bc95 --- /dev/null +++ b/interface/innerkits/src/mem_mgr_client.cpp @@ -0,0 +1,66 @@ +/* + * 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 "mem_mgr_client.h" +#include "memmgr_log.h" + +#include "if_system_ability_manager.h" +#include "ipc_skeleton.h" +#include "iservice_registry.h" +#include "system_ability_definition.h" + +namespace OHOS { +namespace Memory { +namespace { +const std::string TAG = "MemMgrClient"; +} + +IMPLEMENT_SINGLE_INSTANCE(MemMgrClient); + +int32_t MemMgrClient::GetBundlePriorityList(BundlePriorityList &bundlePrioList) +{ + HILOGE("called"); + auto dps = GetMemMgrService(); + if (dps == nullptr) { + HILOGE("MemMgrService is null"); + return -1; + } + return dps->GetBundlePriorityList(bundlePrioList); +} + +sptr MemMgrClient::GetMemMgrService() +{ + HILOGI("called"); + std::lock_guard lock(mutex_); + if (dpProxy_ != nullptr) { + return dpProxy_; + } + + auto samgrProxy = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); + if (samgrProxy == nullptr) { + HILOGE("get samgr failed"); + return nullptr; + } + auto object = samgrProxy->GetSystemAbility(MEMORY_MANAGER_SA_ID); + if (object == nullptr) { + HILOGE("get service failed"); + return nullptr; + } + HILOGI("get service succeed"); + dpProxy_ = iface_cast(object); + return dpProxy_; +} +} // namespace DeviceProfile +} // namespace OHOS \ No newline at end of file diff --git a/interface/innerkits/src/mem_mgr_proxy.cpp b/interface/innerkits/src/mem_mgr_proxy.cpp new file mode 100644 index 0000000..e79626d --- /dev/null +++ b/interface/innerkits/src/mem_mgr_proxy.cpp @@ -0,0 +1,54 @@ +/* + * 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 "mem_mgr_proxy.h" +#include "memmgr_log.h" + +namespace OHOS { +namespace Memory { +namespace { +const std::string TAG = "MemMgrProxy"; +} + +int32_t MemMgrProxy::GetBundlePriorityList(BundlePriorityList &bundlePrioList) +{ + HILOGE("called"); + sptr remote = Remote(); + MessageParcel data; + if (!data.WriteInterfaceToken(IMemMgr::GetDescriptor())) { + HILOGE("write interface token failed"); + return ERR_FLATTEN_OBJECT; + } + if (!data.WriteParcelable(&bundlePrioList)) { + HILOGE("write bundlePrioList failed"); + return ERR_FLATTEN_OBJECT; + } + MessageParcel reply; + MessageOption option; + int32_t error = remote->SendRequest(IMemMgr::MEM_MGR_GET_BUNDLE_PRIORITY_LIST, data, reply, option); + if (error != ERR_NONE) { + HILOGE("transact failed, error: %{public}d", error); + return error; + } + std::shared_ptr list + = std::shared_ptr(reply.ReadParcelable()); + if (list == nullptr) { + return -1; + } + bundlePrioList = *list; + return ERR_OK; +} +} // namespace Memory +} // namespace OHOS diff --git a/memmgr.gni b/memmgr.gni index f520696..078a0f4 100644 --- a/memmgr.gni +++ b/memmgr.gni @@ -20,3 +20,5 @@ memmgr_root_path = "//foundation/resourceschedule/${memmgr_part_name}" memmgr_common_path = "${memmgr_root_path}/common" memmgr_service_path = "${memmgr_root_path}/services/memmgrservice" + +memgr_innerkits_path = "${memmgr_root_path}/interface/innerkits" diff --git a/services/memmgrservice/BUILD.gn b/services/memmgrservice/BUILD.gn index e7ecfdd..792e1e7 100644 --- a/services/memmgrservice/BUILD.gn +++ b/services/memmgrservice/BUILD.gn @@ -33,6 +33,7 @@ config("memory_memmgr_config") { include_dirs = [ "include", "${memmgr_root_path}/common/include", + "${memgr_innerkits_path}/include", "include/event/", "include/reclaim_priority_manager/", "include/kill_strategy_manager/", @@ -58,6 +59,7 @@ ohos_shared_library("memmgrservice") { "src/event/memory_pressure_monitor.cpp", "src/kill_strategy_manager/low_memory_killer.cpp", "src/mem_mgr_service.cpp", + "src/mem_mgr_stub.cpp", "src/reclaim_priority_manager/account_bundle_info.cpp", "src/reclaim_priority_manager/account_priority_info.cpp", "src/reclaim_priority_manager/bundle_priority_info.cpp", @@ -87,6 +89,7 @@ ohos_shared_library("memmgrservice") { "hisysevent_native:libhisysevent", "hiviewdfx_hilog_native:libhilog", "ipc:ipc_core", + "memmgr:memmgrclient", "os_account_standard:libaccountkits", "os_account_standard:os_account_innerkits", "safwk:system_ability_fwk", diff --git a/services/memmgrservice/include/mem_mgr_service.h b/services/memmgrservice/include/mem_mgr_service.h index 52ed562..1801199 100644 --- a/services/memmgrservice/include/mem_mgr_service.h +++ b/services/memmgrservice/include/mem_mgr_service.h @@ -16,18 +16,20 @@ #ifndef OHOS_MEMORY_MEMMGR_SERVICE_H #define OHOS_MEMORY_MEMMGR_SERVICE_H +#include "mem_mgr_stub.h" #include "single_instance.h" #include "system_ability.h" namespace OHOS { namespace Memory { -class MemMgrService : public SystemAbility { +class MemMgrService : public SystemAbility, public MemMgrStub { DECLARE_SYSTEM_ABILITY(MemMgrService); DECLARE_SINGLE_INSTANCE_BASE(MemMgrService); public: MemMgrService(); ~MemMgrService() = default; + virtual int32_t GetBundlePriorityList(BundlePriorityList &bundlePrioList) override; protected: void OnStart() override; diff --git a/services/memmgrservice/include/mem_mgr_stub.h b/services/memmgrservice/include/mem_mgr_stub.h new file mode 100644 index 0000000..6c7bef5 --- /dev/null +++ b/services/memmgrservice/include/mem_mgr_stub.h @@ -0,0 +1,46 @@ +/* + * 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 OHOS_MEMORY_MEMMGR_INTERFACES_INNERKITS_INCLUDE_MEM_MGR_STUB_H +#define OHOS_MEMORY_MEMMGR_INTERFACES_INNERKITS_INCLUDE_MEM_MGR_STUB_H + +#include + +#include "iremote_stub.h" +#include "nocopyable.h" +#include "string_ex.h" +#include "i_mem_mgr.h" + +namespace OHOS { +namespace Memory { +class MemMgrStub : public IRemoteStub { +public: + MemMgrStub(); + virtual ~MemMgrStub(); + + virtual int OnRemoteRequest( + uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) override; + +private: + int32_t HandleGetBunldePriorityList(MessageParcel &data, MessageParcel &reply); + + using MemMgrFunc = int32_t (MemMgrStub::*)(MessageParcel &data, MessageParcel &reply); + std::map memberFuncMap_; + + DISALLOW_COPY_AND_MOVE(MemMgrStub); +}; +} // namespace Memory +} // namespace OHOS +#endif // OHOS_MEMORY_MEMMGR_INTERFACES_INNERKITS_INCLUDE_MEM_MGR_STUB_H diff --git a/services/memmgrservice/src/mem_mgr_service.cpp b/services/memmgrservice/src/mem_mgr_service.cpp index 8cceaf2..4194768 100644 --- a/services/memmgrservice/src/mem_mgr_service.cpp +++ b/services/memmgrservice/src/mem_mgr_service.cpp @@ -69,11 +69,32 @@ void MemMgrService::OnStart() HILOGE("init failed"); return; } + if (!Publish(this)) { + HILOGE("publish SA failed"); + return; + } + HILOGI("publish SA successed"); } void MemMgrService::OnStop() { HILOGI("called"); } + +// implements of innerkits list below + +int32_t MemMgrService::GetBundlePriorityList(BundlePriorityList &bundlePrioList) +{ + HILOGI("called"); + ReclaimPriorityManager::BunldeCopySet bundleSet; + ReclaimPriorityManager::GetInstance().GetBundlePrioSet(bundleSet); + for (auto bundlePriorityInfo : bundleSet) { + Memory::BundlePriority *bi = new Memory::BundlePriority(bundlePriorityInfo.uid_, + bundlePriorityInfo.name_, bundlePriorityInfo.priority_, bundlePriorityInfo.accountId_); + bundlePrioList.AddBundleInfo(*bi); + } + bundlePrioList.SetCount(bundlePrioList.Size()); + return 0; +} } // namespace Memory } // namespace OHOS diff --git a/services/memmgrservice/src/mem_mgr_stub.cpp b/services/memmgrservice/src/mem_mgr_stub.cpp new file mode 100644 index 0000000..51c0b9d --- /dev/null +++ b/services/memmgrservice/src/mem_mgr_stub.cpp @@ -0,0 +1,71 @@ +/* + * 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 "mem_mgr_stub.h" +#include "memmgr_log.h" + +namespace OHOS { +namespace Memory { +namespace { + const std::string TAG = "MemMgrStub"; +} + +MemMgrStub::MemMgrStub() +{ + memberFuncMap_[static_cast(IMemMgr::MEM_MGR_GET_BUNDLE_PRIORITY_LIST)] = + &MemMgrStub::HandleGetBunldePriorityList; +} + +MemMgrStub::~MemMgrStub() +{ + memberFuncMap_.clear(); +} + +int MemMgrStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) +{ + HILOGI("MemMgrStub::OnReceived, code = %{public}d, flags= %{public}d.", code, option.GetFlags()); + std::u16string descriptor = MemMgrStub::GetDescriptor(); + std::u16string remoteDescriptor = data.ReadInterfaceToken(); + if (descriptor != remoteDescriptor) { + HILOGE("local descriptor is not equal to remote"); + return ERR_INVALID_STATE; + } + + auto itFunc = memberFuncMap_.find(code); + if (itFunc != memberFuncMap_.end()) { + auto memberFunc = itFunc->second; + if (memberFunc != nullptr) { + return (this->*memberFunc)(data, reply); + } + } + return IPCObjectStub::OnRemoteRequest(code, data, reply, option); +} + +int32_t MemMgrStub::HandleGetBunldePriorityList(MessageParcel &data, MessageParcel &reply) +{ + HILOGI("called"); + std::shared_ptr list + = std::shared_ptr(data.ReadParcelable()); + + if (!list) { + HILOGE("BundlePriorityList ReadParcelable failed"); + return -1; + } + int32_t ret = GetBundlePriorityList(*list); + reply.WriteParcelable(list.get()); + return ret; +} +} // namespace Memory +} // namespace OHOS diff --git a/test/BUILD.gn b/test/BUILD.gn index 3a1864b..d7174db 100644 --- a/test/BUILD.gn +++ b/test/BUILD.gn @@ -31,6 +31,7 @@ memmgr_deps = [ memmgr_external_deps = [ "hiviewdfx_hilog_native:libhilog", "ipc:ipc_core", + "memmgr:memmgrclient", "os_account_standard:os_account_innerkits", ] @@ -148,9 +149,26 @@ ohos_unittest("reclaim_strategy_manager_test") { subsystem_name = "resourceschedule" } +ohos_unittest("innerkits_test") { + module_out_path = module_output_path + configs = memmgr_service_configs + + sources = [ "unittest/phone/innerkits_test.cpp" ] + + deps = memmgr_deps + if (is_standard_system) { + external_deps = memmgr_external_deps + public_deps = memmgr_public_deps + } + + part_name = "memmgr" + subsystem_name = "resourceschedule" +} + group("memmgr_unittest") { testonly = true deps = [ + ":innerkits_test", ":kernel_interface_test", ":memcg_mgr_test", ":memcg_test", diff --git a/test/unittest/phone/innerkits_test.cpp b/test/unittest/phone/innerkits_test.cpp new file mode 100644 index 0000000..18d9436 --- /dev/null +++ b/test/unittest/phone/innerkits_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 "utils.h" + +#define private public +#define protected public +#include "mem_mgr_client.h" +#include "bundle_priority_list.h" +#undef private +#undef protected + +namespace OHOS { +namespace Memory { +using namespace testing; +using namespace testing::ext; + +class InnerkitsTest : public testing::Test { +public: + static void SetUpTestCase(); + static void TearDownTestCase(); + void SetUp(); + void TearDown(); +}; + +void InnerkitsTest::SetUpTestCase() +{ +} + +void InnerkitsTest::TearDownTestCase() +{ +} + +void InnerkitsTest::SetUp() +{ +} + +void InnerkitsTest::TearDown() +{ +} + +HWTEST_F(InnerkitsTest, GetBundlePriorityList_Test, TestSize.Level1) +{ + BundlePriorityList bundlePrioList; + int32_t ret = MemMgrClient::GetInstance().GetBundlePriorityList(bundlePrioList); + EXPECT_EQ(ret, 0); + bundlePrioList.Show(); +} +} +} -- Gitee