11
11
12
12
/*
13
13
TODO:
14
- - kref and mutex
14
+ - kref and mutex (rwsem)
15
15
- files
16
16
*/
17
17
@@ -20,19 +20,6 @@ static int is_val_signed(struct statsfs_value *val)
20
20
return val -> type & STATSFS_SIGN ;
21
21
}
22
22
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
-
36
23
static struct statsfs_value * find_value (struct statsfs_value_source * src ,
37
24
struct statsfs_value * val )
38
25
{
@@ -46,21 +33,14 @@ static struct statsfs_value *find_value(struct statsfs_value_source *src,
46
33
return NULL ;
47
34
}
48
35
49
- // search for an entry == arg in source
50
36
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 )
53
39
{
54
40
struct statsfs_value * entry ;
55
41
struct statsfs_value_source * src_entry ;
56
- uint8_t is_entry_ok ;
57
42
58
43
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
-
64
44
entry = find_value (src_entry , arg );
65
45
if (entry ) {
66
46
if (val_src ) {
@@ -78,45 +58,6 @@ void statsfs_source_register(struct statsfs_source *source)
78
58
// TODO: creates file /sys/kernel/statsfs/kvm
79
59
}
80
60
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
-
120
61
static struct statsfs_value_source * create_value_source (void * base )
121
62
{
122
63
struct statsfs_value_source * val_src ;
@@ -189,15 +130,8 @@ void statsfs_source_remove_subordinate(struct statsfs_source *source,
189
130
source -> name );
190
131
}
191
132
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 )
201
135
{
202
136
uint64_t value_found ;
203
137
void * address ;
@@ -259,7 +193,7 @@ static void search_all_simple_values(struct statsfs_source *src,
259
193
}
260
194
261
195
// must be here
262
- value_found = get_correct_value (src_entry , val );
196
+ value_found = get_simple_value (src_entry , val );
263
197
agg -> sum += value_found ;
264
198
agg -> count ++ ;
265
199
agg -> count_zero += (value_found == 0 );
@@ -345,13 +279,6 @@ static void set_final_value(struct statsfs_aggregate_value *agg,
345
279
}
346
280
}
347
281
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
-
355
282
int statsfs_source_get_value (struct statsfs_source * source ,
356
283
struct statsfs_value * arg , uint64_t * ret )
357
284
{
@@ -367,24 +294,37 @@ int statsfs_source_get_value(struct statsfs_source *source,
367
294
368
295
// look in simple values
369
296
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 ;
373
302
}
374
303
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 );
383
306
return 0 ;
384
307
}
385
308
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 ;
388
328
}
389
329
390
330
static struct statsfs_value *
@@ -459,4 +399,43 @@ void statsfs_source_put(struct statsfs_source *source)
459
399
460
400
free (source );
461
401
}
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 );
462
441
}
0 commit comments