1 /*
2 * Copyright (c) Huawei Technologies Co., Ltd. 2024. All rights reserved.
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 "ffrt_profiler_socket_service.h"
17
18 #include <cinttypes>
19 #include <unistd.h>
20
21 #include "socket_context.h"
22 #include "logging.h"
23 #include "ffrt_profiler_manager.h"
24
25 namespace OHOS::Developtools::Profiler {
FfrtProfilerSocketService(std::shared_ptr<FfrtProfilerManager> ffrtMgr)26 FfrtProfilerSocketService::FfrtProfilerSocketService(std::shared_ptr<FfrtProfilerManager> ffrtMgr)
27 : ffrtMgr_(ffrtMgr)
28 {
29 serviceName_ = "FfrtProfilerService";
30 StartService(FFRT_PROFILER_UNIX_SOCKET_PATH);
31 }
32
~FfrtProfilerSocketService()33 FfrtProfilerSocketService::~FfrtProfilerSocketService()
34 {
35 serviceEntry_ = nullptr;
36 }
37
StartService(const std::string & unixSocketName)38 bool FfrtProfilerSocketService::StartService(const std::string& unixSocketName)
39 {
40 serviceEntry_ = std::make_shared<ServiceEntry>();
41 if (!serviceEntry_->StartServer(unixSocketName)) {
42 serviceEntry_ = nullptr;
43 PROFILER_LOG_DEBUG(LOG_CORE, "Start IPC Service FAIL");
44 return false;
45 }
46 serviceEntry_->RegisterService(*this);
47 return true;
48 }
49
ProtocolProc(SocketContext & context,uint32_t pnum,const int8_t * buf,const uint32_t size)50 bool FfrtProfilerSocketService::ProtocolProc(SocketContext &context, uint32_t pnum, const int8_t *buf,
51 const uint32_t size)
52 {
53 if (size != sizeof(int)) {
54 return false;
55 }
56 int pid = *const_cast<int *>(reinterpret_cast<const int *>(buf));
57 if (pid == -1) {
58 return false;
59 }
60
61 std::string bundleName = GetProcessName(pid);
62
63 std::lock_guard<std::mutex> lock(mtx_);
64 auto [eventFd, smbFd] = ffrtMgr_->GetFfrtProfilerCtx(pid, bundleName);
65 if (eventFd == 0 && smbFd == 0) {
66 PROFILER_LOG_INFO(LOG_CORE, "Wait for process %s to restart", bundleName.c_str());
67 return true;
68 }
69 if (eventFd == smbFd) {
70 PROFILER_LOG_ERROR(LOG_CORE, "Get eventFd and smbFd failed!, name: %s, pid: %d", bundleName.c_str(), pid);
71 return false;
72 }
73
74 PROFILER_LOG_INFO(LOG_CORE, "ProtocolProc, send eventfd: %d, smbFd: %d, peerPid: %d", eventFd, smbFd, pid);
75 context.SendHookConfig(reinterpret_cast<const uint8_t*>(&config_), sizeof(config_));
76 context.SendFileDescriptor(smbFd);
77 context.SendFileDescriptor(eventFd);
78 return true;
79 }
80
SetConfig(int32_t shmSize,int32_t flushCount,bool block,int32_t clockType)81 void FfrtProfilerSocketService::SetConfig(int32_t shmSize, int32_t flushCount, bool block, int32_t clockType)
82 {
83 config_.shmSize = shmSize;
84 config_.flushCount = flushCount;
85 config_.block = block;
86 config_.clock = clockType;
87 }
88 } // namespace OHOS::Developtools::Profiler