aboutsummaryrefslogtreecommitdiff
path: root/src/tools/perf_replies.c
blob: 9f2a53056f14404b7614f3aeaebfb4e8f806c673 (plain) (blame)
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
/*
    This file is part of GNU libmicrohttpd
    Copyright (C) 2023 Evgeny Grin (Karlson2k)

    Redistribution and use in source and binary forms, with or without
    modification, are permitted provided that the following conditions
    are met:
    1. Redistributions of source code must retain the above copyright
       notice unmodified, this list of conditions and the following
       disclaimer.
    2. Redistributions in binary form must reproduce the above copyright
       notice, this list of conditions and the following disclaimer in
       the documentation and/or other materials provided with the
       distribution.

    THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR
    IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
    OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
    IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
    INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
    (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
    LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
    ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
    (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
    THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

/**
 * @file tools/perf_replies.c
 * @brief  Implementation of HTTP server optimised for fast replies
 *         based on MHD.
 * @author Karlson2k (Evgeny Grin)
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include "mhd_options.h"
#include "microhttpd.h"
#include "mhd_tool_str_to_uint.h"
#include "mhd_tool_get_cpu_count.h"

#if defined(MHD_REAL_CPU_COUNT)
#if MHD_REAL_CPU_COUNT == 0
#undef MHD_REAL_CPU_COUNT
#endif /* MHD_REAL_CPU_COUNT == 0 */
#endif /* MHD_REAL_CPU_COUNT */

#define PERF_RPL_ERR_CODE_BAD_PARAM 65

#ifndef MHD_STATICSTR_LEN_
/**
 * Determine length of static string / macro strings at compile time.
 */
#define MHD_STATICSTR_LEN_(macro) (sizeof(macro) / sizeof(char) - 1)
#endif /* ! MHD_STATICSTR_LEN_ */

/* Static constants */
static const char *const tool_copyright =
  "Copyright (C) 2023 Evgeny Grin (Karlson2k)";

/* Package or build specific string, like
   "Debian 1.2.3-4" or "RevX, built by MSYS2" */
static const char *const build_revision = ""
#ifdef MHD_BUILD_REV_STR
                                          MHD_BUILD_REV_STR
#endif /* MHD_BUILD_REV_STR */
;

#define PERF_REPL_PORT_FALLBACK 48080

/* Dynamic variables */
static char self_name[500] = "perf_replies";
static uint16_t mhd_port = 0;

static void
set_self_name (int argc, char *const *argv)
{
  if ((argc >= 1) && (NULL != argv[0]))
  {
    const char *last_dir_sep;
    last_dir_sep = strrchr (argv[0], '/');
#ifdef _WIN32
    if (1)
    {
      const char *last_w32_dir_sep;
      last_w32_dir_sep = strrchr (argv[0], '\\');
      if ((NULL == last_dir_sep) ||
          ((NULL != last_w32_dir_sep) && (last_w32_dir_sep > last_dir_sep)))
        last_dir_sep = last_w32_dir_sep;
    }
#endif /* _WIN32 */
    if (NULL != last_dir_sep)
    {
      size_t name_len;
      name_len = strlen (last_dir_sep + 1);
      if ((0 != name_len) && ((sizeof(self_name) / sizeof(char)) > name_len))
      {
        strcpy (self_name, last_dir_sep + 1);
        return;
      }
    }
  }
  /* Set default name */
  strcpy (self_name, "perf_replies");
  return;
}


#if defined (HAVE_POPEN) && defined(HAVE_PCLOSE)
/**
 * Read the command output as a number and return the number.
 * Only positive decimal numbers are supported
 * @param cmd the command to run
 * @return zero or positive number read if success,
 *         negative number if any error occurs
 */
static int
get_cmd_out_as_number (const char *cmd)
{
  FILE *cmd_out;
  char buf[255];
  int ret;
  size_t len;

  cmd_out = popen (cmd, "r"
#ifdef _WIN32
                   "t"
#endif /* _WIN32 */
                   );
  if (NULL == cmd_out)
    return -1;
  ret = -1;
  if (buf != fgets (buf, sizeof(buf), cmd_out))
    len = 0;
  else
    len = strlen (buf);
  if ((0 != len) && (sizeof(buf) > (len + 2)) && ! ferror (cmd_out))
  {
    size_t digits_found;
    unsigned int out_value;
    digits_found = mhd_tool_str_to_uint (buf, &out_value);
    if (0 != digits_found)
    {
      if ((0 == buf[digits_found])
#ifdef _WIN32
          || ('\r' == buf[digits_found])
#endif /* _WIN32 */
          || ('\n' == buf[digits_found]))
      {
        ret = (int) out_value; /* Possible negative cast result is interpreted as an error */
      }
    }
  }
  if (0 != pclose (cmd_out))
    ret = -1;
  return ret;
}


#else  /* ! HAVE_POPEN || ! HAVE_PCLOSE */
#define read_cmd_out_as_number(ignore) (-1)
#endif /* ! HAVE_POPEN || ! HAVE_PCLOSE */

static unsigned int
detect_cpu_core_count (void)
{
  int sys_cpu_count;
  sys_cpu_count = mhd_tool_get_system_cpu_count ();
  if (0 >= sys_cpu_count)
  {
    int proc_cpu_count;
    fprintf (stderr, "Failed to detect the number of logical CPU cores "
             "available on the system.\n");
    proc_cpu_count = mhd_tool_get_proc_cpu_count ();
    if (0 < proc_cpu_count)
    {
      fprintf (stderr, "The number of CPU cores available for this process "
               "is used as a fallback.\n");
      sys_cpu_count = proc_cpu_count;
    }
#ifdef MHD_REAL_CPU_COUNT
    if (0 >= sys_cpu_count)
    {
      fprintf (stderr, "configure-detected hardcoded number is used "
               "as a fallback.\n");
      sys_cpu_count = MHD_REAL_CPU_COUNT;
    }
#endif
    if (0 >= sys_cpu_count)
      sys_cpu_count = 1;
    printf ("Assuming %d logical CPU core%s on this system.\n", sys_cpu_count,
            (1 == sys_cpu_count) ? "" : "s");
  }
  else
  {
    printf ("Detected %d logical CPU core%s on this system.\n", sys_cpu_count,
            (1 == sys_cpu_count) ? "" : "s");
  }
  return (unsigned int) sys_cpu_count;
}


static unsigned int
get_cpu_core_count (void)
{
  static unsigned int num_cpu_cores = 0;
  if (0 == num_cpu_cores)
    num_cpu_cores = detect_cpu_core_count ();
  return num_cpu_cores;
}


static unsigned int
detect_process_cpu_core_count (void)
{
  unsigned int num_proc_cpu_cores;
  unsigned int sys_cpu_cores;
  int res;

  sys_cpu_cores = get_cpu_core_count ();
  res = mhd_tool_get_proc_cpu_count ();
  if (0 > res)
  {
    fprintf (stderr, "Cannot detect the number of logical CPU cores available "
             "for this process.\n");
    if (1 != sys_cpu_cores)
      printf ("Assuming all %u system logical CPU cores are available to run "
              "threads of this process.\n", sys_cpu_cores);
    else
      printf ("Assuming single logical CPU core available for this process.\n");
    num_proc_cpu_cores = sys_cpu_cores;
  }
  else
  {
    printf ("Detected %d logical CPU core%s available to run threads "
            "of this process.\n", res, (1 == res) ? "" : "s");
    num_proc_cpu_cores = (unsigned int) res;
  }
  if (num_proc_cpu_cores > sys_cpu_cores)
  {
    fprintf (stderr, "WARNING: Detected number of CPU cores available "
             "for this process (%u) is larger than detected number "
             "of CPU cores on the system (%u).\n",
             num_proc_cpu_cores, sys_cpu_cores);
    num_proc_cpu_cores = sys_cpu_cores;
    fprintf (stderr, "Using %u as the number of logical CPU cores available "
             "for this process.\n", num_proc_cpu_cores);
  }
  return num_proc_cpu_cores;
}


