blob: 8f419d8e126f7818d46c22a551fc154c20b05027 [file] [log] [blame]
Hugo Benichieab511b2016-09-09 09:23:47 +09001/*
2 * Copyright (C) 2016 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
17package com.android.server.connectivity;
18
19import android.content.Context;
20import android.net.ConnectivityMetricsEvent;
21import android.net.IIpConnectivityMetrics;
22import android.net.metrics.IpConnectivityLog;
23import android.os.IBinder;
24import android.os.Parcelable;
25import android.text.TextUtils;
26import android.util.Base64;
27import android.util.Log;
28import com.android.internal.annotations.GuardedBy;
29import com.android.internal.annotations.VisibleForTesting;
30import com.android.server.SystemService;
31import java.io.FileDescriptor;
32import java.io.IOException;
33import java.io.PrintWriter;
34import java.util.ArrayList;
35
36import static com.android.server.connectivity.metrics.IpConnectivityLogClass.IpConnectivityEvent;
37
38/** {@hide} */
39final public class IpConnectivityMetrics extends SystemService {
40 private static final String TAG = IpConnectivityMetrics.class.getSimpleName();
41 private static final boolean DBG = false;
42
43 private static final String SERVICE_NAME = IpConnectivityLog.SERVICE_NAME;
44
45 // Default size of the event buffer. Once the buffer is full, incoming events are dropped.
46 private static final int DEFAULT_BUFFER_SIZE = 2000;
47
48 // Lock ensuring that concurrent manipulations of the event buffer are correct.
49 // There are three concurrent operations to synchronize:
50 // - appending events to the buffer.
51 // - iterating throught the buffer.
52 // - flushing the buffer content and replacing it by a new buffer.
53 private final Object mLock = new Object();
54
55 @VisibleForTesting
56 public final Impl impl = new Impl();
57 @GuardedBy("mLock")
58 private ArrayList<ConnectivityMetricsEvent> mBuffer;
59 @GuardedBy("mLock")
60 private int mDropped;
61 @GuardedBy("mLock")
62 private int mCapacity;
63
64 public IpConnectivityMetrics(Context ctx) {
65 super(ctx);
66 initBuffer();
67 }
68
69 @Override
70 public void onStart() {
71 if (DBG) Log.d(TAG, "onStart");
72 }
73
74 @Override
75 public void onBootPhase(int phase) {
76 if (phase == SystemService.PHASE_SYSTEM_SERVICES_READY) {
77 if (DBG) Log.d(TAG, "onBootPhase");
78
79 publishBinderService(SERVICE_NAME, impl);
80 }
81 }
82
83 @VisibleForTesting
84 public int bufferCapacity() {
85 return DEFAULT_BUFFER_SIZE; // TODO: read from config
86 }
87
88 private void initBuffer() {
89 synchronized (mLock) {
90 mDropped = 0;
91 mCapacity = bufferCapacity();
92 mBuffer = new ArrayList<>(mCapacity);
93 }
94 }
95
96 private int append(ConnectivityMetricsEvent event) {
97 if (DBG) Log.d(TAG, "logEvent: " + event);
98 synchronized (mLock) {
99 final int left = mCapacity - mBuffer.size();
100 if (event == null) {
101 return left;
102 }
103 if (left == 0) {
104 mDropped++;
105 return 0;
106 }
107 mBuffer.add(event);
108 return left - 1;
109 }
110 }
111
112 private String flushEncodedOutput() {
113 final ArrayList<ConnectivityMetricsEvent> events;
114 final int dropped;
115 synchronized (mLock) {
116 events = mBuffer;
117 dropped = mDropped;
118 initBuffer();
119 }
120
121 final byte[] data;
122 try {
123 data = IpConnectivityEventBuilder.serialize(dropped, events);
124 } catch (IOException e) {
125 Log.e(TAG, "could not serialize events", e);
126 return "";
127 }
128
129 return Base64.encodeToString(data, Base64.DEFAULT);
130 }
131
132 /**
133 * Clears the event buffer and prints its content as a protobuf serialized byte array
134 * inside a base64 encoded string.
135 */
136 private void cmdFlush(FileDescriptor fd, PrintWriter pw, String[] args) {
137 pw.print(flushEncodedOutput());
138 }
139
140 /**
141 * Prints the content of the event buffer, either using the events ASCII representation
142 * or using protobuf text format.
143 */
144 private void cmdList(FileDescriptor fd, PrintWriter pw, String[] args) {
145 final ArrayList<ConnectivityMetricsEvent> events;
146 synchronized (mLock) {
147 events = new ArrayList(mBuffer);
148 }
149
150 if (args.length > 1 && args[1].equals("proto")) {
151 for (IpConnectivityEvent ev : IpConnectivityEventBuilder.toProto(events)) {
152 pw.print(ev.toString());
153 }
154 return;
155 }
156
157 for (ConnectivityMetricsEvent ev : events) {
158 pw.println(ev.toString());
159 }
160 }
161
162 private void cmdStats(FileDescriptor fd, PrintWriter pw, String[] args) {
163 synchronized (mLock) {
164 pw.println("Buffered events: " + mBuffer.size());
165 pw.println("Buffer capacity: " + mCapacity);
166 pw.println("Dropped events: " + mDropped);
167 }
168 }
169
170 private void cmdDefault(FileDescriptor fd, PrintWriter pw, String[] args) {
171 if (args.length == 0) {
172 pw.println("No command");
173 return;
174 }
175 pw.println("Unknown command " + TextUtils.join(" ", args));
176 }
177
178 public final class Impl extends IIpConnectivityMetrics.Stub {
179 static final String CMD_FLUSH = "flush";
180 static final String CMD_LIST = "list";
181 static final String CMD_STATS = "stats";
182 static final String CMD_DEFAULT = CMD_STATS;
183
184 @Override
185 public int logEvent(ConnectivityMetricsEvent event) {
186 enforceConnectivityInternalPermission();
187 return append(event);
188 }
189
190 @Override
191 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
192 enforceDumpPermission();
193 if (DBG) Log.d(TAG, "dumpsys " + TextUtils.join(" ", args));
194 final String cmd = (args.length > 0) ? args[0] : CMD_DEFAULT;
195 switch (cmd) {
196 case CMD_FLUSH:
197 cmdFlush(fd, pw, args);
198 return;
199 case CMD_LIST:
200 cmdList(fd, pw, args);
201 return;
202 case CMD_STATS:
203 cmdStats(fd, pw, args);
204 return;
205 default:
206 cmdDefault(fd, pw, args);
207 }
208 }
209
210 private void enforceConnectivityInternalPermission() {
211 enforcePermission(android.Manifest.permission.CONNECTIVITY_INTERNAL);
212 }
213
214 private void enforceDumpPermission() {
215 enforcePermission(android.Manifest.permission.DUMP);
216 }
217
218 private void enforcePermission(String what) {
219 getContext().enforceCallingOrSelfPermission(what, "IpConnectivityMetrics");
220 }
221 };
222}