Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Unverified Commit b037ad4f authored by derfelot's avatar derfelot
Browse files

net: Add network activity statistics tracking from Sony kernel

Taken from Sony 47.2.A.10.107 stock kernel
parent 4f1ea1a6
Loading
Loading
Loading
Loading
+30 −0
Original line number Diff line number Diff line
/*
 * 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)
 */
/*
 * NOTE: This file has been modified by Sony Mobile Communications Inc.
 * Modifications are Copyright (c) 2017 Sony Mobile Communications Inc,
 * and licensed under the license of the file.
 */

#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 */
+8 −0
Original line number Diff line number Diff line
@@ -102,6 +102,14 @@ config ANDROID_PARANOID_NETWORK
	help
		none

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
+1 −0
Original line number Diff line number Diff line
@@ -79,3 +79,4 @@ obj-y += l3mdev/
endif
obj-$(CONFIG_IPC_ROUTER)	+= ipc_router/
obj-$(CONFIG_RMNET_DATA) += rmnet_data/
obj-$(CONFIG_NET_ACTIVITY_STATS)	+= activity_stats.o

net/activity_stats.c

0 → 100644
+121 −0
Original line number Diff line number Diff line
/* 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)
 */
/*
 * NOTE: This file has been modified by Sony Mobile Communications Inc.
 * Modifications are Copyright (c) 2017 Sony Mobile Communications Inc,
 * and licensed under the license of the file.
 */

#include <linux/proc_fs.h>
#include <linux/seq_file.h>
#include <linux/suspend.h>
#include <net/net_namespace.h>

/*
 * 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;

	seq_printf(m, "Min Bucket(sec) Count\n");

	for (i = 0; i < BUCKET_MAX; i++) {
		seq_printf(m, "%15d %lu\n", 1 << i, activity_stats[i]);
	}

	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);