Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save ahrex/9a84f32a33aadc197a688d2158d7e2ea to your computer and use it in GitHub Desktop.
Save ahrex/9a84f32a33aadc197a688d2158d7e2ea to your computer and use it in GitHub Desktop.
Wrap arm64's Go around an FPM qemu-user detection script
From b911bf398bbdfbde514a1298ec46e4cd760a9c98 Mon Sep 17 00:00:00 2001
From: Alexander Huynh
Date: Mon, 6 Aug 2018 04:49:33 +0000
Subject: [PATCH] Wrap arm64's Go around an FPM qemu-user detection script
We can work around threading issues by limiting the CPUs that Go
processes can run on to one.
See https://github.com/golang/go/issues/20763#issuecomment-310601756
---
go/arm64-go-wrapper | 27 +++++++++++++++++++++++++++
go/arm64-postinstall.sh | 18 ++++++++++++++++++
go/common.mk | 2 ++
3 files changed, 47 insertions(+)
create mode 100644 go/arm64-go-wrapper
create mode 100755 go/arm64-postinstall.sh
diff --git a/go/arm64-go-wrapper b/go/arm64-go-wrapper
new file mode 100644
index 0000000..a1a4ea7
--- /dev/null
+++ b/go/arm64-go-wrapper
@@ -0,0 +1,27 @@
+#!/bin/sh
+
+main() {
+ local script_path="$(dirname "$(readlink -f "$0")")"
+
+ local go_path="$script_path/go"
+
+ local head="/usr/bin/head"
+ local lscpu="/usr/bin/lscpu"
+ local sed="/bin/sed"
+ local sort="/usr/bin/sort"
+ local taskset="/usr/bin/taskset"
+
+ local rand_cpu="$(
+ "$lscpu" -p | \
+ "$sed" -ne '/^[0-9]\+/ s/,.*$//pg' | \
+ "$sort" -R | \
+ "$head" -n 1
+ )"
+
+ # limit to one CPU; qemu-user + Go have known issues
+ #
+ # See https://github.com/golang/go/issues/20763#issuecomment-310601756
+ exec "$taskset" -c "$rand_cpu" "$go_path" "$@"
+}
+
+main "$@"
diff --git a/go/arm64-postinstall.sh b/go/arm64-postinstall.sh
new file mode 100755
index 0000000..f0e9826
--- /dev/null
+++ b/go/arm64-postinstall.sh
@@ -0,0 +1,18 @@
+ # This script is intended to be embedded by `fpm` into the post-install
+ # and post-upgrade hooks.
+ #
+ # The template for this embed lives at
+ # https://github.com/jordansissel/fpm/blob/f92427dc8a373571ac3ccf0eec541112798b9f2f/templates/deb/postinst_upgrade.sh.erb,
+ # lines L7-L9 and L30-L32.
+ #
+ # You can see that we're dropped in under a /bin/sh handler, as part of
+ # the shell script. Embed accordingly.
+
+ local qemu_hint_path="/qemu-aarch64"
+ local link_path="/usr/local/bin/go"
+
+ if [ -e "$qemu_hint_path" ] && [ -L "$link_path" ]; then
+ ln -sf ../go/bin/arm64-go-wrapper "$link_path"
+ fi
+
+ # vim: set et ts=4 sw=4:
diff --git a/go/common.mk b/go/common.mk
index d884727..11dc82e 100644
--- a/go/common.mk
+++ b/go/common.mk
@@ -85,0 +86,2 @@ package-%: FPM_ARGS += --architecture $*
+package-arm64: FPM_ARGS += --after-install $(TOP)/arm64-postinstall.sh
+package-arm64: FPM_ARGS += --after-upgrade $(TOP)/arm64-postinstall.sh
--
2.18.0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment