aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFlorian Fischer <florian.fl.fischer@fau.de>2020-04-28 14:55:21 +0200
committerFlorian Fischer <florian.fl.fischer@fau.de>2020-04-28 14:56:29 +0200
commit08db521ad4ac9718a1a41e1c32b22f34c123d33a (patch)
tree78fefebffdcc39e7af50dd96492488cfcf3c63a9
parent289cc71e11fe05b9e06f13dd4a3d67401825c82c (diff)
downloadallocbench-08db521ad4ac9718a1a41e1c32b22f34c123d33a.tar.gz
allocbench-08db521ad4ac9718a1a41e1c32b22f34c123d33a.zip
chattymalloc: trace thread_termination
-rw-r--r--TODO3
-rw-r--r--src/chattymalloc.c44
-rw-r--r--src/chattymalloc.h12
-rwxr-xr-xsrc/chattyparser.py1
4 files changed, 54 insertions, 6 deletions
diff --git a/TODO b/TODO
index 90c0bf1..3341b9e 100644
--- a/TODO
+++ b/TODO
@@ -1,6 +1,3 @@
-- chattymalloc
- - trace thread termination
-
- chattyparser
- fix passed allocations
diff --git a/src/chattymalloc.c b/src/chattymalloc.c
index 17a9a31..d89f531 100644
--- a/src/chattymalloc.c
+++ b/src/chattymalloc.c
@@ -60,7 +60,42 @@ static volatile uint64_t total_entries = 0;
static pthread_cond_t growing;
static pthread_mutex_t growth_mutex;
-__thread pid_t tid = 0;
+static __thread pid_t tid = 0;
+
+// thread specific key to register a destructor
+static pthread_key_t tls_key;
+static pthread_once_t tls_key_once = PTHREAD_ONCE_INIT;
+
+// log_thread_termination forward declaration because it uses trace_write
+static void log_thread_termination(void* key __attribute__ ((unused)));
+
+static void make_tls_key()
+{
+ int err = pthread_key_create(&tls_key, log_thread_termination);
+ if (err) {
+ abort();
+ }
+}
+
+static void init_thread()
+{
+ tid = syscall(SYS_gettid);
+
+ // init our thread destructor
+ int err = pthread_once(&tls_key_once, make_tls_key);
+ if (err) {
+ abort();
+ }
+
+ // set the key to something != NULL to execute the destructor on thread exit
+ // NOLINTNEXTLINE(readability-magic-numbers)
+ err = pthread_setspecific(tls_key, (void*)42);
+ if (err) {
+ abort();
+ }
+}
+
+
/*=========================================================
* intercepted functions
@@ -124,7 +159,7 @@ static void
write_trace(char func, void* ptr, size_t size, size_t var_arg)
{
if (unlikely(tid == 0)) {
- tid = syscall(SYS_gettid);;
+ init_thread();
}
uint64_t idx = __atomic_fetch_add (&next_entry, 1, __ATOMIC_SEQ_CST);
@@ -147,6 +182,11 @@ write_trace(char func, void* ptr, size_t size, size_t var_arg)
trace->var_arg = var_arg;
}
+static void log_thread_termination(void* key __attribute__ ((unused)))
+{
+ write_trace(THREAD_TERMINATION, NULL, 0, 0);
+}
+
static void
trim_trace()
{
diff --git a/src/chattymalloc.h b/src/chattymalloc.h
index 2dbc676..38c42d4 100644
--- a/src/chattymalloc.h
+++ b/src/chattymalloc.h
@@ -19,7 +19,17 @@ along with allocbench. If not, see <http://www.gnu.org/licenses/>.
#include <sys/types.h>
-enum functions{MALLOC, FREE, REALLOC, CALLOC, MEMALIGN, POSIX_MEMALIGN, VALLOC, PVALLOC, ALIGNED_ALLOC};
+enum functions {
+ MALLOC,
+ FREE,
+ REALLOC,
+ CALLOC,
+ MEMALIGN,
+ POSIX_MEMALIGN,
+ VALLOC,
+ PVALLOC,
+ ALIGNED_ALLOC,
+ THREAD_TERMINATION};
typedef struct trace {
pid_t tid;
diff --git a/src/chattyparser.py b/src/chattyparser.py
index 69bdf74..33dbf88 100755
--- a/src/chattyparser.py
+++ b/src/chattyparser.py
@@ -41,6 +41,7 @@ class Function(Enum):
valloc = 6
pvalloc = 7
aligned_alloc = 8
+ thread_termination = 9
class Trace: