Welcome to mirror list, hosted at ThFree Co, Russian Federation.

BenchmarkDialog.cpp « GUI « UI « 7zip « CPP - github.com/kornelski/7z.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 44d250358d5458ec82521156fd3620fc6d522942 (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
// BenchmarkDialog.cpp

#include "StdAfx.h"

#include "../../../../C/CpuArch.h"

#include "../../../Common/Defs.h"
#include "../../../Common/IntToString.h"
#include "../../../Common/MyException.h"
#include "../../../Common/StringConvert.h"
#include "../../../Common/StringToInt.h"

#include "../../../Windows/Synchronization.h"
#include "../../../Windows/System.h"
#include "../../../Windows/Thread.h"
#include "../../../Windows/SystemInfo.h"

#include "../../../Windows/Control/ComboBox.h"
#include "../../../Windows/Control/Edit.h"

#include "../../Common/MethodProps.h"

#include "../FileManager/DialogSize.h"
#include "../FileManager/HelpUtils.h"
#ifdef LANG
#include "../FileManager/LangUtils.h"
#endif

#include "../../MyVersion.h"

#include "../Common/Bench.h"

#include "BenchmarkDialogRes.h"
#include "BenchmarkDialog.h"

using namespace NWindows;

#define kHelpTopic "fm/benchmark.htm"

static const UINT_PTR kTimerID = 4;
static const UINT kTimerElapse = 1000; // 1000

// use PRINT_ITER_TIME to show time of each iteration in log box
// #define PRINT_ITER_TIME

static const unsigned kRatingVector_NumBundlesMax = 20;

enum MyBenchMessages
{
  k_Message_Finished = WM_APP + 1
};

enum My_Message_WPARAM
{
  k_Msg_WPARM_Thread_Finished = 0,
  k_Msg_WPARM_Iter_Finished,
  k_Msg_WPARM_Enc1_Finished
};


struct CBenchPassResult
{
  CTotalBenchRes Enc;
  CTotalBenchRes Dec;
  #ifdef PRINT_ITER_TIME
  DWORD Ticks;
  #endif
  // CBenchInfo EncInfo; // for debug
  // CBenchPassResult() {};
};


struct CTotalBenchRes2: public CTotalBenchRes
{
  UInt64 UnpackSize;

  void Init()
  {
    CTotalBenchRes::Init();
    UnpackSize = 0;
  }

  void SetFrom_BenchInfo(const CBenchInfo &info)
  {
    NumIterations2 = 1;
    Generate_From_BenchInfo(info);
    UnpackSize = info.Get_UnpackSize_Full();
  }

  void Update_With_Res2(const CTotalBenchRes2 &r)
  {
    Update_With_Res(r);
    UnpackSize += r.UnpackSize;
  }
};

  
struct CSyncData
{
  UInt32 NumPasses_Finished;

  // UInt64 NumEncProgress; // for debug
  // UInt64 NumDecProgress; // for debug
  // CBenchInfo EncInfo; // for debug

  CTotalBenchRes2 Enc_BenchRes_1;
  CTotalBenchRes2 Enc_BenchRes;

  CTotalBenchRes2 Dec_BenchRes_1;
  CTotalBenchRes2 Dec_BenchRes;

  #ifdef PRINT_ITER_TIME
  DWORD TotalTicks;
  #endif

  int RatingVector_DeletedIndex;
  // UInt64 RatingVector_NumDeleted;

  bool BenchWasFinished; // all passes were finished
  bool NeedPrint_Freq;
  bool NeedPrint_RatingVector;
  bool NeedPrint_Enc_1;
  bool NeedPrint_Enc;
  bool NeedPrint_Dec_1;
  bool NeedPrint_Dec;
  bool NeedPrint_Tot; // intermediate Total was updated after current pass

  void Init();
};


void CSyncData::Init()
{
  NumPasses_Finished = 0;
  
  // NumEncProgress = 0;
  // NumDecProgress = 0;
  
  Enc_BenchRes.Init();
  Enc_BenchRes_1.Init();
  Dec_BenchRes.Init();
  Dec_BenchRes_1.Init();
  
  #ifdef PRINT_ITER_TIME
  TotalTicks = 0;
  #endif
  
  RatingVector_DeletedIndex = -1;
  // RatingVector_NumDeleted = 0;
  
  BenchWasFinished =
    NeedPrint_Freq =
    NeedPrint_RatingVector =
    NeedPrint_Enc_1 =
    NeedPrint_Enc   =
    NeedPrint_Dec_1 =
    NeedPrint_Dec   =
    NeedPrint_Tot   = false;
};


struct CBenchProgressSync
{
  bool Exit; // GUI asks BenchThread to Exit, and BenchThread reads that variable
  UInt32 NumThreads;
  UInt64 DictSize;
  UInt32 NumPasses_Limit;
  int Level;
  
  // must be written by benchmark thread, read by GUI thread */
  CSyncData sd;
  CRecordVector<CBenchPassResult> RatingVector;

  NWindows::NSynchronization::CCriticalSection CS;

  AString Text;
  bool TextWasChanged;

  /* BenchFinish_Task_HRESULT    - for result from benchmark code
     BenchFinish_Thread_HRESULT  - for Exceptions and service errors
             these arreos must be shown even if user escapes benchmark */

  HRESULT BenchFinish_Task_HRESULT;
  HRESULT BenchFinish_Thread_HRESULT;

  UInt32 NumFreqThreadsPrev;
  UString FreqString_Sync;
  UString FreqString_GUI;

  CBenchProgressSync()
  {
    NumPasses_Limit = 1;
  }

  void Init();
  
  void SendExit()
  {
    NWindows::NSynchronization::CCriticalSectionLock lock(CS);
    Exit = true;
  }
};


void CBenchProgressSync::Init()
{
  Exit = false;
  
  BenchFinish_Task_HRESULT = S_OK;
  BenchFinish_Thread_HRESULT = S_OK;
  
  sd.Init();
  RatingVector.Clear();
  
  NumFreqThreadsPrev = 0;
  FreqString_Sync.Empty();
  FreqString_GUI.Empty();
  
  Text.Empty();
  TextWasChanged = true;
}



struct CMyFont
{
  HFONT _font;
  CMyFont(): _font(NULL) {}
  ~CMyFont()
  {
    if (_font)
      DeleteObject(_font);
  }
  void Create(const LOGFONT *lplf)
  {
    _font = CreateFontIndirect(lplf);
  }
};


class CBenchmarkDialog;

struct CThreadBenchmark
{
  CBenchmarkDialog *BenchmarkDialog;
  DECL_EXTERNAL_CODECS_LOC_VARS2;
  // HRESULT Result;

  HRESULT Process();
  static THREAD_FUNC_DECL MyThreadFunction(void *param)
  {
    /* ((CThreadBenchmark *)param)->Result = */
    ((CThreadBenchmark *)param)->Process();
    return 0;
  }
};


class CBenchmarkDialog:
  public NWindows::NControl::CModalDialog
{
  NWindows::NControl::CComboBox m_Dictionary;
  NWindows::NControl::CComboBox m_NumThreads;
  NWindows::NControl::CComboBox m_NumPasses;
  NWindows::NControl::CEdit _consoleEdit;
  UINT_PTR _timer;

  UInt32 _startTime;
  UInt32 _finishTime;
  bool _finishTime_WasSet;
  
  bool WasStopped_in_GUI;
  bool ExitWasAsked_in_GUI;
  bool NeedRestart;

  CMyFont _font;

  UInt64 RamSize;
  UInt64 RamSize_Limit;
  bool RamSize_Defined;

  UInt32 NumPasses_Finished_Prev;

  UString ElapsedSec_Prev;

  void InitSyncNew()
  {
    NumPasses_Finished_Prev = (UInt32)(Int32)-1;
    ElapsedSec_Prev.Empty();
    Sync.Init();
  }

  virtual bool OnInit();
  virtual bool OnDestroy();
  virtual bool OnSize(WPARAM /* wParam */, int xSize, int ySize);
  virtual bool OnMessage(UINT message, WPARAM wParam, LPARAM lParam);
  virtual bool OnCommand(int code, int itemID, LPARAM lParam);
  virtual void OnHelp();
  virtual void OnCancel();
  virtual bool OnTimer(WPARAM timerID, LPARAM callback);
  virtual bool OnButtonClicked(int buttonID, HWND buttonHWND);

  void Disable_Stop_Button();
  void OnStopButton();
  void RestartBenchmark();
  void StartBenchmark();

  void UpdateGui();

  void PrintTime();
  void PrintRating(UInt64 rating, UINT controlID);
  void PrintUsage(UInt64 usage, UINT controlID);
  void PrintBenchRes(const CTotalBenchRes2 &info, const UINT ids[]);

  UInt32 GetNumberOfThreads();
  size_t OnChangeDictionary();

  void SetItemText_Number(int itemID, UInt64 val, LPCTSTR post = NULL);
  void Print_MemUsage(UString &s, UInt64 memUsage) const;
  bool IsMemoryUsageOK(UInt64 memUsage) const
    { return memUsage + (1 << 20) <= RamSize_Limit; }

  void MyKillTimer();

  void SendExit_Status(const wchar_t *message)
  {
    SetItemText(IDT_BENCH_ERROR_MESSAGE, message);
    Sync.SendExit();
  }

public:
  CBenchProgressSync Sync;

  bool TotalMode;
  CObjectVector<CProperty> Props;

  CSysString Bench2Text;

  NWindows::CThread _thread;
  CThreadBenchmark _threadBenchmark;

  CBenchmarkDialog():
      _timer(0),
      TotalMode(false),
      WasStopped_in_GUI(false),
      ExitWasAsked_in_GUI(false),
      NeedRestart(false)
      {}

  ~CBenchmarkDialog();

  bool PostMsg_Finish(LPARAM param)
  {
    if ((HWND)*this)
      return PostMsg(k_Message_Finished, param);
    // the (HWND)*this is NULL only for some internal code failure
    return true;
  }

  INT_PTR Create(HWND wndParent = 0)
  {
    BIG_DIALOG_SIZE(332, 228);
    return CModalDialog::Create(TotalMode ? IDD_BENCH_TOTAL : SIZED_DIALOG(IDD_BENCH), wndParent);
  }
  void MessageBoxError(LPCWSTR message)
  {
    MessageBoxW(*this, message, L"7-Zip", MB_ICONERROR);
  }
  void MessageBoxError_Status(LPCWSTR message)
  {
    UString s ("ERROR: ");
    s += message;
    MessageBoxError(s);
    SetItemText(IDT_BENCH_ERROR_MESSAGE, s);
  }
};









UString HResultToMessage(HRESULT errorCode);

#ifdef LANG
static const UInt32 kLangIDs[] =
{
  IDT_BENCH_DICTIONARY,
  IDT_BENCH_MEMORY,
  IDT_BENCH_NUM_THREADS,
  IDT_BENCH_SPEED,
  IDT_BENCH_RATING_LABEL,
  IDT_BENCH_USAGE_LABEL,
  IDT_BENCH_RPU_LABEL,
  IDG_BENCH_COMPRESSING,
  IDG_BENCH_DECOMPRESSING,
  IDG_BENCH_TOTAL_RATING,
  IDT_BENCH_CURRENT,
  IDT_BENCH_RESULTING,
  IDT_BENCH_ELAPSED,
  IDT_BENCH_PASSES,
  IDB_STOP,
  IDB_RESTART
};

static const UInt32 kLangIDs_Colon[] =
{
  IDT_BENCH_SIZE
};

#endif

static LPCTSTR const kProcessingString = TEXT("...");
static LPCTSTR const kGB = TEXT(" GB");
static LPCTSTR const kMB = TEXT(" MB");
static LPCTSTR const kKB = TEXT(" KB");
// static LPCTSTR const kMIPS = TEXT(" MIPS");
static LPCTSTR const kKBs = TEXT(" KB/s");

static const unsigned kMinDicLogSize = 18;

static const UInt32 kMinDicSize = (UInt32)1 << kMinDicLogSize;
static const size_t kMaxDicSize = (size_t)1 << (22 + sizeof(size_t) / 4 * 5);
// static const size_t kMaxDicSize = (size_t)1 << 16;
    /*
    #ifdef MY_CPU_64BIT
      (UInt32)(Int32)-1; // we can use it, if we want 4 GB buffer
      // (UInt32)15 << 28;
    #else
      (UInt32)1 << 27;
    #endif
    */


static int ComboBox_Add_UInt32(NWindows::NControl::CComboBox &cb, UInt32 v)
{
  TCHAR s[16];
  ConvertUInt32ToString(v, s);
  int index = (int)cb.AddString(s);
  cb.SetItemData(index, v);
  return index;
}


bool CBenchmarkDialog::OnInit()
{
  #ifdef LANG
  LangSetWindowText(*this, IDD_BENCH);
  LangSetDlgItems(*this, kLangIDs, ARRAY_SIZE(kLangIDs));
  // LangSetDlgItems_Colon(*this, kLangIDs_Colon, ARRAY_SIZE(kLangIDs_Colon));
  LangSetDlgItemText(*this, IDT_BENCH_CURRENT2, IDT_BENCH_CURRENT);
  LangSetDlgItemText(*this, IDT_BENCH_RESULTING2, IDT_BENCH_RESULTING);
  #endif

  InitSyncNew();

  if (TotalMode)
  {
    _consoleEdit.Attach(GetItem(IDE_BENCH2_EDIT));
    LOGFONT f;
    memset(&f, 0, sizeof(f));
    f.lfHeight = 14;
    f.lfWidth = 0;
    f.lfWeight = FW_DONTCARE;
    f.lfCharSet = DEFAULT_CHARSET;
    f.lfOutPrecision = OUT_DEFAULT_PRECIS;
    f.lfClipPrecision = CLIP_DEFAULT_PRECIS;
    f.lfQuality = DEFAULT_QUALITY;

    f.lfPitchAndFamily = FIXED_PITCH;
    // MyStringCopy(f.lfFaceName, TEXT(""));
    // f.lfFaceName[0] = 0;
    _font.Create(&f);
    if (_font._font)
      _consoleEdit.SendMsg(WM_SETFONT, (WPARAM)_font._font, TRUE);
  }

  UInt32 numCPUs = 1;

  {
    AString s ("/ ");
  
    NSystem::CProcessAffinity threadsInfo;
    threadsInfo.InitST();

    #ifndef _7ZIP_ST
    if (threadsInfo.Get() && threadsInfo.processAffinityMask != 0)
      numCPUs = threadsInfo.GetNumProcessThreads();
    else
      numCPUs = NSystem::GetNumberOfProcessors();
    #endif

    s.Add_UInt32(numCPUs);
    s += GetProcessThreadsInfo(threadsInfo);
    SetItemTextA(IDT_BENCH_HARDWARE_THREADS, s);
  
    {
      AString s2;
      GetSysInfo(s, s2);
      SetItemTextA(IDT_BENCH_SYS1, s);
      if (s != s2 && !s2.IsEmpty())
        SetItemTextA(IDT_BENCH_SYS2, s2);
    }
    {
      GetCpuName_MultiLine(s);
      SetItemTextA(IDT_BENCH_CPU, s);
    }
    {
      GetOsInfoText(s);
      s += " : ";
      AddCpuFeatures(s);
      SetItemTextA(IDT_BENCH_CPU_FEATURE, s);
    }

    s = "7-Zip " MY_VERSION_CPU;
    SetItemTextA(IDT_BENCH_VER, s);
  }


  // ----- Num Threads ----------

  if (numCPUs < 1)
    numCPUs = 1;
  numCPUs = MyMin(numCPUs, (UInt32)(1 << 6)); // it's WIN32 limit

  UInt32 numThreads = Sync.NumThreads;

  if (numThreads == (UInt32)(Int32)-1)
    numThreads = numCPUs;
  if (numThreads > 1)
    numThreads &= ~1;
  const UInt32 kNumThreadsMax = (1 << 12);
  if (numThreads > kNumThreadsMax)
    numThreads = kNumThreadsMax;

  m_NumThreads.Attach(GetItem(IDC_BENCH_NUM_THREADS));
  const UInt32 numTheads_Combo = numCPUs * 2;
  UInt32 v = 1;
  int cur = 0;
  for (; v <= numTheads_Combo;)
  {
    int index = ComboBox_Add_UInt32(m_NumThreads, v);
    const UInt32 vNext = v + (v < 2 ? 1 : 2);
    if (v <= numThreads)
    if (numThreads < vNext || vNext > numTheads_Combo)
    {
      if (v != numThreads)
        index = ComboBox_Add_UInt32(m_NumThreads, numThreads);
      cur = index;
    }
    v = vNext;
  }
  m_NumThreads.SetCurSel(cur);
  Sync.NumThreads = GetNumberOfThreads();


  // ----- Dictionary ----------

  m_Dictionary.Attach(GetItem(IDC_BENCH_DICTIONARY));
  
  RamSize = (UInt64)(sizeof(size_t)) << 29;
  RamSize_Defined = NSystem::GetRamSize(RamSize);

  
  #ifdef UNDER_CE
  const UInt32 kNormalizedCeSize = (16 << 20);
  if (RamSize > kNormalizedCeSize && RamSize < (33 << 20))
    RamSize = kNormalizedCeSize;
  #endif
  RamSize_Limit = RamSize / 16 * 15;

  if (Sync.DictSize == (UInt64)(Int64)-1)
  {
    unsigned dicSizeLog = 25;
    #ifdef UNDER_CE
    dicSizeLog = 20;
    #endif
    if (RamSize_Defined)
    for (; dicSizeLog > kBenchMinDicLogSize; dicSizeLog--)
      if (IsMemoryUsageOK(GetBenchMemoryUsage(
          Sync.NumThreads, Sync.Level, (UInt64)1 << dicSizeLog, TotalMode)))
        break;
    Sync.DictSize = (UInt64)1 << dicSizeLog;
  }
  
  if (Sync.DictSize < kMinDicSize) Sync.DictSize = kMinDicSize;
  if (Sync.DictSize > kMaxDicSize) Sync.DictSize = kMaxDicSize;

  cur = 0;
  for (unsigned i = (kMinDicLogSize - 1) * 2; i <= (32 - 1) * 2; i++)
   {
      const size_t dict = (size_t)(2 + (i & 1)) << (i / 2);
      // if (i == (32 - 1) * 2) dict = kMaxDicSize;
      TCHAR s[32];
      const TCHAR *post;
      UInt32 d;
           if (dict >= ((UInt32)1 << 31)) { d = (UInt32)(dict >> 30); post = kGB; }
      else if (dict >= ((UInt32)1 << 21)) { d = (UInt32)(dict >> 20); post = kMB; }
      else                                { d = (UInt32)(dict >> 10); post = kKB; }
      ConvertUInt32ToString(d, s);
      lstrcat(s, post);
      const int index = (int)m_Dictionary.AddString(s);
      m_Dictionary.SetItemData(index, dict);
      if (dict <= Sync.DictSize)
        cur = index;
      if (dict >= kMaxDicSize)
        break;
    }
  m_Dictionary.SetCurSel(cur);


  // ----- Num Passes ----------

  m_NumPasses.Attach(GetItem(IDC_BENCH_NUM_PASSES));
  cur = 0;
  v = 1;
  for (;;)
  {
    int index = ComboBox_Add_UInt32(m_NumPasses, v);
    const bool isLast = (v >= 10000000);
    UInt32 vNext = v * 10;
         if (v < 2) vNext = 2;
    else if (v < 5) vNext = 5;
    else if (v < 10) vNext = 10;

    if (v <= Sync.NumPasses_Limit)
    if (isLast || Sync.NumPasses_Limit < vNext)
    {
      if (v != Sync.NumPasses_Limit)
        index = ComboBox_Add_UInt32(m_NumPasses, Sync.NumPasses_Limit);
      cur = index;
    }
    v = vNext;
    if (isLast)
      break;
  }
  m_NumPasses.SetCurSel(cur);

  if (TotalMode)
    NormalizeSize(true);
  else
    NormalizePosition();

  RestartBenchmark();

  return CModalDialog::OnInit();
}


bool CBenchmarkDialog::OnSize(WPARAM /* wParam */, int xSize, int ySize)
{
  int mx, my;
  GetMargins(8, mx, my);

  if (!TotalMode)
  {
    RECT rect;
    GetClientRectOfItem(IDT_BENCH_LOG, rect);
    int x = xSize - rect.left - mx;
    int y = ySize - rect.top - my;
    if (x < 0) x = 0;
    if (y < 0) y = 0;
    MoveItem(IDT_BENCH_LOG, rect.left, rect.top, x, y, true);
    return false;
  }

  int bx1, bx2, by;

  GetItemSizes(IDCANCEL, bx1, by);
  GetItemSizes(IDHELP, bx2, by);

  {
    int y = ySize - my - by;
    int x = xSize - mx - bx1;
    
    InvalidateRect(NULL);
    
    MoveItem(IDCANCEL, x, y, bx1, by);
    MoveItem(IDHELP, x - mx - bx2, y, bx2, by);
  }

  if (_consoleEdit)
  {
    int yPos = ySize - my - by;
    RECT rect;
    GetClientRectOfItem(IDE_BENCH2_EDIT, rect);
    int y = rect.top;
    int ySize2 = yPos - my - y;
    const int kMinYSize = 20;
    int xx = xSize - mx * 2;
    if (ySize2 < kMinYSize)
    {
      ySize2 = kMinYSize;
    }
    _consoleEdit.Move(mx, y, xx, ySize2);
  }
  return false;
}


UInt32 CBenchmarkDialog::GetNumberOfThreads()
{
  return (UInt32)m_NumThreads.GetItemData_of_CurSel();
}


#define UINT_TO_STR_3(s, val) { \
  s[0] = (wchar_t)('0' + (val) / 100); \
  s[1] = (wchar_t)('0' + (val) % 100 / 10); \
  s[2] = (wchar_t)('0' + (val) % 10); \
  s[3] = 0; }

