performance tweaks

git-svn-id: http://svn.freeswitch.org/svn/freeswitch/trunk@2693 d0543943-73ff-0310-b7d9-9358b9ac24b2
This commit is contained in:
Anthony Minessale
2006-09-14 00:15:03 +00:00
parent 0ff8b417e9
commit 8a0e9ccf1f
9 changed files with 541 additions and 30 deletions
@@ -1242,6 +1242,10 @@ static switch_status_t init_profile(struct mdl_profile *profile, uint8_t login)
profile->exten) {
ldl_handle_t *handle;
if (switch_test_flag(profile, TFLAG_TIMER) && !profile->timer_name) {
profile->timer_name = switch_core_strdup(module_pool, "soft");
}
if (login) {
if (ldl_handle_init(&handle,
profile->login,
+4 -5
View File
@@ -642,7 +642,6 @@ static switch_status_t sofia_on_transmit(switch_core_session_t *session)
static void deactivate_rtp(private_object_t *tech_pvt)
{
int loops = 0;//, sock = -1;
if (switch_rtp_ready(tech_pvt->rtp_session)) {
while (loops < 10 && (switch_test_flag(tech_pvt, TFLAG_READING) || switch_test_flag(tech_pvt, TFLAG_WRITING))) {
switch_yield(10000);
@@ -730,10 +729,6 @@ static switch_status_t activate_rtp(private_object_t *tech_pvt)
ms = tech_pvt->read_codec.implementation->microseconds_per_frame;
flags = (switch_rtp_flag_t) (SWITCH_RTP_FLAG_RAW_WRITE | SWITCH_RTP_FLAG_MINI);
if (switch_test_flag(tech_pvt, TFLAG_TIMER)) {
flags = (switch_rtp_flag_t) (flags | SWITCH_RTP_FLAG_USE_TIMER);
}
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "RTP [%s] %s:%d->%s:%d codec: %u ms: %d\n",
switch_channel_get_name(channel),
@@ -1846,6 +1841,10 @@ static switch_status_t config_sofia(int reload)
}
}
if (switch_test_flag(profile, TFLAG_TIMER) && !profile->timer_name) {
profile->timer_name = switch_core_strdup(profile->pool, "soft");
}
if (!profile->username) {
profile->username = switch_core_strdup(profile->pool, "FreeSWITCH");
}
@@ -0,0 +1,275 @@
/*
* FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
* Copyright (C) 2005/2006, Anthony Minessale II <anthmct@yahoo.com>
*
* Version: MPL 1.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
*
* The Initial Developer of the Original Code is
* Anthony Minessale II <anthmct@yahoo.com>
* Portions created by the Initial Developer are Copyright (C)
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
*
* Anthony Minessale II <anthmct@yahoo.com>
*
*
* mod_threadtimer.c -- Software Timer Module
*
*/
#include <switch.h>
#include <stdio.h>
static switch_memory_pool_t *module_pool = NULL;
static struct {
int32_t RUNNING;
switch_mutex_t *mutex;
} globals;
static const char modname[] = "mod_threadtimer";
#define MAX_ELEMENTS 1000
struct timer_private {
uint32_t tick;
uint32_t reference;
uint32_t interval;
switch_mutex_t *mutex;
struct timer_private *next;
};
typedef struct timer_private timer_private_t;
struct timer_head {
timer_private_t *private_info;
switch_mutex_t *mutex;
};
typedef struct timer_head timer_head_t;
static timer_head_t *TIMER_MATRIX[MAX_ELEMENTS+1];
static inline switch_status_t timer_init(switch_timer_t *timer)
{
timer_private_t *private_info;
timer_head_t *head;
if ((private_info = switch_core_alloc(timer->memory_pool, sizeof(*private_info)))) {
timer->private_info = private_info;
if (!TIMER_MATRIX[timer->interval]) {
if (!(TIMER_MATRIX[timer->interval] = switch_core_alloc(module_pool, sizeof(timer_head_t)))) {
return SWITCH_STATUS_MEMERR;
}
switch_mutex_init(&TIMER_MATRIX[timer->interval]->mutex, SWITCH_MUTEX_NESTED, module_pool);
}
head = TIMER_MATRIX[timer->interval];
switch_mutex_init(&private_info->mutex, SWITCH_MUTEX_NESTED, timer->memory_pool);
private_info->interval = timer->interval;
switch_mutex_lock(head->mutex);
private_info->next = head->private_info;
head->private_info = private_info;
switch_mutex_unlock(head->mutex);
return SWITCH_STATUS_SUCCESS;
}
return SWITCH_STATUS_MEMERR;
}
#define MAX_TICKS 0xFFFFFF00
#define check_overflow(p) if (p->reference > MAX_TICKS) {\
switch_mutex_lock(p->mutex);\
p->reference = p->tick = 0;\
switch_mutex_unlock(p->mutex);\
}\
static inline switch_status_t timer_step(switch_timer_t *timer)
{
timer_private_t *private_info = timer->private_info;
switch_mutex_lock(private_info->mutex);
private_info->reference += private_info->interval;
switch_mutex_unlock(private_info->mutex);
return SWITCH_STATUS_SUCCESS;
}
static inline switch_status_t timer_next(switch_timer_t *timer)
{
timer_private_t *private_info = timer->private_info;
timer_step(timer);
while (private_info->tick < private_info->reference) {
switch_yield(1000);
}
check_overflow(private_info);
timer->samplecount += timer->samples;
return SWITCH_STATUS_SUCCESS;
}
static inline switch_status_t timer_check(switch_timer_t *timer)
{
timer_private_t *private_info = timer->private_info;
switch_status_t status;
switch_mutex_lock(private_info->mutex);
if (private_info->tick < private_info->reference) {
status = SWITCH_STATUS_FALSE;
} else {
private_info->reference += private_info->interval;
check_overflow(private_info);
status = SWITCH_STATUS_SUCCESS;
}
switch_mutex_unlock(private_info->mutex);
return status;
}
static inline switch_status_t timer_destroy(switch_timer_t *timer)
{
timer_private_t *private_info = timer->private_info;
timer_head_t *head;
timer_private_t *ptr, *last = NULL;
head = TIMER_MATRIX[timer->interval];
assert(head != NULL);
assert(private_info != NULL);
switch_mutex_lock(head->mutex);
for(ptr = head->private_info; ptr; ptr = ptr->next) {
if (ptr == private_info) {
if (last) {
last->next = private_info->next;
} else {
head->private_info = private_info->next;
}
}
last = ptr;
}
switch_mutex_unlock(head->mutex);
timer->private_info = NULL;
return SWITCH_STATUS_SUCCESS;
}
static const switch_timer_interface_t timer_interface = {
/*.interface_name */ "thread_soft",
/*.timer_init */ timer_init,
/*.timer_next */ timer_next,
/*.timer_step */ timer_step,
/*.timer_check */ timer_check,
/*.timer_destroy */ timer_destroy
};
static const switch_loadable_module_interface_t mod_threadtimer_module_interface = {
/*.module_name */ modname,
/*.endpoint_interface */ NULL,
/*.timer_interface */ &timer_interface,
/*.switch_dialplan_interface */ NULL,
/*.switch_codec_interface */ NULL,
/*.switch_application_interface */ NULL
};
SWITCH_MOD_DECLARE(switch_status_t) switch_module_load(const switch_loadable_module_interface_t **module_interface, char *filename)
{
if (switch_core_new_memory_pool(&module_pool) != SWITCH_STATUS_SUCCESS) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "OH OH no pool\n");
return SWITCH_STATUS_MEMERR;
}
/* connect my internal structure to the blank pointer passed to me */
*module_interface = &mod_threadtimer_module_interface;
/* indicate that the module should continue to be loaded */
return SWITCH_STATUS_SUCCESS;
}
SWITCH_MOD_DECLARE(switch_status_t) switch_module_runtime(void)
{
switch_time_t reference = switch_time_now();
uint32_t current_ms = 0;
timer_private_t *ptr;
uint32_t x;
memset(&globals, 0, sizeof(globals));
switch_mutex_init(&globals.mutex, SWITCH_MUTEX_NESTED, module_pool);
globals.RUNNING = 1;
while(globals.RUNNING == 1) {
reference += 1000;
while (switch_time_now() < reference) {
switch_yield(500);
}
current_ms++;
for (x = 0; x < 1000; x++) {
int i = x, index;
if (i == 0) {
i = 1;
}
index = (current_ms % i == 0) ? i : 0;
if (TIMER_MATRIX[index] && TIMER_MATRIX[index]->private_info) {
switch_mutex_lock(TIMER_MATRIX[index]->mutex);
for (ptr = TIMER_MATRIX[index]->private_info; ptr; ptr = ptr->next) {
switch_mutex_lock(ptr->mutex);
ptr->tick += ptr->interval;
switch_mutex_unlock(ptr->mutex);
}
switch_mutex_unlock(TIMER_MATRIX[index]->mutex);
}
}
if (current_ms == MAX_ELEMENTS) {
current_ms = 0;
}
}
switch_mutex_lock(globals.mutex);
globals.RUNNING = 0;
switch_mutex_unlock(globals.mutex);
return SWITCH_STATUS_TERM;
}
SWITCH_MOD_DECLARE(switch_status_t) switch_module_shutdown(void)
{
if (globals.RUNNING) {
switch_mutex_lock(globals.mutex);
globals.RUNNING = -1;
switch_mutex_unlock(globals.mutex);
while (globals.RUNNING) {
switch_yield(10000);
}
}
return SWITCH_STATUS_SUCCESS;
}
@@ -0,0 +1,210 @@
<?xml version="1.0" encoding="Windows-1252"?>
<VisualStudioProject
ProjectType="Visual C++"
Version="8.00"
Name="mod_threadtimer"
ProjectGUID="{DCC13474-28DF-47CA-A8EB-72F8CE9A78C5}"
RootNamespace="mod_threadtimer"
Keyword="Win32Proj"
>
<Platforms>
<Platform
Name="Win32"
/>
</Platforms>
<ToolFiles>
</ToolFiles>
<Configurations>
<Configuration
Name="Debug|Win32"
OutputDirectory="$(ConfigurationName)"
IntermediateDirectory="$(ConfigurationName)"
ConfigurationType="2"
InheritedPropertySheets="$(VCInstallDir)VCProjectDefaults\UpgradeFromVC71.vsprops"
CharacterSet="2"
>
<Tool
Name="VCPreBuildEventTool"
/>
<Tool
Name="VCCustomBuildTool"
/>
<Tool
Name="VCXMLDataGeneratorTool"
/>
<Tool
Name="VCWebServiceProxyGeneratorTool"
/>
<Tool
Name="VCMIDLTool"
/>
<Tool
Name="VCCLCompilerTool"
Optimization="0"
AdditionalIncludeDirectories="&quot;$(InputDir)..\..\..\include&quot;;&quot;$(InputDir)include&quot;;&quot;$(InputDir)..\..\..\..\libs\include&quot;"
PreprocessorDefinitions="WIN32;_DEBUG;_WINDOWS;_USRDLL;MOD_EXPORTS"
MinimalRebuild="true"
BasicRuntimeChecks="3"
RuntimeLibrary="1"
UsePrecompiledHeader="0"
WarningLevel="4"
WarnAsError="true"
Detect64BitPortabilityProblems="true"
DebugInformationFormat="3"
/>
<Tool
Name="VCManagedResourceCompilerTool"
/>
<Tool
Name="VCResourceCompilerTool"
/>
<Tool
Name="VCPreLinkEventTool"
/>
<Tool
Name="VCLinkerTool"
OutputFile="$(SolutionDir)$(OutDir)/mod/$(InputName).dll"
LinkIncremental="1"
AdditionalLibraryDirectories="..\..\..\..\w32\vsnet\$(OutDir)"
GenerateDebugInformation="true"
ProgramDatabaseFile="$(OutDir)$(TargetName).pdb"
SubSystem="2"
ImportLibrary="$(OutDir)/mod_threadtimer.lib"
TargetMachine="1"
/>
<Tool
Name="VCALinkTool"
/>
<Tool
Name="VCManifestTool"
/>
<Tool
Name="VCXDCMakeTool"
/>
<Tool
Name="VCBscMakeTool"
/>
<Tool
Name="VCFxCopTool"
/>
<Tool
Name="VCAppVerifierTool"
/>
<Tool
Name="VCWebDeploymentTool"
/>
<Tool
Name="VCPostBuildEventTool"
/>
</Configuration>
<Configuration
Name="Release|Win32"
OutputDirectory="$(ConfigurationName)"
IntermediateDirectory="$(ConfigurationName)"
ConfigurationType="2"
InheritedPropertySheets="$(VCInstallDir)VCProjectDefaults\UpgradeFromVC71.vsprops"
CharacterSet="2"
>
<Tool
Name="VCPreBuildEventTool"
/>
<Tool
Name="VCCustomBuildTool"
/>
<Tool
Name="VCXMLDataGeneratorTool"
/>
<Tool
Name="VCWebServiceProxyGeneratorTool"
/>
<Tool
Name="VCMIDLTool"
/>
<Tool
Name="VCCLCompilerTool"
AdditionalIncludeDirectories="&quot;$(InputDir)..\..\..\include&quot;;&quot;$(InputDir)include&quot;;&quot;$(InputDir)..\..\..\..\libs\include&quot;"
PreprocessorDefinitions="WIN32;NDEBUG;_WINDOWS;_USRDLL;MOD_EXPORTS"
RuntimeLibrary="0"
UsePrecompiledHeader="0"
WarningLevel="4"
WarnAsError="true"
Detect64BitPortabilityProblems="true"
DebugInformationFormat="3"
/>
<Tool
Name="VCManagedResourceCompilerTool"
/>
<Tool
Name="VCResourceCompilerTool"
/>
<Tool
Name="VCPreLinkEventTool"
/>
<Tool
Name="VCLinkerTool"
OutputFile="$(SolutionDir)$(OutDir)/mod/$(InputName).dll"
LinkIncremental="1"
AdditionalLibraryDirectories="..\..\..\..\w32\vsnet\$(OutDir)"
GenerateDebugInformation="true"
ProgramDatabaseFile="$(OutDir)$(TargetName).pdb"
SubSystem="2"
OptimizeReferences="2"
EnableCOMDATFolding="2"
ImportLibrary="$(OutDir)/mod_threadtimer.lib"
TargetMachine="1"
/>
<Tool
Name="VCALinkTool"
/>
<Tool
Name="VCManifestTool"
/>
<Tool
Name="VCXDCMakeTool"
/>
<Tool
Name="VCBscMakeTool"
/>
<Tool
Name="VCFxCopTool"
/>
<Tool
Name="VCAppVerifierTool"
/>
<Tool
Name="VCWebDeploymentTool"
/>
<Tool
Name="VCPostBuildEventTool"
/>
</Configuration>
</Configurations>
<References>
</References>
<Files>
<Filter
Name="Source Files"
Filter="cpp;c;cxx;def;odl;idl;hpj;bat;asm;asmx"
UniqueIdentifier="{4FC737F1-C7A5-4376-A066-2A32D752A2FF}"
>
<File
RelativePath=".\mod_threadtimer.c"
>
</File>
</Filter>
<Filter
Name="Header Files"
Filter="h;hpp;hxx;hm;inl;inc;xsd"
UniqueIdentifier="{93995380-89BD-4b04-88EB-625FBE52EBFB}"
>
</Filter>
<Filter
Name="Resource Files"
Filter="rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx"
UniqueIdentifier="{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}"
>
</Filter>
</Files>
<Globals>
</Globals>
</VisualStudioProject>