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