<p>Sean Bright has uploaded this change for <strong>review</strong>.</p><p><a href="https://gerrit.asterisk.org/10731">View Change</a></p><pre style="font-family: monospace,monospace; white-space: pre-wrap;">core: Add some documentation to the the malloc_trim code<br><br>This adds documentation to handle_cli_malloc_trim() indicating how it can be a<br>useful 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;">git pull ssh://gerrit.asterisk.org:29418/asterisk refs/changes/31/10731/1</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 f980fc6..957950d 100644</span><br><span>--- a/main/cli.c</span><br><span>+++ b/main/cli.c</span><br><span>@@ -1793,30 +1793,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 it 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/10731">change 10731</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/10731"/><meta itemprop="name" content="View Change"/></div></div>

<div style="display:none"> Gerrit-Project: asterisk </div>
<div style="display:none"> Gerrit-Branch: 13 </div>
<div style="display:none"> Gerrit-MessageType: newchange </div>
<div style="display:none"> Gerrit-Change-Id: I1936185e78035bf123cd5e097b793a55eeebdc78 </div>
<div style="display:none"> Gerrit-Change-Number: 10731 </div>
<div style="display:none"> Gerrit-PatchSet: 1 </div>
<div style="display:none"> Gerrit-Owner: Sean Bright <sean.bright@gmail.com> </div>