1 /*
2 * Copyright (c) 2021 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28 /*
29 * Compresor and swap benchmarks.
30 */
31
32 #include <err.h>
33 #include <errno.h>
34 #include <mach/vm_page_size.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <sys/mman.h>
39 #include <sys/sysctl.h>
40 #include <sys/types.h>
41
42 #include "benchmark/helpers.h"
43
44 /*
45 * There are four different types of benchmarks
46 * Each type can run on a variable size buffer
47 * and with different types of data.
48 */
49 typedef enum test_variant {
50 VARIANT_COMPRESS,
51 VARIANT_COMPRESS_AND_DECOMPRESS,
52 VARIANT_SWAPOUT,
53 VARIANT_SWAPOUT_AND_SWAPIN
54 } test_variant_t;
55
56 /*
57 * Each benchmark supports buffers filled with different contents.
58 */
59 typedef enum allocation_type {
60 VARIANT_ZEROS,
61 VARIANT_RANDOM,
62 VARIANT_TYPICAL
63 } allocation_type_t;
64
65 /* Arguments parsed from the command line */
66 typedef struct test_args {
67 uint64_t ta_duration_seconds;
68 uint64_t ta_buffer_size;
69 test_variant_t ta_variant;
70 allocation_type_t ta_alloc_type;
71 bool ta_verbose;
72 } test_args_t;
73
74 struct perf_compressor_data {
75 user_addr_t buffer;
76 size_t buffer_size;
77 uint64_t benchmark_time;
78 uint64_t bytes_processed;
79 uint64_t compressor_growth;
80 };
81
82 /* Test Variants */
83 static const char *kCompressArgument = "compress";
84 static const char *kCompressAndDecompressArgument = "compress-and-decompress";
85
86 /* Allocation types */
87 static const char *kAllocZeroes = "zero";
88 static const char *kAllocRandom = "random";
89 static const char *kAllocTypical = "typical";
90
91 /*
92 * Failure codes
93 */
94 static int kInvalidArgument = 1;
95 static int kAllocationFailure = 2;
96 static int kCompressionFailure = 3;
97 static int kNYI = 4;
98
99 static void parse_arguments(int argc, const char **argv, test_args_t *args /* OUT */);
100 static void print_help(const char** argv);
101 static unsigned char *alloc_and_fill_buffer(size_t size, allocation_type_t alloc_type);
102 void fill_with_typical_data(unsigned char *buf, size_t size);
103 static void run_compress_benchmark(const test_args_t *args);
104 static uint64_t decompress_buffer(unsigned char *buf, size_t size);
105
106 int
main(int argc,const char ** argv)107 main(int argc, const char **argv)
108 {
109 test_args_t args = {0};
110 parse_arguments(argc, argv, &args);
111 switch (args.ta_variant) {
112 case VARIANT_COMPRESS:
113 case VARIANT_COMPRESS_AND_DECOMPRESS:
114 run_compress_benchmark(&args);
115 break;
116 default:
117 err(kNYI, "NYI: Test variant has not been implemented");
118 }
119 }
120
121 static void
run_compress_benchmark(const test_args_t * args)122 run_compress_benchmark(const test_args_t *args)
123 {
124 uint64_t total_compressor_time = 0;
125 uint64_t total_bytes_compressed = 0;
126 uint64_t total_compressor_bytes_used = 0;
127 uint64_t total_decompressor_time = 0;
128 double compressor_throughput = 0, decompressor_throughput = 0, compression_ratio = 0;
129 while (total_compressor_time < args->ta_duration_seconds * NSEC_PER_SEC) {
130 unsigned char *buf;
131 struct perf_compressor_data sysctl_data = {0};
132 size_t len = sizeof(sysctl_data);
133
134 benchmark_log(args->ta_verbose, "Start allocation\n");
135 buf = alloc_and_fill_buffer(args->ta_buffer_size, args->ta_alloc_type);
136 if (!buf) {
137 err(kAllocationFailure, "Unable to allocate test buffer\n");
138 }
139 benchmark_log(args->ta_verbose, "Finished allocation\n");
140
141 sysctl_data.buffer = (user_addr_t) buf;
142 sysctl_data.buffer_size = args->ta_buffer_size;
143 benchmark_log(args->ta_verbose, "Start compression\n");
144 int ret = sysctlbyname("kern.perf_compressor", &sysctl_data, &len, &sysctl_data, sizeof(sysctl_data));
145 if (ret < 0) {
146 fprintf(stderr, "Failed to compress buffer: %s\n", strerror(errno));
147 exit(kCompressionFailure);
148 }
149 if (sysctl_data.bytes_processed != args->ta_buffer_size) {
150 fprintf(stderr, "WARNING: Failed to compress the whole buffer. Only compressed %llu bytes out of %llu bytes\n", sysctl_data.bytes_processed, args->ta_buffer_size);
151 }
152 total_bytes_compressed += sysctl_data.bytes_processed;
153 total_compressor_time += sysctl_data.benchmark_time;
154 total_compressor_bytes_used += sysctl_data.compressor_growth;
155 benchmark_log(args->ta_verbose, "Finished compression\n");
156
157 if (args->ta_variant == VARIANT_COMPRESS_AND_DECOMPRESS) {
158 benchmark_log(args->ta_verbose, "Start decompression\n");
159 total_decompressor_time += decompress_buffer(buf, args->ta_buffer_size);
160 benchmark_log(args->ta_verbose, "Finished decompression\n");
161 }
162 munmap(buf, args->ta_buffer_size);
163 }
164 compressor_throughput = (double) total_bytes_compressed / total_compressor_time * NSEC_PER_SEC;
165 printf("bytes_compressed=%llu, compressor_bytes_used=%llu\n", total_bytes_compressed, total_compressor_bytes_used);
166 compression_ratio = (double) total_bytes_compressed / total_compressor_bytes_used;
167 if (total_decompressor_time != 0) {
168 decompressor_throughput = (double) total_bytes_compressed / total_decompressor_time * NSEC_PER_SEC;
169 }
170 printf("-----Results-----\n");
171 printf("Compressor Throughput");
172 if (args->ta_variant == VARIANT_COMPRESS_AND_DECOMPRESS) {
173 printf(", Decompressor Throughput");
174 }
175 if (args->ta_alloc_type != VARIANT_ZEROS) {
176 printf(", Compression Ratio\n");
177 } else {
178 printf("\n");
179 }
180
181 printf("%.0f", compressor_throughput);
182 if (args->ta_variant == VARIANT_COMPRESS_AND_DECOMPRESS) {
183 printf(", %.0f", decompressor_throughput);
184 }
185 if (args->ta_alloc_type != VARIANT_ZEROS) {
186 printf(", %.2f\n", compression_ratio);
187 } else {
188 printf("\n");
189 }
190 }
191
192 static unsigned char *
alloc_and_fill_buffer(size_t size,allocation_type_t alloc_type)193 alloc_and_fill_buffer(size_t size, allocation_type_t alloc_type)
194 {
195 unsigned char *buf = mmap_buffer(size);
196 if (!buf) {
197 return buf;
198 }
199 switch (alloc_type) {
200 case VARIANT_ZEROS:
201 bzero(buf, size);
202 break;
203 case VARIANT_RANDOM:
204 arc4random_buf(buf, size);
205 break;
206 case VARIANT_TYPICAL:
207 fill_with_typical_data(buf, size);
208 break;
209 default:
210 err(kInvalidArgument, "Unknown allocation variant\n");
211 }
212
213 return buf;
214 }
215
216 static uint64_t
decompress_buffer(unsigned char * buf,size_t size)217 decompress_buffer(unsigned char *buf, size_t size)
218 {
219 /*
220 * Fault in the compressed buffer to measure the decompression
221 * throughput.
222 */
223 uint64_t start_time, end_time;
224 start_time = current_timestamp_ns();
225 volatile unsigned char val;
226 for (unsigned char* ptr = buf; ptr < buf + size; ptr += vm_kernel_page_size) {
227 val = *ptr;
228 }
229 end_time = current_timestamp_ns();
230 return end_time - start_time;
231 }
232
233 /*
234 * Gives us the compression ratio we see in the typical case (~2.5)
235 */
236 void
fill_with_typical_data(unsigned char * buf,size_t size)237 fill_with_typical_data(unsigned char *buf, size_t size)
238 {
239 for (size_t i = 0; i < size / vm_kernel_page_size; i++) {
240 unsigned char val = 0;
241 for (size_t j = 0; j < vm_kernel_page_size; j += 16) {
242 memset(&buf[i * vm_kernel_page_size + j], val, 16);
243 if (i < 3400 * (vm_kernel_page_size / 4096)) {
244 val++;
245 }
246 }
247 }
248 }
249
250 static void
parse_arguments(int argc,const char ** argv,test_args_t * args)251 parse_arguments(int argc, const char** argv, test_args_t *args)
252 {
253 int current_positional_argument = 0;
254 long duration = -1, size_mb = -1;
255 memset(args, 0, sizeof(test_args_t));
256 for (int current_argument = 1; current_argument < argc; current_argument++) {
257 if (argv[current_argument][0] == '-') {
258 if (strcmp(argv[current_argument], "-v") == 0) {
259 args->ta_verbose = true;
260 } else {
261 fprintf(stderr, "Unknown argument %s\n", argv[current_argument]);
262 print_help(argv);
263 exit(kInvalidArgument);
264 }
265 if (current_argument >= argc) {
266 print_help(argv);
267 exit(kInvalidArgument);
268 }
269 } else {
270 const char *curr = argv[current_argument];
271 if (current_positional_argument == 0) {
272 if (strcasecmp(curr, kCompressArgument) == 0) {
273 args->ta_variant = VARIANT_COMPRESS;
274 } else if (strcasecmp(curr, kCompressAndDecompressArgument) == 0) {
275 args->ta_variant = VARIANT_COMPRESS_AND_DECOMPRESS;
276 } else {
277 print_help(argv);
278 exit(kInvalidArgument);
279 }
280 } else if (current_positional_argument == 1) {
281 if (strcasecmp(curr, kAllocZeroes) == 0) {
282 args->ta_alloc_type = VARIANT_ZEROS;
283 } else if (strcasecmp(curr, kAllocRandom) == 0) {
284 args->ta_alloc_type = VARIANT_RANDOM;
285 } else if (strcasecmp(curr, kAllocTypical) == 0) {
286 args->ta_alloc_type = VARIANT_TYPICAL;
287 } else {
288 print_help(argv);
289 exit(kInvalidArgument);
290 }
291 } else if (current_positional_argument == 2) {
292 duration = strtol(argv[current_argument], NULL, 10);
293 if (duration <= 0) {
294 print_help(argv);
295 exit(kInvalidArgument);
296 }
297 } else if (current_positional_argument == 3) {
298 size_mb = strtol(argv[current_argument], NULL, 10);
299 if (size_mb <= 0) {
300 print_help(argv);
301 exit(kInvalidArgument);
302 }
303 } else {
304 print_help(argv);
305 exit(kInvalidArgument);
306 }
307 current_positional_argument++;
308 }
309 }
310 if (current_positional_argument != 4) {
311 fprintf(stderr, "Expected 4 positional arguments. %d were supplied.\n", current_positional_argument);
312 print_help(argv);
313 exit(kInvalidArgument);
314 }
315 args->ta_duration_seconds = (uint64_t) duration;
316 args->ta_buffer_size = ((uint64_t) size_mb * (1UL << 20));
317 }
318
319 static void
print_help(const char ** argv)320 print_help(const char** argv)
321 {
322 fprintf(stderr, "%s: [-v] <test-variant> allocation-type duration_seconds buffer_size_mb\n", argv[0]);
323 fprintf(stderr, "\ntest variants:\n");
324 fprintf(stderr, " %s Measure compressor throughput.\n", kCompressArgument);
325 fprintf(stderr, " %s Measure compressor and decompressor throughput.\n", kCompressAndDecompressArgument);
326 fprintf(stderr, "\n allocation types:\n");
327 fprintf(stderr, " %s All zeros.\n", kAllocZeroes);
328 fprintf(stderr, " %s Random bytes.\n", kAllocRandom);
329 fprintf(stderr, " %s Typical compression ratio (~2.5:1).\n", kAllocTypical);
330 }
331