feat: add log

This commit is contained in:
tqcq 2024-10-21 03:01:23 +00:00
parent 88c4f3238c
commit 96bd340675
2 changed files with 51 additions and 57 deletions

View File

@ -1,74 +1,68 @@
#include "allocator.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <assert.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <assert.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include "utils.h"
int alloc_buffer(MemoryBuffer * mem)
int
alloc_buffer(MemoryBuffer *mem)
{
if (mem->buffer != NULL) {
fprintf(stderr, "[ERROR] - Memory already allocated\n");
}
if (mem->buffer != NULL) { fprintf(stderr, "[ERROR] - Memory already allocated\n"); }
if (mem->align < _SC_PAGE_SIZE) {
mem->align = 0;
}
if (mem->align < _SC_PAGE_SIZE) { mem->align = 0; }
uint64_t alloc_size = mem->align ? mem->size + mem->align : mem->size;
uint64_t alloc_flags = MAP_PRIVATE | MAP_POPULATE;
uint64_t alloc_size = mem->align ? mem->size + mem->align : mem->size;
uint64_t alloc_flags = MAP_PRIVATE | MAP_POPULATE;
if (mem->flags & F_ALLOC_HUGE) {
if (mem->fd == 0) {
fprintf(stderr,
"[ERROR] - Missing file descriptor to allocate hugepage\n");
exit(1);
}
alloc_flags |=
(mem->flags & F_ALLOC_HUGE_1G) ? MAP_ANONYMOUS | MAP_HUGETLB
| (30 << MAP_HUGE_SHIFT)
: (mem->flags & F_ALLOC_HUGE_2M) ? MAP_ANONYMOUS |
MAP_HUGETLB | (21 << MAP_HUGE_SHIFT)
: MAP_ANONYMOUS;
} else {
mem->fd = -1;
alloc_flags |= MAP_ANONYMOUS;
}
mem->buffer = (char *)mmap(NULL, mem->size, PROT_READ | PROT_WRITE,
alloc_flags, mem->fd, 0);
if (mem->buffer == MAP_FAILED) {
perror("[ERROR] - mmap() failed");
exit(1);
}
if (mem->align) {
size_t error = (uint64_t) mem->buffer % mem->align;
size_t left = error ? mem->align - error : 0;
munmap(mem->buffer, left);
mem->buffer += left;
assert((uint64_t) mem->buffer % mem->align == 0);
}
if (mem->flags & F_VERBOSE) {
fprintf(stderr, "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
fprintf(stderr, "[ MEM ] - Buffer: %p\n", mem->buffer);
fprintf(stderr, "[ MEM ] - Size: %ld\n", alloc_size);
fprintf(stderr, "[ MEM ] - Alignment: %ld\n", mem->align);
fprintf(stderr, "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
}
return 0;
if (mem->flags & F_ALLOC_HUGE) {
if (mem->fd == 0) {
fprintf(stderr, "[ERROR] - Missing file descriptor to allocate hugepage\n");
exit(1);
}
alloc_flags |= (mem->flags & F_ALLOC_HUGE_1G) ? MAP_ANONYMOUS | MAP_HUGETLB | (30 << MAP_HUGE_SHIFT)
: (mem->flags & F_ALLOC_HUGE_2M)
? MAP_ANONYMOUS | MAP_HUGETLB | (21 << MAP_HUGE_SHIFT)
: MAP_ANONYMOUS;
} else {
mem->fd = -1;
alloc_flags |= MAP_ANONYMOUS;
}
mem->buffer = (char *) mmap(NULL, mem->size, PROT_READ | PROT_WRITE, alloc_flags, mem->fd, 0);
if (mem->buffer == MAP_FAILED) {
perror("[ERROR] - mmap() failed");
fprintf(stderr, "mem->size: %ld\n", mem->size);
exit(1);
}
if (mem->align) {
size_t error = (uint64_t) mem->buffer % mem->align;
size_t left = error ? mem->align - error : 0;
munmap(mem->buffer, left);
mem->buffer += left;
assert((uint64_t) mem->buffer % mem->align == 0);
}
if (mem->flags & F_VERBOSE) {
fprintf(stderr, "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
fprintf(stderr, "[ MEM ] - Buffer: %p\n", mem->buffer);
fprintf(stderr, "[ MEM ] - Size: %ld\n", alloc_size);
fprintf(stderr, "[ MEM ] - Alignment: %ld\n", mem->align);
fprintf(stderr, "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
}
return 0;
}
int free_buffer(MemoryBuffer * mem)
int
free_buffer(MemoryBuffer *mem)
{
free(mem->physmap);
return munmap(mem->buffer, mem->size);
free(mem->physmap);
return munmap(mem->buffer, mem->size);
}

View File

@ -14,7 +14,7 @@
#define HUGETLB_std "/mnt/huge/buff"
#define CONFIG_NAME_std "tmp/s_cfg.bin"
#define O_FILE_std "DIMM00"
#define ALLOC_SIZE 1 << 29
#define ALLOC_SIZE 1 << 30
#define ALIGN_std 2 << 20
#define PATT_LEN 1024
#define AGGR_std 2