memory.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  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 occurred. */
  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. * Add an extra 'z' prefix to function name to avoid collision when linking
  73. * statically with zlib that exports the 'zcalloc' symbol.
  74. */
  75. void *
  76. zzcalloc (int type, size_t size)
  77. {
  78. void *memory;
  79. memory = calloc (1, size);
  80. if (memory == NULL)
  81. zerror ("calloc", type, size);
  82. alloc_inc (type);
  83. return memory;
  84. }
  85. /*
  86. * Given a pointer returned by zmalloc or zzcalloc, free it and
  87. * return a pointer to a new size, basically acting like realloc().
  88. * Requires: ptr was returned by zmalloc, zzcalloc, or zrealloc with the
  89. * same type.
  90. * Effects: Returns a pointer to the new memory, or aborts.
  91. */
  92. void *
  93. zrealloc (int type, void *ptr, size_t size)
  94. {
  95. void *memory;
  96. if (ptr == NULL) /* is really alloc */
  97. return zzcalloc(type, size);
  98. memory = realloc (ptr, size);
  99. if (memory == NULL)
  100. zerror ("realloc", type, size);
  101. if (ptr == NULL)
  102. alloc_inc (type);
  103. return memory;
  104. }
  105. /*
  106. * Free memory allocated by z*alloc or zstrdup.
  107. * Requires: ptr was returned by zmalloc, zzcalloc, or zrealloc with the
  108. * same type.
  109. * Effects: The memory is freed and may no longer be referenced.
  110. */
  111. void
  112. zfree (int type, void *ptr)
  113. {
  114. if (ptr != NULL)
  115. {
  116. alloc_dec (type);
  117. free (ptr);
  118. }
  119. }
  120. /*
  121. * Duplicate a string, counting memory usage by type.
  122. * Effects: The string is duplicated, and the return value must
  123. * eventually be passed to zfree with the same type. The function will
  124. * succeed or abort.
  125. */
  126. char *
  127. zstrdup (int type, const char *str)
  128. {
  129. void *dup;
  130. dup = strdup (str);
  131. if (dup == NULL)
  132. zerror ("strdup", type, strlen (str));
  133. alloc_inc (type);
  134. return dup;
  135. }
  136. #ifdef MEMORY_LOG
  137. static struct
  138. {
  139. const char *name;
  140. long alloc;
  141. unsigned long t_malloc;
  142. unsigned long c_malloc;
  143. unsigned long t_calloc;
  144. unsigned long c_calloc;
  145. unsigned long t_realloc;
  146. unsigned long t_free;
  147. unsigned long c_strdup;
  148. } mstat [MTYPE_MAX];
  149. static void
  150. mtype_log (char *func, void *memory, const char *file, int line, int type)
  151. {
  152. zlog_debug ("%s: %s %p %s %d", func, lookup (mstr, type), memory, file, line);
  153. }
  154. void *
  155. mtype_zmalloc (const char *file, int line, int type, size_t size)
  156. {
  157. void *memory;
  158. mstat[type].c_malloc++;
  159. mstat[type].t_malloc++;
  160. memory = zmalloc (type, size);
  161. mtype_log ("zmalloc", memory, file, line, type);
  162. return memory;
  163. }
  164. void *
  165. mtype_zcalloc (const char *file, int line, int type, size_t size)
  166. {
  167. void *memory;
  168. mstat[type].c_calloc++;
  169. mstat[type].t_calloc++;
  170. memory = zzcalloc (type, size);
  171. mtype_log ("xcalloc", memory, file, line, type);
  172. return memory;
  173. }
  174. void *
  175. mtype_zrealloc (const char *file, int line, int type, void *ptr, size_t size)
  176. {
  177. void *memory;
  178. /* Realloc need before allocated pointer. */
  179. mstat[type].t_realloc++;
  180. memory = zrealloc (type, ptr, size);
  181. mtype_log ("xrealloc", memory, file, line, type);
  182. return memory;
  183. }
  184. /* Important function. */
  185. void
  186. mtype_zfree (const char *file, int line, int type, void *ptr)
  187. {
  188. mstat[type].t_free++;
  189. mtype_log ("xfree", ptr, file, line, type);
  190. zfree (type, ptr);
  191. }
  192. char *
  193. mtype_zstrdup (const char *file, int line, int type, const char *str)
  194. {
  195. char *memory;
  196. mstat[type].c_strdup++;
  197. memory = zstrdup (type, str);
  198. mtype_log ("xstrdup", memory, file, line, type);
  199. return memory;
  200. }
  201. #else
  202. static struct
  203. {
  204. char *name;
  205. long alloc;
  206. } mstat [MTYPE_MAX];
  207. #endif /* MEMORY_LOG */
  208. /* Increment allocation counter. */
  209. static void
  210. alloc_inc (int type)
  211. {
  212. mstat[type].alloc++;
  213. }
  214. /* Decrement allocation counter. */
  215. static void
  216. alloc_dec (int type)
  217. {
  218. mstat[type].alloc--;
  219. }
  220. /* Looking up memory status from vty interface. */
  221. #include "vector.h"
  222. #include "vty.h"
  223. #include "command.h"
  224. static void
  225. log_memstats(int pri)
  226. {
  227. struct mlist *ml;
  228. for (ml = mlists; ml->list; ml++)
  229. {
  230. struct memory_list *m;
  231. zlog (NULL, pri, "Memory utilization in module %s:", ml->name);
  232. for (m = ml->list; m->index >= 0; m++)
  233. if (m->index && mstat[m->index].alloc)
  234. zlog (NULL, pri, " %-30s: %10ld", m->format, mstat[m->index].alloc);
  235. }
  236. }
  237. void
  238. log_memstats_stderr (const char *prefix)
  239. {
  240. struct mlist *ml;
  241. struct memory_list *m;
  242. int i;
  243. int j = 0;
  244. for (ml = mlists; ml->list; ml++)
  245. {
  246. i = 0;
  247. for (m = ml->list; m->index >= 0; m++)
  248. if (m->index && mstat[m->index].alloc)
  249. {
  250. if (!i)
  251. fprintf (stderr,
  252. "%s: memstats: Current memory utilization in module %s:\n",
  253. prefix,
  254. ml->name);
  255. fprintf (stderr,
  256. "%s: memstats: %-30s: %10ld%s\n",
  257. prefix,
  258. m->format,
  259. mstat[m->index].alloc,
  260. mstat[m->index].alloc < 0 ? " (REPORT THIS BUG!)" : "");
  261. i = j = 1;
  262. }
  263. }
  264. if (j)
  265. fprintf (stderr,
  266. "%s: memstats: NOTE: If configuration exists, utilization may be "
  267. "expected.\n",
  268. prefix);
  269. else
  270. fprintf (stderr,
  271. "%s: memstats: No remaining tracked memory utilization.\n",
  272. prefix);
  273. }
  274. static void
  275. show_separator(struct vty *vty)
  276. {
  277. vty_out (vty, "-----------------------------\r\n");
  278. }
  279. static int
  280. show_memory_vty (struct vty *vty, struct memory_list *list)
  281. {
  282. struct memory_list *m;
  283. int needsep = 0;
  284. for (m = list; m->index >= 0; m++)
  285. if (m->index == 0)
  286. {
  287. if (needsep)
  288. {
  289. show_separator (vty);
  290. needsep = 0;
  291. }
  292. }
  293. else if (mstat[m->index].alloc)
  294. {
  295. vty_out (vty, "%-30s: %10ld\r\n", m->format, mstat[m->index].alloc);
  296. needsep = 1;
  297. }
  298. return needsep;
  299. }
  300. #ifdef HAVE_MALLINFO
  301. static int
  302. show_memory_mallinfo (struct vty *vty)
  303. {
  304. struct mallinfo minfo = mallinfo();
  305. char buf[MTYPE_MEMSTR_LEN];
  306. vty_out (vty, "System allocator statistics:%s", VTY_NEWLINE);
  307. vty_out (vty, " Total heap allocated: %s%s",
  308. mtype_memstr (buf, MTYPE_MEMSTR_LEN, minfo.arena),
  309. VTY_NEWLINE);
  310. vty_out (vty, " Holding block headers: %s%s",
  311. mtype_memstr (buf, MTYPE_MEMSTR_LEN, minfo.hblkhd),
  312. VTY_NEWLINE);
  313. vty_out (vty, " Used small blocks: %s%s",
  314. mtype_memstr (buf, MTYPE_MEMSTR_LEN, minfo.usmblks),
  315. VTY_NEWLINE);
  316. vty_out (vty, " Used ordinary blocks: %s%s",
  317. mtype_memstr (buf, MTYPE_MEMSTR_LEN, minfo.uordblks),
  318. VTY_NEWLINE);
  319. vty_out (vty, " Free small blocks: %s%s",
  320. mtype_memstr (buf, MTYPE_MEMSTR_LEN, minfo.fsmblks),
  321. VTY_NEWLINE);
  322. vty_out (vty, " Free ordinary blocks: %s%s",
  323. mtype_memstr (buf, MTYPE_MEMSTR_LEN, minfo.fordblks),
  324. VTY_NEWLINE);
  325. vty_out (vty, " Ordinary blocks: %ld%s",
  326. (unsigned long)minfo.ordblks,
  327. VTY_NEWLINE);
  328. vty_out (vty, " Small blocks: %ld%s",
  329. (unsigned long)minfo.smblks,
  330. VTY_NEWLINE);
  331. vty_out (vty, " Holding blocks: %ld%s",
  332. (unsigned long)minfo.hblks,
  333. VTY_NEWLINE);
  334. vty_out (vty, "(see system documentation for 'mallinfo' for meaning)%s",
  335. VTY_NEWLINE);
  336. return 1;
  337. }
  338. #endif /* HAVE_MALLINFO */
  339. DEFUN (show_memory,
  340. show_memory_cmd,
  341. "show memory",
  342. "Show running system information\n"
  343. "Memory statistics\n")
  344. {
  345. struct mlist *ml;
  346. int needsep = 0;
  347. #ifdef HAVE_MALLINFO
  348. needsep = show_memory_mallinfo (vty);
  349. #endif /* HAVE_MALLINFO */
  350. for (ml = mlists; ml->list; ml++)
  351. {
  352. if (needsep)
  353. show_separator (vty);
  354. needsep = show_memory_vty (vty, ml->list);
  355. }
  356. return CMD_SUCCESS;
  357. }
  358. void
  359. memory_init (void)
  360. {
  361. install_element (RESTRICTED_NODE, &show_memory_cmd);
  362. install_element (VIEW_NODE, &show_memory_cmd);
  363. }
  364. /* Stats querying from users */
  365. /* Return a pointer to a human friendly string describing
  366. * the byte count passed in. E.g:
  367. * "0 bytes", "2048 bytes", "110kB", "500MiB", "11GiB", etc.
  368. * Up to 4 significant figures will be given.
  369. * The pointer returned may be NULL (indicating an error)
  370. * or point to the given buffer, or point to static storage.
  371. */
  372. const char *
  373. mtype_memstr (char *buf, size_t len, unsigned long bytes)
  374. {
  375. unsigned int m, k;
  376. /* easy cases */
  377. if (!bytes)
  378. return "0 bytes";
  379. if (bytes == 1)
  380. return "1 byte";
  381. /*
  382. * When we pass the 2gb barrier mallinfo() can no longer report
  383. * correct data so it just does something odd...
  384. * Reporting like Terrabytes of data. Which makes users...
  385. * edgy.. yes edgy that's the term for it.
  386. * So let's just give up gracefully
  387. */
  388. if (bytes > 0x7fffffff)
  389. return "> 2GB";
  390. m = bytes >> 20;
  391. k = bytes >> 10;
  392. if (m > 10)
  393. {
  394. if (bytes & (1 << 19))
  395. m++;
  396. snprintf (buf, len, "%d MiB", m);
  397. }
  398. else if (k > 10)
  399. {
  400. if (bytes & (1 << 9))
  401. k++;
  402. snprintf (buf, len, "%d KiB", k);
  403. }
  404. else
  405. snprintf (buf, len, "%ld bytes", bytes);
  406. return buf;
  407. }
  408. unsigned long
  409. mtype_stats_alloc (int type)
  410. {
  411. return mstat[type].alloc;
  412. }