pkg_stats.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. /*
  2. * Copyright (C) 2010 Daniel-Constantin Mierla (asipto.com)
  3. *
  4. * This file is part of Kamailio, a free SIP server.
  5. *
  6. * Kamailio is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * Kamailio is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. *
  20. */
  21. /*!
  22. * \file
  23. * \brief KEX :: Kamailio private memory pool statistics
  24. * \ingroup kex
  25. */
  26. #include <stdio.h>
  27. #include <string.h>
  28. #include <stdlib.h>
  29. #include <unistd.h>
  30. #include "../../dprint.h"
  31. #include "../../ut.h"
  32. #include "../../pt.h"
  33. #include "../../events.h"
  34. #include "../../mem/mem.h"
  35. #include "../../mem/shm_mem.h"
  36. #include "../../rpc.h"
  37. #include "../../rpc_lookup.h"
  38. /**
  39. *
  40. */
  41. typedef struct pkg_proc_stats {
  42. int rank;
  43. unsigned int pid;
  44. unsigned long used;
  45. unsigned long available;
  46. unsigned long real_used;
  47. unsigned long total_frags;
  48. unsigned long total_size;
  49. } pkg_proc_stats_t;
  50. /**
  51. *
  52. */
  53. static pkg_proc_stats_t *_pkg_proc_stats_list = NULL;
  54. /**
  55. *
  56. */
  57. static int _pkg_proc_stats_no = 0;
  58. /**
  59. *
  60. */
  61. int pkg_proc_stats_init(void)
  62. {
  63. _pkg_proc_stats_no = get_max_procs();
  64. if(_pkg_proc_stats_no<=0)
  65. return -1;
  66. if(_pkg_proc_stats_list!=NULL)
  67. return -1;
  68. _pkg_proc_stats_list = (pkg_proc_stats_t*)shm_malloc(
  69. _pkg_proc_stats_no*sizeof(pkg_proc_stats_t));
  70. if(_pkg_proc_stats_list==NULL)
  71. return -1;
  72. memset(_pkg_proc_stats_list, 0,
  73. _pkg_proc_stats_no*sizeof(pkg_proc_stats_t));
  74. return 0;
  75. }
  76. /**
  77. *
  78. */
  79. int pkg_proc_stats_myinit(int rank)
  80. {
  81. struct mem_info info;
  82. if(_pkg_proc_stats_list==NULL)
  83. return -1;
  84. if(process_no>=_pkg_proc_stats_no)
  85. return -1;
  86. _pkg_proc_stats_list[process_no].pid = (unsigned int)my_pid();
  87. _pkg_proc_stats_list[process_no].rank = rank;
  88. /* init pkg usage values */
  89. pkg_info(&info);
  90. _pkg_proc_stats_list[process_no].available = info.free;
  91. _pkg_proc_stats_list[process_no].used = info.used;
  92. _pkg_proc_stats_list[process_no].real_used = info.real_used;
  93. _pkg_proc_stats_list[process_no].total_size = info.total_size;
  94. _pkg_proc_stats_list[process_no].total_frags = info.total_frags;
  95. return 0;
  96. }
  97. /**
  98. *
  99. */
  100. int pkg_proc_stats_destroy(void)
  101. {
  102. if(_pkg_proc_stats_list==NULL)
  103. return -1;
  104. shm_free(_pkg_proc_stats_list);
  105. _pkg_proc_stats_list = 0;
  106. _pkg_proc_stats_no = 0;
  107. return 0;
  108. }
  109. /**
  110. *
  111. */
  112. static int pkg_proc_update_stats(void *data)
  113. {
  114. struct mem_info info;
  115. if(unlikely(_pkg_proc_stats_list==NULL))
  116. return -1;
  117. if(unlikely(process_no>=_pkg_proc_stats_no))
  118. return -1;
  119. pkg_info(&info);
  120. _pkg_proc_stats_list[process_no].available = info.free;
  121. _pkg_proc_stats_list[process_no].used = info.used;
  122. _pkg_proc_stats_list[process_no].real_used = info.real_used;
  123. _pkg_proc_stats_list[process_no].total_frags = info.total_frags;
  124. return 0;
  125. }
  126. /**
  127. *
  128. */
  129. int register_pkg_proc_stats(void)
  130. {
  131. sr_event_register_cb(SREV_PKG_UPDATE_STATS, pkg_proc_update_stats);
  132. return 0;
  133. }
  134. /**
  135. *
  136. */
  137. static const char* rpc_pkg_stats_doc[2] = {
  138. "Private memory (pkg) statistics per process",
  139. 0
  140. };
  141. /**
  142. *
  143. */
  144. int pkg_proc_get_pid_index(unsigned int pid)
  145. {
  146. int i;
  147. for(i=0; i<_pkg_proc_stats_no; i++)
  148. {
  149. if(_pkg_proc_stats_list[i].pid == pid)
  150. return i;
  151. }
  152. return -1;
  153. }
  154. /**
  155. *
  156. */
  157. static void rpc_pkg_stats(rpc_t* rpc, void* ctx)
  158. {
  159. int i;
  160. int limit;
  161. int cval;
  162. str cname;
  163. void* th;
  164. int mode;
  165. if(_pkg_proc_stats_list==NULL)
  166. {
  167. rpc->fault(ctx, 500, "Not initialized");
  168. return;
  169. }
  170. i = 0;
  171. mode = 0;
  172. cval = 0;
  173. limit = _pkg_proc_stats_no;
  174. if (rpc->scan(ctx, "*S", &cname) == 1)
  175. {
  176. if(cname.len==3 && strncmp(cname.s, "pid", 3)==0)
  177. mode = 1;
  178. else if(cname.len==4 && strncmp(cname.s, "rank", 4)==0)
  179. mode = 2;
  180. else if(cname.len==5 && strncmp(cname.s, "index", 5)==0)
  181. mode = 3;
  182. else {
  183. rpc->fault(ctx, 500, "Invalid filter type");
  184. return;
  185. }
  186. if (rpc->scan(ctx, "d", &cval) < 1)
  187. {
  188. rpc->fault(ctx, 500, "One more parameter expected");
  189. return;
  190. }
  191. if(mode==1)
  192. {
  193. i = pkg_proc_get_pid_index((unsigned int)cval);
  194. if(i<0)
  195. {
  196. rpc->fault(ctx, 500, "No such pid");
  197. return;
  198. }
  199. limit = i + 1;
  200. } else if(mode==3) {
  201. i=cval;
  202. limit = i + 1;
  203. }
  204. }
  205. for(; i<limit; i++)
  206. {
  207. /* add entry node */
  208. if(mode!=2 || _pkg_proc_stats_list[i].rank==cval)
  209. {
  210. if (rpc->add(ctx, "{", &th) < 0)
  211. {
  212. rpc->fault(ctx, 500, "Internal error creating rpc");
  213. return;
  214. }
  215. if(rpc->struct_add(th, "dddddddd",
  216. "entry", i,
  217. "pid", _pkg_proc_stats_list[i].pid,
  218. "rank", _pkg_proc_stats_list[i].rank,
  219. "used", _pkg_proc_stats_list[i].used,
  220. "free", _pkg_proc_stats_list[i].available,
  221. "real_used", _pkg_proc_stats_list[i].real_used,
  222. "total_size", _pkg_proc_stats_list[i].total_size,
  223. "total_frags", _pkg_proc_stats_list[i].total_frags
  224. )<0)
  225. {
  226. rpc->fault(ctx, 500, "Internal error creating rpc");
  227. return;
  228. }
  229. }
  230. }
  231. }
  232. /**
  233. *
  234. */
  235. rpc_export_t kex_pkg_rpc[] = {
  236. {"pkg.stats", rpc_pkg_stats, rpc_pkg_stats_doc, RET_ARRAY},
  237. {0, 0, 0, 0}
  238. };
  239. /**
  240. *
  241. */
  242. int pkg_proc_stats_init_rpc(void)
  243. {
  244. if (rpc_register_array(kex_pkg_rpc)!=0)
  245. {
  246. LM_ERR("failed to register RPC commands\n");
  247. return -1;
  248. }
  249. return 0;
  250. }