lvlhash for ports moved to separate files for future re-use.

This commit is contained in:
Max Romanov
2017-06-23 19:19:41 +03:00
parent 58e20da984
commit 0fdcdde6c8
6 changed files with 190 additions and 102 deletions

View File

@@ -18,6 +18,7 @@ NXT_LIB_DEPS=" \
src/nxt_process.h \ src/nxt_process.h \
src/nxt_signal.h \ src/nxt_signal.h \
src/nxt_port.h \ src/nxt_port.h \
src/nxt_port_hash.h \
src/nxt_port_memory.h \ src/nxt_port_memory.h \
src/nxt_dyld.h \ src/nxt_dyld.h \
src/nxt_thread.h \ src/nxt_thread.h \
@@ -70,6 +71,7 @@ NXT_LIB_DEPS=" \
src/nxt_application.h \ src/nxt_application.h \
src/nxt_master_process.h \ src/nxt_master_process.h \
src/nxt_router.h \ src/nxt_router.h \
src/nxt_port_hash.h \
" "
NXT_LIB_SRCS=" \ NXT_LIB_SRCS=" \
@@ -142,6 +144,7 @@ NXT_LIB_SRCS=" \
src/nxt_controller.c \ src/nxt_controller.c \
src/nxt_router.c \ src/nxt_router.c \
src/nxt_application.c \ src/nxt_application.c \
src/nxt_port_hash.c \
" "
NXT_LIB_SRC0=" \ NXT_LIB_SRC0=" \

View File

@@ -158,6 +158,7 @@ typedef struct nxt_upstream_source_s nxt_upstream_source_t;
#include <nxt_http_source.h> #include <nxt_http_source.h>
#include <nxt_fastcgi_source.h> #include <nxt_fastcgi_source.h>
#include <nxt_runtime.h> #include <nxt_runtime.h>
#include <nxt_port_hash.h>
#if (NXT_LIB_UNIT_TEST) #if (NXT_LIB_UNIT_TEST)

139
src/nxt_port_hash.c Normal file
View File

@@ -0,0 +1,139 @@
/*
* Copyright (C) Max Romanov
* Copyright (C) NGINX, Inc.
*/
#include <nxt_port_hash.h>
// Explicitly using 32 bit types to avoid possible alignment.
typedef struct {
int32_t pid;
uint32_t port_id;
} nxt_pid_port_id_t;
static nxt_int_t
nxt_port_hash_test(nxt_lvlhsh_query_t *lhq, void *data)
{
nxt_port_t *port;
nxt_pid_port_id_t *pid_port_id;
port = data;
pid_port_id = (nxt_pid_port_id_t *) lhq->key.start;
if (lhq->key.length == sizeof(nxt_pid_port_id_t) &&
pid_port_id->pid == port->pid &&
pid_port_id->port_id == port->id) {
return NXT_OK;
}
return NXT_DECLINED;
}
static const nxt_lvlhsh_proto_t lvlhsh_ports_proto nxt_aligned(64) = {
NXT_LVLHSH_DEFAULT,
nxt_port_hash_test,
nxt_lvlhsh_alloc,
nxt_lvlhsh_free,
};
nxt_port_t *
nxt_port_hash_first(nxt_lvlhsh_t *port_hash, nxt_lvlhsh_each_t *lhe)
{
nxt_memzero(lhe, sizeof(nxt_lvlhsh_each_t));
lhe->proto = &lvlhsh_ports_proto;
return nxt_port_hash_next(port_hash, lhe);
}
void
nxt_port_hash_add(nxt_lvlhsh_t *port_hash, nxt_mp_t *mem_pool,
nxt_port_t *port)
{
nxt_pid_port_id_t pid_port;
nxt_lvlhsh_query_t lhq;
pid_port.pid = port->pid;
pid_port.port_id = port->id;
lhq.key_hash = nxt_murmur_hash2(&pid_port, sizeof(pid_port));
lhq.key.length = sizeof(pid_port);
lhq.key.start = (u_char *) &pid_port;
lhq.proto = &lvlhsh_ports_proto;
lhq.replace = 0;
lhq.value = port;
lhq.pool = mem_pool;
switch (nxt_lvlhsh_insert(port_hash, &lhq)) {
case NXT_OK:
break;
default:
nxt_thread_log_error(NXT_LOG_WARN, "port #%d for pid %PI add failed",
port->id, port->pid);
break;
}
}
void
nxt_port_hash_remove(nxt_lvlhsh_t *port_hash, nxt_mp_t *mem_pool,
nxt_port_t *port)
{
nxt_pid_port_id_t pid_port;
nxt_lvlhsh_query_t lhq;
pid_port.pid = port->pid;
pid_port.port_id = port->id;
lhq.key_hash = nxt_murmur_hash2(&pid_port, sizeof(pid_port));
lhq.key.length = sizeof(pid_port);
lhq.key.start = (u_char *) &pid_port;
lhq.proto = &lvlhsh_ports_proto;
lhq.replace = 0;
lhq.value = port;
lhq.pool = mem_pool;
switch (nxt_lvlhsh_delete(port_hash, &lhq)) {
case NXT_OK:
break;
default:
break;
}
}
nxt_port_t *
nxt_port_hash_find(nxt_lvlhsh_t *port_hash, nxt_pid_t pid,
nxt_port_id_t port_id)
{
nxt_pid_port_id_t pid_port;
nxt_lvlhsh_query_t lhq;
pid_port.pid = pid;
pid_port.port_id = port_id;
lhq.key_hash = nxt_murmur_hash2(&pid_port, sizeof(pid_port));
lhq.key.length = sizeof(pid_port);
lhq.key.start = (u_char *) &pid_port;
lhq.proto = &lvlhsh_ports_proto;
/* TODO lock ports */
if (nxt_lvlhsh_find(port_hash, &lhq) == NXT_OK) {
nxt_thread_log_debug("process port (%PI, %d) found", pid, port_id);
return lhq.value;
}
nxt_thread_log_debug("process port (%PI, %d) not found", pid, port_id);
return NULL;
}

