Skip to content

Instantly share code, notes, and snippets.

@lvshaco
lvshaco / gist:0f85263e6f5e760fe142
Created August 10, 2015 07:11
等待所有线程创建初始化完成后,主线程才继续执行[memcached]
/*
* Number of worker threads that have finished setting themselves up.
*/
static int init_count = 0;
static pthread_mutex_t init_lock;
static pthread_cond_t init_cond;
----main thread------------------------------------------------------------------------

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  =   3 µs
Send 2K bytes over 1 Gbps network ....... 20,000 ns  =  20 µs
SSD random read ........................ 150,000 ns  = 150 µs

Read 1 MB sequentially from memory ..... 250,000 ns = 250 µs

@lvshaco
lvshaco / pstack
Last active December 22, 2015 10:49
pstack for debain
#! /bin/bash
if [[ $1 -eq 0 ]];then
echo "usage:$0 pid"
exit
fi
TMPFILE="${TMPDIR:=/tmp}/pstack$$"
# Remove TMPFILE on termination
trap 'rm -f "$TMPFILE" >/dev/null 2>&1' 0
@lvshaco
lvshaco / coredump.c
Last active December 20, 2015 09:59
coredump in windows
// additional library DbgHelp.lib
#include <winbase.h>
#include <dbghelp.h>
static void _create_dump_file(const char* dumpfile, EXCEPTION_POINTERS* exception) {
HANDLE hfile = CreateFile(dumpfile,
GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
MINIDUMP_EXCEPTION_INFORMATION dumpinfo;
dumpinfo.ExceptionPointers = exception;
@lvshaco
lvshaco / perror.c
Last active December 19, 2015 12:19
perror in windows
// 在项目设置中General选项设置:Character Set -> Use Multi-Byte Character Set
#include <windows.h>
#include <stdio.h>
int main(int argc, char* argv[]) {
if (argc < 2) {
printf("usage: %s error_id\n", argv[0]);
return -1;
}
DWORD errid = strtoul(argv[1], NULL, 10);