File: stats.c

package info (click to toggle)
c-icap 1%3A0.5.3-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 3,600 kB
  • sloc: ansic: 27,220; sh: 4,415; makefile: 242; perl: 95; awk: 10
file content (326 lines) | stat: -rw-r--r-- 9,664 bytes parent folder | download | duplicates (5)
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
/*
 *  Copyright (C) 2004-2008 Christos Tsantilas
 *
 *  This program is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2.1 of the License, or (at your option) any later version.
 *
 *  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
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 *  MA  02110-1301  USA.
 */


#include "common.h"
#include "stats.h"
#include <assert.h>

struct stat_entry_list STAT_INT64 = {NULL, 0, 0};
struct stat_entry_list STAT_KBS = {NULL, 0, 0};
struct stat_groups_list STAT_GROUPS = {NULL, 0, 0};;

struct stat_area *STATS = NULL;

#define STEP 128

int ci_stat_memblock_size(void)
{
    return _CI_ALIGN(sizeof(struct stat_memblock))+STAT_INT64.entries_num*sizeof(uint64_t)+STAT_KBS.entries_num*sizeof(kbs_t);
}

int stat_entry_by_name(struct stat_entry_list *list, const char *label);

int stat_entry_add(struct stat_entry_list *list,const char *label, int type, int gid)
{
    struct stat_entry *l;
    int indx;

    if (!list)
        return -1;

    indx = stat_entry_by_name(list, label);
    if (indx >= 0 )
        return indx;

    if (list->size == list->entries_num) {

        if (list->size == 0) {
            list->entries = malloc(STEP*sizeof(struct stat_entry));
            if (!list->entries)
                return -1;
        } else {
            l = realloc(list->entries, (list->size+STEP)*sizeof(struct stat_entry));
            if (!l)
                return -1;
            list->entries = l;
        }
        list->size += STEP;
    }
    list->entries[list->entries_num].label = strdup(label);
    list->entries[list->entries_num].type = type;
    list->entries[list->entries_num].gid = gid;
    indx = list->entries_num;
    list->entries_num++;
    return indx;
}

void stat_entry_release_list(struct stat_entry_list *list)
{
    int i;
    if (!list->entries)
        return;
    for (i = 0; i < list->entries_num; i++)
        free(list->entries[i].label);
    free(list->entries);
    list->entries = NULL;
    list->size = 0;
    list->entries_num = 0;
}

int stat_entry_by_name(struct stat_entry_list *list, const char *label)
{
    int i;
    if (!list->entries)
        return -1;

    for (i = 0; i < list->entries_num; i++)
        if (strcmp(label, list->entries[i].label) == 0) return i;

    return -1;
}

int stat_group_add(char *group)
{
    char **group_list;
    int gid = 0;

    for (gid = 0; gid < STAT_GROUPS.entries_num; gid++) {
        if (strcmp(STAT_GROUPS.groups[gid], group) == 0)
            return gid;
    }

    if (STAT_GROUPS.size == 0) {
        STAT_GROUPS.groups = malloc(STEP * sizeof(char *));
        if (!STAT_GROUPS.groups)
            return -1;
        STAT_GROUPS.size = STEP;
    } else if (STAT_GROUPS.size == STAT_GROUPS.entries_num) {
        group_list = realloc(STAT_GROUPS.groups, (STAT_GROUPS.size+STEP)*sizeof(char *));
        if (!group_list)
            return -1;
        STAT_GROUPS.groups = group_list;
        STAT_GROUPS.size += STEP;
    }
    STAT_GROUPS.groups[STAT_GROUPS.entries_num] = strdup(group);
    gid = STAT_GROUPS.entries_num;
    STAT_GROUPS.entries_num++;
    return gid;
}

int ci_stat_entry_register(char *label, int type, char *group)
{
    int gid;

    gid = stat_group_add(group);
    if (gid < 0)
        return -1;

    if (type == STAT_INT64_T) {
        return stat_entry_add(&STAT_INT64, label, type, gid);
    } else if (type == STAT_KBS_T) {
        return stat_entry_add(&STAT_KBS, label, type, gid);
    }
    return -1;
}

void ci_stat_entry_release_lists()
{
    stat_entry_release_list(&STAT_INT64);
    stat_entry_release_list(&STAT_KBS);
}

void ci_stat_attach_mem(void *mem_block,int size,void (*release_mem)(void *))
{
    if (STATS)
        return;

    STATS = ci_stat_area_construct(mem_block, size, release_mem);
}

void ci_stat_release()
{
    if (!STATS)
        return;
    ci_stat_area_destroy(STATS);
    STATS = NULL;
}

void ci_stat_uint64_inc(int ID, int count)
{
    if (!STATS || !STATS->mem_block)
        return;
    if (ID < 0 || ID >= STATS->mem_block->counters64_size)
        return;
    ci_thread_mutex_lock(&STATS->mtx);
    STATS->mem_block->counters64[ID] += count;
    ci_thread_mutex_unlock(&STATS->mtx);
}

void ci_stat_kbs_inc(int ID, int count)
{
    if (!STATS->mem_block)
        return;

    if (ID < 0 || ID >= STATS->mem_block->counterskbs_size)
        return;

    ci_thread_mutex_lock(&STATS->mtx);
    STATS->mem_block->counterskbs[ID].bytes += count;
    STATS->mem_block->counterskbs[ID].kb += (STATS->mem_block->counterskbs[ID].bytes >> 10);
    STATS->mem_block->counterskbs[ID].bytes &= 0x3FF;
    ci_thread_mutex_unlock(&STATS->mtx);
}


