• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     if (getuid() == 0) {
31         StartService(FFRT_PROFILER_UNIX_SOCKET_FULL_PATH);
32     } else {
33         StartService(FFRT_PROFILER_UNIX_SOCKET_PATH);
34     }
35 }
36 
~FfrtProfilerSocketService()37 FfrtProfilerSocketService::~FfrtProfilerSocketService()
38 {
39     serviceEntry_ = nullptr;
40 }
41 
StartService(const std::string & unixSocketName)42 bool FfrtProfilerSocketService::StartService(const std::string& unixSocketName)
43 {
44     serviceEntry_ = std::make_shared<ServiceEntry>();
45     if (!serviceEntry_->StartServer(unixSocketName)) {
46         serviceEntry_ = nullptr;
47         PROFILER_LOG_DEBUG(LOG_CORE, "Start IPC Service FAIL");
48         return false;
49     }
50     serviceEntry_->RegisterService(*this);
51     return true;
52 }
53 
ProtocolProc(SocketContext & context,uint32_t pnum,const int8_t * buf,const uint32_t size)54 bool FfrtProfilerSocketService::ProtocolProc(SocketContext &context, uint32_t pnum, const int8_t *buf,
55     const uint32_t size)
56 {
57     if (size != sizeof(int)) {
58         return false;
59     }
60     int pid = *const_cast<int *>(reinterpret_cast<const int *>(buf));
61     if (pid == -1) {
62         return false;
63     }
64 
65     std::string bundleName = GetProcessName(pid);
66 
67     std::lock_guard<std::mutex> lock(mtx_);
68     auto [eventFd, smbFd] = ffrtMgr_->GetFfrtProfilerCtx(pid, bundleName);
69     if (eventFd == 0 && smbFd == 0) {
70         PROFILER_LOG_INFO(LOG_CORE, "Wait for process %s to restart", bundleName.c_str());
71         return true;
72     }
73     if (eventFd == smbFd) {
74         PROFILER_LOG_ERROR(LOG_CORE, "Get eventFd and smbFd failed!, name: %s, pid: %d", bundleName.c_str(), pid);
75         return false;
76     }
77 
78     PROFILER_LOG_INFO(LOG_CORE, "ProtocolProc, send eventfd: %d, smbFd: %d, peerPid: %d", eventFd, smbFd, pid);
79     context.SendHookConfig(reinterpret_cast<const uint8_t*>(&config_), sizeof(config_));
80     context.SendFileDescriptor(smbFd);
81     context.SendFileDescriptor(eventFd);
82     return true;
83 }
84 
SetConfig(int32_t shmSize,int32_t flushCount,bool block,int32_t clockType)85 void FfrtProfilerSocketService::SetConfig(int32_t shmSize, int32_t flushCount, bool block, int32_t clockType)
86 {
87     config_.shmSize = shmSize;
88     config_.flushCount = flushCount;
89     config_.block = block;
90     config_.clock = clockType;
91 }
92 } // namespace OHOS::Developtools::Profiler