1 /*
2 * Copyright (C) 2021 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 "executor/traffic_dumper.h"
17 #ifdef HIDUMPER_ABILITY_RUNTIME_ENABLE
18 #include "net_stats_client.h"
19 #endif
20 #include "dump_common_utils.h"
21
22 namespace OHOS {
23 namespace HiviewDFX {
24 static const std::string RECEIVED_BYTES = "Received Bytes:";
25 static const std::string SEND_BYTES = "Sent Bytes:";
26
TrafficDumper()27 TrafficDumper::TrafficDumper()
28 {}
29
~TrafficDumper()30 TrafficDumper::~TrafficDumper()
31 {}
32
PreExecute(const std::shared_ptr<DumperParameter> & parameter,StringMatrix dumpDatas)33 DumpStatus TrafficDumper::PreExecute(const std::shared_ptr<DumperParameter> ¶meter, StringMatrix dumpDatas)
34 {
35 pid_ = parameter->GetOpts().netPid_;
36 result_ = dumpDatas;
37 return DumpStatus::DUMP_OK;
38 }
39
Execute()40 DumpStatus TrafficDumper::Execute()
41 {
42 DUMPER_HILOGI(MODULE_COMMON, "info|TrafficDumper Execute");
43 if (result_ != nullptr) {
44 pid_ >= 0 ? GetApplicationUidBytes() : GetAllBytes();
45 }
46 DUMPER_HILOGI(MODULE_COMMON, "info|TrafficDumper Execute end");
47 return status_;
48 }
49
AfterExecute()50 DumpStatus TrafficDumper::AfterExecute()
51 {
52 return DumpStatus::DUMP_OK;
53 }
54
GetAllBytes()55 void TrafficDumper::GetAllBytes()
56 {
57 DUMPER_HILOGD(MODULE_SERVICE, "debug|GetAllRxBytes.\n");
58 uint64_t receivedStats = 0;
59 #ifdef HIDUMPER_ABILITY_RUNTIME_ENABLE
60 int32_t ret = DelayedSingleton<NetManagerStandard::NetStatsClient>::GetInstance()->GetAllRxBytes(receivedStats);
61 if (ret != NetManagerStandard::NETMANAGER_SUCCESS) {
62 DUMPER_HILOGE(MODULE_SERVICE, "GetAllRxBytes failed, ret:%{public}d.\n", ret);
63 status_ = DumpStatus::DUMP_FAIL;
64 }
65 #endif
66 std::vector<std::string> line_vector;
67 line_vector.push_back(RECEIVED_BYTES + std::to_string(receivedStats));
68 result_->push_back(line_vector);
69 #ifdef HIDUMPER_ABILITY_RUNTIME_ENABLE
70 uint64_t sendStats = 0;
71 ret = DelayedSingleton<NetManagerStandard::NetStatsClient>::GetInstance()->GetAllTxBytes(sendStats);
72 if (ret != NetManagerStandard::NETMANAGER_SUCCESS) {
73 DUMPER_HILOGE(MODULE_SERVICE, "GetAllRxBytes failed, ret:%{public}d.\n", ret);
74 status_ = DumpStatus::DUMP_FAIL;
75 }
76 #endif
77 line_vector.clear();
78 line_vector.push_back(SEND_BYTES + std::to_string(sendStats));
79 result_->push_back(line_vector);
80 status_ = DumpStatus::DUMP_OK;
81 }
82
GetApplicationUidBytes()83 void TrafficDumper::GetApplicationUidBytes()
84 {
85 DUMPER_HILOGD(MODULE_SERVICE, "debug|GetApplicationUidBytes.\n");
86 DumpCommonUtils::PidInfo currentPidInfo;
87 currentPidInfo.pid_ = pid_;
88 currentPidInfo.uid_ = -1;
89 if (!DumpCommonUtils::GetProcessInfo(pid_, currentPidInfo)) {
90 DUMPER_HILOGE(MODULE_SERVICE, "GetProcUid failed, pid:%{public}d, uid:%{public}d.\n",
91 pid_, currentPidInfo.uid_);
92 return;
93 }
94 uint64_t receivedStats = 0;
95 #ifdef HIDUMPER_ABILITY_RUNTIME_ENABLE
96 int32_t ret = DelayedSingleton<NetManagerStandard::NetStatsClient>::GetInstance()->GetUidRxBytes(
97 receivedStats, static_cast<uint32_t>(currentPidInfo.uid_));
98 if (ret != NetManagerStandard::NETMANAGER_SUCCESS) {
99 DUMPER_HILOGE(MODULE_SERVICE, "GetUidRxBytes failed, ret:%{public}d, uid:%{public}d.\n",
100 ret, currentPidInfo.uid_);
101 status_ = DumpStatus::DUMP_FAIL;
102 }
103 #endif
104 std::vector<std::string> line_vector;
105 line_vector.push_back(RECEIVED_BYTES + std::to_string(receivedStats));
106 result_->push_back(line_vector);
107
108 uint64_t sendStats = 0;
109 #ifdef HIDUMPER_ABILITY_RUNTIME_ENABLE
110 ret = DelayedSingleton<NetManagerStandard::NetStatsClient>::GetInstance()->GetUidTxBytes(
111 sendStats, static_cast<uint32_t>(currentPidInfo.uid_));
112 if (ret != NetManagerStandard::NETMANAGER_SUCCESS) {
113 DUMPER_HILOGE(MODULE_SERVICE, "GetUidTxBytes failed, ret:%{public}d, uid:%{public}d.\n",
114 ret, currentPidInfo.uid_);
115 status_ = DumpStatus::DUMP_FAIL;
116 }
117 #endif
118 line_vector.clear();
119 line_vector.push_back(SEND_BYTES + std::to_string(sendStats));
120 result_->push_back(line_vector);
121 status_ = DumpStatus::DUMP_OK;
122 DUMPER_HILOGD(MODULE_SERVICE, "debug|GetApplicationUidBytes end, pid:%{public}d, uid:%{public}d.\n",
123 pid_, currentPidInfo.uid_);
124 }
125 } // namespace HiviewDFX
126 } // namespace OHOS
127