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 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922
|
/*****************************************************************************
* Copyright (C) 2014 Silicon Graphics International Corp.
* All rights reserved.
****************************************************************************/
#if HAVE_CONFIG_H
# include "config.h"
#endif
#define _GNU_SOURCE
#include <stdlib.h>
#include <stdio.h>
#include <sys/param.h>
#include <netloc.h>
#ifdef HAVE_NETLOC_NOSUB
# include <netloc_map.h>
#else
# include <netloc/map.h>
#endif
typedef struct node_group {
char *node_name;
int node_name_len;
int cpus;
int memory;
int cores_per_socket;
int threads_per_core;
} node_group;
typedef struct switch_name {
const char *sw_name;
unsigned long physical_id;
} switch_name;
// Parse the command line arguments and update variables appropriately
static int parse_args(int argc, char ** argv);
// Check the directory parameters to make sure they are formatted correctly
static int check_directory_parameters();
// initialize NetLoc topology to be used to lookup NetLoc information
static netloc_topology_t setup_topology(char *data_uri);
// initialize NetLoc map to be used to lookup HwLoc information
static netloc_map_t setup_map(char *data_uri);
// Generate a topology.conf file based on NetLoc topology and save it to file
static int generate_topology_file(netloc_topology_t *topology, netloc_map_t *map);
// Loop through and parse all of the switches and their connections
static int loop_through_switches(netloc_topology_t *topology,
netloc_map_t *map, netloc_dt_lookup_table_t *switches);
// Loop through and parse all of the edges for a switch
static int loop_through_edges(netloc_topology_t *topology, netloc_map_t *map,
netloc_node_t *node, const char *src_name, FILE *f_temp);
// Add a switch connection and its link speed to the switch list
static int add_switch_connection(netloc_edge_t **edges, int idx, int num_edges,
const char *src_name, const char *dst_name, char *switch_str);
// calculate the link speed for an edge between two switches
static int calculate_link_speed(netloc_edge_t *edge);
// Add a node connection to the node list
static int add_node_connection( netloc_topology_t *topology, netloc_map_t *map,
netloc_edge_t *edge, char *node_str );
// Find a node group that matches the specifications given
static int find_node_group( int cpus, int cores_per_socket,
int threads_per_core, int memory, const char *dst_name);
// Make a new node group in the table and fill in information
static void make_new_node_group( int cpus, int cores_per_socket,
int threads_per_core, int memory, const char *dst_name);
// Save Topology data of network to topology.conf file
static int save_topology_data_to_file();
// Gets the name and the hw_loc topology for a NetLoc node
static int get_node_name_and_topology(netloc_topology_t *topology, netloc_map_t *map,
netloc_node_t *node, const char **name, hwloc_topology_t *hw_topo);
// Gets the name of a switch in the network
static int get_switch_name( netloc_topology_t *topology, netloc_map_t *map,
netloc_node_t *node, const char **name );
// Find a switch_name that matches the Physical ID given
static int find_switch_name( netloc_node_t *node );
// Compares switch_name with all of the names in the table
static int check_unique_switch_name( char *sw_name);
// Make a new switch_name entry in the table and fill in information
static int make_new_switch_name( netloc_topology_t *topology, netloc_map_t *map,
netloc_node_t *node, const char **name );
#define NETLOC_DIR "netloc"
const char * ARG_OUTDIR = "--outdir";
const char * ARG_SHORT_OUTDIR = "-o";
const char * ARG_DATADIR = "--datadir";
const char * ARG_SHORT_DATADIR = "-d";
const char * ARG_VERBOSE = "--verbose";
const char * ARG_SHORT_VERBOSE = "-v";
const char * ARG_FABRIC = "--fabric";
const char * ARG_SHORT_FABRIC = "-f";
const char * ARG_HELP = "--help";
const char * ARG_SHORT_HELP = "-h";
static char * outdir = NULL;
static char * datadir = NULL;
static char * fabric = "fe80:0000:0000:0000";
static int verbose = 0;
static int max_nodes = 0, max_switches = 0;
static node_group *node_group_table = NULL;
static int node_group_cnt = 0;
static int node_groups_max = 32;
static switch_name **switch_name_table = NULL;
static int switch_name_cnt = 0;
static int switch_name_max = 256;
static char *file_location = NULL, *file_location_temp= NULL;
int main(int argc, char ** argv) {
int ret;
netloc_topology_t topology;
netloc_map_t map;
// Parse the command line arguments and update variables appropriately
if( 0 != parse_args(argc, argv) ) {
printf(
"Usage: %s\n"
"\t%s|%s <directory with hwloc and netloc data directories>\n"
"\t[%s|%s <output directory>]\n"
"\t[%s|%s <IB Fabric ID, eg. fec0:0000:0000:0000>]\n"
"\t[%s|%s] [--help|-h]\n",
argv[0],
ARG_DATADIR, ARG_SHORT_DATADIR,
ARG_OUTDIR, ARG_SHORT_OUTDIR,
ARG_FABRIC, ARG_SHORT_FABRIC,
ARG_VERBOSE, ARG_SHORT_VERBOSE);
printf(" Default %-10s = current working directory\n", ARG_OUTDIR);
return NETLOC_ERROR;
}
asprintf(&file_location, "%stopology.conf", outdir);
asprintf(&file_location_temp, "%s.temp", file_location);
// initialize NetLoc topology to be used to lookup NetLoc information
topology = setup_topology(datadir);
(verbose) ? printf("Successfully Created Network Topology \n") : 0 ;
// initialize NetLoc map to be used to lookup HwLoc information
map = setup_map(datadir);
(verbose) ? printf("Successfully Created Network Map\n") : 0 ;
node_group_table = malloc( sizeof(node_group) * node_groups_max );
switch_name_table = malloc( sizeof(switch_name *) * switch_name_max );
// Generate a topology.conf file based on NetLoc topology and save to file
ret = generate_topology_file(&topology, &map);
if( NETLOC_SUCCESS == ret )
printf("\nDone generating topology.conf file from NetLoc data\n");
else
printf("Error: Couldn't Create topology.conf file from NetLoc data\n");
netloc_detach(topology);
netloc_map_destroy(map);
return ret;
}
// Parse the command line arguments and update variables appropriately
static int parse_args(int argc, char ** argv) {
int i, ret = NETLOC_SUCCESS;
for(i = 1; i < argc; ++i ) {
// --outdir
if( ( 0 == strncmp(ARG_OUTDIR, argv[i], strlen(ARG_OUTDIR)) ) ||
(0 == strncmp(ARG_SHORT_OUTDIR, argv[i], strlen(ARG_SHORT_OUTDIR))) ) {
++i;
if( i >= argc ) {
fprintf(stderr, "Error: Must supply an argument to %s\n",
ARG_OUTDIR );
return NETLOC_ERROR;
}
outdir = strdup(argv[i]);
}
// --datadir (directory with hwloc and netloc input data directories)
else if( 0 ==strncmp(ARG_DATADIR, argv[i], strlen(ARG_DATADIR)) ||
0 == strncmp(ARG_SHORT_DATADIR, argv[i], strlen(ARG_SHORT_DATADIR)) ) {
++i;
if( i >= argc ) {
fprintf(stderr, "Error: Must supply an argument to %s "
"(input data directory)\n", ARG_DATADIR );
return NETLOC_ERROR;
}
datadir = strdup(argv[i]);
}
// verbose output
else if( 0 == strncmp(ARG_VERBOSE, argv[i], strlen(ARG_VERBOSE)) ||
(0 == strncmp(ARG_SHORT_VERBOSE, argv[i], strlen(ARG_SHORT_VERBOSE)))){
verbose = 1;
}
// Help
else if( 0 == strncmp(ARG_HELP, argv[i], strlen(ARG_HELP)) ||
0 == strncmp(ARG_SHORT_HELP, argv[i], strlen(ARG_SHORT_HELP)) ) {
return NETLOC_ERROR;
} else if (0 == strcmp(ARG_FABRIC, argv[i]) ||
0 == strcmp(ARG_SHORT_FABRIC, argv[i])) {
i++;
if (i >= argc) {
fprintf(stderr,
"Error: Must supply an argument to %s (fabric ID)\n",
ARG_FABRIC);
}
fabric = strdup(argv[i]);
}
// Unknown options throw warnings
else {
fprintf(stderr, "Warning: Unknown argument of <%s>\n", argv[i]);
return NETLOC_ERROR;
}
}
// Check the directory parameters to make sure they are formatted correctly
ret = check_directory_parameters();
return ret;
}
// Check the directory parameters to make sure they are formatted correctly
static int check_directory_parameters() {
int ret = NETLOC_SUCCESS;
// Check Output Directory Parameter
if( NULL == outdir || strlen(outdir) <= 0 ) {
if( NULL != outdir )
free(outdir);
// Default: current working directory
outdir = strdup(".");
}
if( '/' != outdir[strlen(outdir)-1] ) {
outdir = (char *)realloc(outdir, sizeof(char) * (strlen(outdir)+1));
outdir[strlen(outdir)+1] = '\0';
outdir[strlen(outdir)] = '/';
}
// Check Input Data Directory Parameter
if( NULL == datadir || strlen(datadir) <= 0 ) {
fprintf(stderr, "Error: Must supply an argument to %s|%s (input data"
" directory)\n", ARG_DATADIR, ARG_SHORT_DATADIR );
return NETLOC_ERROR;
}
else if( '/' != datadir[strlen(datadir)-1] ) {
datadir = (char *)realloc(datadir, sizeof(char) * (strlen(datadir)+1));
datadir[strlen(datadir)+1] = '\0';
datadir[strlen(datadir)] = '/';
}
// Display Parsed Arguments
(verbose) ? printf(" Input Data Directory: %s\n", datadir) : 0 ;
(verbose) ? printf(" Output Directory : %s\n", outdir) : 0 ;
return ret;
}
// initialize NetLoc topology to be used to lookup NetLoc information
static netloc_topology_t setup_topology(char *data_uri)
{
int ret;
netloc_topology_t topology;
netloc_network_t *tmp_network = NULL;
char *search_uri = NULL;
// Setup a Network connection
tmp_network = netloc_dt_network_t_construct();
tmp_network->network_type = NETLOC_NETWORK_TYPE_INFINIBAND;
tmp_network->subnet_id = strdup(fabric);
asprintf(&search_uri, "file://%s%s", data_uri, NETLOC_DIR);
ret = netloc_find_network(search_uri, tmp_network);
free(search_uri);
if (NETLOC_SUCCESS != ret) {
fprintf(stderr,
"Error: netloc_find_network return error (%d)\n"
"\tConsider passing a different IB fabric ID with -f\n",
ret);
exit(ret);
}
// Attach to the topology context
ret = netloc_attach(&topology, *tmp_network);
netloc_dt_network_t_destruct(tmp_network);
if( NETLOC_SUCCESS != ret ) {
fprintf(stderr, "Error: netloc_attach returned an error (%d)\n", ret);
exit(ret);
}
return topology;
}
// initialize NetLoc map to be used to lookup HwLoc information
static netloc_map_t setup_map(char *data_uri)
{
int err;
netloc_map_t map;
char *path;
err = netloc_map_create(&map);
if (err) {
fprintf(stderr, "Failed to create the map\n");
exit(EXIT_FAILURE);
}
asprintf(&path, "%shwloc", data_uri);
err = netloc_map_load_hwloc_data(map, path);
free(path);
if (err) {
fprintf(stderr, "Failed to load hwloc data\n");
exit(EXIT_FAILURE);
}
asprintf(&path, "file://%s%s", data_uri, NETLOC_DIR);
err = netloc_map_load_netloc_data(map, path);
free(path);
if (err) {
fprintf(stderr, "Failed to load netloc data\n");
exit(EXIT_FAILURE);
}
err = netloc_map_build(map, 0);
if (err) {
fprintf(stderr, "Failed to build map data\n");
exit(EXIT_FAILURE);
}
return map;
}
// Generate a topology.conf file based on NetLoc topology and save it to file
static int generate_topology_file(netloc_topology_t *topology,
netloc_map_t *map)
{
int ret;
netloc_dt_lookup_table_t switches = NULL;
// Get all of the switches
ret = netloc_get_all_switch_nodes(*topology, &switches);
if( NETLOC_SUCCESS != ret ) {
fprintf(stderr, "Error: get_all_switch_nodes returned %d\n", ret);
return ret;
}
// Loop through and parse all of the switches and their connections
ret = loop_through_switches(topology, map, &switches);
if( NETLOC_SUCCESS != ret ) {
fprintf(stderr, "Error: loop_through_switches returned %d\n", ret);
return ret;
}
// Save Topology data of network to topology.conf file
save_topology_data_to_file();
// Cleanup
netloc_lookup_table_destroy(switches);
free(switches);
free(file_location);
free(file_location_temp);
int i;
for ( i = 0; i < node_group_cnt; i++)
free(node_group_table[i].node_name);
free(node_group_table);
for ( i = 0; i < switch_name_cnt; i++)
free(switch_name_table[i]);
free(switch_name_table);
return NETLOC_SUCCESS;
}
// Loop through and parse all of the switches and their connections
static int
loop_through_switches(netloc_topology_t *topology,
netloc_map_t *map, netloc_dt_lookup_table_t *switches)
{
int ret;
netloc_dt_lookup_table_iterator_t hti = NULL;
FILE *f_temp = fopen(file_location_temp, "w");
/* Loop through all of the switches */
hti = netloc_dt_lookup_table_iterator_t_construct(*switches);
while (!netloc_lookup_table_iterator_at_end(hti)) {
const char * key = netloc_lookup_table_iterator_next_key(hti);
if (NULL == key) {break;}
netloc_node_t *node = (netloc_node_t *)
netloc_lookup_table_access(*switches, key);
if (NETLOC_NODE_TYPE_SWITCH != node->node_type) {
fprintf(stderr, "Error: Returned unexpected node: %s\n",
netloc_pretty_print_node_t(node));
return NETLOC_ERROR;
}
// Get the Switch Name
const char *src_name;
ret = get_switch_name(topology, map, node, &src_name);
if (NETLOC_SUCCESS != ret) {
if (verbose) {
fprintf(stderr,
"Did not find data for any nodes attached to switch %s\n",
netloc_pretty_print_node_t(node));
}
continue;
}
// Loop through and parse all of the edges for a switch
loop_through_edges(topology, map, node, src_name, f_temp);
}
// Cleanup
fclose(f_temp);
netloc_dt_lookup_table_iterator_t_destruct(hti);
return NETLOC_SUCCESS;
}
// Loop through and parse all of the edges for a switch
static int
loop_through_edges(netloc_topology_t *topology, netloc_map_t *map,
netloc_node_t *node, const char *src_name, FILE *f_temp)
{
int ret, i, num_edges, nodes_cnt = 0, switches_cnt = 0;
netloc_edge_t **edges = NULL;
size_t slen = 4096;
char *switch_str = malloc(sizeof(char) * slen);
char *node_str = malloc(sizeof(char) * slen);
strcpy(switch_str, "");
strcpy(node_str, "");
// Get all of the edges
ret = netloc_get_all_edges(*topology, node, &num_edges, &edges);
if (NETLOC_SUCCESS != ret) {
fprintf(stderr,
"Error: get_all_edges_by_id returned %d for"
" node %s\n", ret, node->description);
return ret;
}
(verbose) ? printf("\nFound Switch: %s - %s which has %d edges \n",
src_name, node->physical_id, num_edges) : 0;
// Loop through all of the edges
for (i = 0; i < num_edges; i++) {
(verbose) ? printf("\tEdge %2d - Speed: %s, Width: %s - " , i,
edges[i]->speed, edges[i]->width) : 0;
if (NETLOC_NODE_TYPE_SWITCH == edges[i]->dest_node->node_type) {
// get the dest_node name
const char *dst_name;
ret = get_switch_name(
topology, map, edges[i]->dest_node, &dst_name);
if (NETLOC_SUCCESS != ret) {
if (verbose) {
fprintf(stderr,
"Did not find data for any nodes attached to switch %s\n",
netloc_pretty_print_node_t(node));
}
continue;
}
// Add name and link_speed to switch_str
ret = add_switch_connection(edges, i, num_edges, src_name,
dst_name, switch_str);
if (NETLOC_SUCCESS == ret) {switches_cnt++;}
} else if (NETLOC_NODE_TYPE_HOST == edges[i]->dest_node->node_type) {
// if edge goes to a node, add name to node_str and put in a group
ret = add_node_connection(topology, map, edges[i], node_str);
if (NETLOC_SUCCESS == ret) {nodes_cnt++;}
} else {
fprintf(stderr,
"Error: Returned unexpected node: %s\n",
netloc_pretty_print_node_t(edges[i]->dest_node));
return NETLOC_ERROR;
}
}
// update maximum totals needed later
max_switches = MAX(switches_cnt, max_switches);
max_nodes = MAX(max_nodes, nodes_cnt);
// Erase any trailing commas
assert(0 < strlen(switch_str) && slen > strlen(switch_str));
assert(0 < strlen(node_str) && slen > strlen(node_str));
switch_str[strlen(switch_str) - 1] = '\0';
node_str[strlen(node_str) - 1] = '\0';
// combine strings together and output to tolopogy file
fprintf(f_temp, "SwitchName=%s Switches=%s Nodes=%s\n",
src_name, switch_str, node_str);
free(switch_str);
free(node_str);
return NETLOC_SUCCESS;
}
// Add a switch connection and its link speed to the switch list
static int
add_switch_connection(netloc_edge_t **edges, int idx, int num_edges,
const char *src_name, const char *dst_name, char *switch_str)
{
netloc_node_t* dn = edges[idx]->dest_node;
char * pch = strstr(switch_str, dst_name);
int i, total_link_speed = 0;
unsigned long current_ID = dn->physical_id_int;
// Print out node information
(verbose) ? printf("Dst:%9s - (%s - %s) [%20s][%18lu]/[%7s] - (%d edges)\n",
dst_name, netloc_decode_network_type(dn->network_type),
netloc_decode_node_type(dn->node_type), dn->physical_id,
dn->physical_id_int, dn->logical_id, dn->num_edges) : 0;
// Check to see if this switch is already on the switch connection list
if (pch != NULL) {return NETLOC_ERROR;}
// Total up the link speed for all the connections between the two switches
for (i = idx; i < num_edges; i++) {
// If the IDs match then the connections go to the same switch
if (edges[i]->dest_node->physical_id_int == current_ID) {
int link_speed = calculate_link_speed(edges[i]);
if (0 >= link_speed) {
fprintf(stderr,
"\nError: invalid connection width %s or "
"speed %s between %s and %s\n",
edges[idx]->width,
edges[idx]->speed, src_name, dst_name);
return NETLOC_ERROR;
}
total_link_speed += link_speed;
}
}
// Put the switch and its link_speed on the switch string
sprintf(switch_str, "%s%s-%d,", switch_str, dst_name, total_link_speed);
return NETLOC_SUCCESS;
}
// calculate the link speed for an edge between two switches
static int calculate_link_speed(netloc_edge_t *edge)
{
// calculate the link speed between the two switches
int link_speed = atoi(edge->width);
if (link_speed < 1 || (link_speed > 24 ) ){
return -1;
}
if ( strcasecmp(edge->speed, "SDR" ) == 0 )
link_speed *= 2;
else if ( strcasecmp(edge->speed, "DDR" ) == 0 )
link_speed *= 4;
else if ( strcasecmp(edge->speed, "QDR" ) == 0 )
link_speed *= 8;
else if ( strcasecmp(edge->speed, "FDR-10" ) == 0 )
link_speed *= 10;
else if ( strcasecmp(edge->speed, "FDR" ) == 0 )
link_speed *= 14;
else if ( strcasecmp(edge->speed, "EDR" ) == 0 )
link_speed *= 25;
else if ( strcasecmp(edge->speed, "HDR" ) == 0 )
link_speed *= 50;
else{
return -1;
}
return link_speed;
}
// Add a node connection to the node list
static int
add_node_connection(netloc_topology_t *topology, netloc_map_t *map,
netloc_edge_t *edge, char *node_str)
{
int ret;
hwloc_topology_t dst_hw_topo;
const char *dst_name;
ret = get_node_name_and_topology(topology, map, edge->dest_node,
&dst_name, &dst_hw_topo);
if (NETLOC_SUCCESS != ret) {return NETLOC_ERROR;}
(verbose) ? printf( "Dst:%9s - ", dst_name) : 0;
sprintf(node_str, "%s%s,",node_str, dst_name);
// get and calculate needed node information
hwloc_obj_t hw_obj = hwloc_get_root_obj(dst_hw_topo);
int cpus = hwloc_get_nbobjs_by_type(dst_hw_topo, HWLOC_OBJ_PU);
int sockets = hwloc_get_nbobjs_by_type(dst_hw_topo, HWLOC_OBJ_SOCKET);
int cores = hwloc_get_nbobjs_by_type(dst_hw_topo, HWLOC_OBJ_CORE);
int cores_per_socket = cores / sockets;
int threads_per_core = cpus / cores;
int memory = hw_obj->memory.total_memory/1024/1024;
// Find a node group that matches the specifications given
ret = find_node_group(cpus, cores_per_socket, threads_per_core,
memory, dst_name);
// if couldn't find a matching node group, create a new one
if (ret == node_group_cnt) {
// Make a new node group in the table and fill in information
make_new_node_group(cpus, cores_per_socket, threads_per_core,
memory, dst_name);
}
netloc_node_t* dn = edge->dest_node;
( verbose ) ? printf("(%s - %s) [%20s][%18lu]/[%7s] - (%d edges)\n",
netloc_decode_network_type(dn->network_type),
netloc_decode_node_type(dn->node_type), dn->physical_id,
dn->physical_id_int, dn->logical_id, dn->num_edges) : 0;
return NETLOC_SUCCESS;
}
// Find a node group that matches the specifications given
static int find_node_group( int cpus, int cores_per_socket,
int threads_per_core, int memory, const char *dst_name)
{
int j;
for ( j=0; j < node_group_cnt; j++){
// Check to make sure all of the numbers are the same
if ((node_group_table[j].cpus == cpus) &&
(node_group_table[j].memory == memory) &&
(node_group_table[j].cores_per_socket == cores_per_socket) &&
(node_group_table[j].threads_per_core == threads_per_core)){
// Make node_name string bigger if there isn't enough space
if ((strlen(node_group_table[j].node_name) + strlen(dst_name) + 3)
>= node_group_table[j].node_name_len ){
node_group_table[j].node_name_len *= 2;
char *temp_node_name =
(char *) realloc( node_group_table[j].node_name,
sizeof(char) * node_group_table[j].node_name_len);
if (temp_node_name == NULL) {
printf("Error (re)allocating memory - node_name string\n");
exit(-1);
}
node_group_table[j].node_name = temp_node_name;
}
sprintf(node_group_table[j].node_name, "%s,%s",
node_group_table[j].node_name, dst_name);
return j;
}
}
return j;
}
// Make a new node group in the table and fill in information
static void make_new_node_group( int cpus, int cores_per_socket,
int threads_per_core, int memory, const char *dst_name)
{
node_group_table[node_group_cnt].node_name = malloc( sizeof(char) * 2048);
node_group_table[node_group_cnt].node_name_len = 2048;
strcpy(node_group_table[node_group_cnt].node_name, dst_name);
node_group_table[node_group_cnt].cpus = cpus;
node_group_table[node_group_cnt].memory = memory;
node_group_table[node_group_cnt].cores_per_socket = cores_per_socket;
node_group_table[node_group_cnt].threads_per_core = threads_per_core;
node_group_cnt++;
// if there aren't any more empty groups, make new ones
if ( node_group_cnt >= node_groups_max){
node_groups_max *= 2;
node_group *temp_node_group = realloc(node_group_table,
sizeof(node_group) * node_groups_max);
if ( temp_node_group == NULL){
printf("Error (re)allocating memory for more node groups");
exit(-1);
}
node_group_table = temp_node_group;
}
}
// Save Topology data of network to topology.conf file
int save_topology_data_to_file()
{
int j;
// open up files to save data to topology.conf
FILE *f = fopen(file_location, "w");
FILE *f_temp = fopen(file_location_temp, "r");
if ( (f == NULL) || (f_temp == NULL) ){
printf("Error opening file!\n");
exit(1);
}
// print hypercube topology configuration information for reference
fprintf(f,"#############################################################"
"#####\n# Slurm's network topology configuration file for use with the"
" topology/hypercube plugin\n#########################################"
"#########################\n# Hypcube topology information:\n# Maximum "
"Number of Dimensions: %d \n# Maximum Number of Nodes per Switch: %d\n"
"\n##################################################################\n"
,max_switches, max_nodes);
/*
* Print out compute nodes info and partitions nodes list for slurm.conf
* in case the user wants to use this tool to fill in their node list for
* that config file.
*/
fprintf(f, "# Compute Nodes information for slurm.conf:\n");
for ( j=0; j < node_group_cnt; j++){
fprintf(f,"# NodeName=%s CPUs=%d RealMemory=%d CoresPerSocket=%d "
"ThreadsPerCore=%d State=UNKNOWN\n", node_group_table[j].node_name,
node_group_table[j].cpus, node_group_table[j].memory,
node_group_table[j].cores_per_socket,
node_group_table[j].threads_per_core);
}
fprintf(f,"\n###########################################################"
"#######\n# Partition nodes list for slurm.conf: \n" "# Nodes=" );
for ( j=0; j < node_group_cnt-1; j++){
fprintf(f, "%s,", node_group_table[j].node_name );
}
fprintf(f, "%s \n", node_group_table[j].node_name );
// copy switch information from temp file to topology.conf
fprintf(f, "\n#########################################################"
"#########\n# Switch Hypercube Topology Information: \n");
char ch;
while ( ( ch = fgetc(f_temp) ) != EOF )
fputc(ch, f);
// Cleanup
fclose(f);
fclose(f_temp);
remove(file_location_temp);
return NETLOC_SUCCESS;
}
// Gets the name and the hw_loc topology for a NetLoc node
static int
get_node_name_and_topology(
netloc_topology_t *topology, netloc_map_t *map,
netloc_node_t *node, const char **name, hwloc_topology_t *hw_topo)
{
netloc_map_port_t port = NULL;
hwloc_obj_t hw_obj = NULL;
netloc_map_server_t server = NULL;
int ret;
ret = netloc_map_netloc2port(*map, *topology, node, NULL, &port);
if( NETLOC_SUCCESS != ret ) {
if (verbose) {
printf( "\n Error: netloc_map_netloc2port could not find"
" port info for %s\n", netloc_pretty_print_node_t(node) );
}
return ret;
}
ret = netloc_map_port2hwloc(port, hw_topo, &hw_obj);
if( NETLOC_SUCCESS != ret ) {
fprintf(stderr, "Error: netloc_map_port2hwloc returned an error");
return ret;
}
ret = netloc_map_hwloc2server(*map, *hw_topo, &server);
if( NETLOC_SUCCESS != ret ) {
fprintf(stderr, "Error: netloc_map_hwloc2server returned an error");
return ret;
}
ret = netloc_map_server2name(server, name);
if( NETLOC_SUCCESS != ret ) {
fprintf(stderr, "Error: netloc_map_server2name returned an error");
return ret;
}
return NETLOC_SUCCESS;
}
// Gets the name of a switch in the network
static int get_switch_name(netloc_topology_t *topology, netloc_map_t *map,
netloc_node_t *node, const char **name)
{
// Find a switch_name that matches the Physical ID given
int ret = find_switch_name(node);
// If there already a switch_name assigned to the physical ID
if (ret != switch_name_cnt) {
*name = switch_name_table[ret]->sw_name;
}
// Else if couldn't find a matching switch_name create a new one
else{
// Make a switch_name entry in the table and fill in information
ret = make_new_switch_name(topology, map, node, name);
if (NETLOC_SUCCESS != ret) {return ret;}
switch_name *sw_name_entry = malloc(sizeof(switch_name));
sw_name_entry->sw_name = *name;
sw_name_entry->physical_id = node->physical_id_int;
switch_name_table[switch_name_cnt] = sw_name_entry;
switch_name_cnt++;
// If no more room for more switch_names, then make more space
if (switch_name_cnt == switch_name_max) {
switch_name_max *= 2;
switch_name **temp_switch_name_table = realloc(
switch_name_table,
sizeof(switch_name) * switch_name_max);
if (temp_switch_name_table == NULL){
printf("Error (re)allocating memory for more switch_names");
exit(-1);
}
switch_name_table = temp_switch_name_table;
}
}
return NETLOC_SUCCESS;
}
// Find a switch_name that matches the Physical ID given
static int find_switch_name( netloc_node_t *node )
{
int j;
for ( j=0; j < switch_name_cnt; j++){
// Check to see if the numbers are the same
if ( switch_name_table[j]->physical_id == node->physical_id_int ) {
return j;
}
}
return j;
}
// Compares switch_name with all of the names in the table
static int check_unique_switch_name( char *sw_name)
{
int j;
for ( j=0; j < switch_name_cnt; j++){
// Check to see if the names are the same
if ( strcmp( switch_name_table[j]->sw_name, sw_name ) == 0 ) {
break;
}
}
// if the name already exists return 0, else return 1
if ( j < switch_name_cnt )
return NETLOC_ERROR;
else
return NETLOC_SUCCESS;
}
// Make a new switch_name entry in the table and fill in information
static int
make_new_switch_name(netloc_topology_t *topology, netloc_map_t *map,
netloc_node_t *node, const char **name )
{
int ret, i, num_edges;
netloc_edge_t **edges = NULL;
const char *node_name;
//Get all of the edges
ret = netloc_get_all_edges(*topology, node, &num_edges, &edges);
if (NETLOC_SUCCESS != ret) {
fprintf(stderr,
"Error: netloc_get_all_edges returned %d for"
" node %s\n", ret, netloc_pretty_print_node_t(node));
return ret;
}
// get the node name of the first host connected to the switch
for (i = 0; i < num_edges; i++) {
if (NETLOC_NODE_TYPE_HOST == edges[i]->dest_node->node_type) {
hwloc_topology_t dst_hw_topo;
ret = get_node_name_and_topology(
topology, map,
edges[i]->dest_node, &node_name, &dst_hw_topo);
if (NETLOC_SUCCESS == ret) {break;}
}
}
/*
* If we couldn't find hwloc data for any host attached to the switch,
* let's issue a warning but otherwise assume that the switch won't be
* used
*/
if (num_edges == i) {
if (verbose) {
fprintf(stderr,
"Skipping switch because no data was available for attached nodes:\n"
"\t%s\n",
netloc_pretty_print_node_t(node));
}
return NETLOC_ERROR_EMPTY;
}
// Use the node name to create the switch name
char * temp_node_name = strdup(node_name);
char * temp_name = strtok (temp_node_name,"n");
char * sw_name;
int switch_cnt = 0;
asprintf( &sw_name, "%ss%d", temp_name, switch_cnt);
// Check to see if the switch name is unique, change it if it isn't
while (check_unique_switch_name(sw_name) == NETLOC_ERROR) {
free(sw_name);
switch_cnt++;
asprintf( &sw_name, "%ss%d", temp_name, switch_cnt);
}
free(temp_node_name);
*name = sw_name;
return NETLOC_SUCCESS;
}
|