|
| 1 | +#include <unity.h> |
| 2 | + |
| 3 | +#include "cpu.h" |
| 4 | +#include "options.h" |
| 5 | + |
| 6 | +struct cpu_info cpu_info = { 0 }; |
| 7 | + |
| 8 | + |
| 9 | +static void set_cpu_info(int total_logical_cpus, int l2_cache, int l3_cache) { |
| 10 | + cpu_info.total_cores = total_logical_cpus; |
| 11 | + cpu_info.total_logical_cpus = total_logical_cpus; |
| 12 | + cpu_info.l2_cache = l2_cache; |
| 13 | + cpu_info.l3_cache = l3_cache; |
| 14 | +} |
| 15 | + |
| 16 | + |
| 17 | +void test_autoconf_should_GetOptimalThreadsCounti7(void) { |
| 18 | + set_cpu_info(8, 1024, 8192); // 4C/8T 8 MB (Generic i7 CPU) |
| 19 | + |
| 20 | + TEST_ASSERT_EQUAL_INT(4, get_optimal_threads_count(ALGO_CRYPTONIGHT, false, 100)); |
| 21 | + TEST_ASSERT_EQUAL_INT(2, get_optimal_threads_count(ALGO_CRYPTONIGHT, true, 100)); |
| 22 | + |
| 23 | + TEST_ASSERT_EQUAL_INT(8, get_optimal_threads_count(ALGO_CRYPTONIGHT_LITE, false, 100)); |
| 24 | + TEST_ASSERT_EQUAL_INT(4, get_optimal_threads_count(ALGO_CRYPTONIGHT_LITE, true, 100)); |
| 25 | + |
| 26 | + TEST_ASSERT_EQUAL_INT(6, get_optimal_threads_count(ALGO_CRYPTONIGHT_LITE, false, 75)); |
| 27 | + TEST_ASSERT_EQUAL_INT(5, get_optimal_threads_count(ALGO_CRYPTONIGHT_LITE, false, 60)); |
| 28 | + TEST_ASSERT_EQUAL_INT(4, get_optimal_threads_count(ALGO_CRYPTONIGHT_LITE, false, 50)); |
| 29 | + TEST_ASSERT_EQUAL_INT(3, get_optimal_threads_count(ALGO_CRYPTONIGHT_LITE, false, 35)); |
| 30 | + TEST_ASSERT_EQUAL_INT(2, get_optimal_threads_count(ALGO_CRYPTONIGHT_LITE, false, 20)); |
| 31 | + TEST_ASSERT_EQUAL_INT(1, get_optimal_threads_count(ALGO_CRYPTONIGHT_LITE, false, 5)); |
| 32 | +} |
| 33 | + |
| 34 | + |
| 35 | +void test_autoconf_should_GetOptimalThreadsCounti5(void) { |
| 36 | + set_cpu_info(4, 1024, 6144); // 2C/4T 6 MB (Generic i5 CPU) |
| 37 | + |
| 38 | + TEST_ASSERT_EQUAL_INT(3, get_optimal_threads_count(ALGO_CRYPTONIGHT, false, 100)); |
| 39 | + TEST_ASSERT_EQUAL_INT(1, get_optimal_threads_count(ALGO_CRYPTONIGHT, true, 100)); |
| 40 | + |
| 41 | + TEST_ASSERT_EQUAL_INT(3, get_optimal_threads_count(ALGO_CRYPTONIGHT, false, 75)); |
| 42 | + TEST_ASSERT_EQUAL_INT(1, get_optimal_threads_count(ALGO_CRYPTONIGHT, true, 75)); |
| 43 | + |
| 44 | + TEST_ASSERT_EQUAL_INT(4, get_optimal_threads_count(ALGO_CRYPTONIGHT_LITE, false, 100)); |
| 45 | + TEST_ASSERT_EQUAL_INT(3, get_optimal_threads_count(ALGO_CRYPTONIGHT_LITE, true, 100)); |
| 46 | + |
| 47 | + TEST_ASSERT_EQUAL_INT(3, get_optimal_threads_count(ALGO_CRYPTONIGHT_LITE, false, 75)); |
| 48 | + TEST_ASSERT_EQUAL_INT(3, get_optimal_threads_count(ALGO_CRYPTONIGHT_LITE, true, 75)); |
| 49 | +} |
| 50 | + |
| 51 | + |
| 52 | +void test_autoconf_should_GetOptimalThreadsCounti3(void) { |
| 53 | + set_cpu_info(4, 512, 3072); // 2C/4T 3 MB (Generic i3 CPU) |
| 54 | + |
| 55 | + TEST_ASSERT_EQUAL_INT(1, get_optimal_threads_count(ALGO_CRYPTONIGHT, false, 100)); |
| 56 | + TEST_ASSERT_EQUAL_INT(1, get_optimal_threads_count(ALGO_CRYPTONIGHT, true, 100)); |
| 57 | + |
| 58 | + TEST_ASSERT_EQUAL_INT(1, get_optimal_threads_count(ALGO_CRYPTONIGHT, false, 75)); |
| 59 | + TEST_ASSERT_EQUAL_INT(1, get_optimal_threads_count(ALGO_CRYPTONIGHT, true, 75)); |
| 60 | + |
| 61 | + TEST_ASSERT_EQUAL_INT(3, get_optimal_threads_count(ALGO_CRYPTONIGHT_LITE, false, 100)); |
| 62 | + TEST_ASSERT_EQUAL_INT(1, get_optimal_threads_count(ALGO_CRYPTONIGHT_LITE, true, 100)); |
| 63 | + |
| 64 | + TEST_ASSERT_EQUAL_INT(3, get_optimal_threads_count(ALGO_CRYPTONIGHT_LITE, false, 75)); |
| 65 | + TEST_ASSERT_EQUAL_INT(1, get_optimal_threads_count(ALGO_CRYPTONIGHT_LITE, true, 75)); |
| 66 | +} |
| 67 | + |
| 68 | + |
| 69 | +void test_autoconf_should_GetOptimalThreadsCountR7(void) { |
| 70 | + set_cpu_info(16, 4096, 16384); // 8C/16T 16 MB (AMD Ryzen 7) |
| 71 | + |
| 72 | + TEST_ASSERT_EQUAL_INT(8, get_optimal_threads_count(ALGO_CRYPTONIGHT, false, 100)); |
| 73 | + TEST_ASSERT_EQUAL_INT(4, get_optimal_threads_count(ALGO_CRYPTONIGHT, true, 100)); |
| 74 | + |
| 75 | + TEST_ASSERT_EQUAL_INT(8, get_optimal_threads_count(ALGO_CRYPTONIGHT, false, 75)); |
| 76 | + TEST_ASSERT_EQUAL_INT(4, get_optimal_threads_count(ALGO_CRYPTONIGHT, true, 75)); |
| 77 | + |
| 78 | + TEST_ASSERT_EQUAL_INT(16, get_optimal_threads_count(ALGO_CRYPTONIGHT_LITE, false, 100)); |
| 79 | + TEST_ASSERT_EQUAL_INT(8, get_optimal_threads_count(ALGO_CRYPTONIGHT_LITE, true, 100)); |
| 80 | + |
| 81 | + TEST_ASSERT_EQUAL_INT(12, get_optimal_threads_count(ALGO_CRYPTONIGHT_LITE, false, 75)); |
| 82 | + TEST_ASSERT_EQUAL_INT(8, get_optimal_threads_count(ALGO_CRYPTONIGHT_LITE, true, 75)); |
| 83 | +} |
| 84 | + |
| 85 | + |
| 86 | +void test_autoconf_should_GetOptimalThreadsCountTwoE5620(void) { |
| 87 | + set_cpu_info(16, 2048, 24576); // 8C/16T 24 MB (Two E5620) |
| 88 | + |
| 89 | + TEST_ASSERT_EQUAL_INT(12, get_optimal_threads_count(ALGO_CRYPTONIGHT, false, 100)); |
| 90 | + TEST_ASSERT_EQUAL_INT(6, get_optimal_threads_count(ALGO_CRYPTONIGHT, true, 100)); |
| 91 | + |
| 92 | + TEST_ASSERT_EQUAL_INT(12, get_optimal_threads_count(ALGO_CRYPTONIGHT, false, 75)); |
| 93 | + TEST_ASSERT_EQUAL_INT(6, get_optimal_threads_count(ALGO_CRYPTONIGHT, true, 75)); |
| 94 | + |
| 95 | + TEST_ASSERT_EQUAL_INT(16, get_optimal_threads_count(ALGO_CRYPTONIGHT_LITE, false, 100)); |
| 96 | + TEST_ASSERT_EQUAL_INT(12, get_optimal_threads_count(ALGO_CRYPTONIGHT_LITE, true, 100)); |
| 97 | + |
| 98 | + TEST_ASSERT_EQUAL_INT(12, get_optimal_threads_count(ALGO_CRYPTONIGHT_LITE, false, 75)); |
| 99 | + TEST_ASSERT_EQUAL_INT(12, get_optimal_threads_count(ALGO_CRYPTONIGHT_LITE, true, 75)); |
| 100 | +} |
| 101 | + |
| 102 | + |
| 103 | +void test_autoconf_should_GetOptimalThreadsCountVCPU(void) { |
| 104 | + set_cpu_info(1, 1024, 15360); // 1C/1T 15 MB (Single core Virtual CPU) |
| 105 | + |
| 106 | + TEST_ASSERT_EQUAL_INT(1, get_optimal_threads_count(ALGO_CRYPTONIGHT, false, 100)); |
| 107 | + TEST_ASSERT_EQUAL_INT(1, get_optimal_threads_count(ALGO_CRYPTONIGHT, true, 100)); |
| 108 | + |
| 109 | + TEST_ASSERT_EQUAL_INT(1, get_optimal_threads_count(ALGO_CRYPTONIGHT, false, 75)); |
| 110 | + TEST_ASSERT_EQUAL_INT(1, get_optimal_threads_count(ALGO_CRYPTONIGHT, true, 75)); |
| 111 | + |
| 112 | + TEST_ASSERT_EQUAL_INT(1, get_optimal_threads_count(ALGO_CRYPTONIGHT_LITE, false, 100)); |
| 113 | + TEST_ASSERT_EQUAL_INT(1, get_optimal_threads_count(ALGO_CRYPTONIGHT_LITE, true, 100)); |
| 114 | + |
| 115 | + TEST_ASSERT_EQUAL_INT(1, get_optimal_threads_count(ALGO_CRYPTONIGHT_LITE, false, 75)); |
| 116 | + TEST_ASSERT_EQUAL_INT(1, get_optimal_threads_count(ALGO_CRYPTONIGHT_LITE, true, 75)); |
| 117 | +} |
| 118 | + |
| 119 | + |
| 120 | +void test_autoconf_should_GetOptimalThreadsCountNoL3(void) { |
| 121 | + set_cpu_info(8, 8192, 0); // 4C/8T (Multi core Virtual CPU without L3 cache) |
| 122 | + |
| 123 | + TEST_ASSERT_EQUAL_INT(4, get_optimal_threads_count(ALGO_CRYPTONIGHT, false, 100)); |
| 124 | + TEST_ASSERT_EQUAL_INT(2, get_optimal_threads_count(ALGO_CRYPTONIGHT, true, 100)); |
| 125 | + |
| 126 | + TEST_ASSERT_EQUAL_INT(8, get_optimal_threads_count(ALGO_CRYPTONIGHT_LITE, false, 100)); |
| 127 | + TEST_ASSERT_EQUAL_INT(4, get_optimal_threads_count(ALGO_CRYPTONIGHT_LITE, true, 100)); |
| 128 | + |
| 129 | + TEST_ASSERT_EQUAL_INT(6, get_optimal_threads_count(ALGO_CRYPTONIGHT_LITE, false, 75)); |
| 130 | + TEST_ASSERT_EQUAL_INT(5, get_optimal_threads_count(ALGO_CRYPTONIGHT_LITE, false, 60)); |
| 131 | + TEST_ASSERT_EQUAL_INT(4, get_optimal_threads_count(ALGO_CRYPTONIGHT_LITE, false, 50)); |
| 132 | + TEST_ASSERT_EQUAL_INT(3, get_optimal_threads_count(ALGO_CRYPTONIGHT_LITE, false, 35)); |
| 133 | + TEST_ASSERT_EQUAL_INT(2, get_optimal_threads_count(ALGO_CRYPTONIGHT_LITE, false, 20)); |
| 134 | + TEST_ASSERT_EQUAL_INT(1, get_optimal_threads_count(ALGO_CRYPTONIGHT_LITE, false, 5)); |
| 135 | +} |
| 136 | + |
| 137 | + |
| 138 | +int main(void) |
| 139 | +{ |
| 140 | + UNITY_BEGIN(); |
| 141 | + |
| 142 | + RUN_TEST(test_autoconf_should_GetOptimalThreadsCounti7); |
| 143 | + RUN_TEST(test_autoconf_should_GetOptimalThreadsCounti5); |
| 144 | + RUN_TEST(test_autoconf_should_GetOptimalThreadsCounti3); |
| 145 | + RUN_TEST(test_autoconf_should_GetOptimalThreadsCountR7); |
| 146 | + RUN_TEST(test_autoconf_should_GetOptimalThreadsCountR7); |
| 147 | + RUN_TEST(test_autoconf_should_GetOptimalThreadsCountTwoE5620); |
| 148 | + RUN_TEST(test_autoconf_should_GetOptimalThreadsCountVCPU); |
| 149 | + RUN_TEST(test_autoconf_should_GetOptimalThreadsCountNoL3); |
| 150 | + |
| 151 | + return UNITY_END(); |
| 152 | +} |
0 commit comments