Skip to contentSkip to conversation

trace-cmd: make it build against musl C library

PATCH Applied Beniamin Sandu 3 participants 3 messages Nov 30, 2020 – Dec 1, 2020

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 trace-cmd: make it build against musl C library

* add some missing headers and macros
* set pthread affinity using pthread_setaffinity_np after creating the thread instead of pthread_attr_setaffinity_np (which seems to not be implemented in musl)

Tested using https://musl.cc/x86_64-linux-musl-native.tgz

---
 .../include/private/trace-cmd-private.h          |  1 +
 lib/trace-cmd/include/trace-cmd-local.h          |  1 +
 lib/tracefs/include/tracefs-local.h              | 12 ++++++++++++
 lib/tracefs/tracefs-events.c                     |  1 +
 tracecmd/include/trace-local.h                   |  1 +
 tracecmd/trace-tsync.c                           | 16 +++++++++++-----
 6 files changed, 27 insertions(+), 5 deletions(-)
lib/trace-cmd/include/private/trace-cmd-private.h +1 -0
index 458760e..a0dac5d 100644
@@ -6,6 +6,7 @@
 #ifndef _TRACE_CMD_PRIVATE_H
 #define _TRACE_CMD_PRIVATE_H
 
+#include <sys/types.h>
 #include "traceevent/event-parse.h"
 #include "trace-cmd/trace-cmd.h"
 
lib/trace-cmd/include/trace-cmd-local.h +1 -0
index d0a7365..0cd2744 100644
@@ -6,6 +6,7 @@
 #ifndef _TRACE_CMD_LOCAL_H
 #define _TRACE_CMD_LOCAL_H
 
+#include <byteswap.h>
 #include "trace-cmd-private.h"
 
 /* Can be overridden */
lib/tracefs/include/tracefs-local.h +12 -0
index 9cc371b..bdbf89e 100644
@@ -13,4 +13,16 @@ void warning(const char *fmt, ...);
 int str_read_file(const char *file, char **buffer);
 char *trace_append_file(const char *dir, const char *name);
 
+#ifndef ACCESSPERMS
+#define ACCESSPERMS (S_IRWXU|S_IRWXG|S_IRWXO) /* 0777 */
+#endif
+
+#ifndef ALLPERMS
+#define ALLPERMS (S_ISUID|S_ISGID|S_ISVTX|S_IRWXU|S_IRWXG|S_IRWXO) /* 07777 */
+#endif
+
+#ifndef DEFFILEMODE
+#define DEFFILEMODE (S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH) /* 0666*/
+#endif
+
 #endif /* _TRACE_FS_LOCAL_H */
lib/tracefs/tracefs-events.c +1 -0
index 80a25ee..a4e5215 100644
@@ -13,6 +13,7 @@
 #include <errno.h>
 #include <sys/stat.h>
 #include <fcntl.h>
+#include <limits.h>
 
 #include "kbuffer.h"
 #include "tracefs.h"
tracecmd/include/trace-local.h +1 -0
index 28d1b4e..85c7e03 100644
@@ -8,6 +8,7 @@
 
 #include <sys/types.h>
 #include <dirent.h>	/* for DIR */
+#include <limits.h>
 
 #include "trace-cmd-private.h"
 #include "event-utils.h"