static unsigned int
get_process_cpu_core_count (void)
{
  static unsigned int proc_num_cpu_cores = 0;
  if (0 == proc_num_cpu_cores)
    proc_num_cpu_cores = detect_process_cpu_core_count ();
  return proc_num_cpu_cores;
}


static unsigned int num_threads = 0;

static unsigned int
get_num_threads (void)
{
  static const unsigned int max_threads = 32;
  if (0 < num_threads)
    return num_threads;

  num_threads = get_cpu_core_count () / 2;
  if (0 == num_threads)
    num_threads = 1;
  else
  {
    unsigned int num_proc_cpus;
    num_proc_cpus = get_process_cpu_core_count ();
    if (num_proc_cpus >= num_threads)
    {
      printf ("Using half of all available CPU cores, assuming the other half "
              "is used by client / requests generator.\n");
    }
    else
    {
      printf ("Using all CPU cores available for this process as more than "
              "half of CPU cores on this system are still available for use "
              "by client / requests generator.\n");
      num_threads = num_proc_cpus;
    }
  }
  if (max_threads < num_threads)
  {
    printf ("Number of threads are limited to %u as more threads "
            "are unlikely to improve the performance.\n", max_threads);
    num_threads = max_threads;
  }

  return num_threads;
}


/**
 * The result of short parameters processing
 */
enum PerfRepl_param_result
{
  PERF_RPL_PARAM_ERROR,        /**< Error processing parameter */
  PERF_RPL_PARAM_ONE_CHAR,     /**< Processed exactly one character */
  PERF_RPL_PARAM_FULL_STR,     /**< Processed current parameter completely */
  PERF_RPL_PARAM_STR_PLUS_NEXT /**< Current parameter completely and next parameter processed */
};

/**
 * Extract parameter value
 * @param param_name the name of the parameter
 * @param param_tail the pointer to the character after parameter name in
 *                   the parameter string
 * @param next_param the pointer to the next parameter (if any) or NULL
 * @param[out] param_value the pointer where to store resulting value
 * @return enum value, the PERF_PERPL_SPARAM_ONE_CHAR is not used by
 *                     this function
 */
static enum PerfRepl_param_result
get_param_value (const char *param_name, const char *param_tail,
                 const char *next_param, unsigned int *param_value)
{
  const char *value_str;
  size_t digits;
  if (0 != param_tail[0])
  {
    if ('=' != param_tail[0])
      value_str = param_tail;
    else
      value_str = param_tail + 1;
  }
  else
    value_str = next_param;

  if (NULL != value_str)
    digits = mhd_tool_str_to_uint (value_str, param_value);
  else
    digits = 0;

  if ((0 == digits) || (0 != value_str[digits]))
  {
    fprintf (stderr, "Parameter '%s' is not followed by valid number.\n",
             param_name);
    return PERF_RPL_PARAM_ERROR;
  }

  if (0 != param_tail[0])
    return PERF_RPL_PARAM_FULL_STR;

  return PERF_RPL_PARAM_STR_PLUS_NEXT;
}


static void
show_help (void)
{
  printf ("Usage: %s [OPTIONS] [PORT_NUMBER]\n", self_name);
  printf ("Start MHD-based web-server optimised for fast replies.\n");
  printf ("\n");
  printf ("Threads options (mutually exclusive):\n");
  printf ("  -A,     --all-cpus        use all available CPU cores (for \n"
          "                            testing with remote client)\n");
  printf ("  -t NUM, --threads=NUM     use NUM threads\n");
  printf ("  -P,     --thread-per-conn use thread-per-connection mode,\n"
          "                            the number of threads are limited only\n"
          "                            by the number of connection\n");
  printf ("\n");
  printf ("Force polling function (mutually exclusive):\n");
  if (MHD_NO != MHD_is_feature_supported (MHD_FEATURE_EPOLL))
    printf ("  -e,     --epoll           use 'epoll' functionality\n");
  if (MHD_NO != MHD_is_feature_supported (MHD_FEATURE_POLL))
    printf ("  -p,     --poll            use poll() function\n");
  printf ("  -s,     --select          use select() function\n");
  printf ("\n");
  printf ("Response body size options (mutually exclusive):\n");
  printf ("  -E,     --empty           empty response, 0 bytes\n");
  printf ("  -T,     --tiny            tiny response, 3 bytes (default)\n");
  printf ("  -M,     --medium          medium response, 8 KB\n");
  printf ("  -L,     --large           large response, 1 MB\n");
  printf ("\n");
  printf ("Response use options (mutually exclusive):\n");
  printf ("  -S,     --shared          pool of pre-generated shared response\n"
          "                            objects (default)\n");
  printf ("  -I,     --single          single pre-generated response object\n"
          "                            used for all requests\n");
  printf ("  -U,     --unique          response object generated for every\n"
          "                            request and used one time only\n");
  printf ("\n");
  printf ("Other options:\n");
  printf ("  -c NUM, --connections=NUM reject more than NUM client \n"
          "                            connections\n");
  printf ("  -O NUM, --timeout=NUM     set connection timeout to NUM seconds,\n"
          "                            zero means no timeout\n");
  printf ("          --date-header     use the 'Date:' header in every\n"
          "                            reply\n");
  printf ("          --help            display this help and exit\n");
  printf ("  -V,     --version         output version information and exit\n");
  printf ("\n");
  printf ("This tool is part of GNU libmicrohttpd suite.\n");
  printf ("%s\n", tool_copyright);
}


struct PerfRepl_parameters
{
  unsigned int port;
  int all_cpus;
  unsigned int threads;
  int thread_per_conn;
  int epoll;
  int poll;
  int select;
  int empty;
  int tiny;
  int medium;
  int large;
  int shared;
  int single;
  int unique;
  unsigned int connections;
  unsigned int timeout;
  int date_header;
  int help;
  int version;
};

static struct PerfRepl_parameters tool_params = {
  0,
  0,
  0,
  0,
  0,
  0,
  0,
  0,
  0,
  0,
  0,
  0,
  0,
  0,
  0,
  0,
  0,
  0,
  0
};


static enum PerfRepl_param_result
process_param__all_cpus (const char *param_name)
{
  if (0 != tool_params.threads)
  {
    fprintf (stderr, "Parameter '%s' cannot be used together "
             "with '-t' or '--threads'.\n", param_name);
    return PERF_RPL_PARAM_ERROR;
  }
  if (tool_params.thread_per_conn)
  {
    fprintf (stderr, "Parameter '%s' cannot be used together "
             "with '-P' or '--thread-per-conn'.\n", param_name);
    return PERF_RPL_PARAM_ERROR;
  }
  tool_params.all_cpus = ! 0;
  return '-' == param_name[1] ?
         PERF_RPL_PARAM_FULL_STR :PERF_RPL_PARAM_ONE_CHAR;
}


/**
 * Process parameter '-t' or '--threads'
 * @param param_name the name of the parameter as specified in command line
 * @param param_tail the pointer to the character after parameter name in
 *                   the parameter string
 * @param next_param the pointer to the next parameter (if any) or NULL
 * @return enum value, the PERF_PERPL_SPARAM_ONE_CHAR is not used by
 *                     this function
 */
