client/src/unifycr.c
static void* unifycr_superblock_shmget(size_t size, key_t key)
{
    void *scr_shmblock = NULL;
    int scr_shmblock_shmid;

    debug("Key for superblock = %x\n", key);
    if (fs_type != UNIFYCR_LOG) {
		#ifdef ENABLE_NUMA_POLICY
			/* if user requested to use 1 shm/process along with NUMA optimizations */
			if ( key != IPC_PRIVATE ) {
				numa_exit_on_error = 1;
				/* check to see if NUMA control capability is available */
				if (numa_available() >= 0 ) {
					int max_numa_nodes = numa_max_node() + 1;
					debug("Max. number of NUMA nodes = %d\n",max_numa_nodes);
					int num_cores = sysconf(_SC_NPROCESSORS_CONF);
					int my_core, i, pref_numa_bank = -1;

					/* scan through the CPU set to see which core the current process is bound to */
					/* TODO: can alternatively read from the proc filesystem (/proc/self*) */
					cpu_set_t myset;
					CPU_ZERO(&myset);
					sched_getaffinity(0, sizeof(myset), &myset);
					for(  i = 0; i < num_cores; i++ ) {
						if ( CPU_ISSET(i, &myset) ) {
							my_core = i;
						}
					}
					if( my_core < 0 ) {
						debug("Not able to get current core affinity\n");
						return NULL;
					}
					debug("Process running on core %d\n",my_core);

					/* find out which NUMA bank this core belongs to */
					// numa_preferred doesn't work as needed, returns 0 always. placeholder only.
					pref_numa_bank = numa_preferred();
					debug("Preferred NUMA bank for core %d is %d\n", my_core, pref_numa_bank);

					/* create/attach to respective shmblock*/
					scr_shmblock_shmid = shmget( (key+pref_numa_bank), size, IPC_CREAT | IPC_EXCL | S_IRWXU);

				} else {
					debug("NUMA support unavailable!\n");
					return NULL;
				}
			} else {
				/* each process has its own block. use one of the other NUMA policies (unifycr_numa_policy) instead */
				scr_shmblock_shmid = shmget( key, size, IPC_CREAT | IPC_EXCL | S_IRWXU);
			}
		#else
			/* when NUMA optimizations are turned off, just let the kernel allocate pages as it desires */
			/* TODO: Add Huge-Pages support */
			scr_shmblock_shmid = shmget(key, size, IPC_CREAT | IPC_EXCL | S_IRWXU);
		#endif

			if (scr_shmblock_shmid < 0) {
				if (errno == EEXIST) {
					/* superblock already exists, attach to it */
					scr_shmblock_shmid = shmget(key, size, 0);
					scr_shmblock = shmat(scr_shmblock_shmid, NULL, 0);
					if(scr_shmblock < 0) {
						perror("shmat() failed");
						return NULL;
					}
					debug("Superblock exists at %p!\n",scr_shmblock);

					/* init our global variables to point to spots in superblock */
					unifycr_init_pointers(scr_shmblock);

				} else {
					perror("shmget() failed");
					return NULL;
				}
			} else {
				/* brand new superblock created, attach to it */
				scr_shmblock = shmat(scr_shmblock_shmid, NULL, 0);
				if(scr_shmblock == (void *) -1) {
					perror("shmat() failed");
				}
				debug("Superblock created at %p!\n",scr_shmblock);


		#ifdef ENABLE_NUMA_POLICY
				/* set NUMA policy for scr_shmblock */
				if ( unifycr_numa_bank >= 0 ) {
					/* specifically allocate pages from user-set bank */
					numa_tonode_memory(scr_shmblock, size, unifycr_numa_bank);
				} else if ( strcmp(unifycr_numa_policy,"interleaved") == 0) {
					/* interleave the shared-memory segment
					 * across all memory banks when all process share 1-superblock */
					debug("Interleaving superblock across all memory banks\n");
					numa_interleave_memory(scr_shmblock, size, numa_all_nodes_ptr);
				} else if( strcmp(unifycr_numa_policy,"local") == 0) {
					/* each process has its own superblock, let it be allocated from
					 * the closest memory bank */
					debug("Assigning memory from closest bank\n");
					numa_setlocal_memory(scr_shmblock, size);
				}
		#endif
				/* init our global variables to point to spots in superblock */
				unifycr_init_pointers(scr_shmblock);
				/* initialize data structures within block */
				unifycr_init_structures();

			}
    } else {
       	/* Use mmap to allocated share memory for UnifyCR*/
            int ret = -1;
            int fd = -1;
            char shm_name[GEN_STR_LEN] = {0};
            sprintf(shm_name, "%d-super-%d", app_id, key);
            superblock_fd = shm_open(shm_name, MMAP_OPEN_FLAG, MMAP_OPEN_MODE);
            if(-1 == (ret = superblock_fd))
            {
                return NULL;
            }

            ret = ftruncate(superblock_fd, size);
            if(-1 == ret)
            {
            	return NULL;
            }

            scr_shmblock = mmap(NULL, size, PROT_WRITE|PROT_READ, MAP_SHARED,\
            		superblock_fd, SEEK_SET);
            if(NULL == scr_shmblock)
            {
            	return NULL;
            }
            /* init our global variables to point to spots in superblock */
            if (scr_shmblock != NULL) {
    			unifycr_init_pointers(scr_shmblock);
    			unifycr_init_structures();
            }
    }
    return scr_shmblock;
}
/* create superblock of specified size and name, or attach to existing
 * block if available */
