Example #1
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;
}
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;
}