static enum PerfRepl_param_result
process_param__threads (const char *param_name, const char *param_tail,
                        const char *next_param)
{
  unsigned int param_value;
  enum PerfRepl_param_result value_res;

  if (tool_params.all_cpus)
  {
    fprintf (stderr, "Parameter '%s' cannot be used together "
             "with '-A' or '--all-cpus'.\n", param_name);
    return PERF_RPL_PARAM_ERROR;
  }
  if (tool_params.thread_per_conn)
  {
    fprintf (stderr, "Parameter '%s' cannot be used together "
             "with '-P' or '--thread-per-conn'.\n", param_name);
    return PERF_RPL_PARAM_ERROR;
  }
  value_res = get_param_value (param_name, param_tail, next_param,
                               &param_value);
  if (PERF_RPL_PARAM_ERROR == value_res)
    return value_res;

  if (0 == param_value)
  {
    fprintf (stderr, "'0' is not valid value for parameter '%s'.\n",
             param_name);
    return PERF_RPL_PARAM_ERROR;
  }
  tool_params.threads = param_value;
  return value_res;
}


static enum PerfRepl_param_result
process_param__thread_per_conn (const char *param_name)
{
  if (tool_params.all_cpus)
  {
    fprintf (stderr, "Parameter '%s' cannot be used together "
             "with '-A' or '--all-cpus'.\n", param_name);
    return PERF_RPL_PARAM_ERROR;
  }
  if (0 != tool_params.threads)
  {
    fprintf (stderr, "Parameter '%s' cannot be used together "
             "with '-t' or '--threads'.\n", param_name);
    return PERF_RPL_PARAM_ERROR;
  }
  tool_params.thread_per_conn = ! 0;
  return '-' == param_name[1] ?
         PERF_RPL_PARAM_FULL_STR :PERF_RPL_PARAM_ONE_CHAR;
}


static enum PerfRepl_param_result
process_param__epoll (const char *param_name)
{
  if (tool_params.poll)
  {
    fprintf (stderr, "Parameter '%s' cannot be used together "
             "with '-p' or '--poll'.\n", param_name);
    return PERF_RPL_PARAM_ERROR;
  }
  if (tool_params.select)
  {
    fprintf (stderr, "Parameter '%s' cannot be used together "
             "with '-s' or '--select'.\n", param_name);
    return PERF_RPL_PARAM_ERROR;
  }
  tool_params.epoll = ! 0;
  return '-' == param_name[1] ?
         PERF_RPL_PARAM_FULL_STR :PERF_RPL_PARAM_ONE_CHAR;
}


static enum PerfRepl_param_result
process_param__poll (const char *param_name)
{
  if (tool_params.epoll)
  {
    fprintf (stderr, "Parameter '%s' cannot be used together "
             "with '-e' or '--epoll'.\n", param_name);
    return PERF_RPL_PARAM_ERROR;
  }
  if (tool_params.select)
  {
    fprintf (stderr, "Parameter '%s' cannot be used together "
             "with '-s' or '--select'.\n", param_name);
    return PERF_RPL_PARAM_ERROR;
  }
  tool_params.poll = ! 0;
  return '-' == param_name[1] ?
         PERF_RPL_PARAM_FULL_STR :PERF_RPL_PARAM_ONE_CHAR;
}


static enum PerfRepl_param_result
process_param__select (const char *param_name)
{
  if (tool_params.epoll)
  {
    fprintf (stderr, "Parameter '%s' cannot be used together "
             "with '-e' or '--epoll'.\n", param_name);
    return PERF_RPL_PARAM_ERROR;
  }
  if (tool_params.poll)
  {
    fprintf (stderr, "Parameter '%s' cannot be used together "
             "with '-p' or '--poll'.\n", param_name);
    return PERF_RPL_PARAM_ERROR;
  }
  tool_params.select = ! 0;
  return '-' == param_name[1] ?
         PERF_RPL_PARAM_FULL_STR :PERF_RPL_PARAM_ONE_CHAR;
}


static enum PerfRepl_param_result
process_param__empty (const char *param_name)
{
  if (tool_params.tiny)
  {
    fprintf (stderr, "Parameter '%s' cannot be used together "
             "with '-T' or '--tiny'.\n", param_name);
    return PERF_RPL_PARAM_ERROR;
  }
  if (tool_params.medium)
  {
    fprintf (stderr, "Parameter '%s' cannot be used together "
             "with '-M' or '--medium'.\n", param_name);
    return PERF_RPL_PARAM_ERROR;
  }
  if (tool_params.large)
  {
    fprintf (stderr, "Parameter '%s' cannot be used together "
             "with '-L' or '--large'.\n", param_name);
    return PERF_RPL_PARAM_ERROR;
  }
  tool_params.empty = ! 0;
  return '-' == param_name[1] ?
         PERF_RPL_PARAM_FULL_STR :PERF_RPL_PARAM_ONE_CHAR;
}


static enum PerfRepl_param_result
process_param__tiny (const char *param_name)
{
  if (tool_params.empty)
  {
    fprintf (stderr, "Parameter '%s' cannot be used together "
             "with '-E' or '--empty'.\n", param_name);
    return PERF_RPL_PARAM_ERROR;
  }
  if (tool_params.medium)
  {
    fprintf (stderr, "Parameter '%s' cannot be used together "
             "with '-M' or '--medium'.\n", param_name);
    return PERF_RPL_PARAM_ERROR;
  }
  if (tool_params.large)
  {
    fprintf (stderr, "Parameter '%s' cannot be used together "
             "with '-L' or '--large'.\n", param_name);
    return PERF_RPL_PARAM_ERROR;
  }
  tool_params.tiny = ! 0;
  return '-' == param_name[1] ?
         PERF_RPL_PARAM_FULL_STR :PERF_RPL_PARAM_ONE_CHAR;
}


static enum PerfRepl_param_result
process_param__medium (const char *param_name)
{
  if (tool_params.empty)
  {
    fprintf (stderr, "Parameter '%s' cannot be used together "
             "with '-E' or '--empty'.\n", param_name);
    return PERF_RPL_PARAM_ERROR;
  }
  if (tool_params.tiny)
  {
    fprintf (stderr, "Parameter '%s' cannot be used together "
             "with '-T' or '--tiny'.\n", param_name);
    return PERF_RPL_PARAM_ERROR;
  }
  if (tool_params.large)
  {
    fprintf (stderr, "Parameter '%s' cannot be used together "
             "with '-L' or '--large'.\n", param_name);
    return PERF_RPL_PARAM_ERROR;
  }
  tool_params.medium = ! 0;
  return '-' == param_name[1] ?
         PERF_RPL_PARAM_FULL_STR :PERF_RPL_PARAM_ONE_CHAR;
}


static enum PerfRepl_param_result
process_param__large (const char *param_name)
{
  if (tool_params.empty)
  {
    fprintf (stderr, "Parameter '%s' cannot be used together "
             "with '-E' or '--empty'.\n", param_name);
    return PERF_RPL_PARAM_ERROR;
  }
  if (tool_params.tiny)
  {
    fprintf (stderr, "Parameter '%s' cannot be used together "
             "with '-T' or '--tiny'.\n", param_name);
    return PERF_RPL_PARAM_ERROR;
  }
  if (tool_params.medium)
  {
    fprintf (stderr, "Parameter '%s' cannot be used together "
             "with '-M' or '--medium'.\n", param_name);
    return PERF_RPL_PARAM_ERROR;
  }
  tool_params.large = ! 0;
  return '-' == param_name[1] ?
         PERF_RPL_PARAM_FULL_STR :PERF_RPL_PARAM_ONE_CHAR;
}


static enum PerfRepl_param_result
process_param__shared (const char *param_name)
{
  if (tool_params.single)
  {
    fprintf (stderr, "Parameter '%s' cannot be used together "
             "with '-I' or '--single'.\n", param_name);
    return PERF_RPL_PARAM_ERROR;
  }
  if (tool_params.unique)
  {
    fprintf (stderr, "Parameter '%s' cannot be used together "
             "with '-U' or '--unique'.\n", param_name);
    return PERF_RPL_PARAM_ERROR;
  }
  tool_params.shared = ! 0;
  return '-' == param_name[1] ?
         PERF_RPL_PARAM_FULL_STR :PERF_RPL_PARAM_ONE_CHAR;
}


