Skip to content
Snippets Groups Projects
Commit 10f30614 authored by Marek Vavrusa's avatar Marek Vavrusa
Browse files

Estimate number of threads on runtime, cleanup in server.

parent 7571505f
No related branches found
No related tags found
No related merge requests found
......@@ -10,6 +10,11 @@ typedef unsigned int uint;
#define PROJECT_NAME "CuteDNS" // Project name
#define PROJECT_VER 0x000001 // 0xMMIIRR (MAJOR,MINOR,REVISION)
/* Server. */
#define SERVER_MGR_COUNT 2 // TCP + UDP
#define DEFAULT_THR_COUNT 2 // Default thread count for socket manager
#define DEFAULT_PORT 53531 // Default port
/* Common includes.
*/
......
......@@ -8,11 +8,6 @@
/*----------------------------------------------------------------------------*/
static const unsigned short DEFAULT_PORT = 53531;
static const int DEFAULT_THR_COUNT = 3;
/*----------------------------------------------------------------------------*/
cute_server *cute_create()
{
debug_server("Creating Server structure..\n");
......@@ -43,12 +38,16 @@ cute_server *cute_create()
debug_server("Done\n\n");
debug_server("Creating Socket Manager structure..\n");
// Estimate number of threads/manager
long thr_count = sm_estimate_threads();
debug_server("Estimated number of threads per manager: %ld\n", thr_count);
// Create socket handlers
server->manager[UDP] = sm_create(server->nameserver, &udp_master, &udp_worker, DEFAULT_THR_COUNT);
server->manager[TCP] = sm_create(server->nameserver, &tcp_master, &tcp_worker, 2*DEFAULT_THR_COUNT);
server->manager[UDP] = sm_create(server->nameserver, &udp_master, &udp_worker, thr_count);
server->manager[TCP] = sm_create(server->nameserver, &tcp_master, &tcp_worker, thr_count);
// Check socket handlers
for(int i = 0; i < 2; i++) {
for(int i = 0; i < SERVER_MGR_COUNT; i++) {
if (server->manager[i] == NULL ) {
if(i == 1) {
......@@ -130,7 +129,7 @@ int cute_wait(cute_server *server)
void cute_stop( cute_server *server )
{
// Notify servers to stop
for(int i = 0; i < 2; i++) {
for(int i = 0; i < SERVER_MGR_COUNT; i++) {
sm_stop(server->manager[i]);
}
}
......
......@@ -27,13 +27,17 @@
typedef struct cute_server {
/*! @brief Reference to the socket manager structures. */
sm_manager* manager[2]; // TCP + UDP
sm_manager* manager[SERVER_MGR_COUNT];
/*! @brief Reference to the name server structure. */
ns_nameserver *nameserver;
/*! @brief Reference to the zone database structure. */
zdb_database *zone_db;
/*! @brief @todo Server should have some state tracking.
uint state;
*/
} cute_server;
/*----------------------------------------------------------------------------*/
......
......@@ -354,6 +354,17 @@ void sm_destroy( sm_manager **manager )
*manager = NULL;
}
long sm_estimate_threads()
{
#ifdef _SC_NPROCESSORS_ONLN
long ret = sysconf(_SC_NPROCESSORS_ONLN);
if(ret >= 1)
return ret + 1;
#endif
return DEFAULT_THR_COUNT;
}
/*----------------------------------------------------------------------------*/
int sm_remove_event( int epfd, int socket )
......
......@@ -117,6 +117,13 @@ void sm_stop( sm_manager *manager );
*/
void sm_destroy( sm_manager **manager );
/** Return optimal number of threads for instance.
* It is estimated as NUM_CPUs + 1.
* Fallback is DEFAULT_THR_COUNT (\see common.h).
* \return number of threads
*/
long sm_estimate_threads();
/* APIs for extending Socket Manager.
* \todo Temporary APIs.
*/
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment