Search This Blog

Wednesday, February 22, 2006

提供机制,而不是策略

据说unix的设计哲学是:提供机制,而不是策略
第一次看到这句话确实是不明白,因为我并不理解“机制”和“策略”的区别
这两天看ldd,里面倒是很详细地解释了
简单点说,机制就是“能干什么”,而策略是“怎么干”
所以可以这样理解:unix给你提供你需要以之来解决问题的各种功能,但不会告诉你怎么做,那是你的事情
于是,针对一些现象,我又开始忍不住要大放一番“厥词”了,虽然我知道说了也是白说
linux和unix初学者们:
我跟你们一样,也是什么都不懂,并且我越学习就越是发现这一事实是有多么明显,但是有一点我明白了的,那就是,linux/unix提供了机制,但是不会是策略。所以:
在你们遇到问题的时候,先想想unix的设计哲学。
然后想想你手头所有的资源,manual,google,bbs,其中manual是系统提供给你的,如果你想“学会”怎么用linux/unix,那就 先好好利用你所拥有的,而不是一味地“请教”别人,别人没有义务去回答那些已经重复了无数次的问题的。接下去,怎么利用这些资源去解决你所遇到的问题,就 是属于“策略”的范畴了,参照我的理解,那是你的事情。
其实中心思想是:发文前请看置底!!

用emacs上bbs

我一直想着要是在bbs上发文时能像emacs一样编辑,那可太爽了,不过既然bbs不支持emacs的编辑功能,那就用emacs上bbs么
其实很简单,M-x ansi-term,运行后emacs会让你选择程序,默认是bash,然后就会出来一个term,其他的就跟在终端下上bbs一样了
不过发呆时间一长就会掉线,所以可以写个函数防止发呆:
(defvar antiidle)
(defun enable-anti-idle ()
(interactive)
(setq antiidle (run-with-timer 0 180 '(lambda ()
(term-send-up)
(term-send-down)))))

这个函数让emacs每三分钟向term发送一个“上”和一个“下”,然后把run-with-timer返回的值赋给antiidle
(defun disable-anti-idle ()
(interactive)
(cancel-timer antiidle))

这个函数把刚才用run-with-timer“注册”的行为注销掉
这样就可以在需要的时候开启或关闭防止发呆功能,当然,设个快捷键会更方便
另外,在连接主机之前在终端下先运行stty rows 24 cols 80效果会更好
连接上主机以后,默认的是term-char-mode,在这个mode下每按一个键就会向终端发送,所以适合于浏览,如果要发文,那么就会出问题,可以 切换到term-line-mode,然后像平时一样编辑,要发送之前再切换回term-char-mode就可以按C-w发文了
term-char-mode绑定到了C-c C-k
term-line-mode绑定到了C-c C-j

Saturday, February 18, 2006

P4也可以cpufreq!

在家试了一下powernow的威力,非常适合我这种24小时不关机的人,能省不少电呢,当然,不开机更省,呵呵
回学校来又开始折腾(我总是倾向于让我的电脑都有同一个环境,这样至少当我分别在家和在学校时,不需要改变我的习惯),于是看到文档里说cpufreq也能用在P4上,那当然要用。
编译内核基本上和athlon64的时候一样,只是cpu换成了P4,对应的cpufreq的模块换成了,具体如下:
[*] CPU Frequency scaling
│ │ [ ] Enable CPUfreq debugging
│ │ < *> CPU frequency translation statistics
│ │ [*] CPU frequency translation statistics details
│ │ Default CPUFreq governor (performance) --->
│ │ --- 'performance' governor
│ │ 'powersave' governor
│ │ 'userspace' governor for userspace frequency scaling
│ │ 'ondemand' cpufreq policy governor
│ │ <> 'conservative' cpufreq governor
│ │ --- CPUFreq processor drivers
│ │ < *> ACPI Processor P-States driver
│ │ <> AMD Mobile K6-2/K6-3 PowerNow!
│ │ <> AMD Mobile Athlon/Duron PowerNow!
│ │ <> AMD Opteron/Athlon64 PowerNow!
│ │ <> Cyrix MediaGX/NatSemi Geode Suspend Modulation
│ │ <> Intel Enhanced SpeedStep
│ │ <> Intel Speedstep on ICH-M chipsets (ioport interface)
│ │ <> Intel SpeedStep on 440BX/ZX/MX chipsets (SMI interface)
│ │ Intel Pentium 4 clock modulation
│ │ <> nVidia nForce2 FSB changing
│ │ <> Transmeta LongRun
│ │ <> VIA Cyrix III Longhaul
│ │ --- shared options
│ │ [ ] /proc/acpi/processor/../performance interface (deprecated)


