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

  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either
  version 2.1 of the License, or (at your option) any later version.

  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  Lesser General Public License for more details.

  You should have received a copy of the GNU Lesser General Public
  License along with this library; if not, write to the Free Software
  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */

/**
 * @file tools/mhd_tool_get_cpu_count.c
 * @brief  Implementation of functions to detect the number of available
 *         CPU cores.
 * @author Karlson2k (Evgeny Grin)
 */

#include "mhd_options.h"
#include "mhd_tool_get_cpu_count.h"
#ifdef HAVE_SYS_TYPES_H
#  include <sys/types.h>
#endif /* HAVE_SYS_TYPES_H */
#ifdef HAVE_SYS_PARAM_H
#  include <sys/param.h>
#endif /* HAVE_SYS_PARAM_H */
#ifdef HAVE_SYS__CPUSET_H
#  include <sys/_cpuset.h>
#endif /* HAVE_SYS_PARAM_H */
#ifdef HAVE_STDDEF_H
#  include <stddef.h>
#endif /* HAVE_STDDEF_H */
#ifdef HAVE_STRING_H
#  include <string.h>
#endif /* HAVE_STRING_H */
#ifdef HAVE_SYS_SYSCTL_H
#  include <sys/sysctl.h>
#endif /* HAVE_SYS_SYSCTL_H */
#ifdef HAVE_FEATURES_H
#  include <features.h>
#endif /* HAVE_FEATURES_H */
#ifdef HAVE_UNISTD_H
#  include <unistd.h>
#endif /* HAVE_UNISTD_H */
#ifdef HAVE_VXCPULIB_H
#  include <vxCpuLib.h>
#endif
#ifdef HAVE_WINDOWS_H
#  ifndef WIN32_LEAN_AND_MEAN
#    define WIN32_LEAN_AND_MEAN 1
#  endif /* ! WIN32_LEAN_AND_MEAN */
#  include <windows.h>
#  ifndef ALL_PROCESSOR_GROUPS
#    define ALL_PROCESSOR_GROUPS 0xFFFFu
#  endif /* ALL_PROCESSOR_GROUPS */
#elif defined(_WIN32) && ! defined (__CYGWIN__)
#  error Windows headers are required for Windows build
#endif /* HAVE_WINDOWS_H */
#ifdef HAVE_SCHED_H
#  include <sched.h>
#endif /* HAVE_SCHED_H */
#ifdef HAVE_SYS_CPUSET_H
#  include <sys/cpuset.h>
#endif /* HAVE_SYS_CPUSET_H */

#if ! defined(HAS_DECL_CPU_SETSIZE) && ! defined(CPU_SETSIZE)
#  define CPU_SETSIZE (1024)
#  define CPU_SETSIZE_SAFE (64)
#else  /* HAS_DECL_CPU_SETSIZE || CPU_SETSIZE */
#  define CPU_SETSIZE_SAFE CPU_SETSIZE
#endif /* HAS_DECL_CPU_SETSIZE || CPU_SETSIZE */

/* Check and fix possible missing macros */
#if ! defined(HAS_DECL_CTL_HW) && defined(CTL_HW)
#  define HAS_DECL_CTL_HW 1
#endif /* ! HAS_DECL_CTL_HW && CTL_HW */

#if ! defined(HAS_DECL_HW_NCPUONLINE) && defined(HW_NCPUONLINE)
#  define HAS_DECL_HW_NCPUONLINE 1
#endif /* ! HAS_DECL_HW_NCPUONLINE && HW_NCPUONLINE */

#if ! defined(HAS_DECL_HW_AVAILCPU) && defined(HW_AVAILCPU)
#  define HAS_DECL_HW_AVAILCPU 1
#endif /* ! HAS_DECL_HW_AVAILCPU && HW_AVAILCPU */

#if ! defined(HAS_DECL_HW_NCPU) && defined(HW_NCPU)
#  define HAS_DECL_HW_NCPU 1
#endif /* ! HAS_DECL_HW_NCPU && HW_NCPU */

#if ! defined(HAS_DECL__SC_NPROCESSORS_ONLN) && defined(_SC_NPROCESSORS_ONLN)
#  define HAS_DECL__SC_NPROCESSORS_ONLN 1
#endif /* ! HAS_DECL__SC_NPROCESSORS_ONLN && _SC_NPROCESSORS_ONLN */

