small C benchmarks

This commit is contained in:
2024-10-28 16:53:07 +00:00
parent 30bbe8dccd
commit 9531e9c48a
8 changed files with 869 additions and 37 deletions

View File

@@ -0,0 +1,3 @@
cc -g -Wall -o memaccesstest.out -mabi=purecap-benchmark -lpthread memaccesstest.c
cc -g -Wall -o loadmem.out -mabi=purecap-benchmark -lpthread loadmem.c
cc -g -Wall -o glibc-bench.out -mabi=purecap-benchmark -lpthread glibc-bench.c

View File

@@ -0,0 +1,9 @@
#define NPAD 7
#define MIN_WSS sizeof(struct l)
#ifdef __MACH__
#define MAX_WSS 4294967296 //2^31
#else
#define MAX_WSS 8589934592 //2^32
#endif
#define NTHREADS 0

View File

@@ -0,0 +1,208 @@
/* Benchmark malloc and free functions.
Copyright (C) 2019-2021 Free Software Foundation, Inc.
This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with the GNU C Library; if not, see
<https://www.gnu.org/licenses/>. */
// modified by Daan Leijen to fit the bench suite and add lifo/fifo free order.
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/resource.h>
#include "malloc.h"
// #include "bench-timing.h"
// #include "json-lib.h"
/* Benchmark the malloc/free performance of a varying number of blocks of a
given size. This enables performance tracking of the t-cache and fastbins.
It tests 3 different scenarios: single-threaded using main arena,
multi-threaded using thread-arena, and main arena with SINGLE_THREAD_P
false. */
// source: https://github.com/daanx/mimalloc-bench/blob/master/bench/glibc-bench/bench-malloc-thread.c
#define NUM_ITERS 2000000
#define NUM_ALLOCS 4
#define MAX_ALLOCS 1600
// Daan: disable timing
typedef long timing_t;
#define TIMING_NOW(s)
#define TIMING_DIFF(e,start,stop)
typedef struct
{
size_t iters;
size_t size;
int n;
timing_t elapsed;
} malloc_args;
static void
do_benchmark (malloc_args *args, char**arr)
{
timing_t start, stop;
size_t iters = args->iters;
size_t size = args->size;
int n = args->n;
TIMING_NOW (start);
for (int j = 0; j < iters; j++)
{
for (int i = 0; i < n; i++) {
arr[i] = malloc (size);
for(int g = 0; g < size; g++) { arr[i][g] =(char)g; }
}
// free half in fifo order
for (int i = 0; i < n/2; i++) {
free (arr[i]);
}
// and the other half in lifo order
for(int i = n-1; i >= n/2; i--) {
free(arr[i]);
}
}
TIMING_NOW (stop);
TIMING_DIFF (args->elapsed, start, stop);
}
static malloc_args tests[3][NUM_ALLOCS];
static int allocs[NUM_ALLOCS] = { 25, 100, 400, MAX_ALLOCS };
static void *
thread_test (void *p)
{
char **arr = (char**)p;
/* Run benchmark multi-threaded. */
for (int i = 0; i < NUM_ALLOCS; i++)
do_benchmark (&tests[2][i], arr);
return p;
}
void
bench (unsigned long size)
{
size_t iters = NUM_ITERS;
char**arr = (char**)malloc (MAX_ALLOCS * sizeof (void*));
for (int t = 0; t < 3; t++)
for (int i = 0; i < NUM_ALLOCS; i++)
{
tests[t][i].n = allocs[i];
tests[t][i].size = size;
tests[t][i].iters = iters / allocs[i];
/* Do a quick warmup run. */
if (t == 0)
do_benchmark (&tests[0][i], arr);
}
/* Run benchmark single threaded in main_arena. */
for (int i = 0; i < NUM_ALLOCS; i++)
do_benchmark (&tests[0][i], arr);
/* Run benchmark in a thread_arena. */
pthread_t t;
pthread_create (&t, NULL, thread_test, (void*)arr);
pthread_join (t, NULL);
/* Repeat benchmark in main_arena with SINGLE_THREAD_P == false. */
for (int i = 0; i < NUM_ALLOCS; i++)
do_benchmark (&tests[1][i], arr);
free (arr);
/*
json_ctx_t json_ctx;
json_init (&json_ctx, 0, stdout);
json_document_begin (&json_ctx);
json_attr_string (&json_ctx, "timing_type", TIMING_TYPE);
json_attr_object_begin (&json_ctx, "functions");
json_attr_object_begin (&json_ctx, "malloc");
char s[100];
double iters2 = iters;
json_attr_object_begin (&json_ctx, "");
json_attr_double (&json_ctx, "malloc_block_size", size);
struct rusage usage;
getrusage (RUSAGE_SELF, &usage);
json_attr_double (&json_ctx, "max_rss", usage.ru_maxrss);
for (int i = 0; i < NUM_ALLOCS; i++)
{
sprintf (s, "main_arena_st_allocs_%04d_time", allocs[i]);
json_attr_double (&json_ctx, s, tests[0][i].elapsed / iters2);
}
for (int i = 0; i < NUM_ALLOCS; i++)
{
sprintf (s, "main_arena_mt_allocs_%04d_time", allocs[i]);
json_attr_double (&json_ctx, s, tests[1][i].elapsed / iters2);
}
for (int i = 0; i < NUM_ALLOCS; i++)
{
sprintf (s, "thread_arena__allocs_%04d_time", allocs[i]);
json_attr_double (&json_ctx, s, tests[2][i].elapsed / iters2);
}
json_attr_object_end (&json_ctx);
json_attr_object_end (&json_ctx);
json_attr_object_end (&json_ctx);
json_document_end (&json_ctx);
*/
}
static void usage (const char *name)
{
fprintf (stderr, "%s: <alloc_size>\n", name);
exit (1);
}
int
main (int argc, char **argv)
{
long size = 16;
if (argc == 2)
size = strtol (argv[1], NULL, 0);
if (argc > 2 || size <= 0)
usage (argv[0]);
bench (size);
bench (2*size);
bench (4*size);
return 0;
}

