1 /*
2 * Copyright (c) 2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "form_memmgr_client.h"
17
18 #include <functional>
19 #include <dlfcn.h>
20 #include <unistd.h>
21 #include "fms_log_wrapper.h"
22 #include "parameters.h"
23
24 namespace OHOS {
25 namespace AppExecFwk {
26 namespace {
27 constexpr int32_t FORM_RENDER_SERVICE_TYPE = 3;
28 constexpr int32_t FORM_RENDER_SERVICE_STATUS = 1;
29 constexpr int32_t FORM_RENDER_SERVICE_SAID = -1;
30 constexpr char LIB_MEMMGR_CLIENT_NAME[] = "libmemmgrclient.z.so";
31 constexpr char CRITICAL_NOTIFY_PARAM_NAME[] = "const.form.mem_reduce.critical_notify_enable";
32 }
33
FormMemmgrClient()34 FormMemmgrClient::FormMemmgrClient()
35 {
36 criticalNotifyEnable_ = system::GetBoolParameter(CRITICAL_NOTIFY_PARAM_NAME, true);
37 NotifyProcessStatus();
38 }
39
~FormMemmgrClient()40 FormMemmgrClient::~FormMemmgrClient()
41 {}
42
SetCritical(bool critical)43 void FormMemmgrClient::SetCritical(bool critical)
44 {
45 int32_t pid = getprocpid();
46 HILOG_INFO("pid:%{public}" PRId32 ", critical:%{public}d", pid, critical);
47
48 if (!critical && !criticalNotifyEnable_) {
49 HILOG_ERROR("setCritical fail, critical notify enable is false");
50 return;
51 }
52 void *libMemmgrClientHandle = dlopen(LIB_MEMMGR_CLIENT_NAME, RTLD_NOW);
53 if (!libMemmgrClientHandle) {
54 HILOG_ERROR("dlopen libmemmgrclient fail");
55 return;
56 }
57
58 void *setCritical = (dlsym(libMemmgrClientHandle, "set_critical"));
59 if (!setCritical) {
60 HILOG_ERROR("dlsym set_critical fail");
61 dlclose(libMemmgrClientHandle);
62 return;
63 }
64
65 auto setCriticalFunc = reinterpret_cast<int32_t(*)(int32_t, bool, int32_t)>(setCritical);
66 if (setCriticalFunc(pid, critical, FORM_RENDER_SERVICE_SAID) != 0) {
67 HILOG_ERROR("setCriticalFunc fail");
68 } else {
69 critical_.store(critical);
70 }
71 dlclose(libMemmgrClientHandle);
72 }
73
NotifyProcessStatus()74 void FormMemmgrClient::NotifyProcessStatus()
75 {
76 void *libMemMgrClientHandle = dlopen(LIB_MEMMGR_CLIENT_NAME, RTLD_NOW);
77 if (!libMemMgrClientHandle) {
78 HILOG_ERROR("dlopen libmemmgrclient library failed");
79 return;
80 }
81 void *notifyProcessStatusFunc = dlsym(libMemMgrClientHandle, "notify_process_status");
82 if (!notifyProcessStatusFunc) {
83 HILOG_ERROR("dlsm notify_process_status failed");
84 dlclose(libMemMgrClientHandle);
85 return;
86 }
87 auto notifyProcessStatus = reinterpret_cast<int(*)(int, int, int, int)>(notifyProcessStatusFunc);
88 HILOG_INFO("notify to memmgr when frs start");
89 int pid = getprocpid();
90 notifyProcessStatus(pid, FORM_RENDER_SERVICE_TYPE, FORM_RENDER_SERVICE_STATUS, FORM_RENDER_SERVICE_SAID);
91 dlclose(libMemMgrClientHandle);
92 }
93 } // namespace AppExecFwk
94 } // namespace OHOS
95