OpenVPN 3 Core Library
Loading...
Searching...
No Matches
core.hpp
Go to the documentation of this file.
1// OpenVPN -- An application to securely tunnel IP networks
2// over a single port, with support for SSL/TLS-based
3// session authentication and key exchange,
4// packet encryption, packet authentication, and
5// packet compression.
6//
7// Copyright (C) 2012- OpenVPN Inc.
8//
9// SPDX-License-Identifier: MPL-2.0 OR AGPL-3.0-only WITH openvpn3-openssl-exception
10//
11
12// Linux methods for enumerating the number of cores on machine,
13// and binding a thread to a particular core.
14
15#pragma once
16
17#include <thread>
18
20
21#if defined(__APPLE__)
22#include <sys/types.h>
23#include <sys/sysctl.h>
24#elif defined(OPENVPN_PLATFORM_LINUX)
25#include <unistd.h>
26#elif defined(OPENVPN_PLATFORM_WIN)
27#include <windows.h>
28#endif
29
30namespace openvpn {
31
32inline int n_cores()
33{
34 int count = std::thread::hardware_concurrency();
35 // C++11 allows thread::hardware_concurrency() to return 0, fall back
36 // to specific solution if we detect this
37 if (count > 0)
38 return count;
39
40#if defined(__APPLE__)
41 size_t count_len = sizeof(count);
42 if (::sysctlbyname("hw.logicalcpu", &count, &count_len, NULL, 0) != 0)
43 count = 1;
44 return count;
45#elif defined(OPENVPN_PLATFORM_LINUX)
46 long ret = ::sysconf(_SC_NPROCESSORS_ONLN);
47 if (ret <= 0)
48 ret = 1;
49 return static_cast<int>(ret);
50#elif defined(OPENVPN_PLATFORM_WIN)
51 SYSTEM_INFO si;
52 ::GetSystemInfo(&si);
53 return si.dwNumberOfProcessors;
54#else
55 return 1;
56#endif
57}
58
59} // namespace openvpn
Support deferred server-side state creation when client connects.
Definition ovpncli.cpp:95
int n_cores()
Definition core.hpp:32
std::string ret