Skip to content

Instantly share code, notes, and snippets.

@elazarl
elazarl / ecdsa_sign.go
Created March 31, 2014 20:49
An ECDSA based signature scheme compatible with openssl sha256 -sign/-verify
// A signer/verifier compatible with openssl ecdsa signature scheme
//
// $ openssl ecparam -name prime256v1 -genkey -noout >ec.pem
// $ openssl ec -pubout <ec.pem >ecpub.pem
//
// and with sha256 verification
//
// $ echo a|openssl sha256 -sign ec.pem > sig.bin
// $ echo a|openssl sha256 -verify ecpub.pem -signature sig.bin
//
/**
* Usage:
* $ javac KillRegions
* $ # list all regions in cluster, search for first listed region in MYTABLE
* $ java -cp `hbase classpath`:. KillRegions -l|grep MYTABLE|head -n1 >/tmp/regions.txt
* $ # delete first regions in MYTABLE, listed above
* $ java -cp `hbase classpath`:. KillRegions </tmp/regions.txt
*/
import com.google.common.base.Charsets;
import com.google.common.collect.Lists;
@elazarl
elazarl / LongAdderBench.java
Last active August 29, 2015 14:00
Benchmark showing that sometime
/*
Welcome to the game show "lies and benchmarks".
We will present the participants a required result, and they would build a
benchmark that reaches the predetermined conclusions.
When done professionally, this activity is called "benchmarketing".
Our task for today is to prove LongAdder is slower than AtomicLong. Our
observation is, LongAdder prevents write contention to a single a atomic
@elazarl
elazarl / LongAdderBenchResult.md
Last active August 29, 2015 14:01
Results for LongAdderBench.java

By @cmpxchg16 1%-100% writes samples for LongAdderBench.java:

for  1% writes=16,000 (16 threads * 100,000 ops each=1,600,000):
  LongAdder  1.209ms
  AtomicLong 0.281ms
AtomicLong faster than LongAdder by 76.77%

for  2% writes=32,000 (16 threads * 100,000 ops each=1,600,000):
  LongAdder  2.019ms

AtomicLong 0.485ms

@elazarl
elazarl / parse_perf_events.c
Created March 27, 2015 11:29
A small C code sample, demonstrating the complexity of parsing the perf_event_open(2) buffer format
int print_records_read(void *_this, void *buf, int size) {
int nread = 0;
struct perf_record_mmap *mmap_r;
struct perf_record_lost *lost_r;
struct perf_record_comm *comm_r;
struct perf_record_exit *exit_r;
struct perf_record_throttle *throttle_r;
struct perf_record_fork *fork_r;
struct perf_record_read *read_r;
struct print_records *pr = _this;
@elazarl
elazarl / redcon.go
Created June 6, 2011 08:45
redirect network stream
package main
import (
"log"
"io"
"os"
"crypto/tls"
"strings"
"net"
"flag"
#!/usr/bin/env python
# coroutine equivalent of http://play.golang.org/p/lKBbS56f-n
def generate(s,even,odd):
for i,c in enumerate(s):
if i%2 == 0:
even.send(c)
else:
odd.send(c)
@elazarl
elazarl / Publishing.java
Created December 20, 2011 18:02
Trying to recreate the "Holder" class publishing error from JCIP.
/**
* Trying to recreate the "Holder" class publishing error from JCIP.
* See http://stackoverflow.com/a/6812936/55094 for the description of the problem.
*/
public class Publishing {
static class Holder {
int h = 0;
int dummy = 0;
public Holder(int h) {
for (long i=0;i<1000*1000;i++) {
@elazarl
elazarl / trie.c
Created January 21, 2012 23:21
A simple and minimal C implementation of trie
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
// weird exercise from a friend
// need to Implement this struct
typedef struct attrib_t {
struct attrib_t* bytes[256];
const char *data;
@elazarl
elazarl / BencodeWriter.java
Created February 1, 2012 17:00
A simple bencode encoder that can handle streaming data
class BencodeWriter {
static class SizedInputStream {
public InputStream stream;
public int size;
public SizedInputStream(InputStream _stream,int _size) {stream = _stream;size = _size;}
}
public static class BencodeProducer {
private OutputStream out;
public BencodeProducer(OutputStream o) {out = o;}