diff --git a/benchmarks/allocator/allocator_shared_object_file.c b/benchmarks/allocator/allocator_shared_object_file.c index 29504fb..3a5ca81 100644 --- a/benchmarks/allocator/allocator_shared_object_file.c +++ b/benchmarks/allocator/allocator_shared_object_file.c @@ -142,7 +142,7 @@ int MallocCounter; size_t sizeUsed; -static void INITAlloc(void) { +INITAlloc(void) { size_t sz; // Pre Allocate 600 MB @@ -181,7 +181,7 @@ static void INITAlloc(void) { } // Quick malloc implementation with mmap -void* malloc(size_t sz) +void* MALLOCCHERI(size_t sz) { sz = __builtin_align_up(sz, _Alignof(max_align_t)); @@ -201,17 +201,17 @@ void* malloc(size_t sz) } // Quick cheri free implementation -// void free(void *ptr) { +void FREECHERI(void *ptr) { -// // printf("free called \n"); + // printf("free called \n"); -// // get bounds from -// int len = cheri_getlen(ptr); + // get bounds from + int len = cheri_getlen(ptr); -// // printf("free len %d \n", len); + // printf("free len %d \n", len); -// munmap(ptr, len); -// } + munmap(ptr, len); +} static int pagesizes(size_t ps[MAXPAGESIZES]) @@ -230,8 +230,7 @@ pagesizes(size_t ps[MAXPAGESIZES]) return (pscnt); } -__attribute__((constuctor)) -static INITREGULARALLOC(void) { +INITREGULARALLOC(void) { size_t sz; // Hard-coded for 1GB huge page sz = 1073741824; diff --git a/benchmarks/benchmarks/kmeans/kmeans-pthread.c b/benchmarks/benchmarks/kmeans/kmeans-pthread.c index 7a5b4e7..6c030f9 100644 --- a/benchmarks/benchmarks/kmeans/kmeans-pthread.c +++ b/benchmarks/benchmarks/kmeans/kmeans-pthread.c @@ -39,8 +39,8 @@ #include "coz.h" -// #define malloc my_malloc -// #define free my_free +#define malloc MALLOCCHERI +#define free FREECHERI #define DEF_NUM_POINTS 150000 #define DEF_NUM_MEANS 100 @@ -270,10 +270,8 @@ int main(int argc, char **argv) // Extra code snippet added // printf("Initial alloc called\n"); //INITAlloc(); - // INITREGULARALLOC(); + INITREGULARALLOC(); // init_malloc(); - - printf("Starting here\n"); int num_procs, curr_point; int i; diff --git a/benchmarks/benchmarks/kmeans/stddefines.h b/benchmarks/benchmarks/kmeans/stddefines.h index daed8db..3a5ca81 100644 --- a/benchmarks/benchmarks/kmeans/stddefines.h +++ b/benchmarks/benchmarks/kmeans/stddefines.h @@ -30,8 +30,26 @@ #include #include #include +#include #include #include +#include + + +#include +#include +#include +#include +#include + +#include +#include + +#include +#include + +#define MAXPAGESIZES 2 + //#define TIMING @@ -111,4 +129,170 @@ static inline void get_time (struct timeval *t) #endif } +// Expirement work + +#define FILENAME "/dev/contigmem" + +static char *heap_start; +static char *heap; +static size_t HEAP_SIZE = 1024 * 1024 * 1024; + +void *ptr; +int MallocCounter; + +size_t sizeUsed; + +INITAlloc(void) { + + size_t sz; + // Pre Allocate 600 MB + sz = 100000000; + + int fd = open(FILENAME, O_RDWR, 0600); + + if (fd < 0) { + perror("open"); + exit(EXIT_FAILURE); + } + + off_t offset = 0; // offset to seek to. + + if (ftruncate(fd, sz) < 0) { + perror("ftruncate"); + close(fd); + exit(EXIT_FAILURE); + } + + // ptr = mmap(NULL, sz, + // PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANON,-1,0); + + ptr = mmap(NULL, sz, + PROT_READ|PROT_WRITE, MAP_SHARED,fd,0); + + // Added error handling + if(ptr == MAP_FAILED) + { + perror("mmap"); + exit(EXIT_FAILURE); + } + + MallocCounter = (int)sz; + +} + +// Quick malloc implementation with mmap +void* MALLOCCHERI(size_t sz) +{ + sz = __builtin_align_up(sz, _Alignof(max_align_t)); + + // printf("%d \n", sz); + // printf("%d Malloc counter\n", MallocCounter); + + MallocCounter -= sz; + void *ptrLink = &ptr[MallocCounter]; + ptrLink = cheri_setbounds(ptrLink, sz); + + return ptrLink; + +// if (heap + sz > heap_start + HEAP_SIZE) return NULL; +// heap += sz; +// return heap - sz; + +} + +// Quick cheri free implementation +void FREECHERI(void *ptr) { + + // printf("free called \n"); + + // get bounds from + int len = cheri_getlen(ptr); + + // printf("free len %d \n", len); + + munmap(ptr, len); +} + +static int +pagesizes(size_t ps[MAXPAGESIZES]) +{ + int pscnt; + + pscnt = getpagesizes(ps, MAXPAGESIZES); + // ATF_REQUIRE_MSG(pscnt != -1, "getpagesizes failed; errno=%d", errno); + // ATF_REQUIRE_MSG(ps[0] != 0, "psind 0 is %zu", ps[0]); + // ATF_REQUIRE_MSG(pscnt <= MAXPAGESIZES, "invalid pscnt %d", pscnt); + // if (pscnt == 1){ + // printf("pscnt is 1"); + // } + + // atf_tc_skip("no large page support"); + return (pscnt); +} + +INITREGULARALLOC(void) { + size_t sz; + // Hard-coded for 1GB huge page + sz = 1073741824; + + int error, fd, pscnt, pn; + + size_t ps[MAXPAGESIZES]; + + size_t size[3]; + + pn = getpagesizes(size, 3); + printf("page size is [%d]", size[2]); + + pscnt = pagesizes(ps); + + fd = shm_create_largepage(SHM_ANON, O_CREAT | O_RDWR, 1, SHM_LARGEPAGE_ALLOC_DEFAULT, 0); + + if (fd < 0 && errno == ENOTTY) { + perror("sh_create_largepages"); + close(fd); + exit(EXIT_FAILURE); + } + // if (fd < 0) + // perror("no large page supported"); + // exit(EXIT_FAILURE); + // if (fd < 0 && errno == ENOTTY) + // atf_tc_skip("no large page support"); + // ATF_REQUIRE_MSG(fd >= 0, "shm_create_largepage failed; errno=%d", errno); + + if (ftruncate(fd, sz) < 0) { + perror("ftruncate"); + close(fd); + exit(EXIT_FAILURE); + } + // if (error != 0 && errno == ENOMEM) + // /* + // * The test system might not have enough memory to accommodate + // * the request. + // */ + // atf_tc_skip("failed to allocate %zu-byte superpage", sz); + // ATF_REQUIRE_MSG(error == 0, "ftruncate failed; errno=%d", errno); + + ptr = mmap(NULL, sz, + PROT_READ|PROT_WRITE, MAP_SHARED,fd,0); + + // Added error handling + if(ptr == MAP_FAILED) + { + perror("mmap"); + exit(EXIT_FAILURE); + } + + MallocCounter = (int)sz; +} +// Standard Alloc +// void* MALLOCREGULAR(size_t sz) { + +// } + + +// void* CLEARALLOC(void) { +// / +// } + #endif // STDDEFINES_H_