Skip to content

Instantly share code, notes, and snippets.

@eddyxu
eddyxu / backup_source.sh
Created March 28, 2010 05:15
backup git repos
#!/bin/sh
TIMESTAMP=`date +"%F"`
SRC_PATH="/base/src/path"
TAR_PATH="/target/path"
if [ ! -d $TAR_PATH ]; then
mkdir -p $TAR_PATH
fi
@eddyxu
eddyxu / trace_iterator.py
Created April 22, 2010 16:30
Trace Iterator
class Trace:
"""A trace iterator
For each run of iteration, this object returns one trace entry
"""
def __init__(self, filepath):
self.filepath = filepath
self.fd = open(filepath)
def __enter__(self):
return self
@eddyxu
eddyxu / getppid_for_process.c
Created September 21, 2010 02:50
Get parent process id for specific process
pid_t getppid_for_process(pid_t pid) {
#if defined(__APPLE__)
struct kinfo_proc info;
size_t length = sizeof(struct kinfo_proc);
int mib[4] = { CTL_KERN, KERN_PROC, KERN_PROC_PID, pid };
if (sysctl(mib, 4, &info, &length, NULL, 0) < 0)
return -1;
if (length == 0)
return -1;
return info.kp_eproc.e_ppid;
@eddyxu
eddyxu / set_cpus.py
Created November 1, 2011 17:54
Manually set CPU cores online or offline
#!/usr/bin/env python
#
# Lei Xu <eddyxu@gmail.com>
"""Manually set CPU cores online or offline
"""
import glob
import optparse
import os
@eddyxu
eddyxu / latency.txt
Created June 1, 2012 03:52 — forked from jboner/latency.txt
Latency numbers every programmer should know
L1 cache reference 0.5 ns
Branch mispredict 5 ns
L2 cache reference 7 ns
Mutex lock/unlock 25 ns
Main memory reference 100 ns
Compress 1K bytes with Zippy 3,000 ns
Send 2K bytes over 1 Gbps network 20,000 ns
Read 1 MB sequentially from memory 250,000 ns
Round trip within same datacenter 500,000 ns
Disk seek 10,000,000 ns
@eddyxu
eddyxu / benchmark_decorator.py
Created July 1, 2012 02:46
Python benchmark decorator
class benchmark(object):
"""Run a function as benchmark for several times.
Usage:
>>> @benchmark(times=1) # times is a optional parameter.
>>> def awesome_benchmark(arg1, arg2):
>>> # do awesome benchmarks.
"""
def __init__(self, **kwargs):
self.times = kwargs.get('times', 1)
@eddyxu
eddyxu / gist:3459054
Created August 25, 2012 02:32
Git pre-commit script
#!/bin/sh
#
# Git Precommit Script for Research project.
# Lei Xu <eddyxu@gmail.com>
# Enforce C++ Style Checking (Google C++ Style).
# Note: you must have cpplint.py installed in your $PATH
CPPFILES=`git diff --cached --name-only | grep -E '^.+\.(h|hpp|cpp|cc)$'`
if [ ! -z "$CPPFILES" ]; then
echo "$CPPFILES" | xargs -n 128 cpplint.py
@eddyxu
eddyxu / fibonacci.py
Created November 10, 2012 21:37
Fibonacci sequence in python
f1 = 1
f2 = 1
while True:
f2, f1 = f1 + f2, f2
@eddyxu
eddyxu / problem119.py
Last active December 10, 2015 08:58
Eular Project Problem 119
#!/usr/bin/env python
#
# Author: Lei Xu <eddyxu@gmail.com>
"""Eular Project Problem 119
http://projecteuler.net/problem=119
"""
import heapq
@eddyxu
eddyxu / ubuntu-sync-conf.md
Last active December 11, 2015 01:49
Sync ubuntu configurations to a node
  1. Copy /etc/passwd /etc/group /etc/gshadow /etc/shadow /etc/hosts from head to the newly installed node.
  2. Sets /etc/network/interfaces with static IP address
  3. apt-get install nfs-common
  4. Sets /etc/fstab to mount home to the head node.
  5. Reboot.