Skip to content

Commit a14a11d

Browse files
committed
all completed, ready for kernel
1 parent c51d2ed commit a14a11d

File tree

2 files changed

+71
-94
lines changed

2 files changed

+71
-94
lines changed

main.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -647,8 +647,6 @@ static void test_all_aggregations_val_agg_val()
647647
n = get_number_aggr_with_base(sub2, &cont2);
648648
assert(n == ARR_SIZE(test_all_aggr));
649649

650-
// assert(get_total_number_values(src) == ARR_SIZE(test_values) * 2);
651-
652650
n = statsfs_source_add_values(sub1, test_all_aggr, NULL);
653651
assert(n == 0);
654652
n = get_number_aggr_with_base(sub1, NULL);

statsfs.c

Lines changed: 71 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
/*
1313
TODO:
14-
- kref and mutex
14+
- kref and mutex (rwsem)
1515
- files
1616
*/
1717

@@ -20,19 +20,6 @@ static int is_val_signed(struct statsfs_value *val)
2020
return val->type & STATSFS_SIGN;
2121
}
2222

23-
static struct statsfs_value *
24-
find_value_by_name(struct statsfs_value_source *src, char *val)
25-
{
26-
struct statsfs_value *entry;
27-
28-
for (entry = src->values; entry->name; entry++) {
29-
if (!strcmp(entry->name, val)) {
30-
return entry;
31-
}
32-
}
33-
return NULL;
34-
}
35-
3623
static struct statsfs_value *find_value(struct statsfs_value_source *src,
3724
struct statsfs_value *val)
3825
{
@@ -46,21 +33,14 @@ static struct statsfs_value *find_value(struct statsfs_value_source *src,
4633
return NULL;
4734
}
4835

49-
// search for an entry == arg in source
5036
static struct statsfs_value *
51-
search_statsfs_in_source(struct statsfs_source *src, struct statsfs_value *arg,
52-
struct statsfs_value_source **val_src, uint8_t is_aggr)
37+
search_value_in_source(struct statsfs_source *src, struct statsfs_value *arg,
38+
struct statsfs_value_source **val_src)
5339
{
5440
struct statsfs_value *entry;
5541
struct statsfs_value_source *src_entry;
56-
uint8_t is_entry_ok;
5742

5843
list_for_each_entry (src_entry, &src->values_head, list_element) {
59-
is_entry_ok = (src_entry->base_addr != 0) ^ is_aggr;
60-
if (!is_entry_ok) {
61-
continue;
62-
}
63-
6444
entry = find_value(src_entry, arg);
6545
if (entry) {
6646
if (val_src) {
@@ -78,45 +58,6 @@ void statsfs_source_register(struct statsfs_source *source)
7858
// TODO: creates file /sys/kernel/statsfs/kvm
7959
}
8060

81-
struct statsfs_source *statsfs_source_create(const char *fmt, ...)
82-
{
83-
va_list ap;
84-
char buf[100];
85-
struct statsfs_source *ret;
86-
87-
va_start(ap, fmt);
88-
int char_needed = vsnprintf(buf, 100, fmt, ap);
89-
va_end(ap);
90-
91-
ret = malloc(sizeof(struct statsfs_source));
92-
if (!ret) {
93-
printf("Error in creating statsfs_source!\n");
94-
return NULL;
95-
}
96-
97-
ret->name = malloc(char_needed + 1);
98-
if (!ret) {
99-
printf("Error in creating statsfs_source name!\n");
100-
return NULL;
101-
}
102-
memcpy(ret->name, buf, char_needed);
103-
ret->name[char_needed] = '\0';
104-
105-
ret->refcount = 1; //TODO: fix for kernel
106-
ret->values_head = (struct list_head)LIST_HEAD_INIT(ret->values_head);
107-
108-
ret->subordinates_head =
109-
(struct list_head)LIST_HEAD_INIT(ret->subordinates_head);
110-
ret->list_element = (struct list_head)LIST_HEAD_INIT(ret->list_element);
111-
112-
return ret;
113-
}
114-
115-
void statsfs_source_destroy(struct statsfs_source *src)
116-
{
117-
statsfs_source_put(src);
118-
}
119-
12061
static struct statsfs_value_source *create_value_source(void *base)
12162
{
12263
struct statsfs_value_source *val_src;
@@ -189,15 +130,8 @@ void statsfs_source_remove_subordinate(struct statsfs_source *source,
189130
source->name);
190131
}
191132

192-
static struct statsfs_value *
193-
search_value_in_source(struct statsfs_source *src, struct statsfs_value *arg,
194-
struct statsfs_value_source **val_src)
195-
{
196-
return search_statsfs_in_source(src, arg, val_src, 0);
197-
}
198-
199-
static uint64_t get_correct_value(struct statsfs_value_source *src,
200-
struct statsfs_value *val)
133+
static uint64_t get_simple_value(struct statsfs_value_source *src,
134+
struct statsfs_value *val)
201135
{
202136
uint64_t value_found;
203137
void *address;
@@ -259,7 +193,7 @@ static void search_all_simple_values(struct statsfs_source *src,
259193
}
260194

261195
// must be here
262-
value_found = get_correct_value(src_entry, val);
196+
value_found = get_simple_value(src_entry, val);
263197
agg->sum += value_found;
264198
agg->count++;
265199
agg->count_zero += (value_found == 0);
@@ -345,13 +279,6 @@ static void set_final_value(struct statsfs_aggregate_value *agg,
345279
}
346280
}
347281

348-
static struct statsfs_value *
349-
search_aggr_in_source(struct statsfs_source *src, struct statsfs_value *val,
350-
struct statsfs_value_source **val_src)
351-
{
352-
return search_statsfs_in_source(src, val, val_src, 1);
353-
}
354-
355282
int statsfs_source_get_value(struct statsfs_source *source,
356283
struct statsfs_value *arg, uint64_t *ret)
357284
{
@@ -367,24 +294,37 @@ int statsfs_source_get_value(struct statsfs_source *source,
367294

368295
// look in simple values
369296
found = search_value_in_source(source, arg, &src_entry);
370-
if (found) {
371-
*ret = get_correct_value(src_entry, found);
372-
return 0;
297+
298+
if (!found) {
299+
printf("ERROR: Value in source \"%s\" not found!\n",
300+
source->name);
301+
return -ENOENT;
373302
}
374303

375-
// look in aggregates
376-
found = search_aggr_in_source(source, arg, &src_entry);
377-
if (found) {
378-
BUG_ON(found->aggr_kind != STATSFS_NONE);
379-
BUG_ON(src_entry->base_addr == NULL);
380-
init_aggregate_value(&aggr, found);
381-
do_recursive_aggregation(source, found, src_entry, &aggr);
382-
set_final_value(&aggr, found, ret);
304+
if (src_entry->base_addr != NULL) {
305+
*ret = get_simple_value(src_entry, found);
383306
return 0;
384307
}
385308

386-
printf("ERROR: Value in source \"%s\" not found!\n", source->name);
387-
return -ENOENT;
309+
// look in aggregates
310+
BUG_ON(found->aggr_kind != STATSFS_NONE);
311+
init_aggregate_value(&aggr, found);
312+
do_recursive_aggregation(source, found, src_entry, &aggr);
313+
set_final_value(&aggr, found, ret);
314+
return 0;
315+
}
316+
317+
static struct statsfs_value *
318+
find_value_by_name(struct statsfs_value_source *src, char *val)
319+
{
320+
struct statsfs_value *entry;
321+
322+
for (entry = src->values; entry->name; entry++) {
323+
if (!strcmp(entry->name, val)) {
324+
return entry;
325+
}
326+
}
327+
return NULL;
388328
}
389329

390330
static struct statsfs_value *
@@ -459,4 +399,43 @@ void statsfs_source_put(struct statsfs_source *source)
459399

460400
free(source);
461401
}
402+
}
403+
404+
struct statsfs_source *statsfs_source_create(const char *fmt, ...)
405+
{
406+
va_list ap;
407+
char buf[100];
408+
struct statsfs_source *ret;
409+
410+
va_start(ap, fmt);
411+
int char_needed = vsnprintf(buf, 100, fmt, ap);
412+
va_end(ap);
413+
414+
ret = malloc(sizeof(struct statsfs_source));
415+
if (!ret) {
416+
printf("Error in creating statsfs_source!\n");
417+
return NULL;
418+
}
419+
420+
ret->name = malloc(char_needed + 1);
421+
if (!ret) {
422+
printf("Error in creating statsfs_source name!\n");
423+
return NULL;
424+
}
425+
memcpy(ret->name, buf, char_needed);
426+
ret->name[char_needed] = '\0';
427+
428+
ret->refcount = 1; //TODO: fix for kernel
429+
ret->values_head = (struct list_head)LIST_HEAD_INIT(ret->values_head);
430+
431+
ret->subordinates_head =
432+
(struct list_head)LIST_HEAD_INIT(ret->subordinates_head);
433+
ret->list_element = (struct list_head)LIST_HEAD_INIT(ret->list_element);
434+
435+
return ret;
436+
}
437+
438+
void statsfs_source_destroy(struct statsfs_source *src)
439+
{
440+
statsfs_source_put(src);
462441
}

0 commit comments

Comments
 (0)