linux常用命令及一些静态动态库相关知识

1 查找然后grep,最后在复制到特定目录

find . -depth -name *.java | xargs grep -i lijiangtao | awk -F ":" '{print $1}' | xargs -i cp {} .  # 注意,awk -F制定分割符'

2 查找当前系统所打开的所有线程数量

pstree -p | wc -l

3 查看文件夹的总大小

**du -s -h [path] : **

-s 查看总大小

-h 以人类可读的方式显示,以m,g为单位

4 top

按照顺序g2,切换显示模式,例如g2模式可以显示ppid

按c可以显示完整的命令

按P按照cpu来排名

按e将内存单位切换为m

top -H -p [pid]:以线程模式显示信息

5 sed

删除文件中包含某些字符的特定行

sed -i '/0,0,0,0/d' jmx_trace.csv

在第12行后面追加hello.txt文件里面的内容

sed -i '12 r hello.txt' file

在查找到的helloworld前面添加insert content

sed -i '/helloworld/i\inesrt content' file

在查找的hellworld后面添加insert content

sed -i '/helloworld/a\insert content' file

6 统计各个文件夹占用的空间大小

sh du -s -h /* | sort -nr

查看当前目录下所有文件/文件夹所占的空间大小:

sh du -h --max-depth=1 .

7 查看二进制文件符号的相关命令

ldd file : 查看当前二进制文件所依赖的所有动态库

nm *.a/*.so:查看当前动态/静态文件里面的所有符号
其中-D选项指定只查看动态符号表

ldconfig -p : 查看当前ld命令可以链接的所有路径

readelf: 可以查看当前so或者a文件里面的信息,包括rpath等

8 perf一些使用技巧

监控进程/线程耗费性能情况,可以监控的事件有很多:cpu,cache-miss,branch预测等,使用perf list可以查看所有的可监控项

perf record -e cpu-clock -t 4882 :捕捉cpu特定线程的cpu使用率

perf report:显示perf record的结果

9 pidstat

用来监控进程后所有线程的cpu占用率(结合java的jstack,通过nid可以查找出对应的线程)

10 dstat

一个用来监控系统资源利用率的工具stat,常用参数dstat -tlcdnm比普通的top更好用

11 taskset

taskset -apc: 查看某个进程下面所有线程绑定的cpu核

14 关于virtual memory和swap

Virtual memory is a layer of abstraction provided to each process. The computer has, say, 2GB of physical RAM, addressed from 0 to 2G. A process might see an address space of 4GB, which it has entirely to itself. The mapping from virtual addresses to physical addresses is handled by a memory management unit, which is managed by the operating system. Typically this is done in 4KB "pages".

This gives several features:

A process can not see memory in other processes (unless the OS wants it to!)

Memory at a given virtual address may not be located at the same physical address

Memory at a virtual address can be "paged out" to disk, and then "paged in" when it is accessed again.

15 将编译输出文件合并为一个静态库

ar -rv libgtest.a gtest-all.o gtest_main.o

16 LIBRARY_PATH和LD_LIBRARY_PATH的区别

LIBRARY_PATH is used by gcc before compilation to search directories containing static and shared libraries that need to be linked to your program.

LD_LIBRARY_PATH is used by your program to search directories containing shared libraries after it has been successfully compiled and linked.

rpath: gcc编译的选项,把需要寻找的库文件的所在路径编译在elf中,这样子就多了一条寻找so库或者a库的路径

17 ldd **.so

可以尝试解析该so中的所有符号,如果未定义符号无法找到,则在运行的时候才会尝试自己去找,如果找到了,那么说明是当前操作系统的相关路径帮助其找到了so

5 ld在链接静态库的时候,一些没有用到的符号会丢失,可以用--whole-archive解决,详细情况如下

#force all symbols
gcc -fPIC -shared -Wl,-soname,libb.so -o libb.so b.o -Wl,--whole-archive liba.a -Wl,--no-whole-archive
#后面--no-whole-archive表示后面的库不需要在这样子了,否则会出bug #force just a specific symbol
gcc -fPIC -shared -Wl,-soname,libb.so -o libb.so b.o -u foobarize liba.a

Why you need it?

A static library is a simple collection of object files. The one major difference from a bunch of object files is the following: when an undefined symbol needs to be resolved, the library is searched and only the object file that actually defines the symbol is linked. No undefined symbols? Nothing is searched, nothing is linked. To override this default behaviour, the GNU linker implements --whole-archive. Most linkers implement -u to force a particular symbol to be treated as undefinmed.

上一篇:django过滤器,日期格式化


下一篇:public void onItemClick(AdapterView arg0, View view, int position,long arg3)详解【整理自网络】