Blame opamgt/samples/show_switch_cost_matrix.c

Packit 857059
/* Query SA for Switch Cost  Matrix data
Packit 857059
 * and print it to a file. Monitor for any
Packit 857059
 * changes and update.
Packit 857059
 *
Packit 857059
 */
Packit 857059
// core API
Packit 857059
#include <opamgt.h>
Packit 857059
// swcost query
Packit 857059
#include <opamgt_sa.h>
Packit 857059
// fabric change notice
Packit 857059
#include <opamgt_sa_notice.h>
Packit 857059
Packit 857059
#include <stdio.h>
Packit 857059
Packit 857059
typedef struct opa_switch{
Packit 857059
	STL_LID lid;
Packit 857059
	const char * name;
Packit 857059
	struct opa_switch *next;
Packit 857059
}opa_switch;
Packit 857059
Packit 857059
FILE * matrix_file;
Packit 857059
Packit 857059
void free_cost(uint16_t ***cost, int num_rows){
Packit 857059
	int i;
Packit 857059
Packit 857059
	if (!*cost) return;
Packit 857059
Packit 857059
	for(i = 0; i < num_rows; ++i) {
Packit 857059
		if ((*cost)[i]) {
Packit 857059
			free((*cost)[i]);
Packit 857059
			(*cost)[i] = NULL;
Packit 857059
		}
Packit 857059
	}
Packit 857059
	free(*cost);
Packit 857059
	*cost = NULL;
Packit 857059
}
Packit 857059
Packit 857059
void free_switch_list(opa_switch **switchlist_head)
Packit 857059
{
Packit 857059
	if (!*switchlist_head) return;
Packit 857059
	opa_switch * temp = *switchlist_head, *temp2;
Packit 857059
	while(temp){
Packit 857059
		temp2 = temp->next;
Packit 857059
		free(temp);
Packit 857059
		temp = temp2;
Packit 857059
	}
Packit 857059
	*switchlist_head = NULL;
Packit 857059
}
Packit 857059
Packit 857059
/* Helper function to get the associated name for a particular lid
Packit 857059
 */
Packit 857059
const char * get_name(STL_LID lid, STL_NODE_RECORD *node_records, int num_node_records)
Packit 857059
{
Packit 857059
	int  i;
Packit 857059
	for (i = 0; i < num_node_records; ++i) {
Packit 857059
		if (node_records[i].RID.LID == lid)
Packit 857059
			return (const char*) node_records[i].NodeDesc.NodeString;
Packit 857059
	}
Packit 857059
	return "";
Packit 857059
}
Packit 857059
Packit 857059
/* Print Cost Matrix using the Switch Names
Packit 857059
 */
Packit 857059
void print_matrix(uint16_t **cost, opa_switch *switchlist_head)
Packit 857059
{
Packit 857059
	fprintf(matrix_file, "%64s", "");
Packit 857059
	opa_switch * iter = switchlist_head;
Packit 857059
	while(iter){
Packit 857059
		// Node Descriptions are up to 64 bytes
Packit 857059
		fprintf(matrix_file, "%64s", iter->name);
Packit 857059
		iter = iter->next;
Packit 857059
	}
Packit 857059
	fprintf(matrix_file, "\n");
Packit 857059
Packit 857059
	iter = switchlist_head;
Packit 857059
	opa_switch * iter2;
Packit 857059
	while(iter) {
Packit 857059
		fprintf(matrix_file, "%-64s", iter->name);
Packit 857059
		iter2 = switchlist_head;
Packit 857059
		while(iter2) {
Packit 857059
			if(cost[iter->lid])
Packit 857059
				fprintf(matrix_file, "%64d" , cost[iter->lid][iter2->lid]);
Packit 857059
			iter2 = iter2->next;
Packit 857059
		}
Packit 857059
		iter = iter->next;
Packit 857059
		fprintf(matrix_file, "\n");
Packit 857059
	}
Packit 857059
	fprintf(matrix_file, "\n\n\n");
Packit 857059
}
Packit 857059
Packit 857059
Packit 857059
int main(int argc, char **argv)
Packit 857059
{
Packit 857059
	OMGT_STATUS_T status = OMGT_STATUS_SUCCESS;
Packit 857059
	int exitcode = 0;
Packit 857059
Packit 857059
	struct omgt_port *port = NULL;
Packit 857059
	omgt_sa_selector_t selector;
Packit 857059
Packit 857059
	int num_classportinfo_records, num_fabricinfo_records, num_cost_records, num_switch_records;
Packit 857059
	int num_nodes = 0;
Packit 857059
Packit 857059
	STL_CLASS_PORT_INFO *classportinfo_records = NULL;
Packit 857059
	STL_FABRICINFO_RECORD *fabricinfo_records = NULL;
Packit 857059
	STL_SWITCH_COST_RECORD *cost_records = NULL;
Packit 857059
	STL_NODE_RECORD *switch_records = NULL;
Packit 857059
Packit 857059
	STL_NOTICE *notice = NULL;
Packit 857059
	size_t notice_len = 0;
Packit 857059
	struct omgt_port *context = NULL;
Packit 857059
Packit 857059
	uint16_t **cost = NULL;
Packit 857059
	int i,j;
Packit 857059
Packit 857059
	if (argc < 2) {
Packit 857059
		fprintf(stderr, "Usage: %s <output_file>\n", argv[0]);
Packit 857059
		exitcode = 1;
Packit 857059
		goto done;
Packit 857059
	}
Packit 857059
Packit 857059
	matrix_file = fopen(argv[1], "w");
Packit 857059
	if (!matrix_file) {
Packit 857059
		fprintf(stderr, "could not open output file for writing\n");
Packit 857059
		exitcode = 1;
Packit 857059
		goto done;
Packit 857059
	}
Packit 857059
Packit 857059
	// create a session
Packit 857059
	status = omgt_open_port_by_num(&port, 1, 1, NULL);
Packit 857059
	if (OMGT_STATUS_SUCCESS != status) {
Packit 857059
		fprintf(stderr, "failed to open port\n");
Packit 857059
		exitcode = 1;
Packit 857059
		goto close_file;
Packit 857059
	}
Packit 857059
Packit 857059
	// Determine if SA has Switch Cost Record capabiility
Packit 857059
	selector.InputType = InputTypeNoInput;
Packit 857059
	status = omgt_sa_get_classportinfo_records(port, &selector, &num_classportinfo_records, &classportinfo_records);
Packit 857059
	if (OMGT_STATUS_SUCCESS != status || num_classportinfo_records != 1) {
Packit 857059
		fprintf(stderr, "failed to execute classportinfo record query. MADStatus=0x%x\n", omgt_get_sa_mad_status(port));
Packit 857059
		exitcode = 1;
Packit 857059
		goto close_port;
Packit 857059
	}
Packit 857059
Packit 857059
	if (! (classportinfo_records[0].CapMask && STL_SA_CAPABILITY2_SWCOSTRECORD_SUPPORT)) {
Packit 857059
		fprintf(stderr, "SA does not support switchcost records\n");
Packit 857059
		exitcode = 1;
Packit 857059
		goto close_port;
Packit 857059
	}
Packit 857059
Packit 857059
	// Register for Cost Matrix Change Trap
Packit 857059
	if ((status = omgt_sa_register_trap(port, STL_TRAP_COST_MATRIX_CHANGE, port)) != OMGT_STATUS_SUCCESS) {
Packit 857059
		fprintf(stderr, " Error: Could not register for Trap %u: %s (%u)\n",
Packit 857059
				STL_TRAP_COST_MATRIX_CHANGE, omgt_status_totext(status), status);
Packit 857059
		exitcode = 1;
Packit 857059
		goto close_port;
Packit 857059
	}
Packit 857059
Packit 857059
	opa_switch *head_switch = NULL, *temp_switch = NULL;
Packit 857059
	while(!exitcode){
Packit 857059
		selector.InputType = InputTypeNoInput;
Packit 857059
Packit 857059
		/*	Query SA for fabric information to determine how big to make
Packit 857059
		 *	our own cost matrix storage
Packit 857059
		 */
Packit 857059
		status = omgt_sa_get_fabric_info_records(port, &selector, &num_fabricinfo_records, &fabricinfo_records);
Packit 857059
		if (OMGT_STATUS_SUCCESS != status || num_fabricinfo_records < 1) {
Packit 857059
			fprintf(stderr, "failed to get fabricinfo. MADStatus=0x%x\n", omgt_get_sa_mad_status(port));
Packit 857059
			exitcode = 1;
Packit 857059
			goto cleanup;
Packit 857059
		}
Packit 857059
Packit 857059
		/* Using total number of nodes will allocate more space than necessary,
Packit 857059
		 * but allows simpler processing of cost data
Packit 857059
		 */
Packit 857059
		num_nodes = fabricinfo_records[0].NumSwitches + fabricinfo_records[0].NumHFIs;
Packit 857059
		if ((cost = calloc(1, num_nodes * sizeof(uint16_t*))) == NULL) {
Packit 857059
			fprintf(stderr, "failed to allocate memory\n");
Packit 857059
			exitcode = 1;
Packit 857059
			goto cleanup;
Packit 857059
		}
Packit 857059
Packit 857059
		/*	Query SA for switch records so we can have the names of the switches
Packit 857059
		 *	as the cost records are defined with LIDs
Packit 857059
		 */
Packit 857059
		selector.InputType = InputTypeNodeType; // select records by type
Packit 857059
		selector.InputValue.NodeRecord.NodeType = IBA_NODE_SWITCH; // select only Switches
Packit 857059
		status = omgt_sa_get_node_records(port, &selector, &num_switch_records, &switch_records);
Packit 857059
		if (OMGT_STATUS_SUCCESS != status) {
Packit 857059
			fprintf(stderr, "failed to execute node record query. MADStatus=0x%x\n", omgt_get_sa_mad_status(port));
Packit 857059
			exitcode = 1;
Packit 857059
			goto cleanup;
Packit 857059
		}
Packit 857059
Packit 857059
		/* Create a linked list of switches in this fabric
Packit 857059
		 */
Packit 857059
		for (i = 0; i < num_switch_records; ++i) {
Packit 857059
			opa_switch * next_switch;
Packit 857059
			if ((next_switch = malloc(sizeof(opa_switch))) == NULL) {
Packit 857059
				fprintf(stderr, "failed to allocate memory\n");
Packit 857059
				exitcode = 1;
Packit 857059
				goto cleanup;
Packit 857059
			}
Packit 857059
Packit 857059
			next_switch->lid = switch_records[i].RID.LID;
Packit 857059
			next_switch->name = get_name(switch_records[i].RID.LID, switch_records, num_switch_records);
Packit 857059
			next_switch->next = NULL;
Packit 857059
Packit 857059
			if (temp_switch)
Packit 857059
				temp_switch->next = next_switch;
Packit 857059
			else
Packit 857059
				head_switch = next_switch;
Packit 857059
Packit 857059
			temp_switch = next_switch;
Packit 857059
		}
Packit 857059
		temp_switch = NULL;
Packit 857059
Packit 857059
		// reset selector to appropriate type for cost query
Packit 857059
		selector.InputType = InputTypeNoInput;
Packit 857059
		status = omgt_sa_get_switchcost_records(port, &selector, &num_cost_records, &cost_records);
Packit 857059
		if (OMGT_STATUS_SUCCESS != status) {
Packit 857059
			fprintf(stderr, "failed to execute cost record query. MADStatus=0x%x\n", omgt_get_sa_mad_status(port));
Packit 857059
			exitcode = 1;
Packit 857059
			goto cleanup;
Packit 857059
		}
Packit 857059
Packit 857059
		/* Copy returned Cost matrix data into our own format
Packit 857059
		 * When requesting the entire matrix, only the lower
Packit 857059
		 * diagonal is returned as it is symmetric
Packit 857059
		 */
Packit 857059
		int slid, dlid;
Packit 857059
		STL_SWITCH_COST_RECORD cost_record;
Packit 857059
		for (i = 0; i < num_cost_records; ++i) {
Packit 857059
			cost_record = cost_records[i];
Packit 857059
			slid = cost_record.SLID;
Packit 857059
			/* Cost records contain 64 entries, but all may not be filled
Packit 857059
			 * Checking the DLID is > 0 ensures the entry is valid
Packit 857059
			 */
Packit 857059
			for (j = 0; j < STL_SWITCH_COST_NUM_ENTRIES && cost_record.Cost[j].DLID > 0; ++j) {
Packit 857059
				dlid = cost_record.Cost[j].DLID;
Packit 857059
Packit 857059
				// Initialize all entries in cost to zero.
Packit 857059
				// The entries of interest will be overwritten with
Packit 857059
				// actual cost values from the SA
Packit 857059
				if (!cost[slid]) {
Packit 857059
					if ((cost[slid] = calloc(1, num_nodes * sizeof(uint16_t))) == NULL) {
Packit 857059
						fprintf(stderr, "failed to allocate memory\n");
Packit 857059
						exitcode = 1;
Packit 857059
						goto cleanup;
Packit 857059
					}
Packit 857059
				}
Packit 857059
				if (!cost[dlid]) {
Packit 857059
					if ((cost[dlid] = calloc(1, num_nodes * sizeof(uint16_t))) == NULL) {
Packit 857059
						fprintf(stderr, "failed to allocate memory\n");
Packit 857059
						exitcode = 1;
Packit 857059
						goto cleanup;
Packit 857059
					}
Packit 857059
				}
Packit 857059
				// add this cost to matrix for slid/dlid
Packit 857059
				cost[slid][dlid] = cost[dlid][slid] = cost_record.Cost[j].value;
Packit 857059
			}
Packit 857059
		}
Packit 857059
Packit 857059
		print_matrix(cost, head_switch);
Packit 857059
Packit 857059
		printf("\nMonitoring for any changes...\n");
Packit 857059
		fflush(matrix_file);
Packit 857059
Packit 857059
		// monitor cluster for changes, -1 = indefinite wait time
Packit 857059
		if ((status = omgt_sa_get_notice_report(port, &notice, &notice_len, (void **)&context, -1)) != OMGT_STATUS_SUCCESS) {
Packit 857059
			fprintf(stderr, "Error: Could not wait for Notice: %s (%u)\n",
Packit 857059
					omgt_status_totext(status), status);
Packit 857059
			exitcode = 1;
Packit 857059
			goto cleanup;
Packit 857059
		}
Packit 857059
Packit 857059
		// we only registered for Cost Matrix Change notice, if we got something else it's an error
Packit 857059
		if (notice->Attributes.Generic.TrapNumber != STL_TRAP_COST_MATRIX_CHANGE) {
Packit 857059
			fprintf(stderr, "Unhandled Trap Received: %u\n", notice->Attributes.Generic.TrapNumber);
Packit 857059
			exitcode = 1;
Packit 857059
			goto cleanup;
Packit 857059
		}
Packit 857059
Packit 857059
cleanup:
Packit 857059
		if (notice) {
Packit 857059
			free(notice);
Packit 857059
			notice = NULL;
Packit 857059
		}
Packit 857059
Packit 857059
		if (fabricinfo_records) {
Packit 857059
			omgt_sa_free_records(fabricinfo_records);
Packit 857059
			fabricinfo_records = NULL;
Packit 857059
		}
Packit 857059
		if (cost_records) {
Packit 857059
			omgt_sa_free_records(cost_records);
Packit 857059
			cost_records = NULL;
Packit 857059
		}
Packit 857059
		if (switch_records) {
Packit 857059
			omgt_sa_free_records(switch_records);
Packit 857059
			switch_records = NULL;
Packit 857059
		}
Packit 857059
Packit 857059
		free_switch_list(&head_switch);
Packit 857059
		free_cost(&cost, num_nodes);
Packit 857059
Packit 857059
	} //end while
Packit 857059
Packit 857059
Packit 857059
	if ((status = omgt_sa_unregister_trap(port, STL_TRAP_COST_MATRIX_CHANGE)) != OMGT_STATUS_SUCCESS) {
Packit 857059
		fprintf(stderr, "Error: Could not unregister for Trap %u: %s (%u)\n",
Packit 857059
				STL_TRAP_COST_MATRIX_CHANGE, omgt_status_totext(status), status);
Packit 857059
		if (!exitcode) exitcode = 1;
Packit 857059
	}
Packit 857059
Packit 857059
close_port:
Packit 857059
	if (classportinfo_records) omgt_sa_free_records(classportinfo_records);
Packit 857059
Packit 857059
	//close our session
Packit 857059
	omgt_close_port(port);
Packit 857059
Packit 857059
close_file:
Packit 857059
	fclose(matrix_file);
Packit 857059
Packit 857059
done:
Packit 857059
	return exitcode;
Packit 857059
}