#if ! defined(HAS_DECL__SC_NPROC_ONLN) && defined(_SC_NPROC_ONLN)
#  define HAS_DECL__SC_NPROC_ONLN 1
#endif /* ! HAS_DECL__SC_NPROC_ONLN && _SC_NPROC_ONLN */

#if ! defined(HAS_DECL__SC_CRAY_NCPU) && defined(_SC_CRAY_NCPU)
#  define HAS_DECL__SC_CRAY_NCPU 1
#endif /* ! HAS_DECL__SC_CRAY_NCPU && _SC_CRAY_NCPU */

#if ! defined(HAS_DECL__SC_NPROCESSORS_CONF) && defined(_SC_NPROCESSORS_CONF)
#  define HAS_DECL__SC_NPROCESSORS_CONF 1
#endif /* ! HAVE_DECL__SC_NPROCESSORS_CONF && _SC_NPROCESSORS_CONF */

/* Forward declarations */

static int
mhd_tool_get_sys_cpu_count_sysctl_ (void);

/**
 * Detect the number of logical CPU cores available for the process by
 * sched_getaffinity() (and related) function.
 * @return the number of detected logical CPU cores or
 *         -1 if failed to detect (or this function unavailable).
 */
static int
mhd_tool_get_proc_cpu_count_sched_getaffinity_ (void)
{
  int ret = -1;
#if defined(HAVE_SCHED_GETAFFINITY) && defined(HAVE_GETPID)
  /* Glibc style */
  if (0 >= ret)
  {
    cpu_set_t cur_set;
    if (0 == sched_getaffinity (getpid (), sizeof(cur_set), &cur_set))
    {
#ifdef HAVE_CPU_COUNT
      ret = CPU_COUNT (&cur_set);
#else  /* ! HAVE_CPU_COUNT */
      unsigned int i;
      ret = 0;
      for (i = 0; i < CPU_SETSIZE_SAFE; ++i)
      {
        if (CPU_ISSET (i, &cur_set))
          ++ret;
      }
      if (0 == ret)
        ret = -1;
#endif /* ! HAVE_CPU_COUNT */
    }
  }
#ifdef HAVE_CPU_COUNT_S
  if (0 >= ret)
  {
    /* Use 256 times larger size than size for default maximum CPU number.
       Hopefully it would be enough even for exotic situations. */
    static const unsigned int set_size_cpus = 256 * CPU_SETSIZE;
    const size_t set_size_bytes = CPU_ALLOC_SIZE (set_size_cpus);
    cpu_set_t *p_set;

    p_set = CPU_ALLOC (set_size_cpus);
    if (NULL != p_set)
    {
      if (0 == sched_getaffinity (getpid (), set_size_bytes, p_set))
      {
#ifndef MHD_FUNC_CPU_COUNT_S_GETS_CPUS
        ret = CPU_COUNT_S (set_size_bytes, p_set);
#else  /* MHD_FUNC_CPU_COUNT_S_GETS_CPUS */
        ret = CPU_COUNT_S (set_size_cpus, p_set);
#endif /* MHD_FUNC_CPU_COUNT_S_GETS_CPUS */
      }
      CPU_FREE (p_set);
    }
  }
#endif /* HAVE_CPU_COUNT_S */
#endif /* HAVE_SCHED_GETAFFINITY && HAVE_GETPID */
  if (0 >= ret)
    return -1;
  return ret;
}


/**
 * Detect the number of logical CPU cores available for the process by
 * cpuset_getaffinity() function.
 * @return the number of detected logical CPU cores or
 *         -1 if failed to detect (or this function unavailable).
 */
