<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[Freeswitch-trunk][16963] </title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<div id="header">FreeSWITCH Subversion</div>
<dl class="meta">
<dt>Revision</dt> <dd><a href="http://fisheye.freeswitch.org/changelog/FreeSWITCH?cs=16963">16963</a></dd>
<dt>Author</dt> <dd>anthm</dd>
<dt>Date</dt> <dd>2010-03-10 14:44:36 -0600 (Wed, 10 Mar 2010)</dd>
</dl>

<h3>Log Message</h3>
<pre>the last commit would be a little more useful if i check in the files too</pre>

<h3>Added Paths</h3>
<ul>
<li><a href="#freeswitchtrunksrcincludeswitch_profileh">freeswitch/trunk/src/include/switch_profile.h</a></li>
<li><a href="#freeswitchtrunksrcswitch_profilec">freeswitch/trunk/src/switch_profile.c</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="freeswitchtrunksrcincludeswitch_profileh"></a>
<div class="addfile"><h4>Added: freeswitch/trunk/src/include/switch_profile.h (0 => 16963)</h4>
<pre class="diff"><span>
<span class="info">--- freeswitch/trunk/src/include/switch_profile.h                                (rev 0)
+++ freeswitch/trunk/src/include/switch_profile.h        2010-03-10 20:44:36 UTC (rev 16963)
</span><span class="lines">@@ -0,0 +1,58 @@
</span><ins>+/*
+ * Copyright (c) 2009, Sangoma Technologies
+ * Moises Silva &lt;moy@sangoma.com&gt;
+ * All rights reserved.
+ * 
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 
+ * * Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 
+ * * Neither the name of the original author; nor the names of any contributors
+ * may be used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ * 
+ * 
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * &quot;AS IS&quot; AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER
+ * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
+ * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
+ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+/*! \brief opaque profile timer structure */
+struct profile_timer;
+typedef struct profile_timer switch_profile_timer_t;
+
+/*! 
+ * \brief create a new profile timer
+ * \return profile timer structure previously created with new_profile_timer, NULL on error
+ */
+SWITCH_DECLARE(switch_profile_timer_t *) switch_new_profile_timer(void);
+
+/*! 
+ * \brief provides the percentage of idle system time
+ * \param p profile timer structure previously created with new_profile_timer
+ * \param pointer to store the percentage of idle time
+ * \return -1 on error 0 for success
+ */
+SWITCH_DECLARE(int) switch_get_system_idle_time(switch_profile_timer_t *p, double *idle_percentage);
+
+
+/*! 
+ * \brief Deletes profile timer
+ */
+SWITCH_DECLARE(void) switch_delete_profile_timer(switch_profile_timer_t **p);
+
</ins></span></pre></div>
<a id="freeswitchtrunksrcswitch_profilec"></a>
<div class="addfile"><h4>Added: freeswitch/trunk/src/switch_profile.c (0 => 16963)</h4>
<pre class="diff"><span>
<span class="info">--- freeswitch/trunk/src/switch_profile.c                                (rev 0)
+++ freeswitch/trunk/src/switch_profile.c        2010-03-10 20:44:36 UTC (rev 16963)
</span><span class="lines">@@ -0,0 +1,253 @@
</span><ins>+/*
+ * Copyright (c) 2009, Sangoma Technologies
+ * Moises Silva &lt;moy@sangoma.com&gt;
+ * All rights reserved.
+ * 
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 
+ * * Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 
+ * * Neither the name of the original author; nor the names of any contributors
+ * may be used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ * 
+ * 
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * &quot;AS IS&quot; AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER
+ * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
+ * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
+ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+#include &quot;switch.h&quot;
+#include &quot;switch_profile.h&quot;
+
+#ifdef __linux__
+#include &lt;stdio.h&gt;
+#include &lt;string.h&gt;
+#include &lt;stdlib.h&gt;
+#include &lt;unistd.h&gt;
+#include &lt;sys/types.h&gt;
+#include &lt;fcntl.h&gt;
+#include &lt;errno.h&gt;
+#else
+#define _WIN32_WINNT 0x0501 // To make GetSystemTimes visible in windows.h
+#include &lt;windows.h&gt;
+#endif
+
+struct profile_timer 
+{
+        /* bool, just used to retrieve the values for the first time and not calculate the percentage of idle time */
+        int valid_last_times;
+
+        /* last calculated percentage of idle time */
+        double last_percentage_of_idle_time;
+
+#ifdef __linux__
+        /* all of these are the Linux jiffies last retrieved count */
+        unsigned long long last_user_time;
+        unsigned long long last_system_time;
+        unsigned long long last_idle_time;
+
+        unsigned long long last_nice_time;
+        unsigned long long last_irq_time;
+        unsigned long long last_soft_irq_time;
+        unsigned long long last_io_wait_time;
+        unsigned long long last_steal_time;
+
+        /* /proc/stat file descriptor used to retrieve the counters */
+        int procfd;
+        int initd;
+#else
+        __int64 i64LastUserTime;
+        __int64 i64LastKernelTime;
+        __int64 i64LastIdleTime;
+#endif
+};
+
+#ifdef __linux__
+static int read_cpu_stats(switch_profile_timer_t *p, 
+                      unsigned long long *user, 
+                      unsigned long long *nice, 
+                      unsigned long long *system, 
+                      unsigned long long *idle, 
+                      unsigned long long *iowait, 
+                      unsigned long long *irq, 
+                      unsigned long long *softirq, 
+                      unsigned long long *steal)
+{
+// the output of proc should not change that often from one kernel to other
+// see fs/proc/proc_misc.c or fs/proc/stat.c in the Linux kernel for more details
+// also man 5 proc is useful
+#define CPU_ELEMENTS 8 // change this if you change the format string
+#define CPU_INFO_FORMAT &quot;cpu  %llu %llu %llu %llu %llu %llu %llu %llu&quot;
+        static const char procfile[] = &quot;/proc/stat&quot;;
+        int rc = 0;
+        int myerrno = 0;
+        int elements = 0;
+        const char *cpustr = NULL;
+        char statbuff[1024];
+
+        if (!p-&gt;initd) {
+                p-&gt;procfd = open(procfile, O_RDONLY, 0);
+                if(p-&gt;procfd == -1) {
+                        switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, &quot;Failed to open CPU statistics file %s: %s\n&quot;, procfile, strerror(myerrno));
+                        return -1;
+                }
+                p-&gt;initd = 1;
+        } else {
+                lseek(p-&gt;procfd, 0L, SEEK_SET);
+        }
+
+        rc = read(p-&gt;procfd, statbuff, sizeof(statbuff) - 1);
+        if (rc &lt;= 0) {
+                myerrno = errno;
+                switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, &quot;Failed to read CPU statistics file %s: %s\n&quot;, procfile, strerror(myerrno));
+                return -1;
+        }
+
+        cpustr = strstr(statbuff, &quot;cpu &quot;);
+        if (!cpustr) {
+                switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, &quot;wrong format for Linux proc cpu statistics: missing cpu string\n&quot;);
+                return -1;
+        }
+
+        elements = sscanf(cpustr, CPU_INFO_FORMAT, user, nice, system, idle, iowait, irq, softirq, steal);
+        if (elements != CPU_ELEMENTS) {
+                switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, &quot;wrong format for Linux proc cpu statistics: expected %d elements, but just found %d\n&quot;, CPU_ELEMENTS, elements);
+                return -1;
+        }
+        return 0;
+}
+#endif
+
+#ifdef __linux__
+SWITCH_DECLARE(int) switch_get_system_idle_time(switch_profile_timer_t *p, double *idle_percentage)
+{
+        unsigned long long user, nice, system, idle, iowait, irq, softirq, steal;
+        unsigned long long usertime, kerneltime, idletime, totaltime, halftime;
+
+        if (read_cpu_stats(p, &amp;user, &amp;nice, &amp;system, &amp;idle, &amp;iowait, &amp;irq, &amp;softirq, &amp;steal)) {
+                switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, &quot;Failed to retrieve Linux CPU statistics\n&quot;);
+                return -1;
+        }
+
+        if (!p-&gt;valid_last_times) {
+                // we dont strictly need to save all of them but I feel code is more clear if we do
+                p-&gt;valid_last_times = 1;
+                p-&gt;last_user_time = user;
+                p-&gt;last_nice_time = nice;
+                p-&gt;last_system_time = system;
+                p-&gt;last_irq_time = irq;
+                p-&gt;last_soft_irq_time = softirq;
+                p-&gt;last_io_wait_time = iowait;
+                p-&gt;last_steal_time = steal;
+                p-&gt;last_idle_time = idle;
+                p-&gt;last_percentage_of_idle_time = 100.0;
+                *idle_percentage = p-&gt;last_percentage_of_idle_time;
+                return 0;
+        }
+
+        usertime = (user - p-&gt;last_user_time) + (nice - p-&gt;last_nice_time);
+        kerneltime = (system - p-&gt;last_system_time) + (irq - p-&gt;last_irq_time) + (softirq - p-&gt;last_soft_irq_time);
+        kerneltime += (iowait - p-&gt;last_io_wait_time);
+        kerneltime += (steal - p-&gt;last_steal_time);
+        idletime = (idle - p-&gt;last_idle_time);
+
+        totaltime = usertime + kerneltime + idletime;
+
+        if (totaltime &lt;= 0) {
+                // this may happen if not enough time has elapsed and the jiffies counters are the same than the last time we checked
+                // jiffies depend on timer interrupts which depend on the number of HZ compile time setting of the kernel
+                // typical configs set HZ to 100 (that means, 100 jiffies updates per second, that is one each 10ms)
+                // avoid an arithmetic exception and return the same values
+                *idle_percentage = p-&gt;last_percentage_of_idle_time;
+                return 0;
+        }
+
+        halftime = totaltime / 2UL;
+
+        p-&gt;last_percentage_of_idle_time = ((100 * idletime + halftime) / totaltime);
+        *idle_percentage = p-&gt;last_percentage_of_idle_time;
+
+        p-&gt;last_user_time = user;
+        p-&gt;last_nice_time = nice;
+        p-&gt;last_system_time = system;
+        p-&gt;last_irq_time = irq;
+        p-&gt;last_soft_irq_time = softirq;
+        p-&gt;last_io_wait_time = iowait;
+        p-&gt;last_steal_time = steal;
+        p-&gt;last_idle_time = idle;
+
+        return 0;
+}
+
+#else
+
+SWITCH_DECLARE(int) switch_get_system_idle_time(switch_profile_timer_t *p, double *idle_percentage)
+{
+        FILETIME idleTime;
+        FILETIME kernelTime;
+        FILETIME userTime;
+  
+        if (!::GetSystemTimes(&amp;idleTime, &amp;kernelTime, &amp;userTime)) {
+                return false;
+        }
+  
+        __int64 i64UserTime = (__int64)userTime.dwLowDateTime | ((__int64)userTime.dwHighDateTime &lt;&lt; 32);
+
+        __int64 i64KernelTime = (__int64)kernelTime.dwLowDateTime | ((__int64)kernelTime.dwHighDateTime &lt;&lt; 32);
+
+        __int64 i64IdleTime = (__int64)idleTime.dwLowDateTime | ((__int64)idleTime.dwHighDateTime &lt;&lt; 32);
+
+        if (p-&gt;valid_last_times) {
+                __int64 i64User = i64UserTime - p-&gt;i64LastUserTime;
+                __int64 i64Kernel = i64KernelTime - p-&gt;i64LastKernelTime;
+                __int64 i64Idle = i64IdleTime - p-&gt;i64LastIdleTime;
+                __int64 i64System = i64User + i64Kernel;
+                *idle_percentage = 100.0 * i64Idle / i64System;
+        } else {
+                *idle_percentage = 100.0;
+                p-&gt;valid_last_times = 1;
+        }
+
+        /* Remember current value for the next call */
+        p-&gt;i64LastUserTime = i64UserTime;
+        p-&gt;i64LastKernelTime = i64KernelTime;
+        p-&gt;i64LastIdleTime = i64IdleTime;
+
+        /* Success */
+        return 0;
+}
+
+#endif
+
+SWITCH_DECLARE(switch_profile_timer_t *)switch_new_profile_timer(void)
+{
+        return calloc(1, sizeof(switch_profile_timer_t));
+}
+
+SWITCH_DECLARE(void) switch_delete_profile_timer(switch_profile_timer_t **p)
+{
+        if (!p) return;
+
+#ifdef __linux__
+        close((*p)-&gt;procfd);
+#endif
+        free(*p);
+        *p = NULL;
+}
+
+
</ins></span></pre>
</div>
</div>
<div id="footer">See you at ClueCon</div>

</body>
</html>