2013-03-08 15:15:13 +00:00
|
|
|
/*
|
|
|
|
* Asterisk -- An open source telephony toolkit.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2013, Digium, Inc.
|
|
|
|
*
|
|
|
|
* David M. Lee, II <dlee@digium.com>
|
|
|
|
*
|
|
|
|
* See http://www.asterisk.org for more information about
|
|
|
|
* the Asterisk project. Please do not directly contact
|
|
|
|
* any of the maintainers of this project for assistance;
|
|
|
|
* the project provides a web site, mailing lists and IRC
|
|
|
|
* channels for your use.
|
|
|
|
*
|
|
|
|
* This program is free software, distributed under the terms of
|
|
|
|
* the GNU General Public License Version 2. See the LICENSE file
|
|
|
|
* at the top of the source tree.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*! \file
|
|
|
|
*
|
|
|
|
* \brief Stasis Message API.
|
|
|
|
*
|
|
|
|
* \author David M. Lee, II <dlee@digium.com>
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*** MODULEINFO
|
|
|
|
<support_level>core</support_level>
|
|
|
|
***/
|
|
|
|
|
|
|
|
#include "asterisk.h"
|
|
|
|
|
|
|
|
#include "asterisk/astobj2.h"
|
|
|
|
#include "asterisk/hashtab.h"
|
2013-06-17 03:00:38 +00:00
|
|
|
#include "asterisk/stasis_internal.h"
|
2013-03-08 15:15:13 +00:00
|
|
|
#include "asterisk/stasis.h"
|
|
|
|
#include "asterisk/utils.h"
|
2014-03-07 20:41:13 +00:00
|
|
|
#include "asterisk/vector.h"
|
2013-03-08 15:15:13 +00:00
|
|
|
|
|
|
|
#ifdef LOW_MEMORY
|
|
|
|
#define NUM_CACHE_BUCKETS 17
|
|
|
|
#else
|
|
|
|
#define NUM_CACHE_BUCKETS 563
|
|
|
|
#endif
|
|
|
|
|
2013-08-01 13:49:34 +00:00
|
|
|
/*! \internal */
|
|
|
|
struct stasis_cache {
|
|
|
|
struct ao2_container *entries;
|
|
|
|
snapshot_get_id id_fn;
|
2014-03-07 20:41:13 +00:00
|
|
|
cache_aggregate_calc_fn aggregate_calc_fn;
|
|
|
|
cache_aggregate_publish_fn aggregate_publish_fn;
|
2018-08-30 18:08:05 +00:00
|
|
|
int registered;
|
2013-08-01 13:49:34 +00:00
|
|
|
};
|
|
|
|
|
2013-03-15 17:35:16 +00:00
|
|
|
/*! \internal */
|
2013-03-08 15:15:13 +00:00
|
|
|
struct stasis_caching_topic {
|
2013-08-01 13:49:34 +00:00
|
|
|
struct stasis_cache *cache;
|
2013-03-08 15:15:13 +00:00
|
|
|
struct stasis_topic *topic;
|
2013-06-13 11:02:16 +00:00
|
|
|
struct stasis_topic *original_topic;
|
2013-03-08 15:15:13 +00:00
|
|
|
struct stasis_subscription *sub;
|
|
|
|
};
|
|
|
|
|
2014-03-01 00:02:02 +00:00
|
|
|
static void stasis_caching_topic_dtor(void *obj)
|
|
|
|
{
|
2013-03-08 15:15:13 +00:00
|
|
|
struct stasis_caching_topic *caching_topic = obj;
|
Stasis: address refcount races; implementation comments
Change r395954 reordered some stasis object destruction, which should
have been fine. Unfortunately, it caused some hard to reproduce issues
related to objects being accessed after they had been destroyed. The
patch in r396329 fixed the destruction order problem; this patch
addresses the underlying issue. A few other stasis-related fixes were
also added.
* Add ref-bumps around areas where objects may get transitively
destroyed. (For example, where we lock a topic, unref a subscription,
which unrefs the topic, which explodes the topic when we try to
unlock it.)
* Wrote an extensive doxygen page about Stasis implementation,
relationships between objects, lifecycles of objects, how the
refcounting works, etc. Many other comments were added, corrected, or
cleaned up.
* Added an assert to the topic dtor to catch extra ref decrements.
* Fixed type used after destruction errors for graceful shutdown in
stasis_channels.c.
* I added two unit tests in an attempt to catch destruction order
issues. Since the underlying cause is a race condition, though, the
tests rarely failed even when the code was wrong.
* Fixed a leak in stasis_cache_pattern.c.
(closes issue ASTERISK-22243)
Review: https://reviewboard.asterisk.org/r/2746/
git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@396842 65c4cc65-6c06-0410-ace0-fbb531ad65f3
2013-08-16 16:03:34 +00:00
|
|
|
|
|
|
|
/* Caching topics contain subscriptions, and must be manually
|
|
|
|
* unsubscribed. */
|
2013-03-08 15:15:13 +00:00
|
|
|
ast_assert(!stasis_subscription_is_subscribed(caching_topic->sub));
|
Stasis: address refcount races; implementation comments
Change r395954 reordered some stasis object destruction, which should
have been fine. Unfortunately, it caused some hard to reproduce issues
related to objects being accessed after they had been destroyed. The
patch in r396329 fixed the destruction order problem; this patch
addresses the underlying issue. A few other stasis-related fixes were
also added.
* Add ref-bumps around areas where objects may get transitively
destroyed. (For example, where we lock a topic, unref a subscription,
which unrefs the topic, which explodes the topic when we try to
unlock it.)
* Wrote an extensive doxygen page about Stasis implementation,
relationships between objects, lifecycles of objects, how the
refcounting works, etc. Many other comments were added, corrected, or
cleaned up.
* Added an assert to the topic dtor to catch extra ref decrements.
* Fixed type used after destruction errors for graceful shutdown in
stasis_channels.c.
* I added two unit tests in an attempt to catch destruction order
issues. Since the underlying cause is a race condition, though, the
tests rarely failed even when the code was wrong.
* Fixed a leak in stasis_cache_pattern.c.
(closes issue ASTERISK-22243)
Review: https://reviewboard.asterisk.org/r/2746/
git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@396842 65c4cc65-6c06-0410-ace0-fbb531ad65f3
2013-08-16 16:03:34 +00:00
|
|
|
/* If there are any messages in flight to this subscription; that would
|
|
|
|
* be bad. */
|
2013-05-17 21:10:32 +00:00
|
|
|
ast_assert(stasis_subscription_is_done(caching_topic->sub));
|
Stasis: address refcount races; implementation comments
Change r395954 reordered some stasis object destruction, which should
have been fine. Unfortunately, it caused some hard to reproduce issues
related to objects being accessed after they had been destroyed. The
patch in r396329 fixed the destruction order problem; this patch
addresses the underlying issue. A few other stasis-related fixes were
also added.
* Add ref-bumps around areas where objects may get transitively
destroyed. (For example, where we lock a topic, unref a subscription,
which unrefs the topic, which explodes the topic when we try to
unlock it.)
* Wrote an extensive doxygen page about Stasis implementation,
relationships between objects, lifecycles of objects, how the
refcounting works, etc. Many other comments were added, corrected, or
cleaned up.
* Added an assert to the topic dtor to catch extra ref decrements.
* Fixed type used after destruction errors for graceful shutdown in
stasis_channels.c.
* I added two unit tests in an attempt to catch destruction order
issues. Since the underlying cause is a race condition, though, the
tests rarely failed even when the code was wrong.
* Fixed a leak in stasis_cache_pattern.c.
(closes issue ASTERISK-22243)
Review: https://reviewboard.asterisk.org/r/2746/
git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@396842 65c4cc65-6c06-0410-ace0-fbb531ad65f3
2013-08-16 16:03:34 +00:00
|
|
|
|
2018-08-30 18:08:05 +00:00
|
|
|
ao2_container_unregister(stasis_topic_name(caching_topic->topic));
|
|
|
|
|
2013-05-17 21:10:32 +00:00
|
|
|
ao2_cleanup(caching_topic->sub);
|
2013-03-08 15:15:13 +00:00
|
|
|
caching_topic->sub = NULL;
|
|
|
|
ao2_cleanup(caching_topic->cache);
|
|
|
|
caching_topic->cache = NULL;
|
|
|
|
ao2_cleanup(caching_topic->topic);
|
|
|
|
caching_topic->topic = NULL;
|
2013-06-13 11:02:16 +00:00
|
|
|
ao2_cleanup(caching_topic->original_topic);
|
|
|
|
caching_topic->original_topic = NULL;
|
2013-03-08 15:15:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
struct stasis_topic *stasis_caching_get_topic(struct stasis_caching_topic *caching_topic)
|
|
|
|
{
|
|
|
|
return caching_topic->topic;
|
|
|
|
}
|
|
|
|
|
2013-03-15 12:58:23 +00:00
|
|
|
struct stasis_caching_topic *stasis_caching_unsubscribe(struct stasis_caching_topic *caching_topic)
|
2013-03-08 15:15:13 +00:00
|
|
|
{
|
2014-03-01 00:02:02 +00:00
|
|
|
if (!caching_topic) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2013-07-18 14:08:21 +00:00
|
|
|
|
2014-03-01 00:02:02 +00:00
|
|
|
/*
|
|
|
|
* The subscription may hold the last reference to this caching
|
|
|
|
* topic, but we want to make sure the unsubscribe finishes
|
|
|
|
* before kicking of the caching topic's dtor.
|
|
|
|
*/
|
|
|
|
ao2_ref(caching_topic, +1);
|
|
|
|
|
|
|
|
if (stasis_subscription_is_subscribed(caching_topic->sub)) {
|
|
|
|
/*
|
|
|
|
* Increment the reference to hold on to it past the
|
|
|
|
* unsubscribe. Will be cleaned up in dtor.
|
2013-07-18 14:08:21 +00:00
|
|
|
*/
|
2014-03-01 00:02:02 +00:00
|
|
|
ao2_ref(caching_topic->sub, +1);
|
|
|
|
stasis_unsubscribe(caching_topic->sub);
|
|
|
|
} else {
|
|
|
|
ast_log(LOG_ERROR, "stasis_caching_topic unsubscribed multiple times\n");
|
2013-03-08 15:15:13 +00:00
|
|
|
}
|
2014-03-01 00:02:02 +00:00
|
|
|
ao2_cleanup(caching_topic);
|
2013-03-15 12:58:23 +00:00
|
|
|
return NULL;
|
2013-03-08 15:15:13 +00:00
|
|
|
}
|
|
|
|
|
2013-05-17 21:10:32 +00:00
|
|
|
struct stasis_caching_topic *stasis_caching_unsubscribe_and_join(struct stasis_caching_topic *caching_topic)
|
|
|
|
{
|
|
|
|
if (!caching_topic) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Hold a ref past the unsubscribe */
|
|
|
|
ao2_ref(caching_topic, +1);
|
|
|
|
stasis_caching_unsubscribe(caching_topic);
|
|
|
|
stasis_subscription_join(caching_topic->sub);
|
|
|
|
ao2_cleanup(caching_topic);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
stasis: Reduce creation of channel snapshots to improve performance
During some performance testing of Asterisk with AGI, ARI, and lots of Local
channels, we noticed that there's quite a hit in performance during channel
creation and releasing to the dialplan (ARI continue). After investigating
the performance spike that occurs during channel creation, we discovered
that we create a lot of channel snapshots that are technically unnecessary.
This includes creating snapshots during:
* AGI execution
* Returning objects for ARI commands
* During some Local channel operations
* During some dialling operations
* During variable setting
* During some bridging operations
And more.
This patch does the following:
- It removes a number of fields from channel snapshots. These fields were
rarely used, were expensive to have on the snapshot, and hurt performance.
This included formats, translation paths, Log Call ID, callgroup, pickup
group, and all channel variables. As a result, AMI Status,
"core show channel", "core show channelvar", and "pjsip show channel" were
modified to either hit the live channel or not show certain pieces of data.
While this is unfortunate, the performance gain from this patch is worth
the loss in behaviour.
- It adds a mechanism to publish a cached snapshot + blob. A large number of
publications were changed to use this, including:
- During Dial begin
- During Variable assignment (if no AMI variables are emitted - if AMI
variables are set, we have to make snapshots when a variable is changed)
- During channel pickup
- When a channel is put on hold/unhold
- When a DTMF digit is begun/ended
- When creating a bridge snapshot
- When an AOC event is raised
- During Local channel optimization/Local bridging
- When endpoint snapshots are generated
- All AGI events
- All ARI responses that return a channel
- Events in the AgentPool, MeetMe, and some in Queue
- Additionally, some extraneous channel snapshots were being made that were
unnecessary. These were removed.
- The result of ast_hashtab_hash_string is now cached in stasis_cache. This
reduces a large number of calls to ast_hashtab_hash_string, which reduced
the amount of time spent in this function in gprof by around 50%.
#ASTERISK-23811 #close
Reported by: Matt Jordan
Review: https://reviewboard.asterisk.org/r/3568/
........
Merged revisions 416211 from http://svn.asterisk.org/svn/asterisk/branches/12
git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@416216 65c4cc65-6c06-0410-ace0-fbb531ad65f3
2014-06-13 18:24:49 +00:00
|
|
|
/*!
|
|
|
|
* \brief The key for an entry in the cache
|
|
|
|
* \note The items in this struct must be immutable for the item in the cache
|
|
|
|
*/
|
2014-03-07 20:41:13 +00:00
|
|
|
struct cache_entry_key {
|
stasis: Reduce creation of channel snapshots to improve performance
During some performance testing of Asterisk with AGI, ARI, and lots of Local
channels, we noticed that there's quite a hit in performance during channel
creation and releasing to the dialplan (ARI continue). After investigating
the performance spike that occurs during channel creation, we discovered
that we create a lot of channel snapshots that are technically unnecessary.
This includes creating snapshots during:
* AGI execution
* Returning objects for ARI commands
* During some Local channel operations
* During some dialling operations
* During variable setting
* During some bridging operations
And more.
This patch does the following:
- It removes a number of fields from channel snapshots. These fields were
rarely used, were expensive to have on the snapshot, and hurt performance.
This included formats, translation paths, Log Call ID, callgroup, pickup
group, and all channel variables. As a result, AMI Status,
"core show channel", "core show channelvar", and "pjsip show channel" were
modified to either hit the live channel or not show certain pieces of data.
While this is unfortunate, the performance gain from this patch is worth
the loss in behaviour.
- It adds a mechanism to publish a cached snapshot + blob. A large number of
publications were changed to use this, including:
- During Dial begin
- During Variable assignment (if no AMI variables are emitted - if AMI
variables are set, we have to make snapshots when a variable is changed)
- During channel pickup
- When a channel is put on hold/unhold
- When a DTMF digit is begun/ended
- When creating a bridge snapshot
- When an AOC event is raised
- During Local channel optimization/Local bridging
- When endpoint snapshots are generated
- All AGI events
- All ARI responses that return a channel
- Events in the AgentPool, MeetMe, and some in Queue
- Additionally, some extraneous channel snapshots were being made that were
unnecessary. These were removed.
- The result of ast_hashtab_hash_string is now cached in stasis_cache. This
reduces a large number of calls to ast_hashtab_hash_string, which reduced
the amount of time spent in this function in gprof by around 50%.
#ASTERISK-23811 #close
Reported by: Matt Jordan
Review: https://reviewboard.asterisk.org/r/3568/
........
Merged revisions 416211 from http://svn.asterisk.org/svn/asterisk/branches/12
git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@416216 65c4cc65-6c06-0410-ace0-fbb531ad65f3
2014-06-13 18:24:49 +00:00
|
|
|
/*! The message type of the item stored in the cache */
|
2013-03-08 15:15:13 +00:00
|
|
|
struct stasis_message_type *type;
|
stasis: Reduce creation of channel snapshots to improve performance
During some performance testing of Asterisk with AGI, ARI, and lots of Local
channels, we noticed that there's quite a hit in performance during channel
creation and releasing to the dialplan (ARI continue). After investigating
the performance spike that occurs during channel creation, we discovered
that we create a lot of channel snapshots that are technically unnecessary.
This includes creating snapshots during:
* AGI execution
* Returning objects for ARI commands
* During some Local channel operations
* During some dialling operations
* During variable setting
* During some bridging operations
And more.
This patch does the following:
- It removes a number of fields from channel snapshots. These fields were
rarely used, were expensive to have on the snapshot, and hurt performance.
This included formats, translation paths, Log Call ID, callgroup, pickup
group, and all channel variables. As a result, AMI Status,
"core show channel", "core show channelvar", and "pjsip show channel" were
modified to either hit the live channel or not show certain pieces of data.
While this is unfortunate, the performance gain from this patch is worth
the loss in behaviour.
- It adds a mechanism to publish a cached snapshot + blob. A large number of
publications were changed to use this, including:
- During Dial begin
- During Variable assignment (if no AMI variables are emitted - if AMI
variables are set, we have to make snapshots when a variable is changed)
- During channel pickup
- When a channel is put on hold/unhold
- When a DTMF digit is begun/ended
- When creating a bridge snapshot
- When an AOC event is raised
- During Local channel optimization/Local bridging
- When endpoint snapshots are generated
- All AGI events
- All ARI responses that return a channel
- Events in the AgentPool, MeetMe, and some in Queue
- Additionally, some extraneous channel snapshots were being made that were
unnecessary. These were removed.
- The result of ast_hashtab_hash_string is now cached in stasis_cache. This
reduces a large number of calls to ast_hashtab_hash_string, which reduced
the amount of time spent in this function in gprof by around 50%.
#ASTERISK-23811 #close
Reported by: Matt Jordan
Review: https://reviewboard.asterisk.org/r/3568/
........
Merged revisions 416211 from http://svn.asterisk.org/svn/asterisk/branches/12
git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@416216 65c4cc65-6c06-0410-ace0-fbb531ad65f3
2014-06-13 18:24:49 +00:00
|
|
|
/*! The unique ID of the item stored in the cache */
|
2014-03-07 20:41:13 +00:00
|
|
|
const char *id;
|
stasis: Reduce creation of channel snapshots to improve performance
During some performance testing of Asterisk with AGI, ARI, and lots of Local
channels, we noticed that there's quite a hit in performance during channel
creation and releasing to the dialplan (ARI continue). After investigating
the performance spike that occurs during channel creation, we discovered
that we create a lot of channel snapshots that are technically unnecessary.
This includes creating snapshots during:
* AGI execution
* Returning objects for ARI commands
* During some Local channel operations
* During some dialling operations
* During variable setting
* During some bridging operations
And more.
This patch does the following:
- It removes a number of fields from channel snapshots. These fields were
rarely used, were expensive to have on the snapshot, and hurt performance.
This included formats, translation paths, Log Call ID, callgroup, pickup
group, and all channel variables. As a result, AMI Status,
"core show channel", "core show channelvar", and "pjsip show channel" were
modified to either hit the live channel or not show certain pieces of data.
While this is unfortunate, the performance gain from this patch is worth
the loss in behaviour.
- It adds a mechanism to publish a cached snapshot + blob. A large number of
publications were changed to use this, including:
- During Dial begin
- During Variable assignment (if no AMI variables are emitted - if AMI
variables are set, we have to make snapshots when a variable is changed)
- During channel pickup
- When a channel is put on hold/unhold
- When a DTMF digit is begun/ended
- When creating a bridge snapshot
- When an AOC event is raised
- During Local channel optimization/Local bridging
- When endpoint snapshots are generated
- All AGI events
- All ARI responses that return a channel
- Events in the AgentPool, MeetMe, and some in Queue
- Additionally, some extraneous channel snapshots were being made that were
unnecessary. These were removed.
- The result of ast_hashtab_hash_string is now cached in stasis_cache. This
reduces a large number of calls to ast_hashtab_hash_string, which reduced
the amount of time spent in this function in gprof by around 50%.
#ASTERISK-23811 #close
Reported by: Matt Jordan
Review: https://reviewboard.asterisk.org/r/3568/
........
Merged revisions 416211 from http://svn.asterisk.org/svn/asterisk/branches/12
git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@416216 65c4cc65-6c06-0410-ace0-fbb531ad65f3
2014-06-13 18:24:49 +00:00
|
|
|
/*! The hash, computed from \c type and \c id */
|
|
|
|
unsigned int hash;
|
2014-03-07 20:41:13 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct stasis_cache_entry {
|
|
|
|
struct cache_entry_key key;
|
|
|
|
/*! Aggregate snapshot of the stasis cache. */
|
|
|
|
struct stasis_message *aggregate;
|
|
|
|
/*! Local entity snapshot of the stasis event. */
|
|
|
|
struct stasis_message *local;
|
|
|
|
/*! Remote entity snapshots of the stasis event. */
|
|
|
|
AST_VECTOR(, struct stasis_message *) remote;
|
2013-03-08 15:15:13 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static void cache_entry_dtor(void *obj)
|
|
|
|
{
|
2014-03-07 20:41:13 +00:00
|
|
|
struct stasis_cache_entry *entry = obj;
|
|
|
|
size_t idx;
|
|
|
|
|
|
|
|
entry->key.type = NULL;
|
|
|
|
ast_free((char *) entry->key.id);
|
|
|
|
entry->key.id = NULL;
|
|
|
|
|
|
|
|
ao2_cleanup(entry->aggregate);
|
|
|
|
entry->aggregate = NULL;
|
|
|
|
ao2_cleanup(entry->local);
|
|
|
|
entry->local = NULL;
|
|
|
|
|
|
|
|
for (idx = 0; idx < AST_VECTOR_SIZE(&entry->remote); ++idx) {
|
|
|
|
struct stasis_message *remote;
|
|
|
|
|
|
|
|
remote = AST_VECTOR_GET(&entry->remote, idx);
|
|
|
|
ao2_cleanup(remote);
|
|
|
|
}
|
|
|
|
AST_VECTOR_FREE(&entry->remote);
|
2013-03-08 15:15:13 +00:00
|
|
|
}
|
|
|
|
|
stasis: Reduce creation of channel snapshots to improve performance
During some performance testing of Asterisk with AGI, ARI, and lots of Local
channels, we noticed that there's quite a hit in performance during channel
creation and releasing to the dialplan (ARI continue). After investigating
the performance spike that occurs during channel creation, we discovered
that we create a lot of channel snapshots that are technically unnecessary.
This includes creating snapshots during:
* AGI execution
* Returning objects for ARI commands
* During some Local channel operations
* During some dialling operations
* During variable setting
* During some bridging operations
And more.
This patch does the following:
- It removes a number of fields from channel snapshots. These fields were
rarely used, were expensive to have on the snapshot, and hurt performance.
This included formats, translation paths, Log Call ID, callgroup, pickup
group, and all channel variables. As a result, AMI Status,
"core show channel", "core show channelvar", and "pjsip show channel" were
modified to either hit the live channel or not show certain pieces of data.
While this is unfortunate, the performance gain from this patch is worth
the loss in behaviour.
- It adds a mechanism to publish a cached snapshot + blob. A large number of
publications were changed to use this, including:
- During Dial begin
- During Variable assignment (if no AMI variables are emitted - if AMI
variables are set, we have to make snapshots when a variable is changed)
- During channel pickup
- When a channel is put on hold/unhold
- When a DTMF digit is begun/ended
- When creating a bridge snapshot
- When an AOC event is raised
- During Local channel optimization/Local bridging
- When endpoint snapshots are generated
- All AGI events
- All ARI responses that return a channel
- Events in the AgentPool, MeetMe, and some in Queue
- Additionally, some extraneous channel snapshots were being made that were
unnecessary. These were removed.
- The result of ast_hashtab_hash_string is now cached in stasis_cache. This
reduces a large number of calls to ast_hashtab_hash_string, which reduced
the amount of time spent in this function in gprof by around 50%.
#ASTERISK-23811 #close
Reported by: Matt Jordan
Review: https://reviewboard.asterisk.org/r/3568/
........
Merged revisions 416211 from http://svn.asterisk.org/svn/asterisk/branches/12
git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@416216 65c4cc65-6c06-0410-ace0-fbb531ad65f3
2014-06-13 18:24:49 +00:00
|
|
|
static void cache_entry_compute_hash(struct cache_entry_key *key)
|
|
|
|
{
|
2018-08-06 11:36:22 +00:00
|
|
|
key->hash = stasis_message_type_hash(key->type);
|
stasis: Reduce creation of channel snapshots to improve performance
During some performance testing of Asterisk with AGI, ARI, and lots of Local
channels, we noticed that there's quite a hit in performance during channel
creation and releasing to the dialplan (ARI continue). After investigating
the performance spike that occurs during channel creation, we discovered
that we create a lot of channel snapshots that are technically unnecessary.
This includes creating snapshots during:
* AGI execution
* Returning objects for ARI commands
* During some Local channel operations
* During some dialling operations
* During variable setting
* During some bridging operations
And more.
This patch does the following:
- It removes a number of fields from channel snapshots. These fields were
rarely used, were expensive to have on the snapshot, and hurt performance.
This included formats, translation paths, Log Call ID, callgroup, pickup
group, and all channel variables. As a result, AMI Status,
"core show channel", "core show channelvar", and "pjsip show channel" were
modified to either hit the live channel or not show certain pieces of data.
While this is unfortunate, the performance gain from this patch is worth
the loss in behaviour.
- It adds a mechanism to publish a cached snapshot + blob. A large number of
publications were changed to use this, including:
- During Dial begin
- During Variable assignment (if no AMI variables are emitted - if AMI
variables are set, we have to make snapshots when a variable is changed)
- During channel pickup
- When a channel is put on hold/unhold
- When a DTMF digit is begun/ended
- When creating a bridge snapshot
- When an AOC event is raised
- During Local channel optimization/Local bridging
- When endpoint snapshots are generated
- All AGI events
- All ARI responses that return a channel
- Events in the AgentPool, MeetMe, and some in Queue
- Additionally, some extraneous channel snapshots were being made that were
unnecessary. These were removed.
- The result of ast_hashtab_hash_string is now cached in stasis_cache. This
reduces a large number of calls to ast_hashtab_hash_string, which reduced
the amount of time spent in this function in gprof by around 50%.
#ASTERISK-23811 #close
Reported by: Matt Jordan
Review: https://reviewboard.asterisk.org/r/3568/
........
Merged revisions 416211 from http://svn.asterisk.org/svn/asterisk/branches/12
git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@416216 65c4cc65-6c06-0410-ace0-fbb531ad65f3
2014-06-13 18:24:49 +00:00
|
|
|
key->hash += ast_hashtab_hash_string(key->id);
|
|
|
|
}
|
|
|
|
|
2014-03-07 20:41:13 +00:00
|
|
|
static struct stasis_cache_entry *cache_entry_create(struct stasis_message_type *type, const char *id, struct stasis_message *snapshot)
|
2013-03-08 15:15:13 +00:00
|
|
|
{
|
2014-03-07 20:41:13 +00:00
|
|
|
struct stasis_cache_entry *entry;
|
|
|
|
int is_remote;
|
2013-03-08 15:15:13 +00:00
|
|
|
|
|
|
|
ast_assert(id != NULL);
|
2014-03-07 20:41:13 +00:00
|
|
|
ast_assert(snapshot != NULL);
|
2013-03-08 15:15:13 +00:00
|
|
|
|
2014-08-06 12:55:28 +00:00
|
|
|
if (!type) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2013-08-01 13:49:34 +00:00
|
|
|
entry = ao2_alloc_options(sizeof(*entry), cache_entry_dtor,
|
|
|
|
AO2_ALLOC_OPT_LOCK_NOLOCK);
|
2013-03-08 15:15:13 +00:00
|
|
|
if (!entry) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2014-03-07 20:41:13 +00:00
|
|
|
entry->key.id = ast_strdup(id);
|
|
|
|
if (!entry->key.id) {
|
|
|
|
ao2_cleanup(entry);
|
|
|
|
return NULL;
|
|
|
|
}
|
2018-09-14 20:51:41 +00:00
|
|
|
/*
|
|
|
|
* Normal ao2 ref counting rules says we should increment the message
|
|
|
|
* type ref here and decrement it in cache_entry_dtor(). However, the
|
|
|
|
* stasis message snapshot is cached here, will always have the same type
|
|
|
|
* as the cache entry, and can legitimately cause the type ref count to
|
|
|
|
* hit the excessive ref count assertion. Since the cache entry will
|
|
|
|
* always have a snapshot we can get away with not holding a ref here.
|
|
|
|
*/
|
|
|
|
ast_assert(type == stasis_message_type(snapshot));
|
|
|
|
entry->key.type = type;
|
stasis: Reduce creation of channel snapshots to improve performance
During some performance testing of Asterisk with AGI, ARI, and lots of Local
channels, we noticed that there's quite a hit in performance during channel
creation and releasing to the dialplan (ARI continue). After investigating
the performance spike that occurs during channel creation, we discovered
that we create a lot of channel snapshots that are technically unnecessary.
This includes creating snapshots during:
* AGI execution
* Returning objects for ARI commands
* During some Local channel operations
* During some dialling operations
* During variable setting
* During some bridging operations
And more.
This patch does the following:
- It removes a number of fields from channel snapshots. These fields were
rarely used, were expensive to have on the snapshot, and hurt performance.
This included formats, translation paths, Log Call ID, callgroup, pickup
group, and all channel variables. As a result, AMI Status,
"core show channel", "core show channelvar", and "pjsip show channel" were
modified to either hit the live channel or not show certain pieces of data.
While this is unfortunate, the performance gain from this patch is worth
the loss in behaviour.
- It adds a mechanism to publish a cached snapshot + blob. A large number of
publications were changed to use this, including:
- During Dial begin
- During Variable assignment (if no AMI variables are emitted - if AMI
variables are set, we have to make snapshots when a variable is changed)
- During channel pickup
- When a channel is put on hold/unhold
- When a DTMF digit is begun/ended
- When creating a bridge snapshot
- When an AOC event is raised
- During Local channel optimization/Local bridging
- When endpoint snapshots are generated
- All AGI events
- All ARI responses that return a channel
- Events in the AgentPool, MeetMe, and some in Queue
- Additionally, some extraneous channel snapshots were being made that were
unnecessary. These were removed.
- The result of ast_hashtab_hash_string is now cached in stasis_cache. This
reduces a large number of calls to ast_hashtab_hash_string, which reduced
the amount of time spent in this function in gprof by around 50%.
#ASTERISK-23811 #close
Reported by: Matt Jordan
Review: https://reviewboard.asterisk.org/r/3568/
........
Merged revisions 416211 from http://svn.asterisk.org/svn/asterisk/branches/12
git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@416216 65c4cc65-6c06-0410-ace0-fbb531ad65f3
2014-06-13 18:24:49 +00:00
|
|
|
cache_entry_compute_hash(&entry->key);
|
2014-03-07 20:41:13 +00:00
|
|
|
|
|
|
|
is_remote = ast_eid_cmp(&ast_eid_default, stasis_message_eid(snapshot)) ? 1 : 0;
|
|
|
|
if (AST_VECTOR_INIT(&entry->remote, is_remote)) {
|
|
|
|
ao2_cleanup(entry);
|
2013-03-08 15:15:13 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2014-03-07 20:41:13 +00:00
|
|
|
if (is_remote) {
|
|
|
|
if (AST_VECTOR_APPEND(&entry->remote, snapshot)) {
|
|
|
|
ao2_cleanup(entry);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
entry->local = snapshot;
|
2013-03-08 15:15:13 +00:00
|
|
|
}
|
2014-03-07 20:41:13 +00:00
|
|
|
ao2_bump(snapshot);
|
2013-03-08 15:15:13 +00:00
|
|
|
|
|
|
|
return entry;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int cache_entry_hash(const void *obj, int flags)
|
|
|
|
{
|
2014-03-07 20:41:13 +00:00
|
|
|
const struct stasis_cache_entry *object;
|
|
|
|
const struct cache_entry_key *key;
|
2013-03-08 15:15:13 +00:00
|
|
|
|
2014-03-07 20:41:13 +00:00
|
|
|
switch (flags & OBJ_SEARCH_MASK) {
|
|
|
|
case OBJ_SEARCH_KEY:
|
|
|
|
key = obj;
|
|
|
|
break;
|
|
|
|
case OBJ_SEARCH_OBJECT:
|
|
|
|
object = obj;
|
|
|
|
key = &object->key;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
/* Hash can only work on something with a full key. */
|
|
|
|
ast_assert(0);
|
|
|
|
return 0;
|
|
|
|
}
|
2013-03-08 15:15:13 +00:00
|
|
|
|
stasis: Reduce creation of channel snapshots to improve performance
During some performance testing of Asterisk with AGI, ARI, and lots of Local
channels, we noticed that there's quite a hit in performance during channel
creation and releasing to the dialplan (ARI continue). After investigating
the performance spike that occurs during channel creation, we discovered
that we create a lot of channel snapshots that are technically unnecessary.
This includes creating snapshots during:
* AGI execution
* Returning objects for ARI commands
* During some Local channel operations
* During some dialling operations
* During variable setting
* During some bridging operations
And more.
This patch does the following:
- It removes a number of fields from channel snapshots. These fields were
rarely used, were expensive to have on the snapshot, and hurt performance.
This included formats, translation paths, Log Call ID, callgroup, pickup
group, and all channel variables. As a result, AMI Status,
"core show channel", "core show channelvar", and "pjsip show channel" were
modified to either hit the live channel or not show certain pieces of data.
While this is unfortunate, the performance gain from this patch is worth
the loss in behaviour.
- It adds a mechanism to publish a cached snapshot + blob. A large number of
publications were changed to use this, including:
- During Dial begin
- During Variable assignment (if no AMI variables are emitted - if AMI
variables are set, we have to make snapshots when a variable is changed)
- During channel pickup
- When a channel is put on hold/unhold
- When a DTMF digit is begun/ended
- When creating a bridge snapshot
- When an AOC event is raised
- During Local channel optimization/Local bridging
- When endpoint snapshots are generated
- All AGI events
- All ARI responses that return a channel
- Events in the AgentPool, MeetMe, and some in Queue
- Additionally, some extraneous channel snapshots were being made that were
unnecessary. These were removed.
- The result of ast_hashtab_hash_string is now cached in stasis_cache. This
reduces a large number of calls to ast_hashtab_hash_string, which reduced
the amount of time spent in this function in gprof by around 50%.
#ASTERISK-23811 #close
Reported by: Matt Jordan
Review: https://reviewboard.asterisk.org/r/3568/
........
Merged revisions 416211 from http://svn.asterisk.org/svn/asterisk/branches/12
git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@416216 65c4cc65-6c06-0410-ace0-fbb531ad65f3
2014-06-13 18:24:49 +00:00
|
|
|
return (int)key->hash;
|
2013-03-08 15:15:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int cache_entry_cmp(void *obj, void *arg, int flags)
|
|
|
|
{
|
2014-03-07 20:41:13 +00:00
|
|
|
const struct stasis_cache_entry *object_left = obj;
|
|
|
|
const struct stasis_cache_entry *object_right = arg;
|
2014-03-18 02:09:25 +00:00
|
|
|
const struct cache_entry_key *right_key = arg;
|
2014-03-07 20:41:13 +00:00
|
|
|
int cmp;
|
|
|
|
|
|
|
|
switch (flags & OBJ_SEARCH_MASK) {
|
|
|
|
case OBJ_SEARCH_OBJECT:
|
|
|
|
right_key = &object_right->key;
|
|
|
|
/* Fall through */
|
|
|
|
case OBJ_SEARCH_KEY:
|
|
|
|
cmp = object_left->key.type != right_key->type
|
|
|
|
|| strcmp(object_left->key.id, right_key->id);
|
|
|
|
break;
|
|
|
|
case OBJ_SEARCH_PARTIAL_KEY:
|
|
|
|
/* Not supported by container */
|
|
|
|
ast_assert(0);
|
|
|
|
cmp = -1;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
/*
|
|
|
|
* What arg points to is specific to this traversal callback
|
|
|
|
* and has no special meaning to astobj2.
|
|
|
|
*/
|
|
|
|
cmp = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (cmp) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* At this point the traversal callback is identical to a sorted
|
|
|
|
* container.
|
|
|
|
*/
|
|
|
|
return CMP_MATCH;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void cache_dtor(void *obj)
|
|
|
|
{
|
|
|
|
struct stasis_cache *cache = obj;
|
2013-03-08 15:15:13 +00:00
|
|
|
|
2014-03-07 20:41:13 +00:00
|
|
|
ao2_cleanup(cache->entries);
|
|
|
|
cache->entries = NULL;
|
|
|
|
}
|
2013-03-08 15:15:13 +00:00
|
|
|
|
2014-03-07 20:41:13 +00:00
|
|
|
struct stasis_cache *stasis_cache_create_full(snapshot_get_id id_fn,
|
|
|
|
cache_aggregate_calc_fn aggregate_calc_fn,
|
|
|
|
cache_aggregate_publish_fn aggregate_publish_fn)
|
|
|
|
{
|
|
|
|
struct stasis_cache *cache;
|
|
|
|
|
|
|
|
cache = ao2_alloc_options(sizeof(*cache), cache_dtor,
|
|
|
|
AO2_ALLOC_OPT_LOCK_NOLOCK);
|
|
|
|
if (!cache) {
|
|
|
|
return NULL;
|
2013-03-08 15:15:13 +00:00
|
|
|
}
|
|
|
|
|
2014-03-07 20:41:13 +00:00
|
|
|
cache->entries = ao2_container_alloc_hash(AO2_ALLOC_OPT_LOCK_RWLOCK, 0,
|
|
|
|
NUM_CACHE_BUCKETS, cache_entry_hash, NULL, cache_entry_cmp);
|
|
|
|
if (!cache->entries) {
|
|
|
|
ao2_cleanup(cache);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
cache->id_fn = id_fn;
|
|
|
|
cache->aggregate_calc_fn = aggregate_calc_fn;
|
|
|
|
cache->aggregate_publish_fn = aggregate_publish_fn;
|
|
|
|
|
|
|
|
return cache;
|
2013-03-08 15:15:13 +00:00
|
|
|
}
|
|
|
|
|
2014-03-07 20:41:13 +00:00
|
|
|
struct stasis_cache *stasis_cache_create(snapshot_get_id id_fn)
|
2013-08-01 13:49:34 +00:00
|
|
|
{
|
2014-03-07 20:41:13 +00:00
|
|
|
return stasis_cache_create_full(id_fn, NULL, NULL);
|
|
|
|
}
|
2013-08-01 13:49:34 +00:00
|
|
|
|
2014-03-07 20:41:13 +00:00
|
|
|
struct stasis_message *stasis_cache_entry_get_aggregate(struct stasis_cache_entry *entry)
|
|
|
|
{
|
|
|
|
return entry->aggregate;
|
2013-08-01 13:49:34 +00:00
|
|
|
}
|
|
|
|
|
2014-03-07 20:41:13 +00:00
|
|
|
struct stasis_message *stasis_cache_entry_get_local(struct stasis_cache_entry *entry)
|
2013-08-01 13:49:34 +00:00
|
|
|
{
|
2014-03-07 20:41:13 +00:00
|
|
|
return entry->local;
|
|
|
|
}
|
2013-08-01 13:49:34 +00:00
|
|
|
|
2014-03-07 20:41:13 +00:00
|
|
|
struct stasis_message *stasis_cache_entry_get_remote(struct stasis_cache_entry *entry, int idx)
|
|
|
|
{
|
|
|
|
if (idx < AST_VECTOR_SIZE(&entry->remote)) {
|
|
|
|
return AST_VECTOR_GET(&entry->remote, idx);
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
2013-08-01 13:49:34 +00:00
|
|
|
|
2014-03-07 20:41:13 +00:00
|
|
|
/*!
|
|
|
|
* \internal
|
|
|
|
* \brief Find the cache entry in the cache entries container.
|
|
|
|
*
|
|
|
|
* \param entries Container of cached entries.
|
|
|
|
* \param type Type of message to retrieve the cache entry.
|
|
|
|
* \param id Identity of the snapshot to retrieve the cache entry.
|
|
|
|
*
|
|
|
|
* \note The entries container is already locked.
|
|
|
|
*
|
|
|
|
* \retval Cache-entry on success.
|
|
|
|
* \retval NULL Not in cache.
|
|
|
|
*/
|
|
|
|
static struct stasis_cache_entry *cache_find(struct ao2_container *entries, struct stasis_message_type *type, const char *id)
|
|
|
|
{
|
|
|
|
struct cache_entry_key search_key;
|
2014-04-04 17:57:46 +00:00
|
|
|
struct stasis_cache_entry *entry;
|
2014-03-07 20:41:13 +00:00
|
|
|
|
|
|
|
search_key.type = type;
|
|
|
|
search_key.id = id;
|
stasis: Reduce creation of channel snapshots to improve performance
During some performance testing of Asterisk with AGI, ARI, and lots of Local
channels, we noticed that there's quite a hit in performance during channel
creation and releasing to the dialplan (ARI continue). After investigating
the performance spike that occurs during channel creation, we discovered
that we create a lot of channel snapshots that are technically unnecessary.
This includes creating snapshots during:
* AGI execution
* Returning objects for ARI commands
* During some Local channel operations
* During some dialling operations
* During variable setting
* During some bridging operations
And more.
This patch does the following:
- It removes a number of fields from channel snapshots. These fields were
rarely used, were expensive to have on the snapshot, and hurt performance.
This included formats, translation paths, Log Call ID, callgroup, pickup
group, and all channel variables. As a result, AMI Status,
"core show channel", "core show channelvar", and "pjsip show channel" were
modified to either hit the live channel or not show certain pieces of data.
While this is unfortunate, the performance gain from this patch is worth
the loss in behaviour.
- It adds a mechanism to publish a cached snapshot + blob. A large number of
publications were changed to use this, including:
- During Dial begin
- During Variable assignment (if no AMI variables are emitted - if AMI
variables are set, we have to make snapshots when a variable is changed)
- During channel pickup
- When a channel is put on hold/unhold
- When a DTMF digit is begun/ended
- When creating a bridge snapshot
- When an AOC event is raised
- During Local channel optimization/Local bridging
- When endpoint snapshots are generated
- All AGI events
- All ARI responses that return a channel
- Events in the AgentPool, MeetMe, and some in Queue
- Additionally, some extraneous channel snapshots were being made that were
unnecessary. These were removed.
- The result of ast_hashtab_hash_string is now cached in stasis_cache. This
reduces a large number of calls to ast_hashtab_hash_string, which reduced
the amount of time spent in this function in gprof by around 50%.
#ASTERISK-23811 #close
Reported by: Matt Jordan
Review: https://reviewboard.asterisk.org/r/3568/
........
Merged revisions 416211 from http://svn.asterisk.org/svn/asterisk/branches/12
git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@416216 65c4cc65-6c06-0410-ace0-fbb531ad65f3
2014-06-13 18:24:49 +00:00
|
|
|
cache_entry_compute_hash(&search_key);
|
2014-04-04 17:57:46 +00:00
|
|
|
entry = ao2_find(entries, &search_key, OBJ_SEARCH_KEY | OBJ_NOLOCK);
|
|
|
|
|
|
|
|
/* Ensure that what we looked for is what we found. */
|
|
|
|
ast_assert(!entry
|
|
|
|
|| (!strcmp(stasis_message_type_name(entry->key.type),
|
|
|
|
stasis_message_type_name(type)) && !strcmp(entry->key.id, id)));
|
|
|
|
return entry;
|
2014-03-07 20:41:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* \internal
|
|
|
|
* \brief Remove the stasis snapshot in the cache entry determined by eid.
|
|
|
|
*
|
|
|
|
* \param entries Container of cached entries.
|
|
|
|
* \param cached_entry The entry to remove the snapshot from.
|
|
|
|
* \param eid Which snapshot in the cached entry.
|
|
|
|
*
|
|
|
|
* \note The entries container is already locked.
|
|
|
|
*
|
|
|
|
* \return Previous stasis entry snapshot.
|
|
|
|
*/
|
|
|
|
static struct stasis_message *cache_remove(struct ao2_container *entries, struct stasis_cache_entry *cached_entry, const struct ast_eid *eid)
|
|
|
|
{
|
|
|
|
struct stasis_message *old_snapshot;
|
|
|
|
int is_remote;
|
|
|
|
|
|
|
|
is_remote = ast_eid_cmp(eid, &ast_eid_default);
|
|
|
|
if (!is_remote) {
|
|
|
|
old_snapshot = cached_entry->local;
|
|
|
|
cached_entry->local = NULL;
|
|
|
|
} else {
|
|
|
|
int idx;
|
|
|
|
|
|
|
|
old_snapshot = NULL;
|
|
|
|
for (idx = 0; idx < AST_VECTOR_SIZE(&cached_entry->remote); ++idx) {
|
|
|
|
struct stasis_message *cur;
|
|
|
|
|
|
|
|
cur = AST_VECTOR_GET(&cached_entry->remote, idx);
|
|
|
|
if (!ast_eid_cmp(eid, stasis_message_eid(cur))) {
|
|
|
|
old_snapshot = AST_VECTOR_REMOVE_UNORDERED(&cached_entry->remote, idx);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!cached_entry->local && !AST_VECTOR_SIZE(&cached_entry->remote)) {
|
|
|
|
ao2_unlink_flags(entries, cached_entry, OBJ_NOLOCK);
|
|
|
|
}
|
|
|
|
|
|
|
|
return old_snapshot;
|
|
|
|
}
|
2013-08-01 13:49:34 +00:00
|
|
|
|
2014-03-07 20:41:13 +00:00
|
|
|
/*!
|
|
|
|
* \internal
|
|
|
|
* \brief Update the stasis snapshot in the cache entry determined by eid.
|
|
|
|
*
|
|
|
|
* \param cached_entry The entry to remove the snapshot from.
|
|
|
|
* \param eid Which snapshot in the cached entry.
|
|
|
|
* \param new_snapshot Snapshot to replace the old snapshot.
|
|
|
|
*
|
|
|
|
* \return Previous stasis entry snapshot.
|
|
|
|
*/
|
|
|
|
static struct stasis_message *cache_udpate(struct stasis_cache_entry *cached_entry, const struct ast_eid *eid, struct stasis_message *new_snapshot)
|
|
|
|
{
|
|
|
|
struct stasis_message *old_snapshot;
|
|
|
|
int is_remote;
|
|
|
|
int idx;
|
|
|
|
|
|
|
|
is_remote = ast_eid_cmp(eid, &ast_eid_default);
|
|
|
|
if (!is_remote) {
|
|
|
|
old_snapshot = cached_entry->local;
|
|
|
|
cached_entry->local = ao2_bump(new_snapshot);
|
|
|
|
return old_snapshot;
|
|
|
|
}
|
|
|
|
|
|
|
|
old_snapshot = NULL;
|
|
|
|
for (idx = 0; idx < AST_VECTOR_SIZE(&cached_entry->remote); ++idx) {
|
|
|
|
struct stasis_message *cur;
|
2013-08-01 13:49:34 +00:00
|
|
|
|
2014-03-07 20:41:13 +00:00
|
|
|
cur = AST_VECTOR_GET(&cached_entry->remote, idx);
|
|
|
|
if (!ast_eid_cmp(eid, stasis_message_eid(cur))) {
|
|
|
|
old_snapshot = AST_VECTOR_REMOVE_UNORDERED(&cached_entry->remote, idx);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!AST_VECTOR_APPEND(&cached_entry->remote, new_snapshot)) {
|
|
|
|
ao2_bump(new_snapshot);
|
|
|
|
}
|
|
|
|
|
|
|
|
return old_snapshot;
|
2013-08-01 13:49:34 +00:00
|
|
|
}
|
|
|
|
|
2014-03-07 20:41:13 +00:00
|
|
|
struct cache_put_snapshots {
|
|
|
|
/*! Old cache eid snapshot. */
|
|
|
|
struct stasis_message *old;
|
|
|
|
/*! Old cache aggregate snapshot. */
|
|
|
|
struct stasis_message *aggregate_old;
|
|
|
|
/*! New cache aggregate snapshot. */
|
|
|
|
struct stasis_message *aggregate_new;
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct cache_put_snapshots cache_put(struct stasis_cache *cache,
|
|
|
|
struct stasis_message_type *type, const char *id, const struct ast_eid *eid,
|
2013-08-01 13:49:34 +00:00
|
|
|
struct stasis_message *new_snapshot)
|
2013-03-08 15:15:13 +00:00
|
|
|
{
|
2014-03-07 20:41:13 +00:00
|
|
|
struct stasis_cache_entry *cached_entry;
|
|
|
|
struct cache_put_snapshots snapshots;
|
2013-03-08 15:15:13 +00:00
|
|
|
|
2013-08-01 13:49:34 +00:00
|
|
|
ast_assert(cache->entries != NULL);
|
2014-03-07 20:41:13 +00:00
|
|
|
ast_assert(eid != NULL);/* Aggregate snapshots not allowed to be put directly. */
|
2013-08-01 13:49:34 +00:00
|
|
|
ast_assert(new_snapshot == NULL ||
|
|
|
|
type == stasis_message_type(new_snapshot));
|
2013-03-08 15:15:13 +00:00
|
|
|
|
2014-03-07 20:41:13 +00:00
|
|
|
memset(&snapshots, 0, sizeof(snapshots));
|
|
|
|
|
|
|
|
ao2_wrlock(cache->entries);
|
|
|
|
|
|
|
|
cached_entry = cache_find(cache->entries, type, id);
|
2013-03-08 15:15:13 +00:00
|
|
|
|
2014-03-07 20:41:13 +00:00
|
|
|
/* Update the eid snapshot. */
|
|
|
|
if (!new_snapshot) {
|
|
|
|
/* Remove snapshot from cache */
|
2013-03-08 15:15:13 +00:00
|
|
|
if (cached_entry) {
|
2014-03-07 20:41:13 +00:00
|
|
|
snapshots.old = cache_remove(cache->entries, cached_entry, eid);
|
2013-03-08 15:15:13 +00:00
|
|
|
}
|
2014-03-07 20:41:13 +00:00
|
|
|
} else if (cached_entry) {
|
|
|
|
/* Update snapshot in cache */
|
|
|
|
snapshots.old = cache_udpate(cached_entry, eid, new_snapshot);
|
2013-03-08 15:15:13 +00:00
|
|
|
} else {
|
2014-03-07 20:41:13 +00:00
|
|
|
/* Insert into the cache */
|
|
|
|
cached_entry = cache_entry_create(type, id, new_snapshot);
|
2013-03-08 15:15:13 +00:00
|
|
|
if (cached_entry) {
|
2014-03-07 20:41:13 +00:00
|
|
|
ao2_link_flags(cache->entries, cached_entry, OBJ_NOLOCK);
|
2013-03-08 15:15:13 +00:00
|
|
|
}
|
2014-03-07 20:41:13 +00:00
|
|
|
}
|
2013-03-08 15:15:13 +00:00
|
|
|
|
2014-03-07 20:41:13 +00:00
|
|
|
/* Update the aggregate snapshot. */
|
|
|
|
if (cache->aggregate_calc_fn && cached_entry) {
|
|
|
|
snapshots.aggregate_new = cache->aggregate_calc_fn(cached_entry, new_snapshot);
|
|
|
|
snapshots.aggregate_old = cached_entry->aggregate;
|
|
|
|
cached_entry->aggregate = ao2_bump(snapshots.aggregate_new);
|
2013-03-08 15:15:13 +00:00
|
|
|
}
|
|
|
|
|
2014-03-07 20:41:13 +00:00
|
|
|
ao2_unlock(cache->entries);
|
|
|
|
|
|
|
|
ao2_cleanup(cached_entry);
|
|
|
|
return snapshots;
|
2013-03-08 15:15:13 +00:00
|
|
|
}
|
|
|
|
|
2014-03-07 20:41:13 +00:00
|
|
|
/*!
|
|
|
|
* \internal
|
|
|
|
* \brief Dump all entity snapshots in the cache entry into the given container.
|
|
|
|
*
|
|
|
|
* \param snapshots Container to put all snapshots in the cache entry.
|
|
|
|
* \param entry Cache entry to use.
|
|
|
|
*
|
|
|
|
* \retval 0 on success.
|
|
|
|
* \retval non-zero on error.
|
|
|
|
*/
|
|
|
|
static int cache_entry_dump(struct ao2_container *snapshots, const struct stasis_cache_entry *entry)
|
|
|
|
{
|
|
|
|
int idx;
|
|
|
|
int err = 0;
|
|
|
|
|
|
|
|
ast_assert(snapshots != NULL);
|
|
|
|
ast_assert(entry != NULL);
|
|
|
|
|
|
|
|
/* The aggregate snapshot is not a snapshot from an entity. */
|
|
|
|
|
|
|
|
if (entry->local) {
|
|
|
|
err |= !ao2_link(snapshots, entry->local);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (idx = 0; !err && idx < AST_VECTOR_SIZE(&entry->remote); ++idx) {
|
|
|
|
struct stasis_message *snapshot;
|
|
|
|
|
|
|
|
snapshot = AST_VECTOR_GET(&entry->remote, idx);
|
|
|
|
err |= !ao2_link(snapshots, snapshot);
|
|
|
|
}
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct ao2_container *stasis_cache_get_all(struct stasis_cache *cache, struct stasis_message_type *type, const char *id)
|
2013-03-08 15:15:13 +00:00
|
|
|
{
|
2014-03-07 20:41:13 +00:00
|
|
|
struct stasis_cache_entry *cached_entry;
|
|
|
|
struct ao2_container *found;
|
2013-03-08 15:15:13 +00:00
|
|
|
|
2014-03-07 20:41:13 +00:00
|
|
|
ast_assert(cache != NULL);
|
2013-08-01 13:49:34 +00:00
|
|
|
ast_assert(cache->entries != NULL);
|
2014-03-07 20:41:13 +00:00
|
|
|
ast_assert(id != NULL);
|
2013-06-13 11:02:16 +00:00
|
|
|
|
2014-08-06 12:55:28 +00:00
|
|
|
if (!type) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2014-03-07 20:41:13 +00:00
|
|
|
found = ao2_container_alloc_list(AO2_ALLOC_OPT_LOCK_NOLOCK, 0, NULL, NULL);
|
|
|
|
if (!found) {
|
2013-03-08 15:15:13 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2014-03-07 20:41:13 +00:00
|
|
|
ao2_rdlock(cache->entries);
|
|
|
|
|
|
|
|
cached_entry = cache_find(cache->entries, type, id);
|
|
|
|
if (cached_entry && cache_entry_dump(found, cached_entry)) {
|
|
|
|
ao2_cleanup(found);
|
|
|
|
found = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
ao2_unlock(cache->entries);
|
|
|
|
|
|
|
|
ao2_cleanup(cached_entry);
|
|
|
|
return found;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* \internal
|
|
|
|
* \brief Retrieve an item from the cache entry for a specific eid.
|
|
|
|
*
|
|
|
|
* \param entry Cache entry to use.
|
|
|
|
* \param eid Specific entity id to retrieve. NULL for aggregate.
|
|
|
|
*
|
|
|
|
* \note The returned snapshot has not had its reference bumped.
|
|
|
|
*
|
|
|
|
* \retval Snapshot from the cache.
|
|
|
|
* \retval \c NULL if snapshot is not found.
|
|
|
|
*/
|
|
|
|
static struct stasis_message *cache_entry_by_eid(const struct stasis_cache_entry *entry, const struct ast_eid *eid)
|
|
|
|
{
|
|
|
|
int is_remote;
|
|
|
|
int idx;
|
|
|
|
|
|
|
|
if (!eid) {
|
|
|
|
/* Get aggregate. */
|
|
|
|
return entry->aggregate;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get snapshot with specific eid. */
|
|
|
|
is_remote = ast_eid_cmp(eid, &ast_eid_default);
|
|
|
|
if (!is_remote) {
|
|
|
|
return entry->local;
|
2013-03-08 15:15:13 +00:00
|
|
|
}
|
|
|
|
|
2014-03-07 20:41:13 +00:00
|
|
|
for (idx = 0; idx < AST_VECTOR_SIZE(&entry->remote); ++idx) {
|
|
|
|
struct stasis_message *cur;
|
|
|
|
|
|
|
|
cur = AST_VECTOR_GET(&entry->remote, idx);
|
|
|
|
if (!ast_eid_cmp(eid, stasis_message_eid(cur))) {
|
|
|
|
return cur;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct stasis_message *stasis_cache_get_by_eid(struct stasis_cache *cache, struct stasis_message_type *type, const char *id, const struct ast_eid *eid)
|
|
|
|
{
|
|
|
|
struct stasis_cache_entry *cached_entry;
|
|
|
|
struct stasis_message *snapshot = NULL;
|
|
|
|
|
|
|
|
ast_assert(cache != NULL);
|
|
|
|
ast_assert(cache->entries != NULL);
|
|
|
|
ast_assert(id != NULL);
|
|
|
|
|
2014-08-06 12:55:28 +00:00
|
|
|
if (!type) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2014-03-07 20:41:13 +00:00
|
|
|
ao2_rdlock(cache->entries);
|
|
|
|
|
|
|
|
cached_entry = cache_find(cache->entries, type, id);
|
|
|
|
if (cached_entry) {
|
|
|
|
snapshot = cache_entry_by_eid(cached_entry, eid);
|
|
|
|
ao2_bump(snapshot);
|
|
|
|
}
|
|
|
|
|
|
|
|
ao2_unlock(cache->entries);
|
|
|
|
|
|
|
|
ao2_cleanup(cached_entry);
|
|
|
|
return snapshot;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct stasis_message *stasis_cache_get(struct stasis_cache *cache, struct stasis_message_type *type, const char *id)
|
|
|
|
{
|
|
|
|
return stasis_cache_get_by_eid(cache, type, id, &ast_eid_default);
|
2013-03-08 15:15:13 +00:00
|
|
|
}
|
|
|
|
|
2013-03-08 16:00:14 +00:00
|
|
|
struct cache_dump_data {
|
2014-03-07 20:41:13 +00:00
|
|
|
struct ao2_container *container;
|
2013-03-08 16:00:14 +00:00
|
|
|
struct stasis_message_type *type;
|
2014-03-07 20:41:13 +00:00
|
|
|
const struct ast_eid *eid;
|
2013-03-08 16:00:14 +00:00
|
|
|
};
|
|
|
|
|
2014-03-07 20:41:13 +00:00
|
|
|
static int cache_dump_by_eid_cb(void *obj, void *arg, int flags)
|
2013-03-08 16:00:14 +00:00
|
|
|
{
|
|
|
|
struct cache_dump_data *cache_dump = arg;
|
2014-03-07 20:41:13 +00:00
|
|
|
struct stasis_cache_entry *entry = obj;
|
|
|
|
|
|
|
|
if (!cache_dump->type || entry->key.type == cache_dump->type) {
|
|
|
|
struct stasis_message *snapshot;
|
2013-03-08 16:00:14 +00:00
|
|
|
|
2014-03-07 20:41:13 +00:00
|
|
|
snapshot = cache_entry_by_eid(entry, cache_dump->eid);
|
|
|
|
if (snapshot) {
|
|
|
|
if (!ao2_link(cache_dump->container, snapshot)) {
|
|
|
|
ao2_cleanup(cache_dump->container);
|
|
|
|
cache_dump->container = NULL;
|
|
|
|
return CMP_STOP;
|
|
|
|
}
|
|
|
|
}
|
2013-03-08 16:00:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-03-07 20:41:13 +00:00
|
|
|
struct ao2_container *stasis_cache_dump_by_eid(struct stasis_cache *cache, struct stasis_message_type *type, const struct ast_eid *eid)
|
|
|
|
{
|
|
|
|
struct cache_dump_data cache_dump;
|
|
|
|
|
|
|
|
ast_assert(cache != NULL);
|
|
|
|
ast_assert(cache->entries != NULL);
|
|
|
|
|
|
|
|
cache_dump.eid = eid;
|
|
|
|
cache_dump.type = type;
|
|
|
|
cache_dump.container = ao2_container_alloc_list(AO2_ALLOC_OPT_LOCK_NOLOCK, 0, NULL, NULL);
|
|
|
|
if (!cache_dump.container) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
ao2_callback(cache->entries, OBJ_MULTIPLE | OBJ_NODATA, cache_dump_by_eid_cb, &cache_dump);
|
|
|
|
return cache_dump.container;
|
|
|
|
}
|
|
|
|
|
2013-08-01 13:49:34 +00:00
|
|
|
struct ao2_container *stasis_cache_dump(struct stasis_cache *cache, struct stasis_message_type *type)
|
2014-03-07 20:41:13 +00:00
|
|
|
{
|
|
|
|
return stasis_cache_dump_by_eid(cache, type, &ast_eid_default);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int cache_dump_all_cb(void *obj, void *arg, int flags)
|
|
|
|
{
|
|
|
|
struct cache_dump_data *cache_dump = arg;
|
|
|
|
struct stasis_cache_entry *entry = obj;
|
|
|
|
|
|
|
|
if (!cache_dump->type || entry->key.type == cache_dump->type) {
|
|
|
|
if (cache_entry_dump(cache_dump->container, entry)) {
|
|
|
|
ao2_cleanup(cache_dump->container);
|
|
|
|
cache_dump->container = NULL;
|
|
|
|
return CMP_STOP;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct ao2_container *stasis_cache_dump_all(struct stasis_cache *cache, struct stasis_message_type *type)
|
2013-03-08 16:00:14 +00:00
|
|
|
{
|
|
|
|
struct cache_dump_data cache_dump;
|
|
|
|
|
2014-03-07 20:41:13 +00:00
|
|
|
ast_assert(cache != NULL);
|
2013-08-01 13:49:34 +00:00
|
|
|
ast_assert(cache->entries != NULL);
|
2013-03-08 16:00:14 +00:00
|
|
|
|
2014-03-07 20:41:13 +00:00
|
|
|
cache_dump.eid = NULL;
|
2013-03-08 16:00:14 +00:00
|
|
|
cache_dump.type = type;
|
2014-03-07 20:41:13 +00:00
|
|
|
cache_dump.container = ao2_container_alloc_list(AO2_ALLOC_OPT_LOCK_NOLOCK, 0, NULL, NULL);
|
|
|
|
if (!cache_dump.container) {
|
2013-03-08 16:00:14 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2014-03-07 20:41:13 +00:00
|
|
|
ao2_callback(cache->entries, OBJ_MULTIPLE | OBJ_NODATA, cache_dump_all_cb, &cache_dump);
|
|
|
|
return cache_dump.container;
|
2013-03-08 16:00:14 +00:00
|
|
|
}
|
|
|
|
|
2013-05-15 02:37:22 +00:00
|
|
|
STASIS_MESSAGE_TYPE_DEFN(stasis_cache_clear_type);
|
|
|
|
STASIS_MESSAGE_TYPE_DEFN(stasis_cache_update_type);
|
2013-03-08 15:15:13 +00:00
|
|
|
|
2013-06-07 12:56:56 +00:00
|
|
|
struct stasis_message *stasis_cache_clear_create(struct stasis_message *id_message)
|
2013-03-08 15:15:13 +00:00
|
|
|
{
|
2014-03-01 00:02:02 +00:00
|
|
|
return stasis_message_create(stasis_cache_clear_type(), id_message);
|
2013-03-08 15:15:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void stasis_cache_update_dtor(void *obj)
|
|
|
|
{
|
|
|
|
struct stasis_cache_update *update = obj;
|
2014-03-01 00:02:02 +00:00
|
|
|
|
2013-03-08 15:15:13 +00:00
|
|
|
ao2_cleanup(update->old_snapshot);
|
|
|
|
update->old_snapshot = NULL;
|
|
|
|
ao2_cleanup(update->new_snapshot);
|
|
|
|
update->new_snapshot = NULL;
|
|
|
|
ao2_cleanup(update->type);
|
|
|
|
update->type = NULL;
|
|
|
|
}
|
|
|
|
|
Multiple revisions 399887,400138,400178,400180-400181
........
r399887 | dlee | 2013-09-26 10:41:47 -0500 (Thu, 26 Sep 2013) | 1 line
Minor performance bump by not allocate manager variable struct if we don't need it
........
r400138 | dlee | 2013-09-30 10:24:00 -0500 (Mon, 30 Sep 2013) | 23 lines
Stasis performance improvements
This patch addresses several performance problems that were found in
the initial performance testing of Asterisk 12.
The Stasis dispatch object was allocated as an AO2 object, even though
it has a very confined lifecycle. This was replaced with a straight
ast_malloc().
The Stasis message router was spending an inordinate amount of time
searching hash tables. In this case, most of our routers had 6 or
fewer routes in them to begin with. This was replaced with an array
that's searched linearly for the route.
We more heavily rely on AO2 objects in Asterisk 12, and the memset()
in ao2_ref() actually became noticeable on the profile. This was
#ifdef'ed to only run when AO2_DEBUG was enabled.
After being misled by an erroneous comment in taskprocessor.c during
profiling, the wrong comment was removed.
Review: https://reviewboard.asterisk.org/r/2873/
........
r400178 | dlee | 2013-09-30 13:26:27 -0500 (Mon, 30 Sep 2013) | 24 lines
Taskprocessor optimization; switch Stasis to use taskprocessors
This patch optimizes taskprocessor to use a semaphore for signaling,
which the OS can do a better job at managing contention and waiting
that we can with a mutex and condition.
The taskprocessor execution was also slightly optimized to reduce the
number of locks taken.
The only observable difference in the taskprocessor implementation is
that when the final reference to the taskprocessor goes away, it will
execute all tasks to completion instead of discarding the unexecuted
tasks.
For systems where unnamed semaphores are not supported, a really
simple semaphore implementation is provided. (Which gives identical
performance as the original taskprocessor implementation).
The way we ended up implementing Stasis caused the threadpool to be a
burden instead of a boost to performance. This was switched to just
use taskprocessors directly for subscriptions.
Review: https://reviewboard.asterisk.org/r/2881/
........
r400180 | dlee | 2013-09-30 13:39:34 -0500 (Mon, 30 Sep 2013) | 28 lines
Optimize how Stasis forwards are dispatched
This patch optimizes how forwards are dispatched in Stasis.
Originally, forwards were dispatched as subscriptions that are invoked
on the publishing thread. This did not account for the vast number of
forwards we would end up having in the system, and the amount of work it
would take to walk though the forward subscriptions.
This patch modifies Stasis so that rather than walking the tree of
forwards on every dispatch, when forwards and subscriptions are changed,
the subscriber list for every topic in the tree is changed.
This has a couple of benefits. First, this reduces the workload of
dispatching messages. It also reduces contention when dispatching to
different topics that happen to forward to the same aggregation topic
(as happens with all of the channel, bridge and endpoint topics).
Since forwards are no longer subscriptions, the bulk of this patch is
simply changing stasis_subscription objects to stasis_forward objects
(which, admittedly, I should have done in the first place.)
Since this required me to yet again put in a growing array, I finally
abstracted that out into a set of ast_vector macros in
asterisk/vector.h.
Review: https://reviewboard.asterisk.org/r/2883/
........
r400181 | dlee | 2013-09-30 13:48:57 -0500 (Mon, 30 Sep 2013) | 28 lines
Remove dispatch object allocation from Stasis publishing
While looking for areas for performance improvement, I realized that an
unused feature in Stasis was negatively impacting performance.
When a message is sent to a subscriber, a dispatch object is allocated
for the dispatch, containing the topic the message was published to, the
subscriber the message is being sent to, and the message itself.
The topic is actually unused by any subscriber in Asterisk today. And
the subscriber is associated with the taskprocessor the message is being
dispatched to.
First, this patch removes the unused topic parameter from Stasis
subscription callbacks.
Second, this patch introduces the concept of taskprocessor local data,
data that may be set on a taskprocessor and provided along with the data
pointer when a task is pushed using the ast_taskprocessor_push_local()
call. This allows the task to have both data specific to that
taskprocessor, in addition to data specific to that invocation.
With those two changes, the dispatch object can be removed completely,
and the message is simply refcounted and sent directly to the
taskprocessor.
Review: https://reviewboard.asterisk.org/r/2884/
........
Merged revisions 399887,400138,400178,400180-400181 from http://svn.asterisk.org/svn/asterisk/branches/12
git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@400186 65c4cc65-6c06-0410-ace0-fbb531ad65f3
2013-09-30 18:55:27 +00:00
|
|
|
static struct stasis_message *update_create(struct stasis_message *old_snapshot, struct stasis_message *new_snapshot)
|
2013-03-08 15:15:13 +00:00
|
|
|
{
|
2014-03-01 00:02:02 +00:00
|
|
|
struct stasis_cache_update *update;
|
|
|
|
struct stasis_message *msg;
|
2013-03-08 15:15:13 +00:00
|
|
|
|
|
|
|
ast_assert(old_snapshot != NULL || new_snapshot != NULL);
|
|
|
|
|
2014-08-06 12:55:28 +00:00
|
|
|
if (!stasis_cache_update_type()) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2013-08-01 13:49:34 +00:00
|
|
|
update = ao2_alloc_options(sizeof(*update), stasis_cache_update_dtor,
|
|
|
|
AO2_ALLOC_OPT_LOCK_NOLOCK);
|
2013-03-08 15:15:13 +00:00
|
|
|
if (!update) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (old_snapshot) {
|
|
|
|
ao2_ref(old_snapshot, +1);
|
|
|
|
update->old_snapshot = old_snapshot;
|
|
|
|
if (!new_snapshot) {
|
|
|
|
ao2_ref(stasis_message_type(old_snapshot), +1);
|
|
|
|
update->type = stasis_message_type(old_snapshot);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (new_snapshot) {
|
|
|
|
ao2_ref(new_snapshot, +1);
|
|
|
|
update->new_snapshot = new_snapshot;
|
|
|
|
ao2_ref(stasis_message_type(new_snapshot), +1);
|
|
|
|
update->type = stasis_message_type(new_snapshot);
|
|
|
|
}
|
|
|
|
|
2013-03-28 15:45:18 +00:00
|
|
|
msg = stasis_message_create(stasis_cache_update_type(), update);
|
2013-03-08 15:15:13 +00:00
|
|
|
|
2014-03-01 00:02:02 +00:00
|
|
|
ao2_cleanup(update);
|
2013-03-08 15:15:13 +00:00
|
|
|
return msg;
|
|
|
|
}
|
|
|
|
|
2013-08-01 13:49:34 +00:00
|
|
|
static void caching_topic_exec(void *data, struct stasis_subscription *sub,
|
Multiple revisions 399887,400138,400178,400180-400181
........
r399887 | dlee | 2013-09-26 10:41:47 -0500 (Thu, 26 Sep 2013) | 1 line
Minor performance bump by not allocate manager variable struct if we don't need it
........
r400138 | dlee | 2013-09-30 10:24:00 -0500 (Mon, 30 Sep 2013) | 23 lines
Stasis performance improvements
This patch addresses several performance problems that were found in
the initial performance testing of Asterisk 12.
The Stasis dispatch object was allocated as an AO2 object, even though
it has a very confined lifecycle. This was replaced with a straight
ast_malloc().
The Stasis message router was spending an inordinate amount of time
searching hash tables. In this case, most of our routers had 6 or
fewer routes in them to begin with. This was replaced with an array
that's searched linearly for the route.
We more heavily rely on AO2 objects in Asterisk 12, and the memset()
in ao2_ref() actually became noticeable on the profile. This was
#ifdef'ed to only run when AO2_DEBUG was enabled.
After being misled by an erroneous comment in taskprocessor.c during
profiling, the wrong comment was removed.
Review: https://reviewboard.asterisk.org/r/2873/
........
r400178 | dlee | 2013-09-30 13:26:27 -0500 (Mon, 30 Sep 2013) | 24 lines
Taskprocessor optimization; switch Stasis to use taskprocessors
This patch optimizes taskprocessor to use a semaphore for signaling,
which the OS can do a better job at managing contention and waiting
that we can with a mutex and condition.
The taskprocessor execution was also slightly optimized to reduce the
number of locks taken.
The only observable difference in the taskprocessor implementation is
that when the final reference to the taskprocessor goes away, it will
execute all tasks to completion instead of discarding the unexecuted
tasks.
For systems where unnamed semaphores are not supported, a really
simple semaphore implementation is provided. (Which gives identical
performance as the original taskprocessor implementation).
The way we ended up implementing Stasis caused the threadpool to be a
burden instead of a boost to performance. This was switched to just
use taskprocessors directly for subscriptions.
Review: https://reviewboard.asterisk.org/r/2881/
........
r400180 | dlee | 2013-09-30 13:39:34 -0500 (Mon, 30 Sep 2013) | 28 lines
Optimize how Stasis forwards are dispatched
This patch optimizes how forwards are dispatched in Stasis.
Originally, forwards were dispatched as subscriptions that are invoked
on the publishing thread. This did not account for the vast number of
forwards we would end up having in the system, and the amount of work it
would take to walk though the forward subscriptions.
This patch modifies Stasis so that rather than walking the tree of
forwards on every dispatch, when forwards and subscriptions are changed,
the subscriber list for every topic in the tree is changed.
This has a couple of benefits. First, this reduces the workload of
dispatching messages. It also reduces contention when dispatching to
different topics that happen to forward to the same aggregation topic
(as happens with all of the channel, bridge and endpoint topics).
Since forwards are no longer subscriptions, the bulk of this patch is
simply changing stasis_subscription objects to stasis_forward objects
(which, admittedly, I should have done in the first place.)
Since this required me to yet again put in a growing array, I finally
abstracted that out into a set of ast_vector macros in
asterisk/vector.h.
Review: https://reviewboard.asterisk.org/r/2883/
........
r400181 | dlee | 2013-09-30 13:48:57 -0500 (Mon, 30 Sep 2013) | 28 lines
Remove dispatch object allocation from Stasis publishing
While looking for areas for performance improvement, I realized that an
unused feature in Stasis was negatively impacting performance.
When a message is sent to a subscriber, a dispatch object is allocated
for the dispatch, containing the topic the message was published to, the
subscriber the message is being sent to, and the message itself.
The topic is actually unused by any subscriber in Asterisk today. And
the subscriber is associated with the taskprocessor the message is being
dispatched to.
First, this patch removes the unused topic parameter from Stasis
subscription callbacks.
Second, this patch introduces the concept of taskprocessor local data,
data that may be set on a taskprocessor and provided along with the data
pointer when a task is pushed using the ast_taskprocessor_push_local()
call. This allows the task to have both data specific to that
taskprocessor, in addition to data specific to that invocation.
With those two changes, the dispatch object can be removed completely,
and the message is simply refcounted and sent directly to the
taskprocessor.
Review: https://reviewboard.asterisk.org/r/2884/
........
Merged revisions 399887,400138,400178,400180-400181 from http://svn.asterisk.org/svn/asterisk/branches/12
git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@400186 65c4cc65-6c06-0410-ace0-fbb531ad65f3
2013-09-30 18:55:27 +00:00
|
|
|
struct stasis_message *message)
|
2013-03-08 15:15:13 +00:00
|
|
|
{
|
2014-03-07 20:41:13 +00:00
|
|
|
struct stasis_caching_topic *caching_topic_needs_unref;
|
2013-03-08 15:15:13 +00:00
|
|
|
struct stasis_caching_topic *caching_topic = data;
|
2014-03-07 20:41:13 +00:00
|
|
|
struct stasis_message *msg;
|
|
|
|
struct stasis_message *msg_put;
|
|
|
|
struct stasis_message_type *msg_type;
|
|
|
|
const struct ast_eid *msg_eid;
|
|
|
|
const char *msg_id;
|
2013-03-08 15:15:13 +00:00
|
|
|
|
2013-07-18 14:08:21 +00:00
|
|
|
ast_assert(caching_topic != NULL);
|
2013-03-08 15:15:13 +00:00
|
|
|
ast_assert(caching_topic->topic != NULL);
|
2013-08-01 13:49:34 +00:00
|
|
|
ast_assert(caching_topic->cache != NULL);
|
|
|
|
ast_assert(caching_topic->cache->id_fn != NULL);
|
2013-03-08 15:15:13 +00:00
|
|
|
|
|
|
|
if (stasis_subscription_final_message(sub, message)) {
|
|
|
|
caching_topic_needs_unref = caching_topic;
|
2014-03-07 20:41:13 +00:00
|
|
|
} else {
|
|
|
|
caching_topic_needs_unref = NULL;
|
2013-03-08 15:15:13 +00:00
|
|
|
}
|
|
|
|
|
2014-03-07 20:41:13 +00:00
|
|
|
msg_type = stasis_message_type(message);
|
2018-08-30 18:08:05 +00:00
|
|
|
|
2018-09-13 16:06:00 +00:00
|
|
|
/*
|
|
|
|
* app_voicemail used to rely on the cache containing every topic subscribe and
|
|
|
|
* unsubscribe in order to determine if anyone was currently subscribed to a
|
|
|
|
* particular mailbox. This caused the cache to grow unabated for the life of
|
|
|
|
* the asterisk instance. Since it no longer needs the cache of these message
|
|
|
|
* types, and no other function needs them, we no longer cache them.
|
|
|
|
*/
|
2018-08-30 18:08:05 +00:00
|
|
|
if (stasis_subscription_change_type() == msg_type) {
|
2018-09-13 16:06:00 +00:00
|
|
|
ao2_cleanup(caching_topic_needs_unref);
|
|
|
|
return;
|
2018-08-30 18:08:05 +00:00
|
|
|
} else if (stasis_cache_clear_type() == msg_type) {
|
2014-03-07 20:41:13 +00:00
|
|
|
/* Cache clear event. */
|
|
|
|
msg_put = NULL;
|
|
|
|
msg = stasis_message_data(message);
|
|
|
|
msg_type = stasis_message_type(msg);
|
|
|
|
} else {
|
|
|
|
/* Normal cache update event. */
|
|
|
|
msg_put = message;
|
|
|
|
msg = message;
|
|
|
|
}
|
|
|
|
ast_assert(msg_type != NULL);
|
2013-06-07 12:56:56 +00:00
|
|
|
|
2014-03-07 20:41:13 +00:00
|
|
|
msg_eid = stasis_message_eid(msg);/* msg_eid is NULL for aggregate message. */
|
|
|
|
msg_id = caching_topic->cache->id_fn(msg);
|
|
|
|
if (msg_id && msg_eid) {
|
|
|
|
struct stasis_message *update;
|
|
|
|
struct cache_put_snapshots snapshots;
|
2013-06-07 12:56:56 +00:00
|
|
|
|
2014-03-07 20:41:13 +00:00
|
|
|
/* Update the cache */
|
|
|
|
snapshots = cache_put(caching_topic->cache, msg_type, msg_id, msg_eid, msg_put);
|
|
|
|
if (snapshots.old || msg_put) {
|
|
|
|
update = update_create(snapshots.old, msg_put);
|
|
|
|
if (update) {
|
2013-06-07 12:56:56 +00:00
|
|
|
stasis_publish(caching_topic->topic, update);
|
|
|
|
}
|
2014-03-07 20:41:13 +00:00
|
|
|
ao2_cleanup(update);
|
|
|
|
} else {
|
main/stasis_cache: Demote the ERROR message when removing a nonexistent item
This patch demotes the ERROR message that is displayed when a
nonexistent item is removed from the Stasis cache. The genesis of this
demotion is due to chan_sip's realtime peers and their interaction with
Asterisk's core ast_endpoint code, but ostensibly it could happen from
other channel drivers as well.
Since Mark Michelson already did an excellent job of explaining on this
issue, it is quoted here for posterity:
"Internally, when a realtime peer is retrieved, Asterisk creates an
ast_endpoint structure. When that peer is destroyed, the ast_endpoint is
destroyed as well. Part of the destruction of the ast_endpoint involves
clearing the Stasis cache of all information about that endpoint. The
problem here is that the act of creating the ast_endpoint is not enough
to actually put any information in the Stasis cache. Instead, something
has to happen, such as a state change, in order for the Stasis cache to
have any information about that endpoint. When a device registers,
chan_sip creates an ast_endpoint structure, processes the REGISTER, and
then destroys the ast_endpoint. When the ast_endpoint is destroyed,
there is nothing to destroy in the Stasis cache, so an error message is
emitted. When you use rtcachefriends, ast_endpoint structures persist
for the lifetime of the module and so you do not see this error
message."
ASTERISK-25237 #close
Change-Id: I53cebc6b4a897a1ab9564182b75c177780feff70
2017-03-14 14:37:34 +00:00
|
|
|
ast_debug(1,
|
2013-06-07 12:56:56 +00:00
|
|
|
"Attempting to remove an item from the %s cache that isn't there: %s %s\n",
|
2014-03-07 20:41:13 +00:00
|
|
|
stasis_topic_name(caching_topic->topic),
|
|
|
|
stasis_message_type_name(msg_type), msg_id);
|
2013-03-08 15:15:13 +00:00
|
|
|
}
|
|
|
|
|
2014-03-07 20:41:13 +00:00
|
|
|
if (snapshots.aggregate_old != snapshots.aggregate_new) {
|
|
|
|
if (snapshots.aggregate_new && caching_topic->cache->aggregate_publish_fn) {
|
|
|
|
caching_topic->cache->aggregate_publish_fn(caching_topic->original_topic,
|
|
|
|
snapshots.aggregate_new);
|
|
|
|
}
|
|
|
|
update = update_create(snapshots.aggregate_old, snapshots.aggregate_new);
|
|
|
|
if (update) {
|
|
|
|
stasis_publish(caching_topic->topic, update);
|
|
|
|
}
|
|
|
|
ao2_cleanup(update);
|
2013-03-08 15:15:13 +00:00
|
|
|
}
|
|
|
|
|
2014-03-07 20:41:13 +00:00
|
|
|
ao2_cleanup(snapshots.old);
|
|
|
|
ao2_cleanup(snapshots.aggregate_old);
|
|
|
|
ao2_cleanup(snapshots.aggregate_new);
|
2013-03-08 15:15:13 +00:00
|
|
|
}
|
2014-03-07 20:41:13 +00:00
|
|
|
|
|
|
|
ao2_cleanup(caching_topic_needs_unref);
|
2013-03-08 15:15:13 +00:00
|
|
|
}
|
|
|
|
|
2018-08-30 18:08:05 +00:00
|
|
|
static void print_cache_entry(void *v_obj, void *where, ao2_prnt_fn *prnt)
|
|
|
|
{
|
|
|
|
struct stasis_cache_entry *entry = v_obj;
|
|
|
|
|
|
|
|
if (!entry) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
prnt(where, "Type: %s ID: %s Hash: %u", stasis_message_type_name(entry->key.type),
|
|
|
|
entry->key.id, entry->key.hash);
|
|
|
|
}
|
|
|
|
|
2013-08-01 13:49:34 +00:00
|
|
|
struct stasis_caching_topic *stasis_caching_topic_create(struct stasis_topic *original_topic, struct stasis_cache *cache)
|
2013-03-08 15:15:13 +00:00
|
|
|
{
|
2018-01-09 17:10:27 +00:00
|
|
|
struct stasis_caching_topic *caching_topic;
|
|
|
|
char *new_name;
|
2013-03-08 15:15:13 +00:00
|
|
|
int ret;
|
|
|
|
|
2013-10-02 17:12:49 +00:00
|
|
|
ret = ast_asprintf(&new_name, "%s-cached", stasis_topic_name(original_topic));
|
2013-03-08 15:15:13 +00:00
|
|
|
if (ret < 0) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2013-08-01 13:49:34 +00:00
|
|
|
caching_topic = ao2_alloc_options(sizeof(*caching_topic),
|
|
|
|
stasis_caching_topic_dtor, AO2_ALLOC_OPT_LOCK_NOLOCK);
|
2013-03-08 15:15:13 +00:00
|
|
|
if (caching_topic == NULL) {
|
2018-01-09 17:10:27 +00:00
|
|
|
ast_free(new_name);
|
|
|
|
|
2013-03-08 15:15:13 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
caching_topic->topic = stasis_topic_create(new_name);
|
|
|
|
if (caching_topic->topic == NULL) {
|
2018-01-09 17:10:27 +00:00
|
|
|
ao2_ref(caching_topic, -1);
|
2018-08-30 18:08:05 +00:00
|
|
|
ast_free(new_name);
|
2018-01-09 17:10:27 +00:00
|
|
|
|
2013-03-08 15:15:13 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2013-08-01 13:49:34 +00:00
|
|
|
ao2_ref(cache, +1);
|
|
|
|
caching_topic->cache = cache;
|
2018-08-30 18:08:05 +00:00
|
|
|
if (!cache->registered) {
|
|
|
|
if (ao2_container_register(new_name, cache->entries, print_cache_entry)) {
|
|
|
|
ast_log(LOG_ERROR, "Stasis cache container '%p' for '%s' did not register\n",
|
|
|
|
cache->entries, new_name);
|
|
|
|
} else {
|
|
|
|
cache->registered = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ast_free(new_name);
|
2013-03-08 15:15:13 +00:00
|
|
|
|
2018-01-09 17:10:27 +00:00
|
|
|
caching_topic->sub = internal_stasis_subscribe(original_topic, caching_topic_exec, caching_topic, 0, 0);
|
|
|
|
if (caching_topic->sub == NULL) {
|
|
|
|
ao2_ref(caching_topic, -1);
|
|
|
|
|
2013-03-08 15:15:13 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2013-06-13 11:02:16 +00:00
|
|
|
|
|
|
|
ao2_ref(original_topic, +1);
|
|
|
|
caching_topic->original_topic = original_topic;
|
|
|
|
|
2013-07-18 14:08:21 +00:00
|
|
|
/* The subscription holds the reference, so no additional ref bump. */
|
2013-03-08 15:15:13 +00:00
|
|
|
return caching_topic;
|
|
|
|
}
|
|
|
|
|
Avoid unnecessary cleanups during immediate shutdown
This patch addresses issues during immediate shutdowns, where modules
are not unloaded, but Asterisk atexit handlers are run.
In the typical case, this usually isn't a big deal. But the
introduction of the Stasis message bus makes it much more likely for
asynchronous activity to be happening off in some thread during
shutdown.
During an immediate shutdown, Asterisk skips unloading modules. But
while it is processing the atexit handlers, there is a window of time
where some of the core message types have been cleaned up, but the
message bus is still running. Specifically, it's still running
module subscriptions that might be using the core message types. If a
message is received by that subscription in that window, it will
attempt to use a message type that has been cleaned up.
To solve this problem, this patch introduces ast_register_cleanup().
This function operates identically to ast_register_atexit(), except
that cleanup calls are not invoked on an immediate shutdown. All of
the core message type and topic cleanup was moved from atexit handlers
to cleanup handlers.
This ensures that core type and topic cleanup only happens if the
modules that used them are first unloaded.
This patch also changes the ast_assert() when accessing a cleaned up
or uninitialized message type to an error log message. Message type
functions are actually NULL safe across the board, so the assert was a
bit heavy handed. Especially for anyone with DO_CRASH enabled.
Review: https://reviewboard.asterisk.org/r/2562/
git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@390122 65c4cc65-6c06-0410-ace0-fbb531ad65f3
2013-05-30 17:05:53 +00:00
|
|
|
static void stasis_cache_cleanup(void)
|
2013-03-08 15:15:13 +00:00
|
|
|
{
|
2013-05-15 02:37:22 +00:00
|
|
|
STASIS_MESSAGE_TYPE_CLEANUP(stasis_cache_clear_type);
|
|
|
|
STASIS_MESSAGE_TYPE_CLEANUP(stasis_cache_update_type);
|
2013-03-08 15:15:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int stasis_cache_init(void)
|
|
|
|
{
|
Avoid unnecessary cleanups during immediate shutdown
This patch addresses issues during immediate shutdowns, where modules
are not unloaded, but Asterisk atexit handlers are run.
In the typical case, this usually isn't a big deal. But the
introduction of the Stasis message bus makes it much more likely for
asynchronous activity to be happening off in some thread during
shutdown.
During an immediate shutdown, Asterisk skips unloading modules. But
while it is processing the atexit handlers, there is a window of time
where some of the core message types have been cleaned up, but the
message bus is still running. Specifically, it's still running
module subscriptions that might be using the core message types. If a
message is received by that subscription in that window, it will
attempt to use a message type that has been cleaned up.
To solve this problem, this patch introduces ast_register_cleanup().
This function operates identically to ast_register_atexit(), except
that cleanup calls are not invoked on an immediate shutdown. All of
the core message type and topic cleanup was moved from atexit handlers
to cleanup handlers.
This ensures that core type and topic cleanup only happens if the
modules that used them are first unloaded.
This patch also changes the ast_assert() when accessing a cleaned up
or uninitialized message type to an error log message. Message type
functions are actually NULL safe across the board, so the assert was a
bit heavy handed. Especially for anyone with DO_CRASH enabled.
Review: https://reviewboard.asterisk.org/r/2562/
git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@390122 65c4cc65-6c06-0410-ace0-fbb531ad65f3
2013-05-30 17:05:53 +00:00
|
|
|
ast_register_cleanup(stasis_cache_cleanup);
|
2013-03-08 15:15:13 +00:00
|
|
|
|
2013-05-15 02:37:22 +00:00
|
|
|
if (STASIS_MESSAGE_TYPE_INIT(stasis_cache_clear_type) != 0) {
|
2013-03-08 15:15:13 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2013-05-15 02:37:22 +00:00
|
|
|
if (STASIS_MESSAGE_TYPE_INIT(stasis_cache_update_type) != 0) {
|
2013-03-08 15:15:13 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|