CPU Cache 如何影响程序性能

CPU Cache 是CPU 中用于减少平均访问内存时间的高速存储器件。较内存使用的DRAM ,cache 使用的SRAM 速度更快、价格也更高,所以cache 容量一般较小。与cache 相关的概念有:cache line,associativity(相连性),L1 cache,L2 cache ,cache 命中,cache 失效等。

cache 按层次分为L1、L2 甚至L3级cache,速度依次递减,容量依次增大。CPU 直接能够读取的只有L1 cache。在大多数处理器包括Intel CPU 中,cache按照缓存的指令或者数据分为数据缓存和指令缓存(如L1d 和L1i) 。在多核的CPU 中,每个核心具有独立的L1 级cache,往往公用L2 级cache。公用cache 可能存在多线程缓存污染等问题。

cache 和内存传输的最小单位是cache line,一般大小为64 bytes。cache 每次从内存读取或者写入cache line 大小的数据,而不是我们在程序中定义的数据结构的大小。cache lines 之间的替换常用的是LRU 算法。如果CPU 读取的数据在cache 中,则cache 命中(cache hit),否则cache 失效(cache miss)。每一级的cache 失效将到下一级cache 中寻找数据,直至内存。cache miss 是有开销的,小于访存时间,大于cache hit 时间。每次cache 失效,CPU 就会处于停滞(stall)状态,直至缓存读取到所需要的数据。

因为cache 容量远小于内存(有种说法是1:1000),内存和cache 之间存在映射关系(associativity)。按照映射关系不同,分为:全相连映射(fully associative),多路组相连映射(N-way set associative)和直接映射(directly mapped)。全相连映射缓存和内存是全映射关系,内存中任何一块数据都可以没有限制地放的任何一个cache line 中,缓存利用率高,但设计复杂未被采用;直接映射将一块内存地址映射到一个cache line(the number of cache line = memory address%number of cache lines,其中内存地址以cache line 大小为单位),直接映射设计起来简单,但利用率低;多路组相连映射缓存更为常见,是设计复杂度和性能的权衡。它将一块内存映射到不同路上的cache lines 中,如果是8-路组相连,则一块内存映射到8-路上的cache lines 上(the number of cache line = memory address%(number of cache lines/number of ways)。不同路上的负责缓存相同内存的缓存构成一个集合set,不同set 缓存不同内存地址的数据。number of set = cache size/(ways of associativity * size of cache line)。

继续阅读

如何定制自己的color scheme

在vim 配置文件vimrc 中通过colorscheme 命令可以选择自己喜欢的配色方案,在vim 下输入

:colorscheme torte

既可以选择系统自带torte 配色方案,当然你也可以在vim.org 下载自己喜欢的配色方案,在debian 中用户的配色方案放置在/usr/share/vim/vim72/colors/ 目录下。每个配色方案都是一个单独的vim 文件,比如很多vim 用户喜欢用的desert.vim ,但我觉得这样的配色太花哨了,torte 更适合我。所以如果想定制自己的vim 颜色方案可以看看下面的内容。

继续阅读

Debian 安装

之前在vmware 上安装的Debian 系统因为分区空间太小,重启系统的时候无法进入图形节点,startx 也只能打开twm 管理器。用vmware-vdiskmanager 扩容vmdk 提示有问题,就只有重装了,借鉴之前出现的问题有几点建议给在虚拟机上装Debian 的建议:

  1. 不要装Debian 了,改投ubuntu 吧
  2. 给虚拟机磁盘容量大点吧
  3. home root 都不要单独分区了,整一个分区吧
  4. 不要选择“split virtual disk into multiple files”,这样在vmdk 会被切分2GB 一个文件,扩展似乎有问题

废话少说,做好笔记来装机:

下载系统安装包

有两个选择:1. 在官网上http://www.debian.org/ 。2.在163 镜像上下载http://mirrors.163.com/ 

可能你会在选择CD image 的遇到是 amd64、ia64、i386 的选择,做个说明:

amd64 和ia64 都是64 位系统,ia64 较新需要主机支持安腾架构,一般下amd64 即可

i386 针对的是比较老的一些32 位机器

VMware 安装Debian

VMware 上的new virtual machine 等balabala 的就不说了,中间忽略的过程你可以认为选择默认

  1. 【install】、【graphic install】 随你选,还是图形的好看点吧
  2. 【select a language 】这是安装过程中的语言,母语没得说
  3. 【地区】“中国”,【键盘】“美国英语”
  4. 【配置网络】主机名:看着办吧!别和现有的网络重名了,建议和虚拟机名相同
  5. 【设置用户和密码】你会先输入root 的密码,这也就是你之后sudo 要输入的密码,接着建一个自己喜欢的用户名吧!
  6. 【磁盘分区】如果你知道自己干什么,并且你的磁盘够大,那么选择“手动”吧,图简单“使用整个磁盘–配置LVM”
  7. 【磁盘分区】“将所有文件放在一个分区中”然后“继续”…
  8. 【磁盘分区】“如何使用此分区”中选一个你喜欢的,选择ext4 “继续” “分区设定结束”“分区设定结束并将修改写入磁盘”“是”
  9. 【配置软件包管理】“中国”
  10. 【配置软件包管理器】三选一 mirrors.geekbone.org、www.anheng.com.cn  、cdn.debian.net 建议选择第三个。HTTP 代理一般也不用吧~
  11. 【配置软件包管理器】然后就是漫长的下载安装过程,一刻钟左右(看网速)
  12. 【软件选择】看着喜欢选
  13. 【正在设定man-db】将GRUB 启动引导器安装到主引导记录(MBR)上吗?(如果不是虚拟机且不止一个OS 请选否)
  14. 【结束安装进程】继续重启咯

安装系统后的工作

1. 配置网络,配置文件 /etc/network/interfaces  /etc/resolv.conf  记得最后要/etc/init.d/networking restart

auto eth0 #开机自动激活
       iface eth0 inte static #静态IP
       address 192.168.0.56 #本机IP
       netmask 255.255.255.0 #子网掩码
       gateway 192.168.0.254 #路由网关      

# iface eth0 inet dhcp 如果是自动获取的

2. 这时候的字体可能比较难看,背景也不是我想要的“星球”系列,桌面右键“更改桌面背景”字体可修改

3. 更新源文件,配置文件/etc/apt/sources.list ,更新为:

deb http://mirrors.163.com/debian squeeze main non-free contrib
       deb http://mirrors.163.com/debian squeeze-proposed-updates main contrib non-free
       deb http://mirrors.163.com/debian-security squeeze/updates main contrib non-free
      deb-src http://mirrors.163.com/debian squeeze main non-free contrib
      deb-src http://mirrors.163.com/debian squeeze-proposed-updates main contrib non-free
      deb-src http://mirrors.163.com/debian-security squeeze/updates main contrib non-free
      deb http://http.us.debian.org/debian squeeze main contrib non-free
      deb http://non-us.debian.org/debian-non-US squeeze/non-US main contrib non-free
      deb http://security.debian.org squeeze/updates main contrib non-free

【可选】还有一个方法更新源信息,通过安装apt-spy ,通过其更新软件包列表:

首先 apt-get update 再安装apt-spy

apt-get install apt-spy

使用apy 获取镜像服务器列表

apt-get update

然后使用apy 镜像服务器速度,更新sources.list

apt-spy -d stable -a Asia

4. 接着apt-get update 接着apt-get upgrade  一下吧

5.安装基本的编辑工具吧!(在虚拟机中只有装了gcc 才能安装vmware-tools)

apt-get install gcc
       apt-get install make
       apt-get install automake

6. 每次输入sudo 都要输入密码是不是很麻烦呢?修改/etc/sudoers 在输入sudo 命令的时候不要输入密码

在最后一行添加:

root    ALL=(ALL)       ALL   #让用户可以使用root 用户的权限

xxxxxx ALL=(ALL)NOPASSWD: ALL     #所有用户都不用输入密码

xxxxxx 是你的用户名,最后chmod u -w /etc/sudpers 去掉写权限

 

 

大功告成,还要什么自己添加、修改去吧!

可视化函数调用

所需工具:

  1. GCC
  2. Graphviz
  3. 中间件pvtrace,下载
  4. addr2line 下载

过程:

  1. 编译/安装 pvtrace(make,make install 即可)
  2. 将 pvtrace 中instrument.c 拷贝到要进行编译的路径
  3. 按如下代码编译测试程序(注意第三行-finstrument-functions 是连起来的):
 1: $ ls
 2: instrument.c    test.c
 3: $ $ gcc -g -finstrument-functions test.c instrument.c -o test
 4: $ ./test
 5: $ ls
 6: instrument.c     test.c
 7: test             trace.txt
 8: $ pvtrace test
 9: $ ls
 10: graph.dot        test           trace.txt
 11: instrument.c     test.c
 12: $ dot -Tjpg graph.dot -o graph.jpg
 13: $ ls
 14: graph.dot        instrument.c   test.c
 15: graph.jpg        test           trace.txt
 16: $

我的源码:

 1: #include <stdlib.h>
 2:
 3: void step1(){
 4: printf(“I am step1 !\n”);
 5: }
 6:
 7: void step(){
 8: printf(“I am last step !\n”);
 9: }
 10:
 11: void step2(){
 12: printf(“I am step2 !\n”);
 13: step();
 14: }
 15:
 16: int main(void){
 17: step1();
 18: step2();
 19: return 1;
 20: }
 21:

 

编译生成的图片:

PS:在makefile 中添加中间件编译参考[2]。

参考:

[1].http://www.ibm.com/developerworks/cn/linux/l-graphvis/

[2].http://blog.csdn.net/seasonpplp/article/details/7399053

[3].http://blog.csdn.net/fisher_jiang/article/details/6828427