static enum PerfRepl_param_result
process_param__single (const char *param_name)
{
  if (tool_params.shared)
  {
    fprintf (stderr, "Parameter '%s' cannot be used together "
             "with '-S' or '--shared'.\n", param_name);
    return PERF_RPL_PARAM_ERROR;
  }
  if (tool_params.unique)
  {
    fprintf (stderr, "Parameter '%s' cannot be used together "
             "with '-U' or '--unique'.\n", param_name);
    return PERF_RPL_PARAM_ERROR;
  }
  tool_params.single = ! 0;
  return '-' == param_name[1] ?
         PERF_RPL_PARAM_FULL_STR :PERF_RPL_PARAM_ONE_CHAR;
}


static enum PerfRepl_param_result
process_param__unique (const char *param_name)
{
  if (tool_params.shared)
  {
    fprintf (stderr, "Parameter '%s' cannot be used together "
             "with '-S' or '--shared'.\n", param_name);
    return PERF_RPL_PARAM_ERROR;
  }
  if (tool_params.single)
  {
    fprintf (stderr, "Parameter '%s' cannot be used together "
             "with '-I' or '--single'.\n", param_name);
    return PERF_RPL_PARAM_ERROR;
  }
  tool_params.unique = ! 0;
  return '-' == param_name[1] ?
         PERF_RPL_PARAM_FULL_STR :PERF_RPL_PARAM_ONE_CHAR;
}


/**
 * Process parameter '-c' or '--connections'
 * @param param_name the name of the parameter as specified in command line
 * @param param_tail the pointer to the character after parameter name in
 *                   the parameter string
 * @param next_param the pointer to the next parameter (if any) or NULL
 * @return enum value, the PERF_PERPL_SPARAM_ONE_CHAR is not used by
 *                     this function
 */
static enum PerfRepl_param_result
process_param__connections (const char *param_name, const char *param_tail,
                            const char *next_param)
{
  unsigned int param_value;
  enum PerfRepl_param_result value_res;

  value_res = get_param_value (param_name, param_tail, next_param,
                               &param_value);
  if (PERF_RPL_PARAM_ERROR == value_res)
    return value_res;

  if (0 == param_value)
  {
    fprintf (stderr, "'0' is not valid value for parameter '%s'.\n",
             param_name);
    return PERF_RPL_PARAM_ERROR;
  }
  tool_params.connections = param_value;
  return value_res;
}


/**
 * Process parameter '-O' or '--timeout'
 * @param param_name the name of the parameter as specified in command line
 * @param param_tail the pointer to the character after parameter name in
 *                   the parameter string
 * @param next_param the pointer to the next parameter (if any) or NULL
 * @return enum value, the PERF_PERPL_SPARAM_ONE_CHAR is not used by
 *                     this function
 */
static enum PerfRepl_param_result
process_param__timeout (const char *param_name, const char *param_tail,
                        const char *next_param)
{
  unsigned int param_value;
  enum PerfRepl_param_result value_res;

  value_res = get_param_value (param_name, param_tail, next_param,
                               &param_value);
  if (PERF_RPL_PARAM_ERROR == value_res)
    return value_res;

  tool_params.timeout = param_value;
  return value_res;
}


static enum PerfRepl_param_result
process_param__date_header (const char *param_name)
{
  tool_params.date_header = ! 0;
  return '-' == param_name[1] ?
         PERF_RPL_PARAM_FULL_STR :PERF_RPL_PARAM_ONE_CHAR;
}


static enum PerfRepl_param_result
process_param__help (const char *param_name)
{
  /* Use only one of help | version */
  if (! tool_params.version)
    tool_params.help = ! 0;
  return '-' == param_name[1] ?
         PERF_RPL_PARAM_FULL_STR :PERF_RPL_PARAM_ONE_CHAR;
}


static enum PerfRepl_param_result
process_param__version (const char *param_name)
{
  /* Use only one of help | version */
  if (! tool_params.help)
    tool_params.version = ! 0;
  return '-' == param_name[1] ?
         PERF_RPL_PARAM_FULL_STR :PERF_RPL_PARAM_ONE_CHAR;
}


/**
 * Process "short" (one character) parameter.
 * @param param the pointer to character after "-" or after another valid
 *              parameter
 * @param next_param the pointer to the next parameter (if any) or
 *                   NULL if no next parameter
 * @return enum value with result
 */
static enum PerfRepl_param_result
process_short_param (const char *param, const char *next_param)
{
  const char param_chr = param[0];
  if ('A' == param_chr)
    return process_param__all_cpus ("-A");
  else if ('t' == param_chr)
    return process_param__threads ("-t", param + 1, next_param);
  else if ('P' == param_chr)
    return process_param__thread_per_conn ("-P");
  else if ('e' == param_chr)
    return process_param__epoll ("-e");
  else if ('p' == param_chr)
    return process_param__poll ("-p");
  else if ('s' == param_chr)
    return process_param__select ("-s");
  else if ('E' == param_chr)
    return process_param__empty ("-E");
  else if ('T' == param_chr)
    return process_param__tiny ("-T");
  else if ('M' == param_chr)
    return process_param__medium ("-M");
  else if ('L' == param_chr)
    return process_param__large ("-L");
  else if ('S' == param_chr)
    return process_param__shared ("-S");
  else if ('I' == param_chr)
    return process_param__single ("-I");
  else if ('U' == param_chr)
    return process_param__unique ("-U");
  else if ('c' == param_chr)
    return process_param__connections ("-c", param + 1, next_param);
  else if ('O' == param_chr)
    return process_param__timeout ("-O", param + 1, next_param);
  else if ('V' == param_chr)
    return process_param__version ("-V");

  fprintf (stderr, "Unrecognised parameter: -%c.\n", param_chr);
  return PERF_RPL_PARAM_ERROR;
}


/**
 * Process string of "short" (one character) parameters.
 * @param params_str the pointer to first character after "-"
 * @param next_param the pointer to the next parameter (if any) or
 *                   NULL if no next parameter
 * @return enum value with result
 */
static enum PerfRepl_param_result
process_short_params_str (const char *params_str, const char *next_param)
{
  if (0 == params_str[0])
  {
    fprintf (stderr, "Unrecognised parameter: -\n");
    return PERF_RPL_PARAM_ERROR;
  }
  do
  {
    enum PerfRepl_param_result param_res;
    param_res = process_short_param (params_str, next_param);
    if (PERF_RPL_PARAM_ONE_CHAR != param_res)
      return param_res;
  } while (0 != (++params_str)[0]);
  return PERF_RPL_PARAM_FULL_STR;
}


/**
 * Process "long" (--something) parameters.
 * @param param the pointer to first character after "--"
 * @param next_param the pointer to the next parameter (if any) or
 *                   NULL if no next parameter
 * @return enum value, the PERF_PERPL_SPARAM_ONE_CHAR is not used by
 *                     this function
 */
