memory.c 15 KB

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