Skip to content

Commit

Permalink
numactl: variable_programs
Browse files Browse the repository at this point in the history
This version has a small correction: the --interleave option had called
   numa_set_interleave_mask() twice (redundant).

This patch modifies numactl, migspeed, memhog, migratepages, numademo and
stream_main to use variable-length bit masks (the bitmask structure).

Diffed against numactl-1.0.2

Signed-off-by: Cliff Wickman <cpw@sgi.com>
  • Loading branch information
cpwickman authored and filbranden committed Jul 23, 2014
1 parent 9916258 commit 2a3f98c
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 77 deletions.
15 changes: 8 additions & 7 deletions memhog.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,15 @@ void hog(void *map)
int main(int ac, char **av)
{
char *map;
nodemask_t nodes, gnodes;
struct bitmask *nodes, *gnodes;
int policy, gpolicy;
int ret = 0;
int loose = 0;
int i;
int fd = -1;

nodemask_zero(&nodes);
nodes = allocate_nodemask();
gnodes = allocate_nodemask();

while (av[1] && av[1][0] == '-') {
switch (av[1][1]) {
Expand Down Expand Up @@ -106,19 +107,19 @@ int main(int ac, char **av)
if (map == (char*)-1)
err("mmap");

if (mbind(map, length, policy, nodes.n, NUMA_NUM_NODES + 1, 0) < 0)
if (mbind(map, length, policy, nodes->maskp, nodes->size, 0) < 0)
terr("mbind");

gpolicy = -1;
if (get_mempolicy(&gpolicy, gnodes.n, NUMA_NUM_NODES + 1, map,
MPOL_F_ADDR) < 0)
if (get_mempolicy(&gpolicy, gnodes->maskp, gnodes->size, map, MPOL_F_ADDR) < 0)
terr("get_mempolicy");
if (!loose && policy != gpolicy) {
ret = 1;
printf("policy %d gpolicy %d\n", policy, gpolicy);
}
if (!loose && !nodemask_equal(&gnodes, &nodes)) {
printf("nodes differ %lx, %lx!\n", gnodes.n[0], nodes.n[0]);
if (!loose && !bitmask_equal(gnodes, nodes)) {
printf("nodes differ %lx, %lx!\n",
gnodes->maskp[0], nodes->maskp[0]);
ret = 1;
}

Expand Down
8 changes: 4 additions & 4 deletions migratepages.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@
#include <string.h>
#include <unistd.h>
#include <stdarg.h>
#include "numaif.h"
#include "numa.h"
#include "numaif.h"
#include "numaint.h"
#include "util.h"

Expand Down Expand Up @@ -62,8 +62,8 @@ int main(int argc, char *argv[])
char *end;
int rc;
int pid;
nodemask_t fromnodes;
nodemask_t tonodes;
struct bitmask *fromnodes;
struct bitmask *tonodes;

while ((c = getopt_long(argc,argv,"h", opts, NULL)) != -1) {
switch (c) {
Expand All @@ -87,7 +87,7 @@ int main(int argc, char *argv[])
fromnodes = nodemask(argv[1]);
tonodes = nodemask(argv[2]);

rc = numa_migrate_pages(pid, &fromnodes, &tonodes);
rc = numa_migrate_pages(pid, fromnodes, tonodes);

if (rc < 0) {
perror("migrate_pages");
Expand Down
19 changes: 8 additions & 11 deletions migspeed.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#include <stdio.h>
#include <stdlib.h>
#include "numa.h"
#include <numaif.h>
#include "numaif.h"
#include <time.h>
#include <errno.h>
#include <malloc.h>
Expand Down Expand Up @@ -71,8 +71,8 @@ int main(int argc, char *argv[])
struct timespec result;
unsigned long bytes;
double duration, mbytes;
nodemask_t from;
nodemask_t to;
struct bitmask *from;
struct bitmask *to;

pagesize = getpagesize();

Expand Down Expand Up @@ -100,8 +100,7 @@ int main(int argc, char *argv[])
usage();

if (verbose > 1)
printf("numa_max_node = %d NUMA_NUM_NODES=%d\n",
numa_max_node(), NUMA_NUM_NODES);
printf("numa_max_node = %d\n", numa_max_node());

from = nodemask(argv[optind]);
if (errno) {
Expand All @@ -110,7 +109,7 @@ int main(int argc, char *argv[])
}

if (verbose)
printmask("From", &from);
printmask("From", from);

if (!argv[optind+1])
usage();
Expand All @@ -122,7 +121,7 @@ int main(int argc, char *argv[])
}

if (verbose)
printmask("To", &to);
printmask("To", to);

bytes = pages * pagesize;

Expand All @@ -137,8 +136,7 @@ int main(int argc, char *argv[])
exit(2);
}

if (mbind(memory, bytes, MPOL_BIND, (unsigned long *)&from,
NUMA_NUM_NODES+1, 0) < 0)
if (mbind(memory, bytes, MPOL_BIND, from->maskp, from->size, 0) < 0)
numa_error("mbind");

if (verbose)
Expand All @@ -153,8 +151,7 @@ int main(int argc, char *argv[])
displaymap();
clock_gettime(CLOCK_REALTIME, &start);

if (mbind(memory, bytes, MPOL_BIND, (unsigned long *)&to,
NUMA_NUM_NODES+1, MPOL_MF_MOVE)<0)
if (mbind(memory, bytes, MPOL_BIND, to->maskp, to->size, MPOL_MF_MOVE) <0)
numa_error("memory move");

clock_gettime(CLOCK_REALTIME, &end);
Expand Down
73 changes: 39 additions & 34 deletions numactl.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
#include <unistd.h>
#include <stdarg.h>
#include <ctype.h>
#include "numaif.h"
#include "numa.h"
#include "numaif.h"
#include "numaint.h"
#include "util.h"
#include "shm.h"
Expand Down Expand Up @@ -93,31 +93,32 @@ void usage_msg(char *msg, ...)

void show_physcpubind(void)
{
int ncpus = 8192;
int ncpus = number_of_configured_cpus();

for (;;) {
int cpubufsize = round_up(ncpus, BITS_PER_LONG) / BYTES_PER_LONG;
unsigned long cpubuf[cpubufsize / sizeof(long)];

memset(cpubuf,0,cpubufsize);
if (numa_sched_getaffinity(0, cpubufsize, cpubuf) < 0) {
struct bitmask *cpubuf;

cpubuf = bitmask_alloc(ncpus);

if (numa_sched_getaffinity(0, cpubuf) < 0) {
if (errno == EINVAL && ncpus < 1024*1024) {
ncpus *= 2;
continue;
}
err("sched_get_affinity");
}
printcpumask("physcpubind", cpubuf, cpubufsize);
printcpumask("physcpubind", cpubuf);
break;
}
}

void show(void)
{
unsigned long prefnode;
nodemask_t membind, interleave, cpubind;
struct bitmask *membind, *interleave, *cpubind;
unsigned long cur;
int policy;
int numa_num_nodes = number_of_possible_nodes();

if (numa_available() < 0) {
show_physcpubind();
Expand Down Expand Up @@ -153,17 +154,17 @@ void show(void)
printf("%ld (interleave next)\n",cur);
break;
case MPOL_BIND:
printf("%d\n", find_first_bit(&membind, NUMA_NUM_NODES));
printf("%d\n", find_first_bit(&membind, numa_num_nodes));
break;
}
if (policy == MPOL_INTERLEAVE) {
printmask("interleavemask", &interleave);
printmask("interleavemask", interleave);
printf("interleavenode: %ld\n", cur);
}
show_physcpubind();
printmask("cpubind", &cpubind); // for compatibility
printmask("nodebind", &cpubind);
printmask("membind", &membind);
printmask("cpubind", cpubind); // for compatibility
printmask("nodebind", cpubind);
printmask("membind", membind);
}

char *fmt_mem(unsigned long long mem, char *buf)
Expand Down Expand Up @@ -199,11 +200,15 @@ static void print_distances(int maxnode)
void print_node_cpus(int node)
{
int len = 1;
int conf_cpus = number_of_configured_cpus();

for (;;) {
int i;
unsigned long cpus[len];
int i, err;
struct bitmask *cpus;

cpus = bitmask_alloc(conf_cpus);
errno = 0;
int err = numa_node_to_cpus(node, cpus, len * sizeof(long));
err = numa_node_to_cpus(node, cpus);
if (err < 0) {
if (errno == ERANGE) {
len *= 2;
Expand All @@ -212,7 +217,7 @@ void print_node_cpus(int node)
break;
}
for (i = 0; i < len*BITS_PER_LONG; i++)
if (test_bit(i, cpus))
if (bitmask_isbitset(cpus, i))
printf(" %d", i);
break;
}
Expand All @@ -222,7 +227,7 @@ void print_node_cpus(int node)
void hardware(void)
{
int i;
int maxnode = numa_max_node();
int maxnode = number_of_configured_nodes()-1;
printf("available: %d nodes (0-%d)\n", 1+maxnode, maxnode);
for (i = 0; i <= maxnode; i++) {
char buf[64];
Expand Down Expand Up @@ -317,14 +322,15 @@ void get_short_opts(struct option *o, char *s)

int main(int ac, char **av)
{
int c, i, nnodes=0;
int c, ncpus, i, nnodes=0;
long node=-1;
char *end;
char shortopts[array_len(opts)*2 + 1];
struct bitmask *mask;

get_short_opts(opts,shortopts);
while ((c = getopt_long(ac, av, shortopts, opts, NULL)) != -1) {
nodemask_t mask;
switch (c) {
while ((c = getopt_long(ac, av, shortopts, opts, NULL)) != -1) {
switch (c) {
case 's': /* --show */
show();
exit(0);
Expand All @@ -338,9 +344,9 @@ int main(int ac, char **av)
errno = 0;
setpolicy(MPOL_INTERLEAVE);
if (shmfd >= 0)
numa_interleave_memory(shmptr, shmlen, &mask);
numa_interleave_memory(shmptr, shmlen, mask);
else
numa_set_interleave_mask(&mask);
numa_set_interleave_mask(mask);
checkerror("setting interleave mask");
break;
case 'N': /* --cpunodebind */
Expand All @@ -351,20 +357,18 @@ int main(int ac, char **av)
errno = 0;
check_cpubind(do_shm);
did_cpubind = 1;
numa_run_on_node_mask(&mask);
numa_run_on_node_mask(mask);
checkerror("sched_setaffinity");
break;
case 'C': /* --physcpubind */
{
int ncpus;
unsigned long *cpubuf;
numa_max_node();
struct bitmask *cpubuf;
dontshm("-C/--physcpubind");
cpubuf = cpumask(optarg, &ncpus);
errno = 0;
check_cpubind(do_shm);
did_cpubind = 1;
numa_sched_setaffinity(0, CPU_BYTES(ncpus), cpubuf);
numa_sched_setaffinity(0, cpubuf);
checkerror("sched_setaffinity");
free(cpubuf);
break;
Expand All @@ -376,9 +380,9 @@ int main(int ac, char **av)
errno = 0;
numa_set_bind_policy(1);
if (shmfd >= 0) {
numa_tonodemask_memory(shmptr, shmlen, &mask);
numa_tonodemask_memory(shmptr, shmlen, mask);
} else {
numa_set_membind(&mask);
numa_set_membind(mask);
}
numa_set_bind_policy(0);
checkerror("setting membind");
Expand All @@ -387,14 +391,15 @@ int main(int ac, char **av)
checknuma();
setpolicy(MPOL_PREFERRED);
mask = nodemask(optarg);
for (i=0; i<sizeof(mask); i++) {
if (nodemask_isset(&mask, i)) {
for (i=0; i<mask->size; i++) {
if (bitmask_isbitset(mask, i)) {
node = i;
nnodes++;
}
}
if (nnodes != 1)
usage();
bitmask_free(mask);
errno = 0;
numa_set_bind_policy(0);
if (shmfd >= 0)
Expand Down
Loading

0 comments on commit 2a3f98c

Please sign in to comment.