static void NumberToDot3(UInt64 val, WCHAR *s)
{
  ConvertUInt64ToString(val / 1000, s);
  const UInt32 rem = (UInt32)(val % 1000);
  s += MyStringLen(s);
  *s++ = '.';
  UINT_TO_STR_3(s, rem);
}

void CBenchmarkDialog::SetItemText_Number(int itemID, UInt64 val, LPCTSTR post)
{
  TCHAR s[64];
  ConvertUInt64ToString(val, s);
  if (post)
    lstrcat(s, post);
  SetItemText(itemID, s);
}

static void AddSize_MB(UString &s, UInt64 size)
{
  s.Add_UInt64((size + (1 << 20) - 1) >> 20);
  s += kMB;
}

void CBenchmarkDialog::Print_MemUsage(UString &s, UInt64 memUsage) const
{
  AddSize_MB(s, memUsage);
  if (RamSize_Defined)
  {
    s += " / ";
    AddSize_MB(s, RamSize);
  }
}

size_t CBenchmarkDialog::OnChangeDictionary()
{
  const size_t dict = (size_t)m_Dictionary.GetItemData_of_CurSel();
  const UInt64 memUsage = GetBenchMemoryUsage(GetNumberOfThreads(),
      Sync.Level,
      dict,
      false); // totalBench mode

  UString s;
  Print_MemUsage(s, memUsage);

  #ifdef _7ZIP_LARGE_PAGES
  {
    AString s2;
    Add_LargePages_String(s2);
    if (!s2.IsEmpty())
    {
      s.Add_Space();
      s += s2;
    }
  }
  #endif

  SetItemText(IDT_BENCH_MEMORY_VAL, s);

  return dict;
}