tracecmd/trace-tsync.c +11 -5
index e639788..1fce6d0 100644
@@ -104,13 +104,16 @@ int tracecmd_host_tsync(struct buffer_instance *instance,
 
 	pthread_attr_init(&attrib);
 	pthread_attr_setdetachstate(&attrib, PTHREAD_CREATE_JOINABLE);
-	if (!get_first_cpu(&pin_mask, &mask_size))
-		pthread_attr_setaffinity_np(&attrib, mask_size, pin_mask);
 
 	ret = pthread_create(&instance->tsync_thread, &attrib,
 			     tsync_host_thread, &instance->tsync);
-	if (!ret)
+
+	if (!ret) {
+		if (!get_first_cpu(&pin_mask, &mask_size))
+			pthread_setaffinity_np(instance->tsync_thread, mask_size, pin_mask);
 		instance->tsync_thread_running = true;
+	}
+
 	if (pin_mask)
 		CPU_FREE(pin_mask);
 	pthread_attr_destroy(&attrib);
@@ -243,11 +246,14 @@ unsigned int tracecmd_guest_tsync(char *tsync_protos,
 	pthread_attr_init(&attrib);
 	tsync->sync_proto = proto;
 	pthread_attr_setdetachstate(&attrib, PTHREAD_CREATE_JOINABLE);
-	if (!get_first_cpu(&pin_mask, &mask_size))
-		pthread_attr_setaffinity_np(&attrib, mask_size, pin_mask);
 
 	ret = pthread_create(thr_id, &attrib, tsync_agent_thread, tsync);
 
+	if(!ret) {
+		if (!get_first_cpu(&pin_mask, &mask_size))
+			pthread_setaffinity_np(*thr_id, mask_size, pin_mask);
+	}
+
 	if (pin_mask)
 		CPU_FREE(pin_mask);
 	pthread_attr_destroy(&attrib);
Signature · 1 line
2.25.1
Reviewed-by on v2 1/1
#1 lore raw
Quoted 9 lines · Beniamin Sandu

* add some missing headers and macros
* set pthread affinity using pthread_setaffinity_np after creating the thread instead of pthread_attr_setaffinity_np (which seems to not be implemented in musl)

Tested using https://musl.cc/x86_64-linux-musl-native.tgz

Thanks Beniamin!
There is one minor coding style error, as we are using the kernel's coding style.
No need to send another version, we will fix it when pulling the patch. Also, note that we are moving the libtracefs in its own repo:
https://git.kernel.org/pub/scm/libs/libtrace/libtracefs.git/
I'm going to port the libtracefs part of your patch to that repo too.

Quoted 103 lines
---
 .../include/private/trace-cmd-private.h          |  1 +
 lib/trace-cmd/include/trace-cmd-local.h          |  1 +
 lib/tracefs/include/tracefs-local.h              | 12 ++++++++++++
 lib/tracefs/tracefs-events.c                     |  1 +
 tracecmd/include/trace-local.h                   |  1 +
 tracecmd/trace-tsync.c                           | 16 +++++++++++-----
 6 files changed, 27 insertions(+), 5 deletions(-)
lib/trace-cmd/include/private/trace-cmd-private.h +1 -0
index 458760e..a0dac5d 100644
@@ -6,6 +6,7 @@
 #ifndef _TRACE_CMD_PRIVATE_H
 #define _TRACE_CMD_PRIVATE_H
 
+#include <sys/types.h>
 #include "traceevent/event-parse.h"
 #include "trace-cmd/trace-cmd.h"
 
lib/trace-cmd/include/trace-cmd-local.h +1 -0
index d0a7365..0cd2744 100644
@@ -6,6 +6,7 @@
 #ifndef _TRACE_CMD_LOCAL_H
 #define _TRACE_CMD_LOCAL_H
 
+#include <byteswap.h>
 #include "trace-cmd-private.h"
 
 /* Can be overridden */
lib/tracefs/include/tracefs-local.h +12 -0
index 9cc371b..bdbf89e 100644
@@ -13,4 +13,16 @@ void warning(const char *fmt, ...);
 int str_read_file(const char *file, char **buffer);
 char *trace_append_file(const char *dir, const char *name);
 
+#ifndef ACCESSPERMS
+#define ACCESSPERMS (S_IRWXU|S_IRWXG|S_IRWXO) /* 0777 */
+#endif
+
+#ifndef ALLPERMS
+#define ALLPERMS (S_ISUID|S_ISGID|S_ISVTX|S_IRWXU|S_IRWXG|S_IRWXO) /* 07777 */
+#endif
+
+#ifndef DEFFILEMODE
+#define DEFFILEMODE (S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH) /* 0666*/
+#endif
+
 #endif /* _TRACE_FS_LOCAL_H */
lib/tracefs/tracefs-events.c +1 -0
index 80a25ee..a4e5215 100644
@@ -13,6 +13,7 @@
 #include <errno.h>
 #include <sys/stat.h>
 #include <fcntl.h>
+#include <limits.h>
 
 #include "kbuffer.h"
 #include "tracefs.h"
tracecmd/include/trace-local.h +1 -0
index 28d1b4e..85c7e03 100644
@@ -8,6 +8,7 @@
 
 #include <sys/types.h>
 #include <dirent.h>    /* for DIR */
+#include <limits.h>
 
 #include "trace-cmd-private.h"
 #include "event-utils.h"
tracecmd/trace-tsync.c +6 -3
index e639788..1fce6d0 100644
@@ -104,13 +104,16 @@ int tracecmd_host_tsync(struct buffer_instance *instance,
 
        pthread_attr_init(&attrib);
        pthread_attr_setdetachstate(&attrib, PTHREAD_CREATE_JOINABLE);
-       if (!get_first_cpu(&pin_mask, &mask_size))
-               pthread_attr_setaffinity_np(&attrib, mask_size, pin_mask);
 
        ret = pthread_create(&instance->tsync_thread, &attrib,
                             tsync_host_thread, &instance->tsync);
-       if (!ret)
+
+       if (!ret) {
+               if (!get_first_cpu(&pin_mask, &mask_size))
+                       pthread_setaffinity_np(instance->tsync_thread, mask_size, pin_mask);
                instance->tsync_thread_running = true;
+       }
+
        if (pin_mask)
                CPU_FREE(pin_mask);
        pthread_attr_destroy(&attrib);

tracecmd/trace-tsync.c unsigned int tracecmd_guest_tsync(char *tsync_protos, · line 252

Quoted 9 lines
tracecmd/trace-tsync.c +1 -2
@@ -243,11 +246,14 @@ unsigned int tracecmd_guest_tsync(char *tsync_protos,
        pthread_attr_init(&attrib);
        tsync->sync_proto = proto;
        pthread_attr_setdetachstate(&attrib, PTHREAD_CREATE_JOINABLE);
-       if (!get_first_cpu(&pin_mask, &mask_size))
-               pthread_attr_setaffinity_np(&attrib, mask_size, pin_mask);
 
        ret = pthread_create(thr_id, &attrib, tsync_agent_thread, tsync);
 
+       if(!ret) {

There should be a space before the open parenthesis here.

Quoted 10 lines
tracecmd/trace-tsync.c +4 -1
+               if (!get_first_cpu(&pin_mask, &mask_size))
+                       pthread_setaffinity_np(*thr_id, mask_size, pin_mask);
+       }
+
        if (pin_mask)
                CPU_FREE(pin_mask);
        pthread_attr_destroy(&attrib);
--

2.25.1

Signature · 2 lines
Tzvetomir (Ceco) Stoyanov
VMware Open Source Technology Center
#2 lore raw
Quoted 19 lines · On Mon, 30 Nov 2020 18:24:05 +0200 Tzvetomir Stoyanov <[email protected]>…
Quoted 9 lines · Beniamin Sandu

* add some missing headers and macros
* set pthread affinity using pthread_setaffinity_np after creating the thread instead of pthread_attr_setaffinity_np (which seems to not be implemented in musl)

Tested using https://musl.cc/x86_64-linux-musl-native.tgz

Thanks Beniamin!
There is one minor coding style error, as we are using the kernel's coding style.
No need to send another version, we will fix it when pulling the patch. Also, note that we are moving the libtracefs in its own repo:
https://git.kernel.org/pub/scm/libs/libtrace/libtracefs.git/
I'm going to port the libtracefs part of your patch to that repo too.

Quoted 2 lines

I applied this. Thanks Beniamin and Tzvetomir!

-- Steve