Hugo Benichi | 7b314e1 | 2018-01-15 21:54:00 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #define LOG_TAG "TcpSocketMonitor" |
| 18 | |
Bernie Innocenti | 1bcc25a | 2018-08-10 17:15:00 +0900 | [diff] [blame] | 19 | #include <chrono> |
| 20 | #include <cinttypes> |
Hugo Benichi | a9e3c5d | 2018-01-18 10:33:22 +0900 | [diff] [blame] | 21 | #include <thread> |
Hugo Benichi | 4596d2f | 2018-01-19 13:36:29 +0900 | [diff] [blame] | 22 | #include <vector> |
Hugo Benichi | 7b314e1 | 2018-01-15 21:54:00 +0900 | [diff] [blame] | 23 | |
| 24 | #include <arpa/inet.h> |
| 25 | #include <netinet/tcp.h> |
| 26 | #include <linux/tcp.h> |
| 27 | |
Hugo Benichi | 8c39767 | 2018-01-22 22:06:15 +0900 | [diff] [blame] | 28 | #include "Controllers.h" |
Hugo Benichi | a9e3c5d | 2018-01-18 10:33:22 +0900 | [diff] [blame] | 29 | #include "DumpWriter.h" |
Hugo Benichi | a9e3c5d | 2018-01-18 10:33:22 +0900 | [diff] [blame] | 30 | #include "SockDiag.h" |
| 31 | #include "TcpSocketMonitor.h" |
| 32 | |
Hugo Benichi | 7b314e1 | 2018-01-15 21:54:00 +0900 | [diff] [blame] | 33 | namespace android { |
| 34 | namespace net { |
| 35 | |
Hugo Benichi | a9e3c5d | 2018-01-18 10:33:22 +0900 | [diff] [blame] | 36 | using std::chrono::duration_cast; |
Hugo Benichi | a9e3c5d | 2018-01-18 10:33:22 +0900 | [diff] [blame] | 37 | using std::chrono::steady_clock; |
| 38 | |
Hugo Benichi | 7b314e1 | 2018-01-15 21:54:00 +0900 | [diff] [blame] | 39 | constexpr const char* getTcpStateName(int t) { |
| 40 | switch (t) { |
| 41 | case TCP_ESTABLISHED: |
| 42 | return "ESTABLISHED"; |
| 43 | case TCP_SYN_SENT: |
| 44 | return "SYN-SENT"; |
| 45 | case TCP_SYN_RECV: |
| 46 | return "SYN-RECV"; |
| 47 | case TCP_FIN_WAIT1: |
| 48 | return "FIN-WAIT-1"; |
| 49 | case TCP_FIN_WAIT2: |
| 50 | return "FIN-WAIT-2"; |
| 51 | case TCP_TIME_WAIT: |
| 52 | return "TIME-WAIT"; |
| 53 | case TCP_CLOSE: |
| 54 | return "CLOSE"; |
| 55 | case TCP_CLOSE_WAIT: |
| 56 | return "CLOSE-WAIT"; |
| 57 | case TCP_LAST_ACK: |
| 58 | return "LAST-ACK"; |
| 59 | case TCP_LISTEN: |
| 60 | return "LISTEN"; |
| 61 | case TCP_CLOSING: |
| 62 | return "CLOSING"; |
| 63 | default: |
| 64 | return "UNKNOWN"; |
| 65 | } |
| 66 | } |
| 67 | |
Hugo Benichi | 54bfc7c | 2018-01-23 14:16:52 +0900 | [diff] [blame] | 68 | // Helper macro for reading fields into struct tcp_info and handling different struct tcp_info |
| 69 | // versions in the kernel. |
Hugo Benichi | 321b22c | 2018-01-30 11:37:27 +0900 | [diff] [blame] | 70 | #define TCPINFO_GET(ptr, fld, len, zero) \ |
| 71 | (((ptr) != nullptr && (offsetof(struct tcp_info, fld) + sizeof((ptr)->fld)) < len) ? \ |
| 72 | (ptr)->fld : zero) |
Hugo Benichi | 54bfc7c | 2018-01-23 14:16:52 +0900 | [diff] [blame] | 73 | |
Hugo Benichi | cbaa36b | 2018-01-17 12:11:43 +0900 | [diff] [blame] | 74 | static void tcpInfoPrint(DumpWriter &dw, Fwmark mark, const struct inet_diag_msg *sockinfo, |
Hugo Benichi | 54bfc7c | 2018-01-23 14:16:52 +0900 | [diff] [blame] | 75 | const struct tcp_info *tcpinfo, uint32_t tcpinfoLen) { |
Hugo Benichi | a9e3c5d | 2018-01-18 10:33:22 +0900 | [diff] [blame] | 76 | char saddr[INET6_ADDRSTRLEN] = {}; |
| 77 | char daddr[INET6_ADDRSTRLEN] = {}; |
| 78 | inet_ntop(sockinfo->idiag_family, &(sockinfo->id.idiag_src), saddr, sizeof(saddr)); |
| 79 | inet_ntop(sockinfo->idiag_family, &(sockinfo->id.idiag_dst), daddr, sizeof(daddr)); |
Hugo Benichi | 7b314e1 | 2018-01-15 21:54:00 +0900 | [diff] [blame] | 80 | |
Hugo Benichi | a9e3c5d | 2018-01-18 10:33:22 +0900 | [diff] [blame] | 81 | dw.println( |
| 82 | "netId=%d uid=%u mark=0x%x saddr=%s daddr=%s sport=%u dport=%u tcp_state=%s(%u) " |
Hugo Benichi | 4596d2f | 2018-01-19 13:36:29 +0900 | [diff] [blame] | 83 | "rtt=%gms sent=%u lost=%u", |
Hugo Benichi | a9e3c5d | 2018-01-18 10:33:22 +0900 | [diff] [blame] | 84 | mark.netId, |
| 85 | sockinfo->idiag_uid, |
| 86 | mark.intValue, |
| 87 | saddr, |
| 88 | daddr, |
| 89 | ntohs(sockinfo->id.idiag_sport), |
| 90 | ntohs(sockinfo->id.idiag_dport), |
| 91 | getTcpStateName(sockinfo->idiag_state), sockinfo->idiag_state, |
Hugo Benichi | 321b22c | 2018-01-30 11:37:27 +0900 | [diff] [blame] | 92 | TCPINFO_GET(tcpinfo, tcpi_rtt, tcpinfoLen, 0) / 1000.0, |
| 93 | TCPINFO_GET(tcpinfo, tcpi_segs_out, tcpinfoLen, 0), |
| 94 | TCPINFO_GET(tcpinfo, tcpi_lost, tcpinfoLen, 0)); |
Hugo Benichi | 7b314e1 | 2018-01-15 21:54:00 +0900 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | const String16 TcpSocketMonitor::DUMP_KEYWORD = String16("tcp_socket_info"); |
Hugo Benichi | a9e3c5d | 2018-01-18 10:33:22 +0900 | [diff] [blame] | 98 | const milliseconds TcpSocketMonitor::kDefaultPollingInterval = milliseconds(30000); |
Hugo Benichi | 7b314e1 | 2018-01-15 21:54:00 +0900 | [diff] [blame] | 99 | |
| 100 | void TcpSocketMonitor::dump(DumpWriter& dw) { |
Bernie Innocenti | abf8a34 | 2018-08-10 15:17:16 +0900 | [diff] [blame] | 101 | std::lock_guard guard(mLock); |
Hugo Benichi | a9e3c5d | 2018-01-18 10:33:22 +0900 | [diff] [blame] | 102 | |
Hugo Benichi | 7b314e1 | 2018-01-15 21:54:00 +0900 | [diff] [blame] | 103 | dw.println("TcpSocketMonitor"); |
Erik Kline | b31fd69 | 2018-06-06 20:50:11 +0900 | [diff] [blame] | 104 | ScopedIndent tcpSocketMonitorDetails(dw); |
Hugo Benichi | 7b314e1 | 2018-01-15 21:54:00 +0900 | [diff] [blame] | 105 | |
Hugo Benichi | a9e3c5d | 2018-01-18 10:33:22 +0900 | [diff] [blame] | 106 | const auto now = steady_clock::now(); |
| 107 | const auto d = duration_cast<milliseconds>(now - mLastPoll); |
Hugo Benichi | 4596d2f | 2018-01-19 13:36:29 +0900 | [diff] [blame] | 108 | dw.println("running=%d, suspended=%d, last poll %lld ms ago", |
| 109 | mIsRunning, mIsSuspended, d.count()); |
Hugo Benichi | a9e3c5d | 2018-01-18 10:33:22 +0900 | [diff] [blame] | 110 | |
Hugo Benichi | 4596d2f | 2018-01-19 13:36:29 +0900 | [diff] [blame] | 111 | if (!mNetworkStats.empty()) { |
| 112 | dw.blankline(); |
| 113 | dw.println("Network stats:"); |
Bernie Innocenti | 1bcc25a | 2018-08-10 17:15:00 +0900 | [diff] [blame] | 114 | for (const std::pair<uint32_t, TcpStats>& stats : mNetworkStats) { |
Hugo Benichi | 4596d2f | 2018-01-19 13:36:29 +0900 | [diff] [blame] | 115 | if (stats.second.nSockets == 0) { |
| 116 | continue; |
| 117 | } |
Erik Kline | b31fd69 | 2018-06-06 20:50:11 +0900 | [diff] [blame] | 118 | dw.println("netId=%d sent=%d lost=%d rttMs=%gms sentAckDiff=%dms", |
| 119 | stats.first, |
| 120 | stats.second.sent, |
| 121 | stats.second.lost, |
| 122 | stats.second.rttUs / 1000.0 / stats.second.nSockets, |
| 123 | stats.second.sentAckDiffMs / stats.second.nSockets); |
Hugo Benichi | 4596d2f | 2018-01-19 13:36:29 +0900 | [diff] [blame] | 124 | } |
Hugo Benichi | 7b314e1 | 2018-01-15 21:54:00 +0900 | [diff] [blame] | 125 | } |
| 126 | |
Hugo Benichi | 4596d2f | 2018-01-19 13:36:29 +0900 | [diff] [blame] | 127 | if (!mSocketEntries.empty()) { |
| 128 | dw.blankline(); |
| 129 | dw.println("Socket entries:"); |
Bernie Innocenti | 1bcc25a | 2018-08-10 17:15:00 +0900 | [diff] [blame] | 130 | for (const std::pair<uint64_t, SocketEntry>& stats : mSocketEntries) { |
| 131 | dw.println("netId=%u uid=%u cookie=%" PRIu64, stats.second.mark.netId, stats.second.uid, |
| 132 | stats.first); |
Hugo Benichi | 4596d2f | 2018-01-19 13:36:29 +0900 | [diff] [blame] | 133 | } |
| 134 | } |
Hugo Benichi | 7b314e1 | 2018-01-15 21:54:00 +0900 | [diff] [blame] | 135 | |
Hugo Benichi | 4596d2f | 2018-01-19 13:36:29 +0900 | [diff] [blame] | 136 | SockDiag sd; |
| 137 | if (sd.open()) { |
| 138 | dw.blankline(); |
| 139 | dw.println("Current socket dump:"); |
| 140 | const auto tcpInfoReader = [&dw](Fwmark mark, const struct inet_diag_msg *sockinfo, |
| 141 | const struct tcp_info *tcpinfo, uint32_t tcpinfoLen) { |
| 142 | tcpInfoPrint(dw, mark, sockinfo, tcpinfo, tcpinfoLen); |
| 143 | }; |
| 144 | |
| 145 | if (int ret = sd.getLiveTcpInfos(tcpInfoReader)) { |
| 146 | ALOGE("Failed to dump TCP socket info: %s", strerror(-ret)); |
| 147 | } |
| 148 | } else { |
| 149 | ALOGE("Error opening sock diag for dumping TCP socket info"); |
Hugo Benichi | 7b314e1 | 2018-01-15 21:54:00 +0900 | [diff] [blame] | 150 | } |
Hugo Benichi | 7b314e1 | 2018-01-15 21:54:00 +0900 | [diff] [blame] | 151 | } |
| 152 | |
Hugo Benichi | a9e3c5d | 2018-01-18 10:33:22 +0900 | [diff] [blame] | 153 | void TcpSocketMonitor::setPollingInterval(milliseconds nextSleepDurationMs) { |
Bernie Innocenti | abf8a34 | 2018-08-10 15:17:16 +0900 | [diff] [blame] | 154 | std::lock_guard guard(mLock); |
Hugo Benichi | a9e3c5d | 2018-01-18 10:33:22 +0900 | [diff] [blame] | 155 | |
| 156 | mNextSleepDurationMs = nextSleepDurationMs; |
| 157 | |
| 158 | ALOGD("tcpinfo polling interval set to %lld ms", mNextSleepDurationMs.count()); |
| 159 | } |
| 160 | |
| 161 | void TcpSocketMonitor::resumePolling() { |
Hugo Benichi | 4596d2f | 2018-01-19 13:36:29 +0900 | [diff] [blame] | 162 | bool wasSuspended; |
Hugo Benichi | a9e3c5d | 2018-01-18 10:33:22 +0900 | [diff] [blame] | 163 | { |
Bernie Innocenti | abf8a34 | 2018-08-10 15:17:16 +0900 | [diff] [blame] | 164 | std::lock_guard guard(mLock); |
Hugo Benichi | a9e3c5d | 2018-01-18 10:33:22 +0900 | [diff] [blame] | 165 | |
Hugo Benichi | 4596d2f | 2018-01-19 13:36:29 +0900 | [diff] [blame] | 166 | wasSuspended = mIsSuspended; |
Hugo Benichi | a9e3c5d | 2018-01-18 10:33:22 +0900 | [diff] [blame] | 167 | mIsSuspended = false; |
Hugo Benichi | 4596d2f | 2018-01-19 13:36:29 +0900 | [diff] [blame] | 168 | ALOGD("resuming tcpinfo polling (interval=%lldms)", mNextSleepDurationMs.count()); |
Hugo Benichi | a9e3c5d | 2018-01-18 10:33:22 +0900 | [diff] [blame] | 169 | } |
| 170 | |
Hugo Benichi | 4596d2f | 2018-01-19 13:36:29 +0900 | [diff] [blame] | 171 | if (wasSuspended) { |
| 172 | mCv.notify_all(); |
| 173 | } |
Hugo Benichi | a9e3c5d | 2018-01-18 10:33:22 +0900 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | void TcpSocketMonitor::suspendPolling() { |
Bernie Innocenti | abf8a34 | 2018-08-10 15:17:16 +0900 | [diff] [blame] | 177 | std::lock_guard guard(mLock); |
Hugo Benichi | a9e3c5d | 2018-01-18 10:33:22 +0900 | [diff] [blame] | 178 | |
Hugo Benichi | 4596d2f | 2018-01-19 13:36:29 +0900 | [diff] [blame] | 179 | bool wasSuspended = mIsSuspended; |
| 180 | mIsSuspended = true; |
| 181 | ALOGD("suspending tcpinfo polling"); |
| 182 | |
| 183 | if (!wasSuspended) { |
| 184 | mSocketEntries.clear(); |
Hugo Benichi | a9e3c5d | 2018-01-18 10:33:22 +0900 | [diff] [blame] | 185 | } |
| 186 | } |
| 187 | |
| 188 | void TcpSocketMonitor::poll() { |
Bernie Innocenti | abf8a34 | 2018-08-10 15:17:16 +0900 | [diff] [blame] | 189 | std::lock_guard guard(mLock); |
Hugo Benichi | a9e3c5d | 2018-01-18 10:33:22 +0900 | [diff] [blame] | 190 | |
| 191 | if (mIsSuspended) { |
| 192 | return; |
| 193 | } |
| 194 | |
Hugo Benichi | a9e3c5d | 2018-01-18 10:33:22 +0900 | [diff] [blame] | 195 | SockDiag sd; |
| 196 | if (!sd.open()) { |
| 197 | ALOGE("Error opening sock diag for polling TCP socket info"); |
| 198 | return; |
| 199 | } |
| 200 | |
Hugo Benichi | 4596d2f | 2018-01-19 13:36:29 +0900 | [diff] [blame] | 201 | const auto now = steady_clock::now(); |
| 202 | const auto tcpInfoReader = [this, now](Fwmark mark, const struct inet_diag_msg *sockinfo, |
| 203 | const struct tcp_info *tcpinfo, |
| 204 | uint32_t tcpinfoLen) NO_THREAD_SAFETY_ANALYSIS { |
Hugo Benichi | 54bfc7c | 2018-01-23 14:16:52 +0900 | [diff] [blame] | 205 | if (sockinfo == nullptr || tcpinfo == nullptr || tcpinfoLen == 0 || mark.intValue == 0) { |
Hugo Benichi | a9e3c5d | 2018-01-18 10:33:22 +0900 | [diff] [blame] | 206 | return; |
| 207 | } |
Hugo Benichi | 4596d2f | 2018-01-19 13:36:29 +0900 | [diff] [blame] | 208 | updateSocketStats(now, mark, sockinfo, tcpinfo, tcpinfoLen); |
Hugo Benichi | a9e3c5d | 2018-01-18 10:33:22 +0900 | [diff] [blame] | 209 | }; |
| 210 | |
Hugo Benichi | 4596d2f | 2018-01-19 13:36:29 +0900 | [diff] [blame] | 211 | // Reset mNetworkStats |
| 212 | mNetworkStats.clear(); |
| 213 | |
Hugo Benichi | a9e3c5d | 2018-01-18 10:33:22 +0900 | [diff] [blame] | 214 | if (int ret = sd.getLiveTcpInfos(tcpInfoReader)) { |
| 215 | ALOGE("Failed to poll TCP socket info: %s", strerror(-ret)); |
| 216 | return; |
| 217 | } |
| 218 | |
Hugo Benichi | 4596d2f | 2018-01-19 13:36:29 +0900 | [diff] [blame] | 219 | // Remove any SocketEntry not updated |
| 220 | for (auto it = mSocketEntries.cbegin(); it != mSocketEntries.cend();) { |
| 221 | if (it->second.lastUpdate < now) { |
| 222 | it = mSocketEntries.erase(it); |
| 223 | } else { |
| 224 | it++; |
| 225 | } |
| 226 | } |
Hugo Benichi | 8c39767 | 2018-01-22 22:06:15 +0900 | [diff] [blame] | 227 | |
| 228 | const auto listener = gCtls->eventReporter.getNetdEventListener(); |
| 229 | if (listener != nullptr) { |
| 230 | std::vector<int> netIds; |
| 231 | std::vector<int> sentPackets; |
| 232 | std::vector<int> lostPackets; |
| 233 | std::vector<int> rtts; |
| 234 | std::vector<int> sentAckDiffs; |
| 235 | for (auto const& stats : mNetworkStats) { |
| 236 | int32_t nSockets = stats.second.nSockets; |
| 237 | if (nSockets == 0) { |
| 238 | continue; |
| 239 | } |
| 240 | netIds.push_back(stats.first); |
| 241 | sentPackets.push_back(stats.second.sent); |
| 242 | lostPackets.push_back(stats.second.lost); |
| 243 | rtts.push_back(stats.second.rttUs / nSockets); |
| 244 | sentAckDiffs.push_back(stats.second.sentAckDiffMs / nSockets); |
| 245 | } |
| 246 | listener->onTcpSocketStatsEvent(netIds, sentPackets, lostPackets, rtts, sentAckDiffs); |
| 247 | } |
| 248 | |
Hugo Benichi | a9e3c5d | 2018-01-18 10:33:22 +0900 | [diff] [blame] | 249 | mLastPoll = now; |
| 250 | } |
| 251 | |
| 252 | void TcpSocketMonitor::waitForNextPoll() { |
| 253 | bool isSuspended; |
| 254 | milliseconds nextSleepDurationMs; |
| 255 | { |
Bernie Innocenti | abf8a34 | 2018-08-10 15:17:16 +0900 | [diff] [blame] | 256 | std::lock_guard guard(mLock); |
Hugo Benichi | a9e3c5d | 2018-01-18 10:33:22 +0900 | [diff] [blame] | 257 | isSuspended = mIsSuspended; |
| 258 | nextSleepDurationMs= mNextSleepDurationMs; |
| 259 | } |
| 260 | |
| 261 | std::unique_lock<std::mutex> ul(mLock); |
| 262 | if (isSuspended) { |
| 263 | mCv.wait(ul); |
| 264 | } else { |
| 265 | mCv.wait_for(ul, nextSleepDurationMs); |
| 266 | } |
| 267 | } |
| 268 | |
| 269 | bool TcpSocketMonitor::isRunning() { |
Bernie Innocenti | abf8a34 | 2018-08-10 15:17:16 +0900 | [diff] [blame] | 270 | std::lock_guard guard(mLock); |
Hugo Benichi | a9e3c5d | 2018-01-18 10:33:22 +0900 | [diff] [blame] | 271 | return mIsRunning; |
| 272 | } |
| 273 | |
Hugo Benichi | 4596d2f | 2018-01-19 13:36:29 +0900 | [diff] [blame] | 274 | void TcpSocketMonitor::updateSocketStats(time_point now, Fwmark mark, |
| 275 | const struct inet_diag_msg *sockinfo, |
| 276 | const struct tcp_info *tcpinfo, |
| 277 | uint32_t tcpinfoLen) NO_THREAD_SAFETY_ANALYSIS { |
Hugo Benichi | 321b22c | 2018-01-30 11:37:27 +0900 | [diff] [blame] | 278 | int32_t lastAck = TCPINFO_GET(tcpinfo, tcpi_last_ack_recv, tcpinfoLen, 0); |
| 279 | int32_t lastSent = TCPINFO_GET(tcpinfo, tcpi_last_data_sent, tcpinfoLen, 0); |
Hugo Benichi | 4596d2f | 2018-01-19 13:36:29 +0900 | [diff] [blame] | 280 | TcpStats diff = { |
Hugo Benichi | 321b22c | 2018-01-30 11:37:27 +0900 | [diff] [blame] | 281 | .sent = TCPINFO_GET(tcpinfo, tcpi_segs_out, tcpinfoLen, 0), |
| 282 | .lost = TCPINFO_GET(tcpinfo, tcpi_lost, tcpinfoLen, 0), |
| 283 | .rttUs = TCPINFO_GET(tcpinfo, tcpi_rtt, tcpinfoLen, 0), |
Hugo Benichi | 4596d2f | 2018-01-19 13:36:29 +0900 | [diff] [blame] | 284 | .sentAckDiffMs = lastAck - lastSent, |
| 285 | .nSockets = 1, |
| 286 | }; |
| 287 | |
| 288 | { |
| 289 | // Update socket stats with the newest entry, computing the diff w.r.t the previous entry. |
| 290 | const uint64_t cookie = (static_cast<uint64_t>(sockinfo->id.idiag_cookie[0]) << 32) |
| 291 | | static_cast<uint64_t>(sockinfo->id.idiag_cookie[1]); |
| 292 | const SocketEntry previous = mSocketEntries[cookie]; |
| 293 | mSocketEntries[cookie] = { |
| 294 | .sent = diff.sent, |
| 295 | .lost = diff.lost, |
| 296 | .lastUpdate = now, |
| 297 | .mark = mark, |
| 298 | .uid = sockinfo->idiag_uid, |
| 299 | }; |
| 300 | |
| 301 | diff.sent -= previous.sent; |
| 302 | diff.lost -= previous.lost; |
| 303 | } |
| 304 | |
| 305 | { |
| 306 | // Aggregate the diff per network id. |
| 307 | auto& stats = mNetworkStats[mark.netId]; |
| 308 | stats.sent += diff.sent; |
| 309 | stats.lost += diff.lost; |
| 310 | stats.rttUs += diff.rttUs; |
| 311 | stats.sentAckDiffMs += diff.sentAckDiffMs; |
| 312 | stats.nSockets += diff.nSockets; |
| 313 | } |
| 314 | } |
| 315 | |
Hugo Benichi | a9e3c5d | 2018-01-18 10:33:22 +0900 | [diff] [blame] | 316 | TcpSocketMonitor::TcpSocketMonitor() { |
Bernie Innocenti | abf8a34 | 2018-08-10 15:17:16 +0900 | [diff] [blame] | 317 | std::lock_guard guard(mLock); |
Hugo Benichi | a9e3c5d | 2018-01-18 10:33:22 +0900 | [diff] [blame] | 318 | |
| 319 | mNextSleepDurationMs = kDefaultPollingInterval; |
Hugo Benichi | a9e3c5d | 2018-01-18 10:33:22 +0900 | [diff] [blame] | 320 | mIsRunning = true; |
Hugo Benichi | 4596d2f | 2018-01-19 13:36:29 +0900 | [diff] [blame] | 321 | mIsSuspended = true; |
Hugo Benichi | a9e3c5d | 2018-01-18 10:33:22 +0900 | [diff] [blame] | 322 | mPollingThread = std::thread([this] { |
Lorenzo Colitti | 15fc2d3 | 2018-01-26 10:07:20 +0900 | [diff] [blame] | 323 | (void) this; |
Hugo Benichi | a9e3c5d | 2018-01-18 10:33:22 +0900 | [diff] [blame] | 324 | while (isRunning()) { |
| 325 | poll(); |
| 326 | waitForNextPoll(); |
| 327 | } |
| 328 | }); |
| 329 | } |
| 330 | |
| 331 | TcpSocketMonitor::~TcpSocketMonitor() { |
| 332 | { |
Bernie Innocenti | abf8a34 | 2018-08-10 15:17:16 +0900 | [diff] [blame] | 333 | std::lock_guard guard(mLock); |
Hugo Benichi | a9e3c5d | 2018-01-18 10:33:22 +0900 | [diff] [blame] | 334 | mIsRunning = false; |
| 335 | mIsSuspended = true; |
| 336 | } |
| 337 | mCv.notify_all(); |
| 338 | mPollingThread.join(); |
| 339 | } |
| 340 | |
Hugo Benichi | 7b314e1 | 2018-01-15 21:54:00 +0900 | [diff] [blame] | 341 | } // namespace net |
| 342 | } // namespace android |