<p>Kevin Harwell <strong>merged</strong> this change.</p><p><a href="https://gerrit.asterisk.org/10733">View Change</a></p><div style="white-space:pre-wrap">Approvals:
  Joshua Colp: Looks good to me, but someone else must approve
  Jenkins2: Verified
  Kevin Harwell: Looks good to me, approved; Approved for Submit

</div><pre style="font-family: monospace,monospace; white-space: pre-wrap;">core: Add some documentation to the malloc_trim code<br><br>This adds documentation to handle_cli_malloc_trim() indicating how it<br>can be useful when debugging OOM conditions.<br><br>Change-Id: I1936185e78035bf123cd5e097b793a55eeebdc78<br>---<br>M main/cli.c<br>1 file changed, 34 insertions(+), 21 deletions(-)<br><br></pre><pre style="font-family: monospace,monospace; white-space: pre-wrap;"><span>diff --git a/main/cli.c b/main/cli.c</span><br><span>index 653c157..ef73ba3 100644</span><br><span>--- a/main/cli.c</span><br><span>+++ b/main/cli.c</span><br><span>@@ -1781,30 +1781,43 @@</span><br><span> </span><br><span> </span><br><span> #ifdef HAVE_MALLOC_TRIM</span><br><span style="color: hsl(0, 100%, 40%);">-    /* BUGBUG malloc_trim() is a libc specific function.  Non-portable. */</span><br><span style="color: hsl(0, 100%, 40%);">-  static char *handle_cli_malloc_trim(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)</span><br><span style="color: hsl(0, 100%, 40%);">-   {</span><br><span style="color: hsl(0, 100%, 40%);">-               extern int malloc_trim(size_t __pad) __THROW;</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">-               switch (cmd) {</span><br><span style="color: hsl(0, 100%, 40%);">-          case CLI_INIT:</span><br><span style="color: hsl(0, 100%, 40%);">-                  e->command = "malloc trim";</span><br><span style="color: hsl(0, 100%, 40%);">-                        e->usage =</span><br><span style="color: hsl(0, 100%, 40%);">-                           "Usage: malloc trim\n"</span><br><span style="color: hsl(0, 100%, 40%);">-                                "       Try to give excess memory back to the OS.\n";</span><br><span style="color: hsl(0, 100%, 40%);">-                 return NULL;</span><br><span style="color: hsl(0, 100%, 40%);">-            case CLI_GENERATE:</span><br><span style="color: hsl(0, 100%, 40%);">-                      return NULL;</span><br><span style="color: hsl(0, 100%, 40%);">-            }</span><br><span style="color: hsl(120, 100%, 40%);">+/*!</span><br><span style="color: hsl(120, 100%, 40%);">+ * \internal</span><br><span style="color: hsl(120, 100%, 40%);">+ * \brief Attempt to reclaim unused heap memory.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * Users have reported that asterisk will sometimes be killed because it can't allocate</span><br><span style="color: hsl(120, 100%, 40%);">+ * more than around 3G of memory on a 32 bit system.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * Using malloc_trim() will help us to determine if it's because there's a leak or because</span><br><span style="color: hsl(120, 100%, 40%);">+ * the heap is so fragmented that there isn't enough contiguous memory available.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * \note malloc_trim() is a GNU extension and is therefore not portable.</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+static char *handle_cli_malloc_trim(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+     extern int malloc_trim(size_t __pad) __THROW;</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">-               if (malloc_trim(0)) {</span><br><span style="color: hsl(0, 100%, 40%);">-                   ast_cli(a->fd, "Returned some memory to the OS.\n");</span><br><span style="color: hsl(0, 100%, 40%);">-               } else {</span><br><span style="color: hsl(0, 100%, 40%);">-                        ast_cli(a->fd, "No memory returned to the OS.\n");</span><br><span style="color: hsl(0, 100%, 40%);">-         }</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">-               return CLI_SUCCESS;</span><br><span style="color: hsl(120, 100%, 40%);">+   switch (cmd) {</span><br><span style="color: hsl(120, 100%, 40%);">+        case CLI_INIT:</span><br><span style="color: hsl(120, 100%, 40%);">+                e->command = "malloc trim";</span><br><span style="color: hsl(120, 100%, 40%);">+              e->usage =</span><br><span style="color: hsl(120, 100%, 40%);">+                 "Usage: malloc trim\n"</span><br><span style="color: hsl(120, 100%, 40%);">+                      "       Try to give excess memory back to the OS.\n";</span><br><span style="color: hsl(120, 100%, 40%);">+               return NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+  case CLI_GENERATE:</span><br><span style="color: hsl(120, 100%, 40%);">+            return NULL;</span><br><span>         }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+   if (malloc_trim(0)) {</span><br><span style="color: hsl(120, 100%, 40%);">+         ast_cli(a->fd, "Returned some memory to the OS.\n");</span><br><span style="color: hsl(120, 100%, 40%);">+     } else {</span><br><span style="color: hsl(120, 100%, 40%);">+              ast_cli(a->fd, "No memory returned to the OS.\n");</span><br><span style="color: hsl(120, 100%, 40%);">+       }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+   return CLI_SUCCESS;</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span> #endif</span><br><span> </span><br><span> static char *handle_help(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a);</span><br><span></span><br></pre><p>To view, visit <a href="https://gerrit.asterisk.org/10733">change 10733</a>. To unsubscribe, or for help writing mail filters, visit <a href="https://gerrit.asterisk.org/settings">settings</a>.</p><div itemscope itemtype="http://schema.org/EmailMessage"><div itemscope itemprop="action" itemtype="http://schema.org/ViewAction"><link itemprop="url" href="https://gerrit.asterisk.org/10733"/><meta itemprop="name" content="View Change"/></div></div>

<div style="display:none"> Gerrit-Project: asterisk </div>
<div style="display:none"> Gerrit-Branch: master </div>
<div style="display:none"> Gerrit-MessageType: merged </div>
<div style="display:none"> Gerrit-Change-Id: I1936185e78035bf123cd5e097b793a55eeebdc78 </div>
<div style="display:none"> Gerrit-Change-Number: 10733 </div>
<div style="display:none"> Gerrit-PatchSet: 2 </div>
<div style="display:none"> Gerrit-Owner: Sean Bright <sean.bright@gmail.com> </div>
<div style="display:none"> Gerrit-Reviewer: Jenkins2 (1000185) </div>
<div style="display:none"> Gerrit-Reviewer: Joshua Colp <jcolp@digium.com> </div>
<div style="display:none"> Gerrit-Reviewer: Kevin Harwell <kharwell@digium.com> </div>