From patchwork Tue Jan 13 11:14:02 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Kiran Kumar Raparthy X-Patchwork-Id: 42990 Return-Path: X-Original-To: linaro@patches.linaro.org Delivered-To: linaro@patches.linaro.org Received: from mail-wi0-f199.google.com (mail-wi0-f199.google.com [209.85.212.199]) by ip-10-151-82-157.ec2.internal (Postfix) with ESMTPS id E43DF20DE8 for ; Tue, 13 Jan 2015 11:15:00 +0000 (UTC) Received: by mail-wi0-f199.google.com with SMTP id bs8sf1570762wib.2 for ; Tue, 13 Jan 2015 03:15:00 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:delivered-to:from:to:cc:subject:date:message-id :in-reply-to:references:mime-version:content-type :content-transfer-encoding:sender:precedence:list-id :x-original-sender:x-original-authentication-results:mailing-list :list-post:list-help:list-archive:list-unsubscribe; bh=yAiEVoa9yzT9s1kOteycdqX+BtyAAu6r+xJ9KX2Y6Sk=; b=XV0OKPT3fjuP7qLicon7YTkfKcdyjDxc0z5rFEwknrHN+UonjarK3k+ER0VLvoeZnM kjBJ1mXv3LIrznO5hV007xJ66yR+b+/ru4G82ri2eVw2WFd7jmkRlvaMEy81CPrWAwPy vqKfAFaQxZO5QQ5DmgWlF4sWDIT7qxO8Oxt4xxE//UCJHWiG3E9TzCPKC1Lo/tEE1Zon LRNBxvV3da6KsdDQtEWrpwpAwhW/0owPlPT1GG8y0x3z5e/SZT7pOnGIYgFqMvukjnd+ 9+AGwDjB66diUTaugdyQFggII97zUtKnKMyv7GloXQNrOZSUx9RS07EtzkxxB9KkCuEL jmEA== X-Gm-Message-State: ALoCoQmUR3ircu78vvqID1s0L60vOoMi6F7JF3nZSP3ojhv4bf45hqrfckEj2A7hTId4BAfxvuOD X-Received: by 10.194.78.42 with SMTP id y10mr251876wjw.4.1421147700219; Tue, 13 Jan 2015 03:15:00 -0800 (PST) X-BeenThere: patchwork-forward@linaro.org Received: by 10.152.234.169 with SMTP id uf9ls706820lac.75.gmail; Tue, 13 Jan 2015 03:15:00 -0800 (PST) X-Received: by 10.152.27.130 with SMTP id t2mr40918164lag.28.1421147700027; Tue, 13 Jan 2015 03:15:00 -0800 (PST) Received: from mail-lb0-f176.google.com (mail-lb0-f176.google.com. [209.85.217.176]) by mx.google.com with ESMTPS id ld3si2523971lac.115.2015.01.13.03.15.00 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Tue, 13 Jan 2015 03:15:00 -0800 (PST) Received-SPF: pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 209.85.217.176 as permitted sender) client-ip=209.85.217.176; Received: by mail-lb0-f176.google.com with SMTP id p9so1975215lbv.7 for ; Tue, 13 Jan 2015 03:14:59 -0800 (PST) X-Received: by 10.112.104.4 with SMTP id ga4mr41562047lbb.24.1421147699869; Tue, 13 Jan 2015 03:14:59 -0800 (PST) X-Forwarded-To: patchwork-forward@linaro.org X-Forwarded-For: patch@linaro.org patchwork-forward@linaro.org Delivered-To: patch@linaro.org Received: by 10.112.9.200 with SMTP id c8csp1348737lbb; Tue, 13 Jan 2015 03:14:58 -0800 (PST) X-Received: by 10.68.65.35 with SMTP id u3mr51204688pbs.9.1421147698007; Tue, 13 Jan 2015 03:14:58 -0800 (PST) Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id pd2si26550424pbc.181.2015.01.13.03.14.56; Tue, 13 Jan 2015 03:14:57 -0800 (PST) Received-SPF: none (google.com: linux-kernel-owner@vger.kernel.org does not designate permitted sender hosts) client-ip=209.132.180.67; Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752319AbbAMLOv (ORCPT + 27 others); Tue, 13 Jan 2015 06:14:51 -0500 Received: from mail-pa0-f46.google.com ([209.85.220.46]:55482 "EHLO mail-pa0-f46.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751844AbbAMLOr (ORCPT ); Tue, 13 Jan 2015 06:14:47 -0500 Received: by mail-pa0-f46.google.com with SMTP id lf10so3132284pab.5 for ; Tue, 13 Jan 2015 03:14:47 -0800 (PST) X-Received: by 10.70.102.170 with SMTP id fp10mr50192157pdb.73.1421147687039; Tue, 13 Jan 2015 03:14:47 -0800 (PST) Received: from c-krapar-linux.qualcomm.com ([202.46.23.54]) by mx.google.com with ESMTPSA id y2sm16808684pdm.31.2015.01.13.03.14.42 (version=TLSv1 cipher=RC4-SHA bits=128/128); Tue, 13 Jan 2015 03:14:46 -0800 (PST) From: Kiran Raparthy To: linux-kernel@vger.kernel.org Cc: Mike Chan , Arnd Bergmann , Greg Kroah-Hartman , "David S. Miller" , netdev@vger.kernel.org, Android Kernel Team , John Stultz , Sumit Semwal , =?UTF-8?q?Arve=20Hj=F8nnev=E5g?= , Kiran Raparthy Subject: [RFC 2/2] net: activity_stats: Add statistics for network transmission activity Date: Tue, 13 Jan 2015 16:44:02 +0530 Message-Id: <1421147642-28360-2-git-send-email-kiran.kumar@linaro.org> X-Mailer: git-send-email 1.8.2.1 In-Reply-To: <1421147642-28360-1-git-send-email-kiran.kumar@linaro.org> References: <1421147642-28360-1-git-send-email-kiran.kumar@linaro.org> MIME-Version: 1.0 Sender: linux-kernel-owner@vger.kernel.org Precedence: list List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Removed-Original-Auth: Dkim didn't pass. X-Original-Sender: kiran.kumar@linaro.org X-Original-Authentication-Results: mx.google.com; spf=pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 209.85.217.176 as permitted sender) smtp.mail=patch+caf_=patchwork-forward=linaro.org@linaro.org Mailing-list: list patchwork-forward@linaro.org; contact patchwork-forward+owners@linaro.org X-Google-Group-Id: 836684582541 List-Post: , List-Help: , List-Archive: List-Unsubscribe: , From: Mike Chan net: activity_stats: Add statistics for network transmission activity When enabled, tracks the frequency of network transmissions (inbound and outbound) and buckets them accordingly. Buckets are determined by time between network activity. Each bucket represents the number of network transmisions that were N sec or longer apart. Where N is defined as 1 << bucket index. This network pattern tracking is particularly useful for wireless networks (ie: 3G) where batching network activity closely together is more power efficient than far apart. New file: /proc/net/stat/activity output: Min Bucket(sec) Count 1 7 2 0 4 1 8 0 16 0 32 2 64 1 128 0 Cc: Arnd Bergmann Cc: Greg Kroah-Hartman Cc: David S. Miller Cc: linux-kernel@vger.kernel.org Cc: netdev@vger.kernel.org Cc: Android Kernel Team Cc: John Stultz Cc: Sumit Semwal Cc: Arve Hjřnnevĺg Signed-off-by: Mike Chan [Kiran: Added context to commit message. included build fix from Arve,continued to use uid_t but used covnersion from kuid_t where ever necessary] Signed-off-by: Kiran Raparthy --- drivers/misc/uid_stat.c | 3 ++ include/net/activity_stats.h | 25 ++++++++++ net/Kconfig | 8 +++ net/Makefile | 1 + net/activity_stats.c | 116 +++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 153 insertions(+) create mode 100644 include/net/activity_stats.h create mode 100644 net/activity_stats.c diff --git a/drivers/misc/uid_stat.c b/drivers/misc/uid_stat.c index aaaa406..63065e2 100644 --- a/drivers/misc/uid_stat.c +++ b/drivers/misc/uid_stat.c @@ -24,6 +24,7 @@ #include #include #include +#include static DEFINE_SPINLOCK(uid_lock); static LIST_HEAD(uid_list); @@ -126,6 +127,7 @@ int uid_stat_tcp_snd(kuid_t uid, int size) { struct uid_stat *entry; + activity_stats_update(); entry = find_or_create_uid_stat(uid); if (!entry) return -1; @@ -137,6 +139,7 @@ int uid_stat_tcp_rcv(kuid_t uid, int size) { struct uid_stat *entry; + activity_stats_update(); entry = find_or_create_uid_stat(uid); if (!entry) return -1; diff --git a/include/net/activity_stats.h b/include/net/activity_stats.h new file mode 100644 index 0000000..10e4c15 --- /dev/null +++ b/include/net/activity_stats.h @@ -0,0 +1,25 @@ +/* + * Copyright (C) 2010 Google, Inc. + * + * This software is licensed under the terms of the GNU General Public + * License version 2, as published by the Free Software Foundation, and + * may be copied, distributed, and modified under those terms. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * Author: Mike Chan (mike@android.com) + */ + +#ifndef __activity_stats_h +#define __activity_stats_h + +#ifdef CONFIG_NET_ACTIVITY_STATS +void activity_stats_update(void); +#else +#define activity_stats_update(void) {} +#endif + +#endif /* _NET_ACTIVITY_STATS_H */ diff --git a/net/Kconfig b/net/Kconfig index ff9ffc1..3159361 100644 --- a/net/Kconfig +++ b/net/Kconfig @@ -83,6 +83,14 @@ source "net/netlabel/Kconfig" endif # if INET +config NET_ACTIVITY_STATS + bool "Network activity statistics tracking" + default y + help + Network activity statistics are useful for tracking wireless + modem activity on 2G, 3G, 4G wireless networks. Counts number of + transmissions and groups them in specified time buckets. + config NETWORK_SECMARK bool "Security Marking" help diff --git a/net/Makefile b/net/Makefile index 95fc694..d81a969 100644 --- a/net/Makefile +++ b/net/Makefile @@ -73,6 +73,7 @@ obj-$(CONFIG_OPENVSWITCH) += openvswitch/ obj-$(CONFIG_VSOCKETS) += vmw_vsock/ obj-$(CONFIG_NET_MPLS_GSO) += mpls/ obj-$(CONFIG_HSR) += hsr/ +obj-$(CONFIG_NET_ACTIVITY_STATS) += activity_stats.o ifneq ($(CONFIG_NET_SWITCHDEV),) obj-y += switchdev/ endif diff --git a/net/activity_stats.c b/net/activity_stats.c new file mode 100644 index 0000000..cf0a09d --- /dev/null +++ b/net/activity_stats.c @@ -0,0 +1,116 @@ +/* net/activity_stats.c + * + * Copyright (C) 2010 Google, Inc. + * + * This software is licensed under the terms of the GNU General Public + * License version 2, as published by the Free Software Foundation, and + * may be copied, distributed, and modified under those terms. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * Author: Mike Chan (mike@android.com) + */ + +#include +#include +#include +#include + +/* Track transmission rates in buckets (power of 2). + * 1,2,4,8...512 seconds. + * + * Buckets represent the count of network transmissions at least + * N seconds apart, where N is 1 << bucket index. + */ +#define BUCKET_MAX 10 + +/* Track network activity frequency */ +static unsigned long activity_stats[BUCKET_MAX]; +static ktime_t last_transmit; +static ktime_t suspend_time; +static DEFINE_SPINLOCK(activity_lock); + +void activity_stats_update(void) +{ + int i; + unsigned long flags; + ktime_t now; + s64 delta; + + spin_lock_irqsave(&activity_lock, flags); + now = ktime_get(); + delta = ktime_to_ns(ktime_sub(now, last_transmit)); + + for (i = BUCKET_MAX - 1; i >= 0; i--) { + /* Check if the time delta between network activity is within + * the minimum bucket range. + */ + if (delta < (1000000000ULL << i)) + continue; + + activity_stats[i]++; + last_transmit = now; + break; + } + spin_unlock_irqrestore(&activity_lock, flags); +} + +static int activity_stats_show(struct seq_file *m, void *v) +{ + int i; + int ret; + + seq_printf(m, "Min Bucket(sec) Count\n"); + + for (i = 0; i < BUCKET_MAX; i++) { + ret = seq_printf(m, "%15d %lu\n", 1 << i, activity_stats[i]); + if (ret) + return ret; + } + + return 0; +} + +static int activity_stats_notifier(struct notifier_block *nb, + unsigned long event, void *dummy) +{ + switch (event) { + case PM_SUSPEND_PREPARE: + suspend_time = ktime_get_real(); + break; + + case PM_POST_SUSPEND: + suspend_time = ktime_sub(ktime_get_real(), suspend_time); + last_transmit = ktime_sub(last_transmit, suspend_time); + } + + return 0; +} + +static int activity_stats_open(struct inode *inode, struct file *file) +{ + return single_open(file, activity_stats_show, PDE_DATA(inode)); +} + +static const struct file_operations activity_stats_fops = { + .open = activity_stats_open, + .read = seq_read, + .llseek = seq_lseek, + .release = seq_release, +}; + +static struct notifier_block activity_stats_notifier_block = { + .notifier_call = activity_stats_notifier, +}; + +static int __init activity_stats_init(void) +{ + proc_create("activity", S_IRUGO, + init_net.proc_net_stat, &activity_stats_fops); + return register_pm_notifier(&activity_stats_notifier_block); +} + +subsys_initcall(activity_stats_init);