Untitled

mail@pastecode.io avatar
unknown
c_cpp
2 years ago
1.6 kB
3
Indexable
Never
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <time.h>
#include <unistd.h>
#include <string.h>

#define PHYS_ADDR 0x840000000

static void *page_addr;

#define TOTAL_SIZE (1ULL * 1024L * 1024L * 1024L) // 1 GB

static void mem_init()
{
	uint64_t pagesize, addr, len;
	int fd;

	fd = open("/dev/mem", O_RDWR);
	if (fd == -1) {
		perror("Failed to open /dev/mem");
		exit(1);
	}

	pagesize = getpagesize();
    printf("pagesize: %lu\n", pagesize);
	addr = PHYS_ADDR & (~(pagesize - 1));
    printf("addr: 0x%lx\n", addr);
	len = (PHYS_ADDR & (pagesize - 1)) + TOTAL_SIZE;
    printf("len: %lu\n", len);
	page_addr = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, addr);
	if (page_addr == MAP_FAILED) {
		perror("Failed to mmap plain device path");
		exit(1);
	}

	close(fd);
}

int main() {
	char *mem;
    struct timespec begin, end;
    double elapsed_time;

	mem_init();

    mem = malloc(1ULL * 1024 * 1024 * 1024);

    clock_gettime(CLOCK_MONOTONIC, &begin);
    memset(mem, 0x00, 1ULL * 1024 * 1024 * 1024);
    clock_gettime(CLOCK_MONOTONIC, &end);
    elapsed_time = (double)(end.tv_sec - begin.tv_sec) + ((double)(end.tv_nsec - begin.tv_nsec))/1000000000;
    fprintf(stderr, "Elapsed time: %lf\n", elapsed_time);

    clock_gettime(CLOCK_MONOTONIC, &begin);
    memset(page_addr, 0x00, 1ULL * 1024 * 1024 * 1024);
    clock_gettime(CLOCK_MONOTONIC, &end);
    elapsed_time = (double)(end.tv_sec - begin.tv_sec) + ((double)(end.tv_nsec - begin.tv_nsec))/1000000000;
    fprintf(stderr, "Elapsed time: %lf\n", elapsed_time);

	return 0;
}