BLOCKDEVICEP blockdevice_create(char *name, int blocksize, int numblocks) { BLOCKDEVICEP devp ; int rc ; if (blocksize != REAL_BLOCK_LENGTH) { /* ** For now we simplify things and just force ** all blockdevices to have 1k block sizes. ** We can fix this assumption up later. */ return NULL ; } if (strlen(name) >= BLOCK_DEV_MAX_NAME_LEN) { return NULL ; } devp = malloc(sizeof(BLOCKDEVICE)) ; if (NULL == devp) { return NULL ; } devp->name = malloc(strlen(name)+1) ; if (NULL == devp->name) { free(devp) ; return NULL ; } devp->sem = semaphore_create(1234, 0) ; devp->dirtysem = semaphore_create(1235, 0) ; /*if (! devp->sem) { free(devp) ; return NULL ; }*/ strcpy(devp->name, name) ; devp->blocksize = blocksize ; devp->numblocks = numblocks ; devp->lastblockchecked = 0 ; rc = shmem_open() ; if (0 == rc) { semaphore_destroy(devp->sem, 1) ; free(devp->name) ; free(devp) ; return NULL ; } devp->bd = (struct SHMEM_BLOCK_OVERLAY *) shmem_get_memptr() ; initialize_block_dev_shmem(devp->bd, blocksize, numblocks, name) ; return devp ; }
int main(int argc, char *argv[]) { g_shm = (sharedif_t*)shmem_open(SHMID_MINFLOWCAP, sizeof(sharedif_t)); if (g_shm == NULL) { printf("ERROR: unable to get pointer to shared memory segment\n"); return 0; } tval_t ts={0,0}; for (int i=0;i<60*60;i++) // stop after hour { if (ts.tv_sec == 0) { ts.tv_sec = time(NULL); continue;} if ((time(NULL) - ts.tv_sec) >= 1) { ts.tv_sec = time(NULL); every_second(ts, g_shm); } usleep(250000); } }
int main(void) { void* ptr; struct bitmask *nmask; int err; nmask = numa_allocate_nodemask(); numa_bitmask_setbit(nmask, 0); ptr = shmem_open(); err = mbind(ptr, 4096 * 3, MPOL_INTERLEAVE, nmask->maskp, nmask->size, 0); if (err < 0) perror("mbind1"), exit(1); err = mbind(ptr + 4096, 4096, MPOL_BIND, nmask->maskp, nmask->size, 0); if (err < 0) perror("mbind1"), exit(1); return 0; }
BLOCKDEVICEP blockdevice_open(char *name) { BLOCKDEVICEP devp ; int rc ; /* ** Shared memory has only been setup for 1 block device (currently). ** This makes open really easy. We just attach to the shared memory ** segment, suck the header information out, fill up our structure, ** and away we go. Of course, there is absolutely no error checking ** here (yet), we could run into the case where the shared memory ** has not been created yet. We'll worry about this later. Iterate, ** iterate, iterate. */ devp = malloc(sizeof(BLOCKDEVICE)) ; if (NULL == devp) { return NULL ; } devp->name = malloc(strlen(name)+1) ; if (NULL == devp->name) { free(devp) ; return NULL ; } devp->sem = semaphore_open(1234) ; if (! devp->sem) { free(devp) ; return NULL ; } devp->dirtysem = semaphore_open(1235) ; if (! devp->dirtysem) { free(devp) ; return NULL ; } rc = shmem_open() ; if (0 == rc) { semaphore_destroy(devp->sem, 0) ; semaphore_destroy(devp->dirtysem, 0) ; free(devp->name) ; free(devp) ; return NULL ; } devp->bd = (struct SHMEM_BLOCK_OVERLAY *) shmem_get_memptr() ; strcpy(devp->name, name) ; devp->blocksize = devp->bd->header.blocksize ; devp->numblocks = devp->bd->header.numblocks ; return devp ; }