Skip to content

Commit d5764c9

Browse files
committed
Fix per-flag-cache size tracking
1 parent 0ec6e14 commit d5764c9

File tree

3 files changed

+42
-33
lines changed

3 files changed

+42
-33
lines changed

Makefile

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ dnsseed: dns.o bitcoin.o netbase.o protocol.o db.o main.o util.o
55
g++ -pthread $(LDFLAGS) -o dnsseed dns.o bitcoin.o netbase.o protocol.o db.o main.o util.o -lcrypto
66

77
%.o: %.cpp bitcoin.h netbase.h protocol.h db.h serialize.h uint256.h util.h
8-
g++ -pthread $(CXXFLAGS) -Wno-invalid-offsetof -c -o $@ $<
8+
g++ -std=c++11 -pthread $(CXXFLAGS) -Wall -Wno-unused -Wno-sign-compare -Wno-reorder -Wno-comment -c -o $@ $<
99

1010
dns.o: dns.c
11-
gcc -pthread -std=c99 $(CXXFLAGS) dns.c -c -o dns.o
11+
gcc -pthread -std=c99 $(CXXFLAGS) dns.c -Wall -c -o dns.o
1212

1313
%.o: %.cpp

main.cpp

+39-30
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <stdio.h>
88
#include <stdlib.h>
99
#include <getopt.h>
10+
#include <atomic>
1011

1112
#include "bitcoin.h"
1213
#include "db.h"
@@ -188,19 +189,25 @@ extern "C" void* ThreadCrawler(void* data) {
188189
db.ResultMany(ips);
189190
db.Add(addr);
190191
} while(1);
192+
return nullptr;
191193
}
192194

193195
extern "C" int GetIPList(void *thread, char *requestedHostname, addr_t *addr, int max, int ipv4, int ipv6);
194196

