ANDROID: uid_sys_stats: simplify code structure

Make code easier to read and maintain with macro definitions.

Bug: 278138377

Change-Id: Ia52b3357556c973ea3875ea316d74d94893ad2d5
Signed-off-by: Peifeng Li <lipeifeng@oppo.com>
Signed-off-by: Helium-Studio <67852324+Helium-Studio@users.noreply.github.com>
This commit is contained in:
Peifeng Li
2024-03-19 20:57:58 +08:00
committed by Helium-Studio
parent 7f7b7e8ed1
commit 81bbefeb15

View File

@@ -33,11 +33,20 @@
#define UID_HASH_BITS 10
#define UID_HASH_NUMS (1 << UID_HASH_BITS)
DECLARE_HASHTABLE(hash_table, UID_HASH_BITS);
/*
* uid_lock[bkt] ensure consistency of hash_table[bkt]
*/
/* uid_lock[bkt] ensure consistency of hash_table[bkt] */
spinlock_t uid_lock[UID_HASH_NUMS];
#define for_each_bkt(bkt) \
for (bkt = 0; bkt < HASH_SIZE(hash_table); bkt++)
/* iterate over all uid_entrys hashing to the same bkt */
#define for_each_uid_entry(uid_entry, bkt) \
hlist_for_each_entry(uid_entry, &hash_table[bkt], hash)
#define for_each_uid_entry_safe(uid_entry, tmp, bkt) \
hlist_for_each_entry_safe(uid_entry, tmp,\
&hash_table[bkt], hash)
static struct proc_dir_entry *cpu_parent;
static struct proc_dir_entry *io_parent;
static struct proc_dir_entry *proc_parent;
@@ -74,20 +83,33 @@ struct uid_entry {
struct hlist_node hash;
};
static void init_hash_table_and_lock(void)
{
int i;
hash_init(hash_table);
for (i = 0; i < UID_HASH_NUMS; i++)
spin_lock_init(&uid_lock[i]);
}
static inline int uid_to_bkt(uid_t uid)
{
return hash_min(uid, HASH_BITS(hash_table));
}
static inline int trylock_uid(uid_t uid)
{
return spin_trylock(
&uid_lock[hash_min(uid, HASH_BITS(hash_table))]);
return spin_trylock(&uid_lock[uid_to_bkt(uid)]);
}
static inline void lock_uid(uid_t uid)
{
spin_lock(&uid_lock[hash_min(uid, HASH_BITS(hash_table))]);
spin_lock(&uid_lock[uid_to_bkt(uid)]);
}
static inline void unlock_uid(uid_t uid)
{
spin_unlock(&uid_lock[hash_min(uid, HASH_BITS(hash_table))]);
spin_unlock(&uid_lock[uid_to_bkt(uid)]);
}
static inline void lock_uid_by_bkt(u32 bkt)
@@ -144,7 +166,9 @@ static void compute_io_bucket_stats(struct io_stats *io_bucket,
static struct uid_entry *find_uid_entry(uid_t uid)
{
struct uid_entry *uid_entry;
hash_for_each_possible(hash_table, uid_entry, hash, uid) {
u32 bkt = uid_to_bkt(uid);
for_each_uid_entry(uid_entry, bkt) {
if (uid_entry->uid == uid)
return uid_entry;
}
@@ -201,11 +225,9 @@ static int uid_cputime_show(struct seq_file *m, void *v)
struct uid_entry *uid_entry = NULL;
u32 bkt;
for (bkt = 0, uid_entry = NULL; uid_entry == NULL &&
bkt < HASH_SIZE(hash_table); bkt++) {
for_each_bkt(bkt) {
lock_uid_by_bkt(bkt);
hlist_for_each_entry(uid_entry, &hash_table[bkt], hash) {
for_each_uid_entry(uid_entry, bkt) {
u64 total_utime = uid_entry->utime;
u64 total_stime = uid_entry->stime;
@@ -239,8 +261,6 @@ static int uid_remove_open(struct inode *inode, struct file *file)
static ssize_t uid_remove_write(struct file *file,
const char __user *buffer, size_t count, loff_t *ppos)
{
struct uid_entry *uid_entry;
struct hlist_node *tmp;
char uids[128];
char *start_uid, *end_uid = NULL;
long int uid_start = 0, uid_end = 0;
@@ -267,9 +287,12 @@ static ssize_t uid_remove_write(struct file *file,
cpufreq_task_times_remove_uids(uid_start, uid_end);
for (; uid_start <= uid_end; uid_start++) {
struct uid_entry *uid_entry;
struct hlist_node *tmp;
u32 bkt = uid_to_bkt((uid_t)uid_start);
lock_uid(uid_start);
hash_for_each_possible_safe(hash_table, uid_entry, tmp,
hash, (uid_t)uid_start) {
for_each_uid_entry_safe(uid_entry, tmp, bkt) {
if (uid_start == uid_entry->uid) {
hash_del(&uid_entry->hash);
kfree(uid_entry);
@@ -350,10 +373,9 @@ static int uid_io_show(struct seq_file *m, void *v)
struct uid_entry *uid_entry = NULL;
u32 bkt;
for (bkt = 0, uid_entry = NULL; uid_entry == NULL && bkt < HASH_SIZE(hash_table);
bkt++) {
for_each_bkt(bkt) {
lock_uid_by_bkt(bkt);
hlist_for_each_entry(uid_entry, &hash_table[bkt], hash) {
for_each_uid_entry(uid_entry, bkt) {
update_io_stats_uid(uid_entry);
@@ -528,15 +550,6 @@ static struct notifier_block process_notifier_block = {
.notifier_call = process_notifier,
};
static void init_hash_table_and_lock(void)
{
int i;
hash_init(hash_table);
for (i = 0; i < UID_HASH_NUMS; i++)
spin_lock_init(&uid_lock[i]);
}
static int __init proc_uid_sys_stats_init(void)
{
init_hash_table_and_lock();