Skip to content

Commit

Permalink
Updating ruby-check and Gemfile.lock
Browse files Browse the repository at this point in the history
  • Loading branch information
kigster committed Oct 29, 2024
1 parent dba2432 commit ae3b874
Show file tree
Hide file tree
Showing 2 changed files with 124 additions and 94 deletions.
40 changes: 20 additions & 20 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
GIT
remote: https://github.com/asciidoctor/asciidoctor-pdf.git
revision: c3e27b121235230c34586320a2d718e0f898178e
revision: daeafdde601c87e644c9ed28091672b48cc80c40
specs:
asciidoctor-pdf (3.0.0.alpha.0)
asciidoctor (~> 2.0)
Expand Down Expand Up @@ -31,7 +31,7 @@ GEM
remote: https://rubygems.org/
specs:
Ascii85 (1.1.1)
activesupport (7.2.1)
activesupport (7.2.1.2)
base64
bigdecimal
concurrent-ruby (~> 1.0, >= 1.3.1)
Expand All @@ -50,21 +50,21 @@ GEM
console (~> 1.26)
fiber-annotation
io-event (~> 1.6, >= 1.6.5)
async-http (0.80.0)
async-http (0.82.2)
async (>= 2.10.2)
async-pool (~> 0.7)
io-endpoint (~> 0.11)
io-stream (~> 0.4)
async-pool (~> 0.9)
io-endpoint (~> 0.14)
io-stream (~> 0.6)
metrics (~> 0.12)
protocol-http (~> 0.37)
protocol-http1 (~> 0.27)
protocol-http1 (>= 0.28.1)
protocol-http2 (~> 0.19)
traces (>= 0.10)
traces (~> 0.10)
async-http-faraday (0.19.0)
async-http (~> 0.42)
faraday
async-pool (0.8.1)
async-pool (0.10.1)
async (>= 1.25)
metrics
traces
base64 (0.2.0)
bigdecimal (3.1.8)
Expand All @@ -76,7 +76,7 @@ GEM
fiber-annotation
fiber-local (~> 1.1)
json
css_parser (1.19.0)
css_parser (1.19.1)
addressable
drb (2.2.1)
faraday (2.12.0)
Expand Down Expand Up @@ -104,19 +104,19 @@ GEM
rainbow (>= 2.2.1)
rake (>= 10.0)
hashery (2.1.2)
hexapdf (0.47.0)
hexapdf (1.0.0)
cmdparse (~> 3.0, >= 3.0.3)
geom2d (~> 0.4, >= 0.4.1)
openssl (>= 2.2.1)
i18n (1.14.6)
concurrent-ruby (~> 1.0)
io-endpoint (0.13.1)
io-event (1.7.0)
io-stream (0.4.3)
json (2.7.2)
io-endpoint (0.14.0)
io-event (1.7.3)
io-stream (0.6.0)
json (2.7.4)
logger (1.6.1)
matrix (0.4.2)
metrics (0.10.2)
metrics (0.12.0)
minitest (5.25.1)
multi_json (1.15.0)
net-http (0.4.1)
Expand Down Expand Up @@ -147,8 +147,8 @@ GEM
pdf-reader (~> 2.0)
prawn (~> 2.2)
protocol-hpack (1.5.1)
protocol-http (0.37.0)
protocol-http1 (0.27.0)
protocol-http (0.42.0)
protocol-http1 (0.28.1)
protocol-http (~> 0.22)
protocol-http2 (0.19.3)
protocol-hpack (~> 1.4)
Expand All @@ -157,7 +157,7 @@ GEM
pygments.rb (3.0.0)
rainbow (3.1.1)
rake (13.2.1)
rexml (3.3.8)
rexml (3.3.9)
rghost (0.9.9)
rouge (4.4.0)
ruby-rc4 (0.1.5)
Expand Down
178 changes: 104 additions & 74 deletions bin/ruby-check
Original file line number Diff line number Diff line change
@@ -1,41 +1,13 @@
#!/usr/bin/env bash
# vi: ft=sh
# vi: ft=bash
#
# https://gist.github.com/kigster/8ddebf9fddff25620e64d558dd4d56b7
# © 2019-2024 Konstantin Gredeskoul, Inc., All rights reserved. MIT LICENSE
#
# @description This is a script from the Bashmatic Library
# It validates all discovered rubies and checks
# for jemalloc presence.
#
# © 2019-2022 Konstantin Gredeskoul, Inc., All rights reserved.
# MIT LICENSE
# ————————————————————————————————————————————————————————————————
# This script verifies that the ruby interpreter (either the one in
# the $PATH, or the one specified by the -r / --ruby option) is linked
# with jemalloc library for efficient memory utilization, and/or
# support YJIT (Yet Another JIT) compiler.
#
# It works well on Linux and Mac OSX.
#
# BACKGROUND
# ————————————————————————————————————————————————————————————————
# Ruby versions 2.5 and earlier had a simpler method, typically
# running the following command and expecting the output to
# contain something like '-ljemalloc -lm ... '
#
# ruby -r rbconfig -e "puts RbConfig.CONFIG['LIBS']"
#
# Unfortunately, this method no longer works with Ruby 2.6
# and later, nor did it ever work with Linux.
#
# For this reason, the following script is provided to verify
# if your Ruby is using jemalloc memory allocator or not.
#
# USAGE:
# curl -fSL http://bit.ly/ruby-jemalloc-check > ruby-check
# chmod 755 ruby-check
#
# ruby-check --help
# ruby-check -r /usr/bin/ruby
# ruby-check -a -v # check all in verbose mode
#
# ————————————————————————————————————————————————————————————————
# @see https://github.com/kigster/bashmatic/bin/ruby-check