/***********************************************
   Low level functions
*/
struct stat_area *ci_stat_area_construct(void *mem_block, int size, void (*release_mem)(void *))
{
    struct stat_area  *area = NULL;
    if (size < ci_stat_memblock_size() )
        return NULL;

    area = malloc(sizeof(struct stat_area));
    if (!area)
        return NULL;

    assert(((struct stat_memblock *)mem_block)->sig == MEMBLOCK_SIG);

    ci_thread_mutex_init(&(area->mtx));
    area->mem_block = mem_block;
    area->release_mem = release_mem;
    area->mem_block->counters64 = mem_block + _CI_ALIGN(sizeof(struct stat_memblock));
    area->mem_block->counterskbs = mem_block + _CI_ALIGN(sizeof(struct stat_memblock)) + STAT_INT64.entries_num*sizeof(uint64_t);
    area->mem_block->counters64_size =  STAT_INT64.entries_num;
    area->mem_block->counterskbs_size = STAT_KBS.entries_num;
    ci_stat_area_reset(area);
    return area;
}

void ci_stat_area_reset(struct stat_area *area)
{
    int i;

    ci_thread_mutex_lock(&(area->mtx));
    for (i = 0; i < area->mem_block->counters64_size; i++)
        area->mem_block->counters64[i] = 0;
    for (i = 0; i < area->mem_block->counterskbs_size; i++) {
        area->mem_block->counterskbs[i].kb = 0;
        area->mem_block->counterskbs[i].bytes = 0;
    }
    ci_thread_mutex_unlock(&(area->mtx));
}


void ci_stat_area_destroy(struct stat_area  *area)
{
    ci_thread_mutex_destroy(&(area->mtx));
    if (area->release_mem)
        area->release_mem(area->mem_block);
    free(area);
}

/*Does not realy needed*/
void ci_stat_area_uint64_inc(struct stat_area *area,int ID, int count)
{
    if (!area->mem_block)
        return;
    if (ID < 0 || ID >= area->mem_block->counters64_size)
        return;
    ci_thread_mutex_lock(&area->mtx);
    area->mem_block->counters64[ID] += count;
    ci_thread_mutex_unlock(&area->mtx);
}

/*Does not realy needed*/
void ci_stat_area_kbs_inc(struct stat_area *area,int ID, int count)
{
    if (!area->mem_block)
        return;

    if (ID < 0 || ID >= area->mem_block->counterskbs_size)
        return;

    ci_thread_mutex_lock(&area->mtx);
    area->mem_block->counterskbs[ID].bytes += count;
    area->mem_block->counterskbs[ID].kb += (area->mem_block->counterskbs[ID].bytes >> 10);
    area->mem_block->counterskbs[ID].bytes &= 0x3FF;
    ci_thread_mutex_unlock(&area->mtx);
}

/*Make a memblock area from continues memory block*/
void stat_memblock_fix(struct stat_memblock *mem_block)
{
    assert(mem_block->sig == MEMBLOCK_SIG);
    mem_block->counters64_size =  STAT_INT64.entries_num;
    mem_block->counterskbs_size = STAT_KBS.entries_num;
    mem_block->counters64 = (void *)mem_block + _CI_ALIGN(sizeof(struct stat_memblock));
    mem_block->counterskbs = (void *)mem_block + _CI_ALIGN(sizeof(struct stat_memblock))
                             + mem_block->counters64_size*sizeof(uint64_t);
}

/*Reconstruct a memblock which is located to a continues memory block*/
void stat_memblock_reconstruct(struct stat_memblock *mem_block)
{
    assert(mem_block->sig == MEMBLOCK_SIG);
    mem_block->counters64 = (void *)mem_block + _CI_ALIGN(sizeof(struct stat_memblock));
    mem_block->counterskbs = (void *)mem_block + _CI_ALIGN(sizeof(struct stat_memblock))
                             + mem_block->counters64_size*sizeof(uint64_t);
}

void ci_stat_memblock_reset(struct stat_memblock *block)
{
    int i;
    for (i = 0; i < block->counters64_size; i++)
        block->counters64[i] = 0;
    for (i = 0; i < block->counterskbs_size; i++) {
        block->counterskbs[i].kb = 0;
        block->counterskbs[i].bytes = 0;
    }
}

void ci_stat_memblock_merge(struct stat_memblock *dest_block, struct stat_memblock *mem_block)
{
    int i;
    if (!dest_block || !mem_block)
        return;

    for (i = 0; i < dest_block->counters64_size && i < mem_block->counters64_size; i++)
        dest_block->counters64[i] += mem_block->counters64[i];

    for (i = 0; i < dest_block->counterskbs_size && i < mem_block->counterskbs_size; i++) {
        dest_block->counterskbs[i].kb += mem_block->counterskbs[i].kb;
        dest_block->counterskbs[i].bytes += mem_block->counterskbs[i].bytes;
        dest_block->counterskbs[i].kb += (dest_block->counterskbs[i].bytes >> 10);
        dest_block->counterskbs[i].bytes &= 0x3FF;
    }
}



void ci_stat_area_merge(struct stat_area *dest, struct stat_area *src)
{
    if (!dest->mem_block || !src->mem_block)
        return;

    ci_stat_memblock_merge(dest->mem_block, src->mem_block);
}