[Asterisk-code-review] taskprocessor.c: Added "like" support to 'core show taskprocessors' (...asterisk[13])
Benjamin Keith Ford
asteriskteam at digium.com
Wed Sep 25 09:45:13 CDT 2019
Benjamin Keith Ford has uploaded this change for review. ( https://gerrit.asterisk.org/c/asterisk/+/12960
Change subject: taskprocessor.c: Added "like" support to 'core show taskprocessors'
......................................................................
taskprocessor.c: Added "like" support to 'core show taskprocessors'
Added "like" support for 'core show taskprocessors'. Now you
can specify a specific set of taskprocessors (or just one) by
adding the keyword "like" to the above command, followed by
your search criteria.
Change-Id: I021e740201e9ba487204b5451e46feb0e3222464
---
A doc/CHANGES-staging/taskprocessor-like-support.txt
M main/taskprocessor.c
2 files changed, 149 insertions(+), 41 deletions(-)
git pull ssh://gerrit.asterisk.org:29418/asterisk refs/changes/60/12960/1
diff --git a/doc/CHANGES-staging/taskprocessor-like-support.txt b/doc/CHANGES-staging/taskprocessor-like-support.txt
new file mode 100644
index 0000000..8f61d39
--- /dev/null
+++ b/doc/CHANGES-staging/taskprocessor-like-support.txt
@@ -0,0 +1,6 @@
+Subject: taskprocessor.c
+
+Added "like" support for 'core show taskprocessors'. Now you
+can specify a specific set of taskprocessors (or just one) by
+adding the keyword "like" to the above command, followed by
+your search criteria.
diff --git a/main/taskprocessor.c b/main/taskprocessor.c
index 270ce74..b9b58cf 100644
--- a/main/taskprocessor.c
+++ b/main/taskprocessor.c
@@ -501,58 +501,160 @@
return cmp;
}
-static char *cli_tps_report(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
+/*!
+ * \internal
+ * \brief Create and return a sorted taskprocessor ao2 container.
+ * \since 13.30.0
+ *
+ * \note The container will need to be unref'd by the caller.
+ *
+ * \retval sorted container on success
+ * \retval NULL on failure
+ */
+static struct ao2_container *get_sorted_taskprocessors_list(void)
{
- char name[256];
- int tcount;
- unsigned long qsize;
- unsigned long maxqsize;
- unsigned long processed;
struct ao2_container *sorted_tps;
- struct ast_taskprocessor *tps;
- struct ao2_iterator iter;
-#define FMT_HEADERS "%-70s %10s %10s %10s %10s %10s\n"
-#define FMT_FIELDS "%-70s %10lu %10lu %10lu %10lu %10lu\n"
-
- switch (cmd) {
- case CLI_INIT:
- e->command = "core show taskprocessors";
- e->usage =
- "Usage: core show taskprocessors\n"
- " Shows a list of instantiated task processors and their statistics\n";
- return NULL;
- case CLI_GENERATE:
- return NULL;
- }
-
- if (a->argc != e->args) {
- return CLI_SHOWUSAGE;
- }
sorted_tps = ao2_container_alloc_rbtree(AO2_ALLOC_OPT_LOCK_NOLOCK, 0, tps_sort_cb,
NULL);
if (!sorted_tps
|| ao2_container_dup(sorted_tps, tps_singletons, 0)) {
+ ast_debug(1, "Failed to retrieve sorted taskprocessors\n");
ao2_cleanup(sorted_tps);
- return CLI_FAILURE;
+ return NULL;
+ }
+
+ return sorted_tps;
+}
+
+/*!
+ * \internal
+ * \brief Tab completion for taskprocessors.
+ * \since 13.30.0
+ *
+ * \param word the string to tab complete on
+ * \param state the current state of the word for tab completion
+ *
+ * \retval match on success
+ * \retval NULL on failure
+ */
+static char *tps_report_generate_helper(const char *word, int state)
+{
+ int which = 0;
+ int wordlen = strlen(word);
+ char *ret = NULL;
+ struct ao2_container *sorted_tps;
+ struct ast_taskprocessor *tps;
+ struct ao2_iterator iter;
+
+ sorted_tps = get_sorted_taskprocessors_list();
+ if (!sorted_tps) {
+ return NULL;
+ }
+
+ iter = ao2_iterator_init(sorted_tps, AO2_ITERATOR_UNLINK);
+ while ((tps = ao2_iterator_next(&iter))) {
+ if (!strncasecmp(word, tps->name, wordlen) && ++which > state) {
+ ret = ast_strdup(tps->name);
+ break;
+ }
+ }
+ ao2_iterator_destroy(&iter);
+ ao2_ref(sorted_tps, -1);
+
+ return ret;
+}
+
+#define FMT_HEADERS "%-70s %10s %10s %10s %10s %10s\n"
+#define FMT_FIELDS "%-70s %10lu %10lu %10lu %10lu %10lu\n"
+
+/*!
+ * \internal
+ * \brief Print taskprocessor information to CLI.
+ * \since 13.30.0
+ *
+ * \param fd the file descriptor
+ * \param tps the taskprocessor
+ */
+static void tps_report_taskprocessor_list_helper(int fd, struct ast_taskprocessor *tps)
+{
+ ast_cli(fd, FMT_FIELDS, tps->name, tps->stats._tasks_processed_count,
+ tps->tps_queue_size, tps->stats.max_qsize, tps->tps_queue_low,
+ tps->tps_queue_high);
+}
+
+/*!
+ * \internal
+ * \brief Prints an optionally narrowed down list of taskprocessors to the CLI.
+ * \since 13.30.0
+ *
+ * \param fd the file descriptor
+ * \param like the string we are matching on
+ *
+ * \retval number of taskprocessors on success
+ * \retval 0 otherwise
+ */
+static int tps_report_taskprocessor_list(int fd, const char *like)
+{
+ int tps_count = 0;
+ struct ao2_container *sorted_tps;
+ struct ast_taskprocessor *tps;
+ struct ao2_iterator iter;
+
+ sorted_tps = get_sorted_taskprocessors_list();
+ if (!sorted_tps) {
+ return 0;
+ }
+
+ iter = ao2_iterator_init(sorted_tps, AO2_ITERATOR_UNLINK);
+ while ((tps = ao2_iterator_next(&iter))) {
+ if (like) {
+ if (!strncasecmp(like, tps->name, strlen(like))) {
+ tps_report_taskprocessor_list_helper(fd, tps);
+ tps_count++;
+ }
+ } else {
+ tps_report_taskprocessor_list_helper(fd, tps);
+ tps_count++;
+ }
+ ast_taskprocessor_unreference(tps);
+ }
+ ao2_iterator_destroy(&iter);
+ ao2_ref(sorted_tps, -1);
+
+ return tps_count;
+}
+
+static char *cli_tps_report(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
+{
+ const char *like;
+
+ switch (cmd) {
+ case CLI_INIT:
+ e->command = "core show taskprocessors [like]";
+ e->usage =
+ "Usage: core show taskprocessors [like keyword]\n"
+ " Shows a list of instantiated task processors and their statistics\n";
+ return NULL;
+ case CLI_GENERATE:
+ if (a->pos == e->args) {
+ return tps_report_generate_helper(a->word, a->n);
+ } else {
+ return NULL;
+ }
+ }
+
+ if (a->argc == e->args - 1) {
+ like = "";
+ } else if (a->argc == e->args + 1 && !strcasecmp(a->argv[e->args-1], "like")) {
+ like = a->argv[e->args];
+ } else {
+ return CLI_SHOWUSAGE;
}
ast_cli(a->fd, "\n" FMT_HEADERS, "Processor", "Processed", "In Queue", "Max Depth", "Low water", "High water");
- tcount = 0;
- iter = ao2_iterator_init(sorted_tps, AO2_ITERATOR_UNLINK);
- while ((tps = ao2_iterator_next(&iter))) {
- ast_copy_string(name, tps->name, sizeof(name));
- qsize = tps->tps_queue_size;
- maxqsize = tps->stats.max_qsize;
- processed = tps->stats._tasks_processed_count;
- ast_cli(a->fd, FMT_FIELDS, name, processed, qsize, maxqsize,
- tps->tps_queue_low, tps->tps_queue_high);
- ast_taskprocessor_unreference(tps);
- ++tcount;
- }
- ao2_iterator_destroy(&iter);
- ast_cli(a->fd, "\n%d taskprocessors\n\n", tcount);
- ao2_ref(sorted_tps, -1);
+ ast_cli(a->fd, "\n%d taskprocessors\n\n", tps_report_taskprocessor_list(a->fd, like));
+
return CLI_SUCCESS;
}
--
To view, visit https://gerrit.asterisk.org/c/asterisk/+/12960
To unsubscribe, or for help writing mail filters, visit https://gerrit.asterisk.org/settings
Gerrit-Project: asterisk
Gerrit-Branch: 13
Gerrit-Change-Id: I021e740201e9ba487204b5451e46feb0e3222464
Gerrit-Change-Number: 12960
Gerrit-PatchSet: 1
Gerrit-Owner: Benjamin Keith Ford <bford at digium.com>
Gerrit-MessageType: newchange
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.digium.com/pipermail/asterisk-code-review/attachments/20190925/b8f92043/attachment.html>
More information about the asterisk-code-review
mailing list