static const UInt32 g_IDs[] =
{
  IDT_BENCH_COMPRESS_SIZE1,
  IDT_BENCH_COMPRESS_SIZE2,
  IDT_BENCH_COMPRESS_USAGE1,
  IDT_BENCH_COMPRESS_USAGE2,
  IDT_BENCH_COMPRESS_SPEED1,
  IDT_BENCH_COMPRESS_SPEED2,
  IDT_BENCH_COMPRESS_RATING1,
  IDT_BENCH_COMPRESS_RATING2,
  IDT_BENCH_COMPRESS_RPU1,
  IDT_BENCH_COMPRESS_RPU2,
  
  IDT_BENCH_DECOMPR_SIZE1,
  IDT_BENCH_DECOMPR_SIZE2,
  IDT_BENCH_DECOMPR_SPEED1,
  IDT_BENCH_DECOMPR_SPEED2,
  IDT_BENCH_DECOMPR_RATING1,
  IDT_BENCH_DECOMPR_RATING2,
  IDT_BENCH_DECOMPR_USAGE1,
  IDT_BENCH_DECOMPR_USAGE2,
  IDT_BENCH_DECOMPR_RPU1,
  IDT_BENCH_DECOMPR_RPU2,
  
  IDT_BENCH_TOTAL_USAGE_VAL,
  IDT_BENCH_TOTAL_RATING_VAL,
  IDT_BENCH_TOTAL_RPU_VAL
};
  

