int main(int argc, char ** argv) {
    int ret, i;
    netloc_topology_t topology;
    char ** search_uris = NULL;
    int num_search_uris = 1;
    int num_all_networks = 0;
    netloc_network_t **all_networks = NULL;
    char * filename = NULL;

    /*
     * Parse Args
     */
    if( 0 != (ret = parse_args(argc, argv)) ) {
        printf("Usage: %s [<input directory>] [%s|%s] [%s|%s <export type>] [%s|%s] [%s|%s]\n",
               argv[0],
               ARG_FULL, ARG_SHORT_FULL,
               ARG_EXPORT, ARG_SHORT_EXPORT,
               ARG_VERBOSE, ARG_SHORT_VERBOSE,
               ARG_HELP, ARG_SHORT_HELP);
        printf("       Default <input directory> = current working directory\n");
        printf("       Valid Options for %s:\n", ARG_EXPORT );
        for(i = 0; i < num_valid_export_types; ++i ) {
            printf("\t\t%s\n", valid_export_types[i]);
        }
        return NETLOC_ERROR;
    }


    /*
     * Find all networks
     */
    search_uris = (char **)malloc(sizeof(char*) * num_search_uris);
    asprintf(&search_uris[0], "file://%s", indir);
    ret = netloc_foreach_network((const char * const *)search_uris,
                                 1,
                                 NULL, NULL,
                                 &num_all_networks,
                                 &all_networks);

    if( verbose ) {
        printf("  Found %d Networks.\n", num_all_networks);
        printf("---------------------------------------------------\n");
    }

    for(i = 0; i < num_all_networks; ++i) {
        /*
         * Attach to the topology
         */
        ret = netloc_attach(&topology, *all_networks[i]);
        if( NETLOC_SUCCESS != ret ) {
            return ret;
        }

        /*
         * Display the topology
         */
        if( 0 == strncmp("screen", export_type, strlen("screen") )) {
            ret = display_topo_screen(topology, all_networks[i] );
        }
        else if( 0 == strncmp("graphml", export_type, strlen("graphml")) ) {
            asprintf(&filename, "%s-%s.graphml",
                     netloc_decode_network_type(all_networks[i]->network_type),
                     all_networks[i]->subnet_id);

            printf("Network: %s\n", netloc_pretty_print_network_t(all_networks[i]) );
            printf("\tFilename: %s\n", filename);

            ret = netloc_topology_export_graphml(topology, filename);
        }
        else if( 0 == strncmp("gexf", export_type, strlen("gexf")) ) {
            asprintf(&filename, "%s-%s.gexf",
                     netloc_decode_network_type(all_networks[i]->network_type),
                     all_networks[i]->subnet_id);

            printf("Network: %s\n", netloc_pretty_print_network_t(all_networks[i]) );
            printf("\tFilename: %s\n", filename);

            ret = netloc_topology_export_gexf(topology, filename);
        }

        if( NETLOC_SUCCESS != ret ) {
            return ret;
        }

        /*
         * Detach from the topology
         */
        ret = netloc_detach(topology);
        if( NETLOC_SUCCESS != ret ) {
            return ret;
        }
    }

    if( NULL != filename ) {
        free(filename);
        filename = NULL;
    }

    if( NULL != search_uris ) {
        for(i = 0; i < num_search_uris; ++i ) {
            free(search_uris[i]);
            search_uris[i] = NULL;
        }
        free(search_uris);
        search_uris = NULL;
    }

    return NETLOC_SUCCESS;
}
int main(int argc, char *argv[])
{
  netloc_map_t map;
  netloc_map_server_t srcserver, dstserver;
  hwloc_topology_t srctopo, dsttopo;
  hwloc_obj_t srcobj, dstobj;
  netloc_map_paths_t paths;
  unsigned nr_paths, nr_edges, i, j;
  struct netloc_map_edge_s *edges;
  unsigned flags = 0x3;
  char *path;
  int err;

  if (argc > 2) {
    if (!strcmp(argv[1], "--flags")) {
      flags = (unsigned) strtoul(argv[2], NULL, 0);
      argc -= 2;
      argv += 2;
    }
  }

  if (argc < 6) {
    fprintf(stderr, "%s [options] <datadir> <srcserver> <srcpu> <dstserver> <dstpu>\n", argv[0]);
    fprintf(stderr, "Example: %s mynetlocdata server2 1 server3 7\n", argv[0]);
    fprintf(stderr, "  Loads netloc map from 'mynetlocdata' directory and display all paths\n");
    fprintf(stderr, "  from server 'server2' PU #1 to server 'server3' PU #7.\n");
    fprintf(stderr, "Options:\n");
    fprintf(stderr, "  --flags N    Use value N as map paths flags. Default is 3 which displays all edges.\n");
    exit(EXIT_FAILURE);
  }

  err = netloc_map_create(&map);
  if (err) {
    fprintf(stderr, "Failed to create the map\n");
    exit(EXIT_FAILURE);
  }

  asprintf(&path, "%s/hwloc", argv[1]);
  err = netloc_map_load_hwloc_data(map, path);
  free(path);
  if (err) {
    fprintf(stderr, "Failed to load hwloc data\n");
    return -1;
  }

  asprintf(&path, "file://%s/netloc", argv[1]);
  err = netloc_map_load_netloc_data(map, path);
  free(path);
  if (err) {
    fprintf(stderr, "Failed to load netloc data\n");
    return -1;
  }

  err = netloc_map_build(map, 0);
  if (err) {
    fprintf(stderr, "Failed to build map data\n");
    return -1;
  }

  err = netloc_map_name2server(map, argv[2], &srcserver);
  if (err) {
    fprintf(stderr, "Could not find src server %s\n", argv[2]);
    return -1;
  }
  err = netloc_map_server2hwloc(srcserver, &srctopo);
  if (err) {
    fprintf(stderr, "Could not find src server %s hwloc topology\n", argv[2]);
    return -1;
  }
  srcobj = hwloc_get_obj_by_type(srctopo, HWLOC_OBJ_PU, atoi(argv[3]));
  if (!srcobj) {
    fprintf(stderr, "Could not find src server %s PU #%s\n", argv[2], argv[3]);
    return -1;
  }

  err = netloc_map_name2server(map, argv[4], &dstserver);
  if (err) {
    fprintf(stderr, "Could not find dst server %s\n", argv[4]);
    return -1;
  }
  err = netloc_map_server2hwloc(dstserver, &dsttopo);
  if (err) {
    fprintf(stderr, "Could not find dst server %s hwloc topology\n", argv[4]);
    return -1;
  }
  dstobj = hwloc_get_obj_by_type(dsttopo, HWLOC_OBJ_PU, atoi(argv[5]));
  if (!dstobj) {
    fprintf(stderr, "Could not find dst server %s PU #%s\n", argv[4], argv[5]);
    return -1;
  }

  err = netloc_map_paths_build(map,
			       srctopo, srcobj,
			       dsttopo, dstobj,
			       flags,
			       &paths, &nr_paths);
  if (err < 0) {
    fprintf(stderr, "Failed to build paths\n");
    return -1;
  }

  printf("got %u paths\n", nr_paths);
  for(i=0; i<nr_paths; i++) {
    printf(" path #%u:\n", i);

    err = netloc_map_paths_get(paths, i, &edges, &nr_edges);
    assert(!err);
    printf("  %u edges\n", nr_edges);
    for(j=0; j<nr_edges; j++) {
      struct netloc_map_edge_s *edge = &edges[j];
      printf("   edge #%u type %d: ", j, edge->type);
      switch (edge->type) {
      case NETLOC_MAP_EDGE_TYPE_NETLOC:
        printf("netloc from %s to %s in subnet type %s id %s\n",
	       edge->netloc.edge->src_node_id,
	       edge->netloc.edge->dest_node_id,
	       netloc_decode_network_type(netloc_access_network_ref(edge->netloc.topology)->network_type),
	       netloc_access_network_ref(edge->netloc.topology)->subnet_id);
	break;
      case NETLOC_MAP_EDGE_TYPE_HWLOC_PARENT:
	printf("hwloc UP from %s:%u (%s) to parent %s:%u (%s) weight %u\n",
	       hwloc_obj_type_string(edge->hwloc.src_obj->type), edge->hwloc.src_obj->logical_index, edge->hwloc.src_obj->name ? : "<unnamed>",
	       hwloc_obj_type_string(edge->hwloc.dest_obj->type), edge->hwloc.dest_obj->logical_index, edge->hwloc.dest_obj->name ? : "<unnamed>",
	       edge->hwloc.weight);
	break;
      case NETLOC_MAP_EDGE_TYPE_HWLOC_HORIZONTAL:
	printf("hwloc HORIZONTAL from %s:%u (%s) to cousin %s:%u (%s) weight %u\n",
	       hwloc_obj_type_string(edge->hwloc.src_obj->type), edge->hwloc.src_obj->logical_index, edge->hwloc.src_obj->name ? : "<unnamed>",
	       hwloc_obj_type_string(edge->hwloc.dest_obj->type), edge->hwloc.dest_obj->logical_index, edge->hwloc.dest_obj->name ? : "<unnamed>",
	       edge->hwloc.weight);
	break;
      case NETLOC_MAP_EDGE_TYPE_HWLOC_CHILD:
	printf("hwloc DOWN from %s:%u (%s) to child %s:%u (%s) weight %u\n",
	       hwloc_obj_type_string(edge->hwloc.src_obj->type), edge->hwloc.src_obj->logical_index, edge->hwloc.src_obj->name ? : "<unnamed>",
	       hwloc_obj_type_string(edge->hwloc.dest_obj->type), edge->hwloc.dest_obj->logical_index, edge->hwloc.dest_obj->name ? : "<unnamed>",
	       edge->hwloc.weight);
	break;
      case NETLOC_MAP_EDGE_TYPE_HWLOC_PCI:
	printf("hwloc PCI from %s:%u (%s) to child %s:%u (%s) weight %u\n",
	       hwloc_obj_type_string(edge->hwloc.src_obj->type), edge->hwloc.src_obj->logical_index, edge->hwloc.src_obj->name ? : "<unnamed>",
	       hwloc_obj_type_string(edge->hwloc.dest_obj->type), edge->hwloc.dest_obj->logical_index, edge->hwloc.dest_obj->name ? : "<unnamed>",
	       edge->hwloc.weight);
	break;
      }
    }
  }

  netloc_map_paths_destroy(paths);

  netloc_map_put_hwloc(map, srctopo);
  netloc_map_put_hwloc(map, dsttopo);
  netloc_map_destroy(map);
  return 0;
}
Example #3
0
int main(int argc, char *argv[])
{
  netloc_map_t map;
  netloc_topology_t *ntopos;
  netloc_map_server_t *servers;
  char *datadir, *path;
  unsigned nr_ntopos, nr_servers, i, j;
  int verbose = 0;
  int err;

  argc--;
  argv++;
  if (argc >= 1) {
    if (!strcmp(argv[0], "--verbose")) {
      verbose = 1;
      argc--;
      argv++;
    }
  }

  if (argc < 1) {
    fprintf(stderr, "Missing data directory argument\n");
    return -1;
  }
  datadir = argv[0];
  argv++;
  argc--;

  err = netloc_map_create(&map);
  if (err) {
    fprintf(stderr, "Failed to create the map\n");
    return -1;
  }

  asprintf(&path, "%s/hwloc", datadir);
  err = netloc_map_load_hwloc_data(map, path);
  if (err) {
    fprintf(stderr, "Failed to load hwloc data from %s\n", path);
    return -1;
  }
  free(path);

  asprintf(&path, "file://%s/netloc", datadir);
  err = netloc_map_load_netloc_data(map, path);
  if (err) {
    fprintf(stderr, "Failed to load netloc data from %s\n", path);
    return -1;
  }
  free(path);

  err = netloc_map_build(map, NETLOC_MAP_BUILD_FLAG_COMPRESS_HWLOC);
  if (err) {
    fprintf(stderr, "Failed to build map data\n");
    return -1;
  }

  if (verbose)
    netloc_map_dump(map);

  err = netloc_map_get_subnets(map, &nr_ntopos, &ntopos);
  if (err) {
    fprintf(stderr, "Failed to get the number of subnets\n");
    return -1;
  }

  for(i=0; i<nr_ntopos; i++) {
    netloc_topology_t ntopo = ntopos[i];
    netloc_network_t *net = netloc_access_network_ref(ntopo);
    const char *type = netloc_decode_network_type(net->network_type);

    printf("Subnet type %s id %s\n",
	   type, net->subnet_id);

    netloc_dt_lookup_table_t *nodes;
    err = netloc_get_all_host_nodes(ntopo, &nodes);
    if (err)
      continue;

    netloc_dt_lookup_table_iterator_t *iter = netloc_dt_lookup_table_iterator_t_construct(nodes);
    while( !netloc_lookup_table_iterator_at_end(iter) ) {
      const char *key;
      netloc_node_t *nnode;
      netloc_map_port_t port;
      netloc_map_server_t server;
      hwloc_topology_t htopo;
      hwloc_obj_t hobj;
      const char *servername;

      key = netloc_lookup_table_iterator_next_key(iter);
      if (NULL == key)
	break;
      
      nnode = (netloc_node_t*) netloc_lookup_table_access(nodes, key);
      if (NETLOC_NODE_TYPE_INVALID == nnode->node_type)
	continue;

      err = netloc_map_netloc2port(map, ntopo, nnode, NULL, &port);
      if (err < 0)
	continue;

      err = netloc_map_port2hwloc(port, &htopo, &hobj);
      if (err < 0)
	continue;

      err = netloc_map_port2server(port, &server);
      if (err < 0)
	continue;

      err = netloc_map_server2name(server, &servername);
      if (err < 0)
	continue;

      printf(" node %s connected through object %s address %s\n", servername, hobj->name, nnode->physical_id);

      netloc_map_put_hwloc(map, htopo);
    }

    netloc_dt_lookup_table_iterator_t_destruct(iter);
    netloc_lookup_table_destroy(nodes);
    free(nodes);
  }

  err = netloc_map_get_nbservers(map);
  if (err < 0) {
    fprintf(stderr, "Failed to get the number of servers\n");
    return -1;
  }
  nr_servers = err;

  servers = malloc(nr_servers * sizeof(*servers));
  if (!servers) {
    fprintf(stderr, "Failed to allocate servers array\n");
    return -1;
  }

  err = netloc_map_get_servers(map, 0, nr_servers, servers);
  if (err < 0) {
    fprintf(stderr, "Failed to get servers\n");
    return -1;
  }
  assert((unsigned) err == nr_servers);

  for(i=0; i<nr_servers; i++) {
    const char *servername = "unknown";
    netloc_map_port_t *ports;
    unsigned ports_nr;

    err = netloc_map_server2name(servers[i], &servername);
    assert(!err);
    printf("Server %s\n", servername);

    err = netloc_map_get_server_ports(servers[i], &ports_nr, &ports);
    assert(!err);

    if (!ports)
      continue;

    for(j=0; j<ports_nr; j++) {
      netloc_topology_t ntopo;
      netloc_network_t *net;
      netloc_node_t *nnode;
      netloc_edge_t *nedge;
      hwloc_topology_t htopo;
      hwloc_obj_t hobj;

      err = netloc_map_port2netloc(ports[j], &ntopo, &nnode, &nedge);
      assert(!err);
      net = netloc_access_network_ref(ntopo);

      err = netloc_map_port2hwloc(ports[j], &htopo, &hobj);
      assert(!err);

      const char *type = netloc_decode_network_type(net->network_type);
      printf(" port %s of device %s (id %s)\n"
	     "  on subnet type %s id %s\n"
	     "   towards node %s port %s\n",
	     nedge ? nedge->src_port_id : "<unknown>",
	     hobj->name,
	     nnode ? nnode->physical_id : "<none>",
	     type, 
	     net->subnet_id,
	     nedge ? nedge->dest_node_id : "<none>",
	     nedge ? nedge->dest_port_id : "<unknown>");

      netloc_map_put_hwloc(map, htopo);
    }
  }

  free(servers);
  free(ntopos);
  netloc_map_destroy(map);

  return 0;
}
netloc_data_collection_handle_t * netloc_dc_create(netloc_network_t *network, char * dir)
{
    netloc_data_collection_handle_t *handle = NULL;

    /*
     * Sanity Checks
     */
    if( NULL == network ) {
        fprintf(stderr, "Error: Null network provided\n");
        return NULL;
    }

    if( NETLOC_NETWORK_TYPE_INVALID == network->network_type ) {
        fprintf(stderr, "Error: Invalid network type provided\n");
        return NULL;
    }

    if( NULL == network->subnet_id ) {
        fprintf(stderr, "Error: Null subnet provided\n");
        return NULL;
    }

    /*
     * Setup the handle to the data files
     */
    handle = netloc_dt_data_collection_handle_t_construct();

    handle->network       = netloc_dt_network_t_dup(network);

    asprintf(&handle->unique_id_str, "%s-%s",
             netloc_decode_network_type(network->network_type),
             network->subnet_id);

    /*
     * Setup the data filenames
     */
    if( NULL == dir ) {
        dir = strdup("");
    }
    asprintf(&handle->filename_nodes, "%s/%s-nodes.ndat", dir, handle->unique_id_str);
    asprintf(&handle->filename_physical_paths, "%s/%s-phy-paths.ndat", dir, handle->unique_id_str);
    asprintf(&handle->filename_logical_paths, "%s/%s-log-paths.ndat", dir, handle->unique_id_str);
    asprintf(&handle->data_uri, "file://%s", dir);

    handle->is_open       = true;
    handle->is_read_only  = false;

    /*
     * Set metadata and network data in the JSON files
     */
    handle->node_data = json_object();
    json_object_set_new(handle->node_data, JSON_NODE_FILE_NETWORK_INFO, netloc_dt_network_t_json_encode(handle->network));
    handle->node_data_acc = json_object();

    handle->path_data = json_object();
    json_object_set_new(handle->path_data, JSON_NODE_FILE_NETWORK_INFO, netloc_dt_network_t_json_encode(handle->network));
    handle->path_data_acc = json_object();

    handle->phy_path_data = json_object();
    json_object_set_new(handle->phy_path_data, JSON_NODE_FILE_NETWORK_INFO, netloc_dt_network_t_json_encode(handle->network));
    handle->phy_path_data_acc = json_object();

    return handle;
}