mirror of
https://github.com/hermitcore/libhermit.git
synced 2025-03-30 00:00:15 +01:00
add tests to stress newlib's malloc implementation
This commit is contained in:
parent
d39d8e9f3a
commit
d4a49acf85
4 changed files with 104 additions and 1 deletions
2
test.sh
2
test.sh
|
@ -3,7 +3,7 @@
|
|||
# do not use this script
|
||||
# it is written only for internal tests via Travis CI
|
||||
|
||||
FILES="usr/tests/hello usr/tests/hellof usr/tests/hello++ usr/tests/thr_hello usr/tests/pi usr/benchmarks/stream usr/benchmarks/basic usr/tests/signals"
|
||||
FILES="usr/tests/hello usr/tests/hellof usr/tests/hello++ usr/tests/thr_hello usr/tests/pi usr/benchmarks/stream usr/benchmarks/basic usr/tests/signals usr/tests/test-malloc usr/tests/test-malloc-mt"
|
||||
PROXY=/opt/hermit/bin/proxy
|
||||
|
||||
for f in $FILES; do echo "check $f..."; timeout --kill-after=5m 5m $PROXY $f || exit 1; done
|
||||
|
|
|
@ -9,6 +9,10 @@ add_executable(hello++ hello++.cpp)
|
|||
add_executable(hellof hellof.f90)
|
||||
add_executable(pi pi.go)
|
||||
|
||||
add_executable(test-malloc test-malloc.c)
|
||||
add_executable(test-malloc-mt test-malloc-mt.c)
|
||||
target_link_libraries(test-malloc-mt pthread)
|
||||
|
||||
add_executable(server server.go)
|
||||
target_link_libraries(server netgo)
|
||||
|
||||
|
|
71
usr/tests/test-malloc-mt.c
Normal file
71
usr/tests/test-malloc-mt.c
Normal file
|
@ -0,0 +1,71 @@
|
|||
#include <pthread.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <assert.h>
|
||||
|
||||
#ifndef NUM_THREADS
|
||||
#define NUM_THREADS 3
|
||||
#endif
|
||||
|
||||
#ifndef NUM_ITER
|
||||
#define NUM_ITER 10000
|
||||
#endif
|
||||
|
||||
#ifndef SIZE
|
||||
#define SIZE 16384
|
||||
#endif
|
||||
|
||||
void* perform_work( void* argument )
|
||||
{
|
||||
int passed_in_value;
|
||||
|
||||
passed_in_value = *( ( int* )argument );
|
||||
printf( "Hello World! It's me, thread with argument %d!\n", passed_in_value );
|
||||
|
||||
/* optionally: insert more useful stuff here */
|
||||
int i;
|
||||
void* buf;
|
||||
for(i=0; i<NUM_ITER; i++)
|
||||
{
|
||||
buf = malloc(SIZE*i);
|
||||
free(buf);
|
||||
}
|
||||
malloc_stats();
|
||||
pthread_t id = pthread_self();
|
||||
#ifndef LINUX
|
||||
printf("thread %lu finished allocation\n", id.x);
|
||||
#else
|
||||
printf("thread %u finished allocation\n", id);
|
||||
#endif
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
int main( int argc, char** argv )
|
||||
{
|
||||
pthread_t threads[ NUM_THREADS ];
|
||||
int thread_args[ NUM_THREADS ];
|
||||
int result_code;
|
||||
unsigned index;
|
||||
|
||||
// create all threads one by one
|
||||
for( index = 0; index < NUM_THREADS; ++index )
|
||||
{
|
||||
thread_args[ index ] = index;
|
||||
printf("In main: creating thread %d\n", index);
|
||||
result_code = pthread_create( &threads[index], NULL, perform_work, &thread_args[index] );
|
||||
assert( !result_code );
|
||||
}
|
||||
|
||||
// wait for each thread to complete
|
||||
for( index = 0; index < NUM_THREADS; ++index )
|
||||
{
|
||||
// block until thread 'index' completes
|
||||
result_code = pthread_join( threads[ index ], NULL );
|
||||
assert( !result_code );
|
||||
printf( "In main: thread %d has completed\n", index );
|
||||
}
|
||||
|
||||
printf( "In main: All threads completed successfully\n" );
|
||||
exit( EXIT_SUCCESS );
|
||||
}
|
28
usr/tests/test-malloc.c
Normal file
28
usr/tests/test-malloc.c
Normal file
|
@ -0,0 +1,28 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <assert.h>
|
||||
|
||||
#ifndef NUM_ITER
|
||||
#define NUM_ITER 100000
|
||||
#endif
|
||||
|
||||
#ifndef SIZE
|
||||
#define SIZE 16*1024
|
||||
#endif
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
/* optionally: insert more useful stuff here */
|
||||
void* buf;
|
||||
|
||||
for(int i=0; i<NUM_ITER; i++)
|
||||
{
|
||||
buf = malloc(SIZE*i);
|
||||
free(buf);
|
||||
}
|
||||
malloc_stats();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
Loading…
Add table
Reference in a new issue