static enum PerfRepl_param_result
process_long_param (const char *param, const char *next_param)
{
  const size_t param_len = strlen (param);

  if ((MHD_STATICSTR_LEN_ ("all-cpus") == param_len) &&
      (0 == memcmp (param, "all-cpus", MHD_STATICSTR_LEN_ ("all-cpus"))))
    return process_param__all_cpus ("--all-cpus");
  else if ((MHD_STATICSTR_LEN_ ("threads") <= param_len) &&
           (0 == memcmp (param, "threads", MHD_STATICSTR_LEN_ ("threads"))))
    return process_param__threads ("--threads",
                                   param + MHD_STATICSTR_LEN_ ("threads"),
                                   next_param);
  else if ((MHD_STATICSTR_LEN_ ("thread-per-conn") == param_len) &&
           (0 == memcmp (param, "thread-per-conn",
                         MHD_STATICSTR_LEN_ ("thread-per-conn"))))
    return process_param__thread_per_conn ("--thread-per-conn");
  else if ((MHD_STATICSTR_LEN_ ("epoll") == param_len) &&
           (0 == memcmp (param, "epoll", MHD_STATICSTR_LEN_ ("epoll"))))
    return process_param__epoll ("--epoll");
  else if ((MHD_STATICSTR_LEN_ ("poll") == param_len) &&
           (0 == memcmp (param, "poll", MHD_STATICSTR_LEN_ ("poll"))))
    return process_param__poll ("--poll");
  else if ((MHD_STATICSTR_LEN_ ("select") == param_len) &&
           (0 == memcmp (param, "select", MHD_STATICSTR_LEN_ ("select"))))
    return process_param__select ("--select");
  else if ((MHD_STATICSTR_LEN_ ("empty") == param_len) &&
           (0 == memcmp (param, "empty", MHD_STATICSTR_LEN_ ("empty"))))
    return process_param__empty ("--empty");
  else if ((MHD_STATICSTR_LEN_ ("tiny") == param_len) &&
           (0 == memcmp (param, "tiny", MHD_STATICSTR_LEN_ ("tiny"))))
    return process_param__tiny ("--tiny");
  else if ((MHD_STATICSTR_LEN_ ("medium") == param_len) &&
           (0 == memcmp (param, "medium", MHD_STATICSTR_LEN_ ("medium"))))
    return process_param__medium ("--medium");
  else if ((MHD_STATICSTR_LEN_ ("large") == param_len) &&
           (0 == memcmp (param, "large", MHD_STATICSTR_LEN_ ("large"))))
    return process_param__large ("--large");
  else if ((MHD_STATICSTR_LEN_ ("shared") == param_len) &&
           (0 == memcmp (param, "shared", MHD_STATICSTR_LEN_ ("shared"))))
    return process_param__shared ("--shared");
  else if ((MHD_STATICSTR_LEN_ ("single") == param_len) &&
           (0 == memcmp (param, "single", MHD_STATICSTR_LEN_ ("single"))))
    return process_param__single ("--single");
  else if ((MHD_STATICSTR_LEN_ ("unique") == param_len) &&
           (0 == memcmp (param, "unique", MHD_STATICSTR_LEN_ ("unique"))))
    return process_param__unique ("--unique");
  else if ((MHD_STATICSTR_LEN_ ("connections") <= param_len) &&
           (0 == memcmp (param, "connections",
                         MHD_STATICSTR_LEN_ ("connections"))))
    return process_param__connections ("--connections",
                                       param
                                       + MHD_STATICSTR_LEN_ ("connections"),
                                       next_param);
  else if ((MHD_STATICSTR_LEN_ ("timeout") <= param_len) &&
           (0 == memcmp (param, "timeout",
                         MHD_STATICSTR_LEN_ ("timeout"))))
    return process_param__timeout ("--timeout",
                                   param + MHD_STATICSTR_LEN_ ("timeout"),
                                   next_param);
  else if ((MHD_STATICSTR_LEN_ ("date-header") == param_len) &&
           (0 == memcmp (param, "date-header",
                         MHD_STATICSTR_LEN_ ("date-header"))))
    return process_param__date_header ("--date-header");
  else if ((MHD_STATICSTR_LEN_ ("help") == param_len) &&
           (0 == memcmp (param, "help", MHD_STATICSTR_LEN_ ("help"))))
    return process_param__help ("--help");
  else if ((MHD_STATICSTR_LEN_ ("version") == param_len) &&
           (0 == memcmp (param, "version", MHD_STATICSTR_LEN_ ("version"))))
    return process_param__version ("--version");

  fprintf (stderr, "Unrecognised parameter: --%s.\n", param);
  return PERF_RPL_PARAM_ERROR;
}


static int
process_params (int argc, char *const *argv)
{
  int proc_dash_param = ! 0;
  int i;
  for (i = 1; i < argc; ++i)
  {
    /**
     * The currently processed argument
     */
    const char *const p = argv[i];
    const char *const p_next = (argc == (i + 1)) ? NULL : (argv[i + 1]);
    if (NULL == p)
    {
      fprintf (stderr, "The NULL in the parameter number %d. "
               "The error in the C library?\n", i);
      continue;
    }
    else if (0 == p[0])
      continue; /* Empty */
    else if (proc_dash_param && ('-' == p[0]))
    {
      enum PerfRepl_param_result param_res;
      if ('-' == p[1])
      {
        if (0 == p[2])
        {
          proc_dash_param = 0; /* The '--' parameter */
          continue;
        }
        param_res = process_long_param (p + 2, p_next);
      }
      else
        param_res = process_short_params_str (p + 1, p_next);

      if (PERF_RPL_PARAM_ERROR == param_res)
        return PERF_RPL_ERR_CODE_BAD_PARAM;
      if (PERF_RPL_PARAM_STR_PLUS_NEXT == param_res)
        ++i;
      else if (PERF_RPL_PARAM_ONE_CHAR == param_res)
        abort ();
      continue;
    }
    else if (('0' <= p[0]) && ('9' >= p[0]))
    {
      /* Process the port number */
      unsigned int read_port;
      size_t num_digits;
      num_digits = mhd_tool_str_to_uint (p, &read_port);
      if (0 != p[num_digits])
      {
        fprintf (stderr, "Error in specified port number: %s\n", p);
        return PERF_RPL_ERR_CODE_BAD_PARAM;
      }
      else if (65535 < read_port)
      {
        fprintf (stderr, "Wrong port number: %s\n", p);
        return PERF_RPL_ERR_CODE_BAD_PARAM;
      }
      mhd_port = (uint16_t) read_port;
    }
    else
    {
      fprintf (stderr, "Unrecognised parameter: %s\n\n", p);
      return PERF_RPL_ERR_CODE_BAD_PARAM;
    }
  }
  return 0;
}


static void
print_version (void)
{
  printf ("%s (GNU libmicrohttpd", self_name);
  if (0 != build_revision[0])
    printf ("; %s", build_revision);
  printf (") %s\n", MHD_get_version ());
  printf ("%s\n", tool_copyright);
}


static void
print_all_cores_used (void)
{
  printf ("No CPU cores on this machine are left unused and available "
          "for the client / requests generator. "
          "Testing with remote client is recommended.\n");
}


static void
check_param_port (void)
{
  if (0 != tool_params.port)
    return;
  if (MHD_NO == MHD_is_feature_supported (MHD_FEATURE_AUTODETECT_BIND_PORT))
    tool_params.port = PERF_REPL_PORT_FALLBACK;
}


/**
 * Apply parameter '-A' or '--all-cpus'
 */
static void
check_apply_param__all_cpus (void)
{
  if (! tool_params.all_cpus)
    return;

  num_threads = get_process_cpu_core_count ();
  printf ("Requested use of all available CPU cores for MHD threads.\n");
  if (get_cpu_core_count () == num_threads)
    print_all_cores_used ();
}


/**
 * Apply parameter '-t' or '--threads'
 */
static void
check_apply_param__threads (void)
{
  if (0 == tool_params.threads)
    return;

  num_threads = tool_params.threads;

  if (get_process_cpu_core_count () < num_threads)
  {
    fprintf (stderr, "WARNING: The requested number of threads (%u) is "
             "higher than the number of detected available CPU cores (%u).\n",
             num_threads, get_process_cpu_core_count ());
    fprintf (stderr, "This decreases the performance. "
             "Consider using fewer threads.\n");
  }
  if (get_cpu_core_count () == num_threads)
  {
    printf ("The requested number of threads is equal to the number of "
            "detected CPU cores.\n");
    print_all_cores_used ();
  }
}


