memory.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643
  1. /*
  2. * Memory management routine
  3. * Copyright (C) 1998 Kunihiro Ishiguro
  4. * Portions Copyright (c) 2008 Everton da Silva Marques <everton.marques@gmail.com>
  5. *
  6. * This file is part of GNU Zebra.
  7. *
  8. * GNU Zebra is free software; you can redistribute it and/or modify it
  9. * under the terms of the GNU General Public License as published by the
  10. * Free Software Foundation; either version 2, or (at your option) any
  11. * later version.
  12. *
  13. * GNU Zebra is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with GNU Zebra; see the file COPYING. If not, write to the Free
  20. * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
  21. * 02111-1307, USA.
  22. */
  23. #include <zebra.h>
  24. /* malloc.h is generally obsolete, however GNU Libc mallinfo wants it. */
  25. #if !defined(HAVE_STDLIB_H) || (defined(GNU_LINUX) && defined(HAVE_MALLINFO))
  26. #include <malloc.h>
  27. #endif /* !HAVE_STDLIB_H || HAVE_MALLINFO */
  28. #include "log.h"
  29. #include "memory.h"
  30. static void alloc_inc (int);
  31. static void alloc_dec (int);
  32. static void log_memstats(int log_priority);
  33. static const struct message mstr [] =
  34. {
  35. { MTYPE_THREAD, "thread" },
  36. { MTYPE_THREAD_MASTER, "thread_master" },
  37. { MTYPE_VECTOR, "vector" },
  38. { MTYPE_VECTOR_INDEX, "vector_index" },
  39. { MTYPE_IF, "interface" },
  40. { 0, NULL },
  41. };
  42. /* Fatal memory allocation error occured. */
  43. static void __attribute__ ((noreturn))
  44. zerror (const char *fname, int type, size_t size)
  45. {
  46. zlog_err ("%s : can't allocate memory for `%s' size %d: %s\n",
  47. fname, lookup (mstr, type), (int) size, safe_strerror(errno));
  48. log_memstats(LOG_WARNING);
  49. /* N.B. It might be preferable to call zlog_backtrace_sigsafe here, since
  50. that function should definitely be safe in an OOM condition. But
  51. unfortunately zlog_backtrace_sigsafe does not support syslog logging at
  52. this time... */
  53. zlog_backtrace(LOG_WARNING);
  54. abort();
  55. }
  56. /*
  57. * Allocate memory of a given size, to be tracked by a given type.
  58. * Effects: Returns a pointer to usable memory. If memory cannot
  59. * be allocated, aborts execution.
  60. */
  61. void *
  62. zmalloc (int type, size_t size)
  63. {
  64. void *memory;
  65. memory = malloc (size);
  66. if (memory == NULL)
  67. zerror ("malloc", type, size);
  68. alloc_inc (type);
  69. return memory;
  70. }
  71. /*
  72. * Allocate memory as in zmalloc, and also clear the memory.
  73. */
  74. void *
  75. zcalloc (int type, size_t size)
  76. {
  77. void *memory;
  78. memory = calloc (1, size);
  79. if (memory == NULL)
  80. zerror ("calloc", type, size);
  81. alloc_inc (type);
  82. return memory;
  83. }
  84. /*
  85. * Given a pointer returned by zmalloc or zcalloc, free it and
  86. * return a pointer to a new size, basically acting like realloc().
  87. * Requires: ptr was returned by zmalloc, zcalloc, or zrealloc with the
  88. * same type.
  89. * Effects: Returns a pointer to the new memory, or aborts.
  90. */
  91. void *
  92. zrealloc (int type, void *ptr, size_t size)
  93. {
  94. void *memory;
  95. memory = realloc (ptr, size);
  96. if (memory == NULL)
  97. zerror ("realloc", type, size);
  98. if (ptr == NULL)
  99. alloc_inc (type);
  100. return memory;
  101. }
  102. /*
  103. * Free memory allocated by z*alloc or zstrdup.
  104. * Requires: ptr was returned by zmalloc, zcalloc, or zrealloc with the
  105. * same type.
  106. * Effects: The memory is freed and may no longer be referenced.
  107. */
  108. void
  109. zfree (int type, void *ptr)
  110. {
  111. if (ptr != NULL)
  112. {
  113. alloc_dec (type);
  114. free (ptr);
  115. }
  116. }
  117. /*
  118. * Duplicate a string, counting memory usage by type.
  119. * Effects: The string is duplicated, and the return value must
  120. * eventually be passed to zfree with the same type. The function will
  121. * succeed or abort.
  122. */
  123. char *
  124. zstrdup (int type, const char *str)
  125. {
  126. void *dup;
  127. dup = strdup (str);
  128. if (dup == NULL)
  129. zerror ("strdup", type, strlen (str));
  130. alloc_inc (type);
  131. return dup;
  132. }
  133. #ifdef MEMORY_LOG
  134. static struct
  135. {
  136. const char *name;
  137. long alloc;
  138. unsigned long t_malloc;
  139. unsigned long c_malloc;
  140. unsigned long t_calloc;
  141. unsigned long c_calloc;
  142. unsigned long t_realloc;
  143. unsigned long t_free;
  144. unsigned long c_strdup;
  145. } mstat [MTYPE_MAX];
  146. static void
  147. mtype_log (char *func, void *memory, const char *file, int line, int type)
  148. {
  149. zlog_debug ("%s: %s %p %s %d", func, lookup (mstr, type), memory, file, line);
  150. }
  151. void *
  152. mtype_zmalloc (const char *file, int line, int type, size_t size)
  153. {
  154. void *memory;
  155. mstat[type].c_malloc++;
  156. mstat[type].t_malloc++;
  157. memory = zmalloc (type, size);
  158. mtype_log ("zmalloc", memory, file, line, type);
  159. return memory;
  160. }
  161. void *
  162. mtype_zcalloc (const char *file, int line, int type, size_t size)
  163. {
  164. void *memory;
  165. mstat[type].c_calloc++;
  166. mstat[type].t_calloc++;
  167. memory = zcalloc (type, size);
  168. mtype_log ("xcalloc", memory, file, line, type);
  169. return memory;
  170. }
  171. void *
  172. mtype_zrealloc (const char *file, int line, int type, void *ptr, size_t size)
  173. {
  174. void *memory;
  175. /* Realloc need before allocated pointer. */
  176. mstat[type].t_realloc++;
  177. memory = zrealloc (type, ptr, size);
  178. mtype_log ("xrealloc", memory, file, line, type);
  179. return memory;
  180. }
  181. /* Important function. */
  182. void
  183. mtype_zfree (const char *file, int line, int type, void *ptr)
  184. {
  185. mstat[type].t_free++;
  186. mtype_log ("xfree", ptr, file, line, type);
  187. zfree (type, ptr);
  188. }
  189. char *
  190. mtype_zstrdup (const char *file, int line, int type, const char *str)
  191. {
  192. char *memory;
  193. mstat[type].c_strdup++;
  194. memory = zstrdup (type, str);
  195. mtype_log ("xstrdup", memory, file, line, type);
  196. return memory;
  197. }
  198. #else
  199. static struct
  200. {
  201. char *name;
  202. long alloc;
  203. } mstat [MTYPE_MAX];
  204. #endif /* MEMORY_LOG */
  205. /* Increment allocation counter. */
  206. static void
  207. alloc_inc (int type)
  208. {
  209. mstat[type].alloc++;
  210. }
  211. /* Decrement allocation counter. */
  212. static void
  213. alloc_dec (int type)
  214. {
  215. mstat[type].alloc--;
  216. }
  217. /* Looking up memory status from vty interface. */
  218. #include "vector.h"
  219. #include "vty.h"
  220. #include "command.h"
  221. static void
  222. log_memstats(int pri)
  223. {
  224. struct mlist *ml;
  225. for (ml = mlists; ml->list; ml++)
  226. {
  227. struct memory_list *m;
  228. zlog (NULL, pri, "Memory utilization in module %s:", ml->name);
  229. for (m = ml->list; m->index >= 0; m++)
  230. if (m->index && mstat[m->index].alloc)
  231. zlog (NULL, pri, " %-30s: %10ld", m->format, mstat[m->index].alloc);
  232. }
  233. }
  234. void
  235. log_memstats_stderr (const char *prefix)
  236. {
  237. struct mlist *ml;
  238. struct memory_list *m;
  239. int i;
  240. int j = 0;
  241. for (ml = mlists; ml->list; ml++)
  242. {
  243. i = 0;
  244. for (m = ml->list; m->index >= 0; m++)
  245. if (m->index && mstat[m->index].alloc)
  246. {
  247. if (!i)
  248. fprintf (stderr,
  249. "%s: memstats: Current memory utilization in module %s:\n",
  250. prefix,
  251. ml->name);
  252. fprintf (stderr,
  253. "%s: memstats: %-30s: %10ld%s\n",
  254. prefix,
  255. m->format,
  256. mstat[m->index].alloc,
  257. mstat[m->index].alloc < 0 ? " (REPORT THIS BUG!)" : "");
  258. i = j = 1;
  259. }
  260. }
  261. if (j)
  262. fprintf (stderr,
  263. "%s: memstats: NOTE: If configuration exists, utilization may be "
  264. "expected.\n",
  265. prefix);
  266. else
  267. fprintf (stderr,
  268. "%s: memstats: No remaining tracked memory utilization.\n",
  269. prefix);
  270. }
  271. static void
  272. show_separator(struct vty *vty)
  273. {
  274. vty_out (vty, "-----------------------------\r\n");
  275. }
  276. static int
  277. show_memory_vty (struct vty *vty, struct memory_list *list)
  278. {
  279. struct memory_list *m;
  280. int needsep = 0;
  281. for (m = list; m->index >= 0; m++)
  282. if (m->index == 0)
  283. {
  284. if (needsep)
  285. {
  286. show_separator (vty);
  287. needsep = 0;
  288. }
  289. }
  290. else if (mstat[m->index].alloc)
  291. {
  292. vty_out (vty, "%-30s: %10ld\r\n", m->format, mstat[m->index].alloc);
  293. needsep = 1;
  294. }
  295. return needsep;
  296. }
  297. #ifdef HAVE_MALLINFO
  298. static int
  299. show_memory_mallinfo (struct vty *vty)
  300. {
  301. struct mallinfo minfo = mallinfo();
  302. char buf[MTYPE_MEMSTR_LEN];
  303. vty_out (vty, "System allocator statistics:%s", VTY_NEWLINE);
  304. vty_out (vty, " Total heap allocated: %s%s",
  305. mtype_memstr (buf, MTYPE_MEMSTR_LEN, minfo.arena),
  306. VTY_NEWLINE);
  307. vty_out (vty, " Holding block headers: %s%s",
  308. mtype_memstr (buf, MTYPE_MEMSTR_LEN, minfo.hblkhd),
  309. VTY_NEWLINE);
  310. vty_out (vty, " Used small blocks: %s%s",
  311. mtype_memstr (buf, MTYPE_MEMSTR_LEN, minfo.usmblks),
  312. VTY_NEWLINE);
  313. vty_out (vty, " Used ordinary blocks: %s%s",
  314. mtype_memstr (buf, MTYPE_MEMSTR_LEN, minfo.uordblks),
  315. VTY_NEWLINE);
  316. vty_out (vty, " Free small blocks: %s%s",
  317. mtype_memstr (buf, MTYPE_MEMSTR_LEN, minfo.fsmblks),
  318. VTY_NEWLINE);
  319. vty_out (vty, " Free ordinary blocks: %s%s",
  320. mtype_memstr (buf, MTYPE_MEMSTR_LEN, minfo.fordblks),
  321. VTY_NEWLINE);
  322. vty_out (vty, " Ordinary blocks: %ld%s",
  323. (unsigned long)minfo.ordblks,
  324. VTY_NEWLINE);
  325. vty_out (vty, " Small blocks: %ld%s",
  326. (unsigned long)minfo.smblks,
  327. VTY_NEWLINE);
  328. vty_out (vty, " Holding blocks: %ld%s",
  329. (unsigned long)minfo.hblks,
  330. VTY_NEWLINE);
  331. vty_out (vty, "(see system documentation for 'mallinfo' for meaning)%s",
  332. VTY_NEWLINE);
  333. return 1;
  334. }
  335. #endif /* HAVE_MALLINFO */
  336. DEFUN (show_memory_all,
  337. show_memory_all_cmd,
  338. "show memory all",
  339. "Show running system information\n"
  340. "Memory statistics\n"
  341. "All memory statistics\n")
  342. {
  343. struct mlist *ml;
  344. int needsep = 0;
  345. #ifdef HAVE_MALLINFO
  346. needsep = show_memory_mallinfo (vty);
  347. #endif /* HAVE_MALLINFO */
  348. for (ml = mlists; ml->list; ml++)
  349. {
  350. if (needsep)
  351. show_separator (vty);
  352. needsep = show_memory_vty (vty, ml->list);
  353. }
  354. return CMD_SUCCESS;
  355. }
  356. ALIAS (show_memory_all,
  357. show_memory_cmd,
  358. "show memory",
  359. "Show running system information\n"
  360. "Memory statistics\n")
  361. DEFUN (show_memory_lib,
  362. show_memory_lib_cmd,
  363. "show memory lib",
  364. SHOW_STR
  365. "Memory statistics\n"
  366. "Library memory\n")
  367. {
  368. show_memory_vty (vty, memory_list_lib);
  369. return CMD_SUCCESS;
  370. }
  371. DEFUN (show_memory_zebra,
  372. show_memory_zebra_cmd,
  373. "show memory zebra",
  374. SHOW_STR
  375. "Memory statistics\n"
  376. "Zebra memory\n")
  377. {
  378. show_memory_vty (vty, memory_list_zebra);
  379. return CMD_SUCCESS;
  380. }
  381. DEFUN (show_memory_rip,
  382. show_memory_rip_cmd,
  383. "show memory rip",
  384. SHOW_STR
  385. "Memory statistics\n"
  386. "RIP memory\n")
  387. {
  388. show_memory_vty (vty, memory_list_rip);
  389. return CMD_SUCCESS;
  390. }
  391. DEFUN (show_memory_ripng,
  392. show_memory_ripng_cmd,
  393. "show memory ripng",
  394. SHOW_STR
  395. "Memory statistics\n"
  396. "RIPng memory\n")
  397. {
  398. show_memory_vty (vty, memory_list_ripng);
  399. return CMD_SUCCESS;
  400. }
  401. DEFUN (show_memory_babel,
  402. show_memory_babel_cmd,
  403. "show memory babel",
  404. SHOW_STR
  405. "Memory statistics\n"
  406. "Babel memory\n")
  407. {
  408. show_memory_vty (vty, memory_list_babel);
  409. return CMD_SUCCESS;
  410. }
  411. DEFUN (show_memory_bgp,
  412. show_memory_bgp_cmd,
  413. "show memory bgp",
  414. SHOW_STR
  415. "Memory statistics\n"
  416. "BGP memory\n")
  417. {
  418. show_memory_vty (vty, memory_list_bgp);
  419. return CMD_SUCCESS;
  420. }
  421. DEFUN (show_memory_ospf,
  422. show_memory_ospf_cmd,
  423. "show memory ospf",
  424. SHOW_STR
  425. "Memory statistics\n"
  426. "OSPF memory\n")
  427. {
  428. show_memory_vty (vty, memory_list_ospf);
  429. return CMD_SUCCESS;
  430. }
  431. DEFUN (show_memory_ospf6,
  432. show_memory_ospf6_cmd,
  433. "show memory ospf6",
  434. SHOW_STR
  435. "Memory statistics\n"
  436. "OSPF6 memory\n")
  437. {
  438. show_memory_vty (vty, memory_list_ospf6);
  439. return CMD_SUCCESS;
  440. }
  441. DEFUN (show_memory_isis,
  442. show_memory_isis_cmd,
  443. "show memory isis",
  444. SHOW_STR
  445. "Memory statistics\n"
  446. "ISIS memory\n")
  447. {
  448. show_memory_vty (vty, memory_list_isis);
  449. return CMD_SUCCESS;
  450. }
  451. DEFUN (show_memory_pim,
  452. show_memory_pim_cmd,
  453. "show memory pim",
  454. SHOW_STR
  455. "Memory statistics\n"
  456. "PIM memory\n")
  457. {
  458. show_memory_vty (vty, memory_list_pim);
  459. return CMD_SUCCESS;
  460. }
  461. void
  462. memory_init (void)
  463. {
  464. install_element (RESTRICTED_NODE, &show_memory_cmd);
  465. install_element (RESTRICTED_NODE, &show_memory_all_cmd);
  466. install_element (RESTRICTED_NODE, &show_memory_lib_cmd);
  467. install_element (RESTRICTED_NODE, &show_memory_rip_cmd);
  468. install_element (RESTRICTED_NODE, &show_memory_ripng_cmd);
  469. install_element (RESTRICTED_NODE, &show_memory_babel_cmd);
  470. install_element (RESTRICTED_NODE, &show_memory_bgp_cmd);
  471. install_element (RESTRICTED_NODE, &show_memory_ospf_cmd);
  472. install_element (RESTRICTED_NODE, &show_memory_ospf6_cmd);
  473. install_element (RESTRICTED_NODE, &show_memory_isis_cmd);
  474. install_element (VIEW_NODE, &show_memory_cmd);
  475. install_element (VIEW_NODE, &show_memory_all_cmd);
  476. install_element (VIEW_NODE, &show_memory_lib_cmd);
  477. install_element (VIEW_NODE, &show_memory_rip_cmd);
  478. install_element (VIEW_NODE, &show_memory_ripng_cmd);
  479. install_element (VIEW_NODE, &show_memory_babel_cmd);
  480. install_element (VIEW_NODE, &show_memory_bgp_cmd);
  481. install_element (VIEW_NODE, &show_memory_ospf_cmd);
  482. install_element (VIEW_NODE, &show_memory_ospf6_cmd);
  483. install_element (VIEW_NODE, &show_memory_isis_cmd);
  484. install_element (VIEW_NODE, &show_memory_pim_cmd);
  485. install_element (ENABLE_NODE, &show_memory_cmd);
  486. install_element (ENABLE_NODE, &show_memory_all_cmd);
  487. install_element (ENABLE_NODE, &show_memory_lib_cmd);
  488. install_element (ENABLE_NODE, &show_memory_zebra_cmd);
  489. install_element (ENABLE_NODE, &show_memory_rip_cmd);
  490. install_element (ENABLE_NODE, &show_memory_ripng_cmd);
  491. install_element (ENABLE_NODE, &show_memory_babel_cmd);
  492. install_element (ENABLE_NODE, &show_memory_bgp_cmd);
  493. install_element (ENABLE_NODE, &show_memory_ospf_cmd);
  494. install_element (ENABLE_NODE, &show_memory_ospf6_cmd);
  495. install_element (ENABLE_NODE, &show_memory_pim_cmd);
  496. }
  497. /* Stats querying from users */
  498. /* Return a pointer to a human friendly string describing
  499. * the byte count passed in. E.g:
  500. * "0 bytes", "2048 bytes", "110kB", "500MiB", "11GiB", etc.
  501. * Up to 4 significant figures will be given.
  502. * The pointer returned may be NULL (indicating an error)
  503. * or point to the given buffer, or point to static storage.
  504. */
  505. const char *
  506. mtype_memstr (char *buf, size_t len, unsigned long bytes)
  507. {
  508. unsigned int t, g, m, k;
  509. /* easy cases */
  510. if (!bytes)
  511. return "0 bytes";
  512. if (bytes == 1)
  513. return "1 byte";
  514. if (sizeof (unsigned long) >= 8)
  515. /* Hacked to make it not warn on ILP32 machines
  516. * Shift will always be 40 at runtime. See below too */
  517. t = bytes >> (sizeof (unsigned long) >= 8 ? 40 : 0);
  518. else
  519. t = 0;
  520. g = bytes >> 30;
  521. m = bytes >> 20;
  522. k = bytes >> 10;
  523. if (t > 10)
  524. {
  525. /* The shift will always be 39 at runtime.
  526. * Just hacked to make it not warn on 'smaller' machines.
  527. * Static compiler analysis should mean no extra code
  528. */
  529. if (bytes & (1UL << (sizeof (unsigned long) >= 8 ? 39 : 0)))
  530. t++;
  531. snprintf (buf, len, "%4d TiB", t);
  532. }
  533. else if (g > 10)
  534. {
  535. if (bytes & (1 << 29))
  536. g++;
  537. snprintf (buf, len, "%d GiB", g);
  538. }
  539. else if (m > 10)
  540. {
  541. if (bytes & (1 << 19))
  542. m++;
  543. snprintf (buf, len, "%d MiB", m);
  544. }
  545. else if (k > 10)
  546. {
  547. if (bytes & (1 << 9))
  548. k++;
  549. snprintf (buf, len, "%d KiB", k);
  550. }
  551. else
  552. snprintf (buf, len, "%ld bytes", bytes);
  553. return buf;
  554. }
  555. unsigned long
  556. mtype_stats_alloc (int type)
  557. {
  558. return mstat[type].alloc;
  559. }