set +e

Expand Down Expand Up @@ -68,6 +40,15 @@ export rb_action_yjit_pid=

export rb_action_help=false

declare rb_have_rbenv
if [[ -n "$(command -V rbenv 2>/dev/null)" ]]; then
export rb_have_rbenv=true
else
export rb_have_rbenv=false
fi

export rb_versions_normalized=false

export color_red="\e[1;31m"
export color_green="\e[1;32m"
export color_yellow="\e[1;33m"
Expand All @@ -93,38 +74,64 @@ declare -g -i rubies_with_yjit
export rubies_with_jemalloc=0
export rubies_with_yjit=0

# Versions will contain
mapfile -t rbenv_versions < <(rbenv versions --bare)
export rbenv_versions
if ${rb_have_rbenv}; then
# Versions will contain
mapfile -t rbenv_versions < <(rbenv versions --bare)
export rbenv_versions
fi

export ruby_candidates=(/usr/bin/ruby /usr/local/bin/ruby /opt/hombrew/bin/ruby)
export ruby_candidates=(
/bin/ruby
/sbin/ruby
/usr/bin/ruby
/usr/sbin/ruby
/usr/local/bin/ruby
/opt/hombrew/bin/ruby
$(which ruby 2>/dev/null || true)
)

function rb.normalize-versions() {
${rb_versions_normalized} && return 0

local -a rubies=()
local local=
local version

# shellcheck disable=SC2002
[[ -f .ruby-version ]] && local="$(cat ".ruby-version" | tr -d '\n')"

rubies+=( ${rbenv_versions[@]} )
rubies+=( ${ruby_candidates[@]} )

local -a rubies_checked=()

for candidate in "${rubies[@]}"; do
# compare a string that starts with space, ends with space and has space-separated
# paths listed in a sequence to find duplicates and skip them
if [[ " ${rubies_checked[*]} " =~ " ${candidate} " ]]; then
continue
fi

# register this unique ruby in the list of already seen
rubies_checked+=("${candidate}")

# check if it exists and is executabler
if [[ -x ${candidate} ]]; then
ruby_paths+=("${candidate}")
elif [[ "${candidate}" =~ ^[\.0-9]+$ ]]; then
local version=${candidate}
# make local folder this version
rbenv local "${version}"
local ruby_path="$(rbenv which ruby)"
[[ -x ${ruby_path} ]] && ruby_paths+=("${ruby_path}")
elif [[ "${candidate}" =~ ^[\.0-9]+$ ]] && ${rb_have_rbenv}; then
# this would be the result of "rbenv versions" — we convert it into a real ruby path
version=${candidate}
# make local folder this vxersion
if ${rb_have_rbenv} >/dev/null; then
rbenv local "${version}"
local ruby_path="$(rbenv prefix)/bin/ruby"
[[ -x ${ruby_path} ]] && ruby_paths+=("${ruby_path}")
fi
fi
done

[[ -n ${local} && ${local} != $(rbenv local 2>/dev/null) ]] && rbenv local "${local}"
export rb_versions_normalized=true
}

rb.normalize-versions

function line() {
${rb_option_verbose} && rb.printf "${color_green}──────────────────────────────────────────────────────────────────────────────────────────────────${clr}\n" >&2
}
Expand Down Expand Up @@ -221,6 +228,14 @@ function rb.jemalloc.stats-if-enabled() {
fi
}

function rb.yjit.stats-if-enabled() {
local rb="$1"; shift

if ${rb_action_yjit_stats}; then
rb.yjit.stats "${rb}" "${rb_action_yjit_pid}"
fi
}

