configure: Implement --enable-profiling option
Phil Sutter
reviewerPATCH v4 1/1 configure: Implement --enable-profiling option
This will set compiler flag --coverage so code coverage may be inspected using gcov.
In order to successfully profile processes which are killed or interrupted as well, add a signal handler for those cases which calls exit(). This is relevant for test cases invoking nft monitor.
---
Changes since v3:
- Avoid illegal exit() call in signal handler by use of signalfd()
Changes since v2:
- Include profiling option value in configure's final status report
- Fix build for --enable-profiling
- Add copyright statement to new source file
Changes since v1:
- Add src/profiling.c and include/profiling.h to keep conditionally built code separate
--- .gitignore | 5 +++++ Makefile.am | 21 +++++++++++++++++++ configure.ac | 10 ++++++++- include/profiling.h | 12 +++++++++++ src/mnl.c | 9 +++++++- src/profiling.c | 51 +++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 106 insertions(+), 2 deletions(-) create mode 100644 include/profiling.h create mode 100644 src/profiling.c
.gitignore +5 -0
@@ -19,6 +19,11 @@ nftversion.h # cscope files /cscope.* +# gcov-related +*.gcda +*.gcno +*.gcov + # Generated by tests *.payload.got tests/build/tests.log
Makefile.am +21 -0
@@ -96,6 +96,7 @@ noinst_HEADERS = \ include/owner.h \ include/parser.h \ include/payload.h \ + include/profiling.h \ include/proto.h \ include/rt.h \ include/rule.h \
@@ -163,6 +164,10 @@ AM_CFLAGS = \ AM_YFLAGS = -d -Wno-yacc +if BUILD_PROFILING +AM_CFLAGS += --coverage +endif + ############################################################################### BUILT_SOURCES += src/parser_bison.h
@@ -264,6 +269,10 @@ src_libnftables_la_SOURCES += \ $(NULL) endif +if BUILD_PROFILING +src_libnftables_la_SOURCES += src/profiling.c +endif + src_libnftables_la_LDFLAGS = \ -version-info "${libnftables_LIBVERSION}" \ -Wl,--version-script="$(srcdir)/src//libnftables.map" \
@@ -453,3 +462,15 @@ TESTS = tests/build/run-tests.sh \ tests/py/nft-test.py \ tests/shell/run-tests.sh endif + +all_c_sources = $(filter %.c,$(src_libnftables_la_SOURCES)) $(src_nft_SOURCES) +if BUILD_MINIGMP +all_c_sources += $(src_libminigmp_la_SOURCES) +endif +if BUILD_AFL +all_c_sources += $(tools_nft_afl_SOURCES) +endif +CLEANFILES += src/libparser_la-parser_bison.gcno +CLEANFILES += src/libparser_la-scanner.gcno +CLEANFILES += $(all_c_sources:.c=.gcno) +CLEANFILES += $(src_nft_SOURCES:.c=.gcda)
configure.ac +9 -1
@@ -156,6 +156,13 @@ AC_ARG_ENABLE([distcheck], [enable_distcheck=yes], []) AM_CONDITIONAL([BUILD_DISTCHECK], [test "x$enable_distcheck" = "xyes"]) +AC_ARG_ENABLE([profiling], + AS_HELP_STRING([--enable-profiling], [build for use of gcov/gprof]), + [enable_profiling="$enableval"], [enable_profiling="no"]) +AM_CONDITIONAL([BUILD_PROFILING], [test "x$enable_profiling" = xyes]) +AM_COND_IF([BUILD_PROFILING], + [AC_DEFINE([BUILD_PROFILING], [1], [Define for profiling])]) + AC_CONFIG_FILES([ \ Makefile \ libnftables.pc \
@@ -170,7 +177,8 @@ echo " use mini-gmp: ${with_mini_gmp} enable man page: ${enable_man_doc} libxtables support: ${with_xtables} - json output support: ${with_json}" + json output support: ${with_json} + collect profiling data: ${enable_profiling}" if test "x$unitdir" != "x"; then AC_SUBST([unitdir])
include/profiling.h new file +12 -0
@@ -0,0 +1,12 @@ +#ifndef NFTABLES_PROFILING_H +#define NFTABLES_PROFILING_H + +#ifdef BUILD_PROFILING +int get_signalfd (void ); +void check_signalfd (int fd); +#else +static inline int get_signalfd (void ) {return -1 ; } +static inline void check_signalfd (int fd) {/* empty */ } +#endif + +#endif /* NFTABLES_PROFILING_H */
src/mnl.c +8 -1
@@ -39,6 +39,7 @@#include <errno.h> #include <utils.h> #include <nftables.h> +#include <profiling.h> #include <linux/netfilter.h> #include <linux/netfilter_arp.h>
@@ -2390,6 +2391,7 @@ int mnl_nft_event_listener(struct mnl_socket *nf_sock, unsigned int debug_mask,unsigned int bufsiz =NFTABLES_NLEVENT_BUFSIZ ;int fd =mnl_socket_get_fd (nf_sock);char buf[NFT_NLMSG_MAXSIZE ]; +int sigfd =get_signalfd ();fd_set readfds;int ret;
@@ -2401,11 +2403,16 @@ int mnl_nft_event_listener(struct mnl_socket *nf_sock, unsigned int debug_mask,while (1 ) {FD_ZERO (&readfds);FD_SET (fd, &readfds); +if (sigfd !=-1 ) +FD_SET (sigfd, &readfds); - ret =select (fd +1 , &readfds,NULL ,NULL ,NULL ); + ret =select (max (fd, sigfd) +1 , &readfds,NULL ,NULL ,NULL );if (ret <0 )return -1 ; +if (FD_ISSET (sigfd, &readfds)) +check_signalfd (sigfd); +if (FD_ISSET (fd, &readfds)) { ret =mnl_socket_recvfrom (nf_sock, buf,sizeof (buf));if (ret <0 ) {
src/profiling.c new file +51 -0
51 lines not on this page · raw message
Signature · 1 line
2.51.0
Florian Westphal
maintainerQuoted 6 lines · Phil Sutter
This will set compiler flag --coverage so code coverage may be inspected using gcov.
In order to successfully profile processes which are killed or interrupted as well, add a signal handler for those cases which calls
exit(). This is relevant for test cases invoking nft monitor.
LGTM, thanks Phil. Feel free to push this out.
Phil Sutter
reviewerQuoted 9 lines · Florian Westphal
Quoted 6 lines · Phil Sutter
This will set compiler flag --coverage so code coverage may be inspected using gcov.
In order to successfully profile processes which are killed or interrupted as well, add a signal handler for those cases which calls
exit(). This is relevant for test cases invoking nft monitor.LGTM, thanks Phil. Feel free to push this out.
Thanks for your thorough review, patch applied.