1 /* 2 * Copyright (C) 2019 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 package com.android.networkstack.metrics; 18 19 import android.net.captiveportal.CaptivePortalProbeResult; 20 import android.util.Log; 21 22 import androidx.annotation.NonNull; 23 import androidx.annotation.Nullable; 24 import androidx.annotation.VisibleForTesting; 25 26 import com.android.internal.util.HexDump; 27 import com.android.server.connectivity.nano.DataStallEventProto; 28 29 /** 30 * Collection of utilities for data stall metrics. 31 * 32 * To see if the logs are properly sent to statsd, execute following command. 33 * 34 * $ adb shell cmd stats print-logs 35 * $ adb logcat | grep statsd OR $ adb logcat -b stats 36 * 37 * @hide 38 */ 39 public class DataStallStatsUtils { 40 private static final String TAG = DataStallStatsUtils.class.getSimpleName(); 41 private static final boolean DBG = false; 42 43 /** 44 * Map {@link CaptivePortalProbeResult} to {@link DataStallEventProto}. 45 */ 46 @VisibleForTesting probeResultToEnum(@ullable final CaptivePortalProbeResult result)47 public static int probeResultToEnum(@Nullable final CaptivePortalProbeResult result) { 48 if (result == null) return DataStallEventProto.INVALID; 49 50 if (result.isSuccessful()) { 51 return DataStallEventProto.VALID; 52 } else if (result.isPortal()) { 53 return DataStallEventProto.PORTAL; 54 } else if (result.isPartialConnectivity()) { 55 return DataStallEventProto.PARTIAL; 56 } else { 57 return DataStallEventProto.INVALID; 58 } 59 } 60 61 /** 62 * Write the metric to {@link StatsLog}. 63 */ write(@onNull final DataStallDetectionStats stats, @NonNull final CaptivePortalProbeResult result)64 public static void write(@NonNull final DataStallDetectionStats stats, 65 @NonNull final CaptivePortalProbeResult result) { 66 int validationResult = probeResultToEnum(result); 67 if (DBG) { 68 Log.d(TAG, "write: " + stats + " with result: " + validationResult 69 + ", dns: " + HexDump.toHexString(stats.mDns)); 70 } 71 NetworkStackStatsLog.write(NetworkStackStatsLog.DATA_STALL_EVENT, 72 stats.mEvaluationType, 73 validationResult, 74 stats.mNetworkType, 75 stats.mWifiInfo, 76 stats.mCellularInfo, 77 stats.mDns, 78 stats.mTcpFailRate, 79 stats.mTcpSentSinceLastRecv); 80 } 81 } 82