tests.cpp (2454B)
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <blt/syscall.h> 4 #include <blt/os.h> 5 6 #define LOOK_ITS_A_RACE 1 7 8 #if LOOK_ITS_A_RACE 9 int malloc_sem = -1; 10 #endif 11 12 // 13 // Test malloc() 14 // 15 int malloc_thread(void*) 16 { 17 for (int i = 0; i < 50000; i++){ 18 #if LOOK_ITS_A_RACE 19 sem_acquire(malloc_sem); 20 #endif 21 free(malloc(10000)); 22 23 #if LOOK_ITS_A_RACE 24 sem_release(malloc_sem); 25 #endif 26 } 27 28 printf("malloc thread finished\n"); 29 os_terminate(0); 30 return 0; 31 } 32 33 void malloc_test(void) 34 { 35 printf("starting malloc tests\n"); 36 #if LOOK_ITS_A_RACE 37 malloc_sem = sem_create(1,"malloc lock"); 38 #endif 39 40 for (int i = 0; i < 5; i++) 41 thr_create(malloc_thread, 0, "malloc_thread"); 42 } 43 44 45 // 46 // producer/consumer 47 // 48 struct cl_info { 49 char name[32]; 50 int port; 51 }; 52 53 int consumer(void *_p) 54 { 55 int port = ((cl_info*) _p)->port; 56 char buffer[10]; 57 58 for (;;) { 59 msg_hdr_t header; 60 header.src = 0; 61 header.dst = port; 62 header.data = buffer; 63 header.size = 10; 64 old_port_recv(&header); 65 printf("%s receive\n", ((cl_info*) _p)->name); 66 } 67 } 68 69 int producer(void *_p) 70 { 71 int port = ((cl_info*) _p)->port; 72 char buffer[10]; 73 74 for (;;) { 75 msg_hdr_t header; 76 header.src = port; 77 header.dst = port; 78 header.data = buffer; 79 header.size = 10; 80 old_port_send(&header); 81 printf("%s send\n", ((cl_info*) _p)->name); 82 } 83 } 84 85 void prodcons(int num_producers, int num_consumers) 86 { 87 int port = port_create(0, "prodcons"); 88 printf("%d producers, %d consumers\n", num_producers, num_consumers); 89 for (int i = 0; i < num_producers; i++) { 90 cl_info *scinfo = new cl_info; 91 snprintf(scinfo->name, 32, "producer %d", i + 1); 92 scinfo->port = port; 93 thr_create(producer, scinfo, scinfo->name); 94 } 95 96 for (int i = 0; i < num_consumers; i++) { 97 cl_info *scinfo = new cl_info; 98 snprintf(scinfo->name, 32, "consumer %d", i + 1); 99 scinfo->port = port; 100 thr_create(consumer, scinfo, scinfo->name); 101 } 102 } 103 104 105 int main() 106 { 107 // Case 1: malloc tests. 108 // This crashes the kernel when more than 1 thread is doing malloc/ 109 // free operations. Using only one thread, or using my own locks, 110 // reduces the problem, although I still see a reboot once in a while. 111 malloc_test(); 112 113 // Case 2: single producer, single consumer 114 // The system quickly wedges when I do this. 115 // prodcons(1, 1); 116 117 // case 3: multi-producer, single consumer 118 // The kernel code looks like it might not handle this case properly. 119 // prodcons(2, 1); 120 121 // Case 4: multi-consumer, single producer 122 // prodcons(1, 2); 123 124 return 0; 125 }