def _filter_for_numa_threads(possible, wantthreads):
    """Filter to topologies which closest match to NUMA threads
    :param possible: list of nova.objects.VirtCPUTopology
    :param wantthreads: ideal number of threads

    Determine which topologies provide the closest match to
    the number of threads desired by the NUMA topology of
    the instance.

    The possible topologies may not have any entries
    which match the desired thread count. So this method
    will find the topologies which have the closest
    matching count.

    ie if wantthreads is 4 and the possible topologies
    has entries with 6, 3, 2 or 1 threads, it will
    return the topologies which have 3 threads, as
    this is the closest match not greater than 4.

    :returns: list of nova.objects.VirtCPUTopology
    """

    # First figure out the largest available thread
    # count which is not greater than wantthreads
    mostthreads = 0
    for topology in possible:
        if topology.threads > wantthreads:
            continue
        if topology.threads > mostthreads:
            mostthreads = topology.threads

    # Now restrict to just those topologies which
    # match the largest thread count
    bestthreads = []
    for topology in possible:
        if topology.threads != mostthreads:
            continue
        bestthreads.append(topology)

    return bestthreads