const char* LBSMD_GetConfig(void) { const char* s = 0; HEAP heap; if (LBSM_LBSMD(0) > 0 && errno == EAGAIN) { if ((heap = s_GetHeapCopy(time(0))) != 0) { if ((s = LBSM_GetConfig(heap)) != 0) s = strdup(s); #ifdef LBSM_DEBUG CORE_LOGF(eLOG_Trace, ("LBSM heap[%p, %p, %d] released", heap, HEAP_Base(heap), HEAP_Serial(heap))); #endif /*LBSM_DEBUG*/ CORE_LOCK_WRITE; HEAP_Detach(heap); CORE_UNLOCK; } } if (!s_FastHeapAccess) { /* As a documented side effect, clean up cached copy of LBSM heap */ s_Fini(); } return s; }
static void s_Close(SERV_ITER iter) { if (iter->data) { #ifdef LBSM_DEBUG CORE_LOGF(eLOG_Trace, ("LBSM heap[%p, %p, %d] for \"%s\" deleted", iter->data, HEAP_Base((HEAP) iter->data), HEAP_Serial((HEAP) iter->data), iter->name)); #endif /*LBSM_DEBUG*/ CORE_LOCK_WRITE; HEAP_Detach((HEAP) iter->data); CORE_UNLOCK; iter->data = 0; } if (!s_FastHeapAccess) LBSM_UnLBSMD(-1); }
void LBSM_Shmem_Detach(HEAP heap) { int which; if (!heap) return; which = HEAP_Serial(heap); #ifdef LBSM_DEBUG CORE_LOGF(eLOG_Trace, ("LBSM shmem[%d] detaching", which)); #endif /*LBSM_DEBUG*/ if (which != 1 && which != 2) { CORE_LOGF_X(12, eLOG_Error, ("Bad block number (%d) for LBSM shmem to unlock", which)); } else s_Shmem_RUnlock(which - 1); HEAP_Detach(heap); #ifdef LBSM_DEBUG CORE_LOG(eLOG_Trace, "LBSM DETACHED"); #endif /*LBSM_DEBUG*/ }
const char* LBSMD_GetHostParameter(unsigned int addr, const char* name) { size_t namelen = name && *name ? strlen(name) : 0; const SLBSM_Host* host; HEAP heap; if (!namelen || LBSM_LBSMD(0) <= 0 || errno != EAGAIN || !(heap = s_GetHeapCopy(time(0)))) { return 0; } if (addr == SERV_ANYHOST || addr == SERV_LOCALHOST) addr = s_GetLocalHostAddress(heap); if ((host = LBSM_LookupHost(heap, addr, 0)) != 0 && host->env) { const char* e = (char*) host + host->env; const char* env; for (env = e; *env; env = e) { const char* p; size_t len; if (!(e = strchr(env, '\n'))) { len = strlen(env); e = env + len; } else len = (size_t)(e++ - env); if (!(p = (const char*) memchr(env, '=', len))) continue; len = (size_t)(p - env); if (len != namelen) continue; if (strncasecmp(env, name, namelen) != 0) continue; len = (size_t)(e - ++p); return strndup(p, len); } } CORE_LOCK_WRITE; HEAP_Detach(heap); CORE_UNLOCK; return 0; }
int main(int argc, const char* argv[]) { SHEAP_Block* blk; int r, j, i, n; HEAP heap; char* c; /* CORE_SetLOGFormatFlags(fLOG_None | fLOG_Level | fLOG_OmitNoteLevel); */ CORE_SetLOGFILE(stderr, 0/*false*/); if (argc > 1) g_NCBI_ConnectRandomSeed = atoi(argv[1]); else g_NCBI_ConnectRandomSeed = (int) time(0) ^ NCBI_CONNECT_SRAND_ADDEND; CORE_LOGF(eLOG_Note, ("Using seed %d", g_NCBI_ConnectRandomSeed)); HEAP_Options(eOff/*slow*/, eOn/*newalk*/); srand(g_NCBI_ConnectRandomSeed); for (j = 1; j <= 3; j++) { CORE_LOGF(eLOG_Note, ("Creating heap %d", j)); if (!(heap = HEAP_Create(0, 0, 4096, s_Expand, 0))) CORE_LOG(eLOG_Error, "Cannot create heap"); for (n = 0; n < 1000 && (rand() & 0xFFFF) != 0x1234; n++) { r = rand() & 7; if (r == 1 || r == 3) { int/*bool*/ fast = rand() & 1; i = rand() & 0xFF; if (i) { CORE_LOGF(eLOG_Note, ("Allocating%s data size %d", fast ? " fast" : "", i)); blk = fast ? HEAP_AllocFast(heap, i) : HEAP_Alloc(heap, i); if (blk) { CORE_LOGF(eLOG_Note, ("Done @%u, size %u", HEAP_ADDR(blk, heap), blk->size)); } else CORE_LOG(eLOG_Error, "Allocation failed"); c = (char*) blk + sizeof(*blk); while (i--) *c++ = rand(); s_Walk(heap, 0); } else { assert(!(fast ? HEAP_AllocFast(heap, i) : HEAP_Alloc(heap, i))); } } else if (r == 2 || r == 4) { blk = 0; do { if (!(blk = HEAP_Walk(heap, blk))) break; } while (rand() & 7); if (blk && (short) blk->flag) { unsigned size = blk->size; unsigned data_size = size - sizeof(*blk); CORE_LOGF(eLOG_Note, ("Freeing @%u, size %u, data size %u", HEAP_ADDR(blk, heap), size, data_size)); assert(data_size); HEAP_Free(heap, blk); CORE_LOG(eLOG_Note, "Done"); s_Walk(heap, 0); } } else if (r == 5) { const SHEAP_Block* prev = 0; unsigned ok = 0; blk = 0; for (;;) { if (!(blk = HEAP_Walk(heap, blk))) break; if ((short) blk->flag && (rand() & 7)) { char buf[32]; unsigned size = blk->size; int/*bool*/ fast = rand() & 1; unsigned data_size = size - sizeof(*blk); if (!fast || !prev) *buf = '\0'; else sprintf(buf, ", prev @%u", HEAP_ADDR(prev, heap)); CORE_LOGF(eLOG_Note, ("Freeing%s%s @%u%s in walk," " size %u, data size %u", fast ? " fast" : "", ok ? " more" : "", HEAP_ADDR(blk,heap), buf, size, data_size)); assert(data_size); if (fast) HEAP_FreeFast(heap, blk, prev); else HEAP_Free(heap, blk); CORE_LOG(eLOG_Note, "Done"); s_Walk(heap, 0); ok = 1; if (prev && !((short) prev->flag)) continue; } prev = blk; } if (!ok) s_Walk(heap, "the"); else CORE_LOG(eLOG_Note, "Done with freeing while walking"); } else if (r == 6 || r == 7) { HEAP newheap; if (r == 6) { int/*bool*/ fast = rand() & 1; if (fast) { CORE_LOG(eLOG_Note, "Attaching heap fast"); newheap = HEAP_AttachFast(HEAP_Base(heap), HEAP_Size(heap), 0); } else { CORE_LOG(eLOG_Note, "Attaching heap"); newheap = HEAP_Attach(HEAP_Base(heap), 0); } } else { CORE_LOG(eLOG_Note, "Copying heap"); newheap = HEAP_Copy(heap, 0, 0); } if (!newheap) { CORE_LOGF(eLOG_Error, ("%s failed", r == 6 ? "Attach" : "Copy")); } else s_Walk(newheap, r == 6 ? "attached" : "copied"); HEAP_Detach(newheap); } else { TNCBI_Size size = HEAP_Size(heap); HEAP newheap; CORE_LOG(eLOG_Note, "Trimming heap"); newheap = HEAP_Trim(heap); CORE_LOGF(eLOG_Note, ("Heap %strimmed: %u -> %u", newheap ? "" : "NOT ", size, HEAP_Size(newheap))); if (newheap) { heap = newheap; s_Walk(heap, "trimmed"); } } } HEAP_Destroy(heap); CORE_LOGF(eLOG_Note, ("Heap %d done", j)); } CORE_LOG(eLOG_Note, "Test completed"); CORE_SetLOG(0); return 0; }
static SSERV_Info* s_GetNextInfo(SERV_ITER iter, HOST_INFO* host_info) { const TSERV_Type types = iter->types & ~fSERV_Firewall; size_t i, n, idx[eHost_NoMatch], n_cand, a_cand; EHost_Match best_match, match; struct SLBSM_Candidate* cand; const SLBSM_Service* svc; TNCBI_Time dns_info_time; const SLBSM_Host* host; const char* env, *a; SSERV_Info* info; const char* name; double status; int standby; HEAP heap; char* v; heap = (HEAP)(iter->data != iter ? iter->data : 0); if (heap) { #ifdef LBSM_DEBUG CORE_LOGF(eLOG_Trace, ("LBSM heap[%p, %p, %d] for \"%s\" detected", heap, HEAP_Base(heap), HEAP_Serial(heap), iter->name)); #endif /*LBSM_DEBUG*/ /*noop*/; } else if (!(heap = s_GetHeapCopy(iter->time))) { return iter->external || iter->data == iter || !(types & fSERV_Dns) ? 0 : s_FakeDnsReturn(iter, host_info, 0, NCBI_TIME_INFINITE); } best_match = eHost_InitMatch; memset(idx, 0, sizeof(idx)); standby = -1/*unassigned*/; dns_info_time = 0/*none*/; n = n_cand = a_cand = 0; a = v = 0; cand = 0; svc = 0; name = *iter->name ? iter->name : 0; assert(name || iter->ismask); /*NB: ismask ignored for NULL*/ while ((svc = LBSM_LookupService(heap, name, iter->ismask, svc))) { if (svc->entry.good < iter->time) continue; /* out-of-date entry */ if (!svc->info.time) continue; /* off */ if (types != fSERV_Any && !(types & svc->info.type)) continue; /* type doesn't match */ if (iter->external && svc->info.locl) continue; /* external mapping requested; local/private server */ if (svc->info.locl & 0xF0) { /* private server */ if (svc->info.host && svc->info.host != s_GetLocalHostAddress(heap)) { continue; } } if (svc->info.type == fSERV_Dns) { if (types == fSERV_Any) continue; /* DNS entries have to be requested explicitly */ if (!iter->ismask) { if (dns_info_time < svc->info.time) dns_info_time = svc->info.time; } } else { if (iter->stateless && svc->info.sful) { /* Skip stateful-only non-CGI (NCBID and standalone) svc */ if (!(svc->info.type & fSERV_Http)) continue; } if (!iter->ismask && iter->reverse_dns) { if (dns_info_time < svc->info.time) dns_info_time = svc->info.time; } } if (svc->info.rate > 0.0 || host_info) { if (!(host = s_LookupHost(heap, iter, svc)) && svc->info.rate > 0.0) { continue; /* no host information for non-static server */ } } else host = 0; for (n = 0; n < iter->n_skip; n++) { const SSERV_Info* skip = iter->skip[n]; const char* s = SERV_NameOfInfo(skip); if (*s) { assert(iter->ismask || iter->reverse_dns); if (strcasecmp(s, (const char*) svc + svc->name) == 0 && ((skip->type == fSERV_Dns && !skip->host) || SERV_EqualInfo(skip, &svc->info))) { break; } } else if (SERV_EqualInfo(skip, &svc->info)) break; if (skip->type == fSERV_Firewall && skip->u.firewall.type == svc->info.type) { break; } if (iter->reverse_dns && skip->type == fSERV_Dns && skip->host == svc->info.host && (!skip->port || skip->port == svc->info.port)) { break; } } /*FIXME*//*CORE_LOG(eLOG_Note, (char*) svc + svc->name);*/ if (n >= iter->n_skip) { status = LBSM_CalculateStatus(svc->info.rate, svc->fine, svc->info.flag, &host->sys.load); if (status <= 0.0) { if (!svc->info.rate) { if (!iter->ok_down) continue; /* not operational */ status = 0.0; } else status = copysign(svc->info.rate, -1.0); } } else status = 0.0; /* dummy assignment to keep no-init warning off */ if (v) { free(v); v = 0; } a = env = 0; if (iter->pref < 0.0 && iter->host && (iter->host != svc->info.host || (iter->port && iter->port != svc->info.port))) { /* not a suitable fixed latching */ match = eHost_BadMatch; } else if (iter->arglen) { assert(iter->arg); if (!host) host = s_LookupHost(heap, iter, svc); if ( host && host->env) env = (const char*) host + host->env; match = s_Match(env, iter->arg, iter->arglen, iter->val, iter->vallen, &a, &v); assert(!a || a == iter->arg); } else match = eHost_GoodMatch; if (best_match > match) best_match = match; if (match > eHost_NoMatch) { assert(!v); continue; } if (svc->info.rate) { /* NB: server is _not_ down, but it may have been suppressed */ if (fabs(svc->info.rate) < 0.01) { if (!standby) { if (!iter->ok_suppressed) continue; /* this has to be given out as a suppressed one */ status = copysign(svc->info.rate, -1.0); } else if (standby < 0) standby = 1; } else if (standby) { standby = 0/*cancel*/; if (!iter->ok_suppressed) { memset(idx, 0, sizeof(idx)); for (i = 0; i < n_cand; i++) { if (cand[i].val) free((void*) cand[i].val); } n_cand = 0; } else for (i = 0; i < n_cand; i++) cand[i].cand.status = copysign(cand[i].svc->info.rate,-1.); } } if (n < iter->n_skip) continue; /* excluded/seen; NB: dummy assignment goes off here */ if (!iter->ok_suppressed && status < 0.0) continue; #ifdef NCBI_LB_DEBUG if (iter->arglen) { char* s = SERV_WriteInfo(&svc->info); const char* m; assert(s); switch (match) { case eHost_BestMatch: m = "Best match"; break; case eHost_GoodMatch: m = "Good match"; break; case eHost_FairMatch: m = "Fair match"; break; case eHost_PoorMatch: m = "Poor match"; break; case eHost_NoMatch: m = "No match"; break; default: assert(0); m = "?"; break; } assert(!a || *a); assert(!v || a); CORE_LOGF(eLOG_Note, ("%s%s%s%s: %s%s%s%s%s%s", s, env ? " <" : "", env ? env : "", env ? ">" : "", m, a ? ", arg=" : "", a ? a : "", v ? ", val=" : "", v ? (*v ? v : "\"\"") : "", standby > 0 ? ", standby" : "")); free(s); } #endif /*NCBI_LB_DEBUG*/ /* This server should be taken into consideration */ if (n_cand == a_cand) { struct SLBSM_Candidate* temp; n = a_cand + 10; temp = (struct SLBSM_Candidate*) (cand ? realloc(cand, n * sizeof(*temp)) : malloc ( n * sizeof(*temp))); if (!temp) break; cand = temp; a_cand = n; } if (match < eHost_NoMatch) { assert((size_t) match < sizeof(idx)/sizeof(idx[0])); n = idx[match]; if (n < n_cand) memmove(&cand[n + 1], &cand[n], sizeof(cand[0])*(n_cand - n)); for (i = match; i < sizeof(idx)/sizeof(idx[0]); i++) idx[i]++; } else n = n_cand; cand[n].cand.info = &svc->info; cand[n].cand.status = status; cand[n].host = host; cand[n].svc = svc; cand[n].arg = a; cand[n].val = v; a = v = 0; n_cand++; } if (v) free(v); if (best_match < eHost_NoMatch) { assert(!best_match || !idx[best_match - 1]); for (n = idx[best_match]; n < n_cand; n++) { if (cand[n].val) free((void*) cand[n].val); } n_cand = idx[best_match]; } if (n_cand) { assert(cand); do { if (standby <= 0) { struct SLBSM_Data data; data.cand = cand; data.n_cand = n_cand; n = LB_Select(iter, &data, s_GetCandidate, LBSMD_LOCAL_BONUS); } else { qsort(cand, n_cand, sizeof(*cand), s_SortStandbys); status = cand[0].cand.status; for (n = 1; n < n_cand; n++) { if (status != cand[n].cand.status) break; } n = rand() % n; } svc = cand[n].svc; if (iter->reverse_dns && svc->info.type != fSERV_Dns) { svc = 0; dns_info_time = 0/*none*/; while ((svc = LBSM_LookupService(heap, 0/*all*/, 0, svc)) !=0){ if (svc->info.type != fSERV_Dns || !svc->info.time || svc->info.host != cand[n].svc->info.host || svc->info.port != cand[n].svc->info.port) { continue; } if (!iter->ismask) { if (dns_info_time < svc->info.time) dns_info_time = svc->info.time; } if (iter->external && svc->info.locl) continue;/* external mapping requested; local server */ assert(!(svc->info.locl & 0xF0)); /* no private DNS */ status = LBSM_CalculateStatus(!svc->info.rate ? 0.0 : -LBSM_DEFAULT_RATE, svc->fine, fSERV_Regular, NULL); if (status > 0.0) break; if ((!svc->info.rate && iter->ok_down) || ( svc->info.rate && iter->ok_suppressed)) { cand[n].cand.status = !svc->info.rate ? 0.0 : copysign(svc->info.rate, -1.0); break; } } if (!svc && !dns_info_time) svc = cand[n].svc; } if (svc) break; if (cand[n].val) free((void*) cand[n].val); if (n < --n_cand) memmove(cand + n, cand + n + 1, (n_cand - n) * sizeof(*cand)); } while (n_cand); } else svc = 0; if (svc) { const char* name = (iter->ismask || iter->reverse_dns ? (const char*) svc + svc->name : ""); if ((info = SERV_CopyInfoEx(&svc->info, name)) != 0) { info->rate = cand[n].cand.status; if (info->time != NCBI_TIME_INFINITE) info->time = cand[n].svc->entry.good; if (host_info) { if ((host = cand[n].host) != 0) { *host_info = HINFO_Create(host->addr, &host->sys, sizeof(host->sys), host->env ? (const char*) host + host->env : 0, cand[n].arg, cand[n].val); } else *host_info = 0; } } } else { info = !n_cand && dns_info_time ? s_FakeDnsReturn(iter, host_info, best_match == eHost_InitMatch ? 0/*down*/ : best_match != eHost_BadMatch ? -1/*busy*/ : 1, dns_info_time) : 0; } for (n = 0; n < n_cand; n++) { if (cand[n].val) free((void*) cand[n].val); } if (cand) free(cand); if (!s_FastHeapAccess) { #ifdef LBSM_DEBUG CORE_LOGF(eLOG_Trace, ("LBSM heap[%p, %p, %d] for \"%s\" released", heap, HEAP_Base(heap), HEAP_Serial(heap), iter->name)); #endif /*LBSM_DEBUG*/ CORE_LOCK_WRITE; HEAP_Detach(heap); CORE_UNLOCK; heap = 0; } #ifdef LBSM_DEBUG else { CORE_LOGF(eLOG_Trace, ("LBSM heap[%p, %p, %d] for \"%s\" retained", heap, HEAP_Base(heap), HEAP_Serial(heap), iter->name)); } #endif /*LBSM_DEBUG*/ iter->data = heap; return info; }