Added autodetection of processor cores and used it as the default if an
invalid value is used for threads.
This commit is contained in:
Markus Makela
2015-10-31 07:36:22 +02:00
parent 112e21d507
commit 6810ed15dd
6 changed files with 35 additions and 5 deletions

View File

@ -50,11 +50,13 @@ Please see the section about [Protocol Modules](#protocol-modules) for more deta
### Global Settings ### Global Settings
The global settings, in a section named `[MaxScale]`, allow various parameters that affect MaxScale as a whole to be tuned. Currently the only setting that is supported is the number of threads to use to handle the network traffic. MaxScale will also accept the section name of `[gateway]` for global settings. This is for backward compatibility with versions prior to the naming of MaxScale. The global settings, in a section named `[MaxScale]`, allow various parameters that affect MaxScale as a whole to be tuned.
#### `threads` #### `threads`
To control the number of threads that poll for network traffic set the parameter threads to a number. It is recommended that you start with a single thread and add more as you find the performance is not satisfactory. MaxScale is implemented to be very thread efficient, so a small number of threads is usually adequate to support reasonably heavy workloads. Adding more threads may not improve performance and can consume resources needlessly. This parameter controls the number of threads that poll for network traffic. MaxScale will autodetect the number of processors of the system unless number of threads is manually configured. It is recommended that you let MaxScale detect how many cores the system has and leave this parameter undefined. The number of used cores will be logged into the message logs and if you are not satisfied with the autodetected value, you can manually configure it.
If you want to fine-tune the number of threads, start with a single thread and add more as you find the performance is not satisfactory. MaxScale is implemented to be very thread efficient, so a small number of threads is usually adequate to support reasonably heavy workloads. Adding more threads may not improve performance and can consume resources needlessly. Increasing the amount of worker threads beyond the number of processor cores is not recommended.
``` ```
# Valid options are: # Valid options are:

View File

@ -75,6 +75,7 @@
#include <sys/utsname.h> #include <sys/utsname.h>
#include <pcre.h> #include <pcre.h>
#include <dbusers.h> #include <dbusers.h>
#include <gw.h>
#define PCRE2_CODE_UNIT_WIDTH 8 #define PCRE2_CODE_UNIT_WIDTH 8
#include <pcre2.h> #include <pcre2.h>
@ -1494,6 +1495,15 @@ CONFIG_PARAMETER *p1, *p2;
int int
config_threadcount() config_threadcount()
{ {
spinlock_acquire(&gateway.lock);
if(gateway.n_threads <= 0)
{
int nthr = get_processor_count();
skygw_log_write(LE, "Warning: Invalid value for 'threads': %d. Using default "
"number of %d threads.", gateway.n_threads, nthr);
gateway.n_threads = nthr;
}
spinlock_release(&gateway.lock);
return gateway.n_threads; return gateway.n_threads;
} }
@ -1657,7 +1667,8 @@ global_defaults()
{ {
uint8_t mac_addr[6]=""; uint8_t mac_addr[6]="";
struct utsname uname_data; struct utsname uname_data;
gateway.n_threads = 1; spinlock_init(&gateway.lock);
gateway.n_threads = get_processor_count();
gateway.n_nbpoll = DEFAULT_NBPOLLS; gateway.n_nbpoll = DEFAULT_NBPOLLS;
gateway.pollsleep = DEFAULT_POLLSLEEP; gateway.pollsleep = DEFAULT_POLLSLEEP;
gateway.auth_conn_timeout = DEFAULT_AUTH_CONNECT_TIMEOUT; gateway.auth_conn_timeout = DEFAULT_AUTH_CONNECT_TIMEOUT;

View File

@ -226,3 +226,17 @@ struct hostent *hp;
addr->sin_port = htons(pnum); addr->sin_port = htons(pnum);
return 1; return 1;
} }
/**
* Return the number of processors available.
* @return Number of processors or 1 if the required definition of _SC_NPROCESSORS_CONF
* is not found
*/
long get_processor_count()
{
long processors = 1;
#ifdef _SC_NPROCESSORS_CONF
processors = sysconf(_SC_NPROCESSORS_CONF);
#endif
return processors;
}

View File

@ -88,4 +88,5 @@ int gw_getsockerrno(int fd);
int parse_bindconfig(char *, unsigned short, struct sockaddr_in *); int parse_bindconfig(char *, unsigned short, struct sockaddr_in *);
int setipaddress(struct in_addr *, char *); int setipaddress(struct in_addr *, char *);
char* get_libdir(); char* get_libdir();
long get_processor_count();
#endif #endif

View File

@ -20,6 +20,7 @@
#include <skygw_utils.h> #include <skygw_utils.h>
#include <stdint.h> #include <stdint.h>
#include <openssl/sha.h> #include <openssl/sha.h>
#include <spinlock.h>
/** /**
* @file config.h The configuration handling elements * @file config.h The configuration handling elements
* *
@ -96,6 +97,7 @@ typedef struct config_context {
* The gateway global configuration data * The gateway global configuration data
*/ */
typedef struct { typedef struct {
SPINLOCK lock; /*< Lock used when accessing the global configuration */
int n_threads; /**< Number of polling threads */ int n_threads; /**< Number of polling threads */
char *version_string; /**< The version string of embedded database library */ char *version_string; /**< The version string of embedded database library */
char release_string[_SYSNAME_STR_LENGTH]; /**< The release name string of the system */ char release_string[_SYSNAME_STR_LENGTH]; /**< The release name string of the system */

View File

@ -3,12 +3,12 @@
# Global parameters # Global parameters
# #
# Set the number of threads to a value equal to the number of CPU cores. # Number of threads is autodetected, uncomment for manual configuration
# Complete list of configuration options: # Complete list of configuration options:
# https://github.com/mariadb-corporation/MaxScale/blob/master/Documentation/Getting-Started/Configuration-Guide.md # https://github.com/mariadb-corporation/MaxScale/blob/master/Documentation/Getting-Started/Configuration-Guide.md
[maxscale] [maxscale]
threads=4 #threads=8
# Server definitions # Server definitions
# #