Skip to content

Commit

Permalink
Inclusion of time time measurements now predicated on ENABLE_STATS=y
Browse files Browse the repository at this point in the history
  • Loading branch information
RaduMantu committed Sep 5, 2023
1 parent 7354ec3 commit ce048b6
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 14 deletions.
31 changes: 19 additions & 12 deletions include/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -112,20 +112,27 @@
/* ARM_TIMER - marks the start of a measured operation
* @start : struct timeval that will hold starting time
*/
#define ARM_TIMER(start) \
do { \
gettimeofday(&start, NULL); \
} while (0)
#ifdef ENABLE_STATS
# define ARM_TIMER(start) \
do { \
gettimeofday(&start, NULL); \
} while (0)
#else
# define ARM_TIMER(...)
#endif /* ENABLE_STATS */

/* UPDATE_TIMER - calculates elapsed time and increments counter
* @counter : variable that holds _total_ elapsed us
* @start : struct timeval used with ARM_TIMER previously
*/
#define UPDATE_TIMER(counter, start) \
do { \
struct timeval end; \
gettimeofday(&end, NULL); \
counter += (end.tv_sec - start.tv_sec) * 1e6 \
+ (end.tv_usec - start.tv_usec); \
} while (0)

#ifdef ENABLE_STATS
# define UPDATE_TIMER(counter, start) \
do { \
struct timeval end; \
gettimeofday(&end, NULL); \
counter += (end.tv_sec - start.tv_sec) * 1e6 \
+ (end.tv_usec - start.tv_usec); \
} while (0)
#else
# define UPDATE_TIMER(...)
#endif /* ENABLE_STATS */
7 changes: 6 additions & 1 deletion makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ CRT = certs

# compilation related parameters
CXX = clang++
CXXFLAGS = -std=c++20 -ggdb
CXXFLAGS = -std=c++20 -ggdb -O2
LDFLAGS = $(shell pkg-config --libs \
libnetfilter_queue \
libbpf \
Expand All @@ -22,6 +22,11 @@ LLC = llc
CLANGFLAGS = -D__KERNEL__ -D__BPF_TRACING__ -emit-llvm -O2 -fno-stack-protector -g
LLCFLAGS = -march=bpf -filetype=obj

# build time options
ifeq ($(ENABLE_STATS),y)
CXXFLAGS += -DENABLE_STATS
endif

# identify sources and create object file targets
# main userspace app objects go into OBJ for remake caching
# eBPF objects go straight into BIN to be used by final app
Expand Down
9 changes: 8 additions & 1 deletion src/firewall/firewall.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ sigint_handler(int)
terminate = true;
}

#ifdef ENABLE_STATS
/* print_stats - shows elapsed time statistics on STDIN
*/
static void
Expand Down Expand Up @@ -225,6 +226,7 @@ print_stats(void)
nfqfwdh_packets_ctr,
1e6 * nfqfwdh_packets_ctr / main_loop_elapsed);
}
#endif /* ENABLE_STATS */

/* handle_event - worker thread helper function
* @fd : event source file descriptor
Expand Down Expand Up @@ -287,9 +289,12 @@ handle_event(int32_t fd)
ARM_TIMER(start_marker);
nfq_handle_packet(nf_handle_fwd, (char *) pkt_buff, rb);
UPDATE_TIMER(nfq_eval_fwd_ctr, start_marker);
} elif (fd == STDIN_FILENO) {
}
#ifdef ENABLE_STATS
elif (fd == STDIN_FILENO) {
print_stats();
}
#endif /* ENABLE_STATS */

return 0;
}
Expand Down Expand Up @@ -647,6 +652,7 @@ main(int argc, char *argv[])
INFO("netfilter forward queue added to epoll-p1 monitor");
}

#ifdef ENABLE_STATS
/* add stdin to epoll watchlist (bottom prio) *
* NOTE: this will fail if ran from within a bash script *
* stdin not used for anything at the moment anyway */
Expand All @@ -658,6 +664,7 @@ main(int argc, char *argv[])
/* GOTO(ans == -1, clean_epoll_fd_p1, */
/* "failed to add stdin to epoll-p1 monitor"); */
/* INFO("stdin added to epoll monitor"); */
#endif /* ENABLE_STATS */

/* add priority ordering epoll instances to top level epoll selector *
* NOTE: this is predicated on actually having multiple priorities *
Expand Down

0 comments on commit ce048b6

Please sign in to comment.