Hide NetworkTemplate details, buffered stats I/O.
Move template matching to builder methods instead of exposing the
internal constants. Also rule to match Ethernet usage. Buffer
reading and writing of network stats, making operations 5x faster.
Change-Id: Iedb2d0ab3b26a976811c050f84a164e909eb74b6
diff --git a/core/java/android/net/NetworkTemplate.java b/core/java/android/net/NetworkTemplate.java
index 9381f1d..1ef0d9d 100644
--- a/core/java/android/net/NetworkTemplate.java
+++ b/core/java/android/net/NetworkTemplate.java
@@ -18,6 +18,7 @@
import static android.net.ConnectivityManager.TYPE_WIFI;
import static android.net.ConnectivityManager.TYPE_WIMAX;
+import static android.net.ConnectivityManager.TYPE_ETHERNET;
import static android.net.ConnectivityManager.isNetworkTypeMobile;
import static android.telephony.TelephonyManager.NETWORK_CLASS_2_G;
import static android.telephony.TelephonyManager.NETWORK_CLASS_3_G;
@@ -38,41 +39,69 @@
*/
public class NetworkTemplate implements Parcelable {
+ /** {@hide} */
+ public static final int MATCH_MOBILE_ALL = 1;
+ /** {@hide} */
+ public static final int MATCH_MOBILE_3G_LOWER = 2;
+ /** {@hide} */
+ public static final int MATCH_MOBILE_4G = 3;
+ /** {@hide} */
+ public static final int MATCH_WIFI = 4;
+ /** {@hide} */
+ public static final int MATCH_ETHERNET = 5;
+
/**
* Template to combine all {@link ConnectivityManager#TYPE_MOBILE} style
* networks together. Only uses statistics for requested IMSI.
*/
- public static final int MATCH_MOBILE_ALL = 1;
+ public static NetworkTemplate buildTemplateMobileAll(String subscriberId) {
+ return new NetworkTemplate(MATCH_MOBILE_ALL, subscriberId);
+ }
/**
* Template to combine all {@link ConnectivityManager#TYPE_MOBILE} style
* networks together that roughly meet a "3G" definition, or lower. Only
* uses statistics for requested IMSI.
*/
- public static final int MATCH_MOBILE_3G_LOWER = 2;
+ public static NetworkTemplate buildTemplateMobile3gLower(String subscriberId) {
+ return new NetworkTemplate(MATCH_MOBILE_3G_LOWER, subscriberId);
+ }
/**
* Template to combine all {@link ConnectivityManager#TYPE_MOBILE} style
* networks together that meet a "4G" definition. Only uses statistics for
* requested IMSI.
*/
- public static final int MATCH_MOBILE_4G = 3;
+ public static NetworkTemplate buildTemplateMobile4g(String subscriberId) {
+ return new NetworkTemplate(MATCH_MOBILE_4G, subscriberId);
+ }
/**
* Template to combine all {@link ConnectivityManager#TYPE_WIFI} style
* networks together.
*/
- public static final int MATCH_WIFI = 4;
+ public static NetworkTemplate buildTemplateWifi() {
+ return new NetworkTemplate(MATCH_WIFI, null);
+ }
- final int mMatchRule;
- final String mSubscriberId;
+ /**
+ * Template to combine all {@link ConnectivityManager#TYPE_ETHERNET} style
+ * networks together.
+ */
+ public static NetworkTemplate buildTemplateEthernet() {
+ return new NetworkTemplate(MATCH_ETHERNET, null);
+ }
+ private final int mMatchRule;
+ private final String mSubscriberId;
+
+ /** {@hide} */
public NetworkTemplate(int matchRule, String subscriberId) {
this.mMatchRule = matchRule;
this.mSubscriberId = subscriberId;
}
- public NetworkTemplate(Parcel in) {
+ private NetworkTemplate(Parcel in) {
mMatchRule = in.readInt();
mSubscriberId = in.readString();
}
@@ -110,10 +139,12 @@
return false;
}
+ /** {@hide} */
public int getMatchRule() {
return mMatchRule;
}
+ /** {@hide} */
public String getSubscriberId() {
return mSubscriberId;
}
@@ -131,6 +162,8 @@
return matchesMobile4g(ident);
case MATCH_WIFI:
return matchesWifi(ident);
+ case MATCH_ETHERNET:
+ return matchesEthernet(ident);
default:
throw new IllegalArgumentException("unknown network template");
}
@@ -190,7 +223,17 @@
return false;
}
- public static String getMatchRuleName(int matchRule) {
+ /**
+ * Check if matches Ethernet network template.
+ */
+ private boolean matchesEthernet(NetworkIdentity ident) {
+ if (ident.mType == TYPE_ETHERNET) {
+ return true;
+ }
+ return false;
+ }
+
+ private static String getMatchRuleName(int matchRule) {
switch (matchRule) {
case MATCH_MOBILE_3G_LOWER:
return "MOBILE_3G_LOWER";
@@ -200,6 +243,8 @@
return "MOBILE_ALL";
case MATCH_WIFI:
return "WIFI";
+ case MATCH_ETHERNET:
+ return "ETHERNET";
default:
return "UNKNOWN";
}
diff --git a/services/java/com/android/server/net/NetworkPolicyManagerService.java b/services/java/com/android/server/net/NetworkPolicyManagerService.java
index d30b66b..0c78fe7 100644
--- a/services/java/com/android/server/net/NetworkPolicyManagerService.java
+++ b/services/java/com/android/server/net/NetworkPolicyManagerService.java
@@ -24,8 +24,8 @@
import static android.Manifest.permission.READ_PHONE_STATE;
import static android.content.Intent.ACTION_UID_REMOVED;
import static android.content.Intent.EXTRA_UID;
+import static android.net.ConnectivityManager.ACTION_BACKGROUND_DATA_SETTING_CHANGED;
import static android.net.ConnectivityManager.CONNECTIVITY_ACTION;
-import static android.net.ConnectivityManager.*;
import static android.net.ConnectivityManager.TYPE_MOBILE;
import static android.net.NetworkPolicy.LIMIT_DISABLED;
import static android.net.NetworkPolicy.WARNING_DISABLED;
@@ -42,7 +42,7 @@
import static android.net.NetworkPolicyManager.isUidValidForPolicy;
import static android.net.NetworkTemplate.MATCH_MOBILE_3G_LOWER;
import static android.net.NetworkTemplate.MATCH_MOBILE_4G;
-import static android.net.NetworkTemplate.MATCH_MOBILE_ALL;
+import static android.net.NetworkTemplate.buildTemplateMobileAll;
import static android.text.format.DateUtils.DAY_IN_MILLIS;
import static com.android.internal.util.Preconditions.checkNotNull;
import static com.android.server.net.NetworkStatsService.ACTION_NETWORK_STATS_UPDATED;
@@ -678,7 +678,7 @@
time.setToNow();
final int cycleDay = time.monthDay;
- final NetworkTemplate template = new NetworkTemplate(MATCH_MOBILE_ALL, subscriberId);
+ final NetworkTemplate template = buildTemplateMobileAll(subscriberId);
mNetworkPolicy.add(
new NetworkPolicy(template, cycleDay, 4 * GB_IN_BYTES, LIMIT_DISABLED));
writePolicyLocked();
diff --git a/services/java/com/android/server/net/NetworkStatsService.java b/services/java/com/android/server/net/NetworkStatsService.java
index 54e94db..7ec6b81 100644
--- a/services/java/com/android/server/net/NetworkStatsService.java
+++ b/services/java/com/android/server/net/NetworkStatsService.java
@@ -73,11 +73,12 @@
import com.google.android.collect.Maps;
import com.google.android.collect.Sets;
+import java.io.BufferedInputStream;
+import java.io.BufferedOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileDescriptor;
-import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
@@ -719,10 +720,9 @@
// clear any existing stats and read from disk
mNetworkStats.clear();
- FileInputStream fis = null;
+ DataInputStream in = null;
try {
- fis = mNetworkFile.openRead();
- final DataInputStream in = new DataInputStream(fis);
+ in = new DataInputStream(new BufferedInputStream(mNetworkFile.openRead()));
// verify file magic header intact
final int magic = in.readInt();
@@ -751,7 +751,7 @@
} catch (IOException e) {
Slog.e(TAG, "problem reading network stats", e);
} finally {
- IoUtils.closeQuietly(fis);
+ IoUtils.closeQuietly(in);
}
}
@@ -768,10 +768,9 @@
// clear any existing stats and read from disk
mUidStats.clear();
- FileInputStream fis = null;
+ DataInputStream in = null;
try {
- fis = mUidFile.openRead();
- final DataInputStream in = new DataInputStream(fis);
+ in = new DataInputStream(new BufferedInputStream(mUidFile.openRead()));
// verify file magic header intact
final int magic = in.readInt();
@@ -826,7 +825,7 @@
} catch (IOException e) {
Slog.e(TAG, "problem reading uid stats", e);
} finally {
- IoUtils.closeQuietly(fis);
+ IoUtils.closeQuietly(in);
}
}
@@ -838,7 +837,7 @@
FileOutputStream fos = null;
try {
fos = mNetworkFile.startWrite();
- final DataOutputStream out = new DataOutputStream(fos);
+ final DataOutputStream out = new DataOutputStream(new BufferedOutputStream(fos));
out.writeInt(FILE_MAGIC);
out.writeInt(VERSION_NETWORK_INIT);
@@ -850,6 +849,7 @@
history.writeToStream(out);
}
+ out.flush();
mNetworkFile.finishWrite(fos);
} catch (IOException e) {
if (fos != null) {
@@ -871,7 +871,7 @@
FileOutputStream fos = null;
try {
fos = mUidFile.startWrite();
- final DataOutputStream out = new DataOutputStream(fos);
+ final DataOutputStream out = new DataOutputStream(new BufferedOutputStream(fos));
out.writeInt(FILE_MAGIC);
out.writeInt(VERSION_UID_WITH_TAG);
@@ -895,6 +895,7 @@
}
}
+ out.flush();
mUidFile.finishWrite(fos);
} catch (IOException e) {
if (fos != null) {
diff --git a/services/tests/servicestests/src/com/android/server/NetworkPolicyManagerServiceTest.java b/services/tests/servicestests/src/com/android/server/NetworkPolicyManagerServiceTest.java
index 33fd355..504ba42 100644
--- a/services/tests/servicestests/src/com/android/server/NetworkPolicyManagerServiceTest.java
+++ b/services/tests/servicestests/src/com/android/server/NetworkPolicyManagerServiceTest.java
@@ -27,7 +27,6 @@
import static android.net.NetworkPolicyManager.computeLastCycleBoundary;
import static android.net.NetworkStats.TAG_NONE;
import static android.net.NetworkStats.UID_ALL;
-import static android.net.NetworkTemplate.MATCH_WIFI;
import static org.easymock.EasyMock.anyInt;
import static org.easymock.EasyMock.aryEq;
import static org.easymock.EasyMock.capture;
@@ -88,7 +87,7 @@
private static final long TEST_START = 1194220800000L;
private static final String TEST_IFACE = "test0";
- private static NetworkTemplate sTemplateWifi = new NetworkTemplate(MATCH_WIFI, null);
+ private static NetworkTemplate sTemplateWifi = NetworkTemplate.buildTemplateWifi();
private BroadcastInterceptingContext mServiceContext;
private File mPolicyDir;
diff --git a/services/tests/servicestests/src/com/android/server/NetworkStatsServiceTest.java b/services/tests/servicestests/src/com/android/server/NetworkStatsServiceTest.java
index ac74063..bd80af9 100644
--- a/services/tests/servicestests/src/com/android/server/NetworkStatsServiceTest.java
+++ b/services/tests/servicestests/src/com/android/server/NetworkStatsServiceTest.java
@@ -25,8 +25,8 @@
import static android.net.NetworkStats.IFACE_ALL;
import static android.net.NetworkStats.TAG_NONE;
import static android.net.NetworkStats.UID_ALL;
-import static android.net.NetworkTemplate.MATCH_MOBILE_ALL;
-import static android.net.NetworkTemplate.MATCH_WIFI;
+import static android.net.NetworkTemplate.buildTemplateMobileAll;
+import static android.net.NetworkTemplate.buildTemplateWifi;
import static android.net.TrafficStats.UID_REMOVED;
import static android.text.format.DateUtils.DAY_IN_MILLIS;
import static android.text.format.DateUtils.HOUR_IN_MILLIS;
@@ -81,9 +81,9 @@
private static final String IMSI_1 = "310004";
private static final String IMSI_2 = "310260";
- private static NetworkTemplate sTemplateWifi = new NetworkTemplate(MATCH_WIFI, null);
- private static NetworkTemplate sTemplateImsi1 = new NetworkTemplate(MATCH_MOBILE_ALL, IMSI_1);
- private static NetworkTemplate sTemplateImsi2 = new NetworkTemplate(MATCH_MOBILE_ALL, IMSI_2);
+ private static NetworkTemplate sTemplateWifi = buildTemplateWifi();
+ private static NetworkTemplate sTemplateImsi1 = buildTemplateMobileAll(IMSI_1);
+ private static NetworkTemplate sTemplateImsi2 = buildTemplateMobileAll(IMSI_2);
private static final int UID_RED = 1001;
private static final int UID_BLUE = 1002;
@@ -290,7 +290,7 @@
mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL));
// verify service recorded history
- history = mService.getHistoryForNetwork(new NetworkTemplate(MATCH_WIFI, null));
+ history = mService.getHistoryForNetwork(sTemplateWifi);
assertValues(history, Long.MIN_VALUE, Long.MAX_VALUE, 512L, 512L);
assertEquals(HOUR_IN_MILLIS, history.getBucketDuration());
assertEquals(2, history.size());
@@ -307,7 +307,7 @@
mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL));
// verify identical stats, but spread across 4 buckets now
- history = mService.getHistoryForNetwork(new NetworkTemplate(MATCH_WIFI, null));
+ history = mService.getHistoryForNetwork(sTemplateWifi);
assertValues(history, Long.MIN_VALUE, Long.MAX_VALUE, 512L, 512L);
assertEquals(30 * MINUTE_IN_MILLIS, history.getBucketDuration());
assertEquals(4, history.size());