48 #define NCBI_USE_ERRCODE_X Connect_LBSM
51 #define MAX_IP_ADDR_LEN 16
54 #define LBSMD_LOCAL_BONUS 1.1
109 return h->
env ? (
const char*) h + h->
env : 0;
116 && v->
local && v->
local != (
unsigned int)(-1)) {
125 unsigned int localhost;
146 const char* arg,
size_t arglen,
147 const char*
val,
size_t vallen,
148 const char**
a,
char** v)
163 const char* p = strchr(c ==
env ? c : ++c,
'=');
168 if ((
size_t)(p - q) != arglen ||
strncasecmp(q, arg, arglen) != 0)
172 if (memchr(p + 1,
'!', (c ? (
size_t)(c - p) : strlen(p)) - 1))
174 for (q = p+1 + strspn(p+1,
" \t!"); ; q = p + strspn(p,
" \t!")) {
177 if (*q !=
'\0' && *q !=
'\n')
178 len = strcspn(q,
" \t!\n");
183 if (
len && *q ==
'~') {
188 if (
len == 1 && *q ==
'*') {
191 }
else if (
len == 1 && *q ==
'-') {
204 if (vlen == 2 && q[0] ==
'"' && q[1] ==
'"')
282 return (
int) copysign(1.0,
327 #if defined(_DEBUG) && !defined(NDEBUG)
328 static const unsigned char kZero[
sizeof(v->
cksum)] = { 0 };
329 const char* stop = (
const char*) &v->
cksum;
330 size_t len = (
size_t)(stop - base);
331 unsigned int (*update)(
unsigned int sum,
const void* ptr,
size_t len);
343 cksum = (*update)(cksum, base,
len);
344 cksum = (*update)(cksum, kZero,
sizeof(kZero));
346 cksum = (*update)(cksum, stop +
sizeof(kZero),
len);
347 return v->
cksum == cksum ? 1 : 0;
410 unsigned int count, cksum;
423 (
"Cached LBSM heap[%p, %p, %d] expired",
g_LBSM_Heap,
432 (
"Cached LBSM heap[%p, %p, %d] valid",
g_LBSM_Heap,
445 (
"Bad serial (%d) from LBSM heap attach", serial));
454 if (v && (
void*) v == base) {
456 (
"LBSM heap[%d] version mismatch"
457 " (current=%hu.%hu, expected=%u.%u+)",
462 (
"LBSM heap[%d] version information %s",
463 serial, v ?
"misplaced" :
"missing"));
468 int again = !retry && serial > 1? 1 : 0;
470 (
"LBSM heap[%d] out-of-date"
471 " (current=%lu, expiry=%lu, age=%lu, #%u)%s", serial,
472 (
unsigned long) now, (
unsigned long) v->
entry.
good,
473 (
unsigned long) now -(
unsigned long) v->
entry.
good,
474 v->
count, again ?
", re-trying" :
""));
484 (
"Cached LBSM heap[%p, %p, %d] dropped",
g_LBSM_Heap,
506 (
"Cached LBSM heap[%p, %p, %d] stale",
g_LBSM_Heap,
515 (
"Unable to copy LBSM heap[%d]", serial));
526 (
"Cached LBSM heap[%p, %p, %d] renewed",
535 (
"LBSM heap[%p, %p, %d]%s checksum failure%s",
537 retry == eAgain ?
" persistent" :
"",
538 retry ?
"" :
", re-trying"));
575 strcpy(
buf,
"(unknown)");
577 (
"Dynamic %s server `%s' on [%s] w/%s host entry",
579 (
const char*) svc + svc->
name,
580 buf, host ?
"outdated" :
"o"));
620 (
"LBSM heap[%p, %p, %d] for \"%s\" detected",
630 memset(idx, 0,
sizeof(idx));
633 n = n_cand = a_cand = 0;
638 name = *iter->
name ? iter->
name : 0;
671 if (dns_info_time < svc->
info.time)
676 if (dns_info_time < svc->
info.time)
737 }
else if (iter->
arglen) {
749 if (best_match >
match)
765 }
else if (standby < 0)
767 }
else if (standby) {
770 memset(idx, 0,
sizeof(idx));
771 for (
i = 0;
i < n_cand; ++
i) {
776 }
else for (
i = 0;
i < n_cand; ++
i)
781 if (n < iter->n_skip)
821 v ? (*v ? v :
"\"\"") :
"",
822 standby > 0 ?
", standby" :
""));
828 if (n_cand == a_cand) {
833 ? realloc(
cand,
n *
sizeof(*temp))
834 :
malloc (
n *
sizeof(*temp)));
842 assert((
size_t)
match <
sizeof(idx)/
sizeof(idx[0]));
846 for (
i =
match;
i <
sizeof(idx)/
sizeof(idx[0]); ++
i)
863 assert(!best_match || !idx[best_match - 1]);
864 for (
n = idx[best_match];
n < n_cand; ++
n) {
868 n_cand = idx[best_match];
885 n = (size_t) rand() %
n;
900 if (dns_info_time < svc->
info.time)
920 if (!svc && !dns_info_time)
937 (
const char*) svc + svc->
name :
"");
943 if ((host =
cand[
n].host) != 0) {
971 (
"LBSM heap[%p, %p, %d] for \"%s\" released",
heap,
982 (
"LBSM heap[%p, %p, %d] for \"%s\" retained",
heap,
999 rate, fine,
last->host,
last->port, 0);
1008 (
"LBSM heap[%p, %p, %d] for \"%s\" discarded",
1025 static void s_Fini(
void);
1038 (
"Cached LBSM heap[%p, %p, %d] destroyed",
g_LBSM_Heap,
1075 int no_dispd_follows)
1086 if (!no_dispd_follows)
1089 if (iter->
data == iter)
1091 if (!no_dispd_follows && !
tmp) {
1131 (
"LBSM heap[%p, %p, %d] released",
1151 size_t namelen = name && *name ? strlen(name) : 0;
1165 for (e =
env; *e; e =
env) {
1168 if (!(
env = strchr(e,
'\n'))) {
1172 len = (size_t)(
env++ - e);
1173 if (!(p = (
const char*) memchr(e,
'=',
len)))
1175 len = (size_t)(p - e);
1180 len = (size_t)(
env - ++p);
1186 (
"LBSM heap[%p, %p, %d] released",
1201 (
const SLBSM_Sysinfo*)((
const char*) hinfo +
sizeof(*hinfo));
1203 #if defined(_DEBUG) && !defined(NDEBUG) && defined(NETDAEMONS_VERSION_INT)
1205 static int s_Warn = 0;
1209 strcpy(addr,
"(unknown)");
1211 (
"HINFO may be incorrect for obsolete LBSMD at %s"
1212 " (detected=%hu.%hu.%hu, expected=%s+)", addr,
1227 return si->data.nrproc & 0xFF;
1234 return si->data.nrproc >> 8;
1242 return si->data.hzfreq / 0.128;
1249 return (
int)
si->load.nrtask;
1257 double pgsizemb =
si->data.pgsize / 1024.0;
1258 memusage[0] =
si->load.ram_total * pgsizemb;
1259 memusage[1] =
si->load.ram_cache * pgsizemb;
1260 memusage[2] =
si->load.ram_free * pgsizemb;
1261 memusage[3] =
si->load.swap_total * pgsizemb;
1262 memusage[4] =
si->load.swap_free * pgsizemb;
1273 unsigned short major = (
unsigned short)((
si->data.kernel >> 24) & 0xFF);
1274 unsigned short minor = (
unsigned short)((
si->data.kernel >> 16) & 0xFF);
1276 p->
bits =
si->data.machine >> 14;
1277 p->
arch = (
si->data.machine >> 8) & 0x3F;
1278 p->
ostype =
si->data.machine & 0xFF;
1279 p->
pgsize = (size_t)
si->data.pgsize << 10;
1287 p->
svcpack = (
unsigned short)(((major % div) << 8) |
1298 for (
n = 0;
n <
sizeof(
si->load.port)/
sizeof(
si->load.port[0]); ++
n) {
1299 if (!
si->load.port[
n])
1303 ports[
n].
used =
si->load.used[
n] / 2.0;
1315 lavg[0] =
si->load.avg;
1316 lavg[1] =
si->load.avgBLAST;
1325 status[0] =
si->load.status;
1326 status[1] =
si->load.statusBLAST;
1345 int dispd_to_follow)
1470 status *= (100. - (fine < 0. ? 0. : fine > 100. ? 100. : fine)) / 100.0;
1471 return fabs(status);
static int heap[2 *(256+1+29)+1]
static const char si[8][64]
static DLIST_TYPE *DLIST_NAME() last(DLIST_LIST_TYPE *list)
struct SHINFO_Params::@85 kernel
Kernel/OS version #, if available.
unsigned short port
Port number, host byte order.
TNCBI_Time bootup
System boot time, time_t-compatible.
TNCBI_Time startup
LBSMD start time, time_t-compatible.
void * HEAP_Base(const HEAP heap)
HEAP HEAP_Copy(const HEAP orig, size_t extra, int serial)
SSERV_Info * SERV_CreateDnsInfo(unsigned int host)
unsigned int HEAP_Destroy(HEAP heap)
TNcbiCapacity bits
Platform bitness, 32/64/32+64/0=unknown.
unsigned short svcpack
Kernel service pack (Hi=major, Lo=minor)
int HEAP_Serial(const HEAP heap)
SSERV_FirewallInfo firewall
struct SHINFO_Params::@86 daemon
LBSMD daemon version.
double used
Port usage as percentage, [0..100].
TNcbiOSType ostype
OS type ID, see enum, 0=unknown.
const char * SERV_TypeStr(ESERV_Type type)
unsigned int HEAP_AddRef(HEAP heap)
char * SERV_WriteInfo(const SSERV_Info *info)
unsigned short TSERV_TypeOnly
Server type only, w/o specials.
unsigned int HEAP_Detach(HEAP heap)
TNcbiArch arch
Architecture ID, see enum, 0=unknown.
TNCBI_Size HEAP_Size(const HEAP heap)
#define SERV_LOCALHOST
Special values for the "preferred_host" parameter.
int SERV_EqualInfo(const SSERV_Info *info1, const SSERV_Info *info2)
size_t pgsize
Hardware page size in bytes, if known.
@ fSERV_Stateless
Stateless servers only.
int SOCK_ntoa(unsigned int addr, char *buf, size_t bufsize)
Convert IP address to a string in dotted notation.
unsigned int SOCK_GetLocalHostAddress(ESwitch reget)
Get (and cache for faster follow-up retrievals) IPv4 address of local host.
unsigned int UTIL_Adler32_Update(unsigned int checksum, const void *ptr, size_t len)
Calculate/Update Adler-32 checksum NB: Initial checksum is "1".
enum ENcbiSwitch ESwitch
Aux.
#define NCBI_TIME_INFINITE
unsigned int UTIL_CRC32_Update(unsigned int checksum, const void *ptr, size_t len)
Calculate/Update CRC-32 checksum NB: Initial checksum is "0".
unsigned int
A callback function used to compare two keys in a database.
#define LBSM_DEFAULT_RATE
#define LBSM_STANDBY_THRESHOLD
HOST_INFO HINFO_Create(unsigned int addr, const void *hinfo, size_t hinfo_size, const char *env, const char *arg, const char *val)
size_t LB_Select(SERV_ITER iter, void *data, FGetCandidate get_candidate, double bonus)
const SLBSM_Host * LBSM_LookupHost(HEAP heap, unsigned int addr, const SLBSM_Entry *hint)
int LBSM_SubmitPenaltyOrRerate(const char *name, ESERV_Type type, double rate, TNCBI_Time fine, unsigned int host, unsigned short port, const char *path)
const char * LBSM_GetConfig(HEAP heap)
const SLBSM_Version * LBSM_GetVersion(HEAP heap)
const SLBSM_Service * LBSM_LookupService(HEAP heap, const char *name, int mask, const SLBSM_Service *hint)
#define LBSM_HEAP_VERSION_MIN_CRC32
#define LBSM_KERNELID_SVPKBIT
#define LBSM_HEAP_VERSION_MAJ_CRC32
#define LBSM_HEAP_VERSION_MAJ
#define LBSM_HEAP_VERSION_MIN
#define LBSM_KERNELID_SVPKDIV
pid_t LBSM_UnLBSMD(int undaemon)
int LBSM_LBSMD(int check_n_lock)
void LBSM_Shmem_Detach(HEAP heap)
HEAP LBSM_Shmem_Attach(int fallback)
static HEAP s_GetHeapCopy(TNCBI_Time now)
static int s_VerifyChecksum(const HEAP heap, unsigned int count, unsigned int cksum)
static const SSERV_VTable kLbsmdOp
static SLB_Candidate * s_GetCandidate(void *user_data, size_t n)
static int s_SortStandbys(const void *p1, const void *p2)
static const SLBSM_Sysinfo * s_GetSysinfo(const HOST_INFO hinfo, int warn)
static const char * s_HostEnv(const SLBSM_Host *h)
static SSERV_Info * s_FakeDnsReturn(SERV_ITER iter, HOST_INFO *host_info, int sign, TNCBI_Time time)
static void s_Close(SERV_ITER)
static int s_IfDaemon(void)
static unsigned int s_GetLocalHostAddress(HEAP heap)
static int s_Feedback(SERV_ITER, double, TNCBI_Time)
int LBSM_HINFO_Status(const HOST_INFO hinfo, double status[2])
double LBSM_HINFO_CpuClock(const HOST_INFO hinfo)
int LBSM_HINFO_PortUsage(const HOST_INFO hinfo, SHINFO_PortUsage ports[], size_t count)
const char * LBSMD_GetConfig(void)
int LBSM_HINFO_Memusage(const HOST_INFO hinfo, double memusage[5])
static EHOST_Match s_Match(const char *env, const char *arg, size_t arglen, const char *val, size_t vallen, const char **a, char **v)
HEAP LBSMD_GetHeapCopy(TNCBI_Time now)
double LBSM_CalculateStatus(double rate, double fine, ESERV_Algo algo, const SLBSM_HostLoad *load)
unsigned int LBSMD_GetLocalHostAddress(const void *p)
int LBSM_HINFO_TaskCount(const HOST_INFO hinfo)
int LBSM_HINFO_LoadAverage(const HOST_INFO hinfo, double lavg[2])
volatile HEAP g_LBSM_Heap
const char * LBSMD_GetHostParameter(unsigned int addr, const char *name)
static unsigned int s_Localhost(const SLBSM_Version *v)
ESwitch LBSMD_FastHeapAccess(ESwitch OnOff)
static const SLBSM_Host * s_LookupHost(HEAP heap, const SERV_ITER iter, const SLBSM_Service *svc)
static SSERV_Info * s_GetNextInfo(SERV_ITER, HOST_INFO *)
int LBSM_HINFO_CpuCount(const HOST_INFO hinfo)
static ESwitch s_LBSM_FastHeapAccess
int LBSM_HINFO_CpuUnits(const HOST_INFO hinfo)
#define LBSMD_LOCAL_BONUS
const SSERV_VTable * SERV_LBSMD_Open(SERV_ITER iter, SSERV_Info **info, HOST_INFO *host_info, int no_dispd_follows)
int LBSM_HINFO_MachineParams(const HOST_INFO hinfo, SHINFO_Params *p)
unsigned int g_NCBI_ConnectRandomSeed
#define CORE_LOGF_X(subcode, level, fmt_args)
#define CORE_LOGF_ERRNO_X(subcode, level, error, fmt_args)
#define NCBI_CONNECT_SRAND_ADDEND
#define CORE_LOGF(level, fmt_args)
SSERV_Info * SERV_CopyInfoEx(const SSERV_Info *orig, const char *name)
const char * SERV_NameOfInfo(const SSERV_Info *info)
const char * SERV_CurrentName(SERV_ITER iter)
#define NETDAEMONS_VERSION_INT
#define NETDAEMONS_MAJOR_OF(ver)
#define NETDAEMONS_MINOR_OF(ver)
#define NETDAEMONS_PATCH_OF(ver)
#define NETDAEMONS_VERSION_STR
static int match(register const pcre_uchar *eptr, register const pcre_uchar *ecode, const pcre_uchar *mstart, int offset_top, match_data *md, eptrblock *eptrb, unsigned int rdepth)
const SLBSM_Service * svc
struct SLBSM_Candidate * cand
static const struct type types[]