View File

@@ -0,0 +1,26 @@
#include <stdio.h>
#include <stdlib.h>
#include "malloc.h"
int main(){
int *ptr;
unsigned long i,n;
printf("Enter number of int(4 byte) you want to allocate:");
scanf("%lu",&n);
printf("Allocating %lu bytes......\n",n*sizeof(int));
ptr=(int*)malloc(n*sizeof(int));
if (ptr==NULL){
printf("ERROR!Memory not allocated!");
exit(0);
}
printf("Filling int into memory.....\n");
for (i = 0; i < n; i++){
ptr[i] = 1;
}
printf("Sleep 10 seconds......\n");
sleep(10);
printf("Free memory.\n");
free(ptr);
return 0;
}

View File

@@ -0,0 +1,298 @@
/* 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 <assert.h>
#include <stdlib.h>
#include <sys/time.h>
#include <sys/errno.h>
#include <stdint.h>
#include <stdio.h>
#include <unistd.h>
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <cheriintrin.h>
#include <cheri/cheric.h>
#include <sys/stat.h>
#include <fcntl.h>
#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;
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_

View File

@@ -0,0 +1,317 @@
/*
memaccesstest.c
Author Alex Bordei at bigstep
All rights reserved.
*/
// source: https://github.com/bigstepinc/memaccesstest/tree/master
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <pthread.h>
#include "config.h"
#ifndef __MACH__
#include <time.h>
#else
#include <mach/mach_time.h>
#endif
#include "malloc.h"
struct l{
struct l *n;
struct l *p;
long int pad[NPAD-1];
};
uint64_t rand64()
{
// Combin 4 parts of low 16-bit of each rand()
uint64_t R0 = (uint64_t)random() << 48;
uint64_t R1 = (uint64_t)random() << 48 >> 16;
uint64_t R2 = (uint64_t)random() << 48 >> 32;
uint64_t R3 = (uint64_t)random() << 48 >> 48;
return R0 | R1 | R2 | R3;
};
#ifdef DEBUG
void print_ll_lin(struct l *root,unsigned long working_set_size)
{
struct l *current=root->n;
unsigned long count;
printf("0");
count=0;
while(current!=root)
{
printf("->%ld",current-root);
current=current->n;
count++;
if(count>working_set_size/sizeof(struct l))
{
printf("Error:cycle!");
break;
}
}
printf("\n");
}
void print_ll_lin_back(struct l *root,unsigned long working_set_size)
{
struct l *current=root->p;
unsigned long count;
printf("0");
count=0;
while(current!=root)
{
printf("->%ld",current-root);
current=current->p;
count++;
if(count>working_set_size/sizeof(struct l))
{
printf("Error:cycle!");
break;
}
}
printf("\n");
}
void print_ll(struct l *root,unsigned long working_set_size)
{
unsigned long i;
for(i=0;i<(working_set_size/sizeof(struct l));i++)
printf("DEBUG: %ld<-%ld->%ld\n",(root+i)->p-root,i,((root+i)->n)-root);
printf("DEBUG ");
print_ll_lin(root, working_set_size);
printf("DEBUG ");
print_ll_lin_back(root, working_set_size);
}
#endif
void build_sequencial_ll(struct l *root, long working_set_size)
{
unsigned long i;
struct l *current;
for(i=0;i<(working_set_size/sizeof(struct l));i++)
{
current=root+i;
if(i==(working_set_size/sizeof(struct l)-1))
current->n=root;
else
current->n=current+1;
if(i==0)
current->p=root+working_set_size/sizeof(struct l)-1;
else
current->p=current-1;
}
}
void build_random_ll(struct l *root, long working_set_size)
{
unsigned long i;
struct l *a;
struct l *b;
struct l *oldan;
struct l *oldbn;
struct l *oldap;
struct l *oldbp;
unsigned long rnd;
unsigned long tries;
build_sequencial_ll(root, working_set_size);
//construct the array of elements
//link the list to the next element in the array
for(i=1;i<(working_set_size/sizeof(struct l)-1);i++)
{
a=(root+i);
b=a;
tries=5;
while((b==a || b==root) && tries)
{
rnd=rand64() % (working_set_size/sizeof(struct l)-1);
b=root+rnd;
tries--;
}
if(!tries)
continue;
oldan=a->n; //a->n is lost along the way
oldbn=b->n; //b->n is lost along the way
oldap=a->p; //a->p is lost along the way
oldbp=b->p; //b->p is lost along the way
a->p->n=b;
b->p->n=a;
if(b!=oldan)
{
b->n=oldan;
oldan->p=b;
}
else
{
b->n=a;
a->p=b;
}
if(a!=oldbn)
{
a->n=oldbn;
oldbn->p=a;
}
else
{
a->n=b;
b->p=a;
}
if(b!=oldap)
b->p=oldap;
else
b->p=a;
if(a!=oldbp)
a->p=oldbp;
else
a->p=b;
#ifdef DEBUG
printf("DEBUG: Round i=%ld\n",i);
printf("DEBUG: A=%ld B=%ld\n",a-root,b-root);
print_ll(root, working_set_size);
#endif
}
}
struct thread_start_data
{
struct l *root;
unsigned long working_set_size;
int thread_index;
};
void * walk(void *param)
{
struct thread_start_data *data=(struct thread_start_data*)param;
unsigned long start,end;
struct l *root=data->root;
struct l* current=root->n;
// #ifdef __MACH__
// start=mach_absolute_time();
// #else
// struct timespec ts;
// clock_gettime(CLOCK_MONOTONIC_RAW,&ts);
// start=ts.tv_nsec;
// #endif
/////////////////////////////////////
//traverse the linked list
while(current!=root)
{
#ifdef ADDTEST
current->pad[0]+=current->n->pad[0];
#endif
#ifdef INCTEST
current->pad[0]++;
#endif
current=current->n;
}
/////////////////////////////////////
// #ifdef __MACH__
// end=mach_absolute_time();
// #else
// clock_gettime(CLOCK_MONOTONIC_RAW,&ts);
// end=ts.tv_nsec;
// #endif
// printf("%d,%ld,%ld,%ld\n",data->thread_index,data->working_set_size, end-start, data->working_set_size/sizeof(struct l));
free(data);
return NULL;
}
int main(int argc,char **argv)
{
#ifdef DEBUG
printf("DEBUG:sizeof(struct l)=%ld\n",sizeof(struct l));
#endif
struct l *root;
unsigned long working_set_size;
int i;
struct thread_start_data *tdata;
pthread_t threads[NTHREADS];
root=calloc(MAX_WSS/sizeof(struct l),sizeof(struct l));
for(working_set_size=MIN_WSS;working_set_size<MAX_WSS;working_set_size=working_set_size<<1)
{
#ifdef DEBUG
printf("DEBUG:Working set size=%ld elements=%ld\n",working_set_size,working_set_size/sizeof(struct l));
#endif
#ifdef RANDOM_LIST
build_random_ll(root, working_set_size);
#else
build_sequencial_ll(root, working_set_size);
#endif
#if NTHREADS>0
for(i=0;i<NTHREADS;i++)
{
tdata= malloc(sizeof(struct thread_start_data));
tdata->thread_index=i;
tdata->root=root;
tdata->working_set_size=working_set_size;
pthread_create(&threads[i], NULL, walk, (void*) tdata);
}
for(i=0;i<NTHREADS;i++)
pthread_join(threads[i],NULL);
#else
tdata= malloc(sizeof(struct thread_start_data));
tdata->thread_index=0;
tdata->root=root;
tdata->working_set_size=working_set_size;
walk((void*)tdata);
#endif
}
free(root);
}

