Giter Site home page Giter Site logo

blog-post-issues's Issues

perf

Linux性能诊断:perf使用指南

$ gcc -g -ggdb -fno-omit-frame-pointer <target>
$ perf record --call-graph dwarf -g <target>
$ perf script > out.perf
$ ./stackcollapse-perf.pl out.perf > o.folded
$ ./flamegraph.pl o.folded >| o.svg --width 50000

call graph

https://stackoverflow.com/a/5373814

static void D() { }
static void Y() { D(); }
static void X() { Y(); }
static void C() { D(); X(); }
static void B() { C(); }
static void S() { D(); }
static void P() { S(); }
static void O() { P(); }
static void N() { O(); }
static void M() { N(); }
static void G() { M(); }
static void A() { B(); G(); }

int main() {
  A();
}
$ vim t.cpp
$ clang++ -S -emit-llvm t.cpp -o - | opt -analyze -dot-callgraph
$ cat callgraph.dot | 
   c++filt | 
   sed 's,>,\\>,g; s,-\\>,->,g; s,<,\\<,g' | 
   awk '/external node/{id=$1} $1 != id' | 
   dot -Tpng -ocallgraph.png
$ eog callgraph.png

callgraph

ctypes

C++

  • adder.cc
#include "adder.h"
int add(int a, int b)
{
    return a + b;
}
  • adder.h
extern "C" {
int add(int a, int b);
}

compile command

$ g++ -o libadder.so -shared -fPIC adder.cc

Python

  • caller.py
from ctypes import cdll

adder_lib = cdll.LoadLibrary("libadder.so")
add = adder_lib.add
print(add(1, 3))

free cache memory

$ sudo sh -c 'echo 1 > /proc/sys/vm/drop_caches'
$ sudo sh -c 'echo 2 > /proc/sys/vm/drop_caches'
$ sudo sh -c 'echo 3 > /proc/sys/vm/drop_caches'

W: GPG error

W: GPG error: file:/var/cuda-repo-10-0-local-10.0.130-410.48  Release: The following signatures couldn't be verified because the public key is not available: NO_PUBKEY F60F4B3D7FA2AF80

Ref: NVIDIA/nvidia-docker#571 (comment)
screenshot from 2019-01-03 21-55-40

$ sudo vim /etc/apt/sources.list.d/cuda.list

screenshot from 2019-01-03 21-55-57

deb https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1604/x86_64 /

screenshot from 2019-01-03 22-05-47

$ wget -qO - https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1604/x86_64/7fa2af80.pub | sudo apt-key add -

screenshot from 2019-01-03 22-00-03

emacs

smooth-scrolling

(require 'smooth-scrolling)
(smooth-scrolling-mode 1)
(setq smooth-scroll-margin 5)

inputrc

"\e[A": history-search-backward
"\e[B": history-search-forward

vim syntax

http://vimcdoc.sourceforge.net/doc/syntax.html#:syn-arguments
https://github.com/vim/vim/tree/master/runtime/syntax

after/syntax/lcc.vim

syn match lccIntType            "int[0-9]*\(\s\|\[\)"me=e-1
syn match lccFloatType          "float[0-9]*\(\s\|\[\)"me=e-1

syn keyword lccSecure           public secure
syn keyword lccPackage          package

syn keyword lccConditional      if else
syn keyword lccRepeat           while for bwhile bfor

syn match lccUserFunction       "\<\h\w*\>\(\s\|\n\)*("me=e-1

hi def link lccIntType          Type
hi def link lccFloatType        Type
hi def link lccSecure           StorageClass
hi def link lccPackage          Include
hi def link lccRepeat           Repeat
hi def link lccConditional      Conditional
hi def link lccUserFunction     Function

oh-my-bash

install

sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmybash/oh-my-bash/master/tools/install.sh)"

theme

$ mcd .oh-my-bash/themes/ouo
$ vim ouo.theme.sh
# ouo theme
# Author: Huang Po Hsuan
# inspired by theme "pure" "cupcake"

# scm theming
SCM_THEME_PROMPT_PREFIX=""
SCM_THEME_PROMPT_SUFFIX=""

SCM_THEME_PROMPT_DIRTY=" ${bold_red}✗${normal}"
SCM_THEME_PROMPT_CLEAN=" ${green}✓${normal}"
SCM_GIT_CHAR="${green}±${normal}"
SCM_GIT_BEHIND_CHAR="${bold_blue}↓${normal}"
SCM_GIT_AHEAD_CHAR="${bold_blue}↑${normal}"
SCM_GIT_UNTRACKED_CHAR="${bold_blue}⌀${normal}"
SCM_GIT_UNSTAGED_CHAR="${bold_yellow}•${normal}"
SCM_GIT_STAGED_CHAR="${bold_green}+${normal}"

