Skip to contentSkip to conversation

configure: Implement --enable-profiling option

PATCH Superseded Phil Sutter 2 participants 3 messages Feb 5 – Feb 11

Subsystems (2)

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Phil Sutter

author reviewer
#0 lore raw

PATCH v2 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 v1:
- Add src/profiling.c and include/profiling.h to keep conditionally built code separate

---
 .gitignore          |  5 +++++
 Makefile.am         | 21 +++++++++++++++++++++
 configure.ac        |  7 +++++++
 include/profiling.h | 10 ++++++++++
 src/main.c          |  3 +++
 src/profiling.c     | 26 ++++++++++++++++++++++++++
 6 files changed, 72 insertions(+)
 create mode 100644 include/profiling.h
 create mode 100644 src/profiling.c
.gitignore +5 -0
index 719829b65d212..8673393fac397 100644
@@ -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
index bff746b53a0b4..5dfd2606e0fc7 100644
@@ -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
@@ -297,6 +302,10 @@ if BUILD_CLI
 src_nft_SOURCES += src/cli.c
 endif
 
+if BUILD_PROFILING
+src_nft_SOURCES += src/profiling.c
+endif
+
 src_nft_LDADD = src/libnftables.la
 
 ###############################################################################
@@ -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 +7 -0
index 022608627908a..5756f873f61a7 100644
@@ -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				\
include/profiling.h new file +10 -0
new file mode 100644
index 0000000000000..77b50ec6b5338
@@ -0,0 +1,10 @@
+#ifndef NFTABLES_PROFILING_H
+#define NFTABLES_PROFILING_H
+
+#ifdef BUILD_PROFILING
+static void setup_sighandler(void);
+#else
+static inline void setup_sighandler(void) { /* empty */ }
+#endif
+
+#endif /* NFTABLES_PROFILING_H */
src/main.c +3 -0
index 29b0533dee7c9..163d9312b20f4 100644
@@ -19,6 +19,7 @@
 #include <sys/types.h>
 
 #include <nftables/libnftables.h>
+#include <profiling.h>
 #include <utils.h>
 #include <cli.h>
 
@@ -375,6 +376,8 @@ int main(int argc, char * const *argv)
 	if (getuid() != geteuid())
 		_exit(111);
 
+	setup_sighandler();
+
 	if (!nft_options_check(argc, argv))
 		exit(EXIT_FAILURE);
 
src/profiling.c new file +26 -0
new file mode 100644
index 0000000000000..ab292e768da9e
@@ -0,0 +1,26 @@
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+static void termhandler(int signo)
+{
+	switch (signo) {
+	case SIGTERM:
+		exit(143);
+	case SIGINT:
+		exit(130);
+	}
+}
+
+void setup_sighandler(void)
+{
+	struct sigaction act = {
+		.sa_handler = termhandler,
+	};
+
+	if (sigaction(SIGTERM, &act, NULL) == -1 ||
+	    sigaction(SIGINT, &act, NULL) == -1) {
+		perror("sigaction");
+		exit(1);
+	}
+}
Signature · 1 line
2.51.0
maintainer
#1 lore raw
Quoted 14 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.

index 022608627908a..5756f873f61a7 100644

configure.ac +0 -0
@@ -156,6 +156,13 @@ AC_ARG_ENABLE([distcheck],
 	      [enable_distcheck=yes], [])
 AM_CONDITIONAL([BUILD_DISTCHECK], [test "x$enable_distcheck" = "xyes"])
 

configure.ac AC_ARG_ENABLE([distcheck], · line 168

Quoted 10 lines
configure.ac +7 -0
+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				\

I think this should show up in:

nft configuration:

  cli support:                  readline
  enable debugging symbols:     yes
  use mini-gmp:                 no
  enable man page:              yes
  libxtables support:           no
  json output support:          yes
  systemd unit:                 no
author reviewer
#2 lore raw
Quoted 28 lines · Florian Westphal
Quoted 14 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.

index 022608627908a..5756f873f61a7 100644

configure.ac +0 -0
@@ -156,6 +156,13 @@ AC_ARG_ENABLE([distcheck],
 	      [enable_distcheck=yes], [])
 AM_CONDITIONAL([BUILD_DISTCHECK], [test "x$enable_distcheck" = "xyes"])
 

configure.ac AC_ARG_ENABLE([distcheck], · line 168

Quoted 10 lines
configure.ac +7 -0
+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				\

I think this should show up in:

Quoted 8 lines

nft configuration:

  cli support:                  readline
  enable debugging symbols:     yes
  use mini-gmp:                 no
  enable man page:              yes
  libxtables support:           no
  json output support:          yes
  systemd unit:                 no

ACK, I'll prepare a v3.

Thanks, Phil