diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000000000000000000000000000000000000..d670ba552018ec9adcf48b9f659018438643c208 --- /dev/null +++ b/.clang-format @@ -0,0 +1,129 @@ +# ClangFormat configuration file +# Based on Google style with 4-space indentation + +# Run manually to reformat a file: +# clang-format -i --style=file + +BasedOnStyle: Google +Language: Cpp + +# Indentation +IndentWidth: 4 +TabWidth: 4 +UseTab: Never +IndentCaseLabels: true +IndentGotoLabels: true +IndentPPDirectives: None +IndentExternBlock: AfterExternBlock +IndentWrappedFunctionNames: false + +# Alignment +AlignAfterOpenBracket: Align +AlignArrayOfStructures: None +AlignConsecutiveAssignments: None +AlignConsecutiveBitFields: None +AlignConsecutiveDeclarations: None +AlignConsecutiveMacros: None +AlignEscapedNewlines: Left +AlignOperands: Align +AlignTrailingComments: true + +# Allow formatting of short constructs on single lines +AllowShortBlocksOnASingleLine: Never +AllowShortCaseLabelsOnASingleLine: false +AllowShortEnumsOnASingleLine: true +AllowShortFunctionsOnASingleLine: All +AllowShortIfStatementsOnASingleLine: Never +AllowShortLambdasOnASingleLine: All +AllowShortLoopsOnASingleLine: true + +# Breaking +AlwaysBreakAfterDefinitionReturnType: None +AlwaysBreakAfterReturnType: None +AlwaysBreakBeforeMultilineStrings: true +AlwaysBreakTemplateDeclarations: Yes +BinPackArguments: false +BinPackParameters: false +BreakBeforeBinaryOperators: None +BreakBeforeBraces: Custom +BraceWrapping: + AfterCaseLabel: false + AfterClass: true + AfterControlStatement: false + AfterEnum: false + AfterFunction: true + AfterNamespace: true + AfterObjCDeclaration: false + AfterStruct: false + AfterUnion: false + AfterExternBlock: false + BeforeCatch: false + BeforeElse: false + BeforeLambdaBody: false + BeforeWhile: false + IndentBraces: false + SplitEmptyFunction: true + SplitEmptyRecord: true + SplitEmptyNamespace: true +BreakBeforeConceptDeclarations: true +BreakBeforeTernaryOperators: true +BreakConstructorInitializers: BeforeColon +BreakInheritanceList: BeforeColon +BreakStringLiterals: true + +# Column limit +ColumnLimit: 120 +ContinuationIndentWidth: 4 + +# Spaces +SpaceAfterCStyleCast: false +SpaceAfterLogicalNot: false +SpaceAfterTemplateKeyword: true +SpaceAroundPointerQualifiers: Default +SpaceBeforeAssignmentOperators: true +SpaceBeforeCaseColon: false +SpaceBeforeCpp11BracedList: false +SpaceBeforeCtorInitializerColon: true +SpaceBeforeInheritanceColon: true +SpaceBeforeParens: ControlStatements +SpaceBeforeRangeBasedForLoopColon: true +SpaceBeforeSquareBrackets: false +SpaceInEmptyBlock: false +SpaceInEmptyParentheses: false +SpacesBeforeTrailingComments: 2 +SpacesInAngles: Never +SpacesInCStyleCastParentheses: false +SpacesInConditionalStatement: false +SpacesInContainerLiterals: true +SpacesInParentheses: false +SpacesInSquareBrackets: false + +# Sorting and grouping +SortIncludes: CaseSensitive +SortUsingDeclarations: true +IncludeBlocks: Preserve + +# Other formatting options +AccessModifierOffset: -1 +CompactNamespaces: false +ConstructorInitializerIndentWidth: 4 +Cpp11BracedListStyle: true +DeriveLineEnding: true +DerivePointerAlignment: true +EmptyLineAfterAccessModifier: Never +EmptyLineBeforeAccessModifier: LogicalBlock +FixNamespaceComments: true +KeepEmptyLinesAtTheStartOfBlocks: false +MaxEmptyLinesToKeep: 1 +NamespaceIndentation: None +PenaltyBreakAssignment: 2 +PenaltyBreakBeforeFirstCallParameter: 1 +PenaltyBreakComment: 300 +PenaltyBreakFirstLessLess: 120 +PenaltyBreakString: 1000 +PenaltyBreakTemplateDeclaration: 10 +PenaltyExcessCharacter: 1000000 +PenaltyReturnTypeOnItsOwnLine: 200 +PointerAlignment: Left +ReflowComments: true +SeparateDefinitionBlocks: Leave diff --git a/infer_engines/vllm b/infer_engines/vllm new file mode 160000 index 0000000000000000000000000000000000000000..58738772410c5e0d60b61db39538a9b313d2d7ad --- /dev/null +++ b/infer_engines/vllm @@ -0,0 +1 @@ +Subproject commit 58738772410c5e0d60b61db39538a9b313d2d7ad diff --git a/omni/accelerators/sched/global_proxy/lb_sdk/modules/ngx_http_upstream_greedy_timeout_module/ngx_http_upstream_greedy_timeout_module.c b/omni/accelerators/sched/global_proxy/lb_sdk/modules/ngx_http_upstream_greedy_timeout_module/ngx_http_upstream_greedy_timeout_module.c index 2106390205d170c11397c4edd680cfc4067f4429..24366c240246e8b57ef4986fa9ab09e151f69b81 100644 --- a/omni/accelerators/sched/global_proxy/lb_sdk/modules/ngx_http_upstream_greedy_timeout_module/ngx_http_upstream_greedy_timeout_module.c +++ b/omni/accelerators/sched/global_proxy/lb_sdk/modules/ngx_http_upstream_greedy_timeout_module/ngx_http_upstream_greedy_timeout_module.c @@ -1,11 +1,11 @@ // SPDX-License-Identifier: MIT // Copyright (c) 2025 Huawei Technologies Co., Ltd. All Rights Reserved. +#include #include #include #include #include -#include typedef struct { ngx_flag_t enable; @@ -45,7 +45,8 @@ static ngx_int_t ngx_http_gts_get_peer(ngx_peer_connection_t *pc, void *data); static void ngx_http_gts_free_peer(ngx_peer_connection_t *pc, void *data, ngx_uint_t state); static ngx_int_t ngx_http_gts_init_shm_zone(ngx_shm_zone_t *shm_zone, void *data); -static char *ngx_http_gts_set_flag(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) { +static char *ngx_http_gts_set_flag(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) +{ ngx_str_t *value = cf->args->elts; ngx_flag_t *fp = (ngx_flag_t *)((char *)conf + cmd->offset); *fp = ngx_atoi(value[1].data, value[1].len); @@ -53,57 +54,48 @@ static char *ngx_http_gts_set_flag(ngx_conf_t *cf, ngx_command_t *cmd, void *con } static ngx_command_t ngx_http_gts_commands[] = { - { ngx_string("greedy_timeout"), - NGX_HTTP_UPS_CONF|NGX_CONF_FLAG, - ngx_http_gts_set_flag, - NGX_HTTP_SRV_CONF_OFFSET, - offsetof(ngx_http_gts_conf_t, enable), - NULL }, - - { ngx_string("greedy_timeout_warmup"), - NGX_HTTP_UPS_CONF|NGX_CONF_TAKE1, - ngx_conf_set_num_slot, - NGX_HTTP_SRV_CONF_OFFSET, - offsetof(ngx_http_gts_conf_t, warmup), - NULL }, - - { ngx_string("greedy_timeout_exp"), - NGX_HTTP_UPS_CONF|NGX_CONF_TAKE1, - ngx_conf_set_double_slot, - NGX_HTTP_SRV_CONF_OFFSET, - offsetof(ngx_http_gts_conf_t, exp), - NULL }, - - { ngx_string("greedy_timeout_shm_size"), - NGX_HTTP_MAIN_CONF|NGX_CONF_TAKE1, - ngx_http_gts_set_shm_size, - 0, - 0, - NULL }, - - ngx_null_command -}; + {ngx_string("greedy_timeout"), + NGX_HTTP_UPS_CONF | NGX_CONF_FLAG, + ngx_http_gts_set_flag, + NGX_HTTP_SRV_CONF_OFFSET, + offsetof(ngx_http_gts_conf_t, enable), + NULL}, + + {ngx_string("greedy_timeout_warmup"), + NGX_HTTP_UPS_CONF | NGX_CONF_TAKE1, + ngx_conf_set_num_slot, + NGX_HTTP_SRV_CONF_OFFSET, + offsetof(ngx_http_gts_conf_t, warmup), + NULL}, + + {ngx_string("greedy_timeout_exp"), + NGX_HTTP_UPS_CONF | NGX_CONF_TAKE1, + ngx_conf_set_double_slot, + NGX_HTTP_SRV_CONF_OFFSET, + offsetof(ngx_http_gts_conf_t, exp), + NULL}, + + {ngx_string("greedy_timeout_shm_size"), NGX_HTTP_MAIN_CONF | NGX_CONF_TAKE1, ngx_http_gts_set_shm_size, 0, 0, NULL}, + + ngx_null_command}; static ngx_http_module_t ngx_http_gts_module_ctx = { - NULL, - ngx_http_gts_postconfig, - NULL, NULL, - ngx_http_gts_create_srv_conf, - ngx_http_gts_merge_srv_conf, - NULL, NULL -}; - -ngx_module_t ngx_http_upstream_greedy_timeout_module = { - NGX_MODULE_V1, - &ngx_http_gts_module_ctx, - ngx_http_gts_commands, - NGX_HTTP_MODULE, - NULL, NULL, NULL, NULL, NULL, NULL, NULL, - NGX_MODULE_V1_PADDING -}; - -static char * -ngx_conf_set_double_slot(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) + NULL, ngx_http_gts_postconfig, NULL, NULL, ngx_http_gts_create_srv_conf, ngx_http_gts_merge_srv_conf, NULL, NULL}; + +ngx_module_t ngx_http_upstream_greedy_timeout_module = {NGX_MODULE_V1, + &ngx_http_gts_module_ctx, + ngx_http_gts_commands, + NGX_HTTP_MODULE, + NULL, + NULL, + NULL, + NULL, + NULL, + NULL, + NULL, + NGX_MODULE_V1_PADDING}; + +static char *ngx_conf_set_double_slot(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) { ngx_str_t *value = cf->args->elts; double *dp = (double *)((char *)conf + cmd->offset); @@ -113,8 +105,7 @@ ngx_conf_set_double_slot(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) return NGX_CONF_OK; } -static char * -ngx_http_gts_set_shm_size(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) +static char *ngx_http_gts_set_shm_size(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) { ngx_str_t *value = cf->args->elts; ssize_t size = ngx_parse_size(&value[1]); @@ -124,10 +115,11 @@ ngx_http_gts_set_shm_size(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) } size = ngx_align(size, ngx_pagesize); if ((size_t)size < 8 * ngx_pagesize) { - ngx_conf_log_error(NGX_LOG_WARN, cf, 0, "greedy_timeout_shm_size must be at least %udKiB", (8 * ngx_pagesize) >> 10); + ngx_conf_log_error( + NGX_LOG_WARN, cf, 0, "greedy_timeout_shm_size must be at least %udKiB", (8 * ngx_pagesize) >> 10); size = 8 * ngx_pagesize; } - if (ngx_http_gts_shm_size && ngx_http_gts_shm_size != (ngx_uint_t) size) { + if (ngx_http_gts_shm_size && ngx_http_gts_shm_size != (ngx_uint_t)size) { ngx_conf_log_error(NGX_LOG_WARN, cf, 0, "Cannot change memory area size without restart, ignoring change"); } else { ngx_http_gts_shm_size = size; @@ -136,32 +128,29 @@ ngx_http_gts_set_shm_size(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) return NGX_CONF_OK; } -static void * -ngx_http_gts_create_srv_conf(ngx_conf_t *cf) +static void *ngx_http_gts_create_srv_conf(ngx_conf_t *cf) { ngx_http_gts_conf_t *conf = ngx_pcalloc(cf->pool, sizeof(*conf)); if (conf == NULL) { return NULL; } - conf->enable = 0; - conf->warmup = NGX_CONF_UNSET_UINT; - conf->exp = NGX_CONF_UNSET; + conf->enable = 0; + conf->warmup = NGX_CONF_UNSET_UINT; + conf->exp = NGX_CONF_UNSET; return conf; } -static char * -ngx_http_gts_merge_srv_conf(ngx_conf_t *cf, void *parent, void *child) +static char *ngx_http_gts_merge_srv_conf(ngx_conf_t *cf, void *parent, void *child) { ngx_http_gts_conf_t *prev = parent; ngx_http_gts_conf_t *conf = child; - ngx_conf_merge_value(conf->enable, prev->enable, 0); + ngx_conf_merge_value(conf->enable, prev->enable, 0); ngx_conf_merge_uint_value(conf->warmup, prev->warmup, 5); ngx_conf_merge_value(conf->exp, prev->exp, 1.8); return NGX_CONF_OK; } -static ngx_int_t -ngx_http_gts_init_shm_zone(ngx_shm_zone_t *shm_zone, void *data) +static ngx_int_t ngx_http_gts_init_shm_zone(ngx_shm_zone_t *shm_zone, void *data) { ngx_slab_pool_t *shpool; ngx_http_gts_shm_block_t *shm_block; @@ -179,7 +168,8 @@ ngx_http_gts_init_shm_zone(ngx_shm_zone_t *shm_zone, void *data) n = 512; size_t sz = sizeof(ngx_http_gts_shm_block_t) + (n - 1) * sizeof(ngx_http_gts_shm_processor_t); shm_block = ngx_slab_alloc(shpool, sz); - if (!shm_block) return NGX_ERROR; + if (!shm_block) + return NGX_ERROR; shm_block->warmup = 5; shm_block->exp = 1.8; @@ -192,8 +182,7 @@ ngx_http_gts_init_shm_zone(ngx_shm_zone_t *shm_zone, void *data) return NGX_OK; } -static ngx_int_t -ngx_http_gts_postconfig(ngx_conf_t *cf) +static ngx_int_t ngx_http_gts_postconfig(ngx_conf_t *cf) { ngx_http_upstream_main_conf_t *upcf; ngx_http_upstream_srv_conf_t **uscfp; @@ -208,8 +197,7 @@ ngx_http_gts_postconfig(ngx_conf_t *cf) uscfp = upcf->upstreams.elts; for (i = 0; i < upcf->upstreams.nelts; i++) { - conf = ngx_http_conf_upstream_srv_conf(uscfp[i], - ngx_http_upstream_greedy_timeout_module); + conf = ngx_http_conf_upstream_srv_conf(uscfp[i], ngx_http_upstream_greedy_timeout_module); if (!conf->enable) { continue; } @@ -220,9 +208,8 @@ ngx_http_gts_postconfig(ngx_conf_t *cf) } ngx_str_t name = ngx_string("greedy_timeout"); - ngx_http_gts_shm_zone = ngx_shared_memory_add( - cf, &name, ngx_http_gts_shm_size, - &ngx_http_upstream_greedy_timeout_module); + ngx_http_gts_shm_zone = + ngx_shared_memory_add(cf, &name, ngx_http_gts_shm_size, &ngx_http_upstream_greedy_timeout_module); if (ngx_http_gts_shm_zone == NULL) { return NGX_ERROR; } @@ -230,16 +217,13 @@ ngx_http_gts_postconfig(ngx_conf_t *cf) } uscfp[i]->peer.init = ngx_http_gts_upstream_init; - ngx_log_error(NGX_LOG_WARN, cf->log, 0, - "[GreedyTimeout] enabled on upstream[%ui]", i); + ngx_log_error(NGX_LOG_WARN, cf->log, 0, "[GreedyTimeout] enabled on upstream[%ui]", i); } return NGX_OK; } -static ngx_int_t -ngx_http_gts_upstream_init(ngx_http_request_t *r, - ngx_http_upstream_srv_conf_t *uscf) +static ngx_int_t ngx_http_gts_upstream_init(ngx_http_request_t *r, ngx_http_upstream_srv_conf_t *uscf) { ngx_http_upstream_t *u = r->upstream; ngx_http_upstream_rr_peer_data_t *rrp; @@ -262,17 +246,18 @@ ngx_http_gts_upstream_init(ngx_http_request_t *r, shpool = (ngx_slab_pool_t *)ngx_http_gts_shm_zone->shm.addr; n = rrp->peers->number; - if (n > gts_shm->p) n = gts_shm->p; + if (n > gts_shm->p) + n = gts_shm->p; ngx_shmtx_lock(&shpool->mutex); - z = (double) r->request_length; + z = (double)r->request_length; cost = gts_shm->warmup + pow(z, gts_shm->exp); earliest = gts_shm->procs[0].available_time; for (i = 1; i < n; i++) { if (gts_shm->procs[i].available_time < earliest) { earliest = gts_shm->procs[i].available_time; - chosen = i; + chosen = i; } } now = ngx_current_msec / 1000.0; @@ -283,24 +268,27 @@ ngx_http_gts_upstream_init(ngx_http_request_t *r, ngx_shmtx_unlock(&shpool->mutex); gdata = ngx_pcalloc(r->pool, sizeof(*gdata)); - gdata->rrp = rrp; + gdata->rrp = rrp; gdata->chosen = chosen; - u->peer.data = gdata; - u->peer.get = ngx_http_gts_get_peer; - u->peer.free = ngx_http_gts_free_peer; + u->peer.data = gdata; + u->peer.get = ngx_http_gts_get_peer; + u->peer.free = ngx_http_gts_free_peer; struct sockaddr_in *sin = (struct sockaddr_in *)rrp->peers->peer[chosen].sockaddr; ngx_uint_t port = ntohs(sin->sin_port); - ngx_log_error(NGX_LOG_WARN, r->connection->log, 0, - "[GreedyTimeout] assign request to peer #%ui (port=%ui), request_length=%O", - chosen, port, r->request_length); + ngx_log_error(NGX_LOG_WARN, + r->connection->log, + 0, + "[GreedyTimeout] assign request to peer #%ui (port=%ui), request_length=%O", + chosen, + port, + r->request_length); return NGX_OK; } -static ngx_int_t -ngx_http_gts_get_peer(ngx_peer_connection_t *pc, void *data) +static ngx_int_t ngx_http_gts_get_peer(ngx_peer_connection_t *pc, void *data) { ngx_http_gts_peer_data_t *gdata = data; ngx_http_upstream_rr_peer_data_t *rrp = gdata->rrp; @@ -314,16 +302,14 @@ ngx_http_gts_get_peer(ngx_peer_connection_t *pc, void *data) return NGX_BUSY; pc->sockaddr = peers->peer[idx].sockaddr; - pc->socklen = peers->peer[idx].socklen; - pc->name = &peers->peer[idx].name; + pc->socklen = peers->peer[idx].socklen; + pc->name = &peers->peer[idx].name; rrp->current = &peers->peer[idx]; - ngx_log_debug(NGX_LOG_DEBUG_HTTP, pc->log, 0, - "gts_get_peer: chosen=%ui addr=\"%V\"", idx, pc->name); + ngx_log_debug(NGX_LOG_DEBUG_HTTP, pc->log, 0, "gts_get_peer: chosen=%ui addr=\"%V\"", idx, pc->name); return NGX_OK; } -static void -ngx_http_gts_free_peer(ngx_peer_connection_t *pc, void *data, ngx_uint_t state) +static void ngx_http_gts_free_peer(ngx_peer_connection_t *pc, void *data, ngx_uint_t state) { ngx_http_gts_peer_data_t *gdata = data; ngx_http_upstream_rr_peer_data_t *rrp = gdata->rrp; diff --git a/omni/accelerators/sched/global_proxy/lb_sdk/modules/ngx_http_upstream_length_balance_module/ngx_http_upstream_length_balance_module.c b/omni/accelerators/sched/global_proxy/lb_sdk/modules/ngx_http_upstream_length_balance_module/ngx_http_upstream_length_balance_module.c index ac69ae9fb448d309250739d30a8e6d02f40fc4a5..f6d6c56c653f8b881e1894d8920cd18203031bf2 100644 --- a/omni/accelerators/sched/global_proxy/lb_sdk/modules/ngx_http_upstream_length_balance_module/ngx_http_upstream_length_balance_module.c +++ b/omni/accelerators/sched/global_proxy/lb_sdk/modules/ngx_http_upstream_length_balance_module/ngx_http_upstream_length_balance_module.c @@ -1,12 +1,12 @@ // SPDX-License-Identifier: MIT // Copyright (c) 2025 Huawei Technologies Co., Ltd. All Rights Reserved. +#include +#include #include #include #include #include -#include -#include #define NGX_DEFAULT_MAX_UPSTREAM_SERVERS 512 #define LENGTH_BALANCE_SHM_NAME "length_balance_shm" @@ -73,8 +73,7 @@ static ngx_int_t ngx_http_upstream_init_length_balance_peer(ngx_http_request_t * static ngx_int_t ngx_http_upstream_get_length_balance_peer(ngx_peer_connection_t *pc, void *data); static void ngx_http_upstream_free_length_balance_peer(ngx_peer_connection_t *pc, void *data, ngx_uint_t state); -static char * -ngx_conf_set_double_slot(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) +static char *ngx_conf_set_double_slot(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) { ngx_str_t *value = cf->args->elts; double *dp = (double *)((char *)conf + cmd->offset); @@ -83,60 +82,58 @@ ngx_conf_set_double_slot(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) } static ngx_command_t ngx_http_upstream_length_balance_commands[] = { - { ngx_string("length_balance"), - NGX_HTTP_UPS_CONF | NGX_CONF_NOARGS, - ngx_http_upstream_length_balance, - 0, - 0, - NULL }, - - { ngx_string("length_balance_merge_threshold"), - NGX_HTTP_UPS_CONF | NGX_CONF_TAKE1, - ngx_conf_set_num_slot, - NGX_HTTP_SRV_CONF_OFFSET, - offsetof(ngx_http_length_balance_conf_t, merge_threshold), - NULL }, - - { ngx_string("length_balance_req_len_weight"), - NGX_HTTP_UPS_CONF | NGX_CONF_TAKE1, - ngx_conf_set_double_slot, - NGX_HTTP_SRV_CONF_OFFSET, - offsetof(ngx_http_length_balance_conf_t, req_len_weight), - NULL }, - - { ngx_string("length_balance_decay_factor"), - NGX_HTTP_UPS_CONF | NGX_CONF_TAKE1, - ngx_conf_set_num_slot, - NGX_HTTP_SRV_CONF_OFFSET, - offsetof(ngx_http_length_balance_conf_t, decay_factor), - NULL }, - - ngx_null_command -}; - -static ngx_http_module_t ngx_http_upstream_length_balance_module_ctx = { - NULL, - NULL, - NULL, NULL, - ngx_http_length_balance_create_srv_conf, - ngx_http_length_balance_merge_srv_conf, - NULL, NULL -}; - -ngx_module_t ngx_http_upstream_length_balance_module = { - NGX_MODULE_V1, - &ngx_http_upstream_length_balance_module_ctx, - ngx_http_upstream_length_balance_commands, - NGX_HTTP_MODULE, - NULL, NULL, NULL, NULL, NULL, NULL, NULL, - NGX_MODULE_V1_PADDING -}; - -static void * -ngx_http_length_balance_create_srv_conf(ngx_conf_t *cf) + {ngx_string("length_balance"), NGX_HTTP_UPS_CONF | NGX_CONF_NOARGS, ngx_http_upstream_length_balance, 0, 0, NULL}, + + {ngx_string("length_balance_merge_threshold"), + NGX_HTTP_UPS_CONF | NGX_CONF_TAKE1, + ngx_conf_set_num_slot, + NGX_HTTP_SRV_CONF_OFFSET, + offsetof(ngx_http_length_balance_conf_t, merge_threshold), + NULL}, + + {ngx_string("length_balance_req_len_weight"), + NGX_HTTP_UPS_CONF | NGX_CONF_TAKE1, + ngx_conf_set_double_slot, + NGX_HTTP_SRV_CONF_OFFSET, + offsetof(ngx_http_length_balance_conf_t, req_len_weight), + NULL}, + + {ngx_string("length_balance_decay_factor"), + NGX_HTTP_UPS_CONF | NGX_CONF_TAKE1, + ngx_conf_set_num_slot, + NGX_HTTP_SRV_CONF_OFFSET, + offsetof(ngx_http_length_balance_conf_t, decay_factor), + NULL}, + + ngx_null_command}; + +static ngx_http_module_t ngx_http_upstream_length_balance_module_ctx = {NULL, + NULL, + NULL, + NULL, + ngx_http_length_balance_create_srv_conf, + ngx_http_length_balance_merge_srv_conf, + NULL, + NULL}; + +ngx_module_t ngx_http_upstream_length_balance_module = {NGX_MODULE_V1, + &ngx_http_upstream_length_balance_module_ctx, + ngx_http_upstream_length_balance_commands, + NGX_HTTP_MODULE, + NULL, + NULL, + NULL, + NULL, + NULL, + NULL, + NULL, + NGX_MODULE_V1_PADDING}; + +static void *ngx_http_length_balance_create_srv_conf(ngx_conf_t *cf) { ngx_http_length_balance_conf_t *conf = ngx_pcalloc(cf->pool, sizeof(*conf)); - if (conf == NULL) return NULL; + if (conf == NULL) + return NULL; conf->enable = NGX_CONF_UNSET; conf->merge_threshold = NGX_CONF_UNSET_UINT; conf->req_len_weight = NGX_CONF_UNSET; @@ -144,8 +141,7 @@ ngx_http_length_balance_create_srv_conf(ngx_conf_t *cf) return conf; } -static char * -ngx_http_length_balance_merge_srv_conf(ngx_conf_t *cf, void *parent, void *child) +static char *ngx_http_length_balance_merge_srv_conf(ngx_conf_t *cf, void *parent, void *child) { ngx_http_length_balance_conf_t *prev = parent; ngx_http_length_balance_conf_t *conf = child; @@ -156,8 +152,7 @@ ngx_http_length_balance_merge_srv_conf(ngx_conf_t *cf, void *parent, void *child return NGX_CONF_OK; } -static ngx_int_t -ngx_http_length_balance_init_shm_zone(ngx_shm_zone_t *shm_zone, void *data) +static ngx_int_t ngx_http_length_balance_init_shm_zone(ngx_shm_zone_t *shm_zone, void *data) { if (data) { shm_zone->data = data; @@ -170,11 +165,11 @@ ngx_http_length_balance_init_shm_zone(ngx_shm_zone_t *shm_zone, void *data) return NGX_OK; } -static char * -ngx_http_upstream_length_balance(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) +static char *ngx_http_upstream_length_balance(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) { if (length_balance_shm_zone == NULL) { - length_balance_shm_zone = ngx_shared_memory_add(cf, &length_balance_shm_name, SHM_SIZE, &ngx_http_upstream_length_balance_module); + length_balance_shm_zone = + ngx_shared_memory_add(cf, &length_balance_shm_name, SHM_SIZE, &ngx_http_upstream_length_balance_module); if (length_balance_shm_zone == NULL) { return NGX_CONF_ERROR; } @@ -188,8 +183,7 @@ ngx_http_upstream_length_balance(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) return NGX_CONF_OK; } -static ngx_int_t -ngx_http_upstream_init_length_balance(ngx_conf_t *cf, ngx_http_upstream_srv_conf_t *us) +static ngx_int_t ngx_http_upstream_init_length_balance(ngx_conf_t *cf, ngx_http_upstream_srv_conf_t *us) { ngx_uint_t i; ngx_uint_t n; @@ -197,8 +191,7 @@ ngx_http_upstream_init_length_balance(ngx_conf_t *cf, ngx_http_upstream_srv_conf ngx_http_length_peer_t *peer, **peerp; ngx_http_length_peers_t *peers; - ngx_http_length_balance_conf_t *conf = - ngx_http_conf_upstream_srv_conf(us, ngx_http_upstream_length_balance_module); + ngx_http_length_balance_conf_t *conf = ngx_http_conf_upstream_srv_conf(us, ngx_http_upstream_length_balance_module); if (us->servers == NULL) { ngx_log_error(NGX_LOG_EMERG, cf->log, 0, "No upstream servers defined"); @@ -234,20 +227,26 @@ ngx_http_upstream_init_length_balance(ngx_conf_t *cf, ngx_http_upstream_srv_conf peerp = &peers->peer; if (ngx_atomic_fetch_add(&next_shm_idx, 0) + n > SHM_SIZE / sizeof(ngx_http_length_stats_t)) { - ngx_log_error(NGX_LOG_EMERG, cf->log, 0, - "length_balance shared memory is too small for %ui servers of upstream \"%V\". Total servers %ui, available %D", - n, &us->host, ngx_atomic_fetch_add(&next_shm_idx, 0) + n, SHM_SIZE / sizeof(ngx_http_length_stats_t)); + ngx_log_error(NGX_LOG_EMERG, + cf->log, + 0, + "length_balance shared memory is too small for %ui servers of upstream \"%V\". Total servers " + "%ui, available %D", + n, + &us->host, + ngx_atomic_fetch_add(&next_shm_idx, 0) + n, + SHM_SIZE / sizeof(ngx_http_length_stats_t)); return NGX_ERROR; } peers->shm_base_idx = ngx_atomic_fetch_add(&next_shm_idx, n); for (i = 0; i < n; i++) { - peer[i].sockaddr = server[i].addrs[0].sockaddr; - peer[i].socklen = server[i].addrs[0].socklen; - peer[i].name = server[i].addrs[0].name; - peer[i].weight = server[i].weight; - peer[i].down = server[i].down; - peer[i].next = NULL; + peer[i].sockaddr = server[i].addrs[0].sockaddr; + peer[i].socklen = server[i].addrs[0].socklen; + peer[i].name = server[i].addrs[0].name; + peer[i].weight = server[i].weight; + peer[i].down = server[i].down; + peer[i].next = NULL; peer[i].stat_offset = i; *peerp = &peer[i]; peerp = &peer[i].next; @@ -263,8 +262,7 @@ ngx_http_upstream_init_length_balance(ngx_conf_t *cf, ngx_http_upstream_srv_conf return NGX_OK; } -static ngx_int_t -ngx_http_upstream_init_length_balance_peer(ngx_http_request_t *r, ngx_http_upstream_srv_conf_t *us) +static ngx_int_t ngx_http_upstream_init_length_balance_peer(ngx_http_request_t *r, ngx_http_upstream_srv_conf_t *us) { ngx_http_length_peer_data_t *lp; @@ -290,8 +288,7 @@ ngx_http_upstream_init_length_balance_peer(ngx_http_request_t *r, ngx_http_upstr return NGX_OK; } -static ngx_int_t -ngx_http_upstream_get_length_balance_peer(ngx_peer_connection_t *pc, void *data) +static ngx_int_t ngx_http_upstream_get_length_balance_peer(ngx_peer_connection_t *pc, void *data) { ngx_http_length_peer_data_t *lp = data; ngx_http_length_peer_t *peer, *min_peers[NGX_DEFAULT_MAX_UPSTREAM_SERVERS]; @@ -304,9 +301,13 @@ ngx_http_upstream_get_length_balance_peer(ngx_peer_connection_t *pc, void *data) u_char buf[32]; u_char *p = ngx_sprintf(buf, "%.3f", conf->req_len_weight); *p = '\0'; - ngx_log_error(NGX_LOG_WARN, lp->log, 0, - "[LengthBalance-Param] merge_threshold=%ui, req_len_weight=%s, decay_factor=%ui", - conf->merge_threshold, buf, conf->decay_factor); + ngx_log_error(NGX_LOG_WARN, + lp->log, + 0, + "[LengthBalance-Param] merge_threshold=%ui, req_len_weight=%s, decay_factor=%ui", + conf->merge_threshold, + buf, + conf->decay_factor); ngx_atomic_t req_len = (r && r->headers_in.content_length_n > 0) ? r->headers_in.content_length_n : 0; @@ -318,16 +319,21 @@ ngx_http_upstream_get_length_balance_peer(ngx_peer_connection_t *pc, void *data) ngx_uint_t global_idx = lp->peers->shm_base_idx + peer->stat_offset; if (global_idx >= SHM_SIZE / sizeof(ngx_http_length_stats_t)) { - ngx_log_error(NGX_LOG_EMERG, lp->log, 0, - "length_balance: global_idx %ui out of bounds (max %D) for peer \"%V\", upstream \"%V\"", - global_idx, SHM_SIZE / sizeof(ngx_http_length_stats_t), &peer->name, &lp->peers->name); + ngx_log_error(NGX_LOG_EMERG, + lp->log, + 0, + "length_balance: global_idx %ui out of bounds (max %D) for peer \"%V\", upstream \"%V\"", + global_idx, + SHM_SIZE / sizeof(ngx_http_length_stats_t), + &peer->name, + &lp->peers->name); continue; } ngx_http_length_stats_t *stat = &lp->stats[global_idx]; ngx_atomic_t current_requests = stat->total_requests + ngx_atomic_fetch_add(&lp->peers->local_requests[i], 0); - ngx_atomic_t current_length = stat->total_length + ngx_atomic_fetch_add(&lp->peers->local_lengths[i], 0); + ngx_atomic_t current_length = stat->total_length + ngx_atomic_fetch_add(&lp->peers->local_lengths[i], 0); double score = current_requests + current_length + req_len * conf->req_len_weight; @@ -344,16 +350,16 @@ ngx_http_upstream_get_length_balance_peer(ngx_peer_connection_t *pc, void *data) if (count == 0) { pc->name = &lp->peers->name; - ngx_log_error(NGX_LOG_ERR, lp->log, 0, - "length_balance: no active peers found for upstream \"%V\"", &lp->peers->name); + ngx_log_error( + NGX_LOG_ERR, lp->log, 0, "length_balance: no active peers found for upstream \"%V\"", &lp->peers->name); return NGX_BUSY; } ngx_uint_t rand_idx = ngx_random() % count; peer = min_peers[rand_idx]; pc->sockaddr = peer->sockaddr; - pc->socklen = peer->socklen; - pc->name = &peer->name; + pc->socklen = peer->socklen; + pc->name = &peer->name; lp->current = peer; ngx_uint_t p_local_idx = peer->stat_offset; @@ -361,9 +367,8 @@ ngx_http_upstream_get_length_balance_peer(ngx_peer_connection_t *pc, void *data) struct sockaddr_in *sin = (struct sockaddr_in *)peer->sockaddr; ngx_uint_t port = ntohs(sin->sin_port); - ngx_log_error(NGX_LOG_WARN, lp->log, 0, - "[Length Balance]: request assigned to port=%ui, request_length=%O", - port, req_len); + ngx_log_error( + NGX_LOG_WARN, lp->log, 0, "[Length Balance]: request assigned to port=%ui, request_length=%O", port, req_len); ngx_atomic_fetch_add(&lp->peers->local_requests[p_local_idx], 1); ngx_atomic_fetch_add(&lp->peers->local_lengths[p_local_idx], req_len); @@ -389,12 +394,18 @@ ngx_http_upstream_get_length_balance_peer(ngx_peer_connection_t *pc, void *data) ngx_atomic_t new_total_len = (old_total_len / conf->decay_factor) + len; ngx_atomic_cmp_set(&lp->stats[global_idx].total_requests, old_total_req, new_total_req); - ngx_atomic_cmp_set(&lp->stats[global_idx].total_length, old_total_len, new_total_len); + ngx_atomic_cmp_set(&lp->stats[global_idx].total_length, old_total_len, new_total_len); } else { - ngx_log_error(NGX_LOG_EMERG, lp->log, 0, - "length_balance: merge global_idx %ui out of bounds (max %D) for local_idx %ui, upstream \"%V\"", - global_idx, SHM_SIZE / sizeof(ngx_http_length_stats_t), i, &lp->peers->name); + ngx_log_error(NGX_LOG_EMERG, + lp->log, + 0, + "length_balance: merge global_idx %ui out of bounds (max %D) for local_idx %ui, " + "upstream \"%V\"", + global_idx, + SHM_SIZE / sizeof(ngx_http_length_stats_t), + i, + &lp->peers->name); } } } @@ -404,8 +415,7 @@ ngx_http_upstream_get_length_balance_peer(ngx_peer_connection_t *pc, void *data) return NGX_OK; } -static void -ngx_http_upstream_free_length_balance_peer(ngx_peer_connection_t *pc, void *data, ngx_uint_t state) +static void ngx_http_upstream_free_length_balance_peer(ngx_peer_connection_t *pc, void *data, ngx_uint_t state) { ngx_http_length_peer_data_t *lp = data; if (lp) { diff --git a/omni/accelerators/sched/global_proxy/lb_sdk/modules/ngx_http_upstream_prefill_score_balance_module/ngx_http_upstream_prefill_score_balance_module.c b/omni/accelerators/sched/global_proxy/lb_sdk/modules/ngx_http_upstream_prefill_score_balance_module/ngx_http_upstream_prefill_score_balance_module.c index 96582945ca5e33946bbf0cd0b42126bec02de349..c3095ed221b5a5499dec46054d086b83e2766927 100644 --- a/omni/accelerators/sched/global_proxy/lb_sdk/modules/ngx_http_upstream_prefill_score_balance_module/ngx_http_upstream_prefill_score_balance_module.c +++ b/omni/accelerators/sched/global_proxy/lb_sdk/modules/ngx_http_upstream_prefill_score_balance_module/ngx_http_upstream_prefill_score_balance_module.c @@ -2,15 +2,15 @@ // Copyright (c) 2025 Huawei Technologies Co., Ltd. All Rights Reserved. #include +#include #include #include #include #include -#include #include typedef struct { - ngx_flag_t enable; + ngx_flag_t enable; } ngx_http_prefill_score_conf_t; typedef struct { @@ -42,51 +42,53 @@ static ngx_int_t ngx_http_prefill_score_get_peer(ngx_peer_connection_t *pc, void static void ngx_http_prefill_score_free_peer(ngx_peer_connection_t *pc, void *data, ngx_uint_t state); static ngx_int_t ngx_http_prefill_score_init_shm_zone(ngx_shm_zone_t *shm_zone, void *data); -static char *ngx_http_prefill_score_set_flag(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) { +static char *ngx_http_prefill_score_set_flag(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) +{ ngx_str_t *value = cf->args->elts; ngx_flag_t *fp = (ngx_flag_t *)((char *)conf + cmd->offset); *fp = ngx_atoi(value[1].data, value[1].len); return NGX_CONF_OK; } -static ngx_command_t ngx_http_upstream_prefill_score_commands[] = { - { ngx_string("prefill_score_balance"), - NGX_HTTP_UPS_CONF | NGX_CONF_FLAG, - ngx_http_prefill_score_set_flag, - NGX_HTTP_SRV_CONF_OFFSET, - offsetof(ngx_http_prefill_score_conf_t, enable), - NULL }, - - { ngx_string("prefill_score_shm_size"), - NGX_HTTP_MAIN_CONF | NGX_CONF_TAKE1, - ngx_conf_set_size_slot, - 0, - 0, - &ngx_http_prefill_score_shm_size }, - - ngx_null_command -}; - -static ngx_http_module_t ngx_http_upstream_prefill_score_module_ctx = { - NULL, - ngx_http_prefill_score_postconfig, - NULL, NULL, - ngx_http_prefill_score_create_srv_conf, - ngx_http_prefill_score_merge_srv_conf, - NULL, NULL -}; - -ngx_module_t ngx_http_upstream_prefill_score_balance_module = { - NGX_MODULE_V1, - &ngx_http_upstream_prefill_score_module_ctx, - ngx_http_upstream_prefill_score_commands, - NGX_HTTP_MODULE, - NULL, NULL, NULL, NULL, NULL, NULL, NULL, - NGX_MODULE_V1_PADDING -}; - -static void * -ngx_http_prefill_score_create_srv_conf(ngx_conf_t *cf) +static ngx_command_t ngx_http_upstream_prefill_score_commands[] = {{ngx_string("prefill_score_balance"), + NGX_HTTP_UPS_CONF | NGX_CONF_FLAG, + ngx_http_prefill_score_set_flag, + NGX_HTTP_SRV_CONF_OFFSET, + offsetof(ngx_http_prefill_score_conf_t, enable), + NULL}, + + {ngx_string("prefill_score_shm_size"), + NGX_HTTP_MAIN_CONF | NGX_CONF_TAKE1, + ngx_conf_set_size_slot, + 0, + 0, + &ngx_http_prefill_score_shm_size}, + + ngx_null_command}; + +static ngx_http_module_t ngx_http_upstream_prefill_score_module_ctx = {NULL, + ngx_http_prefill_score_postconfig, + NULL, + NULL, + ngx_http_prefill_score_create_srv_conf, + ngx_http_prefill_score_merge_srv_conf, + NULL, + NULL}; + +ngx_module_t ngx_http_upstream_prefill_score_balance_module = {NGX_MODULE_V1, + &ngx_http_upstream_prefill_score_module_ctx, + ngx_http_upstream_prefill_score_commands, + NGX_HTTP_MODULE, + NULL, + NULL, + NULL, + NULL, + NULL, + NULL, + NULL, + NGX_MODULE_V1_PADDING}; + +static void *ngx_http_prefill_score_create_srv_conf(ngx_conf_t *cf) { ngx_http_prefill_score_conf_t *conf = ngx_pcalloc(cf->pool, sizeof(*conf)); if (conf == NULL) { @@ -96,15 +98,15 @@ ngx_http_prefill_score_create_srv_conf(ngx_conf_t *cf) return conf; } -static char* ngx_http_prefill_score_merge_srv_conf(ngx_conf_t* cf, void* parent, void* child) { - ngx_http_prefill_score_conf_t* prev = parent; - ngx_http_prefill_score_conf_t* conf = child; +static char *ngx_http_prefill_score_merge_srv_conf(ngx_conf_t *cf, void *parent, void *child) +{ + ngx_http_prefill_score_conf_t *prev = parent; + ngx_http_prefill_score_conf_t *conf = child; ngx_conf_merge_value(conf->enable, prev->enable, 0); return NGX_CONF_OK; } -static ngx_int_t -ngx_http_prefill_score_init_shm_zone(ngx_shm_zone_t *shm_zone, void *data) +static ngx_int_t ngx_http_prefill_score_init_shm_zone(ngx_shm_zone_t *shm_zone, void *data) { ngx_slab_pool_t *shpool; ngx_http_prefill_score_shm_block_t *shm_block; @@ -136,15 +138,13 @@ ngx_http_prefill_score_init_shm_zone(ngx_shm_zone_t *shm_zone, void *data) return NGX_OK; } -static double -ngx_http_prefill_score_time_cost(ngx_uint_t request_length) +static double ngx_http_prefill_score_time_cost(ngx_uint_t request_length) { double l = (double)request_length / 4.; return l * 0.0345 + 120.0745; } -static ngx_int_t -ngx_http_prefill_score_postconfig(ngx_conf_t *cf) +static ngx_int_t ngx_http_prefill_score_postconfig(ngx_conf_t *cf) { ngx_http_upstream_main_conf_t *upcf; ngx_http_upstream_srv_conf_t **uscfp; @@ -159,8 +159,7 @@ ngx_http_prefill_score_postconfig(ngx_conf_t *cf) uscfp = upcf->upstreams.elts; for (i = 0; i < upcf->upstreams.nelts; i++) { - conf = ngx_http_conf_upstream_srv_conf(uscfp[i], - ngx_http_upstream_prefill_score_balance_module); + conf = ngx_http_conf_upstream_srv_conf(uscfp[i], ngx_http_upstream_prefill_score_balance_module); if (!conf->enable) { continue; } @@ -172,8 +171,7 @@ ngx_http_prefill_score_postconfig(ngx_conf_t *cf) ngx_str_t name = ngx_string("prefill_score_balance"); ngx_http_prefill_score_shm_zone = ngx_shared_memory_add( - cf, &name, ngx_http_prefill_score_shm_size, - &ngx_http_upstream_prefill_score_balance_module); + cf, &name, ngx_http_prefill_score_shm_size, &ngx_http_upstream_prefill_score_balance_module); if (ngx_http_prefill_score_shm_zone == NULL) { return NGX_ERROR; } @@ -181,16 +179,13 @@ ngx_http_prefill_score_postconfig(ngx_conf_t *cf) } uscfp[i]->peer.init = ngx_http_prefill_score_upstream_init; - ngx_log_error(NGX_LOG_WARN, cf->log, 0, - "[PrefillScoreBalance] enabled on upstream[%ui]", i); + ngx_log_error(NGX_LOG_WARN, cf->log, 0, "[PrefillScoreBalance] enabled on upstream[%ui]", i); } return NGX_OK; } -static ngx_int_t -ngx_http_prefill_score_upstream_init(ngx_http_request_t *r, - ngx_http_upstream_srv_conf_t *uscf) +static ngx_int_t ngx_http_prefill_score_upstream_init(ngx_http_request_t *r, ngx_http_upstream_srv_conf_t *uscf) { ngx_http_upstream_t *u = r->upstream; ngx_http_upstream_rr_peer_data_t *rrp; @@ -226,15 +221,18 @@ ngx_http_prefill_score_upstream_init(ngx_http_request_t *r, chosen = 0; for (i = 0; i < n; i++) { - double peer_load = ((double)pfs_shm->peers[i].total_time_cost + my_time_cost) * ((double)pfs_shm->peers[i].active_requests + 1) - - ((double)pfs_shm->peers[i].total_time_cost) * ((double)pfs_shm->peers[i].active_requests); - - ngx_log_error(NGX_LOG_WARN, r->connection->log, 0, - "[PrefillScoreBalance] peer=%ui, active_requests=%uA, total_time_cost=%f, after_add=%f", - i, - pfs_shm->peers[i].active_requests, - (double)pfs_shm->peers[i].total_time_cost, - peer_load); + double peer_load = ((double)pfs_shm->peers[i].total_time_cost + my_time_cost) * + ((double)pfs_shm->peers[i].active_requests + 1) - + ((double)pfs_shm->peers[i].total_time_cost) * ((double)pfs_shm->peers[i].active_requests); + + ngx_log_error(NGX_LOG_WARN, + r->connection->log, + 0, + "[PrefillScoreBalance] peer=%ui, active_requests=%uA, total_time_cost=%f, after_add=%f", + i, + pfs_shm->peers[i].active_requests, + (double)pfs_shm->peers[i].total_time_cost, + peer_load); if (peer_load < min_load) { min_load = peer_load; @@ -245,9 +243,15 @@ ngx_http_prefill_score_upstream_init(ngx_http_request_t *r, ngx_atomic_fetch_add(&pfs_shm->peers[chosen].active_requests, 1); ngx_atomic_fetch_add(&pfs_shm->peers[chosen].total_time_cost, (ngx_atomic_int_t)my_time_cost); - ngx_log_error(NGX_LOG_WARN, r->connection->log, 0, + ngx_log_error( + NGX_LOG_WARN, + r->connection->log, + 0, "[PrefillScoreBalance] request assigned to peer=%ui, min_load=%f, request_time_cost=%.6f, request_length=%ui", - chosen, min_load, my_time_cost, (ngx_uint_t)r->request_length); + chosen, + min_load, + my_time_cost, + (ngx_uint_t)r->request_length); ngx_shmtx_unlock(&shpool->mutex); @@ -262,8 +266,7 @@ ngx_http_prefill_score_upstream_init(ngx_http_request_t *r, return NGX_OK; } -static ngx_int_t -ngx_http_prefill_score_get_peer(ngx_peer_connection_t *pc, void *data) +static ngx_int_t ngx_http_prefill_score_get_peer(ngx_peer_connection_t *pc, void *data) { ngx_http_prefill_score_peer_data_t *pdata = data; ngx_http_upstream_rr_peer_data_t *rrp = pdata->rrp; @@ -279,14 +282,13 @@ ngx_http_prefill_score_get_peer(ngx_peer_connection_t *pc, void *data) } pc->sockaddr = peers->peer[idx].sockaddr; - pc->socklen = peers->peer[idx].socklen; - pc->name = &peers->peer[idx].name; + pc->socklen = peers->peer[idx].socklen; + pc->name = &peers->peer[idx].name; rrp->current = &peers->peer[idx]; return NGX_OK; } -static void -ngx_http_prefill_score_free_peer(ngx_peer_connection_t *pc, void *data, ngx_uint_t state) +static void ngx_http_prefill_score_free_peer(ngx_peer_connection_t *pc, void *data, ngx_uint_t state) { ngx_http_prefill_score_peer_data_t *pdata = data; ngx_slab_pool_t *shpool; @@ -295,7 +297,7 @@ ngx_http_prefill_score_free_peer(ngx_peer_connection_t *pc, void *data, ngx_uint shpool = (ngx_slab_pool_t *)ngx_http_prefill_score_shm_zone->shm.addr; ngx_shmtx_lock(&shpool->mutex); ngx_atomic_fetch_add(&pfs_shm->peers[pdata->chosen].active_requests, (ngx_atomic_int_t)-1); - ngx_atomic_fetch_add(&pfs_shm->peers[pdata->chosen].total_time_cost, (ngx_atomic_int_t)-(pdata->my_time_cost)); + ngx_atomic_fetch_add(&pfs_shm->peers[pdata->chosen].total_time_cost, (ngx_atomic_int_t) - (pdata->my_time_cost)); ngx_shmtx_unlock(&shpool->mutex); ngx_http_upstream_rr_peer_data_t *rrp = pdata->rrp; diff --git a/omni/accelerators/sched/global_proxy/lb_sdk/modules/ngx_http_upstream_weighted_least_active_module/ngx_http_upstream_weighted_least_active_module.c b/omni/accelerators/sched/global_proxy/lb_sdk/modules/ngx_http_upstream_weighted_least_active_module/ngx_http_upstream_weighted_least_active_module.c index bc95a40a4af130ced707e72c162d957d6d30c595..aefc9457a2280b38f6627812cb61a952175b773e 100644 --- a/omni/accelerators/sched/global_proxy/lb_sdk/modules/ngx_http_upstream_weighted_least_active_module/ngx_http_upstream_weighted_least_active_module.c +++ b/omni/accelerators/sched/global_proxy/lb_sdk/modules/ngx_http_upstream_weighted_least_active_module/ngx_http_upstream_weighted_least_active_module.c @@ -1,11 +1,11 @@ // SPDX-License-Identifier: MIT // Copyright (c) 2025 Huawei Technologies Co., Ltd. All Rights Reserved. +#include #include #include #include #include -#include #include typedef struct { @@ -38,36 +38,42 @@ static void *ngx_http_weighted_least_active_create_srv_conf(ngx_conf_t *cf); static char *ngx_http_weighted_least_active_merge_srv_conf(ngx_conf_t *cf, void *parent, void *child); static ngx_int_t ngx_http_weighted_least_active_postconfig(ngx_conf_t *cf); -static ngx_int_t ngx_http_weighted_least_active_upstream_init(ngx_http_request_t *r, ngx_http_upstream_srv_conf_t *uscf); +static ngx_int_t ngx_http_weighted_least_active_upstream_init(ngx_http_request_t *r, + ngx_http_upstream_srv_conf_t *uscf); static ngx_int_t ngx_http_weighted_least_active_get_peer(ngx_peer_connection_t *pc, void *data); static void ngx_http_weighted_least_active_free_peer(ngx_peer_connection_t *pc, void *data, ngx_uint_t state); static ngx_int_t ngx_http_weighted_least_active_init_shm_zone(ngx_shm_zone_t *shm_zone, void *data); static ngx_int_t ngx_http_weighted_least_active_body_filter(ngx_http_request_t *r, ngx_chain_t *in); -void ngx_http_weighted_least_active_add_decoded_tokens(ngx_http_request_t *r, ngx_uint_t num_tokens) { +void ngx_http_weighted_least_active_add_decoded_tokens(ngx_http_request_t *r, ngx_uint_t num_tokens) +{ ngx_http_weighted_least_active_peer_data_t *pdata = r->upstream ? r->upstream->peer.data : NULL; ngx_slab_pool_t *shpool; if (wla_shm == NULL || pdata == NULL) { return; } - shpool = (ngx_slab_pool_t *) ngx_http_weighted_least_active_shm_zone->shm.addr; + shpool = (ngx_slab_pool_t *)ngx_http_weighted_least_active_shm_zone->shm.addr; ngx_shmtx_lock(&shpool->mutex); ngx_atomic_fetch_add(&wla_shm->peers[pdata->chosen].total_decode_num, (ngx_atomic_int_t)num_tokens); ngx_atomic_fetch_add(&pdata->decode_token_count, (ngx_atomic_int_t)num_tokens); - ngx_log_error(NGX_LOG_WARN, r->connection->log, 0, - "[WeightedLeastActive-Add] peer=%ui request=%p decode_token +%ui, peer_total_decode_token=%uA, request_total_decode_token=%uA", - pdata->chosen, - r, - num_tokens, - wla_shm->peers[pdata->chosen].total_decode_num, - pdata->decode_token_count); + ngx_log_error(NGX_LOG_WARN, + r->connection->log, + 0, + "[WeightedLeastActive-Add] peer=%ui request=%p decode_token +%ui, peer_total_decode_token=%uA, " + "request_total_decode_token=%uA", + pdata->chosen, + r, + num_tokens, + wla_shm->peers[pdata->chosen].total_decode_num, + pdata->decode_token_count); ngx_shmtx_unlock(&shpool->mutex); } -static char *ngx_http_weighted_least_active_set_flag(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) { +static char *ngx_http_weighted_least_active_set_flag(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) +{ ngx_str_t *value = cf->args->elts; ngx_flag_t *fp = (ngx_flag_t *)((char *)conf + cmd->offset); *fp = ngx_atoi(value[1].data, value[1].len); @@ -75,43 +81,45 @@ static char *ngx_http_weighted_least_active_set_flag(ngx_conf_t *cf, ngx_command } static ngx_command_t ngx_http_weighted_least_active_commands[] = { - { ngx_string("weighted_least_active"), - NGX_HTTP_UPS_CONF|NGX_CONF_FLAG, - ngx_http_weighted_least_active_set_flag, - NGX_HTTP_SRV_CONF_OFFSET, - offsetof(ngx_http_weighted_least_active_conf_t, enable), - NULL }, - - { ngx_string("weighted_least_active_shm_size"), - NGX_HTTP_MAIN_CONF|NGX_CONF_TAKE1, - ngx_conf_set_size_slot, - 0, - 0, - &ngx_http_weighted_least_active_shm_size }, - - ngx_null_command -}; - -static ngx_http_module_t ngx_http_weighted_least_active_module_ctx = { - NULL, - ngx_http_weighted_least_active_postconfig, - NULL, NULL, - ngx_http_weighted_least_active_create_srv_conf, - ngx_http_weighted_least_active_merge_srv_conf, - NULL, NULL -}; - -ngx_module_t ngx_http_upstream_weighted_least_active_module = { - NGX_MODULE_V1, - &ngx_http_weighted_least_active_module_ctx, - ngx_http_weighted_least_active_commands, - NGX_HTTP_MODULE, - NULL, NULL, NULL, NULL, NULL, NULL, NULL, - NGX_MODULE_V1_PADDING -}; - -static void * -ngx_http_weighted_least_active_create_srv_conf(ngx_conf_t *cf) + {ngx_string("weighted_least_active"), + NGX_HTTP_UPS_CONF | NGX_CONF_FLAG, + ngx_http_weighted_least_active_set_flag, + NGX_HTTP_SRV_CONF_OFFSET, + offsetof(ngx_http_weighted_least_active_conf_t, enable), + NULL}, + + {ngx_string("weighted_least_active_shm_size"), + NGX_HTTP_MAIN_CONF | NGX_CONF_TAKE1, + ngx_conf_set_size_slot, + 0, + 0, + &ngx_http_weighted_least_active_shm_size}, + + ngx_null_command}; + +static ngx_http_module_t ngx_http_weighted_least_active_module_ctx = {NULL, + ngx_http_weighted_least_active_postconfig, + NULL, + NULL, + ngx_http_weighted_least_active_create_srv_conf, + ngx_http_weighted_least_active_merge_srv_conf, + NULL, + NULL}; + +ngx_module_t ngx_http_upstream_weighted_least_active_module = {NGX_MODULE_V1, + &ngx_http_weighted_least_active_module_ctx, + ngx_http_weighted_least_active_commands, + NGX_HTTP_MODULE, + NULL, + NULL, + NULL, + NULL, + NULL, + NULL, + NULL, + NGX_MODULE_V1_PADDING}; + +static void *ngx_http_weighted_least_active_create_srv_conf(ngx_conf_t *cf) { ngx_http_weighted_least_active_conf_t *conf = ngx_pcalloc(cf->pool, sizeof(*conf)); if (conf == NULL) { @@ -121,8 +129,7 @@ ngx_http_weighted_least_active_create_srv_conf(ngx_conf_t *cf) return conf; } -static char * -ngx_http_weighted_least_active_merge_srv_conf(ngx_conf_t *cf, void *parent, void *child) +static char *ngx_http_weighted_least_active_merge_srv_conf(ngx_conf_t *cf, void *parent, void *child) { ngx_http_weighted_least_active_conf_t *prev = parent; ngx_http_weighted_least_active_conf_t *conf = child; @@ -130,12 +137,11 @@ ngx_http_weighted_least_active_merge_srv_conf(ngx_conf_t *cf, void *parent, void return NGX_CONF_OK; } -static ngx_int_t -ngx_http_weighted_least_active_init_shm_zone(ngx_shm_zone_t *shm_zone, void *data) +static ngx_int_t ngx_http_weighted_least_active_init_shm_zone(ngx_shm_zone_t *shm_zone, void *data) { ngx_slab_pool_t *shpool; ngx_http_weighted_least_active_shm_block_t *shm_block; - ngx_uint_t i; + ngx_uint_t i; ngx_uint_t n; if (data) { @@ -144,10 +150,11 @@ ngx_http_weighted_least_active_init_shm_zone(ngx_shm_zone_t *shm_zone, void *dat return NGX_OK; } - shpool = (ngx_slab_pool_t *) shm_zone->shm.addr; + shpool = (ngx_slab_pool_t *)shm_zone->shm.addr; n = 512; - size_t sz = sizeof(ngx_http_weighted_least_active_shm_block_t) + (n - 1) * sizeof(ngx_http_weighted_least_active_shm_peer_t); + size_t sz = sizeof(ngx_http_weighted_least_active_shm_block_t) + + (n - 1) * sizeof(ngx_http_weighted_least_active_shm_peer_t); shm_block = ngx_slab_alloc(shpool, sz); if (!shm_block) { return NGX_ERROR; @@ -163,8 +170,7 @@ ngx_http_weighted_least_active_init_shm_zone(ngx_shm_zone_t *shm_zone, void *dat return NGX_OK; } -static ngx_int_t -ngx_http_weighted_least_active_body_filter(ngx_http_request_t *r, ngx_chain_t *in) +static ngx_int_t ngx_http_weighted_least_active_body_filter(ngx_http_request_t *r, ngx_chain_t *in) { ngx_chain_t *cl; ngx_uint_t num_tokens = 0; @@ -232,17 +238,21 @@ ngx_http_weighted_least_active_body_filter(ngx_http_request_t *r, ngx_chain_t *i } if (num_tokens > 0) { - ngx_log_error(NGX_LOG_WARN, r->connection->log, 0, - "[WeightedLeastActive-Filter] chunk tokens: %ui (prompt=%ui, output=%ui, first=%d)", - num_tokens, prompt_tokens, output_tokens, is_first); + ngx_log_error(NGX_LOG_WARN, + r->connection->log, + 0, + "[WeightedLeastActive-Filter] chunk tokens: %ui (prompt=%ui, output=%ui, first=%d)", + num_tokens, + prompt_tokens, + output_tokens, + is_first); ngx_http_weighted_least_active_add_decoded_tokens(r, num_tokens); } return ngx_http_next_body_filter(r, in); } -static ngx_int_t -ngx_http_weighted_least_active_postconfig(ngx_conf_t *cf) +static ngx_int_t ngx_http_weighted_least_active_postconfig(ngx_conf_t *cf) { ngx_http_upstream_main_conf_t *upcf; ngx_http_upstream_srv_conf_t **uscfp; @@ -257,8 +267,7 @@ ngx_http_weighted_least_active_postconfig(ngx_conf_t *cf) uscfp = upcf->upstreams.elts; for (i = 0; i < upcf->upstreams.nelts; i++) { - conf = ngx_http_conf_upstream_srv_conf(uscfp[i], - ngx_http_upstream_weighted_least_active_module); + conf = ngx_http_conf_upstream_srv_conf(uscfp[i], ngx_http_upstream_weighted_least_active_module); if (!conf->enable) { continue; } @@ -273,8 +282,7 @@ ngx_http_weighted_least_active_postconfig(ngx_conf_t *cf) ngx_str_t name = ngx_string("weighted_least_active"); ngx_http_weighted_least_active_shm_zone = ngx_shared_memory_add( - cf, &name, ngx_http_weighted_least_active_shm_size, - &ngx_http_upstream_weighted_least_active_module); + cf, &name, ngx_http_weighted_least_active_shm_size, &ngx_http_upstream_weighted_least_active_module); if (ngx_http_weighted_least_active_shm_zone == NULL) { return NGX_ERROR; } @@ -282,16 +290,13 @@ ngx_http_weighted_least_active_postconfig(ngx_conf_t *cf) } uscfp[i]->peer.init = ngx_http_weighted_least_active_upstream_init; - ngx_log_error(NGX_LOG_WARN, cf->log, 0, - "[WeightedLeastActive] enabled on upstream[%ui]", i); + ngx_log_error(NGX_LOG_WARN, cf->log, 0, "[WeightedLeastActive] enabled on upstream[%ui]", i); } return NGX_OK; } -static ngx_int_t -ngx_http_weighted_least_active_upstream_init(ngx_http_request_t *r, - ngx_http_upstream_srv_conf_t *uscf) +static ngx_int_t ngx_http_weighted_least_active_upstream_init(ngx_http_request_t *r, ngx_http_upstream_srv_conf_t *uscf) { ngx_http_upstream_t *u = r->upstream; ngx_http_upstream_rr_peer_data_t *rrp; @@ -311,7 +316,7 @@ ngx_http_weighted_least_active_upstream_init(ngx_http_request_t *r, wla_shm = ngx_http_weighted_least_active_shm_zone->data; } - shpool = (ngx_slab_pool_t *) ngx_http_weighted_least_active_shm_zone->shm.addr; + shpool = (ngx_slab_pool_t *)ngx_http_weighted_least_active_shm_zone->shm.addr; n = rrp->peers->number; if (n > wla_shm->peer_count) { n = wla_shm->peer_count; @@ -358,14 +363,13 @@ ngx_http_weighted_least_active_upstream_init(ngx_http_request_t *r, u->peer.get = ngx_http_weighted_least_active_get_peer; u->peer.free = ngx_http_weighted_least_active_free_peer; - ngx_log_error(NGX_LOG_WARN, r->connection->log, 0, - "[WeightedLeastActive] assign request to peer #%ui", pdata->chosen); + ngx_log_error( + NGX_LOG_WARN, r->connection->log, 0, "[WeightedLeastActive] assign request to peer #%ui", pdata->chosen); return NGX_OK; } -static ngx_int_t -ngx_http_weighted_least_active_get_peer(ngx_peer_connection_t *pc, void *data) +static ngx_int_t ngx_http_weighted_least_active_get_peer(ngx_peer_connection_t *pc, void *data) { ngx_http_weighted_least_active_peer_data_t *pdata = data; ngx_http_upstream_rr_peer_data_t *rrp = pdata->rrp; @@ -387,18 +391,18 @@ ngx_http_weighted_least_active_get_peer(ngx_peer_connection_t *pc, void *data) return NGX_OK; } -static void -ngx_http_weighted_least_active_free_peer(ngx_peer_connection_t *pc, void *data, ngx_uint_t state) +static void ngx_http_weighted_least_active_free_peer(ngx_peer_connection_t *pc, void *data, ngx_uint_t state) { ngx_http_weighted_least_active_peer_data_t *pdata = data; ngx_slab_pool_t *shpool; if (wla_shm == NULL) { return; } - shpool = (ngx_slab_pool_t *) ngx_http_weighted_least_active_shm_zone->shm.addr; + shpool = (ngx_slab_pool_t *)ngx_http_weighted_least_active_shm_zone->shm.addr; ngx_shmtx_lock(&shpool->mutex); ngx_atomic_fetch_add(&wla_shm->peers[pdata->chosen].active_requests, (ngx_atomic_int_t)-1); - ngx_atomic_fetch_add(&wla_shm->peers[pdata->chosen].total_decode_num, (ngx_atomic_int_t)-(pdata->decode_token_count)); + ngx_atomic_fetch_add(&wla_shm->peers[pdata->chosen].total_decode_num, + (ngx_atomic_int_t) - (pdata->decode_token_count)); ngx_shmtx_unlock(&shpool->mutex); ngx_http_upstream_rr_peer_data_t *rrp = pdata->rrp; diff --git a/omni/accelerators/sched/global_proxy/modules/ngx_http_prefill_module/jsmn.h b/omni/accelerators/sched/global_proxy/modules/ngx_http_prefill_module/jsmn.h index e3cd63a59eafa7cba19b1668e75a1ca018cb852f..8f20b3694aeafddc932612cbf88532aac5ff4202 100644 --- a/omni/accelerators/sched/global_proxy/modules/ngx_http_prefill_module/jsmn.h +++ b/omni/accelerators/sched/global_proxy/modules/ngx_http_prefill_module/jsmn.h @@ -248,9 +248,9 @@ static int jsmn_parse_string( parser->pos++; for (i = 0; i < 4 && parser->pos < len && js[parser->pos] != '\0'; i++) { /* If it isn't a hex character we have an error */ - if (!((js[parser->pos] >= 48 && js[parser->pos] <= 57) || /* 0-9 */ - (js[parser->pos] >= 65 && js[parser->pos] <= 70) || /* A-F */ - (js[parser->pos] >= 97 && js[parser->pos] <= 102))) { /* a-f */ + if (!((js[parser->pos] >= 48 && js[parser->pos] <= 57) || /* 0-9 */ + (js[parser->pos] >= 65 && js[parser->pos] <= 70) || /* A-F */ + (js[parser->pos] >= 97 && js[parser->pos] <= 102))) { /* a-f */ parser->pos = start; return JSMN_ERROR_INVAL; } diff --git a/omni/accelerators/sched/global_proxy/modules/ngx_http_prefill_module/ngx_http_prefill_module.c b/omni/accelerators/sched/global_proxy/modules/ngx_http_prefill_module/ngx_http_prefill_module.c index d487f0e5cad25c2862ad58c4142c73aed959ad3d..69288b52313a38ae843ad14e7b61c15388ec4725 100644 --- a/omni/accelerators/sched/global_proxy/modules/ngx_http_prefill_module/ngx_http_prefill_module.c +++ b/omni/accelerators/sched/global_proxy/modules/ngx_http_prefill_module/ngx_http_prefill_module.c @@ -19,11 +19,11 @@ static ngx_int_t ngx_http_prefill_init(ngx_conf_t *cf); static ngx_command_t ngx_http_prefill_commands[] = { {ngx_string("prefill"), - NGX_HTTP_MAIN_CONF | NGX_HTTP_SRV_CONF | NGX_HTTP_LOC_CONF | NGX_CONF_TAKE1, - ngx_conf_set_str_slot, - NGX_HTTP_LOC_CONF_OFFSET, - offsetof(ngx_http_prefill_loc_conf_t, prefill_location), - NULL}, + NGX_HTTP_MAIN_CONF | NGX_HTTP_SRV_CONF | NGX_HTTP_LOC_CONF | NGX_CONF_TAKE1, + ngx_conf_set_str_slot, + NGX_HTTP_LOC_CONF_OFFSET, + offsetof(ngx_http_prefill_loc_conf_t, prefill_location), + NULL}, ngx_null_command}; @@ -42,17 +42,17 @@ static ngx_http_module_t ngx_http_prefill_module_ctx = { }; ngx_module_t ngx_http_prefill_module = {NGX_MODULE_V1, - &ngx_http_prefill_module_ctx, /* module context */ - ngx_http_prefill_commands, /* module directives */ - NGX_HTTP_MODULE, /* module type */ - NULL, /* init master */ - NULL, /* init module */ - NULL, /* init process */ - NULL, /* init thread */ - NULL, /* exit thread */ - NULL, /* exit process */ - NULL, /* exit master */ - NGX_MODULE_V1_PADDING}; + &ngx_http_prefill_module_ctx, /* module context */ + ngx_http_prefill_commands, /* module directives */ + NGX_HTTP_MODULE, /* module type */ + NULL, /* init master */ + NULL, /* init module */ + NULL, /* init process */ + NULL, /* init thread */ + NULL, /* exit thread */ + NULL, /* exit process */ + NULL, /* exit master */ + NGX_MODULE_V1_PADDING}; typedef struct { ngx_uint_t done; @@ -124,11 +124,11 @@ static void ngx_http_gen_decode_request_body(ngx_http_request_t *r, ngx_http_pre } ngx_log_debug2(NGX_LOG_DEBUG_HTTP, - r->connection->log, - 0, - "gen decode request: prefill response body: %d %s", - ctx->prefill_response_body_size, - ctx->prefill_response_body); + r->connection->log, + 0, + "gen decode request: prefill response body: %d %s", + ctx->prefill_response_body_size, + ctx->prefill_response_body); jsmn_init(&parser); int prefill_tokens_size = @@ -175,10 +175,10 @@ static void ngx_http_gen_decode_request_body(ngx_http_request_t *r, ngx_http_pre r, (char *)(ctx->prefill_response_body), tokens, prefill_tokens_size, prefill_response_json_keys[i]); if (key_idx == -1) { ngx_log_debug1(NGX_LOG_DEBUG_HTTP, - r->connection->log, - 0, - "gen decode request: key not found %s", - prefill_response_json_keys[i]); + r->connection->log, + 0, + "gen decode request: key not found %s", + prefill_response_json_keys[i]); continue; } @@ -272,11 +272,11 @@ static void ngx_http_gen_decode_request_body(ngx_http_request_t *r, ngx_http_pre } ngx_log_debug2(NGX_LOG_DEBUG_HTTP, - r->connection->log, - 0, - "gen decode request: body for subrequest: %d %s", - total_len, - json_str); + r->connection->log, + 0, + "gen decode request: body for subrequest: %d %s", + total_len, + json_str); #endif // Set content length in header @@ -433,8 +433,7 @@ void gen_prefill_json_str_jsmn( size_t so_key_start = tokens[stream_options_key_idx].start; size_t so_val_end = tokens[stream_options_val_idx].end; size_t j = so_key_start; - while (j > 0 && json[j - 1] != ',') - j--; + while (j > 0 && json[j - 1] != ',') j--; if (j > 0 && json[j - 1] == ',') so_key_start = j - 1; region_infos[region_infos_count++] = @@ -512,8 +511,9 @@ void gen_prefill_json_str_jsmn( return; } -static ngx_int_t ngx_http_gen_prefill_request_body( - ngx_http_request_t *r, ngx_http_request_t *sr, ngx_http_prefill_ctx_t *ctx) +static ngx_int_t ngx_http_gen_prefill_request_body(ngx_http_request_t *r, + ngx_http_request_t *sr, + ngx_http_prefill_ctx_t *ctx) { ngx_chain_t *cl; size_t len = 0; @@ -563,11 +563,11 @@ static ngx_int_t ngx_http_gen_prefill_request_body( ssize_t n = ngx_read_file(buf->file, p, buf_size, buf->file_pos); if (n != (ssize_t)buf_size) { ngx_log_error(NGX_LOG_ERR, - r->connection->log, - 0, - "prefill: failed to read body from file, expected %uz, got %z", - buf_size, - n); + r->connection->log, + 0, + "prefill: failed to read body from file, expected %uz, got %z", + buf_size, + n); return NGX_ERROR; } p += buf_size; @@ -591,11 +591,11 @@ static ngx_int_t ngx_http_gen_prefill_request_body( } ngx_log_debug2(NGX_LOG_DEBUG_HTTP, - r->connection->log, - 0, - "prefill: modified body for subrequest: %d, %s", - str_len, - modified_json_str); + r->connection->log, + 0, + "prefill: modified body for subrequest: %d, %s", + str_len, + modified_json_str); b = ngx_pcalloc(r->pool, sizeof(ngx_buf_t)); if (b == NULL) { diff --git a/omni/accelerators/sched/global_proxy/modules/ngx_http_set_request_id_module/ngx_http_set_request_id_module.c b/omni/accelerators/sched/global_proxy/modules/ngx_http_set_request_id_module/ngx_http_set_request_id_module.c index b79ef1f4f71a2bae81494346f97e6dd107be24b4..ef88e01496bc59dba159f1543a43e22b7bcd36da 100644 --- a/omni/accelerators/sched/global_proxy/modules/ngx_http_set_request_id_module/ngx_http_set_request_id_module.c +++ b/omni/accelerators/sched/global_proxy/modules/ngx_http_set_request_id_module/ngx_http_set_request_id_module.c @@ -120,14 +120,14 @@ static ngx_http_module_t ngx_http_set_request_id_module_ctx = { }; ngx_module_t ngx_http_set_request_id_module = {NGX_MODULE_V1, - &ngx_http_set_request_id_module_ctx, // Module context - NULL, // Module instructions - NGX_HTTP_MODULE, // Module type - NULL, // init master - NULL, // init module - NULL, // init process - NULL, // init thread - NULL, // exit thread - NULL, // exit process - NULL, // exit master - NGX_MODULE_V1_PADDING}; \ No newline at end of file + &ngx_http_set_request_id_module_ctx, // Module context + NULL, // Module instructions + NGX_HTTP_MODULE, // Module type + NULL, // init master + NULL, // init module + NULL, // init process + NULL, // init thread + NULL, // exit thread + NULL, // exit process + NULL, // exit master + NGX_MODULE_V1_PADDING}; \ No newline at end of file diff --git a/omni/accelerators/sched/global_proxy/modules/ngx_http_upstream_auto_balance_controller_module/ngx_http_upstream_auto_balance_controller_module.c b/omni/accelerators/sched/global_proxy/modules/ngx_http_upstream_auto_balance_controller_module/ngx_http_upstream_auto_balance_controller_module.c index bf2de85234515c0e912c5217652f9817cc12a08e..cb647ff68a411cc7a32d6d79798f8dcd65a4e7c6 100644 --- a/omni/accelerators/sched/global_proxy/modules/ngx_http_upstream_auto_balance_controller_module/ngx_http_upstream_auto_balance_controller_module.c +++ b/omni/accelerators/sched/global_proxy/modules/ngx_http_upstream_auto_balance_controller_module/ngx_http_upstream_auto_balance_controller_module.c @@ -1,14 +1,13 @@ // SPDX-License-Identifier: MIT // Copyright (c) 2025 Huawei Technologies Co., Ltd. All Rights Reserved. +#include #include #include #include #include -#include #include - typedef struct { ngx_flag_t enable; ngx_uint_t auto_balance_controller_batch_size; @@ -33,37 +32,30 @@ typedef struct { static ngx_shm_zone_t *ngx_http_auto_balance_controller_shm_zone = NULL; static ngx_uint_t ngx_http_auto_balance_controller_shm_size = 0; static ngx_uint_t ngx_http_auto_balance_controller_batch_size = 24; -static ngx_http_auto_balance_controller_shm_block_t - *auto_balance_controller_shm = NULL; +static ngx_http_auto_balance_controller_shm_block_t *auto_balance_controller_shm = NULL; static void *ngx_http_auto_balance_controller_create_srv_conf(ngx_conf_t *cf); static ngx_int_t ngx_http_auto_balance_controller_postconfig(ngx_conf_t *cf); -static ngx_int_t ngx_http_auto_balance_controller_upstream_init( - ngx_http_request_t *r, ngx_http_upstream_srv_conf_t *uscf); -static ngx_int_t -ngx_http_auto_balance_controller_get_peer(ngx_peer_connection_t *pc, - void *data); -static void -ngx_http_auto_balance_controller_free_peer(ngx_peer_connection_t *pc, - void *data, ngx_uint_t state); -static ngx_int_t -ngx_http_auto_balance_controller_init_shm_zone(ngx_shm_zone_t *shm_zone, - void *data); +static ngx_int_t ngx_http_auto_balance_controller_upstream_init(ngx_http_request_t *r, + ngx_http_upstream_srv_conf_t *uscf); +static ngx_int_t ngx_http_auto_balance_controller_get_peer(ngx_peer_connection_t *pc, void *data); +static void ngx_http_auto_balance_controller_free_peer(ngx_peer_connection_t *pc, void *data, ngx_uint_t state); +static ngx_int_t ngx_http_auto_balance_controller_init_shm_zone(ngx_shm_zone_t *shm_zone, void *data); static ngx_command_t ngx_http_auto_balance_controller_commands[] = { {ngx_string("auto_balance_controller"), NGX_HTTP_UPS_CONF | NGX_CONF_FLAG, - ngx_conf_set_flag_slot, NGX_HTTP_SRV_CONF_OFFSET, - offsetof(ngx_http_auto_balance_controller_conf_t, enable), + ngx_conf_set_flag_slot, + NGX_HTTP_SRV_CONF_OFFSET, + offsetof(ngx_http_auto_balance_controller_conf_t, enable), NULL}, {ngx_string("auto_balance_controller_batch_size"), NGX_HTTP_UPS_CONF | NGX_CONF_TAKE1, ngx_conf_set_num_slot, NGX_HTTP_SRV_CONF_OFFSET, - offsetof(ngx_http_auto_balance_controller_conf_t, - auto_balance_controller_batch_size), + offsetof(ngx_http_auto_balance_controller_conf_t, auto_balance_controller_batch_size), NULL}, ngx_null_command}; @@ -77,24 +69,22 @@ static ngx_http_module_t ngx_http_auto_balance_controller_module_ctx = { NULL, NULL}; -ngx_module_t ngx_http_upstream_auto_balance_controller_module = { - NGX_MODULE_V1, - &ngx_http_auto_balance_controller_module_ctx, - ngx_http_auto_balance_controller_commands, - NGX_HTTP_MODULE, - NULL, - NULL, - NULL, - NULL, - NULL, - NULL, - NULL, - NGX_MODULE_V1_PADDING}; +ngx_module_t ngx_http_upstream_auto_balance_controller_module = {NGX_MODULE_V1, + &ngx_http_auto_balance_controller_module_ctx, + ngx_http_auto_balance_controller_commands, + NGX_HTTP_MODULE, + NULL, + NULL, + NULL, + NULL, + NULL, + NULL, + NULL, + NGX_MODULE_V1_PADDING}; static void *ngx_http_auto_balance_controller_create_srv_conf(ngx_conf_t *cf) { - ngx_http_auto_balance_controller_conf_t *conf = - ngx_pcalloc(cf->pool, sizeof(*conf)); + ngx_http_auto_balance_controller_conf_t *conf = ngx_pcalloc(cf->pool, sizeof(*conf)); if (conf == NULL) { return NULL; } @@ -103,9 +93,7 @@ static void *ngx_http_auto_balance_controller_create_srv_conf(ngx_conf_t *cf) return conf; } -static ngx_int_t -ngx_http_auto_balance_controller_init_shm_zone(ngx_shm_zone_t *shm_zone, - void *data) +static ngx_int_t ngx_http_auto_balance_controller_init_shm_zone(ngx_shm_zone_t *shm_zone, void *data) { ngx_slab_pool_t *shpool; ngx_http_auto_balance_controller_shm_block_t *shm_block; @@ -152,14 +140,12 @@ static ngx_int_t ngx_http_auto_balance_controller_postconfig(ngx_conf_t *cf) shm_name->len = sizeof("auto_balance_controller") - 1; shm_name->data = (u_char *)"auto_balance_controller"; ngx_http_auto_balance_controller_shm_zone = ngx_shared_memory_add( - cf, shm_name, ngx_http_auto_balance_controller_shm_size, - &ngx_http_upstream_auto_balance_controller_module); + cf, shm_name, ngx_http_auto_balance_controller_shm_size, &ngx_http_upstream_auto_balance_controller_module); if (ngx_http_auto_balance_controller_shm_zone == NULL) { ngx_pfree(cf->pool, shm_name); return NGX_ERROR; } - ngx_http_auto_balance_controller_shm_zone->init = - ngx_http_auto_balance_controller_init_shm_zone; + ngx_http_auto_balance_controller_shm_zone->init = ngx_http_auto_balance_controller_init_shm_zone; ngx_http_upstream_main_conf_t *upcf; ngx_http_upstream_srv_conf_t **uscfp; @@ -172,23 +158,19 @@ static ngx_int_t ngx_http_auto_balance_controller_postconfig(ngx_conf_t *cf) } uscfp = upcf->upstreams.elts; for (i = 0; i < upcf->upstreams.nelts; i++) { - conf = ngx_http_conf_upstream_srv_conf( - uscfp[i], ngx_http_upstream_auto_balance_controller_module); + conf = ngx_http_conf_upstream_srv_conf(uscfp[i], ngx_http_upstream_auto_balance_controller_module); if (conf->enable == 1) { - uscfp[i]->peer.init = - ngx_http_auto_balance_controller_upstream_init; - if (conf->auto_balance_controller_batch_size != - NGX_CONF_UNSET_UINT) { - ngx_http_auto_balance_controller_batch_size = - conf->auto_balance_controller_batch_size; + uscfp[i]->peer.init = ngx_http_auto_balance_controller_upstream_init; + if (conf->auto_balance_controller_batch_size != NGX_CONF_UNSET_UINT) { + ngx_http_auto_balance_controller_batch_size = conf->auto_balance_controller_batch_size; } } } return NGX_OK; } -static ngx_int_t ngx_http_auto_balance_controller_upstream_init( - ngx_http_request_t *r, ngx_http_upstream_srv_conf_t *uscf) +static ngx_int_t ngx_http_auto_balance_controller_upstream_init(ngx_http_request_t *r, + ngx_http_upstream_srv_conf_t *uscf) { ngx_http_upstream_t *u = r->upstream; ngx_http_upstream_rr_peer_data_t *rrp; @@ -203,8 +185,7 @@ static ngx_int_t ngx_http_auto_balance_controller_upstream_init( rrp = u->peer.data; if (auto_balance_controller_shm == NULL) { - auto_balance_controller_shm = - ngx_http_auto_balance_controller_shm_zone->data; + auto_balance_controller_shm = ngx_http_auto_balance_controller_shm_zone->data; } n = rrp->peers->number; @@ -219,14 +200,11 @@ static ngx_int_t ngx_http_auto_balance_controller_upstream_init( ngx_uint_t active_requests[n]; ngx_uint_t total_request_length[n]; ngx_slab_pool_t *shpool; - shpool = - (ngx_slab_pool_t *)ngx_http_auto_balance_controller_shm_zone->shm.addr; + shpool = (ngx_slab_pool_t *)ngx_http_auto_balance_controller_shm_zone->shm.addr; ngx_shmtx_lock(&shpool->mutex); for (i = 0; i < n; i++) { - active_requests[i] = ngx_atomic_fetch_add( - &auto_balance_controller_shm->peers[i].active_requests, 0); - total_request_length[i] = ngx_atomic_fetch_add( - &auto_balance_controller_shm->peers[i].total_request_length, 0); + active_requests[i] = ngx_atomic_fetch_add(&auto_balance_controller_shm->peers[i].active_requests, 0); + total_request_length[i] = ngx_atomic_fetch_add(&auto_balance_controller_shm->peers[i].total_request_length, 0); } ngx_shmtx_unlock(&shpool->mutex); ngx_uint_t all_overload = 1; @@ -241,8 +219,7 @@ static ngx_int_t ngx_http_auto_balance_controller_upstream_init( if (all_overload) { ngx_uint_t min_active_request = 0; for (i = 0; i < n; i++) { - if (candidate_count == 0 || - active_requests[i] < min_active_request) { + if (candidate_count == 0 || active_requests[i] < min_active_request) { candidate_count = 0; min_active_request = active_requests[i]; candidate[candidate_count++] = i; @@ -253,12 +230,10 @@ static ngx_int_t ngx_http_auto_balance_controller_upstream_init( } else { ngx_uint_t min_total_request_length = 0; for (i = 0; i < n; i++) { - if (active_requests[i] >= - ngx_http_auto_balance_controller_batch_size) { + if (active_requests[i] >= ngx_http_auto_balance_controller_batch_size) { continue; } - if (candidate_count == 0 || - total_request_length[i] < min_total_request_length) { + if (candidate_count == 0 || total_request_length[i] < min_total_request_length) { candidate_count = 0; min_total_request_length = total_request_length[i]; candidate[candidate_count++] = i; @@ -270,11 +245,9 @@ static ngx_int_t ngx_http_auto_balance_controller_upstream_init( ngx_uint_t rand_idx = ngx_random() % candidate_count; chosen = candidate[rand_idx]; - ngx_atomic_fetch_add( - &auto_balance_controller_shm->peers[chosen].active_requests, 1); - ngx_atomic_fetch_add( - &auto_balance_controller_shm->peers[chosen].total_request_length, - (ngx_atomic_int_t)r->request_length); + ngx_atomic_fetch_add(&auto_balance_controller_shm->peers[chosen].active_requests, 1); + ngx_atomic_fetch_add(&auto_balance_controller_shm->peers[chosen].total_request_length, + (ngx_atomic_int_t)r->request_length); pdata = ngx_pcalloc(r->pool, sizeof(*pdata)); if (pdata == NULL) { @@ -287,16 +260,17 @@ static ngx_int_t ngx_http_auto_balance_controller_upstream_init( u->peer.get = ngx_http_auto_balance_controller_get_peer; u->peer.free = ngx_http_auto_balance_controller_free_peer; - ngx_log_error( - NGX_LOG_WARN, r->connection->log, 0, - "[auto_balance_controller] assign request(len=%ui) to peer #%ui", - pdata->request_length, pdata->chosen); + ngx_log_error(NGX_LOG_WARN, + r->connection->log, + 0, + "[auto_balance_controller] assign request(len=%ui) to peer #%ui", + pdata->request_length, + pdata->chosen); return NGX_OK; } -static ngx_int_t -ngx_http_auto_balance_controller_get_peer(ngx_peer_connection_t *pc, void *data) +static ngx_int_t ngx_http_auto_balance_controller_get_peer(ngx_peer_connection_t *pc, void *data) { ngx_http_auto_balance_controller_peer_data_t *pdata = data; ngx_http_upstream_rr_peer_data_t *rrp = pdata->rrp; @@ -318,24 +292,18 @@ ngx_http_auto_balance_controller_get_peer(ngx_peer_connection_t *pc, void *data) return NGX_OK; } -static void -ngx_http_auto_balance_controller_free_peer(ngx_peer_connection_t *pc, - void *data, ngx_uint_t state) +static void ngx_http_auto_balance_controller_free_peer(ngx_peer_connection_t *pc, void *data, ngx_uint_t state) { ngx_http_auto_balance_controller_peer_data_t *pdata = data; if (auto_balance_controller_shm == NULL) { return; } ngx_slab_pool_t *shpool; - shpool = - (ngx_slab_pool_t *)ngx_http_auto_balance_controller_shm_zone->shm.addr; + shpool = (ngx_slab_pool_t *)ngx_http_auto_balance_controller_shm_zone->shm.addr; ngx_shmtx_lock(&shpool->mutex); - ngx_atomic_fetch_add( - &auto_balance_controller_shm->peers[pdata->chosen].active_requests, - (ngx_atomic_int_t)-1); - ngx_atomic_fetch_add( - &auto_balance_controller_shm->peers[pdata->chosen].total_request_length, - (ngx_atomic_int_t) - (pdata->request_length)); + ngx_atomic_fetch_add(&auto_balance_controller_shm->peers[pdata->chosen].active_requests, (ngx_atomic_int_t)-1); + ngx_atomic_fetch_add(&auto_balance_controller_shm->peers[pdata->chosen].total_request_length, + (ngx_atomic_int_t) - (pdata->request_length)); ngx_shmtx_unlock(&shpool->mutex); ngx_http_upstream_rr_peer_data_t *rrp = pdata->rrp; diff --git a/omni/accelerators/sched/global_proxy/modules/ngx_http_upstream_least_total_load_module/ngx_http_upstream_least_total_load_module.c b/omni/accelerators/sched/global_proxy/modules/ngx_http_upstream_least_total_load_module/ngx_http_upstream_least_total_load_module.c index 1d5a36e1effc3df1b58b80f2b80b34db032bf8ab..a6708ad1886140b0ce903fee7bb5f0b2e4033cc0 100644 --- a/omni/accelerators/sched/global_proxy/modules/ngx_http_upstream_least_total_load_module/ngx_http_upstream_least_total_load_module.c +++ b/omni/accelerators/sched/global_proxy/modules/ngx_http_upstream_least_total_load_module/ngx_http_upstream_least_total_load_module.c @@ -1,16 +1,14 @@ // SPDX-License-Identifier: MIT // Copyright (c) 2025 Huawei Technologies Co., Ltd. All Rights Reserved. - #include +#include +#include #include #include #include #include -#include #include -#include - typedef struct { ngx_flag_t enable; @@ -39,15 +37,12 @@ static prefill_upstream_info_t *prefill_shm = NULL; static ngx_uint_t ngx_http_least_total_load_shm_size = 0; static ngx_uint_t ngx_http_least_total_load_batch_size = 0; -static ngx_int_t ngx_http_least_total_load_get_peer(ngx_peer_connection_t *pc, - void *data); -static void ngx_http_least_total_load_free_peer(ngx_peer_connection_t *pc, - void *data, ngx_uint_t state); +static ngx_int_t ngx_http_least_total_load_get_peer(ngx_peer_connection_t *pc, void *data); +static void ngx_http_least_total_load_free_peer(ngx_peer_connection_t *pc, void *data, ngx_uint_t state); static void *ngx_http_least_total_load_create_srv_conf(ngx_conf_t *cf) { - ngx_http_least_total_load_conf_t *conf = - ngx_pcalloc(cf->pool, sizeof(*conf)); + ngx_http_least_total_load_conf_t *conf = ngx_pcalloc(cf->pool, sizeof(*conf)); if (conf == NULL) { return NULL; } @@ -56,8 +51,7 @@ static void *ngx_http_least_total_load_create_srv_conf(ngx_conf_t *cf) return conf; } -static ngx_int_t -ngx_http_least_total_load_init_shm_zone(ngx_shm_zone_t *shm_zone, void *data) +static ngx_int_t ngx_http_least_total_load_init_shm_zone(ngx_shm_zone_t *shm_zone, void *data) { ngx_slab_pool_t *shpool; prefill_upstream_info_t *shm_block; @@ -72,8 +66,7 @@ ngx_http_least_total_load_init_shm_zone(ngx_shm_zone_t *shm_zone, void *data) shpool = (ngx_slab_pool_t *)shm_zone->shm.addr; n = 512; - size_t sz = sizeof(prefill_upstream_info_t) + - (n - 1) * sizeof(ngx_http_least_total_load_shm_peer_t); + size_t sz = sizeof(prefill_upstream_info_t) + (n - 1) * sizeof(ngx_http_least_total_load_shm_peer_t); shm_block = ngx_slab_alloc(shpool, sz); if (shm_block == NULL) { return NGX_ERROR; @@ -103,53 +96,45 @@ static ngx_command_t ngx_http_upstream_least_total_load_commands[] = { NGX_HTTP_SRV_CONF_OFFSET, offsetof(ngx_http_least_total_load_conf_t, batch_size), NULL}, - ngx_null_command}; - -static ngx_http_module_t ngx_http_upstream_least_total_load_module_ctx = { - NULL, - ngx_http_least_total_load_postconfig, - NULL, - NULL, - ngx_http_least_total_load_create_srv_conf, - NULL, - NULL, - NULL}; - -ngx_module_t ngx_http_upstream_least_total_load_module = { - NGX_MODULE_V1, - &ngx_http_upstream_least_total_load_module_ctx, - ngx_http_upstream_least_total_load_commands, - NGX_HTTP_MODULE, - NULL, - NULL, - NULL, - NULL, - NULL, - NULL, - NULL, - NGX_MODULE_V1_PADDING}; - -static ngx_int_t -least_total_load_select_solver(prefill_upstream_info_t *prefill_shm, - ngx_uint_t worker_num, ngx_uint_t req_length, - ngx_uint_t *chosen) + ngx_null_command}; + +static ngx_http_module_t ngx_http_upstream_least_total_load_module_ctx = {NULL, + ngx_http_least_total_load_postconfig, + NULL, + NULL, + ngx_http_least_total_load_create_srv_conf, + NULL, + NULL, + NULL}; + +ngx_module_t ngx_http_upstream_least_total_load_module = {NGX_MODULE_V1, + &ngx_http_upstream_least_total_load_module_ctx, + ngx_http_upstream_least_total_load_commands, + NGX_HTTP_MODULE, + NULL, + NULL, + NULL, + NULL, + NULL, + NULL, + NULL, + NGX_MODULE_V1_PADDING}; + +static ngx_int_t least_total_load_select_solver(prefill_upstream_info_t *prefill_shm, + ngx_uint_t worker_num, + ngx_uint_t req_length, + ngx_uint_t *chosen) { ngx_uint_t count = 0; double min_score = 0; const ngx_uint_t max_tie = worker_num; ngx_uint_t min_peers[max_tie]; - double least_total_load_batch_size = - (double)ngx_http_least_total_load_batch_size; + double least_total_load_batch_size = (double)ngx_http_least_total_load_batch_size; for (ngx_uint_t i = 0; i < worker_num; ++i) { - ngx_uint_t length_sum_workers = - ngx_atomic_fetch_add(&(prefill_shm->peers[i].total_length_sum), 0); - ngx_uint_t request_sum_workers = - ngx_atomic_fetch_add(&(prefill_shm->peers[i].total_request_sum), 0); - double score = - length_sum_workers * - ((ceil(request_sum_workers / least_total_load_batch_size) + 1.0) / - 2.0); + ngx_uint_t length_sum_workers = ngx_atomic_fetch_add(&(prefill_shm->peers[i].total_length_sum), 0); + ngx_uint_t request_sum_workers = ngx_atomic_fetch_add(&(prefill_shm->peers[i].total_request_sum), 0); + double score = length_sum_workers * ((ceil(request_sum_workers / least_total_load_batch_size) + 1.0) / 2.0); if (count == 0 || score < min_score) { min_score = score; count = 0; @@ -165,9 +150,7 @@ least_total_load_select_solver(prefill_upstream_info_t *prefill_shm, return NGX_OK; } -static ngx_int_t -ngx_http_least_total_load_upstream_init(ngx_http_request_t *r, - ngx_http_upstream_srv_conf_t *uscf) +static ngx_int_t ngx_http_least_total_load_upstream_init(ngx_http_request_t *r, ngx_http_upstream_srv_conf_t *uscf) { ngx_http_upstream_t *u = r->upstream; ngx_http_upstream_rr_peer_data_t *rrp; @@ -194,20 +177,19 @@ ngx_http_least_total_load_upstream_init(ngx_http_request_t *r, return NGX_ERROR; } - least_total_load_select_solver(prefill_shm, n, - (ngx_uint_t)r->request_length, &chosen); + least_total_load_select_solver(prefill_shm, n, (ngx_uint_t)r->request_length, &chosen); ngx_shmtx_lock(&shpool->mutex); - ngx_atomic_fetch_add(&(prefill_shm->peers[chosen].total_length_sum), - (ngx_atomic_int_t)r->request_length); - ngx_atomic_fetch_add(&(prefill_shm->peers[chosen].total_request_sum), - (ngx_atomic_int_t)1); + ngx_atomic_fetch_add(&(prefill_shm->peers[chosen].total_length_sum), (ngx_atomic_int_t)r->request_length); + ngx_atomic_fetch_add(&(prefill_shm->peers[chosen].total_request_sum), (ngx_atomic_int_t)1); ngx_shmtx_unlock(&shpool->mutex); - ngx_log_error( - NGX_LOG_WARN, r->connection->log, 0, - "[least_total_load] request assigned to peer=%ui, request_length=%ui", - chosen, (ngx_uint_t)r->request_length); + ngx_log_error(NGX_LOG_WARN, + r->connection->log, + 0, + "[least_total_load] request assigned to peer=%ui, request_length=%ui", + chosen, + (ngx_uint_t)r->request_length); pdata = ngx_pcalloc(r->pool, sizeof(*pdata)); if (pdata == NULL) { @@ -223,8 +205,7 @@ ngx_http_least_total_load_upstream_init(ngx_http_request_t *r, return NGX_OK; } -static ngx_int_t ngx_http_least_total_load_get_peer(ngx_peer_connection_t *pc, - void *data) +static ngx_int_t ngx_http_least_total_load_get_peer(ngx_peer_connection_t *pc, void *data) { ngx_http_least_total_load_peer_data_t *pdata = data; ngx_http_upstream_rr_peer_data_t *rrp = pdata->rrp; @@ -244,22 +225,17 @@ static ngx_int_t ngx_http_least_total_load_get_peer(ngx_peer_connection_t *pc, return NGX_OK; } -static void ngx_http_least_total_load_free_peer(ngx_peer_connection_t *pc, - void *data, ngx_uint_t state) +static void ngx_http_least_total_load_free_peer(ngx_peer_connection_t *pc, void *data, ngx_uint_t state) { ngx_http_least_total_load_peer_data_t *pdata = data; if (prefill_shm != NULL && pdata->chosen < pdata->rrp->peers->number) { ngx_slab_pool_t *shpool; - shpool = - (ngx_slab_pool_t *)ngx_http_least_total_load_shm_zone->shm.addr; + shpool = (ngx_slab_pool_t *)ngx_http_least_total_load_shm_zone->shm.addr; ngx_shmtx_lock(&shpool->mutex); - ngx_atomic_fetch_add( - &prefill_shm->peers[pdata->chosen].total_request_sum, - (ngx_atomic_int_t)-1); - ngx_atomic_fetch_add( - &prefill_shm->peers[pdata->chosen].total_length_sum, - (ngx_atomic_int_t) - (pdata->request_length)); + ngx_atomic_fetch_add(&prefill_shm->peers[pdata->chosen].total_request_sum, (ngx_atomic_int_t)-1); + ngx_atomic_fetch_add(&prefill_shm->peers[pdata->chosen].total_length_sum, + (ngx_atomic_int_t) - (pdata->request_length)); ngx_shmtx_unlock(&shpool->mutex); pdata->chosen = pdata->rrp->peers->number; } @@ -279,15 +255,13 @@ static ngx_int_t ngx_http_least_total_load_postconfig(ngx_conf_t *cf) } shm_name->len = sizeof("upstream_prefill_least_total_load") - 1; shm_name->data = (u_char *)"upstream_prefill_least_total_load"; - ngx_http_least_total_load_shm_zone = - ngx_shared_memory_add(cf, shm_name, ngx_http_least_total_load_shm_size, - &ngx_http_upstream_least_total_load_module); + ngx_http_least_total_load_shm_zone = ngx_shared_memory_add( + cf, shm_name, ngx_http_least_total_load_shm_size, &ngx_http_upstream_least_total_load_module); if (ngx_http_least_total_load_shm_zone == NULL) { ngx_pfree(cf->pool, shm_name); return NGX_ERROR; } - ngx_http_least_total_load_shm_zone->init = - ngx_http_least_total_load_init_shm_zone; + ngx_http_least_total_load_shm_zone->init = ngx_http_least_total_load_init_shm_zone; ngx_http_upstream_main_conf_t *upcf; ngx_http_upstream_srv_conf_t **uscfp; @@ -300,19 +274,19 @@ static ngx_int_t ngx_http_least_total_load_postconfig(ngx_conf_t *cf) } uscfp = upcf->upstreams.elts; for (i = 0; i < upcf->upstreams.nelts; i++) { - conf = ngx_http_conf_upstream_srv_conf( - uscfp[i], ngx_http_upstream_least_total_load_module); + conf = ngx_http_conf_upstream_srv_conf(uscfp[i], ngx_http_upstream_least_total_load_module); if (conf->enable == 1) { uscfp[i]->peer.init = ngx_http_least_total_load_upstream_init; if (conf->batch_size == NGX_CONF_UNSET_UINT) { - ngx_http_least_total_load_batch_size = - DEFAULT_least_total_load_BATCH_SIZE; + ngx_http_least_total_load_batch_size = DEFAULT_least_total_load_BATCH_SIZE; } else { ngx_http_least_total_load_batch_size = conf->batch_size; } } } - ngx_log_error(NGX_LOG_WARN, cf->log, 0, + ngx_log_error(NGX_LOG_WARN, + cf->log, + 0, "[least_total_load] least_total_load_batch_size=%ui\n", ngx_http_least_total_load_batch_size); return NGX_OK;