有了支持cpufreq的内核以后基本上就成功一半了
然后安装三个额外的包:
#apt-get install acpid cpufreqd cpufrequtils
这里插一句,据说powernowd也是支持P4的,不过我没试过,谁有兴趣可以试一下
这三个包装好后就有一个完整的环境了,如果图方便的话,重启一下,不过,重启是window$的德性,还是手动吧
其实本来不用手动启动的,因为包的安装脚本会帮我们启动,不过acpid不能在X环境下启动,会提示资源或设备忙,所以可以先退出X,启动acpid以后再进X,当然,如果不用X的话就不用这么麻烦了
接下去就只剩最后一步了,配置一下/etc/cpufreqd.conf
默认的配置似乎是针对笔记本的,什么ac=on,ac=off的
先简单介绍一下cpufreqd.conf
这个文件由一系列的section组成,分别是gerneral,profile和rule
gerneral部份是总的设置,需要改的是把以下两行的注释去掉
# enable_remote=1
# remote_group=root

不然的话cpufreqd-get和cpufreqd-set就无法工作
然后是profile,这个部份为rule提供一些模板
name是profile的名字,随便你喜欢,可以有空格,不能重复
minfreq是本profile下CPU的最低工作频率,百分数表示
maxfreq以此类推
policy 这个能使用的选项取决于 /sys/devices/system/cpu/cpu0/cpufreq/scaling_available_governors,cat一下就有 了,而它又取决于刚才编译内核时的几个选项,分别是userspace,ondemand,performance和powersave
other plugin entries放到下面一起说
最后是rule,rule比较复杂一点
name是rule的名字,和profile的name差不多
profile是符合该rule时所使用的profile的name
other plugin entries提供匹配一个rule的机制,我只说几个比较有用的,详细的可以man cpufreqd.conf,acpi_temperature plugin用来匹配CPU的温度,当然这也需要内核的支持,可以参考我前面powernow的文章,比如说当CPU温度高于50度时让CPU稍微休息一 下,就这样匹配:acpi_temperature=50-100,cpu plugin用来匹配CPU的使用率,比如可以用cpu_interval=50-100和刚才的acpi_temperature=50-100一起来 匹配CPU过热,programs plugin可以匹配一个程序,这样就可以在看电影或者编译软件时让CPU以最高频率跑起来,这样写:programs=xxx,有不止一个程序的话用逗 号间隔,基本上这些就够用了,如果还是不明白就对照着默认的配置文件,再看看manual,应该很容易就搞定了

这些都完成以后,让cpufreqd重启一下:
#/etc/init.d/cpufreqd restart
可以运行一下cpufreqd-get查看当前的状态,下面是我的结果:
root@huzhou:/home/cxl# cpufreqd-get
socket I'll try to connect: /tmp/cpufreqd-zRGa6x/cpufreqd


Name (#1): On Demand High *
Governor: ondemand
Min freq: 1499962
Max freq: 2399940


Name (#2): On Demand Low
Governor: ondemand
Min freq: 899977
Max freq: 1799955


Name (#3): Performance High
Governor: performance
Min freq: 2399940
Max freq: 2399940


Name (#4): Performance Low
Governor: performance
Min freq: 1799955
Max freq: 1799955


Name (#5): Powersave High
Governor: powersave
Min freq: 1799955
Max freq: 1799955


Name (#6): Powersave Low
Governor: powersave
Min freq: 599985
Max freq: 599985


可以再看一下/etc/proc/cpuinfo
root@huzhou:/home/cxl# cat /proc/cpuinfo
processor : 0
vendor_id : GenuineIntel
cpu family : 15
model : 2
model name : Intel(R) Pentium(R) 4 CPU 2.40GHz
stepping : 7
cpu MHz : 1493.971
cache size : 512 KB
fdiv_bug : no
hlt_bug : no
f00f_bug : no
coma_bug : no
fpu : yes
fpu_exception : yes
cpuid level : 2
wp : yes
flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe cid xtpr
bogomips : 4784.88


可以看到频率已经降下来了
不过我的sensors显示核心电压并没有下降,目前为止我还不知道是怎么回事:)

Thursday, February 09, 2006

温度、风扇、电压和cool'n'quiet(athlon64)

要在系统中得到主板上各处的温度,风扇转速和电压,必须在内核中有相应的支持,还要有lm-sensors,下面简单介绍一下步骤:
编译内核,在device drivers->i2c中选上对应的选项
< *> I2C support
I2C device interface
I2C Algorithms ---> //这里面的全选上
I2C Hardware Bus support ---> //这里面的选上对应硬件的模块
Miscellaneous I2C Chip support --->
[ ] I2C Core debugging messages
[ ] I2C Algorithm debugging messages
[ ] I2C Bus debugging messages
[ ] I2C Chip debugging messages


然后是device drivers->Hardware Monitoring support
这里要先弄清楚自己主板的sensor的芯片,然后选上对应的模块
然后安装lm-sensors,root运行sensors-detect,按照提示一步一步来,如果成功探测到芯片的话,最后按照提示把结果写入 /etc/modules就可以了,然后/etc/init.d/lm-sensors start,重新启动一下gkrellm,就会发现风扇,温度和电压已经可选了,随时监控:)

接着再说说AMD的cool'n'quiet
cool'n'quiet技术可以根据当前CPU的负载来自动调节CPU的频率,电压以及风扇的转速,电费吃人啊,能省则省啦
当然,这个功能也是需要内核的支持的,在Power management options->ACPI (Advanced Configuration and Power Interface) Support里选上Processor
CPU Frequency scaling里
[*] CPU Frequency scaling
[ ] Enable CPUfreq debugging
< *> CPU frequency translation statistics
[ ] CPU frequency translation statistics details
Default CPUFreq governor (performance) --->
--- 'performance' governor
'powersave' governor
'userspace' governor for userspace frequency scaling
'ondemand' cpufreq policy governor
<> Intel Enhanced SpeedStep
<> ACPI Processor P-States driver
--- shared options
[ ] /proc/acpi/processor/../performance interface (deprecated)


还要在bios里把pownow功能打开
完成后加载上必要的模块,先看一下/proc/cpuinfo
root@huzhou:/home/cxl# cat /proc/cpuinfo
processor : 0
vendor_id : AuthenticAMD
cpu family : 15
model : 47
model name : AMD Athlon(tm) 64 Processor 3000+
stepping : 0
cpu MHz : 2025.046
cache size : 512 KB
fpu : yes
fpu_exception : yes
cpuid level : 1
wp : yes
flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 syscall nx mmxext fxsr_opt lm 3dnowext 3dnow pni lahf_lm
bogomips : 4054.34
TLB size : 1024 4K pages
clflush size : 64
cache_alignment : 64
address sizes : 40 bits physical, 48 bits virtual
power management: ts fid vid ttp tm stc


然后root@huzhou:/home/cxl# echo ondemand>/sys/devices/system/cpu/cpu0/cpufreq/scaling_governor
再看看
root@huzhou:/home/cxl# cat /proc/cpuinfo
processor : 0
vendor_id : AuthenticAMD
cpu family : 15
model : 47
model name : AMD Athlon(tm) 64 Processor 3000+
stepping : 0
cpu MHz : 1125.025
cache size : 512 KB
fpu : yes
fpu_exception : yes
cpuid level : 1
wp : yes
flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 syscall nx mmxext fxsr_opt lm 3dnowext 3dnow pni lahf_lm
bogomips : 2252.41
TLB size : 1024 4K pages
clflush size : 64
cache_alignment : 64
address sizes : 40 bits physical, 48 bits virtual
power management: ts fid vid ttp tm stc


可以看到频率已经从2025降到1125了(我超过频,如果没超频的话,是从1800到1000)
如果这时候有CPU密集型的进程运行,再看频率,会发现又是2025了

另外,如果发现加载powernow-k8模块时有如下错误的话
powernow-k8: BIOS error - no PSB or ACPI _PSS objects
可以尝试一下升级bios
enjoy it!

Friday, February 03, 2006

gdb

其实我不会用gdb,一直都不会,但是直到上学期做汇编课程设计的时候,实在是没办法了,不用gdb根本就不可能发现问题了,才硬着头皮开始学着用。然后就发现它真的是一个很优秀的调试器。
当然我是在emacs里用的,不过跟shell下用是没什么区别的,如果不去管emacs提供的那些快捷键的话。
如果启动gdb的时候没有指定要调试的程序,那么在启动后可以用file filename来完成这个工作。
然后呢,run就可以运行要调试的程序。哦,还有,如果是用gcc编译的话,加个-g的选择,可以给gdb提供足够的调试信息。
break可以设置断点,我喜欢break 1这样让程序一启动就暂停
until linenum可以让程序运行一直到指定的行号(或指定的行号以后)
list可以显示出源程序以及行号
p,就是print,可以打印出程序中的变量值,这个比较重要,p后可跟一个/F,其中F是输出的格式,比如x是十六进制,d是十进制,o是八进制等等, 还可以打印整个数组,如果有array[3] 这样的声明,那么可以用p *array@3把array的3个元素打印出来
next可以让程序运行一条指令(调试汇编时是机器指令,高级语言时我想应该是一条语句,而在c语言里声明变量之类的语句是不会被next单独执行一次的)
这些是最基本的命令,用法相当复杂,具体可以查看info gdb
调试器在你对于你的程序运行错误没有头绪的时候是很有用处的