diff options
| author | Jonathan Bradley <jcb@pikum.xyz> | 2024-12-17 16:10:30 -0500 |
|---|---|---|
| committer | Jonathan Bradley <jcb@pikum.xyz> | 2024-12-17 16:15:05 -0500 |
| commit | 1dc3bc5db3b3ba60399bee4753abe2f78374a2dd (patch) | |
| tree | eaad96f0a30010c00294a687658f6f6e421f0e03 | |
| parent | 33484cccdea2790721fb20f75588d1ed4fb53017 (diff) | |
pktmr: first-pass, all macros, no IMPL
| -rw-r--r-- | Makefile | 17 | ||||
| -rw-r--r-- | pk.h.in | 5 | ||||
| -rw-r--r-- | pktmr.h | 27 | ||||
| -rw-r--r-- | test/pktmr.c | 24 | ||||
| -rw-r--r-- | test/pktmr.cpp | 24 |
5 files changed, 97 insertions, 0 deletions
@@ -27,6 +27,8 @@ SRC = \ test/pkarr.cpp \ test/pkstn.c\ test/pkstn.cpp \ + test/pktmr.c\ + test/pktmr.cpp \ OBJ = $(SRC:%.c=.o) PPOBJ = $(SRC:%.cpp=.so) @@ -41,6 +43,7 @@ all: options .WAIT clean .WAIT \ pkev \ pkarr \ pkstn \ + pktmr \ test-pkmem-types test-pkmem-types-cpp \ test-pkmem test-pkmem-cpp \ test-pkmacros test-pkmacros-cpp \ @@ -48,6 +51,7 @@ all: options .WAIT clean .WAIT \ test-pkev test-pkev-cpp \ test-pkarr test-pkarr-cpp \ test-pkstn test-pkstn-cpp \ + test-pktmr test-pktmr-cpp \ options: @echo at-suite build options: @@ -85,6 +89,8 @@ pkarr: pkmem pkarr.gch pkarr.gchpp pkstn: pkstn.gch pkstn.gchpp +pktmr: pktmr.gch pktmr.gchpp + build: pkmacros build: pkmem-types build: pkmem @@ -92,6 +98,7 @@ build: pkstr build: pkev build: pkarr build: pkstn +build: pktmr echo "#ifndef PK_SINGLE_HEADER_FILE_H\n#define PK_SINGLE_HEADER_FILE_H" > pk.h cat pk.h.in \ pkmacros.h \ @@ -101,6 +108,7 @@ build: pkstn pkev.h \ pkarr.h \ pkstn.h \ + pktmr.h \ >> pk.h echo "#endif /* PK_SINGLE_HEADER_FILE_H */" >> pk.h sed -i -r \ @@ -153,6 +161,12 @@ test-pkstn: test/pkstn.o test-pkstn-cpp: test/pkstn.so $(CXX) -g -O0 -std=c++23 $(CPPFLAGS) -o test/$@ $^ $(LDFLAGS) +test-pktmr: test/pktmr.o + $(CC) -g -O0 -std=c2x $(CFLAGS) -o test/$@ $^ $(LDFLAGS) + +test-pktmr-cpp: test/pktmr.so + $(CXX) -g -O0 -std=c++23 $(CPPFLAGS) -o test/$@ $^ $(LDFLAGS) + test: pkmacros pkmem-types pkmem pkstr pkev pkarr test: test-pkmacros test-pkmacros-cpp test: test-pkmem-types test-pkmem-types-cpp @@ -161,6 +175,7 @@ test: test-pkstr test-pkstr-cpp test: test-pkev test-pkev-cpp test: test-pkarr test-pkarr-cpp test: test-pkstn test-pkstn-cpp +test: test-pktmr test-pktmr-cpp test: @echo "" ./test/test-pkmacros ; echo Result: $$? "\n" @@ -177,6 +192,8 @@ test: ./test/test-pkarr-cpp ; echo Result: $$? "\n" ./test/test-pkstn ; echo Result: $$? "\n" ./test/test-pkstn-cpp ; echo Result: $$? "\n" + ./test/test-pktmr ; echo Result: $$? "\n" + ./test/test-pktmr-cpp ; echo Result: $$? "\n" clean: rm -f *.plist *.gch *.gchpp *.o *.so test/*.o test/*.so test/test-* @@ -170,6 +170,11 @@ * Provides a thorough interface for interacting with the `stoi` family of * procedures. * +******************************************************************************** +* pktmr.h: No IMPL define, all methods are macros. +* +* Offers a set of `pk_tmr*` macros for elapsed time checking. +* *******************************************************************************/ #define PK_VERSION "@@PK_VERSION@@" @@ -0,0 +1,27 @@ +#ifndef PK_PKTMR_H +#define PK_PKTMR_H + +#include <time.h> + +/* 2024-12-17 JCB + * I have read that in more recent Linux kernels, _MONOTONIC and _REALTIME + * do not require syscalls, while all of the other calls can. + * In testing on my personal machine, this seems to hold true. Using + * CLOCK_PROCESS_CPUTIME_ID consistently elapsed thousands of nanoseconds, + * even with no work between sequential _start() and _stop() calls. + * Meanwhile, the same test with _MONOTONIC elapsed only tens of nanoseconds. + * Consider replacing explicit usage with a define for more user control. + */ + +/* struct pk_tmr */ +struct pk_tmr { + struct timespec b; // begin + struct timespec e; // end +}; + +#define pk_tmr_start(tmr) { clock_gettime(CLOCK_MONOTONIC, &tmr.b); } +#define pk_tmr_stop(tmr) { clock_gettime(CLOCK_MONOTONIC, &tmr.e); } +#define pk_tmr_duration_double(tmr) ((1000.0 * tmr.e.tv_sec + 1e-6 * tmr.e.tv_nsec) - (1000.0 * tmr.b.tv_sec + 1e-6 * tmr.b.tv_nsec)) +#define pk_tmr_duration_nano(tmr) ((((uint64_t)tmr.e.tv_sec * (uint64_t)1000000000) + tmr.e.tv_nsec) - (((uint64_t)tmr.b.tv_sec * (uint64_t)1000000000) + (uint64_t)tmr.b.tv_nsec)) + +#endif /* PK_PKTMR_H */ diff --git a/test/pktmr.c b/test/pktmr.c new file mode 100644 index 0000000..5237415 --- /dev/null +++ b/test/pktmr.c @@ -0,0 +1,24 @@ + +#include "../pktmr.h" +#include "../pkmacros.h" + +#include <stdint.h> +#include <stdio.h> + +int main(int argc, char *argv[]) +{ + struct pk_tmr tmr = {0}; + uint64_t asdf; + (void)asdf; + (void)stdout; + + // timer + { + pk_tmr_start(tmr); + pk_tmr_stop(tmr); + PK_LOGV_INF("%s: elapsed time (nano): %li\n", __FILE__, pk_tmr_duration_nano(tmr)); + PK_LOGV_INF("%s: elapsed time (double): %.9f\n", __FILE__, pk_tmr_duration_double(tmr)); + } + + return 0; +} diff --git a/test/pktmr.cpp b/test/pktmr.cpp new file mode 100644 index 0000000..1a5894f --- /dev/null +++ b/test/pktmr.cpp @@ -0,0 +1,24 @@ + +#include "../pktmr.h" +#include "../pkmacros.h" + +#include <stdint.h> +#include <stdio.h> + +int main(int argc, char *argv[]) +{ + struct pk_tmr tmr = {}; + uint64_t asdf; + (void)asdf; + (void)stdout; + + // timer + { + pk_tmr_start(tmr); + pk_tmr_stop(tmr); + PK_LOGV_INF("%s: elapsed time (nano): %li\n", __FILE__, pk_tmr_duration_nano(tmr)); + PK_LOGV_INF("%s: elapsed time (double): %.9f\n", __FILE__, pk_tmr_duration_double(tmr)); + } + + return 0; +} |