static int
mhd_tool_get_proc_cpu_count_cpuset_getaffinity_ (void)
{
  int ret = -1;
#if defined(HAVE_CPUSET_GETAFFINITY)
  /* FreeBSD style */
  if (0 >= ret)
  {
    cpuset_t cur_mask;
    /* The should get "anonymous" mask/set. The anonymous mask is always
       a subset of the assigned set (which is a subset of the root set). */
    if (0 == cpuset_getaffinity (CPU_LEVEL_WHICH, CPU_WHICH_PID, (id_t) -1,
                                 sizeof(cur_mask), &cur_mask))
    {
#ifdef HAVE_CPU_COUNT
      ret = CPU_COUNT (&cur_mask);
#else  /* ! HAVE_CPU_COUNT */
      unsigned int i;
      ret = 0;
      for (i = 0; i < CPU_SETSIZE_SAFE; ++i)
      {
        if (CPU_ISSET (i, &cur_mask))
          ++ret;
      }
      if (0 == ret)
        ret = -1;
#endif /* ! HAVE_CPU_COUNT */
    }
  }
#ifdef HAVE_CPU_COUNT_S
  if (0 >= ret)
  {
    /* Use 256 times larger size than size for default maximum CPU number.
       Hopefully it would be enough even for exotic situations. */
    static const unsigned int mask_size_cpus = 256 * CPU_SETSIZE;
    const size_t mask_size_bytes = CPU_ALLOC_SIZE (mask_size_cpus);
    cpuset_t *p_mask;

    p_mask = CPU_ALLOC (mask_size_cpus);
    if (NULL != p_mask)
    {
      if (0 == cpuset_getaffinity (CPU_LEVEL_WHICH, CPU_WHICH_PID, (id_t) -1,
                                   mask_size_bytes, p_mask))
      {
#ifndef MHD_FUNC_CPU_COUNT_S_GETS_CPUS
        ret = CPU_COUNT_S (mask_size_bytes, p_mask);
#else  /* MHD_FUNC_CPU_COUNT_S_GETS_CPUS */
        ret = CPU_COUNT_S (mask_size_cpus, p_mask);
#endif /* MHD_FUNC_CPU_COUNT_S_GETS_CPUS */
      }
      CPU_FREE (p_mask);
    }
  }
#endif /* HAVE_CPU_COUNT_S */
#endif /* HAVE_CPUSET_GETAFFINITY */
  if (0 >= ret)
    return -1;
  return ret;
}


/**
 * Detect the number of logical CPU cores available for the process by
 * sched_getaffinity_np() (and related) function.
 * @return the number of detected logical CPU cores or
 *         -1 if failed to detect (or this function unavailable).
 */
static int
mhd_tool_get_proc_cpu_count_sched_getaffinity_np_ (void)
{
  int ret = -1;
#if defined(HAVE_SCHED_GETAFFINITY_NP) && defined(HAVE_GETPID)
  /* NetBSD style */
  cpuset_t *cpuset_ptr;
  cpuset_ptr = cpuset_create ();
  if (NULL != cpuset_ptr)
  {
    if (0 == sched_getaffinity_np (getpid (), cpuset_size (cpuset_ptr),
                                   cpuset_ptr))
    {
      cpuid_t cpu_num;
#if defined(HAVE_SYSCONF) && defined(HAVE_DECL__SC_NPROCESSORS_CONF)
      unsigned int max_num = 0;
      long sc_value = -1;
      sc_value = sysconf (_SC_NPROCESSORS_ONLN);
      if (0 < sc_value)
        max_num = (unsigned int) sc_value;
      if (0 < max_num)
      {
        ret = 0;
        for (cpu_num = 0; cpu_num < max_num; ++cpu_num)
          if (0 < cpuset_isset (cpu_num, cpuset_ptr))
            ++ret;
      }
      else /* Combined with the next 'if' */
#endif /* HAVE_SYSCONF && HAVE_DECL__SC_NPROCESSORS_CONF */
      if (1)
      {
        int res;
        cpu_num = 0;
        ret = 0;
        do
        {
          res = cpuset_isset (cpu_num++, cpuset_ptr);
          if (0 < res)
            ++ret;
        } while (0 <= res);
      }
#ifdef __NetBSD__
      if (0 == ret)
      {
        /* On NetBSD "unset" affinity (exactly zero CPUs) means
           "all CPUs are available". */
        ret = mhd_tool_get_sys_cpu_count_sysctl_ ();
      }
#endif /* __NetBSD__ */
    }
    cpuset_destroy (cpuset_ptr);
  }
#endif /* HAVE_SCHED_GETAFFINITY_NP && HAVE_GETPID */
  if (0 >= ret)
    return -1;
  return ret;
}


/**
 * Detect the number of logical CPU cores available for the process.
 * The number of cores available for this process could be different from
 * value of cores available on the system. The OS may have limit on number
 * assigned/allowed cores for single process and process may have limited
 * CPU affinity.
 * @return the number of logical CPU cores available for the process or
 *         -1 if failed to detect
 */
