]> git.ipfire.org Git - thirdparty/freeswitch.git/commitdiff
add softtimer to the core, begin framework for static modules and reduces calls to...
authorAnthony Minessale <anthony.minessale@gmail.com>
Wed, 3 Oct 2007 16:44:11 +0000 (16:44 +0000)
committerAnthony Minessale <anthony.minessale@gmail.com>
Wed, 3 Oct 2007 16:44:11 +0000 (16:44 +0000)
git-svn-id: http://svn.freeswitch.org/svn/freeswitch/trunk@5784 d0543943-73ff-0310-b7d9-9358b9ac24b2

21 files changed:
Makefile.am
src/include/private/switch_core_pvt.h
src/include/switch_core.h
src/include/switch_loadable_module.h
src/mod/applications/mod_fifo/mod_fifo.c
src/mod/endpoints/mod_alsa/mod_alsa.c
src/mod/endpoints/mod_dingaling/mod_dingaling.c
src/mod/endpoints/mod_portaudio/mod_portaudio.c
src/mod/endpoints/mod_sofia/sofia.c
src/mod/say/mod_say_en/mod_say_en.c
src/mod/timers/mod_softtimer/mod_softtimer.vcproj [deleted file]
src/softtimer.c [moved from src/mod/timers/mod_softtimer/mod_softtimer.c with 85% similarity]
src/switch_channel.c
src/switch_console.c
src/switch_core.c
src/switch_core_session.c
src/switch_event.c
src/switch_loadable_module.c
src/switch_log.c
src/switch_scheduler.c
src/switch_stun.c

index bfc254e1c654f1ac7ff6e3b1e2c826441ebb73be..e28123e356524246017e2f170c854f472a23fb09 100644 (file)
@@ -53,6 +53,7 @@ src/switch_ivr.c \
 src/switch_stun.c\
 src/switch_log.c\
 src/switch_xml.c\
+src/softtimer.c\
 libs/stfu/stfu.c\
 src/switch_cpp.cpp\
 libs/libteletone/src/libteletone_detect.c\
index b7c698f6a3083f90aa8f65b6a4f5d5a8ccb46fce..3992eb9e24a6c0cae6d1eb8c7de67c97f11864cd 100644 (file)
@@ -152,6 +152,10 @@ struct switch_runtime {
        uint8_t running;
        char uuid_str[SWITCH_UUID_FORMATTED_LENGTH + 1];
        uint32_t flags;
+       switch_time_t timestamp;
+       switch_mutex_t *throttle_mutex;
+       uint32_t sps_total;
+       int32_t sps;
 };
 
 extern struct switch_runtime runtime;