View File

@@ -0,0 +1,6 @@
Look into:
- https://github.com/daanx/mimalloc-bench/blob/master/bench/glibc-bench/bench-malloc-thread.c

View File

@@ -3,9 +3,7 @@
#===============================================================================
# Compiler can be set below, or via environment variable
ifeq ($(CC),)
CC = gcc
endif
CC = cc
OPTIMIZE = yes
OPENMP = no
DEBUG = yes
@@ -33,25 +31,11 @@ obj = $(source:.c=.o)
# Sets Flags
#===============================================================================
# Make sure we didn't pick up lower case cc
ifeq (cc,$(CC))
CC = clang
endif
# Standard Flags
CFLAGS := -std=gnu99 -Wall
# Linker Flags
LDFLAGS = -lm
# Regular gcc Compiler
ifneq (,$(findstring gcc,$(CC)))
CFLAGS += -flto
ifeq ($(OPENMP),yes)
CFLAGS += -fopenmp -DOPENMP
endif
endif
# LLVM Compiler
# ifneq (,$(findstring clang,$(CC)))
# CFLAGS += -flto
@@ -75,31 +59,12 @@ endif
# endif
# Profiling Flags
ifeq ($(PROFILE),yes)
CFLAGS += -pg
LDFLAGS += -pg
endif
# Optimization Flags
ifeq ($(OPTIMIZE),yes)
CFLAGS += -o
endif
# MPI
ifeq ($(MPI),yes)
CC = mpicc
CFLAGS += -DMPI
endif
# AML
ifeq ($(AML),yes)
LDFLAGS += $(shell pkg-config --libs aml)
CFLAGS += -DAML $(shell pkg-config --cflags aml)
endif
CFLAGS += -mabi=purecap-benchmark
CFLAGS += -lpthread
CFLAGS += -g -Wall -mabi=purecap-benchmark -lpthread
#===============================================================================
# Targets to Build