Skip to content

Instantly share code, notes, and snippets.

@pkhuong
pkhuong / ppp_lock.rs
Last active April 26, 2024 13:24
Fast path for single-owner Arc<Mutex>
//! The `ppp_lock` module defines traits to access the value in a [`Arc<Mutex<T>>`]
//! with higher-order [`Update<T>::update`] or [`TryUpdate<T>::try_update`]
//! methods. The higher-order interface lets the methods implement a fast path
//! for single-reference mutable (i.e., exclusively owned) [`Arc`]s.
//!
//! Using the `ppp_lock` traits avoids atomics for private owners (single-reference
//! `&mut Arc<Mutex<T>>`), while incurring marginal additional overhead when the
//! object is actually shared (multi-reference `Arc`)... and the traits, with their
//! higher-order methods, complicate access compared to regular locking.
//!
@pkhuong
pkhuong / yannakakis.md
Last active April 13, 2024 14:36
A minimal version of Yannakakis's algorithm for mostly plain Python
#!/usr/bin/env sed -re s|^|\x20\x20\x20\x20| -e s|^\x20{4}\x23\x23{(.*)$|<details><summary>\1</summary>\n| -e s|^\x20{4}\x23\x23}$|\n</details>| -e s|^\x20{4}\x23\x23\x20?|| -e s|\x0c|\x20|
license, imports
# Yannakakis.py by Paul Khuong
#
# To the extent possible under law, the person who associated CC0 with
# Yannakakis.py has waived all copyright and related or neighboring rights
# to Yannakakis.py.

% perf stat ./tsx_per_core
Aborts: 73381
No reason: 57535
reason 0: 0
reason 1: 15833
reason 2: 15833
reason 3: 13
reason 4: 0
reason 5: 0
81597398 68933303 85476712 72654716 76819140 72119787 77728188 74167801 75244800 72119632 75951739 72606078 74035109 70998737 89185152 58165171 86098994 83338241 88201758 81982602 77964219 77861602 75642915 79207346 73514808 79929374 74429694 71508632 0 0 0 0
@pkhuong
pkhuong / dynamic-variance.py
Last active January 9, 2023 21:03
Fully dynamic variance for a bag of observations
import math
import struct
import unittest
import hypothesis.strategies as st
from hypothesis.stateful import Bundle, RuleBasedStateMachine, consumes, invariant, multiple, precondition, rule
class VarianceStack:
def __init__(self):
self.n = 0
@pkhuong
pkhuong / libbts.c
Last active October 18, 2022 09:01
minimal BTS tracing wrapper for linux perf
#define RUN_ME /*
exec cc -O2 -W -Wall -std=c99 -shared $0 -o "$(basename $0 .c).so" -fPIC
*/
/*
* Copyright 2019 Paul Khuong
* SPDX-License-Identifier: BSD-2-Clause
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
@pkhuong
pkhuong / signal-on-instruction-count.c
Created February 18, 2020 02:21
Trigger a unix signal based on the HW instruction counter PMC
#define RUN_ME /*
exec cc -g -ggdb -O2 -W -Wall -std=c99 $0 -o "$(basename $0 .c)"
*/
/*
* Copyright 2020 Paul Khuong
* SPDX-License-Identifier: BSD-2-Clause
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
@pkhuong
pkhuong / LICENCE
Last active December 15, 2021 23:25
MIT-licensed support code for coverage-guided Hypothesis testing
Copyright 2020 Paul Khuong, Backtrace I/O, Inc.
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWAR
@pkhuong
pkhuong / README.md
Created May 3, 2021 15:39 — forked from parsley42/README.md
Chromebook ssh-agent setup

Setting up ssh-agent in Linux/Crostini on Chromebook

  • Create your user systemd directory:
$ mkdir -p .config/systemd/user
  • Edit the unit file .config/systemd/user/ssh-agent.service:
[Unit]
Description=SSH key agent
@pkhuong
pkhuong / 00RECORD_STREAM.md
Last active January 11, 2021 17:09
Word-stuffed record stream

Backtrace's log record framing format

We use this self-synchronising format to store key metadata for our server-side embedded crash database. See this blog post for more details and explanations.

We released the source under the MIT license, but the scheme is simple enough that coding an interface tuned to one's specific needs probably makes sense.

@pkhuong
pkhuong / kmv_ci.py
Last active September 25, 2020 21:13
Numerical implementation of Beyer et al's confidence intervals for k min values
"""Numerical implementation of Beyer et al's confidence intervals for k min values
[On Synopses for Distinct-Value Estimation Under Multiset Operations](http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.230.7008&rep=rep1&type=pdf#page=6), Section 4.3.
ci_probability(k, eps) computes the probability that the unbiased
cardinality estimate
\hat{D}_k^{UB} = (k - 1) / U(k),
where k is the number of minimum values we keep, and U(k) is the