forked from Qihoo360/Atlas
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchassis-stats.c
More file actions
76 lines (55 loc) · 2.08 KB
/
chassis-stats.c
File metadata and controls
76 lines (55 loc) · 2.08 KB
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
/* $%BEGINLICENSE%$
Copyright (c) 2008, Oracle and/or its affiliates. All rights reserved.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; version 2 of the
License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
02110-1301 USA
$%ENDLICENSE%$ */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <glib.h>
#include "chassis-stats.h"
chassis_stats_t *chassis_global_stats = NULL;
chassis_stats_t * chassis_stats_new(void) {
if (chassis_global_stats != NULL) return chassis_global_stats;
chassis_global_stats = g_new0(chassis_stats_t, 1);
g_debug("%s: created new global chassis stats at %p", G_STRLOC, (void*)chassis_global_stats);
return chassis_global_stats;
}
void chassis_stats_free(chassis_stats_t *stats) {
if (!stats) return;
if (stats == chassis_global_stats) {
g_free(stats);
chassis_global_stats = NULL;
} else {
/* there should only be one glbal chassis stats struct at any given time */
g_assert_not_reached();
}
}
GHashTable* chassis_stats_get(chassis_stats_t *stats){
GHashTable *stats_hash;
if (stats == NULL) return NULL;
/* NOTE: the keys are strdup'ed, the values are simply integers */
stats_hash = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, NULL);
#define STR(x) #x
#define N(x) g_strdup(x)
#define ADD_STAT(x) g_hash_table_insert(stats_hash, N( STR(x)), GUINT_TO_POINTER(g_atomic_int_get(&(stats->x))))
#define ADD_ALLOC_STAT(x) ADD_STAT(x ## _alloc); ADD_STAT(x ## _free);
ADD_ALLOC_STAT(lua_mem);
ADD_STAT(lua_mem_bytes);
ADD_STAT(lua_mem_bytes_max);
#undef N
#undef STR
#undef ADD_STAT
#undef ADD_ALLOC_STAT
return stats_hash;
}