CLOCK_THEME_PROMPT_PREFIX=''
CLOCK_THEME_PROMPT_SUFFIX=' '
THEME_SHOW_CLOCK=true
THEME_CLOCK_COLOR=${THEME_CLOCK_COLOR:-"$green"}
THEME_CLOCK_FORMAT=${THEME_CLOCK_FORMAT:-"[%H:%M]"}

scm_prompt() {
    CHAR=$(scm_char)
    if [ $CHAR = $SCM_NONE_CHAR ]
        then
            return
        else
            echo "[$(scm_prompt_info)]"
    fi
}

rightprompt() {
    printf "%*s" $COLUMNS "$1"
}

ouo_prompt() {
    # ps_host="${bold_blue}\h${normal}";
    # ps_user="${green}\u${normal}";
    # ps_root="${red}\u${red}";
    # ouo="${green}ouo${normal}";
    if [ $? -ne 0 ]
        then
            ps_user_mark="${red}$ ${normal}";
        else
            ps_user_mark="${green}$ ${normal}";
    fi
    ps_root_mark="${green}# ${normal}"
    ps_path="${yellow}\w${normal}"

    # make it work
    # PS1="\n$(clock_prompt)$ps_path $(scm_prompt)"
    PS1L="$ps_path $(scm_prompt)"
    PS1R="$(clock_prompt)"
    # Ref: https://wiki.archlinux.org/index.php/Bash/Prompt_customization#Right-justified_text
    PS1=$(printf "\n$(tput sc; rightprompt $PS1R; tput rc)%s" "$PS1L")

    # case "$HAS_GOTO" in
    #     1) PS1="${PS1}\n${GOTO_PROMPT}"
    #         printf "OuO"
    #         ;;
    # esac

    case $(id -u) in
        # 0) PS1="$ps_root@$ps_host$(scm_prompt):$ps_path\n$ps_root_mark"
        0) PS1="${PS1}\n$ps_root_mark"
            ;;
        # *) PS1="$ps_user@$ps_host$(scm_prompt):$ps_path\n$ps_user_mark"
        *) PS1="${PS1}\n$ps_user_mark"
            ;;
    esac
}

safe_append_prompt_command ouo_prompt
$ vim ~/.bashrc
OSH_THEME="ouo"

bashmarks issue

bash: /tmp/bashmarks.gnc3m4: cannot overwrite existing file

ohmybash/oh-my-bash#50

$ vim ~/.oh-my-bash/plugins/bashmarks/bashmarks.plugin.sh
     # purge line
-    sed "/$2/d" "$1" > "$t"
+    sed "/$2/d" "$1" >| "$t"
     /bin/mv "$t" "$1"

GCC 函式追蹤功能

http://blog.linux.org.tw/~jserv/archives/001870.html

use dladdr to show function name
https://unix.stackexchange.com/a/386903

print indent
https://www.dennisthink.com/?p=63

Use to trace C++ function

Makefile:

all:
	g++ -c tracer.c -g
	g++ tracer.o main.cpp -fPIC -ldl -finstrument-functions -export-dynamic -g
clean:
	rm *.out *.o

tracer.c:

#ifdef __cplusplus
extern "C"
{

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <dlfcn.h>

    void __cyg_profile_func_enter (void *, void *)
        __attribute__((no_instrument_function));
    void __cyg_profile_func_exit (void *, void *)
        __attribute__((no_instrument_function));

    // extern int trace_on;
    // int trace_on = 1;
}

#endif

#define TRACE_FD 3

static FILE *fp_trace=NULL;
static int nDepth = 0;

void __attribute__((constructor)) traceBegin(void) {
    fp_trace = fopen("func_trace.out", "w");
}

void __attribute__((destructor)) traceEnd(void) {
    if (fp_trace != NULL) {
        fclose(fp_trace);
        fp_trace = NULL;
    }
}