int
mhd_tool_get_proc_cpu_count (void)
{
  int res;

#if defined(__linux__) || defined(__GLIBC__)
  /* On Linux kernel try first 'sched_getaffinity()' as it should be
     the native API.
     Also try it first on other kernels if Glibc is used. */
  res = mhd_tool_get_proc_cpu_count_sched_getaffinity_ ();
  if (0 < res)
    return res;

  res = mhd_tool_get_proc_cpu_count_cpuset_getaffinity_ ();
  if (0 < res)
    return res;
#else  /* ! __linux__ && ! __GLIBC__ */
  /* On non-Linux kernels 'cpuset_getaffinity()' could be the native API,
     while 'sched_getaffinity()' could be implemented in compatibility layer. */
  res = mhd_tool_get_proc_cpu_count_cpuset_getaffinity_ ();
  if (0 < res)
    return res;

  res = mhd_tool_get_proc_cpu_count_sched_getaffinity_ ();
  if (0 < res)
    return res;
#endif /* ! __linux__ && ! __GLIBC__ */

  res = mhd_tool_get_proc_cpu_count_sched_getaffinity_np_ ();
  if (0 < res)
    return res;

  return -1;
}


/**
 * Detect the number of processors by special API functions
 * @return number of processors as returned by special API functions or
 *         -1 in case of error or special API functions unavailable
 */
static int
mhd_tool_get_sys_cpu_count_special_api_ (void)
{
  int ret = -1;
#ifdef HAVE_PSTAT_GETDYNAMIC
  if (0 >= ret)
  {
    /* HP-UX things */
    struct pst_dynamic psd_data;
    memset ((void *) &psd_data, 0, sizeof(psd_data));
    if (1 == pstat_getdynamic (&psd_data, sizeof(psd_data), (size_t) 1, 0))
    {
      if (0 < psd_data.psd_proc_cnt)
        ret = (int) psd_data.psd_proc_cnt;
    }
  }
#endif /* HAVE_PSTAT_GETDYNAMIC */
#ifdef HAVE_VXCPUENABLEDGET
  if (0 >= ret)
  {
    /* VxWorks */
    cpuset_t enb_set;
    enb_set = vxCpuEnabledGet ();
    /* Count set bits */
    for (ret = 0; 0 != enb_set; enb_set &= enb_set - 1)
      ++ret;
  }
#endif /* HAVE_VXCPUENABLEDGET */
#if defined(_WIN32) && ! defined (__CYGWIN__)
  if (0 >= ret)
  {
    /* Native W32 */
    HMODULE k32hndl;
    k32hndl = GetModuleHandleA ("kernel32.dll");
    if (NULL != k32hndl)
    {
      typedef DWORD (WINAPI *GAPC_PTR)(WORD GroupNumber);
      GAPC_PTR ptrGetActiveProcessorCount;
      /* Available on W7 or later */
      ptrGetActiveProcessorCount =
        (GAPC_PTR) (void *) GetProcAddress (k32hndl, "GetActiveProcessorCount");
      if (NULL != ptrGetActiveProcessorCount)
      {
        DWORD res;
        res = ptrGetActiveProcessorCount (ALL_PROCESSOR_GROUPS);
        ret = (int) res;
        if (res != (DWORD) ret)
          ret = -1; /* Overflow */
      }
    }
    if ((0 >= ret) && (NULL != k32hndl))
    {
      typedef void (WINAPI *GNSI_PTR)(SYSTEM_INFO *pSysInfo);
      GNSI_PTR ptrGetNativeSystemInfo;
      /* May give incorrect (low) result on versions from W7 to W11
         when more then 64 CPUs are available */
      ptrGetNativeSystemInfo =
        (GNSI_PTR) (void *) GetProcAddress (k32hndl, "GetNativeSystemInfo");
      if (NULL != ptrGetNativeSystemInfo)
      {
        SYSTEM_INFO sysInfo;

        memset ((void *) &sysInfo, 0, sizeof(sysInfo));
        ptrGetNativeSystemInfo (&sysInfo);
        ret = (int) sysInfo.dwNumberOfProcessors;
        if (sysInfo.dwNumberOfProcessors != (DWORD) ret)
          ret = -1; /* Overflow */
      }
    }
  }
  if (0 >= ret)
  {
    /* May give incorrect (low) result on versions from W7 to W11
       when more then 64 CPUs are available */
    SYSTEM_INFO sysInfo;
    memset ((void *) &sysInfo, 0, sizeof(sysInfo));
    GetSystemInfo (&sysInfo);
    ret = (int) sysInfo.dwNumberOfProcessors;
    if (sysInfo.dwNumberOfProcessors != (DWORD) ret)
      ret = -1; /* Overflow */
  }
#endif /* _WIN32 && ! __CYGWIN__ */
  if (0 >= ret)
    return -1;
  return ret;
}


