-
Notifications
You must be signed in to change notification settings - Fork 0
/
stats.c
82 lines (69 loc) · 1.72 KB
/
stats.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
#include "stats.h"
#include <stdio.h>
#include <stdlib.h>
server_stats _s_stats;
void stats_server_init()
{
server_stats s_stats;
s_stats.served_clients = 0;
s_stats.connected_clients = 0;
s_stats.n_objects = 0;
s_stats.total_size = 0;
pthread_mutex_init(&(s_stats.mux), NULL);
_s_stats = s_stats;
}
void stats_server_incr_served()
{
pthread_mutex_lock(&(_s_stats.mux));
_s_stats.served_clients++;
pthread_mutex_unlock(&(_s_stats.mux));
}
void stats_server_incr_client()
{
pthread_mutex_lock(&(_s_stats.mux));
_s_stats.connected_clients++;
pthread_mutex_unlock(&(_s_stats.mux));
}
void stats_server_decr_client()
{
pthread_mutex_lock(&(_s_stats.mux));
_s_stats.connected_clients--;
pthread_mutex_unlock(&(_s_stats.mux));
}
void stats_server_incr_obj()
{
pthread_mutex_lock(&(_s_stats.mux));
_s_stats.n_objects++;
pthread_mutex_unlock(&(_s_stats.mux));
}
void stats_server_decr_obj()
{
pthread_mutex_lock(&(_s_stats.mux));
_s_stats.n_objects--;
pthread_mutex_unlock(&(_s_stats.mux));
}
void stats_server_incr_size(int len)
{
pthread_mutex_lock(&(_s_stats.mux));
_s_stats.total_size += len;
pthread_mutex_unlock(&(_s_stats.mux));
}
void stats_server_decr_size(int len)
{
pthread_mutex_lock(&(_s_stats.mux));
_s_stats.total_size -= len;
pthread_mutex_unlock(&(_s_stats.mux));
}
void stats_server_print()
{
pthread_mutex_lock(&(_s_stats.mux));
fprintf(stdout, "Served clients: %d\n", _s_stats.served_clients);
fprintf(stdout, "Connected clients: %d\n", _s_stats.connected_clients);
fprintf(stdout, "Number objects: %d\n", _s_stats.n_objects);
fprintf(stdout, "Total size: %d\n", _s_stats.total_size);
pthread_mutex_unlock(&(_s_stats.mux));
}
void stats_server_destroy()
{
pthread_mutex_destroy(&(_s_stats.mux));
}