Wednesday, July 20, 2016

Postgres on Btrfs

Postgres performs random writes on big database files, which can cause huge performance impact on files systems featured with COW(Copy On Write) like btrfs and zfs.

A good practice is add "nodatacow" option to database volume in /etc/fstab

Run
mount <mount-point> -o remount
to apply the edited mount options.

Monday, February 2, 2015

Ram disk on OS X

Create a 1.5G ram disk at /Volumes/ramdisk

$ diskutil erasevolume HFS+ "ramdisk" `hdiutil attach -nomount ram://3145728`

http://apple.stackexchange.com/questions/55794/why-do-mac-os-x-ramdisks-appear-to-be-limited-to-550mb-and-how-can-i-change-this

Monday, September 1, 2014

Find the original user of a process invoked by sudo

Let's say you want to find out who kicked off the most memory consuming process using root.

1) open top.
2) sort by mem by pressing >
3) write down the pid
4) sudo vim /proc/<pid>/environ , you will find the the user in: SUDO_USER='\<user\>'

There are some other ways: http://unix.stackexchange.com/questions/7334/using-top-to-see-processes-run-by-a-user-on-behalf-of-sudo

Thursday, August 7, 2014

Join odd and even numbered lines

sed:

sed '$!N;s/\n/,/'


awk:

awk 'NR%2==0 {print p","$0;} NR%2 {p=$0;}'


paste:

paste - -


--------------------------
Seems  I like paste most. To join 3 consecutive lines:

paste - - -

Friday, July 18, 2014

Python Profiler

def do_cprofile(func):
    def profiled_func(*args, **kwargs):
        import cProfile
        profile = cProfile.Profile()
        try:
            profile.enable()
            result = func(*args, **kwargs)
            profile.disable()
            return result
        finally:
            profile.print_stats(sort=1)
    return profiled_func

try:
    from line_profiler import LineProfiler

    def do_profile(follow=[]):
        def inner(func):
            def profiled_func(*args, **kwargs):
                try:
                    profiler = LineProfiler()
                    profiler.add_function(func)
                    for f in follow:
                        profiler.add_function(f)
                    profiler.enable_by_count()
                    return func(*args, **kwargs)
                finally:
                    profiler.print_stats()
            return profiled_func
        return inner

except ImportError:
    def do_profile(follow=[]):
        "Helpful if you accidentally leave in production!"
        def inner(func):
            def nothing(*args, **kwargs):
                return func(*args, **kwargs)
            return nothing
        return inner


@do_profile(follow=[fun2])
def fun1():
    fun2()
    pass


@do_cprofile()
def fun2()
    pass

Monday, July 7, 2014

Java cpu profiling with hprof

Set JAVA_OPTS:

-Xrunhprof:cpu=samples,interval=10,depth=8
 
 
There will be a  file called "java.hprof.txt" in your working directory 

Thursday, September 5, 2013

No syntax highlighting in vimdiff

Vim Syntax highlight often mixed with diff color, which makes text unreadable.

Add following line in ~/.vimrc to turn off syntax off in vimdiff

if &diff | syntax off | endif