/**
 * Apply parameter '-P' or '--thread-per-conn'
 * @return non-zero - OK, zero - error
 */
static int
check_apply_param__thread_per_conn (void)
{
  if (! tool_params.thread_per_conn)
    return ! 0;

  if (tool_params.epoll)
  {
    fprintf (stderr, "'Thread-per-connection' mode cannot be used together "
             "with 'epoll'.\n");
    return 0;
  }
  num_threads = 1;

  return ! 0;
}


/* non-zero - OK, zero - error */
static int
check_param__epoll (void)
{
  if (! tool_params.epoll)
    return ! 0;
  if (MHD_NO == MHD_is_feature_supported (MHD_FEATURE_EPOLL))
  {
    fprintf (stderr, "'epoll' was requested, but this MHD build does not "
             "support 'epoll' functionality.\n");
    return 0;
  }
  return ! 0;
}


/* non-zero - OK, zero - error */
static int
check_param__poll (void)
{
  if (! tool_params.poll)
    return ! 0;
  if (MHD_NO == MHD_is_feature_supported (MHD_FEATURE_POLL))
  {
    fprintf (stderr, "poll() was requested, but this MHD build does not "
             "support polling by poll().\n");
    return 0;
  }
  return ! 0;
}


static void
check_param__empty_tiny_medium_large (void)
{
  if (0 == (tool_params.empty | tool_params.tiny | tool_params.medium
            | tool_params.large))
    tool_params.tiny = ! 0;
}


static void
check_param__shared_single_unique (void)
{
  if (0 == (tool_params.shared | tool_params.single | tool_params.unique))
    tool_params.shared = ! 0;
}


/* Must be called after 'check_apply_param__threads()' and
   'check_apply_param__all_cpus()' */
/* non-zero - OK, zero - error */
static int
check_param__connections (void)
{
  if (0 == tool_params.connections)
    return ! 0;
  if (get_num_threads () > tool_params.connections)
  {
    fprintf (stderr, "The connections number limit (%u) is less than number "
             "of threads used (%u). Use higher value for connections limit.\n",
             tool_params.connections, get_num_threads ());
    return 0;
  }
  return ! 0;
}


/**
 * Apply decoded parameters
 * @return 0 if success,
 *         positive error code if case of error,
 *         -1 to exit program with success (0) error code.
 */
static int
check_apply_params (void)
{
  if (tool_params.help)
  {
    show_help ();
    return -1;
  }
  else if (tool_params.version)
  {
    print_version ();
    return -1;
  }
  check_param_port ();
  check_apply_param__all_cpus ();
  check_apply_param__threads ();
  if (! check_apply_param__thread_per_conn ())
    return PERF_RPL_ERR_CODE_BAD_PARAM;
  if (! check_param__epoll ())
    return PERF_RPL_ERR_CODE_BAD_PARAM;
  if (! check_param__poll ())
    return PERF_RPL_ERR_CODE_BAD_PARAM;
  check_param__empty_tiny_medium_large ();
  check_param__shared_single_unique ();
  if (! check_param__connections ())
    return PERF_RPL_ERR_CODE_BAD_PARAM;
  return 0;
}


/* The pool of shared responses */
static struct MHD_Response **resps = NULL;
static unsigned int num_resps = 0;
/* The single response */
static struct MHD_Response *resp_single = NULL;

/* Use the same memory area to avoid multiple copies.
   The system will keep it in cache. */
static const char tiny_body[] = "Hi!";
static char *body_dyn = NULL; /* Non-static body data */
static size_t body_dyn_size;

/* Non-zero - success, zero - failure */
static int
init_response_body_data (void)
{
  if (0 != body_dyn_size)
  {
    body_dyn = (char *) malloc (body_dyn_size);
    if (NULL == body_dyn)
    {
      fprintf (stderr, "Failed to allocate memory.\n");
      return 0;
    }
    if (tool_params.medium)
    {
      /* Fill the body with HTML-like content */
      size_t pos;
      size_t filler_pos;
      static const char body_header[] =
        "<html>\n"
        "<head>\n<title>Sample page title</title>\n<head>\n"
        "<body>\n";
      static const char body_filler[] =
        "The quick brown fox jumps over the lazy dog.<br>\n";
      static const char body_footer[] =
        "</body>\n"
        "</html>\n";
      pos = 0;
      memcpy (body_dyn + pos, body_header, MHD_STATICSTR_LEN_ (body_header));
      pos += MHD_STATICSTR_LEN_ (body_header);
      for (filler_pos = 0;
           filler_pos < (body_dyn_size - (MHD_STATICSTR_LEN_ (body_header)
                                          + MHD_STATICSTR_LEN_ (body_footer)));
           ++filler_pos)
      {
        body_dyn[pos + filler_pos] =
          body_filler[filler_pos % MHD_STATICSTR_LEN_ (body_filler)];
      }
      pos += filler_pos;
      memcpy (body_dyn + pos, body_footer, MHD_STATICSTR_LEN_ (body_footer));
    }
    else
    {
      /* Fill the body with binary-like content */
      size_t pos;
      for (pos = 0; pos < body_dyn_size; ++pos)
      {
        body_dyn[pos] = (char) (unsigned char) (255U - pos % 256U);
      }
    }
  }
  return ! 0;
}


static struct MHD_Response *
create_response_object (void)
{
#if MHD_VERSION >= 0x00097701
  if (NULL != body_dyn)
    return MHD_create_response_from_buffer_static (body_dyn_size,
                                                   body_dyn);
  else if (tool_params.empty)
    return MHD_create_response_empty (MHD_RF_NONE);

  return MHD_create_response_from_buffer_static (MHD_STATICSTR_LEN_ (tiny_body),
                                                 tiny_body);

#else  /* MHD_VERSION < 0x00097701 */
  if (NULL != body_dyn)
    return MHD_create_response_from_buffer (body_dyn_size,
                                            (void *) body_dyn,
                                            MHD_RESPMEM_PERSISTENT);
  else if (tool_params.empty)
    return MHD_create_response_from_buffer (0,
                                            (void *) tiny_body,
                                            MHD_RESPMEM_PERSISTENT);

  return MHD_create_response_from_buffer (MHD_STATICSTR_LEN_ (tiny_body),
                                          (void *) tiny_body,
                                          MHD_RESPMEM_PERSISTENT);
#endif /* MHD_VERSION < 0x00097701 */
}


static int
init_data (void)
{
  unsigned int i;

  if (tool_params.medium)
    body_dyn_size = 8U * 1024U;
  else if (tool_params.large)
    body_dyn_size = 1024U * 1024U;
  else
    body_dyn_size = 0;

  if (! init_response_body_data ())
    return 25;

  if (tool_params.unique)
    return 0; /* Responses are generated on-fly */

  if (tool_params.single)
  {
    resp_single = create_response_object ();
    if (NULL == resp_single)
    {
      fprintf (stderr, "Failed to create response.\n");
      return 25;
    }
    return 0;
  }

  /* Use more responses to minimise waiting in threads while the response
     used by other thread. */
  if (! tool_params.thread_per_conn)
    num_resps = 16 * get_num_threads ();
  else
    num_resps = 16 * get_cpu_core_count ();

  resps = (struct MHD_Response **)
          malloc ((sizeof(struct MHD_Response *)) * num_resps);
  if (NULL == resps)
  {
    if (NULL != body_dyn)
    {
      free (body_dyn);
      body_dyn = NULL;
    }
    fprintf (stderr, "Failed to allocate memory.\n");
    return 25;
  }
  for (i = 0; i < num_resps; ++i)
  {
    resps[i] = create_response_object ();
    if (NULL == resps[i])
    {
      fprintf (stderr, "Failed to create responses.\n");
      break;
    }
  }
  if (i == num_resps)
    return 0; /* Success */

  /* Cleanup */
  while (--i < num_resps)
    MHD_destroy_response (resps[i]);
  free (resps);
  resps = NULL;
  num_resps = 0;
  if (NULL != body_dyn)
    free (body_dyn);
  body_dyn = NULL;
  return 32;
}


