nix: slightly better memory allocation
[akaros.git] / tests / block_test.c
1 #include <stdio.h>
2 #include <pthread.h>
3 #include <stdlib.h>
4 #include <parlib.h>
5 #include <unistd.h>
6 #include <sys/time.h>
7
8 pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
9 #define printf_safe(...) {}
10 //#define printf_safe(...) \
11         pthread_mutex_lock(&lock); \
12         printf(__VA_ARGS__); \
13         pthread_mutex_unlock(&lock);
14
15 #define NUM_TEST_THREADS 500
16 #define NUM_TEST_LOOPS 1000
17
18 pthread_t my_threads[NUM_TEST_THREADS];
19 void *my_retvals[NUM_TEST_THREADS];
20
21 __thread int my_id;
22 void *block_thread(void* arg)
23 {       
24         assert(!in_vcore_context());
25         for (int i = 0; i < NUM_TEST_LOOPS; i++) {
26                 printf_safe("[A] pthread %d on vcore %d\n", pthread_self()->id, vcore_id());
27                 sys_block(5000 + pthread_self()->id);
28         }
29         return (void*)(long)pthread_self()->id;
30 }
31
32 int main(int argc, char** argv) 
33 {
34         struct timeval tv = {0};
35         if (gettimeofday(&tv, 0))
36                 perror("Time error...");
37         printf("Start time: %dsec %dusec\n", tv.tv_sec, tv.tv_usec);
38         for (int i = 0; i < NUM_TEST_THREADS; i++) {
39                 printf_safe("[A] About to create thread %d\n", i);
40                 pthread_create(&my_threads[i], NULL, &block_thread, NULL);
41         }
42         for (int i = 0; i < NUM_TEST_THREADS; i++) {
43                 printf_safe("[A] About to join on thread %d\n", i);
44                 pthread_join(my_threads[i], &my_retvals[i]);
45                 printf_safe("[A] Successfully joined on thread %d (retval: %p)\n", i,
46                             my_retvals[i]);
47         }
48         if (gettimeofday(&tv, 0))
49                 perror("Time error...");
50         printf("End time  : %dsec %dusec\n", tv.tv_sec, tv.tv_usec);
51         printf("All done, exiting cleanishly\n");
52