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_NETMANAGER_BASE_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> &parameter, 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_NETMANAGER_BASE_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 
70     uint64_t sendStats = 0;
71 #ifdef HIDUMPER_NETMANAGER_BASE_ENABLE
72     ret = DelayedSingleton<NetManagerStandard::NetStatsClient>::GetInstance()->GetAllTxBytes(sendStats);
73     if (ret != NetManagerStandard::NETMANAGER_SUCCESS) {
74         DUMPER_HILOGE(MODULE_SERVICE, "GetAllRxBytes failed, ret:%{public}d.\n", ret);
75         status_ = DumpStatus::DUMP_FAIL;
76     }
77 #endif
78     line_vector.clear();
79     line_vector.push_back(SEND_BYTES + std::to_string(sendStats));
80     result_->push_back(line_vector);
81     status_ = DumpStatus::DUMP_OK;
82 }
83 
GetApplicationUidBytes()84 void TrafficDumper::GetApplicationUidBytes()
85 {
86     DUMPER_HILOGD(MODULE_SERVICE, "debug|GetApplicationUidBytes.\n");
87     DumpCommonUtils::PidInfo currentPidInfo;
88     currentPidInfo.pid_ = pid_;
89     currentPidInfo.uid_ = -1;
90     if (!DumpCommonUtils::GetProcessInfo(pid_, currentPidInfo)) {
91         DUMPER_HILOGE(MODULE_SERVICE, "GetProcUid failed, pid:%{public}d, uid:%{public}d.\n",
92             pid_, currentPidInfo.uid_);
93         return;
94     }
95     uint64_t receivedStats = 0;
96 #ifdef HIDUMPER_NETMANAGER_BASE_ENABLE
97     int32_t ret = DelayedSingleton<NetManagerStandard::NetStatsClient>::GetInstance()->GetUidRxBytes(
98         receivedStats, static_cast<uint32_t>(currentPidInfo.uid_));
99     if (ret != NetManagerStandard::NETMANAGER_SUCCESS) {
100         DUMPER_HILOGE(MODULE_SERVICE, "GetUidRxBytes failed, ret:%{public}d, uid:%{public}d.\n",
101             ret, currentPidInfo.uid_);
102         status_ = DumpStatus::DUMP_FAIL;
103     }
104 #endif
105     std::vector<std::string> line_vector;
106     line_vector.push_back(RECEIVED_BYTES + std::to_string(receivedStats));
107     result_->push_back(line_vector);
108 
109     uint64_t sendStats = 0;
110 #ifdef HIDUMPER_NETMANAGER_BASE_ENABLE
111     ret = DelayedSingleton<NetManagerStandard::NetStatsClient>::GetInstance()->GetUidTxBytes(
112         sendStats, static_cast<uint32_t>(currentPidInfo.uid_));
113     if (ret != NetManagerStandard::NETMANAGER_SUCCESS) {
114         DUMPER_HILOGE(MODULE_SERVICE, "GetUidTxBytes failed, ret:%{public}d, uid:%{public}d.\n",
115             ret, currentPidInfo.uid_);
116         status_ = DumpStatus::DUMP_FAIL;
117     }
118 #endif
119     line_vector.clear();
120     line_vector.push_back(SEND_BYTES + std::to_string(sendStats));
121     result_->push_back(line_vector);
122     status_ = DumpStatus::DUMP_OK;
123     DUMPER_HILOGD(MODULE_SERVICE, "debug|GetApplicationUidBytes end, pid:%{public}d, uid:%{public}d.\n",
124         pid_, currentPidInfo.uid_);
125 }
126 }  // namespace HiviewDFX
127 }  // namespace OHOS
128