static void
deinit_data (void)
{
  if (NULL != resp_single)
    MHD_destroy_response (resp_single);
  resp_single = NULL;
  if (NULL != resps)
  {
    unsigned int i;
    for (i = 0; i < num_resps; ++i)
      MHD_destroy_response (resps[i]);
    num_resps = 0;
    free (resps);
  }
  resps = NULL;
  if (NULL != body_dyn)
    free (body_dyn);
  body_dyn = NULL;
}


static enum MHD_Result
answer_shared_response (void *cls,
                        struct MHD_Connection *connection,
                        const char *url,
                        const char *method,
                        const char *version,
                        const char *upload_data,
                        size_t *upload_data_size,
                        void **req_cls)
{
  static int marker = 0;
  unsigned int resp_index;
  static volatile unsigned int last_index = 0;
  (void) cls;  /* Unused */
  (void) url; (void) version; /* Unused */
  (void) upload_data; (void) upload_data_size; /* Unused */

  if (NULL == *req_cls)
  {
    /* The fist call */
    *req_cls = (void *) &marker;
    /* Do not send reply yet. No error. */
    return MHD_YES;
  }
  if ((0 != strcmp (method, MHD_HTTP_METHOD_GET)) &&
      (0 != strcmp (method, MHD_HTTP_METHOD_HEAD)))
    return MHD_NO; /* Unsupported method, close connection */

  /* This kind of operation does not guarantee that numbers are not reused
     in parallel threads, when processed simultaneously, but this should not
     be a big problem, as it just slow down replies a bit due to
     responses locking. */
  resp_index = (last_index++) % num_resps;
  return MHD_queue_response (connection, MHD_HTTP_OK, resps[resp_index]);
}


static enum MHD_Result
answer_single_response (void *cls,
                        struct MHD_Connection *connection,
                        const char *url,
                        const char *method,
                        const char *version,
                        const char *upload_data,
                        size_t *upload_data_size,
                        void **req_cls)
{
  static int marker = 0;
  (void) cls;  /* Unused */
  (void) url; (void) version; /* Unused */
  (void) upload_data; (void) upload_data_size; /* Unused */

  if (NULL == *req_cls)
  {
    /* The fist call */
    *req_cls = (void *) &marker;
    /* Do not send reply yet. No error. */
    return MHD_YES;
  }
  if ((0 != strcmp (method, MHD_HTTP_METHOD_GET)) &&
      (0 != strcmp (method, MHD_HTTP_METHOD_HEAD)))
    return MHD_NO; /* Unsupported method, close connection */

  return MHD_queue_response (connection, MHD_HTTP_OK, resp_single);
}


static enum MHD_Result
answer_unique_empty_response (void *cls,
                              struct MHD_Connection *connection,
                              const char *url,
                              const char *method,
                              const char *version,
                              const char *upload_data,
                              size_t *upload_data_size,
                              void **req_cls)
{
  static int marker = 0;
  struct MHD_Response *r;
  enum MHD_Result ret;
  (void) cls;  /* Unused */
  (void) url; (void) version; /* Unused */
  (void) upload_data; (void) upload_data_size; /* Unused */

  if (NULL == *req_cls)
  {
    /* The fist call */
    *req_cls = (void *) &marker;
    /* Do not send reply yet. No error. */
    return MHD_YES;
  }
  if ((0 != strcmp (method, MHD_HTTP_METHOD_GET)) &&
      (0 != strcmp (method, MHD_HTTP_METHOD_HEAD)))
    return MHD_NO; /* Unsupported method, close connection */

#if MHD_VERSION >= 0x00097701
  r = MHD_create_response_empty (MHD_RF_NONE);
#else  /* MHD_VERSION < 0x00097701 */
  r = MHD_create_response_from_buffer (0,
                                       NULL,
                                       MHD_RESPMEM_PERSISTENT);
#endif /* MHD_VERSION < 0x00097701 */
  ret = MHD_queue_response (connection, MHD_HTTP_OK, r);
  MHD_destroy_response (r);
  return ret;
}


static enum MHD_Result
answer_unique_tiny_response (void *cls,
                             struct MHD_Connection *connection,
                             const char *url,
                             const char *method,
                             const char *version,
                             const char *upload_data,
                             size_t *upload_data_size,
                             void **req_cls)
{
  static int marker = 0;
  struct MHD_Response *r;
  enum MHD_Result ret;
  (void) cls;  /* Unused */
  (void) url; (void) version; /* Unused */
  (void) upload_data; (void) upload_data_size; /* Unused */

  if (NULL == *req_cls)
  {
    /* The fist call */
    *req_cls = (void *) &marker;
    /* Do not send reply yet. No error. */
    return MHD_YES;
  }
  if ((0 != strcmp (method, MHD_HTTP_METHOD_GET)) &&
      (0 != strcmp (method, MHD_HTTP_METHOD_HEAD)))
    return MHD_NO; /* Unsupported method, close connection */

#if MHD_VERSION >= 0x00097701
  r = MHD_create_response_from_buffer_static (MHD_STATICSTR_LEN_ (tiny_body),
                                              tiny_body);
#else  /* MHD_VERSION < 0x00097701 */
  r = MHD_create_response_from_buffer (MHD_STATICSTR_LEN_ (tiny_body),
                                       (void *) tiny_body,
                                       MHD_RESPMEM_PERSISTENT);
#endif /* MHD_VERSION < 0x00097701 */
  ret = MHD_queue_response (connection, MHD_HTTP_OK, r);
  MHD_destroy_response (r);
  return ret;
}


static enum MHD_Result
answer_unique_dyn_response (void *cls,
                            struct MHD_Connection *connection,
                            const char *url,
                            const char *method,
                            const char *version,
                            const char *upload_data,
                            size_t *upload_data_size,
                            void **req_cls)
{
  static int marker = 0;
  struct MHD_Response *r;
  enum MHD_Result ret;
  (void) cls;  /* Unused */
  (void) url; (void) version; /* Unused */
  (void) upload_data; (void) upload_data_size; /* Unused */

  if (NULL == *req_cls)
  {
    /* The fist call */
    *req_cls = (void *) &marker;
    /* Do not send reply yet. No error. */
    return MHD_YES;
  }
  if ((0 != strcmp (method, MHD_HTTP_METHOD_GET)) &&
      (0 != strcmp (method, MHD_HTTP_METHOD_HEAD)))
    return MHD_NO; /* Unsupported method, close connection */

#if MHD_VERSION >= 0x00097701
  r = MHD_create_response_from_buffer_static (body_dyn_size,
                                              body_dyn);
#else  /* MHD_VERSION < 0x00097701 */
  r = MHD_create_response_from_buffer (body_dyn_size,
                                       (void *) body_dyn,
                                       MHD_RESPMEM_PERSISTENT);
#endif /* MHD_VERSION < 0x00097701 */
  ret = MHD_queue_response (connection, MHD_HTTP_OK, r);
  MHD_destroy_response (r);
  return ret;
}


