Skip to content

Instantly share code, notes, and snippets.

@KJTsanaktsidis
KJTsanaktsidis / utf8_validation_benchmark.rb
Created February 13, 2024 00:39
utf8_validation_benchmark.rb
# frozen_string_literal: true
puts "setting up gems..."
require 'bundler/inline'
gemfile do
gem 'activesupport', '~> 7'
gem 'benchmark-ips', '~> 2'
gemspec path: '.'
end
@KJTsanaktsidis
KJTsanaktsidis / uffd.c
Created January 10, 2024 23:25
User fault handling example
#define _GNU_SOURCE
#include <err.h>
#include <fcntl.h>
#include <linux/userfaultfd.h>
#include <poll.h>
#include <pthread.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@KJTsanaktsidis
KJTsanaktsidis / gai_debug.c
Created December 8, 2023 09:40
Bug #20048 debugging
#include <netinet/in.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <stdio.h>
#include <arpa/inet.h>
#include <errno.h>
#include <string.h>
#include <stdbool.h>
# frozen_string_literal: true
module TrapDetection
def trap(signal, action = nil, &block)
# A block might or might not be given, and action might be absent, a string, or
# a callable. It's actually legal to pass both action and block to Signal.trap, but
# in that case the block is ignored.
if action.respond_to?(:call)
action = Thunk.new(action)
end
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Flame Graph</title>
<meta name="template_version" content="4.0.7"><script>!function(t){var n={};function e(r){if(n[r])return n[r].exports;var i=n[r]={i:r,l:!1,exports:{}};return t[r].call(i.exports,i,i.exports,e),i.l=!0,i.exports}e.m=t,e.c=n,e.d=function(t,n,r){e.o(t,n)||Object.defineProperty(t,n,{enumerable:!0,get:r})},e.r=function(t){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})},e.t=function(t,n){if(1&n&&(t=e(t)),8&n)return t;if(4&n&&"object"==typeof t&&t&&t.__esModule)return t;var r=Object.create(null);if(e.r(r),Object.defineProperty(r,"default",{enumerable:!0,value:t}),2&n&&"string"!=typeof t)for(var i in t)e.d(r,i,function(n){return t[n]}.bind(null,i));return r},e.n=function(t){var n=t&&t.__esModule?function(){return t.default}:function(){return t};return e.d(n,"a",n),n},e.o=function(t,n){return Object.prototype.hasOwnProp
Thread 1 locks the mutex
Thread 1 can get the resource
Thread 1 sleeps for a while ("sleep with resource #1")
Thread 2 tries to lock the mutex, gets put on the waitq
Thread 3 tries to lock the mutex, now the waitq is [2, 3]
Thread 1 returns the resource and unlocks the mutex
Thread 1 signals thread 2, since it's first on the waitq
Thread 2 is eligible for running now (according to ruby) but it's blocked waiting for the GVL
Thread 1 locks the mutex again
Thread 1 takes the resource again
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <time.h>
#include <fcntl.h>
#include <errno.h>
(gdb) info threads
Id Target Id Frame
* 1 LWP 100729 of process 15250 _umtx_op_err () at /usr/src/lib/libthr/arch/amd64/amd64/_umtx_op_err.S:40
2 LWP 105716 of process 15250 _umtx_op_err () at /usr/src/lib/libthr/arch/amd64/amd64/_umtx_op_err.S:40
(gdb) thread 1
[Switching to thread 1 (LWP 100729 of process 15250)]
#0 _umtx_op_err () at /usr/src/lib/libthr/arch/amd64/amd64/_umtx_op_err.S:40
40 in /usr/src/lib/libthr/arch/amd64/amd64/_umtx_op_err.S
(gdb) bt
#0 _umtx_op_err () at /usr/src/lib/libthr/arch/amd64/amd64/_umtx_op_err.S:40
(gdb) info threads
Id Target Id Frame
* 1 LWP 944671 of process 58279 _sigprocmask () at _sigprocmask.S:4
2 LWP 944703 of process 58279 "Timeout stdlib thre" thr_kill () at thr_kill.S:4
(gdb) bt
#0 _sigprocmask () at _sigprocmask.S:4
#1 0x000034cbaa077b44 in handle_signal (actp=actp@entry=0x34cba58367c0, sig=sig@entry=26, info=info@entry=0x34cba5836bb0, ucp=ucp@entry=0x34cba5836840) at /usr/src/lib/libthr/thread/thr_sig.c:288
#2 0x000034cbaa07711f in thr_sighandler (sig=26, info=0x0, _ucp=0x0) at /usr/src/lib/libthr/thread/thr_sig.c:246
#3 <signal handler called>
#4 _umtx_op_err () at /usr/src/lib/libthr/arch/amd64/amd64/_umtx_op_err.S:40
ruby 3.3.0dev (2023-03-18T02:16:48Z ktsanaktsidis/yjit.. d09365c3b3) [aarch64-linux]
-------------- ----------- ---------- --------- ---------- ----------- ------------
bench interp (ms) stddev (%) yjit (ms) stddev (%) interp/yjit yjit 1st itr
activerecord 205.7 0.8 98.5 1.7 2.09 0.51
hexapdf 4967.0 0.8 2458.2 2.4 2.02 1.13
liquid-c 89.4 1.5 63.1 4.2 1.42 0.26
liquid-render 299.2 0.8 135.0 3.6 2.22 0.58
mail 208.0 0.5 105.5 4.8 1.97 0.29
psych-load 4492.7 0.1 1922.0 0.3 2.34 2.21
railsbench 2793.0 0.3 1556.0 1.5 1.80 0.70