1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568
|
/* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil -*- */
/*
* Copyright (c) 2004-2008 The Trustees of Indiana University and Indiana
* University Research and Technology
* Corporation. All rights reserved.
* Copyright (c) 2004-2012 The University of Tennessee and The University
* of Tennessee Research Foundation. All rights
* reserved.
* Copyright (c) 2004-2005 High Performance Computing Center Stuttgart,
* University of Stuttgart. All rights reserved.
* Copyright (c) 2004-2005 The Regents of the University of California.
* All rights reserved.
* Copyright (c) 2008-2013 Cisco Systems, Inc. All rights reserved.
* Copyright (c) 2012-2015 Los Alamos National Security, LLC. All rights
* reserved.
* Copyright (c) 2017 Research Organization for Information Science
* and Technology (RIST). All rights reserved.
* Copyright (c) 2024 Triad National Security, LLC. All rights
* reserved.
* $COPYRIGHT$
*
* Additional copyrights may follow
*
* $HEADER$
*/
#include "opal_config.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef HAVE_UNISTD_H
# include <unistd.h>
#endif
#ifdef HAVE_SYS_PARAM_H
# include <sys/param.h>
#endif
#include <errno.h>
#include "opal/constants.h"
#include "opal/include/opal_stdint.h"
#include "opal/mca/base/mca_base_pvar.h"
#include "opal/mca/base/mca_base_vari.h"
#include "opal/mca/mca.h"
#include "opal/runtime/opal.h"
#include "opal/util/opal_environ.h"
#include "opal/util/output.h"
#include "opal/util/show_help.h"
static opal_pointer_array_t mca_base_var_groups;
static opal_hash_table_t mca_base_var_group_index_hash;
static int mca_base_var_group_count = 0;
static int mca_base_var_groups_timestamp = 0;
static bool mca_base_var_group_initialized = false;
static void mca_base_var_group_constructor(mca_base_var_group_t *group);
static void mca_base_var_group_destructor(mca_base_var_group_t *group);
OBJ_CLASS_INSTANCE(mca_base_var_group_t, opal_object_t, mca_base_var_group_constructor,
mca_base_var_group_destructor);
int mca_base_var_group_init(void)
{
int ret;
if (!mca_base_var_group_initialized) {
OBJ_CONSTRUCT(&mca_base_var_groups, opal_pointer_array_t);
/* These values are arbitrary */
ret = opal_pointer_array_init(&mca_base_var_groups, 128, 16384, 128);
if (OPAL_SUCCESS != ret) {
return ret;
}
OBJ_CONSTRUCT(&mca_base_var_group_index_hash, opal_hash_table_t);
ret = opal_hash_table_init(&mca_base_var_group_index_hash, 256);
if (OPAL_SUCCESS != ret) {
return ret;
}
mca_base_var_group_initialized = true;
mca_base_var_group_count = 0;
}
return OPAL_SUCCESS;
}
int mca_base_var_group_finalize(void)
{
opal_object_t *object;
int size, i;
if (mca_base_var_group_initialized) {
size = opal_pointer_array_get_size(&mca_base_var_groups);
for (i = 0; i < size; ++i) {
object = opal_pointer_array_get_item(&mca_base_var_groups, i);
if (NULL != object) {
OBJ_RELEASE(object);
}
}
OBJ_DESTRUCT(&mca_base_var_groups);
OBJ_DESTRUCT(&mca_base_var_group_index_hash);
mca_base_var_group_count = 0;
mca_base_var_group_initialized = false;
}
return OPAL_SUCCESS;
}
int mca_base_var_group_get_internal(const int group_index, mca_base_var_group_t **group,
bool invalidok)
{
if (group_index < 0) {
return OPAL_ERR_NOT_FOUND;
}
*group = (mca_base_var_group_t *) opal_pointer_array_get_item(&mca_base_var_groups,
group_index);
if (NULL == *group || (!invalidok && !(*group)->group_isvalid)) {
*group = NULL;
return OPAL_ERR_NOT_FOUND;
}
return OPAL_SUCCESS;
}
static int group_find_by_name(const char *full_name, int *index, bool invalidok)
{
mca_base_var_group_t *group;
void *tmp;
int rc;
rc = opal_hash_table_get_value_ptr(&mca_base_var_group_index_hash, full_name, strlen(full_name),
&tmp);
if (OPAL_SUCCESS != rc) {
return rc;
}
rc = mca_base_var_group_get_internal((int) (uintptr_t) tmp, &group, invalidok);
if (OPAL_SUCCESS != rc) {
return rc;
}
if (invalidok || group->group_isvalid) {
*index = (int) (uintptr_t) tmp;
return OPAL_SUCCESS;
}
return OPAL_ERR_NOT_FOUND;
}
static bool compare_strings(const char *str1, const char *str2)
{
if ((NULL != str1 && 0 == strcmp(str1, "*")) || (NULL == str1 && NULL == str2)) {
return true;
}
if (NULL != str1 && NULL != str2) {
return 0 == strcmp(str1, str2);
}
return false;
}
static int group_find_linear(const char *project_name, const char *framework_name,
const char *component_name, bool invalidok)
{
for (int i = 0; i < mca_base_var_group_count; ++i) {
mca_base_var_group_t *group;
int rc = mca_base_var_group_get_internal(i, &group, invalidok);
if (OPAL_SUCCESS != rc) {
continue;
}
if (compare_strings(project_name, group->group_project)
&& compare_strings(framework_name, group->group_framework)
&& compare_strings(component_name, group->group_component)) {
return i;
}
}
return OPAL_ERR_NOT_FOUND;
}
static int group_find(const char *project_name, const char *framework_name,
const char *component_name, bool invalidok)
{
char *full_name;
int ret, index = 0;
if (!mca_base_var_initialized) {
return OPAL_ERR_NOT_FOUND;
}
/* check for wildcards */
if ((project_name && '*' == project_name[0]) || (framework_name && '*' == framework_name[0])
|| (component_name && '*' == component_name[0])) {
return group_find_linear(project_name, framework_name, component_name, invalidok);
}
ret = mca_base_var_generate_full_name4(project_name, framework_name, component_name, NULL,
&full_name);
if (OPAL_SUCCESS != ret) {
return OPAL_ERROR;
}
ret = group_find_by_name(full_name, &index, invalidok);
free(full_name);
return (0 > ret) ? ret : index;
}
static int group_register(const char *project_name, const char *framework_name,
const char *component_name, const char *description)
{
mca_base_var_group_t *group;
int group_id, parent_id = -1;
int ret;
if (NULL == project_name && NULL == framework_name && NULL == component_name) {
/* don't create a group with no name (maybe we should create a generic group?) */
return -1;
}
/* avoid groups of the form opal_opal, ompi_ompi, etc */
if (NULL != project_name && NULL != framework_name
&& (0 == strcmp(project_name, framework_name))) {
project_name = NULL;
}
group_id = group_find(project_name, framework_name, component_name, true);
if (0 <= group_id) {
ret = mca_base_var_group_get_internal(group_id, &group, true);
if (OPAL_SUCCESS != ret) {
/* something went horribly wrong */
assert(NULL != group);
return ret;
}
group->group_isvalid = true;
mca_base_var_groups_timestamp++;
/* group already exists. return it's index */
return group_id;
}
group = OBJ_NEW(mca_base_var_group_t);
group->group_isvalid = true;
if (NULL != project_name) {
group->group_project = strdup(project_name);
if (NULL == group->group_project) {
OBJ_RELEASE(group);
return OPAL_ERR_OUT_OF_RESOURCE;
}
}
if (NULL != framework_name) {
group->group_framework = strdup(framework_name);
if (NULL == group->group_framework) {
OBJ_RELEASE(group);
return OPAL_ERR_OUT_OF_RESOURCE;
}
}
if (NULL != component_name) {
group->group_component = strdup(component_name);
if (NULL == group->group_component) {
OBJ_RELEASE(group);
return OPAL_ERR_OUT_OF_RESOURCE;
}
}
if (NULL != description) {
group->group_description = strdup(description);
if (NULL == group->group_description) {
OBJ_RELEASE(group);
return OPAL_ERR_OUT_OF_RESOURCE;
}
}
if (NULL != framework_name && NULL != component_name) {
if (component_name) {
parent_id = group_register(project_name, framework_name, NULL, NULL);
} else if (framework_name && project_name) {
parent_id = group_register(project_name, NULL, NULL, NULL);
}
}
/* build the group name */
ret = mca_base_var_generate_full_name4(NULL, project_name, framework_name, component_name,
&group->group_full_name);
if (OPAL_SUCCESS != ret) {
OBJ_RELEASE(group);
return ret;
}
group_id = opal_pointer_array_add(&mca_base_var_groups, group);
if (0 > group_id) {
OBJ_RELEASE(group);
return OPAL_ERROR;
}
opal_hash_table_set_value_ptr(&mca_base_var_group_index_hash, group->group_full_name,
strlen(group->group_full_name), (void *) (uintptr_t) group_id);
mca_base_var_group_count++;
mca_base_var_groups_timestamp++;
if (0 <= parent_id) {
mca_base_var_group_t *parent_group;
(void) mca_base_var_group_get_internal(parent_id, &parent_group, false);
opal_value_array_append_item(&parent_group->group_subgroups, &group_id);
}
return group_id;
}
int mca_base_var_group_register(const char *project_name, const char *framework_name,
const char *component_name, const char *description)
{
return group_register(project_name, framework_name, component_name, description);
}
int mca_base_var_group_component_register(const mca_base_component_t *component,
const char *description)
{
return group_register(component->mca_project_name, component->mca_type_name,
component->mca_component_name, description);
}
int mca_base_var_group_deregister(int group_index)
{
mca_base_var_group_t *group;
int size, ret;
int *params, *subgroups;
opal_object_t **enums;
ret = mca_base_var_group_get_internal(group_index, &group, false);
if (OPAL_SUCCESS != ret) {
return ret;
}
group->group_isvalid = false;
/* deregister all associated mca parameters */
size = opal_value_array_get_size(&group->group_vars);
params = OPAL_VALUE_ARRAY_GET_BASE(&group->group_vars, int);
for (int i = 0; i < size; ++i) {
const mca_base_var_t *var;
ret = mca_base_var_get(params[i], &var);
if (OPAL_SUCCESS != ret || !(var->mbv_flags & MCA_BASE_VAR_FLAG_DWG)) {
continue;
}
(void) mca_base_var_deregister(params[i]);
}
/* invalidate all associated mca performance variables */
size = opal_value_array_get_size(&group->group_pvars);
params = OPAL_VALUE_ARRAY_GET_BASE(&group->group_pvars, int);
for (int i = 0; i < size; ++i) {
const mca_base_pvar_t *var;
ret = mca_base_pvar_get(params[i], &var);
if (OPAL_SUCCESS != ret || !(var->flags & MCA_BASE_PVAR_FLAG_IWG)) {
continue;
}
(void) mca_base_pvar_mark_invalid(params[i]);
}
size = opal_value_array_get_size(&group->group_enums);
enums = OPAL_VALUE_ARRAY_GET_BASE(&group->group_enums, opal_object_t *);
for (int i = 0; i < size; ++i) {
OBJ_RELEASE(enums[i]);
}
opal_value_array_set_size(&group->group_enums, 0);
size = opal_value_array_get_size(&group->group_subgroups);
subgroups = OPAL_VALUE_ARRAY_GET_BASE(&group->group_subgroups, int);
for (int i = 0; i < size; ++i) {
(void) mca_base_var_group_deregister(subgroups[i]);
}
/* ordering of variables and subgroups must be the same if the
* group is re-registered */
mca_base_var_groups_timestamp++;
return OPAL_SUCCESS;
}
int mca_base_var_group_find(const char *project_name, const char *framework_name,
const char *component_name)
{
return group_find(project_name, framework_name, component_name, false);
}
int mca_base_var_group_find_by_name(const char *full_name, int *index)
{
return group_find_by_name(full_name, index, false);
}
int mca_base_var_group_add_var(const int group_index, const int param_index)
{
mca_base_var_group_t *group;
int size, i, ret;
int *params;
ret = mca_base_var_group_get_internal(group_index, &group, false);
if (OPAL_SUCCESS != ret) {
return ret;
}
size = opal_value_array_get_size(&group->group_vars);
params = OPAL_VALUE_ARRAY_GET_BASE(&group->group_vars, int);
for (i = 0; i < size; ++i) {
if (params[i] == param_index) {
return i;
}
}
if (OPAL_SUCCESS != (ret = opal_value_array_append_item(&group->group_vars, ¶m_index))) {
return ret;
}
mca_base_var_groups_timestamp++;
/* return the group index */
return (int) opal_value_array_get_size(&group->group_vars) - 1;
}
int mca_base_var_group_add_pvar(const int group_index, const int param_index)
{
mca_base_var_group_t *group;
int size, i, ret;
int *params;
ret = mca_base_var_group_get_internal(group_index, &group, false);
if (OPAL_SUCCESS != ret) {
return ret;
}
size = opal_value_array_get_size(&group->group_pvars);
params = OPAL_VALUE_ARRAY_GET_BASE(&group->group_pvars, int);
for (i = 0; i < size; ++i) {
if (params[i] == param_index) {
return i;
}
}
if (OPAL_SUCCESS != (ret = opal_value_array_append_item(&group->group_pvars, ¶m_index))) {
return ret;
}
mca_base_var_groups_timestamp++;
/* return the group index */
return (int) opal_value_array_get_size(&group->group_pvars) - 1;
}
int mca_base_var_group_add_enum(const int group_index, const void *storage)
{
mca_base_var_group_t *group;
int size, i, ret;
void **params;
ret = mca_base_var_group_get_internal(group_index, &group, false);
if (OPAL_SUCCESS != ret) {
return ret;
}
size = opal_value_array_get_size(&group->group_enums);
params = OPAL_VALUE_ARRAY_GET_BASE(&group->group_enums, void *);
for (i = 0; i < size; ++i) {
if (params[i] == storage) {
return i;
}
}
if (OPAL_SUCCESS != (ret = opal_value_array_append_item(&group->group_enums, storage))) {
return ret;
}
/* return the group index */
return (int) opal_value_array_get_size(&group->group_enums) - 1;
}
int mca_base_var_group_get(const int group_index, const mca_base_var_group_t **group)
{
return mca_base_var_group_get_internal(group_index, (mca_base_var_group_t **) group, false);
}
int mca_base_var_group_set_var_flag(const int group_index, int flags, bool set)
{
mca_base_var_group_t *group;
int size, i, ret;
int *vars;
ret = mca_base_var_group_get_internal(group_index, &group, false);
if (OPAL_SUCCESS != ret) {
return ret;
}
/* set the flag on each valid variable */
size = opal_value_array_get_size(&group->group_vars);
vars = OPAL_VALUE_ARRAY_GET_BASE(&group->group_vars, int);
for (i = 0; i < size; ++i) {
if (0 <= vars[i]) {
(void) mca_base_var_set_flag(vars[i], flags, set);
}
}
return OPAL_SUCCESS;
}
static void mca_base_var_group_constructor(mca_base_var_group_t *group)
{
memset((char *) group + sizeof(group->super), 0, sizeof(*group) - sizeof(group->super));
OBJ_CONSTRUCT(&group->group_subgroups, opal_value_array_t);
opal_value_array_init(&group->group_subgroups, sizeof(int));
OBJ_CONSTRUCT(&group->group_vars, opal_value_array_t);
opal_value_array_init(&group->group_vars, sizeof(int));
OBJ_CONSTRUCT(&group->group_pvars, opal_value_array_t);
opal_value_array_init(&group->group_pvars, sizeof(int));
OBJ_CONSTRUCT(&group->group_enums, opal_value_array_t);
opal_value_array_init(&group->group_enums, sizeof(void *));
}
static void mca_base_var_group_destructor(mca_base_var_group_t *group)
{
free(group->group_full_name);
group->group_full_name = NULL;
free(group->group_description);
group->group_description = NULL;
free(group->group_project);
group->group_project = NULL;
free(group->group_framework);
group->group_framework = NULL;
free(group->group_component);
group->group_component = NULL;
OBJ_DESTRUCT(&group->group_subgroups);
OBJ_DESTRUCT(&group->group_vars);
OBJ_DESTRUCT(&group->group_pvars);
OBJ_DESTRUCT(&group->group_enums);
}
int mca_base_var_group_get_count(void)
{
return mca_base_var_group_count;
}
int mca_base_var_group_get_stamp(void)
{
return mca_base_var_groups_timestamp;
}
|