42
src/nxt_port_hash.h Normal file
View File

@@ -0,0 +1,42 @@
/*
* Copyright (C) Max Romanov
* Copyright (C) NGINX, Inc.
*/
#ifndef _NXT_PORT_HASH_H_INCLUDED_
#define _NXT_PORT_HASH_H_INCLUDED_
#include <nxt_main.h>
void nxt_port_hash_add(nxt_lvlhsh_t *port_hash, nxt_mp_t *mem_pool,
nxt_port_t *port);
void nxt_port_hash_remove(nxt_lvlhsh_t *port_hash, nxt_mp_t *mem_pool,
nxt_port_t *port);
nxt_port_t *nxt_port_hash_find(nxt_lvlhsh_t *port_hash, nxt_pid_t pid,
nxt_port_id_t port_id);
nxt_port_t *nxt_port_hash_first(nxt_lvlhsh_t *port_hash,
nxt_lvlhsh_each_t *lhe);
#define nxt_port_hash_next(port_hash, lhe) \
nxt_lvlhsh_each((port_hash), (lhe))
#define nxt_port_hash_each(port_hash, port) \
do { \
nxt_lvlhsh_each_t _lhe; \
\
for (port = nxt_port_hash_first((port_hash), &_lhe); \
port != NULL; \
port = nxt_port_hash_next((port_hash), &_lhe)) { \
#define nxt_port_hash_loop \
} \
} while(0)
#endif /* _NXT_PORT_HASH_H_INCLIDED_ */

View File

@@ -1499,37 +1499,6 @@ static const nxt_lvlhsh_proto_t lvlhsh_processes_proto nxt_aligned(64) = {
nxt_lvlhsh_free, nxt_lvlhsh_free,
}; };
// Explicitly using 32 bit types to avoid possible alignment.
typedef struct {
int32_t pid;
uint32_t port_id;
} nxt_pid_port_id_t;
static nxt_int_t
nxt_runtime_lvlhsh_port_test(nxt_lvlhsh_query_t *lhq, void *data)
{
nxt_port_t *port;
nxt_pid_port_id_t *pid_port_id;
port = data;
pid_port_id = (nxt_pid_port_id_t *) lhq->key.start;
if (lhq->key.length == sizeof(nxt_pid_port_id_t) &&
pid_port_id->pid == port->pid &&
pid_port_id->port_id == port->id) {
return NXT_OK;
}
return NXT_DECLINED;
}
static const nxt_lvlhsh_proto_t lvlhsh_ports_proto nxt_aligned(64) = {
NXT_LVLHSH_DEFAULT,
nxt_runtime_lvlhsh_port_test,
nxt_lvlhsh_alloc,
nxt_lvlhsh_free,
};
nxt_process_t * nxt_process_t *
nxt_runtime_process_find(nxt_runtime_t *rt, nxt_pid_t pid) nxt_runtime_process_find(nxt_runtime_t *rt, nxt_pid_t pid)
@@ -1692,73 +1661,25 @@ nxt_runtime_process_first(nxt_runtime_t *rt, nxt_lvlhsh_each_t *lhe)
nxt_port_t * nxt_port_t *
nxt_runtime_port_first(nxt_runtime_t *rt, nxt_lvlhsh_each_t *lhe) nxt_runtime_port_first(nxt_runtime_t *rt, nxt_lvlhsh_each_t *lhe)
{ {
nxt_memzero(lhe, sizeof(nxt_lvlhsh_each_t)); return nxt_port_hash_first(&rt->ports, lhe);
lhe->proto = &lvlhsh_ports_proto;
return nxt_runtime_port_next(rt, lhe);
} }
void void
nxt_runtime_port_add(nxt_runtime_t *rt, nxt_port_t *port) nxt_runtime_port_add(nxt_runtime_t *rt, nxt_port_t *port)
{ {
nxt_pid_port_id_t pid_port;
nxt_lvlhsh_query_t lhq;
pid_port.pid = port->pid;
pid_port.port_id = port->id;
lhq.key_hash = nxt_murmur_hash2(&pid_port, sizeof(pid_port));
lhq.key.length = sizeof(pid_port);
lhq.key.start = (u_char *) &pid_port;
lhq.proto = &lvlhsh_ports_proto;
lhq.replace = 0;
lhq.value = port;
lhq.pool = rt->mem_pool;
/* TODO lock ports */ /* TODO lock ports */
switch (nxt_lvlhsh_insert(&rt->ports, &lhq)) { nxt_port_hash_add(&rt->ports, rt->mem_pool, port);
case NXT_OK:
break;
default:
nxt_thread_log_error(NXT_LOG_WARN, "port #%d for pid %PI add failed",
port->id, port->pid);
break;
}
} }
void void
nxt_runtime_port_remove(nxt_runtime_t *rt, nxt_port_t *port) nxt_runtime_port_remove(nxt_runtime_t *rt, nxt_port_t *port)
{ {
nxt_pid_port_id_t pid_port;
nxt_lvlhsh_query_t lhq;
pid_port.pid = port->pid;
pid_port.port_id = port->id;
lhq.key_hash = nxt_murmur_hash2(&pid_port, sizeof(pid_port));
lhq.key.length = sizeof(pid_port);
lhq.key.start = (u_char *) &pid_port;
lhq.proto = &lvlhsh_ports_proto;
lhq.replace = 0;
lhq.value = port;
lhq.pool = rt->mem_pool;
/* TODO lock ports */ /* TODO lock ports */
switch (nxt_lvlhsh_delete(&rt->ports, &lhq)) { nxt_port_hash_remove(&rt->ports, rt->mem_pool, port);
case NXT_OK:
break;
default:
break;
}
} }
@@ -1766,25 +1687,7 @@ nxt_port_t *
nxt_runtime_port_find(nxt_runtime_t *rt, nxt_pid_t pid, nxt_runtime_port_find(nxt_runtime_t *rt, nxt_pid_t pid,
nxt_port_id_t port_id) nxt_port_id_t port_id)
{ {
nxt_pid_port_id_t pid_port;
nxt_lvlhsh_query_t lhq;
pid_port.pid = pid;
pid_port.port_id = port_id;
lhq.key_hash = nxt_murmur_hash2(&pid_port, sizeof(pid_port));
lhq.key.length = sizeof(pid_port);
lhq.key.start = (u_char *) &pid_port;
lhq.proto = &lvlhsh_ports_proto;
/* TODO lock ports */ /* TODO lock ports */
if (nxt_lvlhsh_find(&rt->ports, &lhq) == NXT_OK) { return nxt_port_hash_find(&rt->ports, pid, port_id);
nxt_thread_log_debug("process port (%PI, %d) found", pid, port_id);
return lhq.value;
}
nxt_thread_log_debug("process port (%PI, %d) not found", pid, port_id);
return NULL;
} }

View File

@@ -112,7 +112,7 @@ nxt_port_t *nxt_runtime_port_first(nxt_runtime_t *rt,
nxt_lvlhsh_each_t *lhe); nxt_lvlhsh_each_t *lhe);
#define nxt_runtime_port_next(rt, lhe) \ #define nxt_runtime_port_next(rt, lhe) \
nxt_lvlhsh_each(&rt->ports, lhe) nxt_port_hash_next(&rt->ports, lhe)
/* STUB */ /* STUB */