create superblock of specified size and name, or attach to existing
block if available
create superblock of specified size and name or attach to existing block if available
static void* unifycr_superblock_shmget(size_t size, key_t key)
{
    void *scr_shmblock = NULL;
    int scr_shmblock_shmid;
    debug("Key for superblock = %x\n", key);
    if (fs_type != UNIFYCR_LOG) {
		#ifdef ENABLE_NUMA_POLICY
			if ( key != IPC_PRIVATE ) {
				numa_exit_on_error = 1;
				if (numa_available() >= 0 ) {
					int max_numa_nodes = numa_max_node() + 1;
					debug("Max. number of NUMA nodes = %d\n",max_numa_nodes);
					int num_cores = sysconf(_SC_NPROCESSORS_CONF);
					int my_core, i, pref_numa_bank = -1;
					cpu_set_t myset;
					CPU_ZERO(&myset);
					sched_getaffinity(0, sizeof(myset), &myset);
					for(  i = 0; i < num_cores; i++ ) {
						if ( CPU_ISSET(i, &myset) ) {
							my_core = i;
						}
					}
					if( my_core < 0 ) {
						debug("Not able to get current core affinity\n");
						return NULL;
					}
					debug("Process running on core %d\n",my_core);
					pref_numa_bank = numa_preferred();
					debug("Preferred NUMA bank for core %d is %d\n", my_core, pref_numa_bank);
					scr_shmblock_shmid = shmget( (key+pref_numa_bank), size, IPC_CREAT | IPC_EXCL | S_IRWXU);
				} else {
					debug("NUMA support unavailable!\n");
					return NULL;
				}
			} else {
				scr_shmblock_shmid = shmget( key, size, IPC_CREAT | IPC_EXCL | S_IRWXU);
			}
		#else
			scr_shmblock_shmid = shmget(key, size, IPC_CREAT | IPC_EXCL | S_IRWXU);
		#endif
			if (scr_shmblock_shmid < 0) {
				if (errno == EEXIST) {
					scr_shmblock_shmid = shmget(key, size, 0);
					scr_shmblock = shmat(scr_shmblock_shmid, NULL, 0);
					if(scr_shmblock < 0) {
						perror("shmat() failed");
						return NULL;
					}
					debug("Superblock exists at %p!\n",scr_shmblock);
					unifycr_init_pointers(scr_shmblock);
				} else {
					perror("shmget() failed");
					return NULL;
				}
			} else {
				scr_shmblock = shmat(scr_shmblock_shmid, NULL, 0);
				if(scr_shmblock == (void *) -1) {
					perror("shmat() failed");
				}
				debug("Superblock created at %p!\n",scr_shmblock);
		#ifdef ENABLE_NUMA_POLICY
				if ( unifycr_numa_bank >= 0 ) {
					numa_tonode_memory(scr_shmblock, size, unifycr_numa_bank);
				} else if ( strcmp(unifycr_numa_policy,"interleaved") == 0) {
					debug("Interleaving superblock across all memory banks\n");
					numa_interleave_memory(scr_shmblock, size, numa_all_nodes_ptr);
				} else if( strcmp(unifycr_numa_policy,"local") == 0) {
					debug("Assigning memory from closest bank\n");
					numa_setlocal_memory(scr_shmblock, size);
				}
		#endif
				unifycr_init_pointers(scr_shmblock);
				unifycr_init_structures();
			}
    } else {
            int ret = -1;
            int fd = -1;
            char shm_name[GEN_STR_LEN] = {0};
            sprintf(shm_name, "%d-super-%d", app_id, key);
            superblock_fd = shm_open(shm_name, MMAP_OPEN_FLAG, MMAP_OPEN_MODE);
            if(-1 == (ret = superblock_fd))
            {
                return NULL;
            }
            ret = ftruncate(superblock_fd, size);
            if(-1 == ret)
            {
            	return NULL;
            }
            scr_shmblock = mmap(NULL, size, PROT_WRITE|PROT_READ, MAP_SHARED,\
            		superblock_fd, SEEK_SET);
            if(NULL == scr_shmblock)
            {
            	return NULL;
            }
            if (scr_shmblock != NULL) {
    			unifycr_init_pointers(scr_shmblock);
    			unifycr_init_structures();
            }
    }
    return scr_shmblock;
}
static void * unifycr_superblock_shmget ( size_t size , key_t key ) { void * scr_shmblock = NULL ; int scr_shmblock_shmid ; debug ( " \n " , key ) ; if ( fs_type != UNIFYCR_LOG ) { #ifdef ENABLE_NUMA_POLICY if ( key != IPC_PRIVATE ) { numa_exit_on_error = 1 ; if ( numa_available ( ) >= 0 ) { int max_numa_nodes = numa_max_node ( ) + 1 ; debug ( " \n " , max_numa_nodes ) ; int num_cores = sysconf ( _SC_NPROCESSORS_CONF ) ; int my_core , i , pref_numa_bank = -1 ; cpu_set_t myset ; CPU_ZERO ( & myset ) ; sched_getaffinity ( 0 , sizeof ( myset ) , & myset ) ; for ( i = 0 ; i < num_cores ; i ++ ) { if ( CPU_ISSET ( i , & myset ) ) { my_core = i ; } } if ( my_core < 0 ) { debug ( " \n " ) ; return NULL ; } debug ( " \n " , my_core ) ; pref_numa_bank = numa_preferred ( ) ; debug ( " \n " , my_core , pref_numa_bank ) ; scr_shmblock_shmid = shmget ( ( key + pref_numa_bank ) , size , IPC_CREAT | IPC_EXCL | S_IRWXU ) ; } else { debug ( " \n " ) ; return NULL ; } } else { scr_shmblock_shmid = shmget ( key , size , IPC_CREAT | IPC_EXCL | S_IRWXU ) ; } #else scr_shmblock_shmid = shmget ( key , size , IPC_CREAT | IPC_EXCL | S_IRWXU ) ; #endif if ( scr_shmblock_shmid < 0 ) { if ( errno == EEXIST ) { scr_shmblock_shmid = shmget ( key , size , 0 ) ; scr_shmblock = shmat ( scr_shmblock_shmid , NULL , 0 ) ; if ( scr_shmblock < 0 ) { perror ( " " ) ; return NULL ; } debug ( " \n " , scr_shmblock ) ; unifycr_init_pointers ( scr_shmblock ) ; } else { perror ( " " ) ; return NULL ; } } else { scr_shmblock = shmat ( scr_shmblock_shmid , NULL , 0 ) ; if ( scr_shmblock == ( void * ) -1 ) { perror ( " " ) ; } debug ( " \n " , scr_shmblock ) ; #ifdef ENABLE_NUMA_POLICY if ( unifycr_numa_bank >= 0 ) { numa_tonode_memory ( scr_shmblock , size , unifycr_numa_bank ) ; } else if ( strcmp ( unifycr_numa_policy , " " ) == 0 ) { debug ( " \n " ) ; numa_interleave_memory ( scr_shmblock , size , numa_all_nodes_ptr ) ; } else if ( strcmp ( unifycr_numa_policy , " " ) == 0 ) { debug ( " \n " ) ; numa_setlocal_memory ( scr_shmblock , size ) ; } #endif unifycr_init_pointers ( scr_shmblock ) ; unifycr_init_structures ( ) ; } } else { int ret = -1 ; int fd = -1 ; char shm_name [ GEN_STR_LEN ] = { 0 } ; sprintf ( shm_name , " " , app_id , key ) ; superblock_fd = shm_open ( shm_name , MMAP_OPEN_FLAG , MMAP_OPEN_MODE ) ; if ( -1 == ( ret = superblock_fd ) ) { return NULL ; } ret = ftruncate ( superblock_fd , size ) ; if ( -1 == ret ) { return NULL ; } scr_shmblock = mmap ( NULL , size , PROT_WRITE | PROT_READ , MAP_SHARED , superblock_fd , SEEK_SET ) ; if ( NULL == scr_shmblock ) { return NULL ; } if ( scr_shmblock != NULL ) { unifycr_init_pointers ( scr_shmblock ) ; unifycr_init_structures ( ) ; } } return scr_shmblock ; }
create superblock of specified size and name, or attach to existing
 block if available
create superblock of specified size and name or attach to existing block if available
/* if user requested to use 1 shm/process along with NUMA optimizations */ /* check to see if NUMA control capability is available */ /* scan through the CPU set to see which core the current process is bound to */ /* TODO: can alternatively read from the proc filesystem (/proc/self*) */ /* find out which NUMA bank this core belongs to */ // numa_preferred doesn't work as needed, returns 0 always. placeholder only. /* create/attach to respective shmblock*/ /* each process has its own block. use one of the other NUMA policies (unifycr_numa_policy) instead */ /* when NUMA optimizations are turned off, just let the kernel allocate pages as it desires */ /* TODO: Add Huge-Pages support */ /* superblock already exists, attach to it */ /* init our global variables to point to spots in superblock */ /* brand new superblock created, attach to it */ /* set NUMA policy for scr_shmblock */ /* specifically allocate pages from user-set bank */ /* interleave the shared-memory segment
					 * across all memory banks when all process share 1-superblock */ /* each process has its own superblock, let it be allocated from
					 * the closest memory bank */ /* init our global variables to point to spots in superblock */ /* initialize data structures within block */ /* Use mmap to allocated share memory for UnifyCR*/ /* init our global variables to point to spots in superblock */