memory.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  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. /* malloc.h is generally obsolete, however GNU Libc mallinfo wants it. */
  24. #if !defined(HAVE_STDLIB_H) || (defined(GNU_LINUX) && defined(HAVE_MALLINFO))
  25. #include <malloc.h>
  26. #endif /* !HAVE_STDLIB_H || HAVE_MALLINFO */
  27. #include "log.h"
  28. #include "memory.h"
  29. static void alloc_inc (int);
  30. static void alloc_dec (int);
  31. static void log_memstats(int log_priority);
  32. static struct message mstr [] =
  33. {
  34. { MTYPE_THREAD, "thread" },
  35. { MTYPE_THREAD_MASTER, "thread_master" },
  36. { MTYPE_VECTOR, "vector" },
  37. { MTYPE_VECTOR_INDEX, "vector_index" },
  38. { MTYPE_IF, "interface" },
  39. { 0, NULL },
  40. };
  41. /* Fatal memory allocation error occured. */
  42. static void __attribute__ ((noreturn))
  43. zerror (const char *fname, int type, size_t size)
  44. {
  45. zlog_err ("%s : can't allocate memory for `%s' size %d: %s\n",
  46. fname, lookup (mstr, type), (int) size, safe_strerror(errno));
  47. log_memstats(LOG_WARNING);
  48. /* N.B. It might be preferable to call zlog_backtrace_sigsafe here, since
  49. that function should definitely be safe in an OOM condition. But
  50. unfortunately zlog_backtrace_sigsafe does not support syslog logging at
  51. this time... */
  52. zlog_backtrace(LOG_WARNING);
  53. abort();
  54. }
  55. /* Memory allocation. */
  56. void *
  57. zmalloc (int type, size_t size)
  58. {
  59. void *memory;
  60. memory = malloc (size);
  61. if (memory == NULL)
  62. zerror ("malloc", type, size);
  63. alloc_inc (type);
  64. return memory;
  65. }
  66. /* Memory allocation with num * size with cleared. */
  67. void *
  68. zcalloc (int type, size_t size)
  69. {
  70. void *memory;
  71. memory = calloc (1, size);
  72. if (memory == NULL)
  73. zerror ("calloc", type, size);
  74. alloc_inc (type);
  75. return memory;
  76. }
  77. /* Memory reallocation. */
  78. void *
  79. zrealloc (int type, void *ptr, size_t size)
  80. {
  81. void *memory;
  82. memory = realloc (ptr, size);
  83. if (memory == NULL)
  84. zerror ("realloc", type, size);
  85. return memory;
  86. }
  87. /* Memory free. */
  88. void
  89. zfree (int type, void *ptr)
  90. {
  91. alloc_dec (type);
  92. free (ptr);
  93. }
  94. /* String duplication. */
  95. char *
  96. zstrdup (int type, const char *str)
  97. {
  98. void *dup;
  99. dup = strdup (str);
  100. if (dup == NULL)
  101. zerror ("strdup", type, strlen (str));
  102. alloc_inc (type);
  103. return dup;
  104. }
  105. #ifdef MEMORY_LOG
  106. static struct
  107. {
  108. const char *name;
  109. unsigned long alloc;
  110. unsigned long t_malloc;
  111. unsigned long c_malloc;
  112. unsigned long t_calloc;
  113. unsigned long c_calloc;
  114. unsigned long t_realloc;
  115. unsigned long t_free;
  116. unsigned long c_strdup;
  117. } mstat [MTYPE_MAX];
  118. static void
  119. mtype_log (char *func, void *memory, const char *file, int line, int type)
  120. {
  121. zlog_debug ("%s: %s %p %s %d", func, lookup (mstr, type), memory, file, line);
  122. }
  123. void *
  124. mtype_zmalloc (const char *file, int line, int type, size_t size)
  125. {
  126. void *memory;
  127. mstat[type].c_malloc++;
  128. mstat[type].t_malloc++;
  129. memory = zmalloc (type, size);
  130. mtype_log ("zmalloc", memory, file, line, type);
  131. return memory;
  132. }
  133. void *
  134. mtype_zcalloc (const char *file, int line, int type, size_t size)
  135. {
  136. void *memory;
  137. mstat[type].c_calloc++;
  138. mstat[type].t_calloc++;
  139. memory = zcalloc (type, size);
  140. mtype_log ("xcalloc", memory, file, line, type);
  141. return memory;
  142. }
  143. void *
  144. mtype_zrealloc (const char *file, int line, int type, void *ptr, size_t size)
  145. {
  146. void *memory;
  147. /* Realloc need before allocated pointer. */
  148. mstat[type].t_realloc++;
  149. memory = zrealloc (type, ptr, size);
  150. mtype_log ("xrealloc", memory, file, line, type);
  151. return memory;
  152. }
  153. /* Important function. */
  154. void
  155. mtype_zfree (const char *file, int line, int type, void *ptr)
  156. {
  157. mstat[type].t_free++;
  158. mtype_log ("xfree", ptr, file, line, type);
  159. zfree (type, ptr);
  160. }
  161. char *
  162. mtype_zstrdup (const char *file, int line, int type, const char *str)
  163. {
  164. char *memory;
  165. mstat[type].c_strdup++;
  166. memory = zstrdup (type, str);
  167. mtype_log ("xstrdup", memory, file, line, type);
  168. return memory;
  169. }
  170. #else
  171. static struct
  172. {
  173. char *name;
  174. unsigned long alloc;
  175. } mstat [MTYPE_MAX];
  176. #endif /* MTPYE_LOG */
  177. /* Increment allocation counter. */
  178. static void
  179. alloc_inc (int type)
  180. {
  181. mstat[type].alloc++;
  182. }
  183. /* Decrement allocation counter. */
  184. static void
  185. alloc_dec (int type)
  186. {
  187. mstat[type].alloc--;
  188. }
  189. /* Looking up memory status from vty interface. */
  190. #include "vector.h"
  191. #include "vty.h"
  192. #include "command.h"
  193. static void
  194. log_memstats(int pri)
  195. {
  196. struct mlist *ml;
  197. for (ml = mlists; ml->list; ml++)
  198. {
  199. struct memory_list *m;
  200. zlog (NULL, pri, "Memory utilization in module %s:", ml->name);
  201. for (m = ml->list; m->index >= 0; m++)
  202. if (m->index && mstat[m->index].alloc)
  203. zlog (NULL, pri, " %-30s: %10ld", m->format, mstat[m->index].alloc);
  204. }
  205. }
  206. static void
  207. show_separator(struct vty *vty)
  208. {
  209. vty_out (vty, "-----------------------------\r\n");
  210. }
  211. static int
  212. show_memory_vty (struct vty *vty, struct memory_list *list)
  213. {
  214. struct memory_list *m;
  215. int needsep = 0;
  216. for (m = list; m->index >= 0; m++)
  217. if (m->index == 0)
  218. {
  219. if (needsep)
  220. {
  221. show_separator (vty);
  222. needsep = 0;
  223. }
  224. }
  225. else if (mstat[m->index].alloc)
  226. {
  227. vty_out (vty, "%-30s: %10ld\r\n", m->format, mstat[m->index].alloc);
  228. needsep = 1;
  229. }
  230. return needsep;
  231. }
  232. #ifdef HAVE_MALLINFO
  233. static int
  234. show_memory_mallinfo (struct vty *vty)
  235. {
  236. struct mallinfo minfo = mallinfo();
  237. char buf[MTYPE_MEMSTR_LEN];
  238. vty_out (vty, "System allocator statistics:%s", VTY_NEWLINE);
  239. vty_out (vty, " Total heap allocated: %s%s",
  240. mtype_memstr (buf, MTYPE_MEMSTR_LEN, minfo.arena),
  241. VTY_NEWLINE);
  242. vty_out (vty, " Holding block headers: %s%s",
  243. mtype_memstr (buf, MTYPE_MEMSTR_LEN, minfo.hblkhd),
  244. VTY_NEWLINE);
  245. vty_out (vty, " Used small blocks: %s%s",
  246. mtype_memstr (buf, MTYPE_MEMSTR_LEN, minfo.usmblks),
  247. VTY_NEWLINE);
  248. vty_out (vty, " Used ordinary blocks: %s%s",
  249. mtype_memstr (buf, MTYPE_MEMSTR_LEN, minfo.uordblks),
  250. VTY_NEWLINE);
  251. vty_out (vty, " Free small blocks: %s%s",
  252. mtype_memstr (buf, MTYPE_MEMSTR_LEN, minfo.fsmblks),
  253. VTY_NEWLINE);
  254. vty_out (vty, " Free ordinary blocks: %s%s",
  255. mtype_memstr (buf, MTYPE_MEMSTR_LEN, minfo.fordblks),
  256. VTY_NEWLINE);
  257. vty_out (vty, " Ordinary blocks: %ld%s",
  258. (unsigned long)minfo.ordblks,
  259. VTY_NEWLINE);
  260. vty_out (vty, " Small blocks: %ld%s",
  261. (unsigned long)minfo.smblks,
  262. VTY_NEWLINE);
  263. vty_out (vty, " Holding blocks: %ld%s",
  264. (unsigned long)minfo.hblks,
  265. VTY_NEWLINE);
  266. vty_out (vty, "(see system documentation for 'mallinfo' for meaning)%s",
  267. VTY_NEWLINE);
  268. return 1;
  269. }
  270. #endif /* HAVE_MALLINFO */
  271. DEFUN (show_memory_all,
  272. show_memory_all_cmd,
  273. "show memory all",
  274. "Show running system information\n"
  275. "Memory statistics\n"
  276. "All memory statistics\n")
  277. {
  278. struct mlist *ml;
  279. int needsep = 0;
  280. #ifdef HAVE_MALLINFO
  281. needsep = show_memory_mallinfo (vty);
  282. #endif /* HAVE_MALLINFO */
  283. for (ml = mlists; ml->list; ml++)
  284. {
  285. if (needsep)
  286. show_separator (vty);
  287. needsep = show_memory_vty (vty, ml->list);
  288. }
  289. return CMD_SUCCESS;
  290. }
  291. ALIAS (show_memory_all,
  292. show_memory_cmd,
  293. "show memory",
  294. "Show running system information\n"
  295. "Memory statistics\n")
  296. DEFUN (show_memory_lib,
  297. show_memory_lib_cmd,
  298. "show memory lib",
  299. SHOW_STR
  300. "Memory statistics\n"
  301. "Library memory\n")
  302. {
  303. show_memory_vty (vty, memory_list_lib);
  304. return CMD_SUCCESS;
  305. }
  306. DEFUN (show_memory_zebra,
  307. show_memory_zebra_cmd,
  308. "show memory zebra",
  309. SHOW_STR
  310. "Memory statistics\n"
  311. "Zebra memory\n")
  312. {
  313. show_memory_vty (vty, memory_list_zebra);
  314. return CMD_SUCCESS;
  315. }
  316. DEFUN (show_memory_rip,
  317. show_memory_rip_cmd,
  318. "show memory rip",
  319. SHOW_STR
  320. "Memory statistics\n"
  321. "RIP memory\n")
  322. {
  323. show_memory_vty (vty, memory_list_rip);
  324. return CMD_SUCCESS;
  325. }
  326. DEFUN (show_memory_ripng,
  327. show_memory_ripng_cmd,
  328. "show memory ripng",
  329. SHOW_STR
  330. "Memory statistics\n"
  331. "RIPng memory\n")
  332. {
  333. show_memory_vty (vty, memory_list_ripng);
  334. return CMD_SUCCESS;
  335. }
  336. DEFUN (show_memory_bgp,
  337. show_memory_bgp_cmd,
  338. "show memory bgp",
  339. SHOW_STR
  340. "Memory statistics\n"
  341. "BGP memory\n")
  342. {
  343. show_memory_vty (vty, memory_list_bgp);
  344. return CMD_SUCCESS;
  345. }
  346. DEFUN (show_memory_ospf,
  347. show_memory_ospf_cmd,
  348. "show memory ospf",
  349. SHOW_STR
  350. "Memory statistics\n"
  351. "OSPF memory\n")
  352. {
  353. show_memory_vty (vty, memory_list_ospf);
  354. return CMD_SUCCESS;
  355. }
  356. DEFUN (show_memory_ospf6,
  357. show_memory_ospf6_cmd,
  358. "show memory ospf6",
  359. SHOW_STR
  360. "Memory statistics\n"
  361. "OSPF6 memory\n")
  362. {
  363. show_memory_vty (vty, memory_list_ospf6);
  364. return CMD_SUCCESS;
  365. }
  366. DEFUN (show_memory_isis,
  367. show_memory_isis_cmd,
  368. "show memory isis",
  369. SHOW_STR
  370. "Memory statistics\n"
  371. "ISIS memory\n")
  372. {
  373. show_memory_vty (vty, memory_list_isis);
  374. return CMD_SUCCESS;
  375. }
  376. void
  377. memory_init (void)
  378. {
  379. install_element (VIEW_NODE, &show_memory_cmd);
  380. install_element (VIEW_NODE, &show_memory_all_cmd);
  381. install_element (VIEW_NODE, &show_memory_lib_cmd);
  382. install_element (VIEW_NODE, &show_memory_rip_cmd);
  383. install_element (VIEW_NODE, &show_memory_ripng_cmd);
  384. install_element (VIEW_NODE, &show_memory_bgp_cmd);
  385. install_element (VIEW_NODE, &show_memory_ospf_cmd);
  386. install_element (VIEW_NODE, &show_memory_ospf6_cmd);
  387. install_element (VIEW_NODE, &show_memory_isis_cmd);
  388. install_element (ENABLE_NODE, &show_memory_cmd);
  389. install_element (ENABLE_NODE, &show_memory_all_cmd);
  390. install_element (ENABLE_NODE, &show_memory_lib_cmd);
  391. install_element (ENABLE_NODE, &show_memory_zebra_cmd);
  392. install_element (ENABLE_NODE, &show_memory_rip_cmd);
  393. install_element (ENABLE_NODE, &show_memory_ripng_cmd);
  394. install_element (ENABLE_NODE, &show_memory_bgp_cmd);
  395. install_element (ENABLE_NODE, &show_memory_ospf_cmd);
  396. install_element (ENABLE_NODE, &show_memory_ospf6_cmd);
  397. install_element (ENABLE_NODE, &show_memory_isis_cmd);
  398. }
  399. /* Stats querying from users */
  400. /* Return a pointer to a human friendly string describing
  401. * the byte count passed in. E.g:
  402. * "0 bytes", "2048 bytes", "110kB", "500MiB", "11GiB", etc.
  403. * Up to 4 significant figures will be given.
  404. * The pointer returned may be NULL (indicating an error)
  405. * or point to the given buffer, or point to static storage.
  406. */
  407. const char *
  408. mtype_memstr (char *buf, size_t len, unsigned long bytes)
  409. {
  410. unsigned int t, g, m, k;
  411. /* easy cases */
  412. if (!bytes)
  413. return "0 bytes";
  414. if (bytes == 1)
  415. return "1 byte";
  416. if (sizeof (unsigned long) >= 8)
  417. /* Hacked to make it not warn on ILP32 machines
  418. * Shift will always be 40 at runtime. See below too */
  419. t = bytes >> (sizeof (unsigned long) >= 8 ? 40 : 0);
  420. else
  421. t = 0;
  422. g = bytes >> 30;
  423. m = bytes >> 20;
  424. k = bytes >> 10;
  425. if (t > 10)
  426. {
  427. /* The shift will always be 39 at runtime.
  428. * Just hacked to make it not warn on 'smaller' machines.
  429. * Static compiler analysis should mean no extra code
  430. */
  431. if (bytes & (1UL << (sizeof (unsigned long) >= 8 ? 39 : 0)))
  432. t++;
  433. snprintf (buf, len, "%4d TiB", t);
  434. }
  435. else if (g > 10)
  436. {
  437. if (bytes & (1 << 29))
  438. g++;
  439. snprintf (buf, len, "%d GiB", g);
  440. }
  441. else if (m > 10)
  442. {
  443. if (bytes & (1 << 19))
  444. m++;
  445. snprintf (buf, len, "%d MiB", m);
  446. }
  447. else if (k > 10)
  448. {
  449. if (bytes & (1 << 9))
  450. k++;
  451. snprintf (buf, len, "%d KiB", k);
  452. }
  453. else
  454. snprintf (buf, len, "%ld bytes", bytes);
  455. return buf;
  456. }
  457. unsigned long
  458. mtype_stats_alloc (int type)
  459. {
  460. return mstat[type].alloc;
  461. }