自 2025 年 3 月 27 日起,我们建议您使用 android-latest-release
而非 aosp-main
构建 AOSP 并为其做出贡献。如需了解详情,请参阅 AOSP 的变更。
网络接口统计信息概览
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
在 Android 4.0 中,Linux 网络接口所报告的统计信息会不时进行记录,并用于强制执行网络配额限制、呈现用户可见的图表等操作。
每个网络设备驱动程序(包括 Wi-Fi)都必须遵循标准的内核设备生命周期,并通过 dev_get_stats()
返回正确的统计信息。特别是即便接口处于激活状态时,返回的统计信息也必须严格保持单调统一性。只有在成功完成 unregister_netdev()
或等效的方法来为使用 register_netdevice_notifier()
/register_inetaddr_notifier()
/register_inet6addr_notifier()
注册的回调生成 NETDEV_UNREGISTER
事件后,驱动程序才能重置统计信息。
移动运营商通常在互联网层 (IP) 测量流量消耗。为了在 Android 4.0 中与这种方法保持一致,我们依赖于这样一个事实:对于内核设备,我们关注由 dev_get_stats()
返回的 rx_bytes
和 tx_bytes
值是否准确返回传输的互联网层 (IP
) 字节数。我们也知道,对其他设备而言可能并非如此。现在,该功能依赖于这一特性。新的驱动程序也应该具有该属性,且 dev_get_stats()
值不得包含较低网络层(如以太网标头)的任何封装开销,且最好不包含其他流量(如 ARP),除非可以忽略不计。
Android 框架仅从与 ConnectivityService
中的 NetworkStateTracker
关联的网络接口收集统计信息。这样一来,框架就可以具体识别每个网络接口,包括其类型(如 TYPE_MOBILE
或 TYPE_WIFI
)以及订阅者身份(如 IMSI)。用于路由数据的所有网络接口都应由 NetworkStateTracker
表示,以便正确计算统计信息。
本页面上的内容和代码示例受内容许可部分所述许可的限制。Java 和 OpenJDK 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-03-26。
[null,null,["最后更新时间 (UTC):2025-03-26。"],[],[],null,["# Network interface statistics overview\n\nIn Android 4.0, statistics reported by Linux network interfaces are\nrecorded over time, and are used to enforce network quota limits,\nrender user-visible charts, and more.\n\nEach network device driver (Wi-Fi included) must follow the standard\nkernel device lifecycle, and return correct statistics through\n`dev_get_stats()`. In particular, statistics returned must remain\nstrictly monotonic while the interface is active. Drivers may reset\nstatistics only after successfully completing an `unregister_netdev()`\nor the equivalent that generates a `NETDEV_UNREGISTER` event for\ncallbacks registered with `register_netdevice_notifier()` /\n`register_inetaddr_notifier()` / `register_inet6addr_notifier()`.\n\nMobile operators typically measure data usage at the Internet layer\n(IP). To match this approach in Android 4.0, we rely on the fact that\nfor the kernel devices we care about the `rx_bytes` and `tx_bytes`\nvalues returned by `dev_get_stats()` return exactly the Internet layer\n(`IP`) bytes transferred. But we understand that for other devices it\nmight not be the case. For now, the feature relies on this\npeculiarity. New drivers should have that property also, and the\n`dev_get_stats()` values must not include any encapsulation overhead\nof lower network layers (such as Ethernet headers), and should\npreferably not include other traffic (such as ARP) unless it is\nnegligible.\n\nThe Android framework only collects statistics from network interfaces\nassociated with a `NetworkStateTracker` in `ConnectivityService`. This\nenables the framework to concretely identify each network interface,\nincluding its type (such as `TYPE_MOBILE` or `TYPE_WIFI`) and\nsubscriber identity (such as IMSI). All network interfaces used to\nroute data should be represented by a `NetworkStateTracker` so that\nstatistics can be accounted correctly."]]