static void
print_perf_warnings (void)
{
  int newline_needed = 0;
#if defined (_DEBUG)
  fprintf (stderr, "WARNING: Running with debug asserts enabled, "
           "the performance is suboptimal.\n");
  newline_needed |=  ! 0;
#endif /* _DEBUG */
#if defined(__GNUC__) && ! defined (__OPTIMIZE__)
  fprintf (stderr, "WARNING: The tools is compiled without enabled compiler "
           "optimisations, the performance is suboptimal.\n");
  newline_needed |=  ! 0;
#endif /* __GNUC__ && ! __OPTIMIZE__ */
#if defined(__GNUC__) && defined (__OPTIMIZE_SIZE__)
  fprintf (stderr, "WARNING: The tools is compiled with size-optimisations, "
           "the performance is suboptimal.\n");
#endif /* __GNUC__ && ! __OPTIMIZE__ */
#if MHD_VERSION >= 0x00097701
  if (MHD_NO != MHD_is_feature_supported (MHD_FEATURE_DEBUG_BUILD))
  {
    fprintf (stderr, "WARNING: The libmicrohttpd is compiled with "
             "debug asserts enabled, the performance is suboptimal.\n");
    newline_needed |=  ! 0;
  }
#endif /* MHD_VERSION >= 0x00097701 */
  if (newline_needed)
    printf ("\n");
}


/* Borrowed from daemon.c */
/* TODO: run-time detection */
/**
 * Default connection limit.
 */
#ifdef MHD_POSIX_SOCKETS
#define MHD_MAX_CONNECTIONS_DEFAULT (FD_SETSIZE - 4)
#else
#define MHD_MAX_CONNECTIONS_DEFAULT (FD_SETSIZE - 2)
#endif

static unsigned int
get_mhd_conn_limit (struct MHD_Daemon *d)
{
  /* TODO: implement run-time detection */
  (void) d; /* Unused */
  if (0 != tool_params.connections)
    return tool_params.connections;
  return (unsigned int) MHD_MAX_CONNECTIONS_DEFAULT;
}


static const char *
get_mhd_response_size (void)
{
  if (tool_params.empty)
    return "0 bytes (empty)";
  else if (tool_params.tiny)
    return "3 bytes (tiny)";
  else if (tool_params.medium)
    return "8 KB (medium)";
  else if (tool_params.large)
    return "1 MB (large)";
  abort ();
  return "";
}


static int
run_mhd (void)
{
  MHD_AccessHandlerCallback reply_func;
  struct MHD_Daemon *d;
  unsigned int use_num_threads;
  unsigned int flags = MHD_NO_FLAG;
  struct MHD_OptionItem opt_arr[16];
  size_t opt_count = 0;
  const union MHD_DaemonInfo *d_info;
  const char *poll_mode;
  uint16_t port;

  if (tool_params.thread_per_conn)
    use_num_threads = 0;
  else
    use_num_threads = get_num_threads ();
  printf ("\n");

  print_perf_warnings ();

  printf ("Responses:\n");
  printf ("  Sharing:   ");
  if (tool_params.shared)
  {
    reply_func = &answer_shared_response;
    printf ("pre-generated shared pool with %u objects\n", num_resps);
  }
  else if (tool_params.single)
  {
    reply_func = &answer_single_response;
    printf ("single pre-generated reused response object\n");
  }
  else
  {
    /* Unique responses */
    if (tool_params.empty)
      reply_func = &answer_unique_empty_response;
    else if (tool_params.tiny)
      reply_func = &answer_unique_tiny_response;
    else
      reply_func = &answer_unique_dyn_response;
    printf ("one-time response object generated for every request\n");
  }
  printf ("  Body size: %s\n",
          get_mhd_response_size ());

  flags |= MHD_USE_ERROR_LOG;
  flags |= MHD_USE_INTERNAL_POLLING_THREAD;
  if (tool_params.epoll)
    flags |= MHD_USE_EPOLL;
  else if (tool_params.poll)
    flags |= MHD_USE_POLL;
  else if (tool_params.select)
    (void) flags; /* No special additional flag */
  else
    flags |= MHD_USE_AUTO;

  if (tool_params.thread_per_conn)
    flags |= MHD_USE_THREAD_PER_CONNECTION;

  if (! tool_params.date_header)
    flags |= MHD_USE_SUPPRESS_DATE_NO_CLOCK;

  if (0 != tool_params.connections)
  {
    struct MHD_OptionItem option =
    { MHD_OPTION_CONNECTION_LIMIT, (intptr_t) tool_params.connections, NULL };
    opt_arr[opt_count++] = option;
  }
  if (1 < use_num_threads)
  {
    struct MHD_OptionItem option =
    { MHD_OPTION_THREAD_POOL_SIZE, (intptr_t) use_num_threads, NULL };
    opt_arr[opt_count++] = option;
  }
  if (1)
  {
    struct MHD_OptionItem option =
    { MHD_OPTION_CONNECTION_TIMEOUT, (intptr_t) tool_params.timeout, NULL };
    opt_arr[opt_count++] = option;
  }
  if (1)
  {
    struct MHD_OptionItem option =
    { MHD_OPTION_END, 0, NULL };
    opt_arr[opt_count] = option;
    if (opt_count >= (sizeof(opt_arr) / sizeof(opt_arr[0])))
      abort ();
  }
  d = MHD_start_daemon (flags, mhd_port, NULL, NULL, reply_func, NULL,
                        MHD_OPTION_ARRAY, opt_arr, MHD_OPTION_END);
  if (NULL == d)
  {
    fprintf (stderr, "Error starting MHD daemon.\n");
    return 15;
  }
  d_info = MHD_get_daemon_info (d, MHD_DAEMON_INFO_FLAGS);
  if (NULL == d_info)
    abort ();
  flags = (unsigned int) d_info->flags;
  if (0 != (flags & MHD_USE_POLL))
    poll_mode = "poll()";
  else if (0 != (flags & MHD_USE_EPOLL))
    poll_mode = "epoll";
  else
    poll_mode = "select()";
  d_info = MHD_get_daemon_info (d, MHD_DAEMON_INFO_BIND_PORT);
  if (NULL == d_info)
    abort ();
  port = d_info->port;
  if (0 == port)
    fprintf (stderr, "Cannot detect port number. Consider specifying "
             "port number explicitly.\n");

  printf ("MHD is running.\n");
  printf ("  Bind port:          %u\n", (unsigned int) port);
  printf ("  Polling function:   %s\n", poll_mode);
  printf ("  Threading:          ");
  if (MHD_USE_THREAD_PER_CONNECTION == (flags & MHD_USE_THREAD_PER_CONNECTION))
    printf ("thread per connection\n");
  else if (1 == get_num_threads ())
    printf ("one MHD thread\n");
  else
    printf ("%u MHD threads in thread pool\n", get_num_threads ());
  printf ("  Connections limit:  %u\n", get_mhd_conn_limit (d));
  printf ("  Connection timeout: %u%s\n", tool_params.timeout,
          0 == tool_params.timeout ? " (no timeout)" : "");
  printf ("  'Date:' header:     %s\n",
          tool_params.date_header ? "Yes" : "No");
  printf ("To test with remote client use            "
          "http://HOST_IP:%u/\n", (unsigned int) port);
  printf ("To test with client on the same host use  "
          "http://127.0.0.1:%u/\n", (unsigned int) port);
  printf ("\nPress ENTER to stop.\n");
  if (1)
  {
    char buf[10];
    (void) fgets (buf, sizeof(buf), stdin);
  }
  MHD_stop_daemon (d);
  return 0;
}


int
main (int argc, char *const *argv)
{
  int ret;
  set_self_name (argc, argv);
  ret = process_params (argc, argv);
  if (0 != ret)
    return ret;
  ret = check_apply_params ();
  if (0 > ret)
    return 0;
  if (0 != ret)
    return ret;
  ret = init_data ();
  if (0 != ret)
    return ret;
  ret = run_mhd ();
  deinit_data ();
  return ret;
}