| 1 |
#include <stdlib.h> |
| 2 |
#include <stdio.h> |
| 3 |
#include <stdint.h> |
| 4 |
#include <string.h> |
| 5 |
|
| 6 |
#include "vtv_utils.h" |
| 7 |
#include "vtv_rts.h" |
| 8 |
#include "pthread.h" |
| 9 |
|
| 10 |
|
| 11 |
/* Multi-threaded test for calls to RegisterPair */ |
| 12 |
|
| 13 |
/* This configuration will test mostly inserting of elements that are already inserted since |
| 14 |
the number of repeats is 10 */ |
| 15 |
|
| 16 |
/* This test case may fail depending on the system configuration. |
| 17 |
Check the value of /proc/sys/vm/max_map_count and fix by doing |
| 18 |
Ex: sudo sh -c "echo 131060 > /proc/sys/vm/max_map_count" */ |
| 19 |
|
| 20 |
#define NUM_MAPS 200 |
| 21 |
#define ELEMENTS_PER_MAP 100 |
| 22 |
#define NUM_REPEATS 10 |
| 23 |
|
| 24 |
#define NUM_THREADS 9 |
| 25 |
|
| 26 |
#define KEY_TYPE_FIXED_SIZE 8 |
| 27 |
void *key_buffer = malloc (17); |
| 28 |
typedef char * name_string; |
| 29 |
name_string fake_names[NUM_MAPS]; |
| 30 |
|
| 31 |
/* This variable has to be put in rel.ro */ |
| 32 |
void * volatile maps[NUM_MAPS] VTV_PROTECTED_VAR; |
| 33 |
|
| 34 |
struct fake_vt { |
| 35 |
void * fake_vfp [4]; |
| 36 |
}; |
| 37 |
void * fake_vts [NUM_MAPS * ELEMENTS_PER_MAP]; |
| 38 |
|
| 39 |
volatile int current_map = -1; |
| 40 |
volatile int threads_completed_it = 0; |
| 41 |
|
| 42 |
void |
| 43 |
generate_names (void) |
| 44 |
{ |
| 45 |
int i; |
| 46 |
|
| 47 |
for (i = 0; i < NUM_MAPS; ++i) |
| 48 |
{ |
| 49 |
fake_names[i] = (char *) malloc (9 * sizeof (char)); |
| 50 |
snprintf (fake_names[i], 9, "name%d", i); |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
static uint32_t |
| 55 |
vtv_string_hash(const char *in) |
| 56 |
{ |
| 57 |
const char *s = in; |
| 58 |
uint32_t h = 0; |
| 59 |
|
| 60 |
for ( ; *s; ++s) |
| 61 |
h = 5 * h + *s; |
| 62 |
return h; |
| 63 |
} |
| 64 |
|
| 65 |
void * do_register_pairs(void *) |
| 66 |
{ |
| 67 |
for (int k = 0; k < NUM_REPEATS; k++) |
| 68 |
{ |
| 69 |
int curr_fake_vt = 0; |
| 70 |
for (int i = 0; i < NUM_MAPS; i++) |
| 71 |
{ |
| 72 |
uint32_t *value_ptr = (uint32_t *) key_buffer; |
| 73 |
uint32_t len1 = strlen (fake_names[i]); |
| 74 |
uint32_t hash_value = vtv_string_hash (fake_names[i]); |
| 75 |
void *temp_array[ELEMENTS_PER_MAP]; |
| 76 |
|
| 77 |
while (current_map < (k*NUM_MAPS + i)) |
| 78 |
; |
| 79 |
|
| 80 |
__VLTChangePermission(__VLTP_READ_WRITE); |
| 81 |
|
| 82 |
*value_ptr = len1; |
| 83 |
value_ptr++; |
| 84 |
*value_ptr = hash_value; |
| 85 |
|
| 86 |
memcpy ((char *) key_buffer + KEY_TYPE_FIXED_SIZE, fake_names[i], |
| 87 |
len1); |
| 88 |
|
| 89 |
|
| 90 |
#ifdef VTV_DEBUG |
| 91 |
__VLTRegisterPairDebug ((void **) &maps[i], (char *) key_buffer, 128, |
| 92 |
&fake_vts[curr_fake_vt], "", ""); |
| 93 |
#else |
| 94 |
__VLTRegisterPair ((void **) &maps[i], (char *) key_buffer, 128, |
| 95 |
&fake_vts[curr_fake_vt]); |
| 96 |
#endif |
| 97 |
for (int j = 0; j < ELEMENTS_PER_MAP; j++) |
| 98 |
{ |
| 99 |
temp_array[j] = &fake_vts[curr_fake_vt]; |
| 100 |
curr_fake_vt++; |
| 101 |
} |
| 102 |
|
| 103 |
#ifdef VTV_DEBUG |
| 104 |
__VLTRegisterSetDebug ((void **) &maps[i], (char *) key_buffer, 128, 100, |
| 105 |
(void **) &temp_array); |
| 106 |
#else |
| 107 |
__VLTRegisterSet ((void **) &maps[i], (char *) key_buffer, 128, 100, |
| 108 |
(void **) &temp_array); |
| 109 |
#endif |
| 110 |
__VLTChangePermission(__VLTP_READ_ONLY); |
| 111 |
|
| 112 |
int old_value; |
| 113 |
do { |
| 114 |
old_value = threads_completed_it; |
| 115 |
} while (!__sync_bool_compare_and_swap(&threads_completed_it, old_value, old_value + 1)); |
| 116 |
|
| 117 |
if (old_value == (NUM_THREADS - 1)) // Only one thread will do this. |
| 118 |
{ |
| 119 |
threads_completed_it = 0; |
| 120 |
printf("%c%d", 13, current_map + 1); |
| 121 |
fflush(stdout); |
| 122 |
current_map++; |
| 123 |
} |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
return NULL; |
| 128 |
} |
| 129 |
|
| 130 |
|
| 131 |
int main() |
| 132 |
{ |
| 133 |
pthread_t thread_ids[NUM_THREADS]; |
| 134 |
|
| 135 |
generate_names (); |
| 136 |
|
| 137 |
for (int t = 0; t < NUM_THREADS; t++ ) |
| 138 |
if (pthread_create(&thread_ids[t], NULL, do_register_pairs, NULL) != 0) |
| 139 |
{ |
| 140 |
printf("failed pthread_create\n"); |
| 141 |
exit(1); |
| 142 |
} |
| 143 |
|
| 144 |
current_map = 0; // start the work on the other threads |
| 145 |
|
| 146 |
for (int t = 0; t < NUM_THREADS; t++) |
| 147 |
if (pthread_join(thread_ids[t], NULL) != 0) |
| 148 |
{ |
| 149 |
printf("failed pthread_join\n"); |
| 150 |
exit(2); |
| 151 |
} |
| 152 |
|
| 153 |
printf("\n"); |
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
return 0; |
| 158 |
} |