From 5e4ae7bdf8b79e9604d069a2d0ccea267d7e7729 Mon Sep 17 00:00:00 2001 From: Akilan Date: Mon, 30 Dec 2024 12:54:21 +0000 Subject: [PATCH] test whole program --- .vscode/settings.json | 10 +- benchmarks/benchmarks/XSbench/GridInit.c | 6 +- benchmarks/benchmarks/XSbench/Main.c | 4 +- benchmarks/benchmarks/XSbench/Materials.c | 6 +- benchmarks/benchmarks/XSbench/Simulations.c | 6 +- benchmarks/benchmarks/XSbench/XSutils.c | 6 +- benchmarks/benchmarks/XSbench/io.c | 11 +- benchmarks/benchmarks/XSbench/malloc.h | 327 +++++--------------- benchmarks/benchmarks/barnes/CMakeLists.txt | 116 +++++++ benchmarks/benchmarks/barnes/code.c | 9 + benchmarks/benchmarks/barnes/code.h | 2 + benchmarks/benchmarks/barnes/getparam.c | 2 +- benchmarks/benchmarks/barnes/malloc.h | 297 ++++++++++++++++++ benchmarks/benchmarks/barnes/run.sh | 1 + benchmarks/benchmarks/cfrac/cfrac.c | 9 + benchmarks/benchmarks/cfrac/malloc.h | 316 +++++++++++++++++++ benchmarks/benchmarks/cfrac/pcfrac.c | 4 + benchmarks/benchmarks/cfrac/pdefs.h | 3 + benchmarks/benchmarks/cfrac/pops.c | 25 +- benchmarks/benchmarks/espresso/main.c | 13 +- benchmarks/benchmarks/espresso/malloc.h | 306 ++++++++++++++++++ benchmarks/benchmarks/espresso/utility.h | 4 + localpush.sh | 2 +- 23 files changed, 1194 insertions(+), 291 deletions(-) create mode 100644 benchmarks/benchmarks/barnes/CMakeLists.txt create mode 100644 benchmarks/benchmarks/barnes/malloc.h create mode 100644 benchmarks/benchmarks/barnes/run.sh create mode 100644 benchmarks/benchmarks/cfrac/malloc.h create mode 100644 benchmarks/benchmarks/espresso/malloc.h diff --git a/.vscode/settings.json b/.vscode/settings.json index 03e8e7f..e47cdc8 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -46,7 +46,15 @@ "stdint.h": "c", "bitmap_alloc.h": "c", "buddy.h": "c", - "string.h": "c" + "string.h": "c", + "forward_list": "cpp", + "memory": "cpp", + "__tuple": "cpp", + "type_traits": "cpp", + "limits": "cpp", + "ratio": "cpp", + "tuple": "cpp", + "variant": "cpp" }, "C_Cpp.errorSquiggles": "disabled" } \ No newline at end of file diff --git a/benchmarks/benchmarks/XSbench/GridInit.c b/benchmarks/benchmarks/XSbench/GridInit.c index f953f76..3401561 100644 --- a/benchmarks/benchmarks/XSbench/GridInit.c +++ b/benchmarks/benchmarks/XSbench/GridInit.c @@ -1,10 +1,10 @@ #include "XSbench_header.h" -#include "malloc.h" +// #include "malloc.h" -#define malloc my_malloc -#define free my_free +#define malloc MALLOCCHERI +#define free FREECHERI SimulationData grid_init_do_not_profile( Inputs in, int mype ) { diff --git a/benchmarks/benchmarks/XSbench/Main.c b/benchmarks/benchmarks/XSbench/Main.c index bdc9a44..6ece305 100644 --- a/benchmarks/benchmarks/XSbench/Main.c +++ b/benchmarks/benchmarks/XSbench/Main.c @@ -7,8 +7,8 @@ #include "malloc.h" -#define malloc my_malloc -#define free my_free +#define malloc MALLOCCHERI +#define free FREECHERI int main( int argc, char* argv[] ) { diff --git a/benchmarks/benchmarks/XSbench/Materials.c b/benchmarks/benchmarks/XSbench/Materials.c index 056d98b..e0ffcfe 100644 --- a/benchmarks/benchmarks/XSbench/Materials.c +++ b/benchmarks/benchmarks/XSbench/Materials.c @@ -5,10 +5,10 @@ #include "XSbench_header.h" -#include "malloc.h" +// #include "malloc.h" -#define malloc my_malloc -#define free my_free +#define malloc MALLOCCHERI +#define free FREECHERI // num_nucs represents the number of nuclides that each material contains int * load_num_nucs(long n_isotopes) diff --git a/benchmarks/benchmarks/XSbench/Simulations.c b/benchmarks/benchmarks/XSbench/Simulations.c index 856e8a5..5cca693 100644 --- a/benchmarks/benchmarks/XSbench/Simulations.c +++ b/benchmarks/benchmarks/XSbench/Simulations.c @@ -1,10 +1,10 @@ #include "XSbench_header.h" -#include "malloc.h" +// #include "malloc.h" -#define malloc my_malloc -#define free my_free +#define malloc MALLOCCHERI +#define free FREECHERI //////////////////////////////////////////////////////////////////////////////////// // BASELINE FUNCTIONS diff --git a/benchmarks/benchmarks/XSbench/XSutils.c b/benchmarks/benchmarks/XSbench/XSutils.c index d2cd879..3c9beb8 100644 --- a/benchmarks/benchmarks/XSbench/XSutils.c +++ b/benchmarks/benchmarks/XSbench/XSutils.c @@ -1,10 +1,10 @@ #include "XSbench_header.h" -#include "malloc.h" +// #include "malloc.h" -#define malloc my_malloc -#define free my_free +#define malloc MALLOCCHERI +#define free FREECHERI int double_compare(const void * a, const void * b) { diff --git a/benchmarks/benchmarks/XSbench/io.c b/benchmarks/benchmarks/XSbench/io.c index e1b2eaf..671b0f5 100644 --- a/benchmarks/benchmarks/XSbench/io.c +++ b/benchmarks/benchmarks/XSbench/io.c @@ -1,10 +1,10 @@ #include "XSbench_header.h" -#include "malloc.h" +// #include "malloc.h" -#define malloc my_malloc -#define free my_free +#define malloc MALLOCCHERI +#define free FREECHERI #ifdef MPI #include @@ -267,9 +267,14 @@ Inputs read_CLI( int argc, char * argv[] ) // defaults to baseline kernel input.kernel_id = 0; + + printf(" %d ", 6 * sizeof(char)); + + INITREGULARALLOC(); // defaults to H-M Large benchmark input.HM = (char *) malloc( 6 * sizeof(char) ); + printf(" Malloc finished \n"); input.HM[0] = 'l' ; input.HM[1] = 'a' ; input.HM[2] = 'r' ; diff --git a/benchmarks/benchmarks/XSbench/malloc.h b/benchmarks/benchmarks/XSbench/malloc.h index d569413..5e81797 100644 --- a/benchmarks/benchmarks/XSbench/malloc.h +++ b/benchmarks/benchmarks/XSbench/malloc.h @@ -48,37 +48,9 @@ #include #include -// #include "bitmap.h" -// #include "simple.h" - #define MAXPAGESIZES 2 -// init_malloc(void) { -// // Init malloc implementation -// INITREGULARALLOC(); - -// // init_alloc(100,100000); -// // brm_init(); -// //alloc_init(104857632); -// } - -// void* MALLOC(size_t sz) { -// // malloc implementation -// return MALLOCCHERI(sz); -// // return malloc_buddy(sz); -// // return alloc(sz); -// // return (void *)alloc_chunk(); -// } - -// void FREE(void *ptr) { -// // free implementation -// FREECHERI(ptr); -// // free_chunk(ptr); -// //free_mem(ptr); -// } - - //#define TIMING /* Debug printf */ @@ -92,12 +64,12 @@ assert ((a) == 0); \ } -// static inline void *MALLOC(size_t size) -// { -// void * temp = malloc(size); -// assert(temp); -// return temp; -// } +static inline void *MALLOC(size_t size) +{ + void * temp = malloc(size); + assert(temp); + return temp; +} static inline void *CALLOC(size_t num, size_t size) { @@ -120,35 +92,35 @@ static inline char *GETENV(char *envstr) else return env; } -// #define GET_TIME(start, end, duration) \ -// duration.tv_sec = (end.tv_sec - start.tv_sec); \ -// if (end.tv_nsec >= start.tv_nsec) { \ -// duration.tv_nsec = (end.tv_nsec - start.tv_nsec); \ -// } \ -// else { \ -// duration.tv_nsec = (1000000000L - (start.tv_nsec - end.tv_nsec)); \ -// duration.tv_sec--; \ -// } \ -// if (duration.tv_nsec >= 1000000000L) { \ -// duration.tv_sec++; \ -// duration.tv_nsec -= 1000000000L; \ -// } +#define GET_TIME(start, end, duration) \ + duration.tv_sec = (end.tv_sec - start.tv_sec); \ + if (end.tv_nsec >= start.tv_nsec) { \ + duration.tv_nsec = (end.tv_nsec - start.tv_nsec); \ + } \ + else { \ + duration.tv_nsec = (1000000000L - (start.tv_nsec - end.tv_nsec)); \ + duration.tv_sec--; \ + } \ + if (duration.tv_nsec >= 1000000000L) { \ + duration.tv_sec++; \ + duration.tv_nsec -= 1000000000L; \ + } -// static inline unsigned int time_diff ( -// struct timeval *end, struct timeval *begin) -// { -// #ifdef TIMING -// uint64_t result; +static inline unsigned int time_diff ( + struct timeval *end, struct timeval *begin) +{ +#ifdef TIMING + uint64_t result; -// result = end->tv_sec - begin->tv_sec; -// result *= 1000000; /* usec */ -// result += end->tv_usec - begin->tv_usec; + result = end->tv_sec - begin->tv_sec; + result *= 1000000; /* usec */ + result += end->tv_usec - begin->tv_usec; -// return result; -// #else -// return 0; -// #endif -// } + return result; +#else + return 0; +#endif +} // static inline void get_time (struct timeval *t) // { @@ -170,52 +142,51 @@ int MallocCounter; size_t sizeUsed; -// INITAlloc(void) { +INITAlloc(void) { -// size_t sz; -// // Pre Allocate 600 MB -// sz = 100000000; + size_t sz; + // Pre Allocate 600 MB + sz = 100000000; -// int fd = open(FILENAME, O_RDWR, 0600); + int fd = open(FILENAME, O_RDWR, 0600); -// if (fd < 0) { -// perror("open"); -// exit(EXIT_FAILURE); -// } + if (fd < 0) { + perror("open"); + exit(EXIT_FAILURE); + } -// off_t offset = 0; // offset to seek to. + off_t offset = 0; // offset to seek to. -// if (ftruncate(fd, sz) < 0) { -// perror("ftruncate"); -// close(fd); -// exit(EXIT_FAILURE); -// } + 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_PRIVATE|MAP_ANON,-1,0); -// ptr = mmap(NULL, sz, -// PROT_READ|PROT_WRITE, MAP_SHARED,fd,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); -// } + // Added error handling + if(ptr == MAP_FAILED) + { + perror("mmap"); + exit(EXIT_FAILURE); + } -// MallocCounter = (int)sz; + MallocCounter = (int)sz; -// } +} // Quick malloc implementation with mmap -void* my_malloc(size_t sz) +void* MALLOCCHERI(size_t sz) { - printf("called"); sz = __builtin_align_up(sz, _Alignof(max_align_t)); // printf("%d \n", sz); - printf("%d Malloc counter\n", MallocCounter); + // printf("%d Malloc counter\n", MallocCounter); MallocCounter -= sz; void *ptrLink = &ptr[MallocCounter]; @@ -230,7 +201,7 @@ void* my_malloc(size_t sz) } // Quick cheri free implementation -void my_free(void *ptr) { +void FREECHERI(void *ptr) { // printf("free called \n"); @@ -261,7 +232,7 @@ pagesizes(size_t ps[MAXPAGESIZES]) INITREGULARALLOC(void) { size_t sz; - // Pre Allocate 400 MB + // Hard-coded for 1GB huge page sz = 1073741824; int error, fd, pscnt, pn; @@ -314,170 +285,14 @@ INITREGULARALLOC(void) { MallocCounter = (int)sz; } +// Standard Alloc +// void* MALLOCREGULAR(size_t sz) { + +// } -// ------------------------------ bitmap allocator ---------------------------- +// void* CLEARALLOC(void) { +// / +// } -#define BITS_PER_BYTE 8 - -char *buffer = NULL; /* allocation buffer */ -unsigned char *bitmap = NULL; /* bitmap for the buffer */ - -int buffer_size = 0; /* size of buffer (in bytes) */ -int bitmap_size = 0; /* size of bitmap (in bytes) */ -int bytes_per_chunk = 0; /* size of single chunk (in bytes) */ - -void init_alloc(int num_chunks, int chunk_size) -{ - int i = 0; - - /* we need to increase the num_chunks - * so every bit in bitmap will be used - */ - int adjusted_num_chunks = (num_chunks % BITS_PER_BYTE == 0) - ? num_chunks - : (num_chunks + (BITS_PER_BYTE - (num_chunks % BITS_PER_BYTE))); - - /* we need to increase the chunk_size - * so chunks will be CHERI aligned - * (i.e. 16 bytes for RISC-V 64-bit arch) - */ - int adjusted_chunk_size = - (chunk_size % (sizeof(void *)) == 0) - ? chunk_size - : (chunk_size + (sizeof(void *)) - (chunk_size % (sizeof(void *)))); - - /* check this chunk size is small enough so we can represent - * bounds precisely with CHERI compressed representation - */ - adjusted_chunk_size = cheri_representable_length(adjusted_chunk_size); - - /* request memory for our allocation buffer */ - char *res = mmap(NULL, adjusted_num_chunks * adjusted_chunk_size, PROT_READ | PROT_WRITE, - MAP_ANON | MAP_PRIVATE, -1, 0); - /* request memory for our bitmap */ - bitmap = (void *) mmap(NULL, adjusted_num_chunks / BITS_PER_BYTE, - PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE, -1, 0); - - if (res == MAP_FAILED || bitmap == MAP_FAILED) - { - perror("error in initial mem allocation"); - exit(-1); - } - - /* NB mmap min bounds for capability is 1 page (4K) */ - buffer = res; - /* check buffer is aligned */ - assert((uintptr_t) buffer % sizeof(void *) == 0); - /* check bitmap is aligned */ - assert((uintptr_t) bitmap % sizeof(void *) == 0); - - bytes_per_chunk = adjusted_chunk_size; - buffer_size = adjusted_num_chunks * adjusted_chunk_size; - bitmap_size = adjusted_num_chunks / BITS_PER_BYTE; - - /* zero bitmap, since all chunks are free initially */ - for (i = 0; i < bitmap_size; i++) - { - bitmap[i] = 0; - } - - // set exact bounds for buffer and bitmap? - buffer = cheri_setbounds(buffer, buffer_size); - bitmap = cheri_setbounds(bitmap, bitmap_size); - return; -} - -/* - * allocate fixed size chunk with bitmap allocator - * this is our simplistic `malloc` function - */ -char *alloc_chunk() -{ - unsigned char updated_byte = 0; - int chunk_index = 0; - char *chunk = NULL; - // iterate over all bits in bitmap, looking for a 0 - // when we find a 0, set it to 1 and - // return the corresponding chunk - // (setting its capability bounds) - int i = 0; - while (bitmap[i] == (unsigned char) 0xff) - { - i++; - if (i >= bitmap_size) - break; - } - // do we have a 0? - if (i < bitmap_size && bitmap[i] != (unsigned char) 0xff) - { - // find the lowest 0 ... - int j = 0; - // right shift until bottom bit is 0 - for (j = 0; j < BITS_PER_BYTE; j++) - { - int bit = (bitmap[i] >> j) & 1; - if (bit == 0) - { - break; - } - } - // now i is the word index, j is the bit index - // set this bit to 1 ... - // and work out the chunk to allocate - updated_byte = bitmap[i] + (unsigned char) (1 << j); - bitmap[i] = updated_byte; - - chunk_index = i * BITS_PER_BYTE + j; - chunk = buffer + (chunk_index * bytes_per_chunk); - - /* restrict capability range before returning ptr */ - chunk = cheri_setbounds(chunk, bytes_per_chunk); - } - - return chunk; -} - -void free_chunk(void *chunk) -{ - vaddr_t base = cheri_getbase(chunk); - vaddr_t buff_base = cheri_getbase(buffer); - /* calculate chunk index in buffer */ - int chunk_index = (base - buff_base) / bytes_per_chunk; - assert(chunk_index >= 0); - /* calculate corresponding bitmap index */ - int bitmap_index = chunk_index / BITS_PER_BYTE; - assert(bitmap_index < bitmap_size); - int bitmap_offset = chunk_index % BITS_PER_BYTE; - /* set this bitmap entry to 0 */ - unsigned char updated_byte = bitmap[bitmap_index] & (unsigned char) (~(1 << bitmap_offset)); - bitmap[bitmap_index] = updated_byte; - return; -} - -int num_used_chunks() -{ - int i = 0; - int used_chunks = 0; - - while (i < bitmap_size) - { - unsigned char x = bitmap[i]; - if (x != 0) - { - /* some used chunks here */ - unsigned char j; - for (j = 1; j <= x; j = j << 1) - { - if (x & j) - { - used_chunks++; - } - } - } - i++; - } - return used_chunks; -} - -#endif // STDDEFINES_H_ \ No newline at end of file +#endif // STDDEFINES_H_ diff --git a/benchmarks/benchmarks/barnes/CMakeLists.txt b/benchmarks/benchmarks/barnes/CMakeLists.txt new file mode 100644 index 0000000..917be9f --- /dev/null +++ b/benchmarks/benchmarks/barnes/CMakeLists.txt @@ -0,0 +1,116 @@ +cmake_minimum_required(VERSION 3.0) +project(mimalloc-bench CXX C) +set(CMAKE_CXX_STANDARD 17) + +if (NOT CMAKE_BUILD_TYPE) + message(STATUS "No build type selected, default to *** Release ***") + set(CMAKE_BUILD_TYPE "Release") +endif() + +FUNCTION(PREPEND var prefix) + SET(listVar "") + FOREACH(f ${ARGN}) + LIST(APPEND listVar "${prefix}/${f}") + ENDFOREACH(f) + SET(${var} "${listVar}" PARENT_SCOPE) +ENDFUNCTION(PREPEND) + +# set(cfrac_sources +# cfrac.c +# pops.c pconst.c pio.c +# pabs.c pneg.c pcmp.c podd.c phalf.c +# padd.c psub.c pmul.c pdivmod.c psqrt.c ppowmod.c +# atop.c ptoa.c itop.c utop.c ptou.c errorp.c +# pfloat.c pidiv.c pimod.c picmp.c +# primes.c pcfrac.c pgcd.c) +# PREPEND(cfrac_sources . ${cfrac_sources}) + +# set(espresso_sources +# cofactor.c cols.c compl.c contain.c cubestr.c cvrin.c cvrm.c cvrmisc.c cvrout.c +# dominate.c equiv.c espresso.c essen.c exact.c expand.c gasp.c getopt.c gimpel.c +# globals.c hack.c indep.c irred.c main.c map.c matrix.c mincov.c opo.c pair.c part.c +# primes.c reduce.c rows.c set.c setc.c sharp.c sminterf.c solution.c sparse.c unate.c +# utility.c verify.c) +# PREPEND(espresso_sources . ${espresso_sources}) + +set(barnes_sources + code.c code_io.c load.c grav.c getparam.c util.c) +PREPEND(barnes_sources . ${barnes_sources}) + +# turn off warnings.. +message(STATUS "${CMAKE_C_COMPILER_ID}") +if(CMAKE_C_COMPILER_ID MATCHES "AppleClang|Clang|GNU") + set(FLAGS " -w -Wno-implicit-function-declaration -Wno-implicit-int -Wno-int-conversion -Wno-return-mismatch -Wno-incompatible-pointer-types -mabi=purecap-benchmark") + string(APPEND CMAKE_C_FLAGS ${FLAGS}) + string(APPEND CMAKE_CXX_FLAGS ${FLAGS}) +endif() + +# add_executable(cfrac ${cfrac_sources}) +# target_compile_options(cfrac PRIVATE $<$:-std=gnu89>) +# target_compile_definitions(cfrac PRIVATE NOMEMOPT=1) +# target_link_libraries(cfrac m) + +# add_executable(espresso ${espresso_sources}) +# target_compile_options(espresso PRIVATE $<$:-std=gnu89>) +# target_link_libraries(espresso m) + +add_executable(barnes ${barnes_sources}) +target_link_libraries(barnes m) + +# add_executable(larson larson/larson.cpp) +# target_compile_options(larson PRIVATE -Wno-unused-result) +# target_compile_definitions(larson PRIVATE CPP=1) +# target_link_libraries(larson pthread) + +# add_executable(larson-sized larson/larson.cpp) +# target_compile_options(larson-sized PRIVATE -Wno-unused-result -fsized-deallocation) +# target_compile_definitions(larson-sized PRIVATE CPP=1 SIZED=1) +# target_link_libraries(larson-sized pthread) + +# add_executable(alloc-test alloc-test/test_common.cpp alloc-test/allocator_tester.cpp) +# target_compile_definitions(alloc-test PRIVATE BENCH=4) +# target_link_libraries(alloc-test pthread) + +# if(NOT APPLE) +# add_executable(sh6bench shbench/sh6bench-new.c) +# target_compile_definitions(sh6bench PRIVATE BENCH=1 SYS_MULTI_THREAD=1) +# target_link_libraries(sh6bench pthread) + +# add_executable(sh8bench shbench/sh8bench-new.c) +# target_compile_definitions(sh8bench PRIVATE BENCH=1 SYS_MULTI_THREAD=1) +# target_link_libraries(sh8bench pthread) +# endif() + +# add_executable(cache-scratch cache-scratch/cache-scratch.cpp) +# target_link_libraries(cache-scratch pthread) + +# add_executable(cache-thrash cache-thrash/cache-thrash.cpp) +# target_link_libraries(cache-thrash pthread) + +# add_executable(xmalloc-test xmalloc-test/xmalloc-test.c) +# target_link_libraries(xmalloc-test pthread) + +# add_executable(malloc-large-old malloc-large/malloc-large-old.cpp) +# target_link_libraries(malloc-large-old pthread) + +# add_executable(malloc-large malloc-large/malloc-large.cpp) +# target_link_libraries(malloc-large pthread) + +# add_executable(mstress mstress/mstress.c) +# target_link_libraries(mstress pthread) + +# add_executable(mleak mleak/mleak.c) +# target_link_libraries(mleak pthread) + +# add_executable(rptest rptest/rptest.c rptest/thread.c rptest/timer.c) +# target_compile_options(rptest PRIVATE -fpermissive) +# target_include_directories(rptest PRIVATE rptest) +# target_link_libraries(rptest pthread m) + +# add_executable(glibc-simple glibc-bench/bench-malloc-simple.c) +# target_link_libraries(glibc-simple pthread) + +# add_executable(glibc-thread glibc-bench/bench-malloc-thread.c) +# target_link_libraries(glibc-thread pthread) + +# \ No newline at end of file diff --git a/benchmarks/benchmarks/barnes/code.c b/benchmarks/benchmarks/barnes/code.c index bb7142e..5dd618f 100644 --- a/benchmarks/benchmarks/barnes/code.c +++ b/benchmarks/benchmarks/barnes/code.c @@ -77,6 +77,12 @@ Command line options: #include #include +// #include "malloc.h" + +// #define malloc MALLOCCHERI +// #define free FREECHERI + + string defv[] = { /* DEFAULT PARAMETER VALUES */ /* file names for input/output */ "in=", /* snapshot of initial conditions */ @@ -109,6 +115,9 @@ main(argc, argv) int argc; string argv[]; { + + // INITREGULARALLOC(); + unsigned ProcessId = 0; int c; printf("Run this as\n BARNES < input \n for default values\n"); diff --git a/benchmarks/benchmarks/barnes/code.h b/benchmarks/benchmarks/barnes/code.h index 32a5e86..ab75fa0 100644 --- a/benchmarks/benchmarks/barnes/code.h +++ b/benchmarks/benchmarks/barnes/code.h @@ -26,6 +26,8 @@ #include "defs.h" + + #define PAD_SIZE (PAGE_SIZE / (sizeof(int))) /* Defined by the input file */ diff --git a/benchmarks/benchmarks/barnes/getparam.c b/benchmarks/benchmarks/barnes/getparam.c index fd636cf..dc1a688 100644 --- a/benchmarks/benchmarks/barnes/getparam.c +++ b/benchmarks/benchmarks/barnes/getparam.c @@ -54,7 +54,7 @@ string getparam(name) if (i < 0) error("getparam: %s unknown\n", name); def = extrvalue(defaults[i]); - gets(buf); + gets_s(buf,100); leng = strlen(buf) + 1; if (leng > 1) { return (strcpy(malloc(leng), buf)); diff --git a/benchmarks/benchmarks/barnes/malloc.h b/benchmarks/benchmarks/barnes/malloc.h new file mode 100644 index 0000000..f952ba6 --- /dev/null +++ b/benchmarks/benchmarks/barnes/malloc.h @@ -0,0 +1,297 @@ +/* Copyright (c) 2007-2009, Stanford University +* All rights reserved. +* +* Redistribution and use in source and binary forms, with or without +* modification, are permitted provided that the following conditions are met: +* * Redistributions of source code must retain the above copyright +* notice, this list of conditions and the following disclaimer. +* * Redistributions in binary form must reproduce the above copyright +* notice, this list of conditions and the following disclaimer in the +* documentation and/or other materials provided with the distribution. +* * Neither the name of Stanford University nor the +* names of its contributors may be used to endorse or promote products +* derived from this software without specific prior written permission. +* +* THIS SOFTWARE IS PROVIDED BY STANFORD UNIVERSITY ``AS IS'' AND ANY +* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +* DISCLAIMED. IN NO EVENT SHALL STANFORD UNIVERSITY BE LIABLE FOR ANY +* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND +* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + +#ifndef STDDEFINES_H_ +#define STDDEFINES_H_ + +#include +#include +#include +#include +#include +#include +// #include + + +#include +#include +#include +#include +#include + +#include +#include + +#include +#include + +#define MAXPAGESIZES 2 + + +//#define TIMING + +/* Debug printf */ +#define dprintf(...) fprintf(stdout, __VA_ARGS__) + +/* Wrapper to check for errors */ +#define CHECK_ERROR(a) \ + if (a) \ + { \ + perror("Error at line\n\t" #a "\nSystem Msg"); \ + assert ((a) == 0); \ + } + +static inline void *MALLOC(size_t size) +{ + void * temp = malloc(size); + assert(temp); + return temp; +} + +static inline void *CALLOC(size_t num, size_t size) +{ + void * temp = calloc(num, size); + assert(temp); + return temp; +} + +static inline void *REALLOC(void *ptr, size_t size) +{ + void * temp = realloc(ptr, size); + assert(temp); + return temp; +} + +static inline char *GETENV(char *envstr) +{ + char *env = getenv(envstr); + if (!env) return "0"; + else return env; +} + +#define GET_TIME(start, end, duration) \ + duration.tv_sec = (end.tv_sec - start.tv_sec); \ + if (end.tv_nsec >= start.tv_nsec) { \ + duration.tv_nsec = (end.tv_nsec - start.tv_nsec); \ + } \ + else { \ + duration.tv_nsec = (1000000000L - (start.tv_nsec - end.tv_nsec)); \ + duration.tv_sec--; \ + } \ + if (duration.tv_nsec >= 1000000000L) { \ + duration.tv_sec++; \ + duration.tv_nsec -= 1000000000L; \ + } + +static inline unsigned int time_diff ( + struct timeval *end, struct timeval *begin) +{ +#ifdef TIMING + uint64_t result; + + result = end->tv_sec - begin->tv_sec; + result *= 1000000; /* usec */ + result += end->tv_usec - begin->tv_usec; + + return result; +#else + return 0; +#endif +} + +// static inline void get_time (struct timeval *t) +// { +// #ifdef TIMING +// gettimeofday (t, NULL); +// #endif +// } + +// Expirement work + +#define FILENAME "/dev/contigmem" + +static char *heap_start; +static char *heap; +static size_t HEAP_SIZE = 1024 * 1024 * 1024; + +void *ptrEspresso; +int MallocCounterEspresso; + +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); + + ptrEspresso = mmap(NULL, sz, + PROT_READ|PROT_WRITE, MAP_SHARED,fd,0); + + // Added error handling + if(ptrEspresso == MAP_FAILED) + { + perror("mmap"); + exit(EXIT_FAILURE); + } + + MallocCounterEspresso = (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); + + MallocCounterEspresso -= sz; + void *ptrLink = &ptrEspresso[MallocCounterEspresso]; + 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); + + ptrEspresso = mmap(NULL, sz, + PROT_READ|PROT_WRITE, MAP_SHARED,fd,0); + + // Added error handling + if(ptrEspresso == MAP_FAILED) + { + perror("mmap"); + exit(EXIT_FAILURE); + } + + MallocCounterEspresso = (int)sz; +} +// Standard Alloc +// void* MALLOCREGULAR(size_t sz) { + +// } + + +// void* CLEARALLOC(void) { +// / +// } + +#endif // STDDEFINES_H_ diff --git a/benchmarks/benchmarks/barnes/run.sh b/benchmarks/benchmarks/barnes/run.sh new file mode 100644 index 0000000..636ef82 --- /dev/null +++ b/benchmarks/benchmarks/barnes/run.sh @@ -0,0 +1 @@ +time pmcstat -d -w 1 -p l1d_tlb_rd -p l2d_tlb_rd -p l1d_tlb_refill -p cpu_cycles -p dtlb_walk -p stall_backend -p ll_cache_miss_rd -o barnes-regular.txt ./barnes BARNES < input \ No newline at end of file diff --git a/benchmarks/benchmarks/cfrac/cfrac.c b/benchmarks/benchmarks/cfrac/cfrac.c index 9185d18..5fc341d 100644 --- a/benchmarks/benchmarks/cfrac/cfrac.c +++ b/benchmarks/benchmarks/cfrac/cfrac.c @@ -14,6 +14,11 @@ #include "precision.h" #include "pfactor.h" +#include "malloc.h" + +#define malloc MALLOCCHERI +#define free FREECHERI + #ifdef __STDC__ extern unsigned *pfactorbase(precision n, unsigned k, unsigned *m, unsigned aborts); @@ -177,6 +182,10 @@ int main(argc, argv) int argc; char *argv[]; { + + + INITREGULARALLOC(0); + unsigned m = 0, k = 0; unsigned maxCount = 1<<30, count, maxk = 0; int ch; diff --git a/benchmarks/benchmarks/cfrac/malloc.h b/benchmarks/benchmarks/cfrac/malloc.h new file mode 100644 index 0000000..9397055 --- /dev/null +++ b/benchmarks/benchmarks/cfrac/malloc.h @@ -0,0 +1,316 @@ +/* Copyright (c) 2007-2009, Stanford University +* All rights reserved. +* +* Redistribution and use in source and binary forms, with or without +* modification, are permitted provided that the following conditions are met: +* * Redistributions of source code must retain the above copyright +* notice, this list of conditions and the following disclaimer. +* * Redistributions in binary form must reproduce the above copyright +* notice, this list of conditions and the following disclaimer in the +* documentation and/or other materials provided with the distribution. +* * Neither the name of Stanford University nor the +* names of its contributors may be used to endorse or promote products +* derived from this software without specific prior written permission. +* +* THIS SOFTWARE IS PROVIDED BY STANFORD UNIVERSITY ``AS IS'' AND ANY +* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +* DISCLAIMED. IN NO EVENT SHALL STANFORD UNIVERSITY BE LIABLE FOR ANY +* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND +* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + +#ifndef STDDEFINES_H_ +#define STDDEFINES_H_ + +#include +#include +#include +#include +#include +#include +#include + + +#include +#include +#include +#include +#include + +#include +#include + +#include +#include +#include // For boolean data type (bool, true, false) + +#define MAXPAGESIZES 2 + + +//#define TIMING + +/* Debug printf */ +#define dprintf(...) fprintf(stdout, __VA_ARGS__) + +/* Wrapper to check for errors */ +#define CHECK_ERROR(a) \ + if (a) \ + { \ + perror("Error at line\n\t" #a "\nSystem Msg"); \ + assert ((a) == 0); \ + } + +static inline void *MALLOC(size_t size) +{ + void * temp = malloc(size); + assert(temp); + return temp; +} + +static inline void *CALLOC(size_t num, size_t size) +{ + void * temp = calloc(num, size); + assert(temp); + return temp; +} + +static inline void *REALLOC(void *ptr, size_t size) +{ + void * temp = realloc(ptr, size); + assert(temp); + return temp; +} + +static inline char *GETENV(char *envstr) +{ + char *env = getenv(envstr); + if (!env) return "0"; + else return env; +} + +#define GET_TIME(start, end, duration) \ + duration.tv_sec = (end.tv_sec - start.tv_sec); \ + if (end.tv_nsec >= start.tv_nsec) { \ + duration.tv_nsec = (end.tv_nsec - start.tv_nsec); \ + } \ + else { \ + duration.tv_nsec = (1000000000L - (start.tv_nsec - end.tv_nsec)); \ + duration.tv_sec--; \ + } \ + if (duration.tv_nsec >= 1000000000L) { \ + duration.tv_sec++; \ + duration.tv_nsec -= 1000000000L; \ + } + +static inline unsigned int time_diff ( + struct timeval *end, struct timeval *begin) +{ +#ifdef TIMING + uint64_t result; + + result = end->tv_sec - begin->tv_sec; + result *= 1000000; /* usec */ + result += end->tv_usec - begin->tv_usec; + + return result; +#else + return 0; +#endif +} + +// static inline void get_time (struct timeval *t) +// { +// #ifdef TIMING +// gettimeofday (t, NULL); +// #endif +// } + +// Expirement work + +#define FILENAME "/dev/contigmem" + +static char *heap_start; +static char *heap; +static size_t HEAP_SIZE = 1024 * 1024 * 1024; + +void *ptr; +void *ptr1; +void *ptr2; +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 new\n", sz); + + if (sz > MallocCounter) { + printf("%d Threashold exceeded\n", sz); + INITREGULARALLOC(1); + } + + 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(int full) { + size_t sz; + // Hard-coded for 1GB huge page + sz = 1073741824; + + int error, fd, pscnt, pn; + + size_t ps[MAXPAGESIZES]; + + size_t size[3]; + + // Point to initially to pointer 1 + if (full == 1) { + ptr = ptr2; + } + else { + ptr = ptr1; + } + 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_ + + diff --git a/benchmarks/benchmarks/cfrac/pcfrac.c b/benchmarks/benchmarks/cfrac/pcfrac.c index 2a0d032..ac8f48f 100644 --- a/benchmarks/benchmarks/cfrac/pcfrac.c +++ b/benchmarks/benchmarks/cfrac/pcfrac.c @@ -15,6 +15,10 @@ #include "precision.h" #include "pfactor.h" + +#define malloc MALLOCCHERI +#define free FREECHERI + extern int verbose; unsigned cfracNabort = 0; diff --git a/benchmarks/benchmarks/cfrac/pdefs.h b/benchmarks/benchmarks/cfrac/pdefs.h index 9c43099..e6d0e35 100644 --- a/benchmarks/benchmarks/cfrac/pdefs.h +++ b/benchmarks/benchmarks/cfrac/pdefs.h @@ -14,6 +14,9 @@ #endif #define uMul(u, v) ((u) * (v)) /* fast enough */ +#define malloc MALLOCCHERI +#define free FREECHERI + /* * Optional alternate memory allocator */ diff --git a/benchmarks/benchmarks/cfrac/pops.c b/benchmarks/benchmarks/cfrac/pops.c index 7154dd7..1e615ae 100644 --- a/benchmarks/benchmarks/cfrac/pops.c +++ b/benchmarks/benchmarks/cfrac/pops.c @@ -93,29 +93,30 @@ precision palloc(size) register precision w; register cacheType *kludge = pcache + size; /* for shitty compilers */ -#if !(defined(NOMEMOPT) || defined(BWGC)) - if (size < CACHESIZE && (w = kludge->next) != pUndef) { - kludge->next = ((cacheType *) w)->next; - --kludge->count; - } else { -#endif +// #if !(defined(NOMEMOPT) || defined(BWGC)) +// if (size < CACHESIZE && (w = kludge->next) != pUndef) { +// kludge->next = ((cacheType *) w)->next; +// --kludge->count; +// } else { +// #endif w = (precision) allocate(PrecisionSize + sizeof(digit) * size); if (w == pUndef) { w = errorp(PNOMEM, "palloc", "out of memory"); return w; } -#if !(defined(NOMEMOPT) || defined(BWGC)) - } -#endif +// #if !(defined(NOMEMOPT) || defined(BWGC)) +// } +// #endif #ifndef BWGC w->refcount = 1; #endif w->size = w->alloc = size; -#ifdef DEBUGOPS +// #ifdef DEBUGOPS printf("alloc %.8x\n", w); - fflush(stdout); -#endif + // fflush(stdout); +// #endif return w; + } /* diff --git a/benchmarks/benchmarks/espresso/main.c b/benchmarks/benchmarks/espresso/main.c index fe1829d..d4b8626 100644 --- a/benchmarks/benchmarks/espresso/main.c +++ b/benchmarks/benchmarks/espresso/main.c @@ -6,17 +6,24 @@ #include "espresso.h" #include "main.h" /* table definitions for options */ +#include "malloc.h" + +#define malloc MALLOCCHERI +#define free FREECHERI static FILE *last_fp; static int input_type = FD_type; #include + + void gmalloc_exit(void); static int mainx(int argc, char* argv[]); int main(int argc, char* argv[]) { + INITREGULARALLOC(); int i; extern int optind; for(i = 0; i < 20; i++) { // benchmark N iterations @@ -720,9 +727,9 @@ int *out_type; } } - if (check_arg(argc, argv, "-fdr")) input_type = FDR_type; - if (check_arg(argc, argv, "-fr")) input_type = FR_type; - if (check_arg(argc, argv, "-f")) input_type = F_type; + // if (check_arg(argc, argv, "-fdr")) input_type = FDR_type; + // if (check_arg(argc, argv, "-fr")) input_type = FR_type; + // if (check_arg(argc, argv, "-f")) input_type = F_type; } diff --git a/benchmarks/benchmarks/espresso/malloc.h b/benchmarks/benchmarks/espresso/malloc.h new file mode 100644 index 0000000..fe39747 --- /dev/null +++ b/benchmarks/benchmarks/espresso/malloc.h @@ -0,0 +1,306 @@ +/* Copyright (c) 2007-2009, Stanford University +* All rights reserved. +* +* Redistribution and use in source and binary forms, with or without +* modification, are permitted provided that the following conditions are met: +* * Redistributions of source code must retain the above copyright +* notice, this list of conditions and the following disclaimer. +* * Redistributions in binary form must reproduce the above copyright +* notice, this list of conditions and the following disclaimer in the +* documentation and/or other materials provided with the distribution. +* * Neither the name of Stanford University nor the +* names of its contributors may be used to endorse or promote products +* derived from this software without specific prior written permission. +* +* THIS SOFTWARE IS PROVIDED BY STANFORD UNIVERSITY ``AS IS'' AND ANY +* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +* DISCLAIMED. IN NO EVENT SHALL STANFORD UNIVERSITY BE LIABLE FOR ANY +* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND +* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + +#ifndef STDDEFINES_H_ +#define STDDEFINES_H_ + +#include +#include +#include +#include +#include +#include +// #include + + +#include +#include +#include +#include +#include + +#include +#include + +#include +#include + +#define MAXPAGESIZES 2 + + +//#define TIMING + +/* Debug printf */ +#define dprintf(...) fprintf(stdout, __VA_ARGS__) + +/* Wrapper to check for errors */ +#define CHECK_ERROR(a) \ + if (a) \ + { \ + perror("Error at line\n\t" #a "\nSystem Msg"); \ + assert ((a) == 0); \ + } + +// static inline void *MALLOC(size_t size) +// { +// void * temp = malloc(size); +// assert(temp); +// return temp; +// } + +// static inline void *CALLOC(size_t num, size_t size) +// { +// void * temp = calloc(num, size); +// assert(temp); +// return temp; +// } + +// static inline void *REALLOC(void *ptr, size_t size) +// { +// void * temp = realloc(ptr, size); +// assert(temp); +// return temp; +// } + +// static inline char *GETENV(char *envstr) +// { +// char *env = getenv(envstr); +// if (!env) return "0"; +// else return env; +// } + +// #define GET_TIME(start, end, duration) \ +// duration.tv_sec = (end.tv_sec - start.tv_sec); \ +// if (end.tv_nsec >= start.tv_nsec) { \ +// duration.tv_nsec = (end.tv_nsec - start.tv_nsec); \ +// } \ +// else { \ +// duration.tv_nsec = (1000000000L - (start.tv_nsec - end.tv_nsec)); \ +// duration.tv_sec--; \ +// } \ +// if (duration.tv_nsec >= 1000000000L) { \ +// duration.tv_sec++; \ +// duration.tv_nsec -= 1000000000L; \ +// } + +// static inline unsigned int time_diff ( +// struct timeval *end, struct timeval *begin) +// { +// #ifdef TIMING +// uint64_t result; + +// result = end->tv_sec - begin->tv_sec; +// result *= 1000000; /* usec */ +// result += end->tv_usec - begin->tv_usec; + +// return result; +// #else +// return 0; +// #endif +// } + +// static inline void get_time (struct timeval *t) +// { +// #ifdef TIMING +// gettimeofday (t, NULL); +// #endif +// } + +// Expirement work + +#define FILENAME "/dev/contigmem" + +static char *heap_start; +static char *heap; +static size_t HEAP_SIZE = 1024 * 1024 * 1024; + +void *ptrEspresso; +int MallocCounterEspresso; + +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); + + ptrEspresso = mmap(NULL, sz, + PROT_READ|PROT_WRITE, MAP_SHARED,fd,0); + + // Added error handling + if(ptrEspresso == MAP_FAILED) + { + perror("mmap"); + exit(EXIT_FAILURE); + } + + MallocCounterEspresso = (int)sz; + +} + +int sizeCounter = 0; + +// Quick malloc implementation with mmap +void* MALLOCCHERI(size_t sz) +{ + sz = __builtin_align_up(sz, _Alignof(max_align_t)); + +// // printf("%d \n", sz); + + sizeCounter += sz; + printf("%d Malloc counter\n", sizeCounter); + + MallocCounterEspresso -= sz; + void *ptrLink = &ptrEspresso[MallocCounterEspresso]; + ptrLink = cheri_setbounds(ptrLink, sz); + + return ptrLink; + + // return malloc(sz); + +// 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); + + free(ptr); +} + +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); + + ptrEspresso = mmap(NULL, sz, + PROT_READ|PROT_WRITE, MAP_SHARED,fd,0); + + // Added error handling + if(ptrEspresso == MAP_FAILED) + { + perror("mmap"); + exit(EXIT_FAILURE); + } + + MallocCounterEspresso = (int)sz; +} +// Standard Alloc +// void* MALLOCREGULAR(size_t sz) { + +// } + + +// void* CLEARALLOC(void) { +// / +// } + +#endif // STDDEFINES_H_ diff --git a/benchmarks/benchmarks/espresso/utility.h b/benchmarks/benchmarks/espresso/utility.h index dcb834e..c6b60aa 100644 --- a/benchmarks/benchmarks/espresso/utility.h +++ b/benchmarks/benchmarks/espresso/utility.h @@ -5,6 +5,10 @@ #include //#include +#define malloc MALLOCCHERI +#define free FREECHERI + + /* * assumes the memory manager is libmm.a * - allows malloc(0) or realloc(obj, 0) diff --git a/localpush.sh b/localpush.sh index e42e470..9d1feea 100644 --- a/localpush.sh +++ b/localpush.sh @@ -2,5 +2,5 @@ files=`find . -newermt "-3600 secs" | sed 's|^./||'` for file in $files do - sshpass -p "" scp "$file" "akilan@192.0.0.3:/home/akilan/Alloc-Test/CHERI-Allocator/$file" + sshpass -p "" scp "$file" "akilan@192.168.0.10:/home/akilan/Alloc-Test/CHERI-Allocator/$file" done