/**
 * Detect the number of processors by sysctl*() functions
 * @return number of processors as returned by 'sysctl*' functions or
 *         -1 in case of error or the number cannot be detected
 *         by these functions
 */
static int
mhd_tool_get_sys_cpu_count_sysctl_ (void)
{
  int ret = -1;
  /* Do not use sysctl() function on GNU/Linux even if
     sysctl() is available */
#ifndef __linux__
#ifdef HAVE_SYSCTLBYNAME
  if (0 >= ret)
  {
    size_t value_size = sizeof(ret);
    /* Darwin: The number of available logical CPUs */
    if ((0 != sysctlbyname ("hw.logicalcpu", &ret, &value_size,
                            NULL, 0))
        || (sizeof(ret) != value_size))
      ret = -1;
  }
  if (0 >= ret)
  {
    size_t value_size = sizeof(ret);
    /* FreeBSD: The number of online CPUs */
    if ((0 != sysctlbyname ("kern.smp.cpus", &ret, &value_size,
                            NULL, 0))
        || (sizeof(ret) != value_size))
      ret = -1;
  }
  if (0 >= ret)
  {
    size_t value_size = sizeof(ret);
    /* Darwin: The current number of CPUs available to run threads */
    if ((0 != sysctlbyname ("hw.activecpu", &ret, &value_size,
                            NULL, 0))
        || (sizeof(ret) != value_size))
      ret = -1;
  }
  if (0 >= ret)
  {
    size_t value_size = sizeof(ret);
    /* OpenBSD, NetBSD: The number of online CPUs */
    if ((0 != sysctlbyname ("hw.ncpuonline", &ret, &value_size,
                            NULL, 0))
        || (sizeof(ret) != value_size))
      ret = -1;
  }
  if (0 >= ret)
  {
    size_t value_size = sizeof(ret);
    /* Darwin: The old/alternative name for "hw.activecpu" */
    if ((0 != sysctlbyname ("hw.availcpu", &ret, &value_size,
                            NULL, 0))
        || (sizeof(ret) != value_size))
      ret = -1;
  }
#endif /* HAVE_SYSCTLBYNAME */
#if defined(HAVE_SYSCTL) && \
  defined(HAS_DECL_CTL_HW) && \
  defined(HAS_DECL_HW_NCPUONLINE)
  if (0 >= ret)
  {
    /* OpenBSD, NetBSD: The number of online CPUs */
    int mib[2] = { CTL_HW, HW_NCPUONLINE };
    size_t value_size = sizeof(ret);
    if ((0 != sysctl (mib, 2, &ret, &value_size, NULL, 0))
        || (sizeof(ret) != value_size))
      ret = -1;
  }
#endif /* HAVE_SYSCTL && HAS_DECL_CTL_HW && HAS_DECL_HW_NCPUONLINE */
#if defined(HAVE_SYSCTL) && \
  defined(HAS_DECL_CTL_HW) && \
  defined(HAS_DECL_HW_AVAILCPU)
  if (0 >= ret)
  {
    /* Darwin: The MIB name for "hw.activecpu" */
    int mib[2] = { CTL_HW, HW_AVAILCPU };
    size_t value_size = sizeof(ret);
    if ((0 != sysctl (mib, 2, &ret, &value_size, NULL, 0))
        || (sizeof(ret) != value_size))
      ret = -1;
  }
#endif /* HAVE_SYSCTL && HAS_DECL_CTL_HW && HAS_DECL_HW_AVAILCPU */
#ifdef HAVE_SYSCTLBYNAME
  if (0 >= ret)
  {
    size_t value_size = sizeof(ret);
    /* FreeBSD, OpenBSD, NetBSD, Darwin (and others?): The number of CPUs */
    if ((0 != sysctlbyname ("hw.ncpu", &ret, &value_size,
                            NULL, 0))
        || (sizeof(ret) != value_size))
      ret = -1;
  }
#endif /* HAVE_SYSCTLBYNAME */
#if defined(HAVE_SYSCTL) && \
  defined(HAS_DECL_CTL_HW) && \
  defined(HAS_DECL_HW_NCPU)
  if (0 >= ret)
  {
    /* FreeBSD, OpenBSD, NetBSD, Darwin (and others?): The number of CPUs */
    int mib[2] = { CTL_HW, HW_NCPU };
    size_t value_size = sizeof(ret);
    if ((0 != sysctl (mib, 2, &ret, &value_size, NULL, 0))
        || (sizeof(ret) != value_size))
      ret = -1;
  }
#endif /* HAVE_SYSCTL && HAS_DECL_CTL_HW && HAS_DECL_HW_NCPU */
#endif /* ! __linux__ */
  if (0 >= ret)
    return -1;
  return ret;
}


