Skip to content

Instantly share code, notes, and snippets.

@lyveng
lyveng / availableProcessorsBug.java
Created April 4, 2024 09:04
Java code to illustrate the regression in Runtime.availableProcessors function
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
public class Main {
public static void main(String[] args) throws Exception {
AtomicBoolean stop = new AtomicBoolean();
AtomicInteger count = new AtomicInteger();
int numThreads = 1;
String numThreadsEnv = System.getenv("NUM_THREADS");
@lyveng
lyveng / brew.md
Last active November 30, 2023 02:46
Brew Cheatsheet for Apt/Dpkg Users

Homebrew Cheat Sheet for apt/dpkg Users

This cheat sheet maps common apt/dpkg commands to their Homebrew (brew) equivalents, helping users familiar with Debian's package management to easily transition to Homebrew.

Command Comparison

Task apt/dpkg Command brew Command
Update Package Database sudo apt update brew update
Upgrade Installed Packages sudo apt upgrade brew upgrade
@lyveng
lyveng / find_host.py
Last active January 13, 2021 13:13
Used to find the host that is causing "grouping labels must ensure unique matches" error. Details in https://github.com/prometheus/prometheus/issues/5724
import requests
nodes = [] # populate this variable with the list of nodes in the cluster
prometheus_host = "" # populate this variable with the prometheus service ip
url = "http://" + prometheus_host + "/api/v1/query?query=count by(cluster, node) (sum by(node, cpu) (node_cpu_seconds_total{job=\"node-exporter\"} * on(namespace, pod) group_left(node) node_namespace_pod:kube_pod_info:{node=\"%s\"}))"
for i, node in enumerate(nodes, start=1):
response = requests.get(url % node)
if response.status_code != 200:
print("Failed for node %s" % node)
continue
@lyveng
lyveng / br_debug.c
Created January 2, 2020 04:18
bpftrace to debug packets dropped by L2 Bridge
#include <net/sock.h>
#include <linux/skbuff.h>
#include <linux/ip.h>
struct net_bridge_port {
struct net_bridge *br;
struct net_device *dev;
struct list_head list;
unsigned long flags;
}
@lyveng
lyveng / br_forward.c
Last active January 2, 2020 04:17
Embeds for Blog Post on Ebpf
void br_forward(const struct net_bridge_port *to,
struct sk_buff *skb, bool local_rcv, bool local_orig)
{
if (to && should_deliver(to, skb)) {
if (local_rcv)
deliver_clone(to, skb, local_orig);
else
__br_forward(to, skb, local_orig);
return;
}
class IndexMinPQ:
def __init__(self, max_n):
assert max_n >= 0
self.max_N = max_n
self.n = 0
self.keys = [None] * (max_n + 1)
self.pq = [0] * (max_n + 1)
self.qp = [-1] * (max_n + 1)
@lyveng
lyveng / Jewelery.java
Last active August 29, 2015 14:04
Topcoder Jewelery problem.
import java.util.Arrays;
public class Jewelry {
// Maximum sum that can be formed.
int MAXV = 30000;
// cache for the binmoial coefficient n choose k.
long[][] comb = new long[31][31];
public long howMany(int[] values) {
Arrays.sort(values);
@lyveng
lyveng / NonAdjacentMaximumSumSubsequence.java
Created June 16, 2014 12:53
NonAdjacentMaximumSumSubsequence.java
package geek.livingstone.problems.arrays;
public class NonAdjacentMaximumSumSubsequence {
public static int getMaxSubSequenceSum(int[] A) {
// sum[i] will be the max sum for A[0...i]
int[] sum = new int[A.length+1];
int maxSum = Integer.MIN_VALUE;
for (int i=0;i<A.length;i++)
{
sum[i] = A[i];