forked from powturbo/TurboPFor-Integer-Compression
-
Notifications
You must be signed in to change notification settings - Fork 0
/
icbench.c
1736 lines (1614 loc) · 64.9 KB
/
icbench.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) powturbo 2013-2019
GPL v2 License
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.
- homepage : https://sites.google.com/site/powturbo/
- github : https://github.com/powturbo
- twitter : https://twitter.com/powturbo
- email : powturbo [_AT_] gmail [_DOT_] com
**/
// IcBench: main program
#define _CRT_SECURE_NO_WARNINGS
#define _GNU_SOURCE
#define _LARGEFILE64_SOURCE 1
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
//#include <float.h>
#include <errno.h>
#include <sys/types.h>
#include <ctype.h>
#include <math.h>
#ifdef __APPLE__
#include <sys/malloc.h>
#else
#include <malloc.h>
#endif
#ifndef _WIN32
#include <sys/resource.h>
#endif
#ifdef _MSC_VER
#define PRId64 "I64d"
#include "vs/getopt.h"
#else
#include <inttypes.h>
#include <getopt.h>
#include <unistd.h>
#endif
#if !defined(_WIN32)
#include <sys/mman.h>
#include <sys/resource.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/param.h>
#else
#include <io.h>
#include <fcntl.h>
#define srand48(x) srand(x)
#define drand48() ((double)(rand()) / RAND_MAX)
#define __off64_t _off64_t
#endif
#include <time.h>
#include "conf.h"
#define VINT_IN
#include "vint.h"
#include "bitpack.h"
#include "vsimple.h"
#include "eliasfano.h"
#include "vp4.h"
#include "plugins.h"
//#define RDTSC_ON
#define TM_F 4
#include "time_.h"
#ifndef min
#define min(x,y) (((x)<(y)) ? (x) : (y))
#endif
int strpref(char **str, int n, char sep1, char sep2) {
int i, j=0;
for(;;j++)
for(i = 0; i < n; i++)
if(!str[i][j] || str[i][j] != str[0][j]) {
while (j > 0 && str[0][j-1] != sep1 && str[0][j-1] != sep2) j--;
return j;
}
return 0;
}
int memcheck(unsigned char *_in, unsigned _n, unsigned char *_cpy, int cmp) {
unsigned *in = (unsigned *)_in,*cpy=_cpy,n = (_n+3)/4;
int i;
if(cmp <= 1)
return 0;
for(i = 0; i < n; i++)
if(in[i] != cpy[i]) {
if(cmp > 4) abort(); // crash (AFL) fuzzing
printf("ERROR in[%d]=%x,%d dec[%d]=%x,%d\n", i, in[i], bsr64(in[i]), i, cpy[i], bsr64(cpy[i]) );
if(cmp>3) {
int j;
for(j=i & 0xffffff80u; j < i+128;j++) { unsigned e = in[j] != cpy[j];
if(e) printf("#%d:%x,%x ", j, in[j], cpy[j]);else printf("%d:%x ", j, in[j]);
}
printf("\n");
}
if(cmp > 2) exit(EXIT_FAILURE);
return i+1;
}
return 0;
}
int memcheck64(unsigned char *_in, unsigned _n, unsigned char *_cpy, int cmp) {
uint64_t *in = (uint64_t *)_in,*cpy=_cpy,n = (_n+7)/8;
int i;
if(cmp <= 1)
return 0;
for(i = 0; i < n; i++)
if(in[i] != cpy[i]) {
if(cmp > 4) abort(); // crash (AFL) fuzzing
printf("ERROR in[%d]=%llx,%d dec[%d]=%llx,%d\n", i, in[i], bsr64(in[i]), i, cpy[i], bsr64(cpy[i]) );
if(cmp>3) {
int j;
for(j=i & 0xffffff80u; j < i+128;j++) { unsigned e = in[j] != cpy[j];
if(e) printf("#%d:%x,%x ", j, in[j], cpy[j]);else printf("%d:%x ", j, in[j]);
}
printf("\n");
}
if(cmp > 2) exit(EXIT_FAILURE);
return i+1;
}
return 0;
}
//------------------------------- malloc ------------------------------------------------
#define USE_MMAP
#if __WORDSIZE == 64
#define MAP_BITS 31
#else
#define MAP_BITS 28
#endif
void *_valloc(size_t size, int a) {
if(!size) return NULL;
#if defined(_WIN32)
return VirtualAlloc(NULL, size, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
#elif defined(USE_MMAP)
void *ptr = mmap((size_t)a<<MAP_BITS, size, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
if(ptr == MAP_FAILED) return NULL;
return ptr;
#else
return malloc(size);
#endif
}
void _vfree(void *p, size_t size) {
if(!p) return;
#if defined(_WIN32)
VirtualFree(p, 0, MEM_RELEASE);
#elif defined(USE_MMAP)
munmap(p, size);
#else
free(p);
#endif
}
//--------------------------------------- IcBench ------------------------------------------------------------------
enum {
FMT_TEXT=1,
FMT_HTML,
FMT_HTMLT,
FMT_MARKDOWN,
FMT_VBULLETIN, // ex. post to encode.ru
FMT_CSV,
FMT_TSV,
FMT_SQUASH
};
char *fmtext[] = { "txt", "txt", "html", "htm", "md", "vbul", "csv", "tsv", "squash" };
//------------- plugin : usage ---------------------------------
//extern struct plugg plugg[];
void plugsprt(void) {
struct plugs *gs;
struct plugg *pg;
printf("Codec group:\n");
for(pg = plugg; pg->id[0]; pg++)
printf("%-16s %s %s\n", pg->id, pg->desc, pg->name);
printf("\n\n%-16s %-48s %s\n", "Function","Description", "level");
printf("\n%-16s %-48s %s\n", "--------","-----------", "-----");
for(gs = plugs; gs->id >= 0; gs++)
if(gs->codec)
printf("%-16s %-48s %s\n", gs->name, gs->desc?gs->desc:"", gs->lev?gs->lev:"");
}
void plugsprtv(FILE *f, int fmt) {
struct codecs *co;
char *pv = "";
switch(fmt) {
case FMT_HTMLT:
case FMT_HTML:
printf("%s\n", "<html><head><meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\"><title>IcBench</title></head><body><pre><ul>");
break;
case FMT_VBULLETIN:
fprintf(f,"[list]\n");
break;
}
for(co = codecs; co->coid >= 0; co++)
if(co->coid) {
char name[65],ver[33];
pv = co->name;
ver[0] = 0;
codver(co->coid, co->ver, ver);
sprintf(name, "%s %c%s", co->name, co->ver?'v':' ', ver);
switch(fmt) {
case FMT_VBULLETIN:
fprintf(f, "[*][URL=\"%s\"]%s[/URL]\n", co->url, name );
break;
case FMT_HTML :
fprintf(f, "<li><a href=\"%s\">%s</a>\n", co->url, name );
break;
case FMT_MARKDOWN :
fprintf(f, " - [%s](%s)\n", name, co->url);
break;
default:
fprintf(f, "%-24s\t%s\n", name, co->url );
}
}
switch(fmt) {
case FMT_VBULLETIN:
fprintf(f,"[/list]\n");
break;
case FMT_HTML:
fprintf(f,"</ul></pre></body></html>");
break;
}
}
//------------------ plugin: process ----------------------------------
struct plug {
int id,err,blksize,lev;
char *name,prm[17],tms[20];
int64_t len,memc,memd;
double tc,td;
};
struct plug plug[255];
int seg_ans = 32*1024, seg_huf = 32*1024, seg_anx = 12*1024, seg_hufx=11*1024;
static int cmp = 2,trans;
int verbose=1;
double fac = 1.3;
int plugins(struct plug *plug, struct plugs *gs, int *pk, unsigned bsize, int bsizex, int lev, char *prm) {
int i,k = *pk;
struct plug *p = &plug[k];
for(i = 0; i < k; i++)
if(plug[i].id == gs->id && plug[i].lev == lev && !strcmp(plug[i].prm,prm))
return -1;
memset(p, 0, sizeof(struct plug));
p->id = gs->id;
p->err = 0;
p->name = gs->name;
p->lev = lev;
strncpy(p->prm, prm?prm:(char *)"", 16);
p->prm[16] = 0;
p->tms[0] = 0;
p->blksize = gs->blksize?gs->blksize:bsize;
*pk = ++k;
return 0;
}
int plugreg(struct plug *plug, char *cmd, int k, int bsize, int bsizex) {
static char *cempty="";
int ignore = 0;
while(*cmd) {
char *name;
while(isspace(*cmd))
cmd++;
name = cmd;
while(isalnum(*cmd) || *cmd == '_' || *cmd == '-' || *cmd == '.')
cmd++;
if(*cmd) *cmd++ = 0;
if(!strcmp(name, "ON" )) {
ignore = 1;
continue;
}
else if(!strcmp(name, "OFF")) {
ignore = 0;
continue;
}
for(;;) {
char *prm;
int lev;
while(isspace(*cmd) || *cmd == ',')
cmd++;
prm = cmd;
lev = strtol(cmd, &cmd, 10);
if(prm == cmd) {
lev = -1;
prm = cempty;
}
else if(isalnum(*cmd)) {
prm = cmd;
while(isalnum(*cmd) || *cmd == '_' || *cmd == '-' || *cmd == '.')
cmd++;
if(*cmd)
*cmd++ = 0;
} else
prm = cempty;
{ int found = 0;
struct plugs *gs,*gfs = NULL;
if(!*name)
break;
for(gs = plugs; gs->id >= 0; gs++)
if(!strcasecmp(gs->name, name) ) {
char s[33],*q;
sprintf(s,"%d", lev);
found++;
if(lev<0 && gs->lev && !gs->lev[0] || gs->lev && (q=strstr(gs->lev, s)) && (q==gs->lev || *(q-1) == ',')) {
found++;
plugins(plug, gs, &k, bsize, bsizex, lev, prm);
}
break;
}
if((found<2 || !gs->codec) && !ignore) {
if(!found)
fprintf(stderr, "codec '%s' not found\n", name);
else if(!gs->codec)
fprintf(stderr, "codec '%s' not compiled in icbench\n", name);
else if(lev<0)
fprintf(stderr, "level [%s] not specified for codec '%s'\n", gs->lev, name );
else if(gs->lev && gs->lev[0])
fprintf(stderr, "level '%d' for codec '%s' not in range [%s]\n", lev, name, gs->lev);
else
fprintf(stderr, "codec '%s' has no levels\n", name);
exit(0);
}
}
while(isspace(*cmd))
cmd++;
if(*cmd != ',' && (*cmd < '0' || *cmd > '9'))
break;
}
}
a:plug[k].id = -1;
return k;
}
//------------------ plugin: print/plot -----------------------------
struct bandw {
uint64_t bw;
unsigned rtt;
char *s;
};
static struct bandw bw[] = {
{ 7*KB, 500, "GPRS 56" },//56kbps
{ 57*KB, 150, "2G 456" },
{ 125*KB, 40, "3G 1M" },
{ 250*KB, 5, "DSL 2M" },//DSL 2000
{ 500*KB, 20, "4G 4M" },
{ 3750*KB, 5, "WIFI 30M" },
{12500*KB, 5, "CAB 100M" },
{ 40*MB, 0, "USB2 40MB"},
{ 125*MB, 0, "ETH 1000" },
{ 200*MB, 0, "HDD 200MB"},
{ 550*MB, 0, "SSD 550MB"},
{ 1u*GB, 0, "SSD 1GB" },
{ 2u*GB, 0, "SSD 2GB" },
{ 4ull*GB, 0, "4GB/s" },
{ 8ull*GB, 0, "8GB/s" }
};
#define BWSIZE (sizeof(bw)/sizeof(struct bandw))
void plugprth(FILE *f, int fmt, char *t) {
char *plot = "<script src=https://cdn.plot.ly/plotly-latest.min.js></script>";
char *jquery = "<script src=\"http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js\"></script>";
char *tstyle = "<link rel=\"stylesheet\" href=\"http://tablesorter.com/themes/blue/style.css\" type=\"text/css\" media=\"print, projection, screen\" />";
char *table = "<script type=\"text/javascript\" src=\"http://tablesorter.com/__jquery.tablesorter.min.js\"></script>";
char *code = "<script type=\"text/javascript\">$(function() { $(\"#myTable\").tablesorter({sortList:[[0,0],[2,1]], widgets: ['zebra']}); $(\"#options\").tablesorter({sortList: [[0,0]], headers: { 3:{sorter: false}, 4:{sorter: false}}}); }); </script><script type=\"text/javascript\" src=\"http://tablesorter.com/__jquery.tablesorter.min.js\"></script><script type=\"text/javascript\">$(function() { $(\"#myTable2\").tablesorter({sortList:[[0,0],[2,1]], widgets: ['zebra']}); $(\"#options\").tablesorter({sortList: [[0,0]], headers: { 3:{sorter: false}, 4:{sorter: false}}}); }); </script>";
char s[128];
time_t tm;
time(&tm);
sprintf(s, "IcBench: %s - %s", t, asctime(localtime(&tm)));
switch(fmt) {
case FMT_TEXT:
fprintf(f,"%s\n", s );
break;
case FMT_VBULLETIN:
fprintf(f,"%s\n", s);
break;
case FMT_HTMLT:
fprintf(f,"<html><head><meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\"><title>IcBench: %s - </title></head><body>\n", s);
break;
case FMT_HTML:
fprintf(f,"<html><head><meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\"><title>IcBench: %s - </title>%s%s%s%s%s</head><body>\n", s, plot, jquery, tstyle, table, code);
break;
case FMT_MARKDOWN:
fprintf(f,"#### %s (bold = pareto) MB=1.000.000\n", s);
break;
}
}
void plugprtf(FILE *f, int fmt) {
switch(fmt) {
case FMT_HTML:
fprintf(f,"</body></html>\n");
break;
}
}
void plugprtth(FILE *f, int fmt) {
char *head = " C Size ratio%% Bits/Int C MI/s D MI/s Name File (bold = pareto)";
switch(fmt) {
case FMT_TEXT:
fprintf(f," C Size ratio%% Bits/Int C MI/s D MI/s Name File\n");
break;
case FMT_VBULLETIN:
fprintf(f,"[CODE][B]%s[/B] MB=1.000.0000\n", head);
break;
case FMT_HTMLT:
fprintf(f,"<pre><b>%s</b> MB=1.000.0000\n", head);
break;
case FMT_HTML:
fprintf(f,"<h3>IcBench: Compressor Benchmark</h3><table id='myTable' class='tablesorter' style=\"width:35%%\"><thead><tr><th>C Size</th><th>ratio%%</th><th>C MI/s</th><th>D MI/s</th><th>Name</th><th>C Mem</th><th>D Mem</th><th>File</th></tr></thead><tbody>\n");
break;
case FMT_MARKDOWN:
fprintf(f,"|C Size|ratio%%|Bits/Integer|C MI/s|D MI/s|Name|File|\n|--------:|-----:|--------:|--------:|----------------|----------------|\n");
break;
case FMT_CSV:
fprintf(f,"size,csize,ratio,bpi,ctime,dtime,name,file\n");
break;
case FMT_TSV:
fprintf(f,"size\tcsize\tratio\tbpi\tctime\tdtime\tname\tfile\n");
break;
case FMT_SQUASH:
fprintf(f,"dataset,plugin,codec,level,compressed_size,compress_cpu,compress_wall,decompress_cpu,decompress_wall\n");
break;
}
}
void plugprttf(FILE *f, int fmt) {
switch(fmt) {
case FMT_VBULLETIN:
fprintf(f,"[/CODE]\n");
break;
case FMT_HTMLT:
fprintf(f,"</pre>\n");
break;
case FMT_HTML:
fprintf(f,"</tbody></table>\n");
break;
case FMT_MARKDOWN:
fprintf(f,"\n\n");
break;
}
}
#define RATIO(_clen_, _len_) ((double)_clen_*100.0/_len_)
#define RATIOI(_clen_, _len_) ((double)_clen_*32.0/_len_)
#define FACTOR(_clen_, _len_) ((double)_len_*100.0/(double)_clen_)
#define RATIOF(_clen_, _len_, _mode_) _mode_?FACTOR(_clen_, _len_):RATIO(_clen_, _len_)
int be_factor=0;
void plugprt(struct plug *plug, int64_t totinlen, char *finame, int fmt, FILE *f) {
double ratio = RATIOF(plug->len,totinlen, be_factor),
ratioi = RATIOI(plug->len,totinlen),
tc = TMBS(totinlen,plug->tc), td = TMBS(totinlen,plug->td);
char name[65];
int c = 0, d = 0, n = 0;
if(plug->lev >= 0)
sprintf(name, "%s%s %d%s", plug->err?"?":"", plug->name, plug->lev, plug->prm);
else
sprintf(name, "%s%s%s", plug->err?"?":"", plug->name, plug->prm);
switch(fmt) {
case FMT_TEXT:
fprintf(f,"%12"PRId64" %5.1f %5.2f %8.2f %8.2f %-16s%s\n",
plug->len, ratio, ratioi, tc, td, name, finame);
break;
case FMT_VBULLETIN:
fprintf(f, "%12"PRId64" %5.1f %5.2f %s%8.2f%s %s%8.2f%s %s%-16s%s%s\n",
plug->len, ratio, ratioi, c?"[B]":"", tc, c?"[/B]":"", d?"[B]":"", td, d?"[/B]":"", n?"[B]":"", name, n?"[/B]":"", finame);
break;
case FMT_HTMLT:
fprintf(f, "%12"PRId64" %5.1f %5.2f %s%8.2f%s %s%8.2f%s %s%-16s%s%s\n",
plug->len, ratio, ratioi, c?"<b>":"", tc, c?"</b>":"", d?"<b>":"", td, d?"</b>":"", n?"<b>":"", name, n?"</b>":"", finame);
break;
case FMT_HTML:
fprintf(f, "<tr><td align=\"right\">%11"PRId64"</td><td align=\"right\">%5.1f</td><td align=\"right\">%5.2f</td><td align=\"right\">%s%8.2f%s</td><td align=\"right\">%s%8.2f%s</td><td>%s%-16s%s</td><td align=\"right\">%"PRId64"</td><td align=\"right\">%"PRId64"</td><td>%s</td></tr>\n",
plug->len, ratio, ratioi, c?"<b>":"", tc, c?"</b>":"", d?"<b>":"", td, d?"</b>":"", n?"<b>":"", name, n?"</b>":"",
// SIZE_ROUNDUP(plug->memc, Kb)/Kb, SIZE_ROUNDUP(plug->memd,Kb)/Kb,
plug->memc, plug->memd,
finame);
break;
case FMT_MARKDOWN:
fprintf(f, "|%"PRId64"|%5.1f|%5.2f|%s%.2f%s|%s%.2f%s|%s%s%s|%s|\n",
plug->len, ratio, ratioi, c?"**":"", tc, c?"**":"", d?"**":"", td, d?"**":"", n?"**":"", name, n?"**":"", finame);
break;
case FMT_CSV:
fprintf(f, "%12"PRId64",%11"PRId64",%5.1f,%5.2f,%8.2f,%8.2f,%-16s,%s\n",
totinlen, plug->len, ratio, ratioi, tc, td, name, finame);
break;
case FMT_TSV:
fprintf(f,"%12"PRId64"\t%11"PRId64"\t%5.1f\t5.2f\t%8.2f\t%8.2f\t%-16s\t%s\n",
totinlen, plug->len, ratio, ratioi, tc, td, name, finame);
break;
case FMT_SQUASH:
fprintf(f,"%12"PRId64",%11"PRId64",%5.1f,%8.2f,%8.2f,%-16s,%s\n",
finame, name, name, plug->len, tc, tc, td, td);
break;
}
}
static int blknum, speedup;
enum { SP_SPEEDUPC=1, SP_SPEEDUPD, SP_TRANSFERC, SP_TRANSFERD };
void plugprtph(FILE *f, int fmt) {
int i;
switch(fmt) {
case FMT_HTML:
fprintf(f,"<p><h3>IcBench: Speedup %s sheet</h3><table id='myTable2' class='tablesorter' style=\"width:80%%\"><thead><tr><th>Name</th>", (speedup&1)?"compression":"decompression");
for(i = 0; i < BWSIZE; i++)
fprintf(f, "<th>%s</th>", bw[i].s);
fprintf(f, "<td>File");
if(blknum)
fprintf(f, " blknum=%d ", blknum);
fprintf(f, "</td></tr></thead><tbody>\n");
break;
case FMT_MARKDOWN:
fprintf(f,"#### IcBench: Speedup %s sheet\n\n", (speedup&1)?"compression":"decompression");
fprintf(f, "|Name");
for(i = 0; i < BWSIZE; i++)
fprintf(f, "|%s", bw[i].s);
fprintf(f, "|File");
if(blknum)
fprintf(f, " blknum=%d ", blknum);
fprintf(f, "|\n");
fprintf(f, "|-------------");
for(i = 0; i < BWSIZE; i++)
fprintf(f, "|---------:");
fprintf(f, "|-------------|\n");
break;
case FMT_VBULLETIN:
fprintf(f,"IcBench: Speedup %s sheet\n\n", (speedup&1)?"compression":"decompression");
fprintf(f,"[CODE][B]\n");
default:
fprintf(f,"Name ");
for(i = 0; i < BWSIZE; i++)
fprintf(f, "%10s", bw[i].s);
if(blknum)
fprintf(f, " blknum=%d ", blknum);
fprintf(f, "\n");
if(fmt == FMT_VBULLETIN)
fprintf(f,"[/B]\n");
}
}
static inline double spmbs(double td, int64_t len, int i, int64_t totinlen) {
double t = td + len*TM_T/(double)bw[i].bw + blknum*(bw[i].rtt*1000.0);
return TMBS(totinlen,t);
}
//static inline double spdup(double td, int64_t len, int i, int64_t totinlen) { double t = td + len*TM_T/(double)bw[i].bw + blknum*(bw[i].rtt*1000.0); return ((double)totinlen*TM_T*100.0/t)/(double)bw[i].bw;}
static inline double spdup(double td, int64_t len, int i, int64_t totinlen) {
return (double)totinlen*100.0 / ((double)len + ((td+blknum*bw[i].rtt*1000.0)/TM_T)*(double)bw[i].bw );
}
void plugprtp(struct plug *plug, int64_t totinlen, char *finame, int fmt, int speedup, FILE *f) {
int i;
char name[65];
if(plug->lev>=0)
sprintf(name, "%s%s%s%d%s", plug->err?"?":"", plug->name, fmt==FMT_MARKDOWN?"_":" ", plug->lev, plug->prm);
else
sprintf(name, "%s%s%s", plug->err?"?":"", plug->name, plug->prm);
if(fmt == FMT_HTML)
fprintf(f, "<tr><td>%s</td>", name);
else
fprintf(f, "%-16s", name);
for(i = 0; i < BWSIZE; i++) {
switch(fmt) {
case FMT_HTMLT:
case FMT_HTML:
fprintf(f, "<td align=\"right\">");
break;
case FMT_MARKDOWN:
fprintf(f, "|");
break;
}
switch(speedup) {
case SP_TRANSFERD:
fprintf(f,"%9.3f ", spmbs(plug->td, plug->len, i, totinlen));
break;
case SP_SPEEDUPD:
fprintf(f,"%9d ", (int)(spdup(plug->td, plug->len, i, totinlen)+0.5));
break;
case SP_TRANSFERC:
fprintf(f,"%9.3f ", spmbs(plug->td, plug->len, i, totinlen));
break;
case SP_SPEEDUPC:
fprintf(f,"%9d ", (int)(spdup(plug->td, plug->len, i, totinlen)+0.5));
break;
}
switch(fmt) {
case FMT_HTMLT:
case FMT_HTML:
fprintf(f, "</td>");
break;
case FMT_MARKDOWN:
break;
}
}
switch(fmt) {
case FMT_HTMLT:
case FMT_HTML:
fprintf(f, "<td>%s</td></tr>\n", finame);
break;
case FMT_MARKDOWN:
fprintf(f, "|%s|\n", finame);
break;
default:
fprintf(f, "%s\n", finame);
break;
}
}
struct { unsigned x,y; } divplot[] = {
{ 1920, 1080}, // 16:9
{ 1600, 900},
{ 1280, 720},
{ 800, 600}
};
static unsigned divxy = 1, xlog = 1, xlog2, ylog, ylog2, plotmcpy;
void plugplotb(FILE *f, int fmt, int idiv) {
fprintf(f, "<div id='myDiv%d' style='width: %dpx; height: %dpx;'></div><script>", idiv, divplot[divxy].x, divplot[divxy].y);
}
void plugplot(struct plug *plug, int64_t totinlen, int fmt, int speedup, char *s, FILE *f) {
int i;
char name[65];
if(plug->lev>=0)
sprintf(name, "%s%s_%d%s", plug->err?"?":"", plug->name, plug->lev, plug->prm);
else
sprintf(name, "%s%s%s", plug->err?"?":"", plug->name, plug->prm);
strcat(s,name); strcat(s,",");
fprintf(f, "var %s = { x: [", name);
for(i = 0; i < BWSIZE; i++)
fprintf(f,"%llu%s", bw[i].bw, i+1 < BWSIZE?",":"");
fprintf(f, "],\ny: [");
for(i = 0; i < BWSIZE; i++)
switch(speedup) {
case SP_TRANSFERD:
fprintf(f,"%9.3f%s", spmbs(plug->td, plug->len, i, totinlen), i+1 < BWSIZE?",":"");
break;
case SP_SPEEDUPD:
fprintf(f,"%9d%s", (int)(spdup(plug->td, plug->len, i, totinlen)+0.5), i+1 < BWSIZE?",":"");
break;
case SP_TRANSFERC:
fprintf(f,"%9.3f%s", spmbs(plug->tc, plug->len, i, totinlen), i+1 < BWSIZE?",":"");
break;
case SP_SPEEDUPC:
fprintf(f,"%9d%s", (int)(spdup(plug->tc, plug->len, i, totinlen)+0.5), i+1 < BWSIZE?",":"");
break;
}
fprintf(f, "],\ntype: 'scatter',\nmode: 'lines+markers',\nline: {shape: 'spline'},\nname: '%s'\n};\n", name);
}
void plugplote(FILE *f, int fmt, char *s) {
fprintf(f, "var data = [%s];\nvar layout = {\ntitle:'IcBench Speedup: Transfer+%s Speed',\nxaxis: {\ntitle: '%s Transfer Speed (M=MI/s B=GB/s)',\n%s autorange: true\n }, \n yaxis: {\n\ntitle: 'Speedup %%',\n%sautorange: true\n }\n};\nPlotly.plot('myDiv1', data, layout);</script>\n",
s, (speedup&1)?"Compression":"Decompression", xlog?"log":"", xlog?"type: 'log',\n":"", ylog?"type: 'log',\n":"");
}
int libcmp(const struct plug *e1, const struct plug *e2) {
if(e1->len < e2->len)
return -1;
else if(e1->len > e2->len)
return 1;
else if(e1->td < e2->td)
return -1;
else if(e1->td > e2->td)
return 1;
return 0;
}
int libcmpn(const struct plug *e1, const struct plug *e2) {
int c = strcmp(e1->name, e2->name);
if(c < 0)
return -1;
else if(c > 0)
return 1;
else if(e1->lev < e2->lev)
return -1;
else if(e1->lev > e2->lev)
return 1;
return 0;
}
#define P_MCPY 1 // memcpy id
void plugplotc(struct plug *plug, int k, int64_t totinlen, int fmt, int speedup, char *s, FILE *f) {
int i, n = 0;
char name[65],txt[256];
struct plug *g,*gs=plug,*p;
qsort(plug, k, sizeof(struct plug), (int(*)(const void*,const void*))libcmpn);
for(txt[0] = name[0] = 0, g = plug; g < plug+k; g++)
if(g->id <= P_MCPY && !plotmcpy)
continue;
else {
if(strcmp(g->name, name)) {
if(name[0]) {
fprintf(f, "],\ny: [");
for(p = gs; p < g; p++)
fprintf(f, "%.2f%s", RATIOF(p->len,totinlen, be_factor), p+1<g?",":"");
fprintf(f, "],\nmode: 'markers+text',\ntype: 'scatter',\nname: '%s',\ntextposition: 'top center', textfont: { family: 'Raleway, sans-serif' }, marker: { size: 12 }\n", name, txt);
if(txt[0])
fprintf(f, "\n,text: [%s]\n", txt);
fprintf(f, "};\n");
strcat(s,",");
txt[0] = 0;
}
gs = g;
strcpy(name, g->name);
fprintf(f, "var %s = {\n x: [", g->name);
strcat(s, g->name);
} else {
fprintf(f, ",");
strcat(txt, ",");
}
if(g->lev >= 0) {
char ts[33];
sprintf(ts, "'%s%s%d%s'", divxy>=2?"":g->name, divxy>=2?"":",", g->lev, g->prm);
strcat(txt, ts);
}
{ double t = (speedup&1)?g->tc:g->td; fprintf(f, "%.2f", TMBS(totinlen,t)); }
}
fprintf(f, "],\ny: [");
for(p = gs; p < g; p++)
fprintf(f, "%.2f%s", RATIOF(p->len,totinlen, be_factor), p+1<g?",":"");
fprintf(f, "],\nmode: 'markers+text',\ntype: 'scatter',\nname: '%s',\ntextposition: 'top center', textfont: { family: 'Raleway, sans-serif' }, marker: { size: 12 }\n", name, txt);
if(txt[0])
fprintf(f, "\n,text:[%s]\n", txt);
fprintf(f, "};\n");
}
void plugplotce(FILE *f, int fmt, char *s) {
fprintf(f, "var data = [%s];\nvar layout = {\ntitle:'IcBench: %s',\nxaxis: {\ntitle: '%s speed MI/s',\n%s autorange: true\n }, \n yaxis: {\n\ntitle: 'Ratio (factor)',\n%sautorange: true\n }\n};\nPlotly.plot('myDiv2', data, layout);</script>\n",
s, (speedup&1)?"Compression":"Decompression", xlog2?"log":"", xlog2?"type: 'log',\n":"", ylog2?"type: 'log',\n":"");
}
int plugprts(struct plug *plug, int k, char *finame, int xstdout, uint64_t totlen, int fmt, char *t) {
struct plug *g;
char s[257];
FILE *fo;
if(!totlen) return 0; if(verbose>1) printf("'%s'\n", finame);
qsort(plug, k, sizeof(struct plug), (int(*)(const void*,const void*))libcmp);
sprintf(s, "%s.%s", finame, fmtext[fmt]);
fo = xstdout>=0?stdout:fopen(s, "w");
if(!fo)
die("file create error for '%s'\n", finame);
plugprth( fo, fmt, t);
plugprtth(fo, fmt);
for(g = plug; g < plug+k; g++)
plugprt(g, totlen, finame, fmt, fo);
plugprttf(fo, fmt);
if(speedup) {
switch(fmt) {
case FMT_TEXT :
fprintf(fo, "\n");
break;
case FMT_HTML :
break;
case FMT_HTMLT:
fprintf(fo, "<pre>\n");
break;
case FMT_MARKDOWN :
fprintf(fo, "\n");
break;
}
plugprtph(fo, fmt);
for(g = plug; g < plug+k; g++)
plugprtp(g, totlen, finame, fmt, speedup, fo);
fprintf(fo, "\n");
switch(fmt) {
case FMT_TEXT :
fprintf(fo, "\n"); break;
case FMT_HTML :
fprintf(fo, "</tbody></table>\n"); break;
case FMT_HTMLT:
fprintf(fo, "</pre>\n");
break;
case FMT_VBULLETIN:
fprintf(fo,"[/CODE]\n");
break;
case FMT_MARKDOWN :
fprintf(fo, "\n");
break;
}
if(fmt == FMT_HTML) {
char s[1025];
s[0] = 0; if(verbose>1) printf("generate speedup plot\n");
plugplotb(fo, fmt, 1);
for(g = plug; g < plug+k; g++)
if(g->id > P_MCPY || plotmcpy)
plugplot(g, totlen, fmt, speedup, s, fo);
plugplote(fo, fmt, s);
s[0] = 0; if(verbose>1) printf("generate speed/ratio plot\n");
plugplotb(fo, fmt, 2);
plugplotc(plug, k, totlen, fmt, speedup, s, fo);
plugplotce(fo, fmt, s);
}
}
plugprtf(fo, fmt);
if(xstdout<0) fclose(fo);
}
int plugread(struct plug *plug, char *finame, int64_t *totinlen) {
char s[256],name[33];
struct plug *p=plug;
FILE *fi = fopen(finame, "r");
if(!fi) return -1;
fgets(s, 255, fi);
for(p = plug;;) {
char ss[256],*t = ss,*q;
int i;
memset(p, 0, sizeof(p[0]));
p->tms[0] = 0;
if(!fgets(ss, 255, fi)) break;
for(q = t; *q && *q != '\t'; q++); *q++ = 0; strcpy(s, t); t = q;
*totinlen = strtoull(t, &t, 10);
p->len = strtoull(++t, &t, 10);
p->td = strtod( ++t, &t);
p->tc = strtod( ++t, &t);
for(q = ++t; *q && *q != '\t'; q++); *q++ = 0; strcpy(name,t); t=q;
p->lev = strtoul(t, &t, 10);
for(q = t; *q && *q != '\t'; q++); *q++ = 0; strcpy(p->prm,t); t = q;
p->memc = strtoull(t, &t, 10);
p->memd = strtoull(++t, &t, 10);
for(q = ++t; *q && *q != '\t'; q++); *q++ = 0; strcpy(p->tms,t); t = q;
if(p->prm[0]=='!')
p->prm[0]=0;
for(i = 0; plugs[i].id >=0; i++)
if(!strcmp(name, plugs[i].name)) {
p->name = plugs[i].name;
p->id = plugs[i].id; if(verbose>1) { fprintf(stdout, "%s\t%"PRId64"\t%"PRId64"\t%.6f\t%.6f\t%s\t%d%s\t%s\t%"PRId64"\t%"PRId64"\n", s, *totinlen, p->len, p->td, p->tc, p->name, p->lev, p->prm, p->tms, p->memc, p->memd); fflush(stdout); }
p++;
break;
}
}
fclose(fi);
return p - plug;
}
//----------------------------------- Benchmark -----------------------------------------------------------------------------
static int be_mcpy, be_nblocks, fuzz;
int be_mindelta=-1,mdelta=0;
int checksort(unsigned *in, unsigned n) {
if((be_mindelta == 0 || be_mindelta == 1) && !be_nblocks) {
int i;
for(i = 1; i < n/4; i++)
if(in[i] < in[i-1]+be_mindelta) {
fprintf(stderr, "WARNING: IDs not sorted %d:%u,%u\n", i, in[i-1], in[i]);fflush(stderr);
return -1;
}
}
return 0;
}
unsigned becomp(unsigned char *_in, unsigned _inlen, unsigned char *_out, unsigned outsize, unsigned bsize, int id, int lev, char *prm, int be_mindelta, CODCOMP codcomp) {
unsigned char *op,*oe = _out + outsize;
unsigned char *in,*ip;
if(!_inlen) return 0;
TMBEG(tm_rep,tm_Rep);
for(op = _out, in = _in; in < _in+_inlen; ) {
unsigned inlen = _inlen,bs;
if(be_nblocks) { blknum++;
inlen = ctou32(in); in+=4;
vbput32(op, inlen); // ctou32(op) = inlen; op+=4;
inlen *= 4; if(in+inlen>_in+_inlen) die("FATAL buffer overflow error %d", in - _inlen);
}
for(ip = in, in += inlen; ip < in; ) {
unsigned iplen = in - ip; iplen = min(iplen, bsize); // cnt++; csize += iplen;
op = codcomp(ip, iplen, op, oe-op, id, lev, prm, be_mindelta);
ip += iplen;
if(op > _out+outsize)
die("Compress overflow error %llu, %u in lib=%d\n", outsize, (int)(ptrdiff_t)(op - _out), id);
}
}
TMEND(_inlen); // printf("cnt=%d, csize=%d\n", cnt, csize);
return op - _out;
}
int bedecomp(unsigned char *_in, int _inlen, unsigned char *_out, unsigned _outlen, unsigned bsize, int id, int lev, char *prm, int be_mindelta, CODDECOMP coddecomp) {
unsigned char *ip;
unsigned char *out,*op;
TMBEG(tm_rep2,tm_Rep2);
for(ip = _in, out = _out; out < _out+_outlen;) {
unsigned outlen=_outlen,bs;
if(be_nblocks) {
vbget32(ip, outlen); //outlen = ctou32(ip); ip += 4;
ctou32(out) = outlen; out += 4;
outlen *= 4; if(out+outlen >_out+_outlen) die("FATAL: decompress overflow output error %d ", outlen);
}
for(op = out, out += outlen; op < out; ) {
unsigned oplen = out - op;
oplen = min(oplen, bsize);
ip = coddecomp(ip, 0, op, oplen, id, lev, prm, be_mindelta); if(ip-_in>_inlen) die("FATAL inlen %d,%d,bsize=%d ", _inlen, ip-_in, bsize);
op += oplen;
}
}
if(!(ip - _in))
return 0;
TMEND(_outlen);
return ip - _in;
}
struct plug plugr[32];
int tid;
#define BEPRE
#define BEINI
#define BEPOST
#define BEOPT
#define BEUSAGE
#define BEFILE
#define BENCHSTA
#define INOVD 4*1024
#if defined(_WIN32) && !defined(__MINGW__)
int getpagesize() {
static int pagesize = 0;
if (pagesize == 0) {
SYSTEM_INFO system_info;
GetSystemInfo(&system_info);
pagesize = max(system_info.dwPageSize, system_info.dwAllocationGranularity);
}
return pagesize;
}
#endif
struct cod { CODCOMP comp; CODDECOMP decomp; };
struct cod cods[] = { { codcomp, coddecomp }, {codcomps, coddecomps }, {codcompz, coddecompz } };
unsigned mininlen;
uint64_t filen;
size_t insizem;
char name[65];
uint64_t plugbench(struct plug *plug, unsigned char *in, unsigned inlen, unsigned char *out, unsigned outsize,
unsigned char *_cpy, unsigned bsize, struct plug *plugr, int tid, char *finame) {
double tc = 0.0, td = 0.0;
int be_mode = be_mindelta<0?0:(be_mindelta>1?2:1);
unsigned outlen = becomp(in, inlen, out, outsize, bsize, plug->id, plug->lev, plug->prm, be_mindelta, cods[be_mode].comp);
plug->len += outlen;
plug->tc += (tc += (double)tm_tm/(double)tm_rm);
if(!outlen) plug->tc = 0;
if(outlen && verbose /*&& inlen == filen*/) { printf("%12u %5.1f %5.2f %8.2f ", outlen, RATIOF(outlen,inlen, be_factor), RATIOI(outlen,inlen), TMBS(inlen,tc)); fflush(stdout); }
if(cmp && outlen) {
unsigned char *cpy = _cpy;
unsigned cpylen,e;
if(_cpy != in) memrcpy(cpy, in, inlen);
cpylen = bedecomp(out, outlen, cpy, inlen, bsize, plug->id, plug->lev, plug->prm, be_mindelta, cods[be_mode].decomp);
td = (double)tm_tm/(double)tm_rm; if(verbose /*&& inlen == filen*/) { printf("%8.2f %c%-16s%s\n", TMBS(inlen,td), outlen?' ':'?',name, finame); }
e = memcheck(in, inlen, cpy, cmp);
plug->err = plug->err?plug->err:e;
plug->td += td;
} else if(outlen && verbose /*&& inlen == filen*/) { printf("%8.2f %c%-16s%s\n", 0.0, outlen?' ':'?', name, finame); }
if(!plug->len) plug->err++;
return outlen;
}