1 /* 2 * Copyright (c) Huawei Technologies Co., Ltd. 2021-2023. 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 #ifndef SOCKET_CONTEXT_H 17 #define SOCKET_CONTEXT_H 18 19 #include <cstdint> 20 #ifndef NO_PROTOBUF 21 #include <google/protobuf/message.h> 22 #endif 23 #include <thread> 24 25 #if defined(__i386__) || defined(__x86_64__) 26 const static char DEFAULT_UNIX_SOCKET_PATH[] = "hiprofiler_unix_socket"; 27 const static char DEFAULT_UNIX_SOCKET_HOOK_PATH[] = "hook_unix_socket"; 28 const static char FFRT_PROFILER_UNIX_SOCKET_PATH[] = "ffrt_profiler_unix_socket"; 29 const static char FFRT_PROFILER_UNIX_SOCKET_FULL_PATH[] = "/dev/unix/socket/ffrt_profiler_unix_socket"; 30 const static char NETWORK_PROFILER_UNIX_SOCKET_PATH[] = "network_profiler_unix_socket"; 31 const static char NETWORK_PROFILER_UNIX_SOCKET_FULL_PATH[] = "/dev/unix/socket/network_profiler_unix_socket"; 32 #else 33 const static char DEFAULT_UNIX_SOCKET_PATH[] = "hiprofiler_unix_socket"; 34 const static char DEFAULT_UNIX_SOCKET_HOOK_PATH[] = "hook_unix_socket"; 35 36 const static char DEFAULT_UNIX_SOCKET_FULL_PATH[] = "/dev/unix/socket/hiprofiler_unix_socket"; 37 const static char DEFAULT_UNIX_SOCKET_HOOK_FULL_PATH[] = "/dev/unix/socket/hook_unix_socket"; 38 39 const static char FFRT_PROFILER_UNIX_SOCKET_PATH[] = "ffrt_profiler_unix_socket"; 40 const static char FFRT_PROFILER_UNIX_SOCKET_FULL_PATH[] = "/dev/unix/socket/ffrt_profiler_unix_socket"; 41 42 const static char NETWORK_PROFILER_UNIX_SOCKET_PATH[] = "network_profiler_unix_socket"; 43 const static char NETWORK_PROFILER_UNIX_SOCKET_FULL_PATH[] = "/dev/unix/socket/network_profiler_unix_socket"; 44 #endif 45 46 class ServiceBase; 47 48 enum ClientState { 49 CLIENT_STAT_WORKING, 50 CLIENT_STAT_WAIT_THREAD_EXIT, 51 CLIENT_STAT_THREAD_EXITED, 52 }; 53 54 enum RawProtocol { 55 RAW_PROTOCOL_POINTTO_SERVICE = 1, 56 }; 57 struct RawPointToService { 58 char serviceName_[64]; 59 }; 60 61 class SocketContext { 62 public: 63 SocketContext(); 64 virtual ~SocketContext(); 65 66 bool SendRaw(uint32_t pnum, const int8_t* data, uint32_t size, int sockfd = -1); 67 #ifndef NO_PROTOBUF 68 bool SendProtobuf(uint32_t pnum, google::protobuf::Message& pmsg); 69 #endif 70 bool SendFileDescriptor(int fd); 71 bool SendHookConfig(const uint8_t* config, size_t len); 72 bool SendHeartBeat(); 73 bool IsConnected(); 74 int ReceiveFileDiscriptor(); GetClientState()75 enum ClientState GetClientState() { return clientState_; } GetSocketHandle()76 int GetSocketHandle() { return socketHandle_; } 77 protected: 78 int socketHandle_; 79 bool CreateRecvThread(void (*callback)() = nullptr); 80 volatile enum ClientState clientState_; 81 uint64_t lastProcMS_; 82 83 ServiceBase* serviceBase_; 84 85 virtual int RawProtocolProc(uint32_t pnum, const int8_t* buf, const uint32_t size); 86 87 private: 88 static bool ReceiveData(int sock, uint8_t* databuf, uint32_t size); 89 static void* UnixSocketRecv(void* pp, void (*callback)() = nullptr); 90 std::thread recvThread_; 91 }; 92 93 #endif