-
Notifications
You must be signed in to change notification settings - Fork 12
/
count_helpers.c
194 lines (169 loc) · 4.54 KB
/
count_helpers.c
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <linux/const.h>
#include <linux/refcount.h>
#include <linux/rblist.h>
#include <monitor.h>
#include <count_helpers.h>
struct count_dist {
struct rblist dist;
bool ins;
bool id;
bool key;
int hist_size;
u64 max;
};
static int count_dist_node_cmp(struct rb_node *rbn, const void *entry)
{
struct count_node *n = rb_entry(rbn, struct count_node, rbn);
const struct count_node *e = entry;
const struct count_dist *dist = e->dist;
if (dist->ins) {
if (n->ins > e->ins)
return 1;
else if (n->ins < e->ins)
return -1;
}
if (dist->id) {
if (n->id > e->id)
return 1;
else if (n->id < e->id)
return -1;
}
if (dist->key) {
if (n->key > e->key)
return 1;
else if (n->key < e->key)
return -1;
}
return 0;
}
static struct rb_node *count_dist_node_new(struct rblist *rlist, const void *new_entry)
{
struct count_dist *dist = container_of(rlist, struct count_dist, dist);
const struct count_node *e = new_entry;
struct count_node *n = malloc(sizeof(*n) + sizeof(u64) * dist->hist_size);
if (n) {
memset(n, 0, sizeof(*n) + sizeof(u64) * dist->hist_size);
RB_CLEAR_NODE(&n->rbn);
n->ins = e->ins;
n->id = e->id;
n->key = e->key;
return &n->rbn;
} else
return NULL;
}
static void count_dist_node_delete(struct rblist *rblist, struct rb_node *rb_node)
{
struct count_node *n = rb_entry(rb_node, struct count_node, rbn);
free(n);
}
struct count_dist *count_dist_new(bool ins, bool id, bool key, int hist_size)
{
struct count_dist *dist;
dist = malloc(sizeof(*dist));
if (!dist)
return NULL;
dist->ins = ins;
dist->id = id;
dist->key = key;
dist->hist_size = hist_size;
dist->max = 0;
rblist__init(&dist->dist);
dist->dist.node_cmp = count_dist_node_cmp;
dist->dist.node_new = count_dist_node_new;
dist->dist.node_delete = count_dist_node_delete;
return dist;
}
void count_dist_free(struct count_dist *dist)
{
if (dist) {
rblist__exit(&dist->dist);
free(dist);
}
}
void count_dist_input(struct count_dist *dist, u64 ins, u64 id, u64 key, u64 count)
{
struct count_node e = {
.dist = dist,
.ins = dist->ins ? ins : 0UL,
.id = dist->id ? id : 0UL,
.key = dist->key ? key : 0UL,
};
struct rb_node *rbn = NULL;
struct count_node *n = NULL;
if (!dist)
return ;
rbn = rblist__findnew(&dist->dist, &e);
if (rbn) {
n = rb_entry(rbn, struct count_node, rbn);
n->hist[n->hist_len] += count;
if (n->max < n->hist[n->hist_len])
n->max = n->hist[n->hist_len];
if (dist->max < n->hist[n->hist_len])
dist->max = n->hist[n->hist_len];
n->sum += count;
n->hist_len = (n->hist_len + 1) % dist->hist_size;
}
}
void count_dist_insert(struct count_dist *dist, u64 ins, u64 id, u64 key, u64 i, u64 count)
{
struct count_node e = {
.dist = dist,
.ins = dist->ins ? ins : 0UL,
.id = dist->id ? id : 0UL,
.key = dist->key ? key : 0UL,
};
struct rb_node *rbn = NULL;
struct count_node *n = NULL;
if (!dist)
return ;
rbn = rblist__findnew(&dist->dist, &e);
if (rbn) {
n = rb_entry(rbn, struct count_node, rbn);
i %= dist->hist_size;
n->hist[i] += count;
if (n->max < n->hist[i])
n->max = n->hist[i];
if (dist->max < n->hist[i])
dist->max = n->hist[i];
n->sum += count;
if (i > n->hist_len)
n->hist_len = i;
}
}
void count_dist_print(struct count_dist *dist, print_count_node printnode, void *opaque)
{
struct rb_node *node = NULL;
struct count_node *n = NULL;
if (!dist)
return;
if (rblist__empty(&dist->dist))
return;
for (node = rb_first_cached(&dist->dist.entries); node;
node = rb_next(node)) {
n = rb_entry(node, struct count_node, rbn);
printnode(opaque, n);
}
}
u64 count_dist_max(struct count_dist *dist)
{
return dist ? dist->max : 0;
}
void count_dist_reset(struct count_dist *dist)
{
if (!dist)
return;
rblist__exit(&dist->dist);
dist->max = 0;
}
bool count_dist_empty(struct count_dist *dist)
{
if (!dist)
return true;
return rblist__empty(&dist->dist);
}