Debian Codegraph @

crt/aws-c-cal/bin/sha256_profile/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/cal/cal.h>
7 #include <aws/cal/hash.h>
8
9 #include <aws/common/clock.h>
10 #include <aws/common/device_random.h>
11
12 #include <inttypes.h>
13
14 static void s_profile_streaming_hash_at_chunk_size(
15 struct aws_allocator *allocator,
16 struct aws_byte_cursor to_hash,
17 size_t chunk_size,
18 size_t alignment) {
19 struct aws_hash *hash_impl = aws_sha256_new(allocator);
20 AWS_FATAL_ASSERT(hash_impl);
21
22 struct aws_byte_buf output_buf;
23 AWS_FATAL_ASSERT(
24 !aws_byte_buf_init(&output_buf, allocator, AWS_SHA256_LEN) && "allocation of output buffer failed!");
25
26 struct aws_byte_cursor to_hash_seeked = to_hash;
27
28 uint64_t start = 0;
29 AWS_FATAL_ASSERT(!aws_high_res_clock_get_ticks(&start) && "clock get ticks failed.");
30
31 if (alignment) {
32 size_t alignment_miss = (uintptr_t)to_hash_seeked.ptr % alignment;
33 struct aws_byte_cursor unaligned_chunk = aws_byte_cursor_advance(&to_hash_seeked, alignment_miss);
34 AWS_FATAL_ASSERT(!aws_hash_update(hash_impl, &unaligned_chunk) && "hash compute of unaligned chunk failed");
35 }
36
37 while (to_hash_seeked.len) {
38 size_t remaining = chunk_size > to_hash_seeked.len ? to_hash_seeked.len : chunk_size;
39
40 struct aws_byte_cursor chunk_to_process = aws_byte_cursor_advance(&to_hash_seeked, remaining);
41 AWS_FATAL_ASSERT(!aws_hash_update(hash_impl, &chunk_to_process) && "hash compute of chunk failed");
42 }
43
44 AWS_FATAL_ASSERT(!aws_hash_finalize(hash_impl, &output_buf, 0) && "hash finalize failed");
45 uint64_t end = 0;
46 AWS_FATAL_ASSERT(!aws_high_res_clock_get_ticks(&end) && "clock get ticks failed");
47 fprintf(stdout, "SHA256 streaming computation took %" PRIu64 "ns\n", end - start);
48
49 aws_byte_buf_clean_up(&output_buf);
50 aws_hash_destroy(hash_impl);
51 }
52
53 static void s_profile_oneshot_hash(struct aws_allocator *allocator, struct aws_byte_cursor to_hash) {
54 struct aws_byte_buf output_buf;
55
56 AWS_FATAL_ASSERT(
57 !aws_byte_buf_init(&output_buf, allocator, AWS_SHA256_LEN) && "allocation of output buffer failed!");
58
59 uint64_t start = 0;
60 AWS_FATAL_ASSERT(!aws_high_res_clock_get_ticks(&start) && "clock get ticks failed.");
61 AWS_FATAL_ASSERT(!aws_sha256_compute(allocator, &to_hash, &output_buf, 0) && "Hash computation failed");
62 uint64_t end = 0;
63 AWS_FATAL_ASSERT(!aws_high_res_clock_get_ticks(&end) && "clock get ticks failed");
64 fprintf(stdout, "SHA256 oneshot computation took %" PRIu64 "ns\n", end - start);
65 aws_byte_buf_clean_up(&output_buf);
66 }
67
68 static void s_run_profiles(struct aws_allocator *allocator, size_t to_hash_size, const char *profile_name) {
69 fprintf(stdout, "********************* SHA256 Profile %s ************************************\n\n", profile_name);
70
71 struct aws_byte_buf to_hash;
72 AWS_FATAL_ASSERT(!aws_byte_buf_init(&to_hash, allocator, to_hash_size) && "failed to allocate buffer for hashing");
73 AWS_FATAL_ASSERT(!aws_device_random_buffer(&to_hash) && "reading random data failed");
74 struct aws_byte_cursor to_hash_cur = aws_byte_cursor_from_buf(&to_hash);
75
76 fprintf(stdout, "********************* Chunked/Alignment Runs *********************************\n\n");
77 fprintf(stdout, "****** 128 byte chunks ******\n\n");
78 fprintf(stdout, "8-byte alignment:\n");
79 s_profile_streaming_hash_at_chunk_size(allocator, to_hash_cur, 128, 8);
80 fprintf(stdout, "16-byte alignment:\n");
81 s_profile_streaming_hash_at_chunk_size(allocator, to_hash_cur, 128, 16);
82 fprintf(stdout, "64-byte alignment:\n");
83 s_profile_streaming_hash_at_chunk_size(allocator, to_hash_cur, 128, 64);
84 fprintf(stdout, "128-byte alignment:\n");
85 s_profile_streaming_hash_at_chunk_size(allocator, to_hash_cur, 128, 128);
86 fprintf(stdout, "\n****** 256 byte chunks ******\n\n");
87 fprintf(stdout, "8-byte alignment:\n");
88 s_profile_streaming_hash_at_chunk_size(allocator, to_hash_cur, 256, 8);
89 fprintf(stdout, "16-byte alignment:\n");
90 s_profile_streaming_hash_at_chunk_size(allocator, to_hash_cur, 256, 16);
91 fprintf(stdout, "64-byte alignment:\n");
92 s_profile_streaming_hash_at_chunk_size(allocator, to_hash_cur, 256, 64);
93 fprintf(stdout, "128-byte alignment:\n");
94 s_profile_streaming_hash_at_chunk_size(allocator, to_hash_cur, 256, 128);
95
96 fprintf(stdout, "\n******* 512 byte chunks *****\n\n");
97 fprintf(stdout, "8-byte alignment:\n");
98 s_profile_streaming_hash_at_chunk_size(allocator, to_hash_cur, 512, 8);
99 fprintf(stdout, "16-byte alignment:\n");
100 s_profile_streaming_hash_at_chunk_size(allocator, to_hash_cur, 512, 16);
101 fprintf(stdout, "64-byte alignment:\n");
102 s_profile_streaming_hash_at_chunk_size(allocator, to_hash_cur, 512, 64);
103 fprintf(stdout, "128-byte alignment:\n");
104 s_profile_streaming_hash_at_chunk_size(allocator, to_hash_cur, 512, 128);
105
106 fprintf(stdout, "\n********************** Oneshot Run *******************************************\n\n");
107 s_profile_oneshot_hash(allocator, to_hash_cur);
108 fprintf(stdout, "\n\n");
109 aws_byte_buf_clean_up(&to_hash);
110 }
111
112 int main(void) {
113 struct aws_allocator *allocator = aws_default_allocator();
114 aws_cal_library_init(allocator);
115
116 struct aws_hash *hash_impl = aws_sha256_new(allocator);
117 fprintf(stdout, "Starting profile run for Sha256 using implementation %s\n\n", hash_impl->vtable->provider);
118 s_run_profiles(allocator, 1024, "1 KB");
119 s_run_profiles(allocator, 1024 * 64, "64 KB");
120 s_run_profiles(allocator, 1024 * 128, "128 KB");
121 s_run_profiles(allocator, 1024 * 512, "512 KB");
122
123 aws_hash_destroy(hash_impl);
124 aws_cal_library_clean_up();
125 return 0;
126 }