Skip to content
Snippets Groups Projects
Commit f980c588 authored by Brian Christiansen's avatar Brian Christiansen
Browse files

Parse tres values with unit suffixes [KMGTP]

parent 510abf23
No related branches found
No related tags found
No related merge requests found
......@@ -843,17 +843,6 @@ static int _get_tres_id(char *type, char *name)
return assoc_mgr_find_tres_pos(&tres_rec, false);
}
static int _get_tres_base_unit(char *tres_type)
{
int ret_unit = UNIT_NONE;
if ((!xstrcasecmp(tres_type, "mem")) ||
(!xstrcasecmp(tres_type, "bb"))) {
ret_unit = UNIT_MEGA;
}
return ret_unit;
}
static int _tres_weight_item(double *weights, char *item_str)
{
char *type = NULL, *value_str = NULL, *val_unit = NULL, *name = NULL;
......@@ -889,7 +878,7 @@ static int _tres_weight_item(double *weights, char *item_str)
}
if (val_unit && *val_unit) {
int base_unit = _get_tres_base_unit(type);
int base_unit = slurmdb_get_tres_base_unit(type);
int convert_val = get_convert_unit_val(base_unit, *val_unit);
if (convert_val == SLURM_ERROR)
return SLURM_ERROR;
......@@ -4634,16 +4623,11 @@ extern int revert_num_unit(const char *buf)
extern int get_convert_unit_val(int base_unit, char convert_to)
{
char *unit = "\0KMGTP?";
int conv_unit = 0, conv_value = 0;
char *tmp_char = strchr(unit + 1, toupper(convert_to));
if (!tmp_char) {
error("Conversion suffix '%c' not found in '%s'",
convert_to, unit + 1);
if (!(conv_unit = get_unit_type(convert_to)))
return SLURM_ERROR;
}
conv_unit = tmp_char - unit;
while (base_unit++ < conv_unit) {
if (!conv_value)
conv_value = 1024;
......@@ -4654,6 +4638,26 @@ extern int get_convert_unit_val(int base_unit, char convert_to)
return conv_value;
}
extern int get_unit_type(char unit)
{
char *units = "\0KMGTP";
char *tmp_char = NULL;
if (unit == '\0') {
error("Invalid unit type '%c'. Possible options are '%s'",
unit, units + 1);
return SLURM_ERROR;
}
tmp_char = strchr(units + 1, toupper(unit));
if (!tmp_char) {
error("Invalid unit type '%c'. Possible options are '%s'",
unit, units + 1);
return SLURM_ERROR;
}
return tmp_char - units;
}
#if _DEBUG
static void _print_data(char *data, int len)
......
......@@ -1273,6 +1273,7 @@ extern void convert_num_unit(double num, char *buf, int buf_size,
int orig_type, int spec_type, uint32_t flags);
extern int revert_num_unit(const char *buf);
extern int get_convert_unit_val(int base_type, char convert_to);
extern int get_unit_type(char unit);
extern void parse_int_to_array(int in, int *out);
/*
......
......@@ -3189,6 +3189,7 @@ extern char *slurmdb_format_tres_str(
char *tres_in, List full_tres_list, bool simple)
{
char *tres_str = NULL;
char *val_unit = NULL;
char *tmp_str = tres_in;
uint64_t count;
slurmdb_tres_rec_t *tres_rec;
......@@ -3246,7 +3247,16 @@ extern char *slurmdb_format_tres_str(
"no value found");
break;
}
count = slurm_atoull(++tmp_str);
count = strtoull(++tmp_str, &val_unit, 10);
if (val_unit && *val_unit != ',' && *val_unit != '\0' &&
tres_rec->type) {
int base_unit =
slurmdb_get_tres_base_unit(tres_rec->type);
int convert_val =
get_convert_unit_val(base_unit, *val_unit);
if (convert_val > 0)
count *= convert_val;
}
if (tres_str)
xstrcat(tres_str, ",");
......@@ -3809,3 +3819,14 @@ extern void slurmdb_set_new_tres_cnt(uint64_t **tres_cnt_in,
return;
}
extern int slurmdb_get_tres_base_unit(char *tres_type)
{
int ret_unit = UNIT_NONE;
if ((!xstrcasecmp(tres_type, "mem")) ||
(!xstrcasecmp(tres_type, "bb"))) {
ret_unit = UNIT_MEGA;
}
return ret_unit;
}
......@@ -269,4 +269,6 @@ extern void slurmdb_set_new_tres_cnt(uint64_t **tres_cnt_in,
slurmdb_tres_rec_t **old_array,
int cur_cnt, int max_cnt);
extern int slurmdb_get_tres_base_unit(char *tres_type);
#endif
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment