2011年2月24日星期四

likely() & unlikely() in Linux kernel

 在kernel中的定义:
#define likely(x)       __builtin_expect((x),1)
#define unlikely(x)     __builtin_expect((x),0)


The __builtin_expect is a method that gcc (versions >= 2.96) offer for programmers to indicate branch prediction information to the compiler. The return value of __builtin_expect is the first argument (which could only be an integer) passed to it.


likely和unlikely是用来编译优化的, 只和编译器有关,其实都可以没有。我们知道很多cpu里面有告诉缓存,且有预读机制,likely和unlikely就是增加执行判断语句时的命中率。


   如果是if(lilely(a)),说明a条件发生的可能性大,那么a为真的语句在编译成二进制的时候就应该紧跟在前面程序的后面,这样就会被cache预读取进去,增加程序执行速度。 unlikely则是正好相反。


Ref: http://kerneltrap.org/node/4705
http://www.linuxidc.com/Linux/2009-12/23496.htm

2011年2月23日星期三

在Ubuntu 10.10 中安装 java jdk

由于在ubuntu maverick 官方源中已经放弃了sun-java 的包, 改为openjdk.

要是不想用手动, 还想从更新源里装 sun-java5-jdk or sun-java6-jdk 的话,
可以加入之前版本的更新源, 比如 jaunty(9.04):

sudo add-apt-repository "deb http://mirrors.163.com/ubuntu jaunty multiverse"
sudo add-apt-repository "deb http://mirrors.163.com/ubuntu jaunty-updates multiverse"

我这里用的是163的源, 可以根据自己的情况选择最快的更新源.
然后:
sudo apt-get update
sudo apt-get install sun-java6-jdk
就能顺利装上.

装好之后,可以运行
java -version                             -- 显示当前正在使用的版本
sudo update-java-alternatives -l          -- 查看当前可用的版本.
sudo update-java-alternatives -s java-version            -- 选择要使用的版本, 比如: sudo update-java-alternatives -s java-6-sun


PS: 还有第二种方法, 就是直接加入Canonical 10.10的第三方更新源, 不过这个在国内慢的蛋疼,只有可怜的几K/s 的速度  -.-
sudo add-apt-repository "deb http://archive.canonical.com/ maverick partner"

2011年2月22日星期二

Ubuntu 重装时备份软件列表

如果修改过sources.list, 先备份/etc/apt 目录.

* 备份现有的软件列表:
sudo dpkg --get-selections > pkg_backup.bak

* 重装后导入软件列表:
sudo dpkg --set-selections < pkg_backup.bak
sudo apt-get -y update
sudo apt-get dselect-upgrade

之后就大功告成 ^_^

[转载] touchpad-indicator:插入鼠标时自动关闭触摸板

原文链接:http://wowubuntu.com/touchpad-indicator-2.html

我们之前介绍过Touchpad-indicator,可以通过它来快速关闭和打开触摸板,现在Touchpad新增功能:在你插入鼠标时自动关闭触摸板
安装方法:
sudo add-apt-repository ppa:atareao/atareao
sudo apt-get update
sudo apt-get install touchpad-indicator
注:此ppa是开发者目前的官方ppa,里边包含了Picapy,My Weather Indicator等其他程序
开启上述功能的步骤:
单击首选项-配置的,当出现下图时

拔掉鼠标(如果现在有鼠标链接电脑的话),单击ok,然后出现下一个窗口

然后链接上鼠标即可(当然,Disable touchpad when mouse plugged必须勾选