Ejemplo n.º 1
0
/*
 * map_file_segment() -- map a [range of a] registered file segment.
 */
static int
map_file_segment(segment_t *segp)
{
    glctx_t *gcp = &glctx;

    char *memp;
    size_t size;
    int fd;
    int   flags = segp->seg_flags;

    if (!flags)
        flags = MAP_PRIVATE;	/* default */

    if ((fd = segp->seg_fd) == SEG_FD_NONE) {
        fprintf(stderr, "%s:  file %s not open\n",
                gcp->program_name, segp->seg_path);
        return SEG_ERR;
    }

    size = file_size(fd);

    /*
     * page align offset/length;  verify fit in file
     */
    segp->seg_offset = round_down_to_pagesize(segp->seg_offset);
    if (segp->seg_offset > size) {
        fprintf(stderr, "%s: offset 0x%lx beyond end of file %s\n",
                gcp->program_name, segp->seg_offset,
                segp->seg_path);
        return SEG_ERR;
    }

    if (segp->seg_length == 0)
        segp->seg_length = round_up_to_pagesize(size) -
                           segp->seg_offset;
    else
        segp->seg_length = round_up_to_pagesize(segp->seg_length);

    memp = (char *)mmap(0, segp->seg_length,
                        segp->seg_prot,
                        flags,
                        fd,
                        segp->seg_offset);

    if (memp == MAP_FAILED) {
        int err = errno;
        fprintf(stderr, "%s:  mmap of %s failed - %s\n",
                __FUNCTION__, segp->seg_path, strerror(err));
        return SEG_ERR;
    }

    vprint("%s:  mmap()ed file seg %s at 0x%lx-0x%lx\n",
           gcp->program_name, segp->seg_name,
           memp, memp+segp->seg_length-1);

    segp->seg_start = memp;

    return SEG_OK;
}
Ejemplo n.º 2
0
/*
 * segment_mbind() - set memory policy for a range of specified segment
 *
 * NOTE:  offset is relative to start of mapping, not start of file
 */
int
segment_mbind(char *name, range_t * range, int policy,
	      nodemask_t * nodemask, int flags)
{
	glctx_t *gcp = &glctx;
	segment_t *segp;
	char *start;
	off_t offset;
	size_t length, maxlength;
	int ret;

	segp = segment_get(name);
	if (segp == NULL) {
		fprintf(stderr, "%s:  no such segment:  %s\n",
			gcp->program_name, name);
		return SEG_ERR;
	}

	if (segp->seg_start == MAP_FAILED) {
		fprintf(stderr, "%s:  segment %s not mapped\n",
			gcp->program_name, name);
		return SEG_ERR;
	}

	offset = round_down_to_pagesize(range->offset);
	if (offset >= segp->seg_length) {
		fprintf(stderr, "%s:  offset %ld is past end of segment %s\n",
			gcp->program_name, offset, name);
		return SEG_ERR;
	}

	start = segp->seg_start + offset;
	maxlength = segp->seg_length - offset;

	length = range->length;
	if (length)
		length = round_up_to_pagesize(length);

	/*
	 * note:  we silently truncate to max length [end of segment]
	 */
	if (length == 0 || length > maxlength)
		length = maxlength;

	ret = mbind(segp->seg_start + offset, length, policy, nodemask->n,
		    NUMA_NUM_NODES, flags);

	if (ret == -1) {
		int err = errno;
		fprintf(stderr, "%s:  mbind() of segment %s failed - %s\n",
			gcp->program_name, name, strerror(err));
		return SEG_ERR;
	}

	return SEG_OK;
}
Ejemplo n.º 3
0
/*
 * segment_register:  register an anon, file or shm segment based on args.
 *	for anon and shm, 'name' = segment name.
 *	for file, 'name' = path name; segment name = basename(path)
 *
 * returns: !0 on success; 0 on failure
 */
int
segment_register(seg_type_t type, char *name, range_t *range, int flags)
{
    glctx_t   *gcp = &glctx;
    segment_t *segp;
    char      *path;

    segp = segment_get(basename(name));	/* ensure unique name */
    if (segp != NULL) {
        fprintf(stderr, "%s:  segment %s already exists\n",
                gcp->program_name, segp->seg_name);
        return SEG_ERR;
    }

    segp = get_seg_slot();
    if (segp == NULL)
        return SEG_ERR;

    path = strdup(name);	/* save a copy */
    segp->seg_name   = strdup(basename(name));
    segp->seg_start  = MAP_FAILED;
    segp->seg_length = round_up_to_pagesize(range->length);
    segp->seg_offset = round_down_to_pagesize(range->offset);
    segp->seg_type   = type;
    segp->seg_flags  = flags;			/* possibly 0 */
    segp->seg_prot   = PROT_READ|PROT_WRITE;	/* default */
    segp->seg_fd     = SEG_FD_NONE;
    segp->seg_shmid  = SHM_ID_NONE;

    switch (type) {
    case SEGT_ANON:
        free(path);
        break;

    case SEGT_FILE:
        segp->seg_path = path;
        return open_file(segp);
        break;

    case SEGT_SHM:
        free(path);
        return get_shm_segment(segp);
        break;

    default:
        free(path);
    }
    return SEG_OK;
}
Ejemplo n.º 4
0
/*
 * segment_touch() - "touch" [read or write] each page of specified range
 *                   -- from offset to offset+length -- to fault in or to
 *                   test protection.
 * NOTE:  offset is relative to start of mapping, not start of file!
 */