static const unsigned k_Ids_Enc_1[] = {
  IDT_BENCH_COMPRESS_USAGE1,
  IDT_BENCH_COMPRESS_SPEED1,
  IDT_BENCH_COMPRESS_RPU1,
  IDT_BENCH_COMPRESS_RATING1,
  IDT_BENCH_COMPRESS_SIZE1 };

static const unsigned k_Ids_Enc[] = {
  IDT_BENCH_COMPRESS_USAGE2,
  IDT_BENCH_COMPRESS_SPEED2,
  IDT_BENCH_COMPRESS_RPU2,
  IDT_BENCH_COMPRESS_RATING2,
  IDT_BENCH_COMPRESS_SIZE2 };

static const unsigned k_Ids_Dec_1[] = {
  IDT_BENCH_DECOMPR_USAGE1,
  IDT_BENCH_DECOMPR_SPEED1,
  IDT_BENCH_DECOMPR_RPU1,
  IDT_BENCH_DECOMPR_RATING1,
  IDT_BENCH_DECOMPR_SIZE1 };

static const unsigned k_Ids_Dec[] = {
  IDT_BENCH_DECOMPR_USAGE2,
  IDT_BENCH_DECOMPR_SPEED2,
  IDT_BENCH_DECOMPR_RPU2,
  IDT_BENCH_DECOMPR_RATING2,
  IDT_BENCH_DECOMPR_SIZE2 };

static const unsigned k_Ids_Tot[] = {
  IDT_BENCH_TOTAL_USAGE_VAL,
  0,
  IDT_BENCH_TOTAL_RPU_VAL,
  IDT_BENCH_TOTAL_RATING_VAL,
  0 };


void CBenchmarkDialog::MyKillTimer()
{
  if (_timer != 0)
  {
    KillTimer(kTimerID);
    _timer = 0;
  }
}


bool CBenchmarkDialog::OnDestroy()
{
  /* actually timer was removed before.
     also the timer must be removed by Windows, when window  will be removed. */
  MyKillTimer(); // it's optional code
  return false; // we return (false) to perform default dialog operation
}

void SetErrorMessage_MemUsage(UString &s, UInt64 reqSize, UInt64 ramSize, UInt64 ramLimit, const UString &usageString);

void CBenchmarkDialog::StartBenchmark()
{
  NeedRestart = false;
  WasStopped_in_GUI = false;

  SetItemText_Empty(IDT_BENCH_ERROR_MESSAGE);
  
  MyKillTimer(); // optional code. timer was killed before

  const size_t dict = OnChangeDictionary();
  const UInt32 numThreads = GetNumberOfThreads();
  const UInt32 numPasses = (UInt32)m_NumPasses.GetItemData_of_CurSel();

  for (unsigned i = 0; i < ARRAY_SIZE(g_IDs); i++)
    SetItemText(g_IDs[i], kProcessingString);

  SetItemText_Empty(IDT_BENCH_LOG);
  SetItemText_Empty(IDT_BENCH_ELAPSED_VAL);
  SetItemText_Empty(IDT_BENCH_ERROR_MESSAGE);
  
  const UInt64 memUsage = GetBenchMemoryUsage(numThreads, Sync.Level, dict,
      false); // totalBench
  if (!IsMemoryUsageOK(memUsage))
  {
    UString s2 = LangString(IDT_BENCH_MEMORY);
    if (s2.IsEmpty())
      GetItemText(IDT_BENCH_MEMORY, s2);
    UString s;
    SetErrorMessage_MemUsage(s, memUsage, RamSize, RamSize_Limit, s2);
    MessageBoxError_Status(s);
    return;
  }

  EnableItem(IDB_STOP, true);

  _startTime = GetTickCount();
  _finishTime = _startTime;
  _finishTime_WasSet = false;

  {
    NWindows::NSynchronization::CCriticalSectionLock lock(Sync.CS);
    InitSyncNew();
    Sync.DictSize = dict;
    Sync.NumThreads = numThreads;
    Sync.NumPasses_Limit = numPasses;
  }

  PrintTime();

  _timer = SetTimer(kTimerID, kTimerElapse);
  if (_thread.Create(CThreadBenchmark::MyThreadFunction, &_threadBenchmark) != 0)
  {
    MyKillTimer();
    MessageBoxError_Status(L"Can't create thread");
  };
  return;
}