/**
 * Detect the number of processors by sysconf() function
 * @return number of processors as returned by 'sysconf' function or
 *         -1 in case of error or 'sysconf' unavailable
 */
static int
mhd_tool_get_sys_cpu_count_sysconf_ (void)
{
  int ret = -1;
#if defined(HAVE_SYSCONF) && \
  (defined(HAS_DECL__SC_NPROCESSORS_ONLN) || \
  defined(HAS_DECL__SC_NPROC_ONLN) || \
  defined(HAS_DECL__SC_CRAY_NCPU))
  long value = -1;
#ifdef HAS_DECL__SC_NPROCESSORS_ONLN
  if (0 >= value)
    value = sysconf (_SC_NPROCESSORS_ONLN);
#endif /* HAS_DECL__SC_NPROCESSORS_ONLN */
#ifdef HAS_DECL__SC_NPROC_ONLN
  if (0 >= value)
    value = sysconf (_SC_NPROC_ONLN);
#endif /* HAS_DECL__SC_NPROC_ONLN */
#ifdef HAS_DECL__SC_CRAY_NCPU
  if (0 >= value)
    value = sysconf (_SC_CRAY_NCPU);
#endif /* HAS_DECL__SC_CRAY_NCPU */
  if (0 >= value)
    return -1;
  ret = (int) value;
  if ((long) ret != value)
    return -1; /* Overflow */
#endif /* HAVE_SYSCONF &&
          (HAS_DECL__SC_NPROCESSORS_ONLN || HAS_DECL__SC_NPROC_ONLN ||
           HAS_DECL__SC_CRAY_NCPU) */
  return ret;
}


/**
 * Try to detect the number of logical CPU cores available for the system.
 * The number of available logical CPU cores could be changed any time due to
 * CPU hotplug.
 * @return the number of logical CPU cores available,
 *         -1 if failed to detect.
 */
int
mhd_tool_get_system_cpu_count (void)
{
  int res;

  /* Try specialised APIs first */
  res = mhd_tool_get_sys_cpu_count_special_api_ ();
  if (0 < res)
    return res;

  /* Try sysctl*(). This is typically a direct interface to
     kernel values. */
  res = mhd_tool_get_sys_cpu_count_sysctl_ ();
  if (0 < res)
    return res;

  /* Try sysconf() as the last resort as this is a generic interface
     which can be implemented by parsing system files. */
  res = mhd_tool_get_sys_cpu_count_sysconf_ ();
#if ! defined(__linux__) && ! defined(__GLIBC__)
  if (0 < res)
    return res;
#else  /* __linux__ || __GLIBC__ */
  if (2 < res)
    return res;
  if (0 < res)
  {
    /* '1' or '2' could a be fallback number.
     * See get_nprocs_fallback() in glibc
       sysdeps/unix/sysv/linux/getsysstats.c */

    int proc_cpu_count;

    proc_cpu_count = mhd_tool_get_proc_cpu_count ();
    if ((0 < proc_cpu_count) && (proc_cpu_count <= res))
    {
      /* The detected number of CPUs available for the process
         is 1 or 2 and fits detected number of system CPUS.
         Assume detected number is correct. */
      return res;
    }
  }
#endif /* __linux__ || __GLIBC__  */
  return -1; /* Cannot detect */
}