memory.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. /*
  2. * Memory management routine
  3. * Copyright (C) 1998 Kunihiro Ishiguro
  4. *
  5. * This file is part of GNU Zebra.
  6. *
  7. * GNU Zebra is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License as published by the
  9. * Free Software Foundation; either version 2, or (at your option) any
  10. * later version.
  11. *
  12. * GNU Zebra is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with GNU Zebra; see the file COPYING. If not, write to the Free
  19. * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
  20. * 02111-1307, USA.
  21. */
  22. #include <zebra.h>
  23. #include "log.h"
  24. #include "memory.h"
  25. static void alloc_inc (int);
  26. static void alloc_dec (int);
  27. static void log_memstats(int log_priority);
  28. static struct message mstr [] =
  29. {
  30. { MTYPE_THREAD, "thread" },
  31. { MTYPE_THREAD_MASTER, "thread_master" },
  32. { MTYPE_VECTOR, "vector" },
  33. { MTYPE_VECTOR_INDEX, "vector_index" },
  34. { MTYPE_IF, "interface" },
  35. { 0, NULL },
  36. };
  37. /* Fatal memory allocation error occured. */
  38. static void
  39. zerror (const char *fname, int type, size_t size)
  40. {
  41. zlog_err ("%s : can't allocate memory for `%s' size %d: %s\n",
  42. fname, lookup (mstr, type), (int) size, safe_strerror(errno));
  43. log_memstats(LOG_WARNING);
  44. /* N.B. It might be preferable to call zlog_backtrace_sigsafe here, since
  45. that function should definitely be safe in an OOM condition. But
  46. unfortunately zlog_backtrace_sigsafe does not support syslog logging at
  47. this time... */
  48. zlog_backtrace(LOG_WARNING);
  49. abort();
  50. }
  51. /* Memory allocation. */
  52. void *
  53. zmalloc (int type, size_t size)
  54. {
  55. void *memory;
  56. memory = malloc (size);
  57. if (memory == NULL)
  58. zerror ("malloc", type, size);
  59. alloc_inc (type);
  60. return memory;
  61. }
  62. /* Memory allocation with num * size with cleared. */
  63. void *
  64. zcalloc (int type, size_t size)
  65. {
  66. void *memory;
  67. memory = calloc (1, size);
  68. if (memory == NULL)
  69. zerror ("calloc", type, size);
  70. alloc_inc (type);
  71. return memory;
  72. }
  73. /* Memory reallocation. */
  74. void *
  75. zrealloc (int type, void *ptr, size_t size)
  76. {
  77. void *memory;
  78. memory = realloc (ptr, size);
  79. if (memory == NULL)
  80. zerror ("realloc", type, size);
  81. return memory;
  82. }
  83. /* Memory free. */
  84. void
  85. zfree (int type, void *ptr)
  86. {
  87. alloc_dec (type);
  88. free (ptr);
  89. }
  90. /* String duplication. */
  91. char *
  92. zstrdup (int type, const char *str)
  93. {
  94. void *dup;
  95. dup = strdup (str);
  96. if (dup == NULL)
  97. zerror ("strdup", type, strlen (str));
  98. alloc_inc (type);
  99. return dup;
  100. }
  101. #ifdef MEMORY_LOG
  102. static struct
  103. {
  104. const char *name;
  105. unsigned long alloc;
  106. unsigned long t_malloc;
  107. unsigned long c_malloc;
  108. unsigned long t_calloc;
  109. unsigned long c_calloc;
  110. unsigned long t_realloc;
  111. unsigned long t_free;
  112. unsigned long c_strdup;
  113. } mstat [MTYPE_MAX];
  114. static void
  115. mtype_log (char *func, void *memory, const char *file, int line, int type)
  116. {
  117. zlog_debug ("%s: %s %p %s %d", func, lookup (mstr, type), memory, file, line);
  118. }
  119. void *
  120. mtype_zmalloc (const char *file, int line, int type, size_t size)
  121. {
  122. void *memory;
  123. mstat[type].c_malloc++;
  124. mstat[type].t_malloc++;
  125. memory = zmalloc (type, size);
  126. mtype_log ("zmalloc", memory, file, line, type);
  127. return memory;
  128. }
  129. void *
  130. mtype_zcalloc (const char *file, int line, int type, size_t size)
  131. {
  132. void *memory;
  133. mstat[type].c_calloc++;
  134. mstat[type].t_calloc++;
  135. memory = zcalloc (type, size);
  136. mtype_log ("xcalloc", memory, file, line, type);
  137. return memory;
  138. }
  139. void *
  140. mtype_zrealloc (const char *file, int line, int type, void *ptr, size_t size)
  141. {
  142. void *memory;
  143. /* Realloc need before allocated pointer. */
  144. mstat[type].t_realloc++;
  145. memory = zrealloc (type, ptr, size);
  146. mtype_log ("xrealloc", memory, file, line, type);
  147. return memory;
  148. }
  149. /* Important function. */
  150. void
  151. mtype_zfree (const char *file, int line, int type, void *ptr)
  152. {
  153. mstat[type].t_free++;
  154. mtype_log ("xfree", ptr, file, line, type);
  155. zfree (type, ptr);
  156. }
  157. char *
  158. mtype_zstrdup (const char *file, int line, int type, const char *str)
  159. {
  160. char *memory;
  161. mstat[type].c_strdup++;
  162. memory = zstrdup (type, str);
  163. mtype_log ("xstrdup", memory, file, line, type);
  164. return memory;
  165. }
  166. #else
  167. static struct
  168. {
  169. char *name;
  170. unsigned long alloc;
  171. } mstat [MTYPE_MAX];
  172. #endif /* MTPYE_LOG */
  173. /* Increment allocation counter. */
  174. static void
  175. alloc_inc (int type)
  176. {
  177. mstat[type].alloc++;
  178. }
  179. /* Decrement allocation counter. */
  180. static void
  181. alloc_dec (int type)
  182. {
  183. mstat[type].alloc--;
  184. }
  185. /* Looking up memory status from vty interface. */
  186. #include "vector.h"
  187. #include "vty.h"
  188. #include "command.h"
  189. static void
  190. log_memstats(int pri)
  191. {
  192. struct mlist *ml;
  193. for (ml = mlists; ml->list; ml++)
  194. {
  195. struct memory_list *m;
  196. zlog (NULL, pri, "Memory utilization in module %s:", ml->name);
  197. for (m = ml->list; m->index >= 0; m++)
  198. if (m->index && mstat[m->index].alloc)
  199. zlog (NULL, pri, " %-30s: %10ld", m->format, mstat[m->index].alloc);
  200. }
  201. }
  202. static struct memory_list memory_list_separator[] =
  203. {
  204. { 0, NULL},
  205. {-1, NULL}
  206. };
  207. static void
  208. show_memory_vty (struct vty *vty, struct memory_list *list)
  209. {
  210. struct memory_list *m;
  211. for (m = list; m->index >= 0; m++)
  212. if (m->index == 0)
  213. vty_out (vty, "-----------------------------\r\n");
  214. else
  215. vty_out (vty, "%-30s: %10ld\r\n", m->format, mstat[m->index].alloc);
  216. }
  217. DEFUN (show_memory_all,
  218. show_memory_all_cmd,
  219. "show memory all",
  220. "Show running system information\n"
  221. "Memory statistics\n"
  222. "All memory statistics\n")
  223. {
  224. struct mlist *ml;
  225. for (ml = mlists; ml->list; ml++)
  226. {
  227. if (ml != mlists)
  228. show_memory_vty (vty, memory_list_separator);
  229. show_memory_vty (vty, ml->list);
  230. }
  231. return CMD_SUCCESS;
  232. }
  233. ALIAS (show_memory_all,
  234. show_memory_cmd,
  235. "show memory",
  236. "Show running system information\n"
  237. "Memory statistics\n")
  238. DEFUN (show_memory_lib,
  239. show_memory_lib_cmd,
  240. "show memory lib",
  241. SHOW_STR
  242. "Memory statistics\n"
  243. "Library memory\n")
  244. {
  245. show_memory_vty (vty, memory_list_lib);
  246. return CMD_SUCCESS;
  247. }
  248. DEFUN (show_memory_zebra,
  249. show_memory_zebra_cmd,
  250. "show memory zebra",
  251. SHOW_STR
  252. "Memory statistics\n"
  253. "Zebra memory\n")
  254. {
  255. show_memory_vty (vty, memory_list_zebra);
  256. return CMD_SUCCESS;
  257. }
  258. DEFUN (show_memory_rip,
  259. show_memory_rip_cmd,
  260. "show memory rip",
  261. SHOW_STR
  262. "Memory statistics\n"
  263. "RIP memory\n")
  264. {
  265. show_memory_vty (vty, memory_list_rip);
  266. return CMD_SUCCESS;
  267. }
  268. DEFUN (show_memory_ripng,
  269. show_memory_ripng_cmd,
  270. "show memory ripng",
  271. SHOW_STR
  272. "Memory statistics\n"
  273. "RIPng memory\n")
  274. {
  275. show_memory_vty (vty, memory_list_ripng);
  276. return CMD_SUCCESS;
  277. }
  278. DEFUN (show_memory_bgp,
  279. show_memory_bgp_cmd,
  280. "show memory bgp",
  281. SHOW_STR
  282. "Memory statistics\n"
  283. "BGP memory\n")
  284. {
  285. show_memory_vty (vty, memory_list_bgp);
  286. return CMD_SUCCESS;
  287. }
  288. DEFUN (show_memory_ospf,
  289. show_memory_ospf_cmd,
  290. "show memory ospf",
  291. SHOW_STR
  292. "Memory statistics\n"
  293. "OSPF memory\n")
  294. {
  295. show_memory_vty (vty, memory_list_ospf);
  296. return CMD_SUCCESS;
  297. }
  298. DEFUN (show_memory_ospf6,
  299. show_memory_ospf6_cmd,
  300. "show memory ospf6",
  301. SHOW_STR
  302. "Memory statistics\n"
  303. "OSPF6 memory\n")
  304. {
  305. show_memory_vty (vty, memory_list_ospf6);
  306. return CMD_SUCCESS;
  307. }
  308. DEFUN (show_memory_isis,
  309. show_memory_isis_cmd,
  310. "show memory isis",
  311. SHOW_STR
  312. "Memory statistics\n"
  313. "ISIS memory\n")
  314. {
  315. show_memory_vty (vty, memory_list_isis);
  316. return CMD_SUCCESS;
  317. }
  318. void
  319. memory_init (void)
  320. {
  321. install_element (VIEW_NODE, &show_memory_cmd);
  322. install_element (VIEW_NODE, &show_memory_all_cmd);
  323. install_element (VIEW_NODE, &show_memory_lib_cmd);
  324. install_element (VIEW_NODE, &show_memory_rip_cmd);
  325. install_element (VIEW_NODE, &show_memory_ripng_cmd);
  326. install_element (VIEW_NODE, &show_memory_bgp_cmd);
  327. install_element (VIEW_NODE, &show_memory_ospf_cmd);
  328. install_element (VIEW_NODE, &show_memory_ospf6_cmd);
  329. install_element (VIEW_NODE, &show_memory_isis_cmd);
  330. install_element (ENABLE_NODE, &show_memory_cmd);
  331. install_element (ENABLE_NODE, &show_memory_all_cmd);
  332. install_element (ENABLE_NODE, &show_memory_lib_cmd);
  333. install_element (ENABLE_NODE, &show_memory_zebra_cmd);
  334. install_element (ENABLE_NODE, &show_memory_rip_cmd);
  335. install_element (ENABLE_NODE, &show_memory_ripng_cmd);
  336. install_element (ENABLE_NODE, &show_memory_bgp_cmd);
  337. install_element (ENABLE_NODE, &show_memory_ospf_cmd);
  338. install_element (ENABLE_NODE, &show_memory_ospf6_cmd);
  339. install_element (ENABLE_NODE, &show_memory_isis_cmd);
  340. }