void CBenchmarkDialog::RestartBenchmark()
{
  if (ExitWasAsked_in_GUI)
    return;

  if (_thread.IsCreated())
  {
    NeedRestart = true;
    SendExit_Status(L"Stop for restart ...");
  }
  else
    StartBenchmark();
}


void CBenchmarkDialog::Disable_Stop_Button()
{
  // if we disable focused button, then focus will be lost
  if (GetFocus() == GetItem(IDB_STOP))
  {
    // SendMsg_NextDlgCtl_Prev();
    SendMsg_NextDlgCtl_CtlId(IDB_RESTART);
  }
  EnableItem(IDB_STOP, false);
}


void CBenchmarkDialog::OnStopButton()
{
  if (ExitWasAsked_in_GUI)
    return;

  Disable_Stop_Button();

  WasStopped_in_GUI = true;
  if (_thread.IsCreated())
  {
    SendExit_Status(L"Stop ...");
  }
}



void CBenchmarkDialog::OnCancel()
{
  ExitWasAsked_in_GUI = true;
  
  /*
  SendMsg_NextDlgCtl_Prev();
  EnableItem(IDCANCEL, false);
  */

  if (_thread.IsCreated())
    SendExit_Status(L"Cancel ...");
  else
    CModalDialog::OnCancel();
}


void CBenchmarkDialog::OnHelp()
{
  ShowHelpWindow(kHelpTopic);
}



// void GetTimeString(UInt64 timeValue, wchar_t *s);

void CBenchmarkDialog::PrintTime()
{
  const UInt32 curTime =
    _finishTime_WasSet ?
      _finishTime :
      ::GetTickCount();

  const UInt32 elapsedTime = (curTime - _startTime);

  WCHAR s[64];

  // GetTimeString(elapsedTime / 1000, s);
  ConvertUInt32ToString(elapsedTime / 1000, s);

  if (_finishTime_WasSet)
  {
    WCHAR *p = s + MyStringLen(s);
    *p++ = '.';
    UINT_TO_STR_3(p, elapsedTime % 1000);
  }

  // NumberToDot3((UInt64)elapsedTime, s);

  wcscat(s, L" s");

  // if (WasStopped_in_GUI) wcscat(s, L" X"); // for debug

  if (s == ElapsedSec_Prev)
    return;

  ElapsedSec_Prev = s;

  // static cnt = 0; cnt++; wcscat(s, L" ");
  // UString s2; s2.Add_UInt32(cnt); wcscat(s, s2.Ptr());

  SetItemText(IDT_BENCH_ELAPSED_VAL, s);
}


static UInt64 GetMips(UInt64 ips)
{
  return (ips + 500000) / 1000000;
}


static UInt64 GetUsagePercents(UInt64 usage)
{
  return Benchmark_GetUsage_Percents(usage);
}


static UInt32 GetRating(const CTotalBenchRes &info)
{
  UInt64 numIter = info.NumIterations2;
  if (numIter == 0)
    numIter = 1000000;
  const UInt64 rating64 = GetMips(info.Rating / numIter);
  // return rating64;
  UInt32 rating32 = (UInt32)rating64;
  if (rating32 != rating64)
    rating32 = (UInt32)(Int32)-1;
  return rating32;
};


static void AddUsageString(UString &s, const CTotalBenchRes &info)
{
  UInt64 numIter = info.NumIterations2;
  if (numIter == 0)
    numIter = 1000000;
  UInt64 usage = GetUsagePercents(info.Usage / numIter);

  wchar_t w[64];
  ConvertUInt64ToString(usage, w);
  unsigned len = MyStringLen(w);
  while (len < 5)
  {
    s.Add_Space();
    len++;
  }
  s += w;
  s += "%";
}


static void Add_Dot3String(UString &s, UInt64 val)
{
  WCHAR temp[32];
  NumberToDot3(val, temp);
  s += temp;
}


static void AddRatingString(UString &s, const CTotalBenchRes &info)
{
  // AddUsageString(s, info);
  // s += " ";
  // s.Add_UInt32(GetRating(info));
  Add_Dot3String(s, GetRating(info));
};


