Esempio n. 1
0
static hwloc_const_nodeset_t
hwloc_fix_membind(hwloc_topology_t topology, hwloc_const_nodeset_t nodeset)
{
  hwloc_const_bitmap_t topology_nodeset = hwloc_topology_get_topology_nodeset(topology);
  hwloc_const_bitmap_t complete_nodeset = hwloc_topology_get_complete_nodeset(topology);

  if (!hwloc_topology_get_topology_cpuset(topology)) {
    /* The topology is composed of several systems, the nodeset is thus
     * ambiguous. */
    errno = EXDEV;
    return NULL;
  }

  if (!complete_nodeset) {
    /* There is no NUMA node */
    errno = ENODEV;
    return NULL;
  }

  if (hwloc_bitmap_iszero(nodeset)) {
    errno = EINVAL;
    return NULL;
  }

  if (!hwloc_bitmap_isincluded(nodeset, complete_nodeset)) {
    errno = EINVAL;
    return NULL;
  }

  if (hwloc_bitmap_isincluded(topology_nodeset, nodeset))
    return complete_nodeset;

  return nodeset;
}
int main(void)
{
    hwloc_topology_t topology;
    hwloc_bitmap_t set;
    hwloc_const_bitmap_t cset;
    hwloc_membind_policy_t policy;
    const struct hwloc_topology_support *support;
    int nbnodes;
    hwloc_obj_t obj;
    char *buffer, *s;
    unsigned i;
    int err;

    /* create a topology */
    err = hwloc_topology_init(&topology);
    if (err < 0) {
        fprintf(stderr, "failed to initialize the topology\n");
        return EXIT_FAILURE;
    }
    err = hwloc_topology_load(topology);
    if (err < 0) {
        fprintf(stderr, "failed to load the topology\n");
        hwloc_topology_destroy(topology);
        return EXIT_FAILURE;
    }

    /* retrieve the entire set of NUMA nodes and count them */
    cset = hwloc_topology_get_topology_nodeset(topology);
    nbnodes = hwloc_bitmap_weight(cset);
    if (nbnodes <= 0) {
        /* nbnodes may be -1 when there's no NUMA information,
         * or 0 when the machine is known to be non-NUMA */
        printf("this machine is not NUMA, nothing to do\n");
        hwloc_topology_destroy(topology);
        return EXIT_SUCCESS;
    }
    printf("there are %d nodes in the machine\n", nbnodes);

    /* get the process memory binding as a nodeset */
    set = hwloc_bitmap_alloc();
    if (!set) {
        fprintf(stderr, "failed to allocate a bitmap\n");
        hwloc_topology_destroy(topology);
        return EXIT_FAILURE;
    }
    err = hwloc_get_membind_nodeset(topology, set, &policy, 0);
    if (err < 0) {
        fprintf(stderr, "failed to retrieve my memory binding and policy\n");
        hwloc_topology_destroy(topology);
        hwloc_bitmap_free(set);
        return EXIT_FAILURE;
    }

    /* print the corresponding NUMA nodes */
    hwloc_bitmap_asprintf(&s, set);
    printf("bound to nodeset %s with contains:\n", s);
    free(s);
    hwloc_bitmap_foreach_begin(i, set) {
        obj = hwloc_get_numanode_obj_by_os_index(topology, i);
        printf("  node #%u (OS index %u) with %lld bytes of memory\n",
               obj->logical_index, i, (unsigned long long) obj->memory.local_memory);
    }