Skip to content

Instantly share code, notes, and snippets.

@jcttrll
jcttrll / proc.sh
Created February 19, 2024 13:12
Pure Bash CPU/core/thread count from /proc/cpuinfo
#!/bin/bash -eE
main() {
local -A physicalIds coreIds
local count=0
local name _ value
while IFS=$' \t' read -r name _ value; do
case "$name" in
processor) count=$((count + 1)) ;;
@jcttrll
jcttrll / wait-port-open.sh
Last active June 12, 2023 13:39
Attempt to use PID namespace to control process with timeout, while ensuring child cleanup
#!/bin/bash
waitPortOpen() {
local host=$1
local port=$2
local timeoutSeconds=$3
local intervalSeconds=$4
local connectTimeoutSeconds=$5
if [[ -z "$intervalSeconds" ]]; then
@jcttrll
jcttrll / error-handler-snippet.sh
Created February 29, 2020 13:22
Bash error handler, with stacktrace with clickable file:line links (in IntelliJ)
trap onError ERR
onError() {
local exitCode=$? command=(${BASH_COMMAND})
if [[ ${BASH_SUBSHELL} -gt 0 ]]; then
exit ${exitCode}
fi
echo -e "\nERROR: Failed with exit code $exitCode when executing: ${command[@]}" >&2
@jcttrll
jcttrll / WaitSpinLocksExample.java
Created January 3, 2020 02:28
Java wait()/notify() example showing and explaining the use of a spinlock around the wait() call
public class WaitSpinLocksExample {
public static void main(String[] args) {
Thread thread1, thread2;
Base bad = new BadImplementationSubjectToSpuriousWakeup();
thread1 = new Thread(bad::print);
thread2 = new Thread(bad::calculate);
thread1.start();
thread2.start();
@jcttrll
jcttrll / SimpleIoTest.java
Created February 4, 2019 04:04
Quick 'n' dirty write performance test
import java.io.*;
import java.util.*;
import java.security.SecureRandom;
public class SimpleIoTest {
private static final int BLOCK_SIZE = 1024 * 1024;
private static final int TOTAL_SIZE = 1024 * 1024 * 1024;
private static final int SEQUENTIAL_ITERATIONS = 32;
private static final int RANDOM_ITERATIONS = 16;
@jcttrll
jcttrll / power-delete.psm1
Last active March 6, 2022 17:02
PowerShell module to take ownership of a file/directory, assign permissions allowing deletion, and then delete the item (recursively in the case of directories, and optionally including deletion of all hardlinks to files)
function Power-Delete {
[CmdletBinding()]
param(
[parameter(Position = 0, ValueFromRemainingArguments = $true, ValueFromPipelineByPropertyName = $true)]
$FullName,
[switch]
$AllHardLinks
)
process {
@jcttrll
jcttrll / winkey.sh
Created November 10, 2018 15:49
Show Windows product key embedded in ACPI (for use in VMs)
sudo tail -c 29 /sys/firmware/acpi/tables/MSDM && echo
@jcttrll
jcttrll / blah.wast
Created July 3, 2018 12:42
Trivial example of running assembled WebAssembly text in Node
(module
(func $add (param $a i32) (param $b i32) (result i32)
get_local $a
get_local $b
i32.add
)
(export "add" (func $add))
)
@jcttrll
jcttrll / SimpleTest.groovy
Created March 14, 2018 22:56
Demo of Antlr grammar testing
package com.perihelios.parser.javaregex
import com.perihelios.parser.javaregex.helper.TreeMatcher
import org.junit.Assert
import org.junit.Test
class SimpleTests {
private TreeMatcher treeMatcher = new TreeMatcher(JavaRegexLexer, JavaRegexParser, "regex")
@Test
@jcttrll
jcttrll / build.sh
Last active November 30, 2017 19:05
C mocking experiment
gcc -c main.c -o main.o
gcc -c framework.c -o framework.o
gcc -c test.c -o test.o
gcc -c mock.c -o mock.o && ar rcs mock.a mock.o; rm mock.o
ld custom.ld test.o main.o framework.o \
-L. --build-id --eh-frame-hdr -m elf_x86_64 -dynamic-linker \
/lib64/ld-linux-x86-64.so.2 \
/usr/lib64/gcc/x86_64-suse-linux/4.8/../../../../lib64/crt1.o \
/usr/lib64/gcc/x86_64-suse-linux/4.8/../../../../lib64/crti.o \