1 /*
2 * Copyright (c) 2022 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 "socperf_server.h"
17 #include "accesstoken_kit.h"
18 #include "ipc_skeleton.h"
19 #include "parameters.h"
20
21 namespace OHOS {
22 namespace SOCPERF {
23 const bool REGISTER_RESULT =
24 SystemAbility::MakeAndRegisterAbility(DelayedSingleton<SocPerfServer>::GetInstance().get());
25 const int32_t ENG_MODE = OHOS::system::GetIntParameter("const.debuggable", 0);
26
SocPerfServer()27 SocPerfServer::SocPerfServer() : SystemAbility(SOC_PERF_SERVICE_SA_ID, true)
28 {
29 }
30
~SocPerfServer()31 SocPerfServer::~SocPerfServer()
32 {
33 }
34
OnStart()35 void SocPerfServer::OnStart()
36 {
37 if (!Publish(DelayedSingleton<SocPerfServer>::GetInstance().get())) {
38 SOC_PERF_LOGE("Register SystemAbility for SocPerf FAILED.");
39 return;
40 }
41 if (!socPerf.Init()) {
42 SOC_PERF_LOGE("SocPerf Init FAILED");
43 return;
44 }
45 }
46
OnStop()47 void SocPerfServer::OnStop()
48 {
49 }
50
AllowDump()51 bool SocPerfServer::AllowDump()
52 {
53 if (ENG_MODE == 0) {
54 SOC_PERF_LOGE("Not allow to dump SocPerfServer, mode:%{public}d", ENG_MODE);
55 return false;
56 }
57 Security::AccessToken::AccessTokenID tokenId = IPCSkeleton::GetFirstTokenID();
58 int32_t res = Security::AccessToken::AccessTokenKit::VerifyAccessToken(tokenId, "ohos.permission.DUMP");
59 if (res != Security::AccessToken::PermissionState::PERMISSION_GRANTED) {
60 SOC_PERF_LOGE("Not allow to dump SocPerfServer, permission state:%{public}d", res);
61 return false;
62 }
63 return true;
64 }
65
Dump(int32_t fd,const std::vector<std::u16string> & args)66 int32_t SocPerfServer::Dump(int32_t fd, const std::vector<std::u16string>& args)
67 {
68 if (!AllowDump()) {
69 return ERR_PERMISSION_DENIED;
70 }
71 std::vector<std::string> argsInStr;
72 std::transform(args.begin(), args.end(), std::back_inserter(argsInStr),
73 [](const std::u16string &arg) {
74 return Str16ToStr8(arg);
75 });
76 std::string result;
77 result.append("usage: soc_perf service dump [<options>]\n")
78 .append(" 1. PerfRequest(cmdId, msg)\n")
79 .append(" 2. PerfRequestEx(cmdId, onOffTag, msg)\n")
80 .append(" 3. LimitRequest(clientId, tags, configs, msg)\n")
81 .append(" -h: show the help.\n")
82 .append(" -a: show all info.\n");
83 if (!SaveStringToFd(fd, result)) {
84 SOC_PERF_LOGE("Dump FAILED");
85 }
86 return ERR_OK;
87 }
88
PerfRequest(int32_t cmdId,const std::string & msg)89 void SocPerfServer::PerfRequest(int32_t cmdId, const std::string& msg)
90 {
91 socPerf.PerfRequest(cmdId, msg);
92 }
93
PerfRequestEx(int32_t cmdId,bool onOffTag,const std::string & msg)94 void SocPerfServer::PerfRequestEx(int32_t cmdId, bool onOffTag, const std::string& msg)
95 {
96 socPerf.PerfRequestEx(cmdId, onOffTag, msg);
97 }
98
PowerLimitBoost(bool onOffTag,const std::string & msg)99 void SocPerfServer::PowerLimitBoost(bool onOffTag, const std::string& msg)
100 {
101 socPerf.PowerLimitBoost(onOffTag, msg);
102 }
103
ThermalLimitBoost(bool onOffTag,const std::string & msg)104 void SocPerfServer::ThermalLimitBoost(bool onOffTag, const std::string& msg)
105 {
106 socPerf.ThermalLimitBoost(onOffTag, msg);
107 }
108
LimitRequest(int32_t clientId,const std::vector<int32_t> & tags,const std::vector<int64_t> & configs,const std::string & msg)109 void SocPerfServer::LimitRequest(int32_t clientId,
110 const std::vector<int32_t>& tags, const std::vector<int64_t>& configs, const std::string& msg)
111 {
112 socPerf.LimitRequest(clientId, tags, configs, msg);
113 }
114 } // namespace SOCPERF
115 } // namespace OHOS
116