Skip to content

Commit

Permalink
Low: pengine: Performance increase, stack allocated score2char
Browse files Browse the repository at this point in the history
  • Loading branch information
davidvossel committed Feb 27, 2014
1 parent 94b286b commit 047560c
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 13 deletions.
2 changes: 2 additions & 0 deletions include/crm/common/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
/* Status of an offline client */
# endif

char *crm_itoa_stack(int an_int, char *buf, size_t len);
char *crm_itoa(int an_int);
gboolean crm_is_true(const char *s);
int crm_str_to_boolean(const char *s, int *ret);
Expand All @@ -58,6 +59,7 @@ long long crm_get_msec(const char *input);
unsigned long long crm_get_interval(const char *input);
int char2score(const char *score);
char *score2char(int score);
char *score2char_stack(int score, char *buf, size_t len);

int compare_version(const char *version1, const char *version2);

Expand Down
25 changes: 25 additions & 0 deletions lib/common/utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,20 @@ char2score(const char *score)
return score_f;
}

char *
score2char_stack(int score, char *buf, size_t len)
{
if (score >= node_score_infinity) {
strncpy(buf, INFINITY_S, 9);
} else if (score <= -node_score_infinity) {
strncpy(buf, MINUS_INFINITY_S , 10);
} else {
return crm_itoa_stack(score, buf, len);
}

return buf;
}

char *
score2char(int score)
{
Expand Down Expand Up @@ -366,6 +380,17 @@ generate_hash_key(const char *crm_msg_reference, const char *sys)
return hash_key;
}


char *
crm_itoa_stack(int an_int, char *buffer, size_t len)
{
if (buffer != NULL) {
snprintf(buffer, len, "%d", an_int);
}

return buffer;
}

char *
crm_itoa(int an_int)
{
Expand Down
12 changes: 8 additions & 4 deletions lib/pengine/utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,8 @@ dump_node_scores_worker(int level, const char *file, const char *function, int l
}

if (level == 0) {
char score[128];
int len = sizeof(score);
/* For now we want this in sorted order to keep the regression tests happy */
GListPtr gIter = NULL;
GListPtr list = g_hash_table_get_values(hash);
Expand All @@ -195,23 +197,26 @@ dump_node_scores_worker(int level, const char *file, const char *function, int l
gIter = list;
for (; gIter != NULL; gIter = gIter->next) {
node_t *node = (node_t *) gIter->data;
char *score = score2char(node->weight);
/* This function is called a whole lot, use stack allocated score */
score2char_stack(node->weight, score, len);

if (rsc) {
printf("%s: %s allocation score on %s: %s\n",
comment, rsc->id, node->details->uname, score);
} else {
printf("%s: %s = %s\n", comment, node->details->uname, score);
}
free(score);
}

g_list_free(list);

} else if (hash) {
char score[128];
int len = sizeof(score);
g_hash_table_iter_init(&iter, hash);
while (g_hash_table_iter_next(&iter, NULL, (void **)&node)) {
char *score = score2char(node->weight);
/* This function is called a whole lot, use stack allocated score */
score2char_stack(node->weight, score, len);

if (rsc) {
do_crm_log_alias(LOG_TRACE, file, function, line,
Expand All @@ -221,7 +226,6 @@ dump_node_scores_worker(int level, const char *file, const char *function, int l
do_crm_log_alias(LOG_TRACE, file, function, line + 1, "%s: %s = %s", comment,
node->details->uname, score);
}
free(score);
}
}

Expand Down
12 changes: 7 additions & 5 deletions pengine/master.c
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,8 @@ master_promotion_order(resource_t * rsc, pe_working_set_t * data_set)
node_t *node = NULL;
node_t *chosen = NULL;
clone_variant_data_t *clone_data = NULL;
char score[33];
size_t len = sizeof(score);

get_clone_variant_data(clone_data, rsc);

Expand All @@ -298,7 +300,6 @@ master_promotion_order(resource_t * rsc, pe_working_set_t * data_set)
gIter = rsc->children;
for (; gIter != NULL; gIter = gIter->next) {
resource_t *child = (resource_t *) gIter->data;
char *score = NULL;

chosen = child->fns->location(child, NULL, FALSE);
if (chosen == NULL || child->sort_index < 0) {
Expand All @@ -309,10 +310,9 @@ master_promotion_order(resource_t * rsc, pe_working_set_t * data_set)
node = (node_t *) pe_hash_table_lookup(rsc->allowed_nodes, chosen->details->id);
CRM_ASSERT(node != NULL);
/* adds in master preferences and rsc_location.role=Master */
score = score2char(child->sort_index);
score2char_stack(child->sort_index, score, len);
pe_rsc_trace(rsc, "Adding %s to %s from %s", score,
node->details->uname, child->id);
free(score);
node->weight = merge_weights(child->sort_index, node->weight);
}

Expand Down Expand Up @@ -638,6 +638,9 @@ master_color(resource_t * rsc, node_t * prefer, pe_working_set_t * data_set)
node_t *cons_node = NULL;
enum rsc_role_e next_role = RSC_ROLE_UNKNOWN;

char score[33];
size_t len = sizeof(score);

clone_variant_data_t *clone_data = NULL;

get_clone_variant_data(clone_data, rsc);
Expand Down Expand Up @@ -742,7 +745,7 @@ master_color(resource_t * rsc, node_t * prefer, pe_working_set_t * data_set)
gIter = rsc->children;
for (; gIter != NULL; gIter = gIter->next) {
resource_t *child_rsc = (resource_t *) gIter->data;
char *score = score2char(child_rsc->sort_index);
score2char_stack(child_rsc->sort_index, score, len);

chosen = child_rsc->fns->location(child_rsc, NULL, FALSE);
if (show_scores) {
Expand All @@ -753,7 +756,6 @@ master_color(resource_t * rsc, node_t * prefer, pe_working_set_t * data_set)
do_crm_log(scores_log_level, "%s promotion score on %s: %s",
child_rsc->id, chosen ? chosen->details->uname : "none", score);
}
free(score);

chosen = NULL; /* nuke 'chosen' so that we don't promote more than the
* required number of instances
Expand Down
10 changes: 6 additions & 4 deletions pengine/native.c
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,9 @@ native_choose_node(resource_t * rsc, node_t * prefer, pe_working_set_t * data_se

if (multiple > 1) {
int log_level = LOG_INFO;
char *score = score2char(chosen->weight);
static char score[33];

score2char_stack(chosen->weight, score, sizeof(score));

if (chosen->weight >= INFINITY) {
log_level = LOG_WARNING;
Expand All @@ -222,7 +224,6 @@ native_choose_node(resource_t * rsc, node_t * prefer, pe_working_set_t * data_se
do_crm_log(log_level, "%d nodes with equal score (%s) for"
" running %s resources. Chose %s.",
multiple, score, rsc->id, chosen->details->uname);
free(score);
}

result = native_assign_node(rsc, nodes, chosen, FALSE);
Expand Down Expand Up @@ -1635,11 +1636,12 @@ colocation_match(resource_t * rsc_lh, resource_t * rsc_rh, rsc_colocation_t * co
work = NULL;

} else {
char *score = score2char(constraint->score);
static char score[33];

score2char_stack(constraint->score, score, sizeof(score));

pe_rsc_info(rsc_lh, "%s: Rolling back scores from %s (%d, %s)",
rsc_lh->id, rsc_rh->id, do_check, score);
free(score);
}

if (work) {
Expand Down

0 comments on commit 047560c

Please sign in to comment.