index f08cafa4f8e4ca42ed7fbcb5ffd0af301fb883aa..819f74e8c92ea7a25a7f2e0ddda751b5a87d8ebb 100644 (file)
@@ -252,6 +252,13 @@ SWITCH_DECLARE(switch_status_t) switch_core_init_and_modload(const char *console
 */
 SWITCH_DECLARE(uint32_t) switch_core_session_limit(uint32_t new_limit);
 
+/*! 
+  \brief Set/Get Session Rate Limit
+  \param new_limit new value (if > 0)
+  \return the current session rate limit
+*/
+SWITCH_DECLARE(uint32_t) switch_core_sessions_per_second(uint32_t new_limit);
+
 /*! 
   \brief Destroy the core
   \note to be called at application shutdown
@@ -1464,6 +1471,7 @@ SWITCH_DECLARE(uint8_t) switch_core_session_compare(switch_core_session_t *a, sw
 
 SWITCH_DECLARE(switch_loadable_module_interface_t *) switch_loadable_module_create_module_interface(switch_memory_pool_t *pool, const char *name);
 SWITCH_DECLARE(void *) switch_loadable_module_create_interface(switch_loadable_module_interface_t *mod, switch_module_interface_name_t iname);
+SWITCH_DECLARE(switch_time_t) switch_timestamp_now(void);
 ///\}
 
 /*!
index a38446fac4808680b413f70864b0ef8ee438307f..7df9c4891e8db8ed3d76edea82cd2dc44e071cfc 100644 (file)
@@ -120,13 +120,16 @@ SWITCH_DECLARE(switch_dialplan_interface_t *) switch_loadable_module_get_dialpla
   \param switch_module_load the function to call when the module is loaded
   \param switch_module_runtime a function requested to be started in it's own thread once loaded
   \param switch_module_shutdown the function to call when the system is shutdown
+  \param runtime start the runtime thread or not
   \return the resulting status
   \note only use this function if you are making a module that in turn gateways module loading to another technology
  */
 SWITCH_DECLARE(switch_status_t) switch_loadable_module_build_dynamic(char *filename,
                                                                                                                                         switch_module_load_t switch_module_load,
                                                                                                                                         switch_module_runtime_t switch_module_runtime,
-                                                                                                                                        switch_module_shutdown_t switch_module_shutdown);
+                                                                                                                                        switch_module_shutdown_t switch_module_shutdown,
+                                                                                                                                        switch_bool_t runtime);
+                                                                                                                                        
 
 /*!
   \brief Retrieve the timer interface by it's registered name
@@ -312,6 +315,11 @@ SWITCH_MOD_DECLARE(switch_status_t) switch_module_shutdown(void);
 
 ///\}
 
+#define SWITCH_DECLARE_STATIC_MODULE(init, load, run, shut) void init(void) { \
+               switch_loadable_module_build_dynamic(__FILE__, load, run, shut, SWITCH_FALSE); \
+       }
+
+
 SWITCH_END_EXTERN_C
 #endif
 /* For Emacs:
index 5a26731a454a6eadce801becc5c77d41d6ba80eb..798ca166c1f82e2b6402a1f62f0ae2dc02a258a8 100644 (file)
@@ -112,7 +112,7 @@ SWITCH_STANDARD_APP(fifo_function)
     switch_event_t *event = NULL;
     char date[80] = "";
     switch_time_exp_t tm;
-    switch_time_t ts = switch_time_now();
+    switch_time_t ts = switch_timestamp_now();
     switch_size_t retsize;
 
 
@@ -181,6 +181,7 @@ SWITCH_STANDARD_APP(fifo_function)
         switch_queue_push(node->fifo, uuid);
         switch_mutex_unlock(node->mutex);
 
+        ts = switch_timestamp_now();
         switch_time_exp_lt(&tm, ts);
         switch_strftime(date, &retsize, sizeof(date), "%Y-%m-%d %T", &tm);
         switch_channel_set_variable(channel, "fifo_status", "WAITING");
@@ -208,7 +209,7 @@ SWITCH_STANDARD_APP(fifo_function)
         if (switch_channel_ready(channel)) {
             switch_channel_set_state(channel, CS_HIBERNATE);            
         } else {
-
+            ts = switch_timestamp_now();
             switch_time_exp_lt(&tm, ts);
             switch_strftime(date, &retsize, sizeof(date), "%Y-%m-%d %T", &tm);
             switch_channel_set_variable(channel, "fifo_status", "ABORTED");
@@ -263,7 +264,7 @@ SWITCH_STANDARD_APP(fifo_function)
             switch_event_fire(&event);
         }
 
-
+        ts = switch_timestamp_now();
         switch_time_exp_lt(&tm, ts);
         switch_strftime(date, &retsize, sizeof(date), "%Y-%m-%d %T", &tm);
         switch_channel_set_variable(channel, "fifo_status", "WAITING");
@@ -344,6 +345,7 @@ SWITCH_STANDARD_APP(fifo_function)
                 assert(cloned_profile->next == NULL);
                 switch_channel_set_originatee_caller_profile(channel, cloned_profile);
                                
+                ts = switch_timestamp_now();
                 switch_time_exp_lt(&tm, ts);
                 switch_strftime(date, &retsize, sizeof(date), "%Y-%m-%d %T", &tm);
                 switch_channel_set_variable(channel, "fifo_status", "TALKING");
@@ -355,6 +357,7 @@ SWITCH_STANDARD_APP(fifo_function)
                 switch_channel_set_variable(other_channel, "fifo_target", switch_core_session_get_uuid(session));
                 switch_ivr_multi_threaded_bridge(session, other_session, on_dtmf, other_session, session);
 
+                ts = switch_timestamp_now();
                 switch_time_exp_lt(&tm, ts);
                 switch_strftime(date, &retsize, sizeof(date), "%Y-%m-%d %T", &tm);
                 switch_channel_set_variable(channel, "fifo_status", "WAITING");
index 3d77e9931016b57f32e6a5bc32bff706fb8750f0..51053a6e5f7e8d88e3ff9f2a31a476700ce6b97d 100644 (file)
@@ -180,7 +180,7 @@ static switch_status_t channel_on_init(switch_core_session_t *session)
        assert(channel != NULL);
 
 
-       last = switch_time_now() - waitsec;
+       last = switch_timestamp_now() - waitsec;
 
 
 
@@ -243,7 +243,7 @@ static switch_status_t channel_on_init(switch_core_session_t *session)
                switch_channel_mark_ring_ready(channel);
 
                while (switch_channel_get_state(channel) == CS_INIT && !switch_test_flag(tech_pvt, TFLAG_ANSWER)) {
-                       if (switch_time_now() - last >= waitsec) {
+                       if (switch_timestamp_now() - last >= waitsec) {
                                char buf[512];
                                switch_event_t *event;
 
@@ -257,7 +257,7 @@ static switch_status_t channel_on_init(switch_core_session_t *session)
                                }
 
                                switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "%s\n", buf);
-                               last = switch_time_now();
+                               last = switch_timestamp_now();
                                if (ring_file) {
                                        unsigned int pos = 0;
                                        switch_core_file_seek(&fh, &pos, 0, SEEK_SET);
index c3e1f2c3834619f8c6669b168d852840973b20e1..266071245e1fafb260c0c40a771a3eb5833964d5 100644 (file)
@@ -1061,18 +1061,18 @@ static switch_status_t negotiate_media(switch_core_session_t *session)
 
        switch_set_flag_locked(tech_pvt, TFLAG_IO);
 
-       started = switch_time_now();
+       started = switch_timestamp_now();
 
        /* jingle has no ringing indication so we will just pretend that we got one */
        switch_core_session_queue_indication(session, SWITCH_MESSAGE_INDICATE_RINGING);
        switch_channel_mark_ring_ready(channel);
 
        if (switch_test_flag(tech_pvt, TFLAG_OUTBOUND)) {
-               tech_pvt->next_cand = switch_time_now() + DL_CAND_WAIT;
-               tech_pvt->next_desc = switch_time_now();
+               tech_pvt->next_cand = started + DL_CAND_WAIT;
+               tech_pvt->next_desc = started;
        } else {
-               tech_pvt->next_cand = switch_time_now() + DL_CAND_WAIT;
-               tech_pvt->next_desc = switch_time_now() + DL_CAND_WAIT;
+               tech_pvt->next_cand = started + DL_CAND_WAIT;
+               tech_pvt->next_desc = started + DL_CAND_WAIT;
        }
 
        while (!(switch_test_flag(tech_pvt, TFLAG_CODEC_READY) &&
@@ -1080,7 +1080,7 @@ static switch_status_t negotiate_media(switch_core_session_t *session)
                         switch_test_flag(tech_pvt, TFLAG_ANSWER) && switch_test_flag(tech_pvt, TFLAG_TRANSPORT_ACCEPT) &&
                         tech_pvt->remote_ip && tech_pvt->remote_port
                         && switch_test_flag(tech_pvt, TFLAG_TRANSPORT))) {
-               now = switch_time_now();
+               now = switch_timestamp_now();
                elapsed = (unsigned int) ((now - started) / 1000);
 
                if (switch_channel_get_state(channel) >= CS_HANGUP || switch_test_flag(tech_pvt, TFLAG_BYE)) {
@@ -1365,7 +1365,7 @@ static switch_status_t channel_read_frame(switch_core_session_t *session, switch
 
 #if 0
        if (tech_pvt->last_read) {
-               elapsed = (unsigned int) ((switch_time_now() - tech_pvt->last_read) / 1000);
+               elapsed = (unsigned int) ((switch_timestamp_now() - tech_pvt->last_read) / 1000);
                if (elapsed > 60000) {
                        return SWITCH_STATUS_TIMEOUT;
                }
@@ -1393,7 +1393,7 @@ static switch_status_t channel_read_frame(switch_core_session_t *session, switch
                        payload = tech_pvt->read_frame.payload;
 
 #if 0
-                       elapsed = (unsigned int) ((switch_time_now() - started) / 1000);
+                       elapsed = (unsigned int) ((switch_timestamp_now() - started) / 1000);
 
                        if (timeout > -1) {
                                if (elapsed >= (unsigned int) timeout) {
@@ -1401,7 +1401,7 @@ static switch_status_t channel_read_frame(switch_core_session_t *session, switch
                                }
                        }
 
-                       elapsed = (unsigned int) ((switch_time_now() - last_act) / 1000);
+                       elapsed = (unsigned int) ((switch_timestamp_now() - last_act) / 1000);
                        if (elapsed >= hard_timeout) {
                                return SWITCH_STATUS_BREAK;
                        }
index f6df069639771768ae1a68796f49260bedeb8e90..800b0055f56adae1e85cb27bb9be43e1b870af7f 100644 (file)
@@ -179,7 +179,7 @@ SWITCH_DECLARE_GLOBAL_STRING_FUNC(set_global_dialplan, globals.dialplan)
        assert(channel != NULL);
 
 
-       last = switch_time_now() - waitsec;
+       last = switch_timestamp_now() - waitsec;
 
 
 
@@ -244,7 +244,7 @@ SWITCH_DECLARE_GLOBAL_STRING_FUNC(set_global_dialplan, globals.dialplan)
                while (switch_channel_get_state(channel) == CS_INIT && !switch_test_flag(tech_pvt, TFLAG_ANSWER)) {
                        switch_size_t olen = globals.timer.samples;
                                
-                       if (switch_time_now() - last >= waitsec) {
+                       if (switch_timestamp_now() - last >= waitsec) {
                                char buf[512];
                                switch_event_t *event;
 
@@ -257,7 +257,7 @@ SWITCH_DECLARE_GLOBAL_STRING_FUNC(set_global_dialplan, globals.dialplan)
                                        switch_event_fire(&event);
                                }
                                switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "%s\n", buf);
-                               last = switch_time_now();
+                               last = switch_timestamp_now();
                        }
                        
                        if (ring_file) {
index 0f891317ea7195277e6ccfa503f8bb5c9a1017ef..2d19f1069c39ec56ae952849e93e4d32ed036ecd 100644 (file)
@@ -1832,8 +1832,7 @@ void sofia_handle_sip_i_invite(nua_t *nua, sofia_profile_t *profile, nua_handle_
        }
        
        if (!sofia_endpoint_interface || !(session = switch_core_session_request(sofia_endpoint_interface, NULL))) {
-               switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "Session Alloc Failed!\n");
-               nua_respond(nh, SIP_503_SERVICE_UNAVAILABLE, TAG_END());
+               nua_respond(nh, SIP_486_BUSY_HERE, TAG_END());
                return;
        }
 
index f8a3ccfcf90f7474eae7c736e1800f7d46d29900..3ff8bbf690cbff6aa0d598228be0cfd85c59bd50 100644 (file)
@@ -346,7 +346,7 @@ static switch_status_t en_say_time(switch_core_session_t *session, char *tosay,
        if ((t = atoi(tosay)) > 0) {
                target = switch_time_make(t, 0);
        } else {
-               target = switch_time_now();
+               target = switch_timestamp_now();
        }
        switch_time_exp_lt(&tm, target);
 
diff --git a/src/mod/timers/mod_softtimer/mod_softtimer.vcproj b/src/mod/timers/mod_softtimer/mod_softtimer.vcproj
deleted file mode 100644 (file)
index 917b364..0000000
+++ /dev/null
@@ -1,210 +0,0 @@
-<?xml version="1.0" encoding="Windows-1252"?>\r
-<VisualStudioProject\r
-       ProjectType="Visual C++"\r
-       Version="8.00"\r
-       Name="mod_softtimer"\r
-       ProjectGUID="{DCC13474-28DF-47CA-A8EB-72F8CE9A78C5}"\r
-       RootNamespace="mod_softtimer"\r
-       Keyword="Win32Proj"\r
-       >\r
-       <Platforms>\r
-               <Platform\r
-                       Name="Win32"\r
-               />\r
-       </Platforms>\r
-       <ToolFiles>\r
-       </ToolFiles>\r
-       <Configurations>\r
-               <Configuration\r
-                       Name="Debug|Win32"\r
-                       OutputDirectory="$(ConfigurationName)"\r
-                       IntermediateDirectory="$(ConfigurationName)"\r
-                       ConfigurationType="2"\r
-                       InheritedPropertySheets="$(VCInstallDir)VCProjectDefaults\UpgradeFromVC71.vsprops"\r
-                       CharacterSet="2"\r
-                       >\r
-                       <Tool\r
-                               Name="VCPreBuildEventTool"\r
-                       />\r
-                       <Tool\r
-                               Name="VCCustomBuildTool"\r
-                       />\r
-                       <Tool\r
-                               Name="VCXMLDataGeneratorTool"\r
-                       />\r
-                       <Tool\r
-                               Name="VCWebServiceProxyGeneratorTool"\r
-                       />\r
-                       <Tool\r
-                               Name="VCMIDLTool"\r
-                       />\r
-                       <Tool\r
-                               Name="VCCLCompilerTool"\r
-                               Optimization="0"\r
-                               AdditionalIncludeDirectories="&quot;$(InputDir)..\..\..\include&quot;;&quot;$(InputDir)include&quot;;&quot;$(InputDir)..\..\..\..\libs\include&quot;"\r
-                               PreprocessorDefinitions="WIN32;_DEBUG;_WINDOWS;_USRDLL;MOD_EXPORTS"\r
-                               MinimalRebuild="true"\r
-                               BasicRuntimeChecks="3"\r
-                               RuntimeLibrary="3"\r
-                               UsePrecompiledHeader="0"\r
-                               WarningLevel="4"\r
-                               WarnAsError="true"\r
-                               Detect64BitPortabilityProblems="true"\r
-                               DebugInformationFormat="3"\r
-                       />\r
-                       <Tool\r
-                               Name="VCManagedResourceCompilerTool"\r
-                       />\r
-                       <Tool\r
-                               Name="VCResourceCompilerTool"\r
-                       />\r
-                       <Tool\r
-                               Name="VCPreLinkEventTool"\r
-                       />\r
-                       <Tool\r
-                               Name="VCLinkerTool"\r
-                               OutputFile="$(SolutionDir)$(OutDir)/mod/$(InputName).dll"\r
-                               LinkIncremental="1"\r
-                               AdditionalLibraryDirectories="..\..\..\..\w32\vsnet\$(OutDir)"\r
-                               GenerateDebugInformation="true"\r
-                               ProgramDatabaseFile="$(OutDir)$(TargetName).pdb"\r
-                               SubSystem="2"\r
-                               ImportLibrary="$(OutDir)/mod_softtimer.lib"\r
-                               TargetMachine="1"\r
-                       />\r
-                       <Tool\r
-                               Name="VCALinkTool"\r
-                       />\r
-                       <Tool\r
-                               Name="VCManifestTool"\r
-                       />\r
-                       <Tool\r
-                               Name="VCXDCMakeTool"\r
-                       />\r
-                       <Tool\r
-                               Name="VCBscMakeTool"\r
-                       />\r
-                       <Tool\r
-                               Name="VCFxCopTool"\r
-                       />\r
-                       <Tool\r
-                               Name="VCAppVerifierTool"\r
-                       />\r
-                       <Tool\r
-                               Name="VCWebDeploymentTool"\r
-                       />\r
-                       <Tool\r
-                               Name="VCPostBuildEventTool"\r
-                       />\r
-               </Configuration>\r
-               <Configuration\r
-                       Name="Release|Win32"\r
-                       OutputDirectory="$(ConfigurationName)"\r
-                       IntermediateDirectory="$(ConfigurationName)"\r
-                       ConfigurationType="2"\r
-                       InheritedPropertySheets="$(VCInstallDir)VCProjectDefaults\UpgradeFromVC71.vsprops"\r
-                       CharacterSet="2"\r
-                       >\r
-                       <Tool\r
-                               Name="VCPreBuildEventTool"\r
-                       />\r
-                       <Tool\r
-                               Name="VCCustomBuildTool"\r
-                       />\r
-                       <Tool\r
-                               Name="VCXMLDataGeneratorTool"\r
-                       />\r
-                       <Tool\r
-                               Name="VCWebServiceProxyGeneratorTool"\r
-                       />\r
-                       <Tool\r
-                               Name="VCMIDLTool"\r
-                       />\r
-                       <Tool\r
-                               Name="VCCLCompilerTool"\r
-                               AdditionalIncludeDirectories="&quot;$(InputDir)..\..\..\include&quot;;&quot;$(InputDir)include&quot;;&quot;$(InputDir)..\..\..\..\libs\include&quot;"\r
-                               PreprocessorDefinitions="WIN32;NDEBUG;_WINDOWS;_USRDLL;MOD_EXPORTS"\r
-                               RuntimeLibrary="2"\r
-                               UsePrecompiledHeader="0"\r
-                               WarningLevel="4"\r
-                               WarnAsError="true"\r
-                               Detect64BitPortabilityProblems="true"\r
-                               DebugInformationFormat="3"\r
-                       />\r
-                       <Tool\r
-                               Name="VCManagedResourceCompilerTool"\r
-                       />\r
-                       <Tool\r
-                               Name="VCResourceCompilerTool"\r
-                       />\r
-                       <Tool\r
-                               Name="VCPreLinkEventTool"\r
-                       />\r
-                       <Tool\r
-                               Name="VCLinkerTool"\r
-                               OutputFile="$(SolutionDir)$(OutDir)/mod/$(InputName).dll"\r
-                               LinkIncremental="1"\r
-                               AdditionalLibraryDirectories="..\..\..\..\w32\vsnet\$(OutDir)"\r
-                               GenerateDebugInformation="true"\r
-                               ProgramDatabaseFile="$(OutDir)$(TargetName).pdb"\r
-                               SubSystem="2"\r
-                               OptimizeReferences="2"\r
-                               EnableCOMDATFolding="2"\r
-                               ImportLibrary="$(OutDir)/mod_softtimer.lib"\r
-                               TargetMachine="1"\r
-                       />\r
-                       <Tool\r
-                               Name="VCALinkTool"\r
-                       />\r
-                       <Tool\r
-                               Name="VCManifestTool"\r
-                       />\r
-                       <Tool\r
-                               Name="VCXDCMakeTool"\r
-                       />\r
-                       <Tool\r
-                               Name="VCBscMakeTool"\r
-                       />\r
-                       <Tool\r
-                               Name="VCFxCopTool"\r
-                       />\r
-                       <Tool\r
-                               Name="VCAppVerifierTool"\r
-                       />\r
-                       <Tool\r
-                               Name="VCWebDeploymentTool"\r
-                       />\r
-                       <Tool\r
-                               Name="VCPostBuildEventTool"\r
-                       />\r
-               </Configuration>\r
-       </Configurations>\r
-       <References>\r
-       </References>\r
-       <Files>\r
-               <Filter\r
-                       Name="Source Files"\r
-                       Filter="cpp;c;cxx;def;odl;idl;hpj;bat;asm;asmx"\r
-                       UniqueIdentifier="{4FC737F1-C7A5-4376-A066-2A32D752A2FF}"\r
-                       >\r
-                       <File\r
-                               RelativePath=".\mod_softtimer.c"\r
-                               >\r
-                       </File>\r
-               </Filter>\r
-               <Filter\r
-                       Name="Header Files"\r
-                       Filter="h;hpp;hxx;hm;inl;inc;xsd"\r
-                       UniqueIdentifier="{93995380-89BD-4b04-88EB-625FBE52EBFB}"\r
-                       >\r
-               </Filter>\r
-               <Filter\r
-                       Name="Resource Files"\r
-                       Filter="rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx"\r
-                       UniqueIdentifier="{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}"\r
-                       >\r
-               </Filter>\r
-       </Files>\r
-       <Globals>\r
-       </Globals>\r
-</VisualStudioProject>\r
similarity index 85%
rename from src/mod/timers/mod_softtimer/mod_softtimer.c
rename to src/softtimer.c
index 0cb0b0bcd26f66be6a17ed043b1b07c54ff052b2..2a162ce789d4a3a2026507936dbe979af4e0c0ef 100644 (file)
  * Anthony Minessale II <anthmct@yahoo.com>
  *
  *
- * mod_softtimer.c -- Software Timer Module
+ * softtimer.c -- Software Timer Module
  *
  */
 #include <switch.h>
 #include <stdio.h>
+#include "private/switch_core_pvt.h"
 
 #ifndef UINT32_MAX
 #define UINT32_MAX 0xffffffff
@@ -38,7 +39,7 @@
 
 #define MAX_TICK UINT32_MAX - 1024
 
-
+struct switch_runtime runtime;
 static switch_memory_pool_t *module_pool = NULL;
 
 static struct {
@@ -47,10 +48,10 @@ static struct {
        switch_mutex_t *mutex;
 } globals;
 
-SWITCH_MODULE_LOAD_FUNCTION(mod_softtimer_load);
-SWITCH_MODULE_SHUTDOWN_FUNCTION(mod_softtimer_shutdown);
-SWITCH_MODULE_RUNTIME_FUNCTION(mod_softtimer_runtime);
-SWITCH_MODULE_DEFINITION(mod_softtimer, mod_softtimer_load, mod_softtimer_shutdown, mod_softtimer_runtime);
+SWITCH_MODULE_LOAD_FUNCTION(softtimer_load);
+SWITCH_MODULE_SHUTDOWN_FUNCTION(softtimer_shutdown);
+SWITCH_MODULE_RUNTIME_FUNCTION(softtimer_runtime);
+SWITCH_MODULE_DEFINITION(softtimer, softtimer_load, softtimer_shutdown, softtimer_runtime);
 
 #define MAX_ELEMENTS 1000
 
@@ -195,51 +196,43 @@ static switch_status_t timer_destroy(switch_timer_t *timer)
        return SWITCH_STATUS_SUCCESS;
 }
 
-SWITCH_MODULE_LOAD_FUNCTION(mod_softtimer_load)
-{
-       switch_timer_interface_t *timer_interface;
-       module_pool = pool;
-
-       /* connect my internal structure to the blank pointer passed to me */
-       *module_interface = switch_loadable_module_create_module_interface(pool, modname);
-       timer_interface = switch_loadable_module_create_interface(*module_interface, SWITCH_TIMER_INTERFACE);
-       timer_interface->interface_name = "soft";
-       timer_interface->timer_init = timer_init;
-       timer_interface->timer_next = timer_next;
-       timer_interface->timer_step = timer_step;
-       timer_interface->timer_check = timer_check;
-       timer_interface->timer_destroy = timer_destroy;
-
-       /* indicate that the module should continue to be loaded */
-       return SWITCH_STATUS_SUCCESS;
-}
-
-/* I cant resist setting this to 10ms, we dont even run anything smaller than 20ms so this is already 
-   twice the granularity we need, we'll change it if we need anything smaller
-*/
 
 #define STEP_MS 1
 #define STEP_MIC 1000
 
-SWITCH_MODULE_RUNTIME_FUNCTION(mod_softtimer_runtime)
+SWITCH_MODULE_RUNTIME_FUNCTION(softtimer_runtime)
 {
        switch_time_t reference = switch_time_now();
        uint32_t current_ms = 0;
-       uint32_t x;
-
+       uint32_t x, tick = 0;
+       switch_time_t ts = 0;
+       
        memset(&globals, 0, sizeof(globals));
        switch_mutex_init(&globals.mutex, SWITCH_MUTEX_NESTED, module_pool);
        
        globals.STARTED = globals.RUNNING = 1;
-       
+       switch_mutex_lock(runtime.throttle_mutex);
+       runtime.sps = runtime.sps_total;
+       switch_mutex_unlock(runtime.throttle_mutex);
+
        while (globals.RUNNING == 1) {
                reference += STEP_MIC;
-
-               while (switch_time_now() < reference) {
+               while ((ts = switch_time_now()) < reference) {
                        switch_yield(STEP_MIC);
                }
-
+               runtime.timestamp = ts;
                current_ms += STEP_MS;
+               tick += STEP_MS;
+
+               if (tick >= 1000) {
+                       if (runtime.sps <= 0) {
+                               switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "Over Session Rate of %d!\n", runtime.sps_total);
+                       }
+                       switch_mutex_lock(runtime.throttle_mutex);
+                       runtime.sps = runtime.sps_total;
+                       switch_mutex_unlock(runtime.throttle_mutex);
+                       tick = 0;
+               }
 
                for (x = 0; x < MAX_ELEMENTS; x++) {
                        int i = x, index;
@@ -270,7 +263,27 @@ SWITCH_MODULE_RUNTIME_FUNCTION(mod_softtimer_runtime)
        return SWITCH_STATUS_TERM;
 }
 
-SWITCH_MODULE_SHUTDOWN_FUNCTION(mod_softtimer_shutdown)
+
+SWITCH_MODULE_LOAD_FUNCTION(softtimer_load)
+{
+       switch_timer_interface_t *timer_interface;
+       module_pool = pool;
+
+       /* connect my internal structure to the blank pointer passed to me */
+       *module_interface = switch_loadable_module_create_module_interface(pool, modname);
+       timer_interface = switch_loadable_module_create_interface(*module_interface, SWITCH_TIMER_INTERFACE);
+       timer_interface->interface_name = "soft";
+       timer_interface->timer_init = timer_init;
+       timer_interface->timer_next = timer_next;
+       timer_interface->timer_step = timer_step;
+       timer_interface->timer_check = timer_check;
+       timer_interface->timer_destroy = timer_destroy;
+
+       /* indicate that the module should continue to be loaded */
+       return SWITCH_STATUS_SUCCESS;
+}
+
+SWITCH_MODULE_SHUTDOWN_FUNCTION(softtimer_shutdown)
 {
 
        if (globals.RUNNING) {
@@ -282,7 +295,7 @@ SWITCH_MODULE_SHUTDOWN_FUNCTION(mod_softtimer_shutdown)
                        switch_yield(10000);
                }
        }
-
+       switch_core_destroy_memory_pool(&module_pool);
        return SWITCH_STATUS_SUCCESS;
 }
 
index 63920f358be3ff6c3988653de20c078eefc3d95b..6b674e4927149d9a0ebce334656ce90f9a3b25ba 100644 (file)
@@ -855,7 +855,7 @@ SWITCH_DECLARE(void) switch_channel_set_caller_profile(switch_channel_t *channel
        caller_profile->times->created = switch_time_now();
 
        if (channel->caller_profile && channel->caller_profile->times) {
-               channel->caller_profile->times->transferred = switch_time_now();
+               channel->caller_profile->times->transferred = caller_profile->times->created;
                caller_profile->times->answered = channel->caller_profile->times->answered;
        }
 
index 57a567d938ff72dae79ab07b79de0655607b70f5..fce2e763ad0594779b4396eda5099a308cf2e855 100644 (file)
@@ -151,7 +151,7 @@ SWITCH_DECLARE(void) switch_console_printf(switch_text_channel_t channel, const
                        switch_size_t retsize;
                        switch_time_exp_t tm;
                        switch_event_t *event;
-                       switch_time_exp_lt(&tm, switch_time_now());
+                       switch_time_exp_lt(&tm, switch_timestamp_now());
                        switch_strftime(date, &retsize, sizeof(date), "%Y-%m-%d %T", &tm);
 
                        if (channel == SWITCH_CHANNEL_ID_LOG) {
index 3237f89c08f2bb7843ce0575bcb52158ba20bd60..9802ce2375540380af00a114ce60a5c93095abe6 100644 (file)
@@ -79,6 +79,10 @@ SWITCH_STANDARD_SCHED_FUNC(heartbeat_callback)
        task->runtime = time(NULL) + 20;
 }
 
+SWITCH_DECLARE(switch_time_t) switch_timestamp_now(void)
+{
+       return runtime.timestamp ? runtime.timestamp : switch_time_now();
+}
 
 SWITCH_DECLARE(switch_status_t) switch_core_set_console(const char *console)
 {
@@ -413,12 +417,12 @@ SWITCH_DECLARE(switch_status_t) switch_core_init(const char *console, switch_cor
                return SWITCH_STATUS_MEMERR;
        }
        assert(runtime.memory_pool != NULL);
-
+       switch_mutex_init(&runtime.throttle_mutex, SWITCH_MUTEX_NESTED, runtime.memory_pool);
        switch_core_set_globals();
        switch_core_session_init(runtime.memory_pool);
        switch_core_hash_init(&runtime.global_vars, runtime.memory_pool);
        runtime.flags = flags;
-
+       runtime.sps_total = 30;
 
        if (switch_xml_init(runtime.memory_pool, err) != SWITCH_STATUS_SUCCESS) {
                apr_terminate();
@@ -438,6 +442,8 @@ SWITCH_DECLARE(switch_status_t) switch_core_init(const char *console, switch_cor
                                        if (switch_true(val)) {
                                                switch_set_flag((&runtime), SCF_CRASH_PROT);
                                        }
+                               } else if (!strcasecmp(var, "sessions-per-second")) {
+                                       switch_core_sessions_per_second(atoi(val));
                                } else if (!strcasecmp(var, "max-sessions")) {
                                        switch_core_session_limit(atoi(val));
                                }
@@ -582,9 +588,10 @@ SWITCH_DECLARE(switch_status_t) switch_core_init_and_modload(const char *console
        }
 
        switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CONSOLE,
-                                         "\nFreeSWITCH Version %s Started.\nCrash Protection [%s]\nMax Sessions[%u]\nSQL [%s]\n", SWITCH_VERSION_FULL, 
+                                         "\nFreeSWITCH Version %s Started.\nCrash Protection [%s]\nMax Sessions[%u]\nSession Rate[%d]\nSQL [%s]\n", SWITCH_VERSION_FULL, 
                                          switch_test_flag((&runtime), SCF_CRASH_PROT) ? "Enabled" : "Disabled",
                                          switch_core_session_limit(0),
+                                         switch_core_sessions_per_second(0),
                                          switch_test_flag((&runtime), SCF_USE_SQL) ? "Enabled" : "Disabled"
                                          );
 
index c374b35a6690ca101cfadbf99a35109080822493..dc29a365f8f37216f3b36643b44626508c01cd26 100644 (file)
@@ -37,7 +37,6 @@
 static struct {
        switch_memory_pool_t *memory_pool;
        switch_hash_t *session_table;
-       switch_mutex_t *session_table_mutex;
        uint32_t session_count;
        uint32_t session_limit;
        switch_size_t session_id;
@@ -53,7 +52,7 @@ SWITCH_DECLARE(switch_core_session_t *) switch_core_session_locate(const char *u
        switch_core_session_t *session = NULL;
 
        if (uuid_str) {
-               switch_mutex_lock(session_manager.session_table_mutex);
+               switch_mutex_lock(runtime.throttle_mutex);
                if ((session = switch_core_hash_find(session_manager.session_table, uuid_str))) {
                        /* Acquire a read lock on the session */
 #ifdef SWITCH_DEBUG_RWLOCKS
@@ -65,7 +64,7 @@ SWITCH_DECLARE(switch_core_session_t *) switch_core_session_locate(const char *u
                                session = NULL;
                        }
                }
-               switch_mutex_unlock(session_manager.session_table_mutex);
+               switch_mutex_unlock(runtime.throttle_mutex);
        }
 
        /* if its not NULL, now it's up to you to rwunlock this */
@@ -80,7 +79,7 @@ SWITCH_DECLARE(void) switch_core_session_hupall(switch_call_cause_t cause)
        switch_channel_t *channel;
        uint32_t loops = 0;
 
-       switch_mutex_lock(session_manager.session_table_mutex);
+       switch_mutex_lock(runtime.throttle_mutex);
        for (hi = switch_hash_first(NULL, session_manager.session_table); hi; hi = switch_hash_next(hi)) {
                switch_hash_this(hi, NULL, NULL, &val);
                if (val) {
@@ -90,7 +89,7 @@ SWITCH_DECLARE(void) switch_core_session_hupall(switch_call_cause_t cause)
                        switch_core_session_kill_channel(session, SWITCH_SIG_KILL);
                }
        }
-       switch_mutex_unlock(session_manager.session_table_mutex);
+       switch_mutex_unlock(runtime.throttle_mutex);
 
        while (session_manager.session_count > 0) {
                switch_yield(100000);
@@ -107,7 +106,7 @@ SWITCH_DECLARE(switch_status_t) switch_core_session_message_send(char *uuid_str,
        switch_core_session_t *session = NULL;
        switch_status_t status = SWITCH_STATUS_FALSE;
 
-       switch_mutex_lock(session_manager.session_table_mutex);
+       switch_mutex_lock(runtime.throttle_mutex);
        if ((session = switch_core_hash_find(session_manager.session_table, uuid_str)) != 0) {
                /* Acquire a read lock on the session or forget it the channel is dead */
                if (switch_core_session_read_lock(session) == SWITCH_STATUS_SUCCESS) {
@@ -117,7 +116,7 @@ SWITCH_DECLARE(switch_status_t) switch_core_session_message_send(char *uuid_str,
                        switch_core_session_rwunlock(session);
                }
        }
-       switch_mutex_unlock(session_manager.session_table_mutex);
+       switch_mutex_unlock(runtime.throttle_mutex);
 
        return status;
 }
@@ -127,7 +126,7 @@ SWITCH_DECLARE(switch_status_t) switch_core_session_event_send(char *uuid_str, s
        switch_core_session_t *session = NULL;
        switch_status_t status = SWITCH_STATUS_FALSE;
 
-       switch_mutex_lock(session_manager.session_table_mutex);
+       switch_mutex_lock(runtime.throttle_mutex);
        if ((session = switch_core_hash_find(session_manager.session_table, uuid_str)) != 0) {
                /* Acquire a read lock on the session or forget it the channel is dead */
                if (switch_core_session_read_lock(session) == SWITCH_STATUS_SUCCESS) {
@@ -137,7 +136,7 @@ SWITCH_DECLARE(switch_status_t) switch_core_session_event_send(char *uuid_str, s
                        switch_core_session_rwunlock(session);
                }
        }
-       switch_mutex_unlock(session_manager.session_table_mutex);
+       switch_mutex_unlock(runtime.throttle_mutex);
 
        return status;
 }
@@ -645,12 +644,12 @@ SWITCH_DECLARE(void) switch_core_session_perform_destroy(switch_core_session_t *
        
        switch_scheduler_del_task_group((*session)->uuid_str);
 
-       switch_mutex_lock(session_manager.session_table_mutex);
+       switch_mutex_lock(runtime.throttle_mutex);
        switch_core_hash_delete(session_manager.session_table, (*session)->uuid_str);
        if (session_manager.session_count) {
                session_manager.session_count--;
        }
-       switch_mutex_unlock(session_manager.session_table_mutex);
+       switch_mutex_unlock(runtime.throttle_mutex);
 
        if (switch_event_create(&event, SWITCH_EVENT_CHANNEL_DESTROY) == SWITCH_STATUS_SUCCESS) {
                switch_channel_event_set_data((*session)->channel, event);
@@ -744,18 +743,25 @@ SWITCH_DECLARE(switch_core_session_t *) switch_core_session_request(const switch
        switch_core_session_t *session;
        switch_uuid_t uuid;
        uint32_t count = 0;
+       int32_t sps = 0;
 
        if (!switch_core_ready() || endpoint_interface == NULL) {
                switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "The system cannot create any sessions at this time.\n");
                return NULL;
        }
 
-       switch_mutex_lock(session_manager.session_table_mutex);
+       switch_mutex_lock(runtime.throttle_mutex);
        count = session_manager.session_count;
-       switch_mutex_unlock(session_manager.session_table_mutex);
+       sps = --runtime.sps;
+       switch_mutex_unlock(runtime.throttle_mutex);
+       
+       if (sps <= 0) {
+               //switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "Throttle Error!\n");
+               return NULL;
+       }
 
        if ((count + 1) > session_manager.session_limit) {
-               switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "Over Session Limit!\n");
+               //switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "Over Session Limit!\n");
                return NULL;
        }
 
@@ -779,6 +785,7 @@ SWITCH_DECLARE(switch_core_session_t *) switch_core_session_request(const switch
                return NULL;
        }
 
+
        switch_channel_init(session->channel, session, CS_NEW, 0);
 
        /* The session *IS* the pool you may not alter it because you have no idea how
@@ -810,11 +817,11 @@ SWITCH_DECLARE(switch_core_session_t *) switch_core_session_request(const switch
        switch_queue_create(&session->private_event_queue, SWITCH_EVENT_QUEUE_LEN, session->pool);
 
        snprintf(session->name, sizeof(session->name), "%"SWITCH_SIZE_T_FMT, session->id);
-       switch_mutex_lock(session_manager.session_table_mutex);
+       switch_mutex_lock(runtime.throttle_mutex);
        session->id = session_manager.session_id++;
        switch_core_hash_insert(session_manager.session_table, session->uuid_str, session);
        session_manager.session_count++;
-       switch_mutex_unlock(session_manager.session_table_mutex);
+       switch_mutex_unlock(runtime.throttle_mutex);
 
        return session;
 }
@@ -871,6 +878,15 @@ SWITCH_DECLARE(uint32_t) switch_core_session_limit(uint32_t new_limit)
        return session_manager.session_limit;
 }
 
+SWITCH_DECLARE(uint32_t) switch_core_sessions_per_second(uint32_t new_limit)
+{
+       if (new_limit) {
+               runtime.sps_total = new_limit;
+       }
+
+       return runtime.sps_total;
+}
+
 
 void switch_core_session_init(switch_memory_pool_t *pool)
 {
@@ -879,7 +895,6 @@ void switch_core_session_init(switch_memory_pool_t *pool)
        session_manager.session_id = 1;
        session_manager.memory_pool = pool;
        switch_core_hash_init(&session_manager.session_table, session_manager.memory_pool);
-       switch_mutex_init(&session_manager.session_table_mutex, SWITCH_MUTEX_NESTED, session_manager.memory_pool);
 }
 
 void switch_core_session_uninit(void)
index 78e6f2c6cdf7767e927b17ef7649e502f6da397e..9dd63821a4c3028293f28f8aef728b1fdea516b7 100644 (file)
@@ -870,7 +870,7 @@ SWITCH_DECLARE(switch_status_t) switch_event_fire_detailed(char *file, char *fun
        switch_time_exp_t tm;
        char date[80] = "";
        switch_size_t retsize;
-       switch_time_t ts = switch_time_now();
+       switch_time_t ts = switch_timestamp_now();
 
        assert(BLOCK != NULL);
        assert(RUNTIME_POOL != NULL);
index 9b6e2b4c6aa56e8187697cbbec0a284d5e450322..b158699095f84acce4865c29f42ef3a00dfd16e9 100644 (file)
@@ -655,14 +655,19 @@ static switch_status_t switch_loadable_module_load_file(char *path, char *filena
 
        assert(path != NULL);
 
+       switch_core_new_memory_pool(&pool);
        *new_module = NULL;
-       status = switch_dso_load(&dso, path, loadable_modules.pool);
 
-       if (switch_core_new_memory_pool(&pool) != SWITCH_STATUS_SUCCESS) {
-               switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "OH OH no pool\n");
-               abort();
+       struct_name = switch_core_sprintf(pool, "%s_module_interface", filename);
+
+       status = switch_dso_load(&dso, NULL, loadable_modules.pool);
+       status = switch_dso_sym(&interface_struct_handle, dso, struct_name);
+
+       if (!interface_struct_handle) {
+               status = switch_dso_load(&dso, path, loadable_modules.pool);
        }
 
+
        while (loading) {
                if (status != APR_SUCCESS) {
                        switch_dso_error(dso, derr, sizeof(derr));
@@ -670,8 +675,10 @@ static switch_status_t switch_loadable_module_load_file(char *path, char *filena
                        break;
                }
 
-               struct_name = switch_core_sprintf(pool, "%s_module_interface", filename);
-               status = switch_dso_sym(&interface_struct_handle, dso, struct_name);
+               if (!interface_struct_handle) {
+                       status = switch_dso_sym(&interface_struct_handle, dso, struct_name);
+               }
+
                if (interface_struct_handle) {
                        mod_interface_functions = interface_struct_handle;
                        load_func_ptr = mod_interface_functions->load;
@@ -822,7 +829,8 @@ SWITCH_DECLARE(switch_status_t) switch_loadable_module_unload_module(char *dir,
 SWITCH_DECLARE(switch_status_t) switch_loadable_module_build_dynamic(char *filename,
                                                                                                                                         switch_module_load_t switch_module_load,
                                                                                                                                         switch_module_runtime_t switch_module_runtime,
-                                                                                                                                        switch_module_shutdown_t switch_module_shutdown)
+                                                                                                                                        switch_module_shutdown_t switch_module_shutdown,
+                                                                                                                                        switch_bool_t runtime)
 {
        switch_loadable_module_t *module = NULL;
        switch_module_load_t load_func_ptr = NULL;
@@ -890,7 +898,7 @@ SWITCH_DECLARE(switch_status_t) switch_loadable_module_build_dynamic(char *filen
        if (switch_module_runtime) {
                module->switch_module_runtime = switch_module_runtime;
        }
-       if (module->switch_module_runtime) {
+       if (runtime && module->switch_module_runtime) {
                switch_core_launch_thread(switch_loadable_module_exec, module, module->pool);
        }
        switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CONSOLE, "Successfully Loaded [%s]\n", module_interface->module_name);
@@ -962,6 +970,8 @@ SWITCH_DECLARE(switch_status_t) switch_loadable_module_init()
        switch_core_hash_init(&loadable_modules.dialplan_hash, loadable_modules.pool);
        switch_mutex_init(&loadable_modules.mutex, SWITCH_MUTEX_NESTED, loadable_modules.pool);
 
+       switch_loadable_module_load_module("", "softtimer", SWITCH_FALSE, &err);
+
        if ((xml = switch_xml_open_cfg(cf, &cfg, NULL))) {
                switch_xml_t mods, ld;
                if ((mods = switch_xml_child(cfg, "modules"))) {
@@ -1036,7 +1046,7 @@ SWITCH_DECLARE(switch_status_t) switch_loadable_module_init()
                }
                apr_dir_close(module_dir_handle);
        }
-
+       
        switch_loadable_module_runtime();
 
        return SWITCH_STATUS_SUCCESS;
index 7bb714e31690f128a90e2a357a6d65cc04c3d25e..5e59c99e7d6696c2f628d861816ec526f4623edd 100644 (file)
@@ -162,7 +162,7 @@ SWITCH_DECLARE(void) switch_log_printf(switch_text_channel_t channel, const char
        const char *filep = (file ? switch_cut_path(file) : "");
        const char *funcp = (func ? func : "");
        char *content = NULL;
-       switch_time_t now = switch_time_now();
+       switch_time_t now = switch_timestamp_now();
        uint32_t len;
        const char *extra_fmt = "%s [%s] %s:%d %s()%c%s";
 
index 42aee0048bb6c03b651fc0e8634c50f97cdf5835..d0b164f346fb2a99f67ea6464bc16ba5fa15911c 100644 (file)
@@ -1,5 +1,6 @@
 #include <switch.h>
 
+
 struct switch_scheduler_task_container {
        switch_scheduler_task_t task;
        int64_t executed;
@@ -71,6 +72,7 @@ static int task_thread_loop(int done)
 
 
        switch_mutex_lock(globals.task_mutex);
+
        for (tp = globals.task_list; tp; tp = tp->next) {
                if (done) {
                        tp->destroyed = 1;
index e0933b1e0fc0ad2936e33366dbf731e5358b7d44..e698576787b4bd506774dea5555e0bc32153527e 100644 (file)
@@ -104,7 +104,7 @@ SWITCH_DECLARE(void) switch_stun_random_string(char *buf, uint16_t len, char *se
 
        max = (int) strlen(set);
 
-       srand((unsigned int) switch_time_now());
+       srand((unsigned int) switch_timestamp_now());
 
        for (x = 0; x < len; x++) {
                int j = (int) (max * 1.0 * rand() / (RAND_MAX + 1.0));