1 /**
2 * Copyright (c) 2021-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 "profiling_data.h"
17 #include "utils/logger.h"
18 #include "ipc/ipc_message.h"
19 #include "ipc/ipc_unix_socket.h"
20 #include "ipc/ipc_message_protocol.h"
21 #include "serializer/serializer.h"
22
23 namespace panda::dprof {
SetFeatureDate(const std::string & featureName,std::vector<uint8_t> && data)24 bool ProfilingData::SetFeatureDate(const std::string &featureName, std::vector<uint8_t> &&data)
25 {
26 auto it = features_data_map_.find(featureName);
27 if (it != features_data_map_.end()) {
28 LOG(ERROR, DPROF) << "Feature already exists, featureName=" << featureName;
29 return false;
30 }
31
32 features_data_map_.emplace(std::pair(featureName, std::move(data)));
33 return false;
34 }
35
DumpAndResetFeatures()36 bool ProfilingData::DumpAndResetFeatures()
37 {
38 os::unique_fd::UniqueFd sock(ipc::CreateUnixClientSocket());
39 if (!sock.IsValid()) {
40 LOG(ERROR, DPROF) << "Cannot create client socket";
41 return false;
42 }
43
44 ipc::protocol::Version tmp {ipc::protocol::VERSION};
45
46 std::vector<uint8_t> versionData;
47 serializer::StructToBuffer<ipc::protocol::VERSION_FCOUNT>(tmp, versionData);
48
49 ipc::Message msgVersion(ipc::Message::Id::VERSION, std::move(versionData));
50 if (!SendMessage(sock.Get(), msgVersion)) {
51 LOG(ERROR, DPROF) << "Cannot send version";
52 return false;
53 }
54
55 ipc::protocol::AppInfo tmp2 {app_name_, hash_, pid_};
56 std::vector<uint8_t> appInfoData;
57 serializer::StructToBuffer<ipc::protocol::APP_INFO_FCOUNT>(tmp2, appInfoData);
58
59 ipc::Message msgAppInfo(ipc::Message::Id::APP_INFO, std::move(appInfoData));
60 if (!SendMessage(sock.Get(), msgAppInfo)) {
61 LOG(ERROR, DPROF) << "Cannot send app info";
62 return false;
63 }
64
65 // Send features data
66 for (auto &kv : features_data_map_) {
67 ipc::protocol::FeatureData tmp_data;
68 tmp_data.name = kv.first;
69 tmp_data.data = std::move(kv.second);
70
71 std::vector<uint8_t> featureData;
72 serializer::StructToBuffer<ipc::protocol::FEATURE_DATA_FCOUNT>(tmp_data, featureData);
73
74 ipc::Message msgFeatureData(ipc::Message::Id::FEATURE_DATA, std::move(featureData));
75 if (!SendMessage(sock.Get(), msgFeatureData)) {
76 LOG(ERROR, DPROF) << "Cannot send feature data, featureName=" << tmp_data.name;
77 return false;
78 }
79 }
80
81 features_data_map_.clear();
82 return true;
83 }
84 } // namespace panda::dprof
85