|
| 1 | +From fe4108f726a0e30d429f031a6006f030c5eda113 Mon Sep 17 00:00:00 2001 |
| 2 | +From: ouyangxiangzhen < [email protected]> |
| 3 | +Date: Thu, 6 Jun 2024 19:53:37 +0800 |
| 4 | +Subject: [PATCH] test-tlb: port for NuttX |
| 5 | + |
| 6 | +VELAPLATFO-25227 |
| 7 | + |
| 8 | +This commit ports the test-tlb test program to the NuttX. Additionally, it implements dynamic CPU frequency calculation. |
| 9 | + |
| 10 | +Change-Id: If1c15f36742d7ee5a7d13147b41b3372047dad08 |
| 11 | +Signed-off-by: ouyangxiangzhen < [email protected]> |
| 12 | +--- |
| 13 | + test-tlb.c | 68 ++++++++++++++++++++++++++++++++++++++++++++++++++---- |
| 14 | + 1 file changed, 63 insertions(+), 5 deletions(-) |
| 15 | + |
| 16 | +diff --git a/test-tlb.c b/test-tlb.c |
| 17 | +index 5c8aaa5..767794a 100644 |
| 18 | +--- a/test-tlb.c |
| 19 | ++++ b/test-tlb.c |
| 20 | +@@ -14,13 +14,58 @@ |
| 21 | + #include <math.h> |
| 22 | + #include <time.h> |
| 23 | + |
| 24 | +-#define PAGE_SIZE 4096 |
| 25 | +- |
| 26 | +-#define FREQ 3.9 |
| 27 | + |
| 28 | + static int test_hugepage = 0; |
| 29 | + static int random_list = 0; |
| 30 | + |
| 31 | ++/* Fix definition conflict */ |
| 32 | ++#ifndef PAGE_SIZE |
| 33 | ++#define PAGE_SIZE 4096 |
| 34 | ++#endif |
| 35 | ++ |
| 36 | ++/* dynamic calculate freq */ |
| 37 | ++#define MEASURE_GAP 500000 |
| 38 | ++ |
| 39 | ++#ifdef __x86_64__ |
| 40 | ++static inline unsigned long rdcnt_relax(void) |
| 41 | ++{ |
| 42 | ++ unsigned int lo,hi; |
| 43 | ++ asm volatile ("rdtsc" : "=a" (lo), "=d" (hi)); |
| 44 | ++ return ((unsigned long)hi << 32) | (unsigned long)lo; |
| 45 | ++} |
| 46 | ++#elif defined(__aarch64__) |
| 47 | ++static inline unsigned long rdcnt_relax(void) |
| 48 | ++{ |
| 49 | ++ unsigned long time; |
| 50 | ++ asm volatile("mrs %0, cntvct_el0" : "=r"(time)); |
| 51 | ++ return time; |
| 52 | ++} |
| 53 | ++#else |
| 54 | ++#error "Unsupported architecture for dynamic frequencies measuring" |
| 55 | ++#endif |
| 56 | ++ |
| 57 | ++static inline unsigned long rdcnt(void) |
| 58 | ++{ |
| 59 | ++ return rdcnt_relax(); |
| 60 | ++} |
| 61 | ++ |
| 62 | ++static unsigned long measure_freq(void) |
| 63 | ++{ |
| 64 | ++ unsigned long cycles_per_useconds; |
| 65 | ++ unsigned long start; |
| 66 | ++ unsigned long end; |
| 67 | ++ |
| 68 | ++ start = rdcnt(); |
| 69 | ++ usleep(MEASURE_GAP); |
| 70 | ++ end = rdcnt(); |
| 71 | ++ |
| 72 | ++ cycles_per_useconds = (end - start) / MEASURE_GAP; |
| 73 | ++ |
| 74 | ++ printf("cycles_per_microseconds: %lu\n", cycles_per_useconds); |
| 75 | ++ |
| 76 | ++ return cycles_per_useconds; |
| 77 | ++} |
| 78 | ++ |
| 79 | + static void die(const char *fmt, ...) |
| 80 | + { |
| 81 | + va_list argp; |
| 82 | +@@ -69,7 +114,7 @@ static unsigned long warmup(void *map) |
| 83 | + |
| 84 | + static double do_test(void *map) |
| 85 | + { |
| 86 | +- unsigned long count = 0, offset = 0, usec; |
| 87 | ++ unsigned long volatile count = 0, offset = 0, usec; |
| 88 | + struct timeval start, end; |
| 89 | + struct itimerval itval = { |
| 90 | + .it_interval = { 0, 0 }, |
| 91 | +@@ -190,7 +235,14 @@ static void *create_map(void *map, unsigned long size, unsigned long stride) |
| 92 | + if (map) { |
| 93 | + if (test_hugepage) |
| 94 | + return map; |
| 95 | ++ |
| 96 | ++#ifdef __NuttX__ |
| 97 | ++ if (munmap(map, size) != 0) |
| 98 | ++ die("munmap failed\n"); |
| 99 | ++ map = NULL; |
| 100 | ++#else |
| 101 | + flags |= MAP_FIXED; |
| 102 | ++#endif |
| 103 | + } |
| 104 | + |
| 105 | + mapsize = size; |
| 106 | +@@ -237,9 +289,14 @@ int main(int argc, char **argv) |
| 107 | + const char *arg; |
| 108 | + void *map; |
| 109 | + double cycles; |
| 110 | ++ unsigned long freq; |
| 111 | + |
| 112 | + srandom(time(NULL)); |
| 113 | + |
| 114 | ++ /* Fix segmentation fault when execution without any arguments in NuttX */ |
| 115 | ++ if (argc < 3) |
| 116 | ++ die("bad arguments: test-tlb [-H] <size> <stride>"); |
| 117 | ++ |
| 118 | + while ((arg = argv[1]) != NULL) { |
| 119 | + if (*arg != '-') |
| 120 | + break; |
| 121 | +@@ -261,6 +318,7 @@ int main(int argc, char **argv) |
| 122 | + argv++; |
| 123 | + } |
| 124 | + |
| 125 | ++ freq = measure_freq(); |
| 126 | + size = get_num(argv[1]); |
| 127 | + stride = get_num(argv[2]); |
| 128 | + if (stride < 4 || size < stride) |
| 129 | +@@ -281,6 +339,6 @@ int main(int argc, char **argv) |
| 130 | + } |
| 131 | + |
| 132 | + printf("%6.2fns (~%.1f cycles)\n", |
| 133 | +- cycles, cycles*FREQ); |
| 134 | ++ cycles, cycles * freq); |
| 135 | + return 0; |
| 136 | + } |
| 137 | +-- |
| 138 | +2.34.1 |
| 139 | + |
0 commit comments