195197
class CDnsThread {
196198
public:
199+
struct FlagSpecificData {
200+
int nIPv4, nIPv6;
201+
std::vector<addr_t> cache;
202+
time_t cacheTime;
203+
unsigned int cacheHits;
204+
FlagSpecificData() : nIPv4(0), nIPv6(0), cacheTime(0), cacheHits(0) {}
205+
};
206+
197207
dns_opt_t dns_opt; // must be first
198208
const int id;
199-
std::map<uint64_t, vector<addr_t> > cache;
200-
int nIPv4, nIPv6;
201-
std::map<uint64_t, time_t> cacheTime;
202-
unsigned int cacheHits;
203-
uint64_t dbQueries;
209+
std::map<uint64_t, FlagSpecificData> perflag;
210+
std::atomic<uint64_t> dbQueries;
204211
std::set<uint64_t> filterWhitelist;
205212

206213
void cacheHit(uint64_t requestedFlags, bool force = false) {
@@ -210,34 +217,35 @@ class CDnsThread {
210217
nets[NET_IPV6] = true;
211218
}
212219
time_t now = time(NULL);
213-
cacheHits++;
214-
if (force || cacheHits > (cache[requestedFlags].size()*cache[requestedFlags].size()/400) || (cacheHits*cacheHits > cache[requestedFlags].size() / 20 && (now - cacheTime[requestedFlags] > 5))) {
220+
FlagSpecificData thisflag = perflag[requestedFlags];
221+
thisflag.cacheHits++;
222+
if (force || thisflag.cacheHits * 400 > (thisflag.cache.size()*thisflag.cache.size()) || (thisflag.cacheHits*thisflag.cacheHits * 20 > thisflag.cache.size() && (now - thisflag.cacheTime > 5))) {
215223
set<CNetAddr> ips;
216224
db.GetIPs(ips, requestedFlags, 1000, nets);
217225
dbQueries++;
218-
cache[requestedFlags].clear();
219-
nIPv4 = 0;
220-
nIPv6 = 0;
221-
cache[requestedFlags].reserve(ips.size());
226+
thisflag.cache.clear();
227+
thisflag.nIPv4 = 0;
228+
thisflag.nIPv6 = 0;
229+
thisflag.cache.reserve(ips.size());
222230
for (set<CNetAddr>::iterator it = ips.begin(); it != ips.end(); it++) {
223231
struct in_addr addr;
224232
struct in6_addr addr6;
225233
if ((*it).GetInAddr(&addr)) {
226234
addr_t a;
227235
a.v = 4;
228236
memcpy(&a.data.v4, &addr, 4);
229-
cache[requestedFlags].push_back(a);
230-
nIPv4++;
237+
thisflag.cache.push_back(a);
238+
thisflag.nIPv4++;
231239
} else if ((*it).GetIn6Addr(&addr6)) {
232240
addr_t a;
233241
a.v = 6;
234242
memcpy(&a.data.v6, &addr6, 16);
235-
cache[requestedFlags].push_back(a);
236-
nIPv6++;
243+
thisflag.cache.push_back(a);
244+
thisflag.nIPv6++;
237245
}
238246
}
239-
cacheHits = 0;
240-
cacheTime[requestedFlags] = now;
247+
thisflag.cacheHits = 0;
248+
thisflag.cacheTime = now;
241249
}
242250
}
243251

@@ -250,12 +258,8 @@ class CDnsThread {
250258
dns_opt.cb = GetIPList;
251259
dns_opt.port = opts->nPort;
252260
dns_opt.nRequests = 0;
253-
cache.clear();
254-
cacheTime.clear();
255-
cacheHits = 0;
256261
dbQueries = 0;
257-
nIPv4 = 0;
258-
nIPv6 = 0;
262+
perflag.clear();
259263
filterWhitelist = opts->filter_whitelist;
260264
}
261265

@@ -280,8 +284,9 @@ extern "C" int GetIPList(void *data, char *requestedHostname, addr_t* addr, int
280284
else if (strcasecmp(requestedHostname, thread->dns_opt.host))
281285
return 0;
282286
thread->cacheHit(requestedFlags);
283-
unsigned int size = thread->cache[requestedFlags].size();
284-
unsigned int maxmax = (ipv4 ? thread->nIPv4 : 0) + (ipv6 ? thread->nIPv6 : 0);
287+
auto& thisflag = thread->perflag[requestedFlags];
288+
unsigned int size = thisflag.cache.size();
289+
unsigned int maxmax = (ipv4 ? thisflag.nIPv4 : 0) + (ipv6 ? thisflag.nIPv6 : 0);
285290
if (max > size)
286291
max = size;
287292
if (max > maxmax)
@@ -290,16 +295,16 @@ extern "C" int GetIPList(void *data, char *requestedHostname, addr_t* addr, int
290295
while (i<max) {
291296
int j = i + (rand() % (size - i));
292297
do {
293-
bool ok = (ipv4 && thread->cache[requestedFlags][j].v == 4) ||
294-
(ipv6 && thread->cache[requestedFlags][j].v == 6);
298+
bool ok = (ipv4 && thisflag.cache[j].v == 4) ||
299+
(ipv6 && thisflag.cache[j].v == 6);
295300
if (ok) break;
296301
j++;
297302
if (j==size)
298303
j=i;
299304
} while(1);
300-
addr[i] = thread->cache[requestedFlags][j];
301-
thread->cache[requestedFlags][j] = thread->cache[requestedFlags][i];
302-
thread->cache[requestedFlags][i] = addr[i];
305+
addr[i] = thisflag.cache[j];
306+
thisflag.cache[j] = thisflag.cache[i];
307+
thisflag.cache[i] = addr[i];
303308
i++;
304309
}
305310
return max;
@@ -310,6 +315,7 @@ vector<CDnsThread*> dnsThread;
310315
extern "C" void* ThreadDNS(void* arg) {
311316
CDnsThread *thread = (CDnsThread*)arg;
312317
thread->run();
318+
return nullptr;
313319
}
314320

315321
int StatCompare(const CAddrReport& a, const CAddrReport& b) {
@@ -346,7 +352,7 @@ extern "C" void* ThreadDumper(void*) {
346352
double stat[5]={0,0,0,0,0};
347353
for (vector<CAddrReport>::const_iterator it = v.begin(); it < v.end(); it++) {
348354
CAddrReport rep = *it;
349-
fprintf(d, "%-47s %4d %11"PRId64" %6.2f%% %6.2f%% %6.2f%% %6.2f%% %6.2f%% %6i %08"PRIx64" %5i \"%s\"\n", rep.ip.ToString().c_str(), (int)rep.fGood, rep.lastSuccess, 100.0*rep.uptime[0], 100.0*rep.uptime[1], 100.0*rep.uptime[2], 100.0*rep.uptime[3], 100.0*rep.uptime[4], rep.blocks, rep.services, rep.clientVersion, rep.clientSubVersion.c_str());
355+
fprintf(d, "%-47s %4d %11" PRId64 " %6.2f%% %6.2f%% %6.2f%% %6.2f%% %6.2f%% %6i %08" PRIx64 " %5i \"%s\"\n", rep.ip.ToString().c_str(), (int)rep.fGood, rep.lastSuccess, 100.0*rep.uptime[0], 100.0*rep.uptime[1], 100.0*rep.uptime[2], 100.0*rep.uptime[3], 100.0*rep.uptime[4], rep.blocks, rep.services, rep.clientVersion, rep.clientSubVersion.c_str());
350356
stat[0] += rep.uptime[0];
351357
stat[1] += rep.uptime[1];
352358
stat[2] += rep.uptime[2];
@@ -359,6 +365,7 @@ extern "C" void* ThreadDumper(void*) {
359365
fclose(ff);
360366
}
361367
} while(1);
368+
return nullptr;
362369
}
363370

364371
extern "C" void* ThreadStats(void*) {
@@ -387,6 +394,7 @@ extern "C" void* ThreadStats(void*) {
387394
printf("%s %i/%i available (%i tried in %is, %i new, %i active), %i banned; %llu DNS requests, %llu db queries", c, stats.nGood, stats.nAvail, stats.nTracked, stats.nAge, stats.nNew, stats.nAvail - stats.nTracked - stats.nNew, stats.nBanned, (unsigned long long)requests, (unsigned long long)queries);
388395
Sleep(1000);
389396
} while(1);
397+
return nullptr;
390398
}
391399

392400
static const string mainnet_seeds[] = {"dnsseed.bluematt.me", "bitseed.xf2.org", "dnsseed.bitcoin.dashjr.org", "seed.bitcoin.sipa.be", ""};
@@ -411,6 +419,7 @@ extern "C" void* ThreadSeeder(void*) {
411419
}
412420
Sleep(1800000);
413421
} while(1);
422+
return nullptr;
414423
}
415424

416425
int main(int argc, char **argv) {

uint256.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,7 @@ class base_uint
322322
// hex string to uint
323323
static char phexdigit[256] = { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,1,2,3,4,5,6,7,8,9,0,0,0,0,0,0, 0,0xa,0xb,0xc,0xd,0xe,0xf,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0xa,0xb,0xc,0xd,0xe,0xf,0,0,0,0,0,0,0,0,0 };
324324
const char* pbegin = psz;
325-
while (phexdigit[*psz] || *psz == '0')
325+
while (phexdigit[(unsigned char)*psz] || *psz == '0')
326326
psz++;
327327
psz--;
328328
unsigned char* p1 = (unsigned char*)pn;

0 commit comments

Comments
 (0)