-
Notifications
You must be signed in to change notification settings - Fork 0
/
pidhash.h
100 lines (73 loc) · 1.74 KB
/
pidhash.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
//SPDX-License-Identifier: GPL-2.0-or-later
/*
Copyright (c) 2014-2021 Cyril Hrubis <[email protected]>
*/
#ifndef PIDHASH_H__
#define PIDHASH_H__
#include <sysinfo/read_proc.h>
#include <sysinfo/guid_map_cache.h>
#include <utils/gp_vec.h>
#include <utils/gp_htable2.h>
struct pid {
struct read_proc_stat stat;
int pcpu;
uint64_t lcpu;
int seen;
};
static gp_htable *pid_hash;
static struct pid **pid_table;
static inline void pidhash_init(void)
{
pid_hash = gp_htable_new(0, 0);
pid_table = gp_vec_new(0, sizeof(void *));
}
static inline size_t pidhash_hash(const void *key, size_t htable_size)
{
unsigned int pid = (uintptr_t)key;
return (pid * 13) % htable_size;
}
static inline int pidhash_cmp(const void *key1, const void *key2)
{
return key1 == key2;
}
static inline struct pid *pidhash_lookup(unsigned int pid)
{
struct pid *val;
val = gp_htable_get2(pid_hash, pidhash_hash, pidhash_cmp, (void*)((uintptr_t)pid));
if (val) {
val->seen = 1;
return val;
}
val = malloc(sizeof(*val));
if (!val)
return NULL;
memset(val, 0, sizeof(*val));
val->seen = 1;
if (!GP_VEC_APPEND(pid_table, val)) {
free(val);
return NULL;
}
gp_htable_put2(pid_hash, pidhash_hash, val, (void*)((uintptr_t)pid));
return val;
}
static inline int pidhash_should_trim(void *val)
{
struct pid *pid = val;
int ret = !pid->seen;
pid->seen = 0;
return ret;
}
static inline void pidhash_trim(void)
{
size_t i;
for (i = 0; i < gp_vec_len(pid_table); i++) {
while (i < gp_vec_len(pid_table) && !pid_table[i]->seen)
pid_table = gp_vec_move_shrink(pid_table, i);
}
gp_htable_trim2(pid_hash, pidhash_hash, pidhash_cmp, pidhash_should_trim, free);
}
static inline size_t pidhash_cnt(void)
{
return gp_vec_len(pid_table);
}
#endif /* PIDHASH_H__ */