memory.c 11 KB

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