#include #include #include #include #include #include #define TEST_SIZE (32768-4096) #define TEST_ENTRIES (TEST_SIZE / sizeof(void *)) void *cache_test[TEST_ENTRIES] __attribute__((aligned(32768))); static void init_cache_test(void) { unsigned long last_p, next_p, i; memset(cache_test, 0, sizeof cache_test); last_p = 0; for (i = 0; i < TEST_ENTRIES - 1; i++) { do { next_p = random() % TEST_ENTRIES; } while (cache_test[next_p] != 0 || next_p == last_p); cache_test[last_p] = &cache_test[next_p]; last_p = next_p; } for (i = 0; i < TEST_ENTRIES; i += 8) { //asm volatile ("clwb %0" :: "m"(cache_test[i]): "memory"); } //asm volatile ("sfence"); } static void walk_cache(void) { void *p = &cache_test[0]; while (p) { p = *(void **)p; } } static unsigned long long rdtsc(void) { unsigned a, d; asm volatile("rdtsc" : "=a"(a), "=d"(d) ::); return a + ((unsigned long long)d << 32); } static char buffer[65536] __attribute__((aligned(4096))); int main(__attribute__((unused)) int argc, __attribute__((unused)) char *argv[]) { unsigned long long t1, t2, t3, t4; unsigned long long t1sum, t3sum; unsigned long loop; int h; memset(buffer, 0x11, sizeof buffer); h = open("/dev/mapper/wc", O_RDWR | O_DSYNC); if (h == -1) perror("open"), exit(1); init_cache_test(); t1sum = t3sum = 0; for (loop = 0; loop < 50000; loop++) { walk_cache(); t1 = rdtsc(); walk_cache(); t2 = rdtsc(); if (write(h, buffer, sizeof buffer) != sizeof buffer) perror("write"), exit(1); t3 = rdtsc(); walk_cache(); t4 = rdtsc(); t1sum += t2 - t1; t3sum += t4 - t3; } fprintf(stderr, "%llu - %llu\n", t1sum / loop, t3sum / loop); return 0; }