Debian Codegraph @

third-party/xxhash/tests/bench/benchHash.c

raw
1 /*
2 * Hash benchmark module
3 * Part of the xxHash project
4 * Copyright (C) 2019-2021 Yann Collet
5 *
6 * GPL v2 License
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 *
22 * You can contact the author at:
23 * - xxHash homepage: https://www.xxhash.com
24 * - xxHash source repository: https://github.com/Cyan4973/xxHash
25 */
26
27 /* benchmark hash functions */
28
29 #include <stdlib.h> // malloc
30 #include <assert.h>
31 #include <string.h>
32
33 #include "benchHash.h"
34
35
36 static void initBuffer(void* buffer, size_t size)
37 {
38 const unsigned long long k1 = 11400714785074694791ULL; /* 0b1001111000110111011110011011000110000101111010111100101010000111 */
39 const unsigned long long k2 = 14029467366897019727ULL; /* 0b1100001010110010101011100011110100100111110101001110101101001111 */
40 unsigned long long acc = k2;
41 unsigned char* const p = (unsigned char*)buffer;
42 for (size_t s = 0; s < size; s++) {
43 acc *= k1;
44 p[s] = (unsigned char)(acc >> 56);
45 }
46 }
47
48
49 #define MARGIN_FOR_LATENCY 1024
50 #define START_MASK (MARGIN_FOR_LATENCY-1)
51
52 typedef size_t (*sizeFunction_f)(size_t targetSize);
53
54 /*
55 * bench_hash_internal():
56 * Benchmarks hashfn repeateadly over single input of size `size`
57 * return: nb of hashes per second
58 */
59 static double
60 bench_hash_internal(BMK_benchFn_t hashfn, void* payload,
61 size_t nbBlocks, sizeFunction_f selectSize, size_t size,
62 unsigned total_time_ms, unsigned iter_time_ms)
63 {
64 BMK_timedFnState_shell shell;
65 BMK_timedFnState_t* const txf = BMK_initStatic_timedFnState(&shell, sizeof(shell), total_time_ms, iter_time_ms);
66 assert(txf != NULL);
67
68 size_t const srcSize = (size_t)size;
69 size_t const srcBufferSize = srcSize + MARGIN_FOR_LATENCY;
70 void* const srcBuffer = malloc(srcBufferSize);
71 assert(srcBuffer != NULL);
72 initBuffer(srcBuffer, srcBufferSize);
73 #define FAKE_DSTSIZE 32
74 size_t const dstSize = FAKE_DSTSIZE;
75 char dstBuffer_static[FAKE_DSTSIZE] = {0};
76
77 #define NB_BLOCKS_MAX 1024
78 const void* srcBuffers[NB_BLOCKS_MAX];
79 size_t srcSizes[NB_BLOCKS_MAX];
80 void* dstBuffers[NB_BLOCKS_MAX];
81 size_t dstCapacities[NB_BLOCKS_MAX];
82 assert(nbBlocks < NB_BLOCKS_MAX);
83
84 assert(size > 0);
85 for (size_t n=0; n < nbBlocks; n++) {
86 srcBuffers[n] = srcBuffer;
87 srcSizes[n] = selectSize(size);
88 dstBuffers[n] = dstBuffer_static;
89 dstCapacities[n] = dstSize;
90 }
91
92
93 BMK_benchParams_t params = {
94 .benchFn = hashfn,
95 .benchPayload = payload,
96 .initFn = NULL,
97 .initPayload = NULL,
98 .errorFn = NULL,
99 .blockCount = nbBlocks,
100 .srcBuffers = srcBuffers,
101 .srcSizes = srcSizes,
102 .dstBuffers = dstBuffers,
103 .dstCapacities = dstCapacities,
104 .blockResults = NULL
105 };
106 BMK_runOutcome_t result;
107
108 memset(&result, 0, sizeof(result));
109 while (!BMK_isCompleted_TimedFn(txf)) {
110 result = BMK_benchTimedFn(txf, params);
111 assert(BMK_isSuccessful_runOutcome(result));
112 }
113
114 BMK_runTime_t const runTime = BMK_extract_runTime(result);
115
116 free(srcBuffer);
117 assert(runTime.nanoSecPerRun != 0);
118 return (1000000000U / runTime.nanoSecPerRun) * nbBlocks;
119
120 }
121
122
123 static size_t rand_1_N(size_t N) { return ((size_t)rand() % N) + 1; }
124
125 static size_t identity(size_t s) { return s; }
126
127 static size_t
128 benchLatency(const void* src, size_t srcSize,
129 void* dst, size_t dstCapacity,
130 void* customPayload)
131 {
132 (void)dst; (void)dstCapacity;
133 BMK_benchFn_t benchfn = (BMK_benchFn_t)customPayload;
134 static size_t hash = 0;
135
136 const void* const start = (const char*)src + (hash & START_MASK);
137
138 return hash = benchfn(start, srcSize, dst, dstCapacity, NULL);
139 }
140
141
142
143 #ifndef SIZE_TO_HASH_PER_ROUND
144 # define SIZE_TO_HASH_PER_ROUND 200000
145 #endif
146
147 #ifndef NB_HASH_ROUNDS_MAX
148 # define NB_HASH_ROUNDS_MAX 1000
149 #endif
150
151 double bench_hash(BMK_benchFn_t hashfn,
152 BMK_benchMode benchMode,
153 size_t size, BMK_sizeMode sizeMode,
154 unsigned total_time_ms, unsigned iter_time_ms)
155 {
156 sizeFunction_f const sizef = (sizeMode == BMK_fixedSize) ? identity : rand_1_N;
157 BMK_benchFn_t const benchfn = (benchMode == BMK_throughput) ? hashfn : benchLatency;
158 BMK_benchFn_t const payload = (benchMode == BMK_throughput) ? NULL : hashfn;
159
160 size_t nbBlocks = (SIZE_TO_HASH_PER_ROUND / size) + 1;
161 if (nbBlocks > NB_HASH_ROUNDS_MAX) nbBlocks = NB_HASH_ROUNDS_MAX;
162
163 return bench_hash_internal(benchfn, payload,
164 nbBlocks, sizef, size,
165 total_time_ms, iter_time_ms);
166 }