Skip to content

Commit

Permalink
ccan: update for more efficient ccan/io.
Browse files Browse the repository at this point in the history
A fairly simple change: ccan/io will now call the underlying I/O
routines repeatedly until they indicate they are unfinished, *or* fail
with EAGAIN.  This should make a significant difference to large
nodes, which currently spend far too much time calling poll() to
discover a single fd is still writable (mainly, for streaming gossip).

Signed-off-by: Rusty Russell <[email protected]>
Changelog-Changed: connectd: now should use far less CPU on large nodes.
  • Loading branch information
rustyrussell authored and ShahanaFarooqui committed Jul 2, 2024
1 parent 7ef8645 commit 341b62e
Show file tree
Hide file tree
Showing 8 changed files with 259 additions and 68 deletions.
2 changes: 1 addition & 1 deletion ccan/README
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
CCAN imported from http://ccodearchive.net.

CCAN version: init-2582-g2d188544
CCAN version: init-2586-gd4932820
30 changes: 15 additions & 15 deletions ccan/ccan/bitmap/bitmap.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#define BIT_ALIGN_DOWN(n) ((n) & ~(BITMAP_WORD_BITS - 1))
#define BIT_ALIGN_UP(n) BIT_ALIGN_DOWN((n) + BITMAP_WORD_BITS - 1)

void bitmap_zero_range(bitmap *bitmap, unsigned long n, unsigned long m)
void bitmap_zero_range(bitmap *b, unsigned long n, unsigned long m)
{
unsigned long an = BIT_ALIGN_UP(n);
unsigned long am = BIT_ALIGN_DOWN(m);
Expand All @@ -19,22 +19,22 @@ void bitmap_zero_range(bitmap *bitmap, unsigned long n, unsigned long m)
assert(m >= n);

if (am < an) {
BITMAP_WORD(bitmap, n) &= ~bitmap_bswap(headmask & tailmask);
BITMAP_WORD(b, n) &= ~bitmap_bswap(headmask & tailmask);
return;
}

if (an > n)
BITMAP_WORD(bitmap, n) &= ~bitmap_bswap(headmask);
BITMAP_WORD(b, n) &= ~bitmap_bswap(headmask);

if (am > an)
memset(&BITMAP_WORD(bitmap, an), 0,
memset(&BITMAP_WORD(b, an), 0,
(am - an) / BITMAP_WORD_BITS * sizeof(bitmap_word));

if (m > am)
BITMAP_WORD(bitmap, m) &= ~bitmap_bswap(tailmask);
BITMAP_WORD(b, m) &= ~bitmap_bswap(tailmask);
}

void bitmap_fill_range(bitmap *bitmap, unsigned long n, unsigned long m)
void bitmap_fill_range(bitmap *b, unsigned long n, unsigned long m)
{
unsigned long an = BIT_ALIGN_UP(n);
unsigned long am = BIT_ALIGN_DOWN(m);
Expand All @@ -44,19 +44,19 @@ void bitmap_fill_range(bitmap *bitmap, unsigned long n, unsigned long m)
assert(m >= n);

if (am < an) {
BITMAP_WORD(bitmap, n) |= bitmap_bswap(headmask & tailmask);
BITMAP_WORD(b, n) |= bitmap_bswap(headmask & tailmask);
return;
}

if (an > n)
BITMAP_WORD(bitmap, n) |= bitmap_bswap(headmask);
BITMAP_WORD(b, n) |= bitmap_bswap(headmask);

if (am > an)
memset(&BITMAP_WORD(bitmap, an), 0xff,
memset(&BITMAP_WORD(b, an), 0xff,
(am - an) / BITMAP_WORD_BITS * sizeof(bitmap_word));

if (m > am)
BITMAP_WORD(bitmap, m) |= bitmap_bswap(tailmask);
BITMAP_WORD(b, m) |= bitmap_bswap(tailmask);
}

static int bitmap_clz(bitmap_word w)
Expand All @@ -76,7 +76,7 @@ static int bitmap_clz(bitmap_word w)
#endif
}

unsigned long bitmap_ffs(const bitmap *bitmap,
unsigned long bitmap_ffs(const bitmap *b,
unsigned long n, unsigned long m)
{
unsigned long an = BIT_ALIGN_UP(n);
Expand All @@ -87,15 +87,15 @@ unsigned long bitmap_ffs(const bitmap *bitmap,
assert(m >= n);

if (am < an) {
bitmap_word w = bitmap_bswap(BITMAP_WORD(bitmap, n));
bitmap_word w = bitmap_bswap(BITMAP_WORD(b, n));

w &= (headmask & tailmask);

return w ? am + bitmap_clz(w) : m;
}

if (an > n) {
bitmap_word w = bitmap_bswap(BITMAP_WORD(bitmap, n));
bitmap_word w = bitmap_bswap(BITMAP_WORD(b, n));

w &= headmask;

Expand All @@ -104,7 +104,7 @@ unsigned long bitmap_ffs(const bitmap *bitmap,
}

while (an < am) {
bitmap_word w = bitmap_bswap(BITMAP_WORD(bitmap, an));
bitmap_word w = bitmap_bswap(BITMAP_WORD(b, an));

if (w)
return an + bitmap_clz(w);
Expand All @@ -113,7 +113,7 @@ unsigned long bitmap_ffs(const bitmap *bitmap,
}

if (m > am) {
bitmap_word w = bitmap_bswap(BITMAP_WORD(bitmap, m));
bitmap_word w = bitmap_bswap(BITMAP_WORD(b, m));

w &= tailmask;

Expand Down
63 changes: 31 additions & 32 deletions ccan/ccan/bitmap/bitmap.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,37 +58,37 @@ static inline bitmap_word bitmap_bswap(bitmap_word w)
#define BITMAP_TAIL(_bm, _nbits) \
(BITMAP_TAILWORD(_bm, _nbits) & BITMAP_TAILBITS(_nbits))

static inline void bitmap_set_bit(bitmap *bitmap, unsigned long n)
static inline void bitmap_set_bit(bitmap *b, unsigned long n)
{
BITMAP_WORD(bitmap, n) |= BITMAP_WORDBIT(n);
BITMAP_WORD(b, n) |= BITMAP_WORDBIT(n);
}

static inline void bitmap_clear_bit(bitmap *bitmap, unsigned long n)
static inline void bitmap_clear_bit(bitmap *b, unsigned long n)
{
BITMAP_WORD(bitmap, n) &= ~BITMAP_WORDBIT(n);
BITMAP_WORD(b, n) &= ~BITMAP_WORDBIT(n);
}

static inline void bitmap_change_bit(bitmap *bitmap, unsigned long n)
static inline void bitmap_change_bit(bitmap *b, unsigned long n)
{
BITMAP_WORD(bitmap, n) ^= BITMAP_WORDBIT(n);
BITMAP_WORD(b, n) ^= BITMAP_WORDBIT(n);
}

static inline bool bitmap_test_bit(const bitmap *bitmap, unsigned long n)
static inline bool bitmap_test_bit(const bitmap *b, unsigned long n)
{
return !!(BITMAP_WORD(bitmap, n) & BITMAP_WORDBIT(n));
return !!(BITMAP_WORD(b, n) & BITMAP_WORDBIT(n));
}

void bitmap_zero_range(bitmap *bitmap, unsigned long n, unsigned long m);
void bitmap_fill_range(bitmap *bitmap, unsigned long n, unsigned long m);
void bitmap_zero_range(bitmap *b, unsigned long n, unsigned long m);
void bitmap_fill_range(bitmap *b, unsigned long n, unsigned long m);

static inline void bitmap_zero(bitmap *bitmap, unsigned long nbits)
static inline void bitmap_zero(bitmap *b, unsigned long nbits)
{
memset(bitmap, 0, bitmap_sizeof(nbits));
memset(b, 0, bitmap_sizeof(nbits));
}

static inline void bitmap_fill(bitmap *bitmap, unsigned long nbits)
static inline void bitmap_fill(bitmap *b, unsigned long nbits)
{
memset(bitmap, 0xff, bitmap_sizeof(nbits));
memset(b, 0xff, bitmap_sizeof(nbits));
}

static inline void bitmap_copy(bitmap *dst, const bitmap *src,
Expand Down Expand Up @@ -161,37 +161,36 @@ static inline bool bitmap_subset(const bitmap *src1, const bitmap *src2,
return true;
}

static inline bool bitmap_full(const bitmap *bitmap, unsigned long nbits)
static inline bool bitmap_full(const bitmap *b, unsigned long nbits)
{
unsigned long i;

for (i = 0; i < BITMAP_HEADWORDS(nbits); i++) {
if (bitmap[i].w != -1UL)
if (b[i].w != -1UL)
return false;
}
if (BITMAP_HASTAIL(nbits) &&
(BITMAP_TAIL(bitmap, nbits) != BITMAP_TAILBITS(nbits)))
(BITMAP_TAIL(b, nbits) != BITMAP_TAILBITS(nbits)))
return false;

return true;
}

static inline bool bitmap_empty(const bitmap *bitmap, unsigned long nbits)
static inline bool bitmap_empty(const bitmap *b, unsigned long nbits)
{
unsigned long i;

for (i = 0; i < BITMAP_HEADWORDS(nbits); i++) {
if (bitmap[i].w != 0)
if (b[i].w != 0)
return false;
}
if (BITMAP_HASTAIL(nbits) && (BITMAP_TAIL(bitmap, nbits) != 0))
if (BITMAP_HASTAIL(nbits) && (BITMAP_TAIL(b, nbits) != 0))
return false;

return true;
}

unsigned long bitmap_ffs(const bitmap *bitmap,
unsigned long n, unsigned long m);
unsigned long bitmap_ffs(const bitmap *b, unsigned long n, unsigned long m);

/*
* Allocation functions
Expand Down Expand Up @@ -221,26 +220,26 @@ static inline bitmap *bitmap_alloc1(unsigned long nbits)
return bitmap;
}

static inline bitmap *bitmap_realloc0(bitmap *bitmap,
static inline bitmap *bitmap_realloc0(bitmap *b,
unsigned long obits, unsigned long nbits)
{
bitmap = realloc(bitmap, bitmap_sizeof(nbits));
b = realloc(b, bitmap_sizeof(nbits));

if ((nbits > obits) && bitmap)
bitmap_zero_range(bitmap, obits, nbits);
if ((nbits > obits) && b)
bitmap_zero_range(b, obits, nbits);

return bitmap;
return b;
}

static inline bitmap *bitmap_realloc1(bitmap *bitmap,
static inline bitmap *bitmap_realloc1(bitmap *b,
unsigned long obits, unsigned long nbits)
{
bitmap = realloc(bitmap, bitmap_sizeof(nbits));
b = realloc(b, bitmap_sizeof(nbits));

if ((nbits > obits) && bitmap)
bitmap_fill_range(bitmap, obits, nbits);
if ((nbits > obits) && b)
bitmap_fill_range(b, obits, nbits);

return bitmap;
return b;
}

#endif /* CCAN_BITMAP_H_ */
11 changes: 9 additions & 2 deletions ccan/ccan/io/benchmarks/Makefile
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
ALL:=run-loop run-different-speed run-length-prefix
ALL:=run-loop run-different-speed run-length-prefix run-stream-many
CCANDIR:=../../..
CFLAGS:=-Wall -I$(CCANDIR) -O3 -flto
LDFLAGS:=-O3 -flto
LDLIBS:=-lrt

OBJS:=time.o poll.o io.o err.o timer.o list.o
OBJS:=time.o poll.o io.o err.o timer.o list.o ccan-tal.o ccan-take.o ccan-ilog.o

default: $(ALL)

run-loop: run-loop.o $(OBJS)
run-different-speed: run-different-speed.o $(OBJS)
run-length-prefix: run-length-prefix.o $(OBJS)
run-stream-many: run-stream-many.o $(OBJS)

time.o: $(CCANDIR)/ccan/time/time.c
$(CC) $(CFLAGS) -c -o $@ $<
Expand All @@ -24,6 +25,12 @@ io.o: $(CCANDIR)/ccan/io/io.c
$(CC) $(CFLAGS) -c -o $@ $<
err.o: $(CCANDIR)/ccan/err/err.c
$(CC) $(CFLAGS) -c -o $@ $<
ccan-ilog.o: $(CCANDIR)/ccan/ilog/ilog.c
$(CC) $(CFLAGS) -c -o $@ $<
ccan-tal.o: $(CCANDIR)/ccan/tal/tal.c
$(CC) $(CFLAGS) -c -o $@ $<
ccan-take.o: $(CCANDIR)/ccan/take/take.c
$(CC) $(CFLAGS) -c -o $@ $<

clean:
rm -f *.o $(ALL)
Loading

0 comments on commit 341b62e

Please sign in to comment.