Back to index

numactl  2.0.8~rc4
tshm.c
Go to the documentation of this file.
00001 #include <sys/shm.h>
00002 #include <sys/ipc.h>
00003 #include <sys/fcntl.h>
00004 #include <stdio.h>
00005 #include <numaif.h>
00006 
00007 #define err(x) perror(x),exit(1)
00008 
00009 enum { 
00010        MEMSZ = 10*1024*1024,
00011 }; 
00012 
00013 struct req {
00014        enum cmd { 
00015               SET = 1,
00016               CHECK, 
00017               REPLY,
00018               EXIT,
00019        } cmd;
00020        long offset;
00021        long len;
00022        int policy;
00023        nodemask_t nodes;
00024 }; 
00025 
00026 void worker(void) 
00027 { 
00028        struct req req;
00029        while (read(0, &req, sizeof(struct req) > 0)) { 
00030               switch (req.cmd) { 
00031               case SET:
00032                      if (mbind(map + req.offset, req.len, req.policy, &req.nodes,
00033                               NUMA_MAX_NODES+1, 0) < 0) 
00034                             err("mbind");
00035                      break;
00036               case TEST:
00037                      req.cmd = REPLY;
00038                      if (get_mempolicy(&req.policy, &req.nodes, NUMA_MAX_NODES+1,
00039                                      map + req.offset, MPOL_F_ADDR) < 0)
00040                             err("get_mempolicy");
00041                      write(1, &req, sizeof(struct req)); 
00042                      break;
00043               case EXIT:
00044                      return;
00045               default:
00046                      abort();
00047               }
00048        }
00049 } 
00050 
00051 void sendreq(int fd, enum cmd cmd, int policy, long offset, long len, nodemask_t nodes) 
00052 { 
00053        struct req req = { 
00054               .cmd = cmd, 
00055               .offset = offset, 
00056               .len = len, 
00057               .policy = policy,
00058               .nodes = nodes
00059        }; 
00060        if (write(fd, &req, sizeof(struct req)) != sizeof(struct req))
00061               panic("bad req write");
00062 } 
00063 
00064 void readreq(int fd, int *policy, nodemask_t *nodes, long offset, long len)
00065 {
00066        struct req req;
00067        if (read(fd, &req, sizeof(struct req)) != sizeof(struct req))
00068               panic("bad req read");
00069        if (req.cmd != REPLY)
00070               abort();
00071        *policy = req.policy;
00072        *nodes = req.nodes;
00073 }
00074 
00075 int main(void)
00076 {
00077        int fd = open("tshm", O_CREAT, 0600);   
00078        close(fd);
00079        key_t key = ftok("tshm", 1); 
00080        int shm = shmget(key, MEMSZ,  IPC_CREAT|0600);
00081        if (shm < 0) err("shmget");
00082        char *map = shmat(shm, NULL, 0); 
00083        printf("map = %p\n", map); 
00084 
00085        unsigned long nmask = 0x3;
00086        if (mbind(map, MEMSZ, MPOL_INTERLEAVE, &nmask, 4, 0) < 0) err("mbind1"); 
00087 
00088        int fd[2];
00089        if (pipe(fd) < 0) err("pipe"); 
00090        if (fork() == 0) {
00091               close(0); 
00092               close(1); 
00093               dup2(fd[0], 0);
00094               dup2(fd[1], 1);
00095               worker();
00096               _exit(0);
00097        } 
00098               
00099        int pagesz = getpagesize();
00100        int i;
00101 
00102        srand(1); 
00103        for (;;) { 
00104 
00105               /* chose random offset */
00106 
00107               /* either in child or here */
00108 
00109               /* change policy */
00110 
00111               /* ask other guy to check */ 
00112 
00113        } 
00114        
00115   
00116        shmdt(map);
00117        shmctl(shm, IPC_RMID, 0);
00118 } 
00119 
00120