ANDROID: uid_sys_stats: check previous uid_entry before call find_or_register_uid

Theads in a process are stored in list struct task_struct->thread_group,
so it will be visited continiously in below loop:
do_each_thread(temp, task) {
    ...
} while_each_thread(temp, task);

I add some log in the loop, we can see below information:
[   65.033561] uid 1000, uid_entry ffffffc0f2761600
[   65.033567] uid 1000, uid_entry ffffffc0f2761600
[   65.033574] uid 1000, uid_entry ffffffc0f2761600
[   65.033581] uid 1000, uid_entry ffffffc0f2761600
[   65.033588] uid 1000, uid_entry ffffffc0f2761600
[   65.033595] uid 1000, uid_entry ffffffc0f2761600
[   65.033602] uid 1000, uid_entry ffffffc0f2761600
[   65.033609] uid 1000, uid_entry ffffffc0f2761600
[   65.033615] uid 1000, uid_entry ffffffc0f2761600
[   65.033622] uid 1000, uid_entry ffffffc0f2761600
[   65.033629] uid 1000, uid_entry ffffffc0f2761600
[   65.033637] uid 1000, uid_entry ffffffc0f2761600
[   65.033644] uid 1000, uid_entry ffffffc0f2761600
[   65.033651] uid 1000, uid_entry ffffffc0f2761600
[   65.033658] uid 1000, uid_entry ffffffc0f2761600
[   65.033665] uid 1000, uid_entry ffffffc0f2761600
[   65.033672] uid 1000, uid_entry ffffffc0f2761600
[   65.033680] uid 1000, uid_entry ffffffc0f2761600
[   65.033687] uid 1000, uid_entry ffffffc0f2761600
[   65.033694] uid 1000, uid_entry ffffffc0f2761600
[   65.033701] uid 1000, uid_entry ffffffc0f2761600
[   65.033708] uid 1000, uid_entry ffffffc0f2761600
[   65.033715] uid 1000, uid_entry ffffffc0f2761600
[   65.033722] uid 1000, uid_entry ffffffc0f2761600
[   65.033729] uid 1000, uid_entry ffffffc0f2761600
[   65.033736] uid 1000, uid_entry ffffffc0f2761600
[   65.033743] uid 1000, uid_entry ffffffc0f2761600
[   65.033750] uid 1000, uid_entry ffffffc0f2761600
[   65.033757] uid 1000, uid_entry ffffffc0f2761600
[   65.033763] uid 1000, uid_entry ffffffc0f2761600
[   65.033770] uid 1000, uid_entry ffffffc0f2761600
[   65.033777] uid 1000, uid_entry ffffffc0f2761600
[   65.033784] uid 1000, uid_entry ffffffc0f2761600
[   65.033791] uid 1000, uid_entry ffffffc0f2761600
[   65.033798] uid 1000, uid_entry ffffffc0f2761600

So we can check the previous uid_entry before calling find_or_register_uid
to save time.

Change-Id: I05ec1a1405a80c0a620cb4b4b2f6483dbfde7829
Signed-off-by: Ganesh Mahendran <opensource.ganesh@gmail.com>
This commit is contained in:
Ganesh Mahendran 2017-05-25 15:20:29 +08:00 committed by Amit Pundir
parent 62726d0c68
commit 84118cd32e

View file

@ -95,7 +95,7 @@ static struct uid_entry *find_or_register_uid(uid_t uid)
static int uid_cputime_show(struct seq_file *m, void *v)
{
struct uid_entry *uid_entry;
struct uid_entry *uid_entry = NULL;
struct task_struct *task, *temp;
struct user_namespace *user_ns = current_user_ns();
u64 utime;
@ -113,7 +113,8 @@ static int uid_cputime_show(struct seq_file *m, void *v)
read_lock(&tasklist_lock);
do_each_thread(temp, task) {
uid = from_kuid_munged(user_ns, task_uid(task));
uid_entry = find_or_register_uid(uid);
if (!uid_entry || uid_entry->uid != uid)
uid_entry = find_or_register_uid(uid);
if (!uid_entry) {
read_unlock(&tasklist_lock);
rt_mutex_unlock(&uid_lock);
@ -249,7 +250,7 @@ static void compute_uid_io_bucket_stats(struct io_stats *io_bucket,
static void update_io_stats_all_locked(void)
{
struct uid_entry *uid_entry;
struct uid_entry *uid_entry = NULL;
struct task_struct *task, *temp;
struct user_namespace *user_ns = current_user_ns();
unsigned long bkt;
@ -262,7 +263,8 @@ static void update_io_stats_all_locked(void)
rcu_read_lock();
do_each_thread(temp, task) {
uid = from_kuid_munged(user_ns, task_uid(task));
uid_entry = find_or_register_uid(uid);
if (!uid_entry || uid_entry->uid != uid)
uid_entry = find_or_register_uid(uid);
if (!uid_entry)
continue;
add_uid_io_stats(uid_entry, task, UID_STATE_TOTAL_CURR);