Move NetworkStatsFactory into service directory

In order to notify netd to swap eBPF maps before pulling the
networkStats from eBPF maps, NetworkStatsFactory need to use the
NetdServices to issue binder calls. So it need to be moved from
framework/base/core to framework/base/service since object in
framework/base/core cannot get any system services. This change is also
necessary for setting up a lock inside NetworkStatsFactory to prevent
racing between two netstats caller since the lock need to be hold before
netd trigger the map swap.

Also fix the compile problem caused by moving the NetworkStatsFactory
and the related tests. Rename the packages and the jni functions to a
more proper name.

Bug: 124764595
Bug: 128900919
Test: NetworkStatsFactoryTest
      android.app.usage.cts.NetworkUsageStatsTest
      android.net.cts.TrafficStatsTest

Merged-In: Ifcfe4df81caf8ede2e4e66a76552cb3200378fa8
Change-Id: Ifcfe4df81caf8ede2e4e66a76552cb3200378fa8
This commit is contained in:
Chenbo Feng
2019-03-01 15:07:24 -08:00
parent 7754d368f4
commit f2f1f27098
14 changed files with 183 additions and 35 deletions

View File

@@ -196,7 +196,6 @@ cc_library_shared {
"android_content_res_Configuration.cpp",
"android_animation_PropertyValuesHolder.cpp",
"android_security_Scrypt.cpp",
"com_android_internal_net_NetworkStatsFactory.cpp",
"com_android_internal_os_ClassLoaderFactory.cpp",
"com_android_internal_os_FuseAppLoop.cpp",
"com_android_internal_os_Zygote.cpp",

View File

@@ -213,7 +213,6 @@ extern int register_android_content_res_Configuration(JNIEnv* env);
extern int register_android_animation_PropertyValuesHolder(JNIEnv *env);
extern int register_android_security_Scrypt(JNIEnv *env);
extern int register_com_android_internal_content_NativeLibraryHelper(JNIEnv *env);
extern int register_com_android_internal_net_NetworkStatsFactory(JNIEnv *env);
extern int register_com_android_internal_os_ClassLoaderFactory(JNIEnv* env);
extern int register_com_android_internal_os_FuseAppLoop(JNIEnv* env);
extern int register_com_android_internal_os_Zygote(JNIEnv *env);
@@ -1538,7 +1537,6 @@ static const RegJNIRec gRegJNI[] = {
REG_JNI(register_android_animation_PropertyValuesHolder),
REG_JNI(register_android_security_Scrypt),
REG_JNI(register_com_android_internal_content_NativeLibraryHelper),
REG_JNI(register_com_android_internal_net_NetworkStatsFactory),
REG_JNI(register_com_android_internal_os_FuseAppLoop),
};

View File

@@ -88,10 +88,10 @@ import android.util.SparseIntArray;
import com.android.internal.annotations.GuardedBy;
import com.android.internal.annotations.VisibleForTesting;
import com.android.internal.app.IBatteryStats;
import com.android.internal.net.NetworkStatsFactory;
import com.android.internal.util.DumpUtils;
import com.android.internal.util.HexDump;
import com.android.internal.util.Preconditions;
import com.android.server.net.NetworkStatsFactory;
import com.google.android.collect.Maps;

View File

@@ -14,7 +14,7 @@
* limitations under the License.
*/
package com.android.internal.net;
package com.android.server.net;
import static android.net.NetworkStats.SET_ALL;
import static android.net.NetworkStats.TAG_ALL;

View File

@@ -129,7 +129,6 @@ import android.util.proto.ProtoOutputStream;
import com.android.internal.annotations.GuardedBy;
import com.android.internal.annotations.VisibleForTesting;
import com.android.internal.net.NetworkStatsFactory;
import com.android.internal.net.VpnInfo;
import com.android.internal.util.ArrayUtils;
import com.android.internal.util.DumpUtils;

View File

@@ -51,6 +51,7 @@ cc_library_static {
"com_android_server_PersistentDataBlockService.cpp",
"com_android_server_GraphicsStatsService.cpp",
"onload.cpp",
":lib_networkStatsFactory_native",
],
include_dirs: [
@@ -140,3 +141,10 @@ cc_defaults {
}
}
}
filegroup {
name: "lib_networkStatsFactory_native",
srcs: [
"com_android_server_net_NetworkStatsFactory.cpp",
],
}

View File

