/
stress-zlib.c
1152 lines (997 loc) · 28.3 KB
/
stress-zlib.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (C) 2013-2019 Canonical, Ltd.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* This code is a complete clean re-write of the stress tool by
* Colin Ian King <colin.king@canonical.com> and attempts to be
* backwardly compatible with the stress tool by Amos Waterland
* <apw@rossby.metr.ou.edu> but has more stress tests and more
* functionality.
*
*/
#include "stress-ng.h"
static const help_t help[] = {
{ NULL, "zlib N", "start N workers compressing data with zlib" },
{ NULL, "zlib-ops N", "stop after N zlib bogo compression operations" },
{ NULL, "zlib-level L", "specify zlib compression level 0=fast, 9=best" },
{ NULL, "zlib-method M", "specify zlib random data generation method M" },
{ NULL, NULL, NULL }
};
#if defined(HAVE_LIB_Z)
#include "zlib.h"
#define DATA_SIZE_1K (KB) /* Must be a multiple of 8 bytes */
#define DATA_SIZE_4K (KB * 4) /* Must be a multiple of 8 bytes */
#define DATA_SIZE_16K (KB * 16) /* Must be a multiple of 8 bytes */
#define DATA_SIZE_64K (KB * 64) /* Must be a multiple of 8 bytes */
#define DATA_SIZE_128K (KB * 128) /* Must be a multiple of 8 bytes */
#define DATA_SIZE DATA_SIZE_64K
typedef void (*stress_zlib_rand_data_func)(const args_t *args, uint32_t *data, const int size);
typedef struct {
const char *name; /* human readable form of random data generation selection */
const stress_zlib_rand_data_func func; /* the random data generation function */
} stress_zlib_rand_data_info_t;
typedef struct {
uint64_t xsum;
bool error;
bool pipe_broken;
bool interrupted;
} xsum_t;
static stress_zlib_rand_data_info_t zlib_rand_data_methods[];
static volatile bool pipe_broken = false;
static sigjmp_buf jmpbuf;
static const char *const lorem_ipsum[] = {
"Lorem ipsum dolor sit amet, consectetur adipiscing elit. ",
"Nullam imperdiet quam at ultricies finibus. ",
"Nunc venenatis euismod velit sit amet ornare.",
"Quisque et orci eu eros convallis luctus at facilisis ex. ",
"Quisque fringilla nulla felis, sed mollis est feugiat nec. ",
"Vivamus at urna sit amet velit suscipit iaculis. ",
"Curabitur mauris ipsum, gravida in laoreet ac, dignissim id lacus. ",
"Proin dignissim, erat nec interdum commodo, nulla mi tempor dui, quis scelerisque odio nisi in tortor. ",
"Mauris dignissim ex auctor nulla lobortis semper. ",
"Mauris sit amet tempus risus, ac tincidunt lectus. ",
"Maecenas sollicitudin porttitor nisi ac faucibus. ",
"Cras eu sollicitudin arcu. ",
"In sed fringilla eros, vitae fringilla tortor. ",
"Phasellus mollis feugiat tortor, a ornare nunc auctor porttitor. ",
"Fusce malesuada ut felis vitae vestibulum. ",
"Donec sit amet hendrerit massa, vitae ultrices augue. ",
"Proin volutpat velit ipsum, id imperdiet risus placerat ut. ",
"Cras vitae risus ipsum. ",
"Sed lobortis quam in dictum pulvinar. ",
"In non accumsan justo. ",
"Ut pretium pulvinar gravida. ",
"Proin ultricies nisi libero, a convallis dui vestibulum eu. ",
"Aliquam in molestie magna, et ullamcorper turpis. ",
"Donec id pharetra sem. "
"Duis dui massa, fringilla id mattis nec, consequat eget felis. ",
"Integer a lobortis ipsum, quis ornare felis. ",
"Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. ",
"Nulla sed cursus nibh. ",
"Quisque at ex dolor. ",
"Mauris viverra risus pellentesque nisl dictum rutrum. ",
"Aliquam non est quis enim dictum tristique. ",
"Fusce feugiat hendrerit hendrerit. ",
"Ut egestas sed erat et egestas. ",
"Pellentesque convallis erat sed sapien pellentesque vulputate. ",
"Praesent non sapien aliquet risus varius suscipit. ",
"Curabitur eu felis dignissim, hendrerit magna vitae, condimentum nunc. ",
"Donec ut tincidunt sem. ",
"Sed in leo et metus ultricies semper quis quis ex. ",
"Sed fringilla porta mi vitae condimentum. ",
"In vitae metus libero."
};
/*
* stress_sigpipe_handler()
* SIGFPE handler
*/
static void MLOCKED_TEXT stress_sigpipe_handler(int signum)
{
(void)signum;
pipe_broken = true;
}
static void MLOCKED_TEXT stress_bad_read_handler(int signum)
{
(void)signum;
siglongjmp(jmpbuf, 1);
}
/*
* stress_rand_data_bcd()
* fill buffer with random binary coded decimal digits
*/
static void stress_rand_data_bcd(const args_t *args, uint32_t *data, const int size)
{
register uint8_t *ptr = (uint8_t *)data;
const uint8_t *end = ptr + size;
(void)args;
while (ptr < end) {
uint8_t rndval = mwc8() % 100;
/* Not the most efficient but it works */
*ptr++ = (rndval % 10) | ((rndval / 10) << 4);
}
}
/*
* stress_rand_data_utf8()
* fill buffer with random bytes converted into utf8
*/
static void stress_rand_data_utf8(const args_t *args, uint32_t *data, const int size)
{
const int n = size / sizeof(uint16_t);
register uint8_t *ptr = (uint8_t *)data;
const uint8_t *end = ptr + n;
(void)args;
while (ptr < end) {
uint8_t ch = mwc8();
if (ch <= 0x7f)
*ptr++ = ch;
else {
if (UNLIKELY(ptr < end - 1)) {
*ptr++ = ((ch >> 6) & 0x1f) | 0xc0;
*ptr++ = (ch & 0x3f) | 0x80;
} else {
/* no more space, zero pad */
*ptr = 0;
}
}
}
}
/*
* stress_rand_data_binary()
* fill buffer with random binary data
*/
static void stress_rand_data_binary(const args_t *args, uint32_t *data, const int size)
{
const int n = size / sizeof(*data);
register int i;
(void)args;
for (i = 0; i < n; i++, data++)
*data = mwc32();
}
/*
* stress_rand_data_text()
* fill buffer with random ASCII text
*/
static void stress_rand_data_text(const args_t *args, uint32_t *data, const int size)
{
(void)args;
stress_strnrnd((char *)data, size);
}
/*
* stress_rand_data_01()
* fill buffer with random ASCII 0 or 1
*/
static void stress_rand_data_01(const args_t *args, uint32_t *data, const int size)
{
unsigned char *ptr = (unsigned char *)data;
register int i;
(void)args;
for (i = 0; i < size; i += 8, ptr += 8) {
uint8_t v = mwc8();
*(ptr + 0) = '0' + (v & 1);
v >>= 1;
*(ptr + 1) = '0' + (v & 1);
v >>= 1;
*(ptr + 2) = '0' + (v & 1);
v >>= 1;
*(ptr + 3) = '0' + (v & 1);
v >>= 1;
*(ptr + 4) = '0' + (v & 1);
v >>= 1;
*(ptr + 5) = '0' + (v & 1);
v >>= 1;
*(ptr + 6) = '0' + (v & 1);
v >>= 1;
*(ptr + 7) = '0' + (v & 1);
}
}
/*
* stress_rand_data_digits()
* fill buffer with random ASCII '0' .. '9'
*/
static void stress_rand_data_digits(const args_t *args, uint32_t *data, const int size)
{
unsigned char *ptr = (unsigned char *)data;
register int i;
(void)args;
for (i = 0; i < size; i++, ptr++)
*ptr = '0' + (mwc32() % 10);
}
/*
* stress_rand_data_00_ff()
* fill buffer with random 0x00 or 0xff
*/
static void stress_rand_data_00_ff(const args_t *args, uint32_t *data, const int size)
{
unsigned char *ptr = (unsigned char *)data;
register int i;
(void)args;
for (i = 0; i < size; i += 8, ptr += 8) {
uint8_t v = mwc8();
*(ptr + 0) = (v & 1) ? 0x00 : 0xff;
*(ptr + 1) = (v & 2) ? 0x00 : 0xff;
*(ptr + 2) = (v & 4) ? 0x00 : 0xff;
*(ptr + 3) = (v & 8) ? 0x00 : 0xff;
*(ptr + 4) = (v & 16) ? 0x00 : 0xff;
*(ptr + 5) = (v & 32) ? 0x00 : 0xff;
*(ptr + 6) = (v & 64) ? 0x00 : 0xff;
*(ptr + 7) = (v & 128) ? 0x00 : 0xff;
}
}
/*
* stress_rand_data_nybble()
* fill buffer with 0x00..0x0f
*/
static void stress_rand_data_nybble(const args_t *args, uint32_t *data, const int size)
{
unsigned char *ptr = (unsigned char *)data;
register int i;
(void)args;
for (i = 0; i < size; i += 8, ptr += 8) {
uint32_t v = mwc32();
*(ptr + 0) = v & 0xf;
v >>= 4;
*(ptr + 1) = v & 0xf;
v >>= 4;
*(ptr + 2) = v & 0xf;
v >>= 4;
*(ptr + 3) = v & 0xf;
v >>= 4;
*(ptr + 4) = v & 0xf;
v >>= 4;
*(ptr + 5) = v & 0xf;
v >>= 4;
*(ptr + 6) = v & 0xf;
v >>= 4;
*(ptr + 7) = v & 0xf;
}
}
/*
* stress_rand_data_rarely_1()
* fill buffer with data that is 1 in every 32 bits 1
*/
static void stress_rand_data_rarely_1(const args_t *args, uint32_t *data, const int size)
{
const int n = size / sizeof(*data);
register int i;
(void)args;
for (i = 0; i < n; i++, data++)
*data = 1 << (mwc32() & 0x1f);
}
/*
* stress_rand_data_rarely_0()
* fill buffer with data that is 1 in every 32 bits 0
*/
static void stress_rand_data_rarely_0(const args_t *args, uint32_t *data, const int size)
{
const int n = size / sizeof(*data);
register int i;
(void)args;
for (i = 0; i < n; i++, data++)
*data = ~(1 << (mwc32() & 0x1f));
}
/*
* stress_rand_data_fixed()
* fill buffer with data that is 0x04030201
*/
static void stress_rand_data_fixed(const args_t *args, uint32_t *data, const int size)
{
const int n = size / sizeof(*data);
register int i;
(void)args;
for (i = 0; i < n; i++, data++)
*data = 0x04030201;
}
/*
* stress_rand_data_double()
* fill buffer with double precision floating point binary data
*/
static void stress_rand_data_double(const args_t *args, uint32_t *data, const int size)
{
const int n = size / sizeof(double);
uint8_t *ptr = (uint8_t *)data;
register int i;
static double theta = 0.0;
double dtheta = M_PI / 180.0;
(void)args;
for (i = 0; i < n; i++) {
double s = sin(theta);
(void)memcpy(ptr, &s, sizeof(double));
theta += dtheta;
dtheta += 0.001;
ptr += sizeof(double);
}
}
/*
* stress_rand_data_gray()
* fill buffer with gray code of incrementing 16 bit values
*
*/
static void stress_rand_data_gray(const args_t *args, uint32_t *data, const int size)
{
const int n = size / sizeof(uint16_t);
static uint16_t val = 0;
register uint16_t v = val;
register uint16_t *ptr = (uint16_t *)data;
register int i;
(void)args;
for (i = 0; i < n; i++, v++) {
*(ptr++) = (v >> 1) ^ i;
}
val = v;
}
/*
* stress_rand_data_parity()
* fill buffer with 7 bit data + 1 parity bit
*/
static void stress_rand_data_parity(const args_t *args, uint32_t *data, const int size)
{
uint8_t *ptr = (uint8_t *)data;
register int i;
(void)args;
for (i = 0; i < size; i++, ptr++) {
uint8_t v = mwc8();
uint8_t p = v & 0xfe;
p ^= p >> 4;
p &= 0xf;
p = (0x6996 >> p) & 1;
*ptr = v | p;
}
}
#define PINK_MAX_ROWS (12)
#define PINK_BITS (16)
#define PINK_SHIFT ((sizeof(uint64_t) * 8) - PINK_BITS)
#if !defined(HAVE_BUILTIN_CTZ)
/*
* stress_builtin_ctz()
* implementation of count trailing zeros ctz, this will
* be optimized down to 1 instruction on x86 targets
*/
static inline uint32_t stress_builtin_ctz(register uint32_t x)
{
register unsigned int n;
if (!x)
return 32;
n = 0;
if ((x & 0x0000ffff) == 0) {
n += 16;
x >>= 16;
}
if ((x & 0x000000ff) == 0) {
n += 8;
x >>= 8;
}
if ((x & 0x0000000f) == 0) {
n += 4;
x >>= 4;
}
if ((x & 0x00000003) == 0) {
n += 2;
x >>= 2;
}
if ((x & 0x00000001) == 0) {
n += 1;
}
return n;
}
#endif
/*
* stress_rand_data_pink()
* fill buffer with pink noise 0..255 using an
* the Gardner method with the McCartney
* selection tree optimization
*/
static void stress_rand_data_pink(const args_t *args, uint32_t *data, const int size)
{
int i;
unsigned char *ptr = (unsigned char *)data;
size_t idx = 0;
const size_t mask = (1 << PINK_MAX_ROWS) - 1;
uint64_t sum = 0;
const uint64_t max = (PINK_MAX_ROWS + 1) * (1 << (PINK_BITS - 1));
uint64_t rows[PINK_MAX_ROWS];
const float scalar = 256.0 / max;
(void)args;
(void)memset(rows, 0, sizeof(rows));
for (i = 0; i < size; i++) {
int64_t rnd;
idx = (idx + 1) & mask;
if (idx) {
#if defined(HAVE_BUILTIN_CTZ)
const size_t j = __builtin_ctz(idx);
#else
const size_t j = stress_builtin_ctz(idx);
#endif
sum -= rows[j];
rnd = (int64_t)mwc64() >> PINK_SHIFT;
sum += rnd;
rows[j] = rnd;
}
rnd = (int64_t)mwc64() >> PINK_SHIFT;
*(ptr++) = (int)((scalar * ((int64_t)sum + rnd)) + 128.0);
}
}
/*
* stress_rand_data_brown()
* fills buffer with brown noise.
*/
static void stress_rand_data_brown(const args_t *args, uint32_t *data, const int size)
{
static uint8_t val = 127;
register int i;
register uint8_t *ptr = (unsigned char *)data;
(void)args;
for (i = 0; i < size; i++) {
val += ((mwc8() % 31) - 15);
*(ptr++) = val;
}
}
/*
* stress_rand_data_lrand48()
* fills buffer with random data from lrand48
*/
static void stress_rand_data_lrand48(const args_t *args, uint32_t *data, const int size)
{
static bool seeded = false;
const int n = size / sizeof(*data);
register int i;
if (UNLIKELY(!seeded)) {
srand48(mwc32());
seeded = true;
}
(void)args;
for (i = 0; i < n; i++, data++)
*data = lrand48();
}
/*
* stress_rand_data_latin()
* fill buffer with random latin Lorum Ipsum text.
*/
static void stress_rand_data_latin(const args_t *args, uint32_t *data, const int size)
{
register int i;
static const char *ptr = NULL;
char *dataptr = (char *)data;
(void)args;
if (!ptr)
ptr = lorem_ipsum[mwc32() % SIZEOF_ARRAY(lorem_ipsum)];
for (i = 0; i < size; i++) {
if (!*ptr)
ptr = lorem_ipsum[mwc32() % SIZEOF_ARRAY(lorem_ipsum)];
*dataptr++ = *ptr++;
}
}
/*
* stress_rand_data_objcode()
* fill buffer with object code data from stress-ng
*/
static void stress_rand_data_objcode(const args_t *args, uint32_t *const data, const int size)
{
const int n = size / sizeof(*data);
register int i;
static bool use_rand_data = false;
struct sigaction sigsegv_orig, sigbus_orig;
char *text = NULL, *dataptr, *text_start, *text_end;
const size_t text_len = stress_text_addr(&text_start, &text_end);
if (use_rand_data) {
stress_rand_data_binary(args, data, size);
return;
}
/* Try and install sighandlers */
if (stress_sighandler(args->name, SIGSEGV, stress_bad_read_handler, &sigsegv_orig) < 0) {
use_rand_data = true;
stress_rand_data_binary(args, data, size);
return;
}
if (stress_sighandler(args->name, SIGBUS, stress_bad_read_handler, &sigbus_orig) < 0) {
use_rand_data = true;
(void)stress_sigrestore(args->name, SIGSEGV, &sigsegv_orig);
stress_rand_data_binary(args, data, size);
return;
}
/*
* Some architectures may generate faults on reads
* from specific text pages, so trap these and
* fall back to stress_rand_data_binary.
*/
if (sigsetjmp(jmpbuf, 1) != 0) {
(void)stress_sigrestore(args->name, SIGSEGV, &sigsegv_orig);
(void)stress_sigrestore(args->name, SIGBUS, &sigbus_orig);
stress_rand_data_binary(args, data, size);
return;
}
/* Start in random place in stress-ng text segment */
text = text_len ? text_start + (mwc64() % text_len) : text_start;
for (dataptr = (char *)data, i = 0; i < n; i++, dataptr++) {
*dataptr = *text;
if (text++ >= text_end)
text = text_start;
}
(void)stress_sigrestore(args->name, SIGSEGV, &sigsegv_orig);
(void)stress_sigrestore(args->name, SIGBUS, &sigbus_orig);
}
/*
* stress_rand_data_zero()
* fill buffer with zeros
*/
static void stress_rand_data_zero(const args_t *args, uint32_t *data, const int size)
{
(void)args;
(void)memset(data, 0, size);
}
static const stress_zlib_rand_data_func rand_data_funcs[] = {
stress_rand_data_00_ff,
stress_rand_data_01,
stress_rand_data_digits,
stress_rand_data_bcd,
stress_rand_data_binary,
stress_rand_data_brown,
stress_rand_data_double,
stress_rand_data_fixed,
stress_rand_data_gray,
stress_rand_data_latin,
stress_rand_data_lrand48,
stress_rand_data_nybble,
stress_rand_data_objcode,
stress_rand_data_parity,
stress_rand_data_pink,
stress_rand_data_rarely_1,
stress_rand_data_rarely_0,
stress_rand_data_text,
stress_rand_data_utf8,
stress_rand_data_zero,
};
/*
* stress_zlib_random_test()
* randomly select data generation function
*/
static HOT OPTIMIZE3 void stress_zlib_random_test(const args_t *args, uint32_t *data, const int size)
{
rand_data_funcs[mwc32() % SIZEOF_ARRAY(rand_data_funcs)](args, data, size);
}
/*
* Table of zlib data methods
*/
static stress_zlib_rand_data_info_t zlib_rand_data_methods[] = {
{ "random", stress_zlib_random_test }, /* Special "random" test */
{ "00ff", stress_rand_data_00_ff },
{ "ascii01", stress_rand_data_01 },
{ "asciidigits",stress_rand_data_digits },
{ "bcd", stress_rand_data_bcd },
{ "binary", stress_rand_data_binary },
{ "brown", stress_rand_data_brown },
{ "double", stress_rand_data_double },
{ "gray", stress_rand_data_gray },
{ "fixed", stress_rand_data_fixed },
{ "latin", stress_rand_data_latin },
{ "lrand48", stress_rand_data_lrand48 },
{ "nybble", stress_rand_data_nybble },
{ "objcode", stress_rand_data_objcode },
{ "parity", stress_rand_data_parity },
{ "pink", stress_rand_data_pink },
{ "rarely1", stress_rand_data_rarely_1 },
{ "rarely0", stress_rand_data_rarely_0 },
{ "text", stress_rand_data_text },
{ "utf8", stress_rand_data_utf8 },
{ "zero", stress_rand_data_zero },
{ NULL, NULL }
};
/*
* stress_set_zlib_level
* set zlib compression level, 0..9,
* 0 = no compression, 1 = fastest, 9 = best compression
*/
static int stress_set_zlib_level(const char *opt)
{
uint32_t zlib_level;
zlib_level = get_uint32(opt);
check_range("zlib-level", zlib_level, 0, Z_BEST_COMPRESSION);
return set_setting("zlib-level", TYPE_ID_UINT32, &zlib_level);
}
/*
* stress_set_zlib_method()
* set the default zlib random data method
*/
static int stress_set_zlib_method(const char *opt)
{
stress_zlib_rand_data_info_t *info;
for (info = zlib_rand_data_methods; info->func; info++) {
if (!strcmp(info->name, opt)) {
set_setting("zlib-method", TYPE_ID_UINTPTR_T, &info);
return 0;
}
}
(void)fprintf(stderr, "zlib-method must be one of:");
for (info = zlib_rand_data_methods; info->func; info++) {
(void)fprintf(stderr, " %s", info->name);
}
(void)fprintf(stderr, "\n");
return -1;
}
static const opt_set_func_t opt_set_funcs[] = {
{ OPT_zlib_level, stress_set_zlib_level },
{ OPT_zlib_method, stress_set_zlib_method },
{ 0, NULL }
};
/*
* stress_zlib_err()
* turn a zlib error to something human readable
*/
static const char *stress_zlib_err(const int zlib_err)
{
static char buf[1024];
switch (zlib_err) {
case Z_OK:
return "no error";
case Z_ERRNO:
(void)snprintf(buf, sizeof(buf), "system error, errno=%d (%s)\n",
errno, strerror(errno));
return buf;
case Z_STREAM_ERROR:
return "invalid compression level";
case Z_DATA_ERROR:
return "invalid or incomplete deflate data";
case Z_MEM_ERROR:
return "out of memory";
case Z_VERSION_ERROR:
return "zlib version mismatch";
default:
(void)snprintf(buf, sizeof(buf), "unknown zlib error %d\n", zlib_err);
return buf;
}
}
/*
* stress_zlib_inflate()
* inflate compressed data out of the read
* end of a pipe fd
*/
static int stress_zlib_inflate(
const args_t *args,
const int fd,
const int xsum_fd)
{
int ret, err = 0;
z_stream stream_inf;
uint64_t xsum_chars = 0;
static unsigned char in[DATA_SIZE];
static unsigned char out[DATA_SIZE];
xsum_t xsum;
xsum.xsum = 0;
xsum.error = false;
xsum.pipe_broken = false;
xsum.interrupted = false;
stream_inf.zalloc = Z_NULL;
stream_inf.zfree = Z_NULL;
stream_inf.opaque = Z_NULL;
stream_inf.avail_in = 0;
stream_inf.next_in = Z_NULL;
ret = inflateInit(&stream_inf);
if (ret != Z_OK) {
pr_fail("%s: zlib inflateInit error: %s\n",
args->name, stress_zlib_err(ret));
xsum.error = true;
goto xsum_error;
}
do {
ssize_t sz;
sz = read(fd, in, DATA_SIZE);
if (sz < 0) {
if ((errno != EINTR) && (errno != EPIPE)) {
pr_fail("%s: zlib inflate pipe read error: "
"errno=%d (%s)\n",
args->name, errno, strerror(errno));
(void)inflateEnd(&stream_inf);
xsum.error = true;
goto xsum_error;
} else {
if (errno == EINTR)
xsum.interrupted = true;
if (errno == EPIPE)
xsum.pipe_broken = true;
break;
}
}
if (sz == 0) {
xsum.pipe_broken = true;
break;
}
stream_inf.avail_in = sz;
stream_inf.next_in = in;
do {
stream_inf.avail_out = DATA_SIZE;
stream_inf.next_out = out;
ret = inflate(&stream_inf, Z_NO_FLUSH);
switch (ret) {
case Z_NEED_DICT:
case Z_DATA_ERROR:
case Z_MEM_ERROR:
(void)inflateEnd(&stream_inf);
goto xsum_error;
}
if (g_opt_flags & OPT_FLAGS_VERIFY) {
size_t i;
for (i = 0; i < DATA_SIZE - stream_inf.avail_out; i++) {
xsum.xsum += (uint64_t)out[i];
xsum_chars++;
}
}
} while (stream_inf.avail_out == 0);
} while (ret != Z_STREAM_END);
/*
if (g_opt_flags & OPT_FLAGS_VERIFY) {
pr_dbg("%s: inflate xsum value %" PRIu64
", xsum_chars %" PRIu64 "\n",
args->name, xsum.xsum, xsum_chars);
}
*/
(void)inflateEnd(&stream_inf);
if (write(xsum_fd, &xsum, sizeof(xsum)) < 0) {
pr_fail("%s: zlib inflate pipe write error: "
"errno=%d (%s)\n",
args->name, err, strerror(err));
}
return ((ret == Z_OK) || (ret == Z_STREAM_END)) ?
EXIT_SUCCESS : EXIT_FAILURE;
xsum_error:
xsum.error = true;
if (write(xsum_fd, &xsum, sizeof(xsum)) < 0) {
pr_fail("%s: zlib inflate pipe write error: "
"errno=%d (%s)\n",
args->name, err, strerror(err));
}
return EXIT_FAILURE;
}
/*
* stress_zlib_deflate()
* compress random data and write it down the
* write end of a pipe fd
*/
static int stress_zlib_deflate(
const args_t *args,
const int fd,
const int xsum_fd,
const int zlib_level)
{
int ret, err = 0;
bool do_run;
z_stream stream_def;
uint64_t bytes_in = 0, bytes_out = 0;
uint64_t xsum_chars = 0;
int flush;
stress_zlib_rand_data_info_t *opt_zlib_rand_data_func = &zlib_rand_data_methods[0];
double t1, t2;
xsum_t xsum;
(void)get_setting("zlib-method", &opt_zlib_rand_data_func);
xsum.xsum = 0;
xsum.error = false;
xsum.pipe_broken = false;
xsum.interrupted = false;
stream_def.zalloc = Z_NULL;
stream_def.zfree = Z_NULL;
stream_def.opaque = Z_NULL;
t1 = time_now();
ret = deflateInit(&stream_def, zlib_level);
if (ret != Z_OK) {
pr_fail("%s: zlib deflateInit error: %s\n",
args->name, stress_zlib_err(ret));
xsum.error = true;
ret = EXIT_FAILURE;
goto xsum_error;
}
do {
static uint32_t in[DATA_SIZE / sizeof(uint32_t)];
unsigned char *xsum_in = (unsigned char *)in;
opt_zlib_rand_data_func->func(args, in, DATA_SIZE);
stream_def.avail_in = DATA_SIZE;
stream_def.next_in = (unsigned char *)in;
if (g_opt_flags & OPT_FLAGS_VERIFY) {
for (int i = 0; i < (int)(DATA_SIZE / sizeof(unsigned char)); i++) {
xsum.xsum += (uint64_t)xsum_in[i];
xsum_chars++;
}
}
do_run = keep_stressing();
flush = do_run ? Z_NO_FLUSH : Z_FINISH;
bytes_in += DATA_SIZE;
do {
static unsigned char out[DATA_SIZE];
int def_size, rc;
stream_def.avail_out = DATA_SIZE;
stream_def.next_out = out;
rc = deflate(&stream_def, flush);
if (rc == Z_STREAM_ERROR) {
pr_fail("%s: zlib deflate error: %s\n",
args->name, stress_zlib_err(rc));
(void)deflateEnd(&stream_def);
ret = EXIT_FAILURE;
goto xsum_error;
}
def_size = DATA_SIZE - stream_def.avail_out;
bytes_out += def_size;
if (write(fd, out, def_size) != def_size) {
if ((errno != EINTR) && (errno != EPIPE) && (errno != 0)) {
pr_fail("%s: write error: errno=%d (%s)\n",
args->name, errno, strerror(errno));
(void)deflateEnd(&stream_def);
ret = EXIT_FAILURE;
goto xsum_error;
} else {
if (errno == EINTR)
xsum.interrupted = true;
if (errno == EPIPE)
xsum.pipe_broken = true;
goto finish;
}
}
inc_counter(args);
} while (stream_def.avail_out == 0);
} while (flush != Z_FINISH);
finish:
t2 = time_now();
pr_inf("%s: instance %" PRIu32 ": compression ratio: %5.2f%% (%.2f MB/sec)\n",
args->name, args->instance,
bytes_in ? 100.0 * (double)bytes_out / (double)bytes_in : 0,
(t2 - t1 > 0.0) ? (bytes_in / (t2 - t1)) / MB : 0.0);
/*
if (g_opt_flags & OPT_FLAGS_VERIFY) {
pr_dbg("%s: deflate xsum value %" PRIu64
", xsum_chars %" PRIu64 "\n",
args->name, xsum.xsum, xsum_chars);
}
*/
(void)deflateEnd(&stream_def);
ret = EXIT_SUCCESS;
xsum_error:
if (write(xsum_fd, &xsum, sizeof(xsum)) < 0 ) {
pr_fail("%s: zlib deflate pipe write error: "
"errno=%d (%s)\n",
args->name, err, strerror(err));
}
return ret;