# @description Prints the help screen and exits
function usage() {
rb.printf "
Expand Down Expand Up @@ -252,35 +267,40 @@ ${color_green}DESCRIPTION:${clr}
Ruby is linked with jemalloc, it will print stats multiple times:
one for each Ruby
${color_green}WHICH RUBY:${clr}
-c/--current Detect if the current (in the \$PATH) ruby binary is
linked with jemalloc. Compatible with -s.
${color_green}SELECING RUBIES:${clr}
-c/--current Detect if the current (in the \$PATH) ruby binary is
linked with jemalloc. Compatible with -s.
-a/--all Find all rubies in the \$PATH and ~/.rbenv that
have been linked with jemalloc and yjit
-a/--all Find all rubies in the \$PATH and perform the check(s)
-r/--ruby PATH Path to the ruby binary to check (defaults to the
first ruby found in the \$PATH)
-r/--ruby PATH Path to the ruby binary to check (defaults to the
first ruby found in the \$PATH)
NOTE: the above flags require a select from the following
section, which specifies which checks to perform.
${color_green}ADDITIONAL ACTIONS:${clr}
-j/--jm-stats Print the jemalloc stats (when linked with jemalloc)
-y/--yjit-stats Print the YJIT stats (requires --pid)
${color_green}RUBY CHECKS:${clr}
-j/--jm-stats Strart a ruby process and print its the jemalloc stats
(when linked with jemalloc)
-p/--pid PID of the Ruby process to example for YJIT stats
-y/--yjit-stats PID Print the YJIT stats for another running Ruby PID.
${color_green}OPTIONS:${clr}
-q/--quiet Do not print output, exit with 1 if no jemalloc, or 0
if jemalloc was found.
-q/--quiet Print less output, exit with 1 if no jemalloc, or 0
if jemalloc was found.
-v/--verbose Print all discovered Ruby Interpreters before checking.
-v/--verbose Print all discovered Ruby Interpreters before checking.
-h/--help This page (also printed when no arguments are provided)
-h/--help This page (also printed when no arguments are provided)
"
exit 0
}

function rb.first-pid() {
ps -e -o pid,args | grep -E 'ruby|puma|irb|rails|sidekiq|unicorn|rake' | grep -v grep | awk '{print $1}' | head -1
}

function rb.parse-opts() {
if [[ $# -eq 0 ]]; then
usage
Expand Down Expand Up @@ -317,17 +337,25 @@ function rb.parse-opts() {
export rb_action_je_stats=true
;;

-p | --pid)
shift
export rb_action_yjit_pid=$1
shift
;;

-y | --yjit-stats)
shift
export rb_action_yjit_stats=true
export rb_action_yjit_pid=$1

[[ ${rb_action_yjit_pid} =~ ^-?[0-9]+$ ]] || {
rb.error "Invalid PID provided to --yjit-stats: ${rb_action_yjit_pid}"
if [[ ${1} =~ ^-?[0-9]+$ ]]; then
if [[ -z ${rb_action_yjit_pid} ]]; then
export rb_action_yjit_pid=$1
shift
fi
else
rb.error "--yjit-stats requires a PID argument"
exit 1
}

shift
fi
;;

# Output Options
Expand All @@ -352,7 +380,7 @@ function rb.parse-opts() {
;;
*)
[[ -n $arg ]] && {
rb.printf "${color_red}ERROR: invalid option: $arg${clr}\n"
rb.error "${color_red}ERROR: invalid option: $arg${clr}\n"
exit 1
}
break
Expand Down Expand Up @@ -392,6 +420,8 @@ function rb.main() {

export code=0

rb.normalize-versions

if [[ ${rb_check_current_ruby} == "true" ]]; then
if [[ $(which ruby) =~ shims ]]; then
current_ruby="$(rbenv which ruby)"
Expand Down Expand Up @@ -423,9 +453,9 @@ function rb.main() {
rb.header "Checking specified ruby ${rb_check_current_ruby}..."
rubies_to_check+=("${rb_check_specified_ruby}")
else # default to the first ruby found
usage
# shellcheck disable=SC2317
rb.error "You did not specify which rubies to check."
rb.error "Run with --help for more information."
exit 2
fi

[[ ${#rubies_to_check[@]} -eq 0 ]] && {
Expand Down Expand Up @@ -455,7 +485,7 @@ function rb.main() {
fi

if [[ ${rubies_with_jemalloc} -eq 0 ]]; then
rb.error "no rubies with jemalloc were found :-("
rb.error "No rubies with jemalloc were found :-("
return 1
else
echo
Expand Down

0 comments on commit ae3b874

Please sign in to comment.