@@ -21,9 +21,9 @@
#include <sys/stat.h>
#include <sys/types.h>
#include <core_jni_helpers.h>
#include <jni.h>
#include <nativehelper/JNIHelp.h>
#include <nativehelper/ScopedUtfChars.h>
#include <nativehelper/ScopedLocalRef.h>
#include <nativehelper/ScopedPrimitiveArray.h>
@@ -333,29 +333,27 @@ static const JNINativeMethod gMethods[] = {
(void*) readNetworkStatsDev },
};
int register_com_android_internal_net_NetworkStatsFactory(JNIEnv* env) {
int err = RegisterMethodsOrDie(env,
"com/android/internal/net/NetworkStatsFactory", gMethods,
int register_android_server_net_NetworkStatsFactory(JNIEnv* env) {
int err = jniRegisterNativeMethods(env, "com/android/server/net/NetworkStatsFactory", gMethods,
NELEM(gMethods));
gStringClass = env->FindClass("java/lang/String");
gStringClass = static_cast<jclass>(env->NewGlobalRef(gStringClass));
gStringClass = FindClassOrDie(env, "java/lang/String");
gStringClass = MakeGlobalRefOrDie(env, gStringClass);
jclass clazz = FindClassOrDie(env, "android/net/NetworkStats");
gNetworkStatsClassInfo.size = GetFieldIDOrDie(env, clazz, "size", "I");
gNetworkStatsClassInfo.capacity = GetFieldIDOrDie(env, clazz, "capacity", "I");
gNetworkStatsClassInfo.iface = GetFieldIDOrDie(env, clazz, "iface", "[Ljava/lang/String;");
gNetworkStatsClassInfo.uid = GetFieldIDOrDie(env, clazz, "uid", "[I");
gNetworkStatsClassInfo.set = GetFieldIDOrDie(env, clazz, "set", "[I");
gNetworkStatsClassInfo.tag = GetFieldIDOrDie(env, clazz, "tag", "[I");
gNetworkStatsClassInfo.metered = GetFieldIDOrDie(env, clazz, "metered", "[I");
gNetworkStatsClassInfo.roaming = GetFieldIDOrDie(env, clazz, "roaming", "[I");
gNetworkStatsClassInfo.defaultNetwork = GetFieldIDOrDie(env, clazz, "defaultNetwork", "[I");
gNetworkStatsClassInfo.rxBytes = GetFieldIDOrDie(env, clazz, "rxBytes", "[J");
gNetworkStatsClassInfo.rxPackets = GetFieldIDOrDie(env, clazz, "rxPackets", "[J");
gNetworkStatsClassInfo.txBytes = GetFieldIDOrDie(env, clazz, "txBytes", "[J");
gNetworkStatsClassInfo.txPackets = GetFieldIDOrDie(env, clazz, "txPackets", "[J");
gNetworkStatsClassInfo.operations = GetFieldIDOrDie(env, clazz, "operations", "[J");
jclass clazz = env->FindClass("android/net/NetworkStats");
gNetworkStatsClassInfo.size = env->GetFieldID(clazz, "size", "I");
gNetworkStatsClassInfo.capacity = env->GetFieldID(clazz, "capacity", "I");
gNetworkStatsClassInfo.iface = env->GetFieldID(clazz, "iface", "[Ljava/lang/String;");
gNetworkStatsClassInfo.uid = env->GetFieldID(clazz, "uid", "[I");
gNetworkStatsClassInfo.set = env->GetFieldID(clazz, "set", "[I");
gNetworkStatsClassInfo.tag = env->GetFieldID(clazz, "tag", "[I");
gNetworkStatsClassInfo.metered = env->GetFieldID(clazz, "metered", "[I");
gNetworkStatsClassInfo.roaming = env->GetFieldID(clazz, "roaming", "[I");
gNetworkStatsClassInfo.defaultNetwork = env->GetFieldID(clazz, "defaultNetwork", "[I");
gNetworkStatsClassInfo.rxBytes = env->GetFieldID(clazz, "rxBytes", "[J");
gNetworkStatsClassInfo.rxPackets = env->GetFieldID(clazz, "rxPackets", "[J");
gNetworkStatsClassInfo.txBytes = env->GetFieldID(clazz, "txBytes", "[J");
gNetworkStatsClassInfo.txPackets = env->GetFieldID(clazz, "txPackets", "[J");
gNetworkStatsClassInfo.operations = env->GetFieldID(clazz, "operations", "[J");
return err;
}

View File

@@ -54,6 +54,7 @@ int register_android_server_HardwarePropertiesManagerService(JNIEnv* env);
int register_android_server_SyntheticPasswordManager(JNIEnv* env);
int register_android_server_GraphicsStatsService(JNIEnv* env);
int register_android_hardware_display_DisplayViewport(JNIEnv* env);
int register_android_server_net_NetworkStatsFactory(JNIEnv* env);
int register_android_server_net_NetworkStatsService(JNIEnv* env);
};
@@ -102,6 +103,7 @@ extern "C" jint JNI_OnLoad(JavaVM* vm, void* /* reserved */)
register_android_server_SyntheticPasswordManager(env);
register_android_server_GraphicsStatsService(env);
register_android_hardware_display_DisplayViewport(env);
register_android_server_net_NetworkStatsFactory(env);
register_android_server_net_NetworkStatsService(env);
return JNI_VERSION_1_4;
}

View File

@@ -0,0 +1,29 @@
// Copyright (C) 2015 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// build framework base core benchmarks
// ============================================================
java_library {
name: "networkStatsFactory-benchmarks",
installable: true,
srcs: ["src/**/*.java"],
libs: [
"caliper-api-target",
"services.core",
],
}

View File

@@ -14,10 +14,11 @@
* limitations under the License.
*/
package com.android.internal.net;
package com.android.server.net;
import android.net.NetworkStats;
import android.os.SystemClock;
import com.android.server.net.NetworkStatsFactory;
import com.google.caliper.AfterExperiment;
import com.google.caliper.BeforeExperiment;
import java.io.File;

View File

@@ -1,11 +1,8 @@
//########################################################################
// Build FrameworksNetTests package
//########################################################################
android_test {
name: "FrameworksNetTests",
// Include all test java files.
srcs: ["java/**/*.java"],
java_defaults {
name: "FrameworksNetTests-jni-defaults",
static_libs: [
"frameworks-base-testutils",
"framework-protos",
@@ -20,6 +17,52 @@ android_test {
"android.test.base",
"android.test.mock",
],
jni_libs: [
"ld-android",
"libartbase",
"libbacktrace",
"libbase",
"libbinder",
"libbinderthreadstate",
"libbpf",
"libbpf_android",
"libc++",
"libcrypto",
"libcutils",
"libdexfile",
"libdl_android",
"libhidl-gen-utils",
"libhidlbase",
"libhidltransport",
"libhwbinder",
"libjsoncpp",
"liblog",
"liblzma",
"libnativehelper",
"libnetdbpf",
"libnetdutils",
"libpackagelistparser",
"libpcre2",
"libprocessgroup",
"libselinux",
"libui",
"libutils",
"libvintf",
"libvndksupport",
"libtinyxml2",
"libunwindstack",
"libutilscallstack",
"libziparchive",
"libz",
"netd_aidl_interface-cpp",
"libnetworkstatsfactorytestjni",
],
}
android_test {
name: "FrameworksNetTests",
defaults: ["FrameworksNetTests-jni-defaults"],
srcs: ["java/**/*.java"],
platform_apis: true,
test_suites: ["device-tests"],
certificate: "platform",

View File

@@ -14,7 +14,7 @@
* limitations under the License.
*/
package com.android.internal.net;
package com.android.server.net;
import static android.net.NetworkStats.DEFAULT_NETWORK_NO;
import static android.net.NetworkStats.METERED_NO;
@@ -70,6 +70,10 @@ public class NetworkStatsFactoryTest {
IoUtils.deleteContents(mTestProc);
}
// The libandroid_servers which have the native method is not available to
// applications. So in order to have a test support native library, the native code
// related to networkStatsFactory is compiled to a minimal native library and loaded here.
System.loadLibrary("networkstatsfactorytestjni");
mFactory = new NetworkStatsFactory(mTestProc, false);
}

23
tests/net/jni/Android.bp Normal file
View File

@@ -0,0 +1,23 @@
cc_library_shared {
name: "libnetworkstatsfactorytestjni",
cflags: [
"-Wall",
"-Werror",
"-Wno-unused-parameter",
"-Wthread-safety",
],
srcs: [
":lib_networkStatsFactory_native",
"test_onload.cpp",
],
shared_libs: [
"libbpf_android",
"liblog",
"libnativehelper",
"libnetdbpf",
"libnetdutils",
],
}

View File

@@ -0,0 +1,44 @@
/*
* Copyright (C) 2019 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
* this is a mini native libaray for NetworkStatsFactoryTest to run properly. It
* load all the native method related to NetworkStatsFactory when test run
*/
#include <nativehelper/JNIHelp.h>
#include "jni.h"
#include "utils/Log.h"
#include "utils/misc.h"
namespace android {
int register_android_server_net_NetworkStatsFactory(JNIEnv* env);
};
using namespace android;
extern "C" jint JNI_OnLoad(JavaVM* vm, void* /* reserved */)
{
JNIEnv* env = NULL;
jint result = -1;
if (vm->GetEnv((void**) &env, JNI_VERSION_1_4) != JNI_OK) {
ALOGE("GetEnv failed!");
return result;
}
ALOG_ASSERT(env, "Could not retrieve the env!");
register_android_server_net_NetworkStatsFactory(env);
return JNI_VERSION_1_4;
}