void __cyg_profile_func_enter (void *func,  void *caller)
{
    // if (!trace_on) {
    //     return;
    // }
    nDepth++;
    for(int i = 0 ; i < nDepth ; i++) {
        if(fp_trace) {
            fprintf(fp_trace,"\t");
        }
    }

    Dl_info info;
    if (fp_trace == NULL) {
        fp_trace = fdopen(TRACE_FD, "w");
        if (fp_trace == NULL) abort();
        setbuf(fp_trace, NULL);
    }
    if (dladdr(func, &info)) {
        // fprintf (fp_trace, "in %p [%s] %s\n",
        //         func,
        //         info.dli_fname ? info.dli_fname : "?",
        //         info.dli_sname ? info.dli_sname : "?");
        fprintf (fp_trace, "in [%s] %s\n",
                info.dli_fname ? info.dli_fname : "?",
                info.dli_sname ? info.dli_sname : "?");
    }
}

void __cyg_profile_func_exit(void *func, void *caller)
{
    // if (!trace_on) {
    //     return;
    // }
    for(int i = 0 ; i < nDepth ; i++) {
        if(fp_trace) {
            fprintf(fp_trace,"\t");
        }
    }
    nDepth--;

    Dl_info info;
    if (fp_trace == NULL) {
        fp_trace = fdopen(TRACE_FD, "w");
        if (fp_trace == NULL) abort();
        setbuf(fp_trace, NULL);
    }
    if (dladdr(func, &info)) {
        // fprintf (fp_trace, "out %p [%s] %s\n",
        //         func,
        //         info.dli_fname ? info.dli_fname : "?",
        //         info.dli_sname ? info.dli_sname : "?");
        fprintf (fp_trace, "out [%s] %s\n",
                info.dli_fname ? info.dli_fname : "?",
                info.dli_sname ? info.dli_sname : "?");
    }
}

main.cpp:

#include <iostream>
#include <cstdio>
#include <cstdlib>
extern int traceon;
// int trace_on = 1;

using namespace std;

int bar(float f) {
    printf("%f\n", f);
}

void foo(int a) {
    printf("%d\n", a);
    bar(4.4);
}

int main()
{
    puts("c++");
    foo (3);
    puts("gg");
    return 0;
}

func_trace.out:

	in [./a.out] ?
		in [./a.out] ?
		out [./a.out] ?
	out [./a.out] ?
	in [./a.out] main
		in [./a.out] _Z3fooi
			in [./a.out] _Z3barf
			out [./a.out] _Z3barf
		out [./a.out] _Z3fooi
	out [./a.out] main

universal-ctags

https://github.com/universal-ctags/ctags

$ sudo apt install autoconf automake libtool
$ sudo apt install pkg-config
$ git clone https://github.com/universal-ctags/ctags.git
$ cd ctags
$ ./autogen.sh
$ ./configure --program-prefix=uni
$ make
$ sudo make install
Plug 'majutsushi/tagbar', {'do': 'unictags -R -h \".h .c .hpp .cpp .java .python .y .l\"'}
let g:tagbar_ctags_bin = '/usr/local/bin/unictags'

fzf

# ref: https://github.com/junegunn/fzf/issues/542#issuecomment-210463399
$ vim $(fzf)

Install oracle jdk

Ref: https://askubuntu.com/a/55960

Steps

  1. download from official website: https://www.oracle.com/technetwork/java/javase/downloads/index.html
  2. use following commands, if the version is 1.8 then change 7 to 8 below.
tar -xvf jdk-7*
sudo mkdir /usr/lib/jvm
sudo mv ./jdk1.7* /usr/lib/jvm/jdk1.7.0
sudo update-alternatives --install "/usr/bin/java" "java" "/usr/lib/jvm/jdk1.7.0/bin/java" 1
sudo update-alternatives --install "/usr/bin/javac" "javac" "/usr/lib/jvm/jdk1.7.0/bin/javac" 1
sudo update-alternatives --install "/usr/bin/javaws" "javaws" "/usr/lib/jvm/jdk1.7.0/bin/javaws" 1
sudo chmod a+x /usr/bin/java
sudo chmod a+x /usr/bin/javac
sudo chmod a+x /usr/bin/javaws

ubuntu ime

$ sudo apt install fcitx-bin fcitx-chewing
$ im-config

git rebase fix

Scenario:
Find a bug in dev-branch branch.
Create a fix-branch from master, fix it and create PR.
Merge the fix to dev-branch after merging PR to upstream/master.

$ git add .
$ git commit -m "STASH"
$ git checkout master

$ git checkout -b fix-branch
# fix code
$ git add
$ git commit
$ git push origin fix-branch
# Create PR to upstream repo's master
# Wait the merge by reviewer

$ git checkout master
$ git fetch upstream
$ git merge upstream/master
$ git checkout dev-branch
$ git rebase master
$ git push -f origin/dev-branch

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.