| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
#include "asterisk.h" |
| 20 |
|
| 21 |
#include "asterisk/astobj2.h" |
| 22 |
#include "asterisk/serializer.h" |
| 23 |
#include "asterisk/taskprocessor.h" |
| 24 |
#include "asterisk/threadpool.h" |
| 25 |
#include "asterisk/taskpool.h" |
| 26 |
#include "asterisk/utils.h" |
| 27 |
#include "asterisk/vector.h" |
| 28 |
#include "asterisk/serializer_shutdown_group.h" |
| 29 |
|
| 30 |
struct ast_serializer_pool { |
| 31 |
|
| 32 |
struct ast_serializer_shutdown_group *shutdown_group; |
| 33 |
|
| 34 |
int shutdown_group_timeout; |
| 35 |
|
| 36 |
AST_VECTOR_RW(, struct ast_taskprocessor *) serializers; |
| 37 |
|
| 38 |
char name[]; |
| 39 |
}; |
| 40 |
|
| 41 |
int ast_serializer_pool_destroy(struct ast_serializer_pool *pool) |
| 42 |
{ |
| 43 |
if (!pool) { |
| 44 |
return 0; |
| 45 |
} |
| 46 |
|
| 47 |
|
| 48 |
AST_VECTOR_RW_WRLOCK(&pool->serializers); |
| 49 |
AST_VECTOR_RESET(&pool->serializers, ast_taskprocessor_unreference); |
| 50 |
AST_VECTOR_RW_UNLOCK(&pool->serializers); |
| 51 |
|
| 52 |
|
| 53 |
if (pool->shutdown_group) { |
| 54 |
int remaining; |
| 55 |
|
| 56 |
ast_debug(3, "Waiting on serializers before destroying pool '%s'\n", pool->name); |
| 57 |
|
| 58 |
remaining = ast_serializer_shutdown_group_join( |
| 59 |
pool->shutdown_group, pool->shutdown_group_timeout); |
| 60 |
|
| 61 |
if (remaining) { |
| 62 |
|
| 63 |
ast_log(LOG_WARNING, "'%s' serializer pool destruction timeout. " |
| 64 |
"'%d' dependencies still processing.\n", pool->name, remaining); |
| 65 |
return remaining; |
| 66 |
} |
| 67 |
|
| 68 |
ao2_ref(pool->shutdown_group, -1); |
| 69 |
pool->shutdown_group = NULL; |
| 70 |
} |
| 71 |
|
| 72 |
AST_VECTOR_RW_FREE(&pool->serializers); |
| 73 |
ast_free(pool); |
| 74 |
|
| 75 |
return 0; |
| 76 |
} |
| 77 |
|
| 78 |
struct ast_serializer_pool *ast_serializer_pool_create(const char *name, |
| 79 |
unsigned int size, struct ast_threadpool *threadpool, int timeout) |
| 80 |
{ |
| 81 |
struct ast_serializer_pool *pool; |
| 82 |
char tps_name[AST_TASKPROCESSOR_MAX_NAME + 1]; |
| 83 |
size_t idx; |
| 84 |
|
| 85 |
ast_assert(size > 0); |
| 86 |
|
| 87 |
pool = ast_malloc(sizeof(*pool) + strlen(name) + 1); |
| 88 |
if (!pool) { |
| 89 |
return NULL; |
| 90 |
} |
| 91 |
|
| 92 |
strcpy(pool->name, name); |
| 93 |
|
| 94 |
pool->shutdown_group_timeout = timeout; |
| 95 |
pool->shutdown_group = timeout > -1 ? ast_serializer_shutdown_group_alloc() : NULL; |
| 96 |
|
| 97 |
AST_VECTOR_RW_INIT(&pool->serializers, size); |
| 98 |
|
| 99 |
for (idx = 0; idx < size; ++idx) { |
| 100 |
struct ast_taskprocessor *tps; |
| 101 |
|
| 102 |
|
| 103 |
ast_taskprocessor_name_append(tps_name, sizeof(tps_name), name); |
| 104 |
|
| 105 |
tps = ast_threadpool_serializer_group(tps_name, threadpool, pool->shutdown_group); |
| 106 |
if (!tps) { |
| 107 |
ast_serializer_pool_destroy(pool); |
| 108 |
ast_log(LOG_ERROR, "Pool create: unable to create named serializer '%s'\n", |
| 109 |
tps_name); |
| 110 |
return NULL; |
| 111 |
} |
| 112 |
|
| 113 |
if (AST_VECTOR_APPEND(&pool->serializers, tps)) { |
| 114 |
ast_serializer_pool_destroy(pool); |
| 115 |
ast_log(LOG_ERROR, "Pool create: unable to append named serializer '%s'\n", |
| 116 |
tps_name); |
| 117 |
return NULL; |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
return pool; |
| 122 |
} |
| 123 |
|
| 124 |
struct ast_serializer_pool *ast_serializer_taskpool_create(const char *name, |
| 125 |
unsigned int size, struct ast_taskpool *taskpool, int timeout) |
| 126 |
{ |
| 127 |
struct ast_serializer_pool *pool; |
| 128 |
char tps_name[AST_TASKPROCESSOR_MAX_NAME + 1]; |
| 129 |
size_t idx; |
| 130 |
|
| 131 |
ast_assert(size > 0); |
| 132 |
|
| 133 |
pool = ast_malloc(sizeof(*pool) + strlen(name) + 1); |
| 134 |
if (!pool) { |
| 135 |
return NULL; |
| 136 |
} |
| 137 |
|
| 138 |
strcpy(pool->name, name); |
| 139 |
|
| 140 |
pool->shutdown_group_timeout = timeout; |
| 141 |
pool->shutdown_group = timeout > -1 ? ast_serializer_shutdown_group_alloc() : NULL; |
| 142 |
|
| 143 |
AST_VECTOR_RW_INIT(&pool->serializers, size); |
| 144 |
|
| 145 |
for (idx = 0; idx < size; ++idx) { |
| 146 |
struct ast_taskprocessor *tps; |
| 147 |
|
| 148 |
|
| 149 |
ast_taskprocessor_name_append(tps_name, sizeof(tps_name), name); |
| 150 |
|
| 151 |
tps = ast_taskpool_serializer_group(tps_name, taskpool, pool->shutdown_group); |
| 152 |
if (!tps) { |
| 153 |
ast_serializer_pool_destroy(pool); |
| 154 |
ast_log(LOG_ERROR, "Pool create: unable to create named serializer '%s'\n", |
| 155 |
tps_name); |
| 156 |
return NULL; |
| 157 |
} |
| 158 |
|
| 159 |
if (AST_VECTOR_APPEND(&pool->serializers, tps)) { |
| 160 |
ast_serializer_pool_destroy(pool); |
| 161 |
ast_log(LOG_ERROR, "Pool create: unable to append named serializer '%s'\n", |
| 162 |
tps_name); |
| 163 |
return NULL; |
| 164 |
} |
| 165 |
} |
| 166 |
|
| 167 |
return pool; |
| 168 |
} |
| 169 |
|
| 170 |
const char *ast_serializer_pool_name(const struct ast_serializer_pool *pool) |
| 171 |
{ |
| 172 |
return pool->name; |
| 173 |
} |
| 174 |
|
| 175 |
struct ast_taskprocessor *ast_serializer_pool_get(struct ast_serializer_pool *pool) |
| 176 |
{ |
| 177 |
struct ast_taskprocessor *res; |
| 178 |
size_t idx; |
| 179 |
|
| 180 |
if (!pool) { |
| 181 |
return NULL; |
| 182 |
} |
| 183 |
|
| 184 |
AST_VECTOR_RW_RDLOCK(&pool->serializers); |
| 185 |
if (AST_VECTOR_SIZE(&pool->serializers) == 0) { |
| 186 |
AST_VECTOR_RW_UNLOCK(&pool->serializers); |
| 187 |
return NULL; |
| 188 |
} |
| 189 |
|
| 190 |
res = AST_VECTOR_GET(&pool->serializers, 0); |
| 191 |
|
| 192 |
|
| 193 |
for (idx = 1; idx < AST_VECTOR_SIZE(&pool->serializers); ++idx) { |
| 194 |
struct ast_taskprocessor *cur = AST_VECTOR_GET(&pool->serializers, idx); |
| 195 |
if (ast_taskprocessor_size(cur) < ast_taskprocessor_size(res)) { |
| 196 |
res = cur; |
| 197 |
} |
| 198 |
} |
| 199 |
|
| 200 |
AST_VECTOR_RW_UNLOCK(&pool->serializers); |
| 201 |
return res; |
| 202 |
} |
| 203 |
|
| 204 |
int ast_serializer_pool_set_alerts(struct ast_serializer_pool *pool, long high, long low) |
| 205 |
{ |
| 206 |
size_t idx; |
| 207 |
long tps_queue_high; |
| 208 |
long tps_queue_low; |
| 209 |
|
| 210 |
if (!pool) { |
| 211 |
return 0; |
| 212 |
} |
| 213 |
|
| 214 |
tps_queue_high = high; |
| 215 |
if (tps_queue_high <= 0) { |
| 216 |
ast_log(AST_LOG_WARNING, "Invalid '%s-*' taskprocessor high water alert " |
| 217 |
"trigger level '%ld'\n", pool->name, tps_queue_high); |
| 218 |
tps_queue_high = AST_TASKPROCESSOR_HIGH_WATER_LEVEL; |
| 219 |
} |
| 220 |
|
| 221 |
tps_queue_low = low; |
| 222 |
if (tps_queue_low < -1 || tps_queue_high < tps_queue_low) { |
| 223 |
ast_log(AST_LOG_WARNING, "Invalid '%s-*' taskprocessor low water clear alert " |
| 224 |
"level '%ld'\n", pool->name, tps_queue_low); |
| 225 |
tps_queue_low = -1; |
| 226 |
} |
| 227 |
|
| 228 |
for (idx = 0; idx < AST_VECTOR_SIZE(&pool->serializers); ++idx) { |
| 229 |
struct ast_taskprocessor *cur = AST_VECTOR_GET(&pool->serializers, idx); |
| 230 |
if (ast_taskprocessor_alert_set_levels(cur, tps_queue_low, tps_queue_high)) { |
| 231 |
ast_log(AST_LOG_WARNING, "Failed to set alert levels for serializer '%s'.\n", |
| 232 |
ast_taskprocessor_name(cur)); |
| 233 |
} |
| 234 |
} |
| 235 |
|
| 236 |
return 0; |
| 237 |
} |