Ejemplo n.º 1
0
static void *getAndAllocFromPool(int blockid, size_t size, CMEM_AllocParams *params)
{
    int poolid;

    poolid = CMEM_getPool2(blockid, size);

    if (poolid == -1) return NULL;

    return allocFromPool(blockid, poolid, params);
}
Ejemplo n.º 2
0
void testCache(size_t size, int block)
{
    unsigned int *ptr1_nocache = NULL;
    unsigned int *ptr1_cache = NULL;
    unsigned int *ptr1_dma = NULL;
    unsigned int *ptr2 = NULL;
    off_t physp;
    off_t physp_dma;
    off_t physp_nocache;
    off_t physp_cache;
    int poolid;
    unsigned int i, j;
    struct timeval start_tv, end_tv;
    unsigned long diff;
    int foo, bar;
    CMEM_AllocParams params;

    printf("Allocating first noncached buffer.\n");

    /* First allocate a buffer from the pool that best fits */
    ptr1_nocache = CMEM_alloc2(block, size, NULL);

    if (ptr1_nocache == NULL) {
        fprintf(stderr, "Failed to allocate buffer of size %d\n", size);
        goto cleanup;
    }

    printf("Allocated buffer of size %d at address %#x.\n", size,
           (unsigned int) ptr1_nocache);

    /* Find out and print the physical address of this buffer */
    physp_nocache = CMEM_getPhys(ptr1_nocache);

    if (physp_nocache == 0) {
        fprintf(stderr, "Failed to get physical address of buffer %#x\n",
                (unsigned int) ptr1_nocache);
        goto cleanup;
    }

    printf("Physical address of allocated buffer is %#llx.\n",
           (unsigned long long)physp_nocache);

    /* Write some data into this buffer */
    for (i=0; i < size / sizeof(size_t) ; i++) {
        ptr1_nocache[i] = 0xbeefbeef;
    }

    printf("Allocating first cached buffer.\n");

    /* First allocate a buffer from the pool that best fits */
    params = CMEM_DEFAULTPARAMS;
    params.flags = CMEM_CACHED;
    ptr1_cache = CMEM_alloc2(block, size, &params);

    if (ptr1_cache == NULL) {
        fprintf(stderr, "Failed to allocate buffer of size %d\n", size);
        goto cleanup;
    }

    printf("Allocated buffer of size %d at address %#x.\n", size,
           (unsigned int)ptr1_cache);

    /* Find out and print the physical address of this buffer */
    physp_cache = CMEM_getPhys(ptr1_cache);

    if (physp_cache == 0) {
        fprintf(stderr, "Failed to get physical address of buffer %#x\n",
                (unsigned int)ptr1_cache);
        goto cleanup;
    }

    printf("Physical address of allocated buffer is %#llx.\n",
           (unsigned long long)physp_cache);

    /* Write some data into this buffer */
    for (i = 0; i < size / sizeof(size_t); i++) {
        ptr1_cache[i] = 0x0dead1ce;
    }

    printf("Allocating noncached DMA source buffer.\n");

    /* Allocate a noncached buffer for the DMA source */
    ptr1_dma = CMEM_alloc2(block, size, NULL);

    if (ptr1_dma == NULL) {
        fprintf(stderr, "Failed to allocate buffer of size %d\n", size);
        goto cleanup;
    }

    printf("Allocated buffer of size %d at address %#x.\n", size,
           (unsigned int)ptr1_dma);

    /* Find out and print the physical address of this buffer */
    physp_dma = CMEM_getPhys(ptr1_dma);

    if (physp_dma == 0) {
        fprintf(stderr, "Failed to get physical address of buffer %#x\n",
                (unsigned int)ptr1_dma);
        goto cleanup;
    }

    printf("Physical address of allocated buffer is %#llx.\n",
           (unsigned long long)physp_dma);

    /* Initialize DMA source buffer */
    for (i = 0; i < size / sizeof(size_t); i++) {
        ptr1_cache[i] = 0x0dead1ce;
    }

    /*
     * Measure the write performance of each buffer to check that one
     * is cached and the other isn't.
     */
    printf("Measuring R-M-W performance (cached should be quicker).\n");
    for (j = 0; j < 3; j++) {
	printf("R-M-W noncached buffer %#llx\n",
	       (unsigned long long)physp_nocache);
	gettimeofday(&start_tv, NULL);
	for (i = 0; i < (size / sizeof(size_t)); i += 1) {
	    ptr1_nocache[i] += 1;
	}
	gettimeofday(&end_tv, NULL);
	diff = end_tv.tv_usec - start_tv.tv_usec;
	if (end_tv.tv_sec > start_tv.tv_sec) {
	    diff += (end_tv.tv_sec - start_tv.tv_sec) * 1000000;
	}
	printf("  diff=%ld\n", diff);

	printf("R-M-W cached buffer %#llx\n", (unsigned long long)physp_cache);
	gettimeofday(&start_tv, NULL);
	for (i = 0; i < (size / sizeof(size_t)); i += 1) {
	    ptr1_cache[i] += 1;
	}
	gettimeofday(&end_tv, NULL);
	diff = end_tv.tv_usec - start_tv.tv_usec;
	if (end_tv.tv_sec > start_tv.tv_sec) {
	    diff += (end_tv.tv_sec - start_tv.tv_sec) * 1000000;
	}
	printf("  diff=%ld\n", diff);
    }

    printf("Invalidate cached buffer %p\n", ptr1_cache);

    foo = *ptr1_cache;
    bar = foo;
    bar++;
    *ptr1_cache = bar;
    CMEM_cacheInv(ptr1_cache, size);
    printf("post-flush *ptr1_cache=0x%x\n", foo);
    printf("wrote 0x%x to *ptr1_cache\n", bar);
    printf("post-inv *ptr1_cache=0x%x\n", *ptr1_cache);

    printf("R-M-W cached buffer %#llx\n", (unsigned long long)physp_cache);
    gettimeofday(&start_tv, NULL);
    for (i = 0; i < (size / sizeof(size_t)); i += 1) {
	ptr1_cache[i] += 1;
    }
    gettimeofday(&end_tv, NULL);
    diff = end_tv.tv_usec - start_tv.tv_usec;
    if (end_tv.tv_sec > start_tv.tv_sec) {
	diff += (end_tv.tv_sec - start_tv.tv_sec) * 1000000;
    }
    printf("  diff=%ld\n", diff);

    /*
     * Now allocate another buffer by first finding out which pool that fits
     * best, and then explicitly allocating from that pool. This gives more
     * control at the cost of an extra function call, but essentially does
     * the same thing as the above CMEM_alloc() call.
     */
    printf("Allocating second buffer.\n");

    poolid = CMEM_getPool2(block, size);

    if (poolid == -1) {
        fprintf(stderr, "Failed to get a pool which fits size %d\n", size);
        goto cleanup;
    }

    printf("Got a pool (%d) that fits the size %d\n", poolid, size);

    ptr2 = CMEM_allocPool2(block, poolid, NULL);

    if (ptr2 == NULL) {
        fprintf(stderr, "Failed to allocate buffer of size %d\n", size);
        goto cleanup;
    }

    printf("Allocated buffer of size %d at address %#x.\n", size,
           (unsigned int)ptr2);

    /* Find out and print the physical address of this buffer */
    physp = CMEM_getPhys(ptr2);

    if (physp == 0) {
        fprintf(stderr, "Failed to get physical address of buffer %#x\n",
                (unsigned int)ptr2);
        goto cleanup;
    }

    printf("Physical address of allocated buffer is %#llx.\n",
           (unsigned long long)physp);

    /* Write some data into this buffer */
    for (i=0; i < size / sizeof(size_t); i++) {
        ptr2[i] = 0xfeebfeeb;
    }

    if (non_interactive_flag == FALSE) {
	printf("Inspect your memory map in /proc/%d/maps.\n", getpid());
	printf("Also look at your pool info under /proc/cmem\n");

	printf("Press ENTER to exit (after 'cat /proc/cmem' if desired).\n");
	getchar();
    }

cleanup:
    if (ptr1_nocache != NULL) {
	if (CMEM_free(ptr1_nocache, NULL) < 0) {
	    fprintf(stderr, "Failed to free buffer at %#x\n",
		    (unsigned int)ptr1_nocache);
	}
	printf("Successfully freed buffer at %#x.\n",
	       (unsigned int)ptr1_nocache);
    }

    if (ptr1_cache != NULL) {
	if (CMEM_free(ptr1_cache, &params) < 0) {
	    fprintf(stderr, "Failed to free buffer at %#x\n",
		    (unsigned int)ptr1_cache);
	}
	printf("Successfully freed buffer at %#x.\n",
	       (unsigned int)ptr1_cache);
    }

    if (ptr1_dma != NULL) {
	if (CMEM_free(ptr1_dma, NULL) < 0) {
	    fprintf(stderr, "Failed to free buffer at %#x\n",
		    (unsigned int)ptr1_dma);
	}
	printf("Successfully freed buffer at %#x.\n",
	       (unsigned int)ptr1_dma);
    }

    if (ptr2 != NULL) {
	if (CMEM_free(ptr2, NULL) < 0) {
	    fprintf(stderr, "Failed to free buffer at %#x\n",
		    (unsigned int)ptr2);
	}
	printf("Successfully freed buffer at %#x.\n",
	       (unsigned int)ptr2);
    }
}