Debian Codegraph @

crt/aws-checksums/bin/benchmark/main.c

raw symbols SCIP
1 /**
2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 * SPDX-License-Identifier: Apache-2.0.
4 */
5
6 #include <aws/checksums/crc.h>
7 #include <aws/checksums/private/crc64_priv.h>
8 #include <aws/checksums/private/crc32_priv.h>
9
10 #include <aws/common/allocator.h>
11 #include <aws/common/byte_buf.h>
12 #include <aws/common/clock.h>
13 #include <aws/common/cpuid.h>
14 #include <aws/common/device_random.h>
15
16 #include <inttypes.h>
17
18 struct aws_allocator_types {
19 struct aws_allocator *allocator;
20 const char *name;
21 };
22
23 struct checksum_profile_run {
24 void (*profile_run)(struct aws_byte_cursor checksum_this);
25 const char *name;
26 };
27
28 static void s_runcrc32_sw(struct aws_byte_cursor checksum_this) {
29 uint32_t crc = aws_checksums_crc32_sw(checksum_this.ptr, (int)checksum_this.len, 0);
30 (void)crc;
31 }
32
33 static void s_runcrc32(struct aws_byte_cursor checksum_this) {
34 uint32_t crc = aws_checksums_crc32(checksum_this.ptr, (int)checksum_this.len, 0);
35 (void)crc;
36 }
37
38 static void s_runcrc32c_sw(struct aws_byte_cursor checksum_this) {
39 uint32_t crc = aws_checksums_crc32c_sw(checksum_this.ptr, (int)checksum_this.len, 0);
40 (void)crc;
41 }
42
43 static void s_runcrc32c(struct aws_byte_cursor checksum_this) {
44 uint32_t crc = aws_checksums_crc32c(checksum_this.ptr, (int)checksum_this.len, 0);
45 (void)crc;
46 }
47
48 static void s_runcrc64_sw(struct aws_byte_cursor checksum_this) {
49 uint64_t crc = aws_checksums_crc64nvme_sw(checksum_this.ptr, (int)checksum_this.len, 0);
50 (void)crc;
51 }
52
53 static void s_runcrc64(struct aws_byte_cursor checksum_this) {
54 uint64_t crc = aws_checksums_crc64nvme(checksum_this.ptr, (int)checksum_this.len, 0);
55 (void)crc;
56 }
57
58 static void s_runcrc64_multi(struct aws_byte_cursor checksum_this) {
59 if (checksum_this.len <= 8 * 1024) {
60 uint64_t crc = aws_checksums_crc64nvme(checksum_this.ptr, (int)checksum_this.len, 0);
61 (void)crc;
62 } else {
63 uint64_t crc = 0;
64 for (size_t offset = 0; offset < checksum_this.len; offset += 8 * 1024) {
65 // Calculate size of this chunk (handles last chunk if not full 8KB)
66 size_t chunk_size = ((offset + 8 * 1024) > checksum_this.len) ?
67 (checksum_this.len - offset) : 8 * 1024;
68
69 crc = aws_checksums_crc64nvme(checksum_this.ptr + offset, (int)chunk_size, crc);
70 }
71 (void)crc;
72 }
73 }
74
75 #define KB_TO_BYTES(kb) ((kb) * 1024)
76 #define MB_TO_BYTES(mb) ((mb) * 1024 * 1024)
77 #define GB_TO_BYTES(gb) ((gb) * 1024 * 1024 * 1024ULL)
78
79 int main(void) {
80
81 fprintf(stdout, "hw features for this run:\n");
82 fprintf(stdout, "clmul: %s\n", aws_cpu_has_feature(AWS_CPU_FEATURE_CLMUL) ? "true" : "false");
83 fprintf(stdout, "sse4.1: %s\n", aws_cpu_has_feature(AWS_CPU_FEATURE_SSE_4_1) ? "true" : "false");
84 fprintf(stdout, "sse4.2: %s\n", aws_cpu_has_feature(AWS_CPU_FEATURE_SSE_4_2) ? "true" : "false");
85 fprintf(stdout, "avx2: %s\n", aws_cpu_has_feature(AWS_CPU_FEATURE_AVX2) ? "true" : "false");
86 fprintf(stdout, "avx512: %s\n", aws_cpu_has_feature(AWS_CPU_FEATURE_AVX512) ? "true" : "false");
87 fprintf(stdout, "arm crc: %s\n", aws_cpu_has_feature(AWS_CPU_FEATURE_ARM_CRC) ? "true" : "false");
88 fprintf(stdout, "bmi2: %s\n", aws_cpu_has_feature(AWS_CPU_FEATURE_BMI2) ? "true" : "false");
89 fprintf(stdout, "vpclmul: %s\n", aws_cpu_has_feature(AWS_CPU_FEATURE_VPCLMULQDQ) ? "true" : "false");
90 fprintf(stdout, "arm pmull: %s\n", aws_cpu_has_feature(AWS_CPU_FEATURE_ARM_PMULL) ? "true" : "false");
91 fprintf(stdout, "arm crypto: %s\n\n", aws_cpu_has_feature(AWS_CPU_FEATURE_ARM_CRYPTO) ? "true" : "false");
92
93 struct aws_allocator_types allocators[2];
94 allocators[0].allocator = aws_default_allocator();
95 allocators[0].name = "Default runtime allocator";
96 allocators[1].allocator = aws_aligned_allocator();
97 allocators[1].name = "Aligned allocator";
98
99 struct checksum_profile_run profile_runs[] = {
100 {.profile_run = s_runcrc32_sw, .name = "crc32 C only"},
101 {.profile_run = s_runcrc32, .name = "crc32 with hw optimizations"},
102 {.profile_run = s_runcrc32c_sw, .name = "crc32c C only"},
103 {.profile_run = s_runcrc32c, .name = "crc32c with hw optimizations"},
104 {.profile_run = s_runcrc64_sw, .name = "crc64nvme C only"},
105 {.profile_run = s_runcrc64, .name = "crc64nvme with hw optimizations"},
106 {.profile_run = s_runcrc64_multi, .name = "crc64nvme with hw optimizations(multi)"},
107 };
108
109 const size_t allocators_array_size = AWS_ARRAY_SIZE(allocators);
110 const size_t profile_runs_size = AWS_ARRAY_SIZE(profile_runs);
111
112 for (size_t i = 0; i < profile_runs_size; ++i) {
113 fprintf(stdout, "--------Profile %s---------\n", profile_runs[i].name);
114
115 for (size_t j = 0; j < allocators_array_size; ++j) {
116 fprintf(stdout, "%s\n\n", allocators[j].name);
117
118 struct aws_allocator *allocator = allocators[j].allocator;
119
120 // get buffer sizes large enough that all the simd code paths get hit hard, but
121 // also measure the smaller buffer paths since they often can't be optimized as thoroughly.
122 size_t buffer_sizes[] = {8, 16, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384,
123 KB_TO_BYTES(32), KB_TO_BYTES(64), KB_TO_BYTES(256), MB_TO_BYTES(1), MB_TO_BYTES(10), MB_TO_BYTES(100), GB_TO_BYTES(1)};
124 size_t buffer_sizes_len = AWS_ARRAY_SIZE(buffer_sizes);
125
126 // warm it up to factor out the cpuid checks:
127 struct aws_byte_cursor warmup_cur = aws_byte_cursor_from_array(buffer_sizes, buffer_sizes_len);
128 profile_runs[i].profile_run(warmup_cur);
129
130 for (size_t k = 0; k < buffer_sizes_len; ++k) {
131 struct aws_byte_buf x_bytes;
132 aws_byte_buf_init(&x_bytes, allocator, buffer_sizes[k]);
133 aws_device_random_buffer(&x_bytes);
134 uint64_t start_time = 0;
135 aws_high_res_clock_get_ticks(&start_time);
136 profile_runs[i].profile_run(aws_byte_cursor_from_buf(&x_bytes));
137 uint64_t end_time = 0;
138 aws_high_res_clock_get_ticks(&end_time);
139 fprintf(
140 stdout,
141 "buffer size %zu (bytes), latency: %" PRIu64 " ns throughput: %f GiB/s\n",
142 buffer_sizes[k],
143 end_time - start_time,
144 (buffer_sizes[k] * 1000000000.0 /* ns -> sec factor */ / GB_TO_BYTES(1)) / (end_time - start_time));
145 aws_byte_buf_clean_up(&x_bytes);
146 }
147 fprintf(stdout, "\n");
148 }
149 }
150 return 0;
151 }