static void AddRatingsLine(UString &s, const CTotalBenchRes &enc, const CTotalBenchRes &dec
    #ifdef PRINT_ITER_TIME
    , DWORD ticks
    #endif
    )
{
  // AddUsageString(s, enc); s += " ";

  AddRatingString(s, enc);
  s += "  ";
  AddRatingString(s, dec);
  
  CTotalBenchRes tot_BenchRes;
  tot_BenchRes.SetSum(enc, dec);
  
  s += "  ";
  AddRatingString(s, tot_BenchRes);
  
  s += " "; AddUsageString(s, tot_BenchRes);

  
  #ifdef PRINT_ITER_TIME
  s += " ";
  {
    Add_Dot3String(s, ticks;
    s += " s";
    // s.Add_UInt32(ticks); s += " ms";
  }
  #endif
}


void CBenchmarkDialog::PrintRating(UInt64 rating, UINT controlID)
{
  // SetItemText_Number(controlID, GetMips(rating), kMIPS);
  WCHAR s[64];
  NumberToDot3(GetMips(rating), s);
  MyStringCat(s, L" GIPS");
  SetItemText(controlID, s);
}

void CBenchmarkDialog::PrintUsage(UInt64 usage, UINT controlID)
{
  SetItemText_Number(controlID, GetUsagePercents(usage), TEXT("%"));
}


// void SetItemText_Number

void CBenchmarkDialog::PrintBenchRes(
    const CTotalBenchRes2 &info,
    const UINT ids[])
{
  if (info.NumIterations2 == 0)
    return;
  if (ids[1] != 0)
    SetItemText_Number(ids[1], (info.Speed >> 10) / info.NumIterations2, kKBs);
  PrintRating(info.Rating / info.NumIterations2, ids[3]);
  PrintRating(info.RPU / info.NumIterations2, ids[2]);
  PrintUsage(info.Usage / info.NumIterations2, ids[0]);
  if (ids[4] != 0)
  {
    UInt64 val = info.UnpackSize;
    LPCTSTR kPostfix;
    if (val >= ((UInt64)1 << 40))
    {
      kPostfix = kGB;
      val >>= 30;
    }
    else
    {
      kPostfix = kMB;
      val >>= 20;
    }
    SetItemText_Number(ids[4], val, kPostfix);
  }
}


// static UInt32 k_Message_Finished_cnt = 0;
// static UInt32 k_OnTimer_cnt = 0;

bool CBenchmarkDialog::OnMessage(UINT message, WPARAM wParam, LPARAM lParam)
{
  if (message != k_Message_Finished)
    return CModalDialog::OnMessage(message, wParam, lParam);

  {
    if (wParam == k_Msg_WPARM_Thread_Finished)
    {
      _finishTime = GetTickCount();
      _finishTime_WasSet = true;
      MyKillTimer();

      if (_thread.Wait_Close() != 0)
      {
        MessageBoxError_Status(L"Thread Wait Error");
      }

      if (!WasStopped_in_GUI)
      {
        WasStopped_in_GUI = true;
        Disable_Stop_Button();
      }

      HRESULT res = Sync.BenchFinish_Thread_HRESULT;
      if (res != S_OK)
      // if (!ExitWasAsked_in_GUI || res != E_ABORT)
        MessageBoxError_Status(HResultToMessage(res));

      if (ExitWasAsked_in_GUI)
      {
        // SetItemText(IDT_BENCH_ERROR_MESSAGE, "before CModalDialog::OnCancel()");
        // Sleep (2000);
        // MessageBoxError(L"test");
        CModalDialog::OnCancel();
        return true;
      }
    
      SetItemText_Empty(IDT_BENCH_ERROR_MESSAGE);

      res = Sync.BenchFinish_Task_HRESULT;
      if (res != S_OK)
      {
        if (!WasStopped_in_GUI || res != E_ABORT)
        {
          UString m;
          if (res == S_FALSE)
            m = "Decoding error";
          else if (res == CLASS_E_CLASSNOTAVAILABLE)
            m = "Can't find 7z.dll";
          else
            m = HResultToMessage(res);
          MessageBoxError_Status(m);
        }
      }

      if (NeedRestart)
      {
        StartBenchmark();
        return true;
      }
    }
    // k_Message_Finished_cnt++;
    UpdateGui();
    return true;
  }
}


bool CBenchmarkDialog::OnTimer(WPARAM timerID, LPARAM /* callback */)
{
  // k_OnTimer_cnt++;
  if (timerID == kTimerID)
    UpdateGui();
  return true;
}


void CBenchmarkDialog::UpdateGui()
{
  PrintTime();

  if (TotalMode)
  {
    bool wasChanged = false;
    {
      NWindows::NSynchronization::CCriticalSectionLock lock(Sync.CS);
      
      if (Sync.TextWasChanged)
      {
        wasChanged = true;
        Bench2Text += Sync.Text;
        Sync.Text.Empty();
        Sync.TextWasChanged = false;
      }
    }
    if (wasChanged)
      _consoleEdit.SetText(Bench2Text);
    return;
  }

  CSyncData sd;
  CRecordVector<CBenchPassResult> RatingVector;

  {
    NWindows::NSynchronization::CCriticalSectionLock lock(Sync.CS);
    sd = Sync.sd;

    if (sd.NeedPrint_RatingVector)
      RatingVector = Sync.RatingVector;
    
    if (sd.NeedPrint_Freq)
    {
      Sync.FreqString_GUI = Sync.FreqString_Sync;
      sd.NeedPrint_RatingVector = true;
    }

    Sync.sd.NeedPrint_RatingVector = false;
    Sync.sd.NeedPrint_Enc_1 = false;
    Sync.sd.NeedPrint_Enc   = false;
    Sync.sd.NeedPrint_Dec_1 = false;
    Sync.sd.NeedPrint_Dec   = false;
    Sync.sd.NeedPrint_Tot   = false;
    Sync.sd.NeedPrint_Freq = false;
  }

  if (sd.NumPasses_Finished != NumPasses_Finished_Prev)
  {
    SetItemText_Number(IDT_BENCH_PASSES_VAL, sd.NumPasses_Finished, TEXT(" /"));
    NumPasses_Finished_Prev = sd.NumPasses_Finished;
  }

  if (sd.NeedPrint_Enc_1) PrintBenchRes(sd.Enc_BenchRes_1, k_Ids_Enc_1);
  if (sd.NeedPrint_Enc)   PrintBenchRes(sd.Enc_BenchRes,   k_Ids_Enc);
  if (sd.NeedPrint_Dec_1) PrintBenchRes(sd.Dec_BenchRes_1, k_Ids_Dec_1);
  if (sd.NeedPrint_Dec)   PrintBenchRes(sd.Dec_BenchRes,   k_Ids_Dec);
 
  if (sd.BenchWasFinished && sd.NeedPrint_Tot)
  {
    CTotalBenchRes2 tot_BenchRes = sd.Enc_BenchRes;
    tot_BenchRes.Update_With_Res2(sd.Dec_BenchRes);
    PrintBenchRes(tot_BenchRes, k_Ids_Tot);
  }


  if (sd.NeedPrint_RatingVector)
  // for (unsigned k = 0; k < 1; k++)
  {
    UString s;
    s += Sync.FreqString_GUI;
    if (!RatingVector.IsEmpty())
    {
      if (!s.IsEmpty())
        s.Add_LF();
      s += "Compr Decompr Total   CPU"
          #ifdef PRINT_ITER_TIME
          "   Time"
          #endif
          ;
      s.Add_LF();
    }
    // s += "GIPS    GIPS   GIPS    %   s"; s.Add_LF();
    for (unsigned i = 0; i < RatingVector.Size(); i++)
    {
      if (i != 0)
        s.Add_LF();
      if ((int)i == sd.RatingVector_DeletedIndex)
      {
        s += "...";
        s.Add_LF();
      }
      const CBenchPassResult &pair = RatingVector[i];
      /*
        s += "g:"; s.Add_UInt32((UInt32)pair.EncInfo.GlobalTime);
        s += " u:"; s.Add_UInt32((UInt32)pair.EncInfo.UserTime);
        s += " ";
      */
      AddRatingsLine(s, pair.Enc, pair.Dec
            #ifdef PRINT_ITER_TIME
            , pair.Ticks
            #endif
            );
      /*
      {
        UInt64 v = i + 1;
        if (sd.RatingVector_DeletedIndex >= 0 && i >= (unsigned)sd.RatingVector_DeletedIndex)
          v += sd.RatingVector_NumDeleted;
        char temp[64];
        ConvertUInt64ToString(v, temp);
        s += " : ";
        s += temp;
      }
      */
    }

    if (sd.BenchWasFinished)
    {
      s.Add_LF();
      s += "-------------";
      s.Add_LF();
      {
        // average time is not correct because of freq detection in first iteration
        AddRatingsLine(s, sd.Enc_BenchRes, sd.Dec_BenchRes
              #ifdef PRINT_ITER_TIME
              , (DWORD)(sd.TotalTicks / (sd.NumPasses_Finished ? sd.NumPasses_Finished : 1))
              #endif
              );
      }
    }
    // s.Add_LF(); s += "OnTimer: "; s.Add_UInt32(k_OnTimer_cnt);
    // s.Add_LF(); s += "finished Message: "; s.Add_UInt32(k_Message_Finished_cnt);
    // static cnt = 0; cnt++; s.Add_LF(); s += "Print: "; s.Add_UInt32(cnt);
    // s.Add_LF(); s += "NumEncProgress: "; s.Add_UInt32((UInt32)sd.NumEncProgress);
    // s.Add_LF(); s += "NumDecProgress: "; s.Add_UInt32((UInt32)sd.NumDecProgress);
    SetItemText(IDT_BENCH_LOG, s);
  }
}


bool CBenchmarkDialog::OnCommand(int code, int itemID, LPARAM lParam)
{
  if (code == CBN_SELCHANGE &&
      (itemID == IDC_BENCH_DICTIONARY ||
       itemID == IDC_BENCH_NUM_PASSES ||
       itemID == IDC_BENCH_NUM_THREADS))
  {
    RestartBenchmark();
    return true;
  }
  return CModalDialog::OnCommand(code, itemID, lParam);
}


bool CBenchmarkDialog::OnButtonClicked(int buttonID, HWND buttonHWND)
{
  switch (buttonID)
  {
    case IDB_RESTART:
      RestartBenchmark();
      return true;
    case IDB_STOP:
      OnStopButton();
      return true;
  }
  return CModalDialog::OnButtonClicked(buttonID, buttonHWND);
}





// ---------- Benchmark Thread ----------

struct CBenchCallback: public IBenchCallback
{
  UInt64 dictionarySize;
  CBenchProgressSync *Sync;
  CBenchmarkDialog *BenchmarkDialog;
  
  HRESULT SetEncodeResult(const CBenchInfo &info, bool final);
  HRESULT SetDecodeResult(const CBenchInfo &info, bool final);
};

HRESULT CBenchCallback::SetEncodeResult(const CBenchInfo &info, bool final)
{
  bool needPost = false;
  {
    NSynchronization::CCriticalSectionLock lock(Sync->CS);
    if (Sync->Exit)
      return E_ABORT;
    CSyncData &sd = Sync->sd;
    // sd.NumEncProgress++;
    CTotalBenchRes2 &br = sd.Enc_BenchRes_1;
    {
      UInt64 dictSize = Sync->DictSize;
      if (final)
      {
        // sd.EncInfo = info;
      }
      else
      {
        /* if (!final), then CBenchInfo::NumIterations means totalNumber of threads.
           so we can reduce the dictionary */
        if (dictSize > info.UnpackSize)
          dictSize = info.UnpackSize;
      }
      br.Rating = info.GetRating_LzmaEnc(dictSize);
    }
    br.SetFrom_BenchInfo(info);
    sd.NeedPrint_Enc_1 = true;
    if (final)
    {
      sd.Enc_BenchRes.Update_With_Res2(br);
      sd.NeedPrint_Enc = true;
      needPost = true;
    }
  }

  if (needPost)
    BenchmarkDialog->PostMsg(k_Message_Finished, k_Msg_WPARM_Enc1_Finished);

  return S_OK;
}


HRESULT CBenchCallback::SetDecodeResult(const CBenchInfo &info, bool final)
{
  NSynchronization::CCriticalSectionLock lock(Sync->CS);
  if (Sync->Exit)
    return E_ABORT;
  CSyncData &sd = Sync->sd;
  // sd.NumDecProgress++;
  CTotalBenchRes2 &br = sd.Dec_BenchRes_1;
  br.Rating = info.GetRating_LzmaDec();
  br.SetFrom_BenchInfo(info);
  sd.NeedPrint_Dec_1 = true;
  if (final)
    sd.Dec_BenchRes.Update_With_Res2(br);
  return S_OK;
}


struct CBenchCallback2: public IBenchPrintCallback
{
  CBenchProgressSync *Sync;
  bool TotalMode;

  void Print(const char *s);
  void NewLine();
  HRESULT CheckBreak();
};

void CBenchCallback2::Print(const char *s)
{
  if (TotalMode)
  {
    NSynchronization::CCriticalSectionLock lock(Sync->CS);
    Sync->Text += s;
    Sync->TextWasChanged = true;
  }
}

void CBenchCallback2::NewLine()
{
  Print("\xD\n");
}

HRESULT CBenchCallback2::CheckBreak()
{
  if (Sync->Exit)
    return E_ABORT;
  return S_OK;
}



struct CFreqCallback: public IBenchFreqCallback
{
  CBenchmarkDialog *BenchmarkDialog;

  virtual HRESULT AddCpuFreq(unsigned numThreads, UInt64 freq, UInt64 usage);
  virtual HRESULT FreqsFinished(unsigned numThreads);
};

HRESULT CFreqCallback::AddCpuFreq(unsigned numThreads, UInt64 freq, UInt64 usage)
{
  HRESULT res;
  {
    CBenchProgressSync &sync = BenchmarkDialog->Sync;
    NSynchronization::CCriticalSectionLock lock(sync.CS);
    UString &s = sync.FreqString_Sync;
    if (sync.NumFreqThreadsPrev != numThreads)
    {
      sync.NumFreqThreadsPrev = numThreads;
      if (!s.IsEmpty())
        s.Add_LF();
      s.Add_UInt32(numThreads);
      s += "T Frequency (MHz):";
      s.Add_LF();
    }
    s += " ";
    if (numThreads != 1)
    {
      s.Add_UInt64(GetUsagePercents(usage));
      s += '%';
      s.Add_Space();
    }
    s.Add_UInt64(GetMips(freq));
    // BenchmarkDialog->Sync.sd.NeedPrint_Freq = true;
    res = sync.Exit ? E_ABORT : S_OK;
  }
  // BenchmarkDialog->PostMsg(k_Message_Finished, k_Msg_WPARM_Enc1_Finished);
  return res;
}

HRESULT CFreqCallback::FreqsFinished(unsigned /* numThreads */)
{
  HRESULT res;
  {
    CBenchProgressSync &sync = BenchmarkDialog->Sync;
    NSynchronization::CCriticalSectionLock lock(sync.CS);
    sync.sd.NeedPrint_Freq = true;
    BenchmarkDialog->PostMsg(k_Message_Finished, k_Msg_WPARM_Enc1_Finished);
    res = sync.Exit ? E_ABORT : S_OK;
  }
  BenchmarkDialog->PostMsg(k_Message_Finished, k_Msg_WPARM_Enc1_Finished);
  return res;
}



// define USE_DUMMY only for debug
// #define USE_DUMMY
#ifdef USE_DUMMY
static unsigned dummy = 1;
static unsigned Dummy(unsigned limit)
{
  unsigned sum = 0;
  for (unsigned k = 0; k < limit; k++)
  {
    sum += dummy;
    if (sum == 0)
      break;
  }
  return sum;
}
#endif


HRESULT CThreadBenchmark::Process()
{
  /* the first benchmark pass can be slow,
     if we run benchmark while the window is being created,
     and (no freq detecion loop) && (dictionary is small) (-mtic is small) */
    
  // Sleep(300); // for debug
  #ifdef USE_DUMMY
  Dummy(1000 * 1000 * 1000); // for debug
  #endif

  CBenchProgressSync &sync = BenchmarkDialog->Sync;
  HRESULT finishHRESULT = S_OK;
  
  try
  {
    for (UInt32 passIndex = 0;; passIndex++)
    {
      // throw 1; // to debug
      // throw CSystemException(E_INVALIDARG); // to debug

      UInt64 dictionarySize;
      UInt32 numThreads;
      {
        NSynchronization::CCriticalSectionLock lock(sync.CS);
        if (sync.Exit)
          break;
        dictionarySize = sync.DictSize;
        numThreads = sync.NumThreads;
      }

      #ifdef PRINT_ITER_TIME
      const DWORD startTick = GetTickCount();
      #endif
      
      CBenchCallback callback;
      
      callback.dictionarySize = dictionarySize;
      callback.Sync = &sync;
      callback.BenchmarkDialog = BenchmarkDialog;
      
      CBenchCallback2 callback2;
      callback2.TotalMode = BenchmarkDialog->TotalMode;
      callback2.Sync = &sync;
      
      CFreqCallback freqCallback;
      freqCallback.BenchmarkDialog = BenchmarkDialog;

      HRESULT result;
     
      try
      {
        CObjectVector<CProperty> props;

        props = BenchmarkDialog->Props;

        if (BenchmarkDialog->TotalMode)
        {
          props = BenchmarkDialog->Props;
        }
        else
        {
          {
            CProperty prop;
            prop.Name = "mt";
            prop.Value.Add_UInt32(numThreads);
            props.Add(prop);
          }
          {
            CProperty prop;
            prop.Name = 'd';
            prop.Name.Add_UInt32((UInt32)(dictionarySize >> 10));
            prop.Name += 'k';
            props.Add(prop);
          }
        }
        
        result = Bench(EXTERNAL_CODECS_LOC_VARS
            BenchmarkDialog->TotalMode ? &callback2 : NULL,
            BenchmarkDialog->TotalMode ? NULL : &callback,
            props, 1, false,
            (!BenchmarkDialog->TotalMode) && passIndex == 0 ? &freqCallback: NULL);
        
        // result = S_FALSE; // for debug;
        // throw 1;
      }
      catch(...)
      {
        result = E_FAIL;
      }

      #ifdef PRINT_ITER_TIME
      const DWORD numTicks = GetTickCount() - startTick;
      #endif

      bool finished = true;

      NSynchronization::CCriticalSectionLock lock(sync.CS);

      if (result != S_OK)
      {
        sync.BenchFinish_Task_HRESULT = result;
        break;
      }

      {
        CSyncData &sd = sync.sd;

        sd.NumPasses_Finished++;
        #ifdef PRINT_ITER_TIME
        sd.TotalTicks += numTicks;
        #endif

        if (BenchmarkDialog->TotalMode)
          break;

        {
          CTotalBenchRes tot_BenchRes = sd.Enc_BenchRes_1;
          tot_BenchRes.Update_With_Res(sd.Dec_BenchRes_1);

          sd.NeedPrint_RatingVector = true;
          {
            CBenchPassResult pair;
            // pair.EncInfo = sd.EncInfo; // for debug
            pair.Enc = sd.Enc_BenchRes_1;
            pair.Dec = sd.Dec_BenchRes_1;
            #ifdef PRINT_ITER_TIME
            pair.Ticks = numTicks;
            #endif
            sync.RatingVector.Add(pair);
            // pair.Dec_Defined = true;
          }
        }
          
        sd.NeedPrint_Dec = true;
        sd.NeedPrint_Tot = true;

        if (sync.RatingVector.Size() > kRatingVector_NumBundlesMax)
        {
          // sd.RatingVector_NumDeleted++;
          sd.RatingVector_DeletedIndex = (int)(kRatingVector_NumBundlesMax / 4);
          sync.RatingVector.Delete((unsigned)(sd.RatingVector_DeletedIndex));
        }

        if (sync.sd.NumPasses_Finished < sync.NumPasses_Limit)
          finished = false;
        else
        {
          sync.sd.BenchWasFinished = true;
          // BenchmarkDialog->_finishTime = GetTickCount();
          // return 0;
        }
      }

      if (BenchmarkDialog->TotalMode)
        break;

      /*
      if (newTick - prevTick < 1000)
        numSameTick++;
      if (numSameTick > 5 || finished)
      {
        prevTick = newTick;
        numSameTick = 0;
      */
      // for (unsigned i = 0; i < 1; i++)
      {
        // we suppose that PostMsg messages will be processed in order.
        if (!BenchmarkDialog->PostMsg_Finish(k_Msg_WPARM_Iter_Finished))
        {
          finished = true;
          finishHRESULT = E_FAIL;
          // throw 1234567;
        }
      }
      if (finished)
        break;
    }
    // return S_OK;
  }
  catch(CSystemException &e)
  {
    finishHRESULT = e.ErrorCode;
    // BenchmarkDialog->MessageBoxError(HResultToMessage(e.ErrorCode));
    // return E_FAIL;
  }
  catch(...)
  {
    finishHRESULT = E_FAIL;
    // BenchmarkDialog->MessageBoxError(HResultToMessage(E_FAIL));
    // return E_FAIL;
  }

  if (finishHRESULT != S_OK)
  {
    NSynchronization::CCriticalSectionLock lock(sync.CS);
    sync.BenchFinish_Thread_HRESULT = finishHRESULT;
  }
  if (!BenchmarkDialog->PostMsg_Finish(k_Msg_WPARM_Thread_Finished))
  {
    // sync.BenchFinish_Thread_HRESULT = E_FAIL;
  }
  return 0;
}



static void ParseNumberString(const UString &s, NCOM::CPropVariant &prop)
{
  const wchar_t *end;
  UInt64 result = ConvertStringToUInt64(s, &end);
  if (*end != 0 || s.IsEmpty())
    prop = s;
  else if (result <= (UInt32)0xFFFFFFFF)
    prop = (UInt32)result;
  else
    prop = result;
}


HRESULT Benchmark(
    DECL_EXTERNAL_CODECS_LOC_VARS
    const CObjectVector<CProperty> &props, UInt32 numIterations, HWND hwndParent)
{
  CBenchmarkDialog bd;

  bd.TotalMode = false;
  bd.Props = props;
  if (numIterations == 0)
    numIterations = 1;
  bd.Sync.NumPasses_Limit = numIterations;
  bd.Sync.DictSize = (UInt64)(Int64)-1;
  bd.Sync.NumThreads = (UInt32)(Int32)-1;
  bd.Sync.Level = -1;

  COneMethodInfo method;

  UInt32 numCPUs = 1;
  #ifndef _7ZIP_ST
  numCPUs = NSystem::GetNumberOfProcessors();
  #endif
  UInt32 numThreads = numCPUs;

  FOR_VECTOR (i, props)
  {
    const CProperty &prop = props[i];
    UString name = prop.Name;
    name.MakeLower_Ascii();
    if (name.IsEqualTo_Ascii_NoCase("m") && prop.Value == L"*")
    {
      bd.TotalMode = true;
      continue;
    }

    NCOM::CPropVariant propVariant;
    if (!prop.Value.IsEmpty())
      ParseNumberString(prop.Value, propVariant);
    if (name.IsPrefixedBy(L"mt"))
    {
      #ifndef _7ZIP_ST
      RINOK(ParseMtProp(name.Ptr(2), propVariant, numCPUs, numThreads));
      if (numThreads != numCPUs)
        bd.Sync.NumThreads = numThreads;
      #endif
      continue;
    }
    /*
    if (name.IsEqualTo("time"))
    {
      // UInt32 testTime = 4;
      // RINOK(ParsePropToUInt32(L"", propVariant, testTime));
      continue;
    }
    RINOK(method.ParseMethodFromPROPVARIANT(name, propVariant));
    */
    // here we need to parse DictSize property, and ignore unknown properties
    method.ParseMethodFromPROPVARIANT(name, propVariant);
  }

  if (bd.TotalMode)
  {
    // bd.Bench2Text.Empty();
    bd.Bench2Text = "7-Zip " MY_VERSION_CPU;
    bd.Bench2Text += (char)0xD;
    bd.Bench2Text.Add_LF();
  }

  {
    UInt64 dict;
    if (method.Get_DicSize(dict))
      bd.Sync.DictSize = dict;
  }
  bd.Sync.Level = method.GetLevel();

  // Dummy(1000 * 1000 * 1);

  {
    CThreadBenchmark &benchmarker = bd._threadBenchmark;
    #ifdef EXTERNAL_CODECS
    benchmarker.__externalCodecs = __externalCodecs;
    #endif
    benchmarker.BenchmarkDialog = &bd;
  }

  bd.Create(hwndParent);

  return S_OK;
}


CBenchmarkDialog::~CBenchmarkDialog()
{
  if (_thread.IsCreated())
  {
    /* the following code will be not executed in normal code flow.
       it can be called, if there is some internal failure in dialog code. */
    Attach(NULL);
    MessageBoxError(L"The flaw in benchmark thread code");
    Sync.SendExit();
    _thread.Wait_Close();
  }
}