summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Makefile17
-rw-r--r--pk.h.in5
-rw-r--r--pktmr.h27
-rw-r--r--test/pktmr.c24
-rw-r--r--test/pktmr.cpp24
5 files changed, 97 insertions, 0 deletions
diff --git a/Makefile b/Makefile
index 3e6ceef..f97460f 100644
--- a/Makefile
+++ b/Makefile
@@ -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-*
diff --git a/pk.h.in b/pk.h.in
index ef61ecd..ddc897e 100644
--- a/pk.h.in
+++ b/pk.h.in
@@ -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@@"
diff --git a/pktmr.h b/pktmr.h
new file mode 100644
index 0000000..9245373
--- /dev/null
+++ b/pktmr.h
@@ -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;
+}