Skip to content

Instantly share code, notes, and snippets.

@fjahr
Last active April 18, 2024 13:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fjahr/c0c339a4b5021ae815d5bcff42e9fc57 to your computer and use it in GitHub Desktop.
Save fjahr/c0c339a4b5021ae815d5bcff42e9fc57 to your computer and use it in GitHub Desktop.
libevent usage

Where is libevent used?

$ git grep "#include <event2"

// Just config
// configure.ac:      #include <event2/http.h>

src/bitcoin-cli.cpp:#include <event2/buffer.h>
src/bitcoin-cli.cpp:#include <event2/keyvalq_struct.h>

// Done with https://github.com/bitcoin/bitcoin/pull/29904
src/common/url.cpp:#include <event2/http.h>

src/httpserver.cpp:#include <event2/buffer.h>
src/httpserver.cpp:#include <event2/bufferevent.h>
src/httpserver.cpp:#include <event2/http.h>
src/httpserver.cpp:#include <event2/http_struct.h>
src/httpserver.cpp:#include <event2/keyvalq_struct.h>
src/httpserver.cpp:#include <event2/thread.h>
src/httpserver.cpp:#include <event2/util.h>

// A hand full of raii inline wrappers
src/support/events.h:#include <event2/event.h>
src/support/events.h:#include <event2/http.h>

// Just tests
// src/test/fuzz/http_request.cpp:#include <event2/buffer.h>
// src/test/fuzz/http_request.cpp:#include <event2/event.h>
// src/test/fuzz/http_request.cpp:#include <event2/http.h>
// src/test/fuzz/http_request.cpp:#include <event2/http_struct.h>
// src/test/raii_event_tests.cpp:#include <event2/event.h>

src/torcontrol.cpp:#include <event2/buffer.h>
src/torcontrol.cpp:#include <event2/bufferevent.h>
src/torcontrol.cpp:#include <event2/event.h>
src/torcontrol.cpp:#include <event2/thread.h>
src/torcontrol.cpp:#include <event2/util.h>

// Done with XXX (pending some discussion on the TorControl callbacks)
src/torcontrol.h:#include <event2/util.h>

Basically, we have usage already nicely isolated in 4 files: bitcoin-cli.cpp, httpserver.cpp, support/events.h, and torcontrol.cpp where the support file can be almost ignored.

Which symbols are we using in each file? Is there a large overlap between the files?

Getting libevent symbols

$ for file in /opt/homebrew/Cellar/libevent/2.1.12_1/lib/*.dylib; do
    /usr/bin/nm -g "$file" | awk '{if (length($3) > 1 && substr($3, 1, 1) == "_") print substr($3, 2)}'
done | sort | uniq > libevent_symbols.txt
$ cat libevent_symbols.txt | wc -l
     646

Getting the usage of symbols in each of our 4 files

$ grep -oFf libevent_symbols.txt ../bitcoin/src/httpserver.cpp | cut -c 2- | sort | uniq -c | wc -l
      45
$ grep -oFf libevent_symbols.txt ../bitcoin/src/bitcoin-cli.cpp | cut -c 2- | sort | uniq -c | wc -l
      15
$ grep -oFf libevent_symbols.txt ../bitcoin/src/support/events.h | cut -c 2- | sort | uniq -c | wc -l
       6
$ grep -oFf libevent_symbols.txt ../bitcoin/src/torcontrol.cpp | cut -c 2- | sort | uniq -c | wc -l
      20

How big is the overlap of all of the used symbols between these files?

$ grep -h -oFf libevent_symbols.txt ../bitcoin/src/httpserver.cpp ../bitcoin/src/bitcoin-cli.cpp ../bitcoin/src/support/events.h ../bitcoin/src/torcontrol.cpp | sort | uniq | wc -l
      63

How much of libevent that we are using directly?

event2 in the current stable version includes 26 header files

$ find /opt/homebrew/Cellar/libevent/2.1.12_1/include/event2/ -type f -name "*.h" | wc -l
      26

In the above grep you can see that we only use 8 of them:

event2/buffer.h
event2/bufferevent.h
event2/http.h
event2/http_struct.h
event2/keyvalq_struct.h
event2/thread.h
event2/util.h
event2/event.h
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment