Flip call (NULL is more common). Also add in helgrind and fix calloc.
[awesomized/libmemcached] / libmemcached / memcached_analyze.c
1 #include "common.h"
2
3 static void calc_largest_consumption(memcached_analysis_st *result,
4 const uint32_t server_num,
5 const uint64_t nbytes)
6 {
7 if (result->most_used_bytes < nbytes)
8 {
9 result->most_used_bytes= nbytes;
10 result->most_consumed_server= server_num;
11 }
12 }
13
14 static void calc_oldest_node(memcached_analysis_st *result,
15 const uint32_t server_num,
16 const uint32_t uptime)
17 {
18 if (result->longest_uptime < uptime)
19 {
20 result->longest_uptime= uptime;
21 result->oldest_server= server_num;
22 }
23 }
24
25 static void calc_least_free_node(memcached_analysis_st *result,
26 const uint32_t server_num,
27 const long max_allowed_bytes,
28 const long used_bytes)
29 {
30 uint64_t remaining_bytes= (uint64_t)max_allowed_bytes - used_bytes;
31
32 if (result->least_remaining_bytes == 0 ||
33 remaining_bytes < result->least_remaining_bytes)
34 {
35 result->least_remaining_bytes= remaining_bytes;
36 result->least_free_server= server_num;
37 }
38 }
39
40 static void calc_average_item_size(memcached_analysis_st *result,
41 const uint64_t total_items,
42 const uint64_t total_bytes)
43 {
44 if (total_items > 0 && total_bytes > 0)
45 result->average_item_size= total_bytes / total_items;
46 }
47
48 static void calc_hit_ratio(memcached_analysis_st *result,
49 const uint64_t total_get_hits,
50 const uint64_t total_get_cmds)
51 {
52 if (total_get_hits == 0 || total_get_cmds == 0)
53 {
54 result->pool_hit_ratio= 0;
55 return;
56 }
57
58 double temp= (double)total_get_hits/total_get_cmds;
59 result->pool_hit_ratio= temp * 100;
60 }
61
62 memcached_analysis_st *memcached_analyze(memcached_st *memc,
63 memcached_stat_st *stat,
64 memcached_return *error)
65 {
66 uint64_t total_items= 0, total_bytes= 0;
67 uint64_t total_get_cmds= 0, total_get_hits= 0;
68 uint32_t server_count, x;
69 memcached_analysis_st *result;
70
71 *error= MEMCACHED_SUCCESS;
72 server_count= memcached_server_count(memc);
73 result= (memcached_analysis_st*)calloc(1, sizeof(memcached_analysis_st)
74 * (memc->number_of_hosts));
75 if (!result)
76 {
77 *error= MEMCACHED_MEMORY_ALLOCATION_FAILURE;
78 return NULL;
79 }
80
81 for (x= 0; x < server_count; x++)
82 {
83 calc_largest_consumption(result, x, stat[x].bytes);
84 calc_oldest_node(result, x, stat[x].uptime);
85 calc_least_free_node(result, x, stat[x].limit_maxbytes, stat[x].bytes);
86
87 total_get_hits+= stat[x].get_hits;
88 total_get_cmds+= stat[x].cmd_get;
89 total_items+= stat[x].curr_items;
90 total_bytes+= stat[x].bytes;
91 }
92
93 calc_average_item_size(result, total_items, total_bytes);
94 calc_hit_ratio(result, total_get_hits, total_get_cmds);
95
96 return result;
97 }