Skip to content

Loading…

Low: pengine: Performance increase, stack allocated score2char #446

Merged
merged 1 commit into from

2 participants

@davidvossel

No description provided.

@beekhof beekhof merged commit c30f974 into ClusterLabs:master

1 check passed

Details default The Travis CI build passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Showing with 48 additions and 13 deletions.
  1. +2 −0 include/crm/common/util.h
  2. +25 −0 lib/common/utils.c
  3. +8 −4 lib/pengine/utils.c
  4. +7 −5 pengine/master.c
  5. +6 −4 pengine/native.c
View
2 include/crm/common/util.h
@@ -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);
@@ -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);
View
25 lib/common/utils.c
@@ -216,6 +216,20 @@ char2score(const char *score)
}
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)
{
if (score >= node_score_infinity) {
@@ -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)
{
View
12 lib/pengine/utils.c
@@ -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);
@@ -195,7 +197,8 @@ 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",
@@ -203,15 +206,17 @@ dump_node_scores_worker(int level, const char *file, const char *function, int l
} 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,
@@ -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);
}
}
View
12 pengine/master.c
@@ -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);
@@ -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) {
@@ -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);
}
@@ -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);
@@ -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) {
@@ -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
View
10 pengine/native.c
@@ -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;
@@ -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);
@@ -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) {
Something went wrong with that request. Please try again.