1 #include <libmemcached/common.h>
3 static void calc_largest_consumption(memcached_analysis_st
*result
,
4 const uint32_t server_num
,
7 if (result
->most_used_bytes
< nbytes
)
9 result
->most_used_bytes
= nbytes
;
10 result
->most_consumed_server
= server_num
;
14 static void calc_oldest_node(memcached_analysis_st
*result
,
15 const uint32_t server_num
,
16 const uint32_t uptime
)
18 if (result
->longest_uptime
< uptime
)
20 result
->longest_uptime
= uptime
;
21 result
->oldest_server
= server_num
;
25 static void calc_least_free_node(memcached_analysis_st
*result
,
26 const uint32_t server_num
,
27 const uint64_t max_allowed_bytes
,
28 const uint64_t used_bytes
)
30 uint64_t remaining_bytes
= max_allowed_bytes
- used_bytes
;
32 if (result
->least_remaining_bytes
== 0 ||
33 remaining_bytes
< result
->least_remaining_bytes
)
35 result
->least_remaining_bytes
= remaining_bytes
;
36 result
->least_free_server
= server_num
;
40 static void calc_average_item_size(memcached_analysis_st
*result
,
41 const uint64_t total_items
,
42 const uint64_t total_bytes
)
44 if (total_items
> 0 && total_bytes
> 0)
46 result
->average_item_size
= (uint32_t) (total_bytes
/ total_items
);
50 static void calc_hit_ratio(memcached_analysis_st
*result
,
51 const uint64_t total_get_hits
,
52 const uint64_t total_get_cmds
)
54 if (total_get_hits
== 0 || total_get_cmds
== 0)
56 result
->pool_hit_ratio
= 0;
60 double temp
= double(total_get_hits
) / total_get_cmds
;
61 result
->pool_hit_ratio
= temp
* 100;
64 memcached_analysis_st
*memcached_analyze(memcached_st
*shell
,
65 memcached_stat_st
*memc_stat
,
66 memcached_return_t
*error
)
68 Memcached
* memc
= memcached2Memcached(shell
);
69 uint64_t total_items
= 0, total_bytes
= 0;
70 uint64_t total_get_cmds
= 0, total_get_hits
= 0;
72 if (memc
== NULL
or memc_stat
== NULL
)
77 memcached_return_t not_used
;
83 *error
= MEMCACHED_SUCCESS
;
84 uint32_t server_count
= memcached_server_count(memc
);
85 memcached_analysis_st
*result
= (memcached_analysis_st
*)libmemcached_xcalloc(memc
,
86 memcached_server_count(memc
),
87 memcached_analysis_st
);
91 *error
= MEMCACHED_MEMORY_ALLOCATION_FAILURE
;
97 for (uint32_t x
= 0; x
< server_count
; x
++)
99 calc_largest_consumption(result
, x
, memc_stat
[x
].bytes
);
100 calc_oldest_node(result
, x
, uint32_t(memc_stat
[x
].uptime
));
101 calc_least_free_node(result
, x
,
102 memc_stat
[x
].limit_maxbytes
,
105 total_get_hits
+= memc_stat
[x
].get_hits
;
106 total_get_cmds
+= memc_stat
[x
].cmd_get
;
107 total_items
+= memc_stat
[x
].curr_items
;
108 total_bytes
+= memc_stat
[x
].bytes
;
111 calc_average_item_size(result
, total_items
, total_bytes
);
112 calc_hit_ratio(result
, total_get_hits
, total_get_cmds
);
117 void memcached_analyze_free(memcached_analysis_st
*ptr
)
119 libmemcached_free(ptr
->root
, ptr
);