int
segment_touch(char *name, range_t *range, int rw)
{
    glctx_t       *gcp = &glctx;
    segment_t     *segp;
    off_t          offset;
    size_t         length, maxlength;
    unsigned long *memp;
    struct timeval t_start, t_end;

    segp = segment_get(name);
    if (segp == NULL) {
        fprintf(stderr, "%s:  no such segment:  %s\n",
                gcp->program_name, name);
        return SEG_ERR;
    }

    offset    = round_down_to_pagesize(range->offset);
    if (offset >= segp->seg_length) {
        fprintf(stderr, "%s:  offset %ld is past end of segment %s\n",
                gcp->program_name, offset, name);
        return SEG_ERR;
    }

    memp      = (unsigned long*)(segp->seg_start + offset);
    maxlength = segp->seg_length - offset;

    length = range->length;
    if (length)
        length = round_up_to_pagesize(length);

    /*
     * note:  we silently truncate to max length [end of segment]
     */
    if (length == 0 || length > maxlength)
        length = maxlength;

    gettimeofday(&t_start, NULL);
    touch_memory(rw, memp, length);
    gettimeofday(&t_end, NULL);
    printf("%s:  touched %d pages in %6.3f secs\n",
           gcp->program_name, length/gcp->pagesize,
           (float)(tv_diff_usec(&t_start, &t_end))/1000000.0);

    return SEG_OK;
}
Ejemplo n.º 5
0
int segment_location(char *name, range_t * range)
{
	glctx_t *gcp = &glctx;
	segment_t *segp;
	char *apage, *end;
	off_t offset;
	size_t length, maxlength;
	int pgid, i;
	bool need_nl;

	segp = segment_get(name);
	if (segp == NULL) {
		fprintf(stderr, "%s:  no such segment:  %s\n",
			gcp->program_name, name);
		return SEG_ERR;
	}

	if (segp->seg_start == MAP_FAILED) {
		fprintf(stderr, "%s:  segment %s not mapped\n",
			gcp->program_name, name);
		return SEG_ERR;
	}

	offset = round_down_to_pagesize(range->offset);
	if (offset >= segp->seg_length) {
		fprintf(stderr, "%s:  offset %ld is past end of segment %s\n",
			gcp->program_name, offset, name);
		return SEG_ERR;
	}

	apage = segp->seg_start + offset;
	maxlength = segp->seg_length - offset;

	length = range->length;
	if (length)
		length = round_up_to_pagesize(length);

	/*
	 * note:  we silently truncate to max length [end of segment]
	 */
	if (length == 0 || length > maxlength)
		length = maxlength;

	end = apage + length;
	pgid = offset / gcp->pagesize;

	show_one_segment(segp, false);	/* show mapping, no header */

	printf("page offset   ");
	for (i = 0; i < PG_PER_LINE; ++i)
		printf(" +%02d", i);
	printf("\n");
	if (pgid & PPL_MASK) {
		/*
		 * start partial line
		 */
		int pgid2 = pgid & ~PPL_MASK;
		printf("%12x: ", pgid2);
		while (pgid2 < pgid) {
			printf("    ");
			++pgid2;
		}
		need_nl = true;
	} else
		need_nl = false;

	for (; apage < end; apage += gcp->pagesize, ++pgid) {
		int node;

		node = get_node(apage);
		if (node < 0) {
			fprintf(stderr, "\n%s:  "
				"failed to get node for segment %s, offset 0x%x\n",
				gcp->program_name, name, SEG_OFFSET(segp,
								    apage));
			return SEG_ERR;
		}

		if ((pgid & PPL_MASK) == 0) {
			if (need_nl)
				printf("\n");
			printf("%12x: ", pgid);	/* start a new line */
			need_nl = true;
		}
		printf(" %3d", node);

		if (signalled(gcp)) {
			reset_signal();
			break;
		}
	}
	printf("\n");

	return SEG_OK;
}