Thinking beyond source code

Recording my throughs


  • 首页

  • 关于

  • 归档

  • 标签

How can we avoid delete/create inconsistency?

发表于 2009-11-29 | 分类于 未分类

This is a file system note: @MIT 6.824 2006 Lecture 6

Think this satiation,

unlink("f1");
create("f2");
Create happens to re-use the i-node freed by the unlink.
suppose only create write goes to disk, but none of the unlink's writes.

Crash.

After re-start, what does recovery see?

The file system looks correct! Nothing to fix!
But file f1 actually has file f2's contents!

Serious *undetected* inconsisency.

This is *not* a state the file system counld have been in if the crash had occured slightly earlier or later. And fsck did not notify the user there was an unfixable problem!

How can we avoid this delete/create inconsistency?

Observation: We only care about what's visible in the file system tree.

Goal: on-disk directory entry must always point to correct on-disk i-node.

Unlink Rule: remove dirent *on disk* before freeing i-node.

Create Rule: initialize new i-node *on disk* before creating directory entry.

In general, directory entry writes should be commit points.
Crash just before leves us with unused allocated i-node.
Crash just after is fine.

感冒了,翻墙了

发表于 2009-11-16 | 分类于 未分类

冬天气温降的很快,就连以往冬天不那么冷的广东,这次都很冷很冷了。我都感冒了, 不过不是猪流感。

还有一件事就是使用ssh tunnel翻墙了,很爽阿。

赞一个OpenSSH。

Find, 清理你的硬盘

发表于 2009-09-11 | 分类于 未分类

最近因为几份庞大的代码, 动则几个G的空间就没有了。 在我的Linux上分区实在不够,但是又不舍得删一些720P的电影, 只好想尽办法来清理硬盘上的垃圾。

有了GNU的工具链,这个就非常容易。

cd /
find -size +100M -print

有了这条就可以找到大于100M的文件,于是我找到一堆不需要的压缩包。 删之。 空间剧增。

Ssh without typing passwd

发表于 2009-05-23 | 分类于 未分类

Blogspot被墙了,只好发到这里了。

在公司的时候,总是有这样的需求:你需要ssh或者scp东西到别人的机器上面,有时候会比较频繁,但是每次都得你的目标机的人过来输一下密码,很麻烦,效率也很低。

在看一篇hadoop的文章的时候看到了一些ssh,和公钥的用法(唉,没办法,比较土,现在才知道),可以避免输密码。

我这里就直接抄下来了,版权都在原作者.

使用三台机器,机器名分别为 homer06, homer07, homer08,均安装 Redhat Enterprise Linux 5.0 (其它 Linux 发行版亦可), 确保各台机器之间网络畅通,机器名与 IP 地址之间解析正确,从任一台机器都可以 ping 通其它机器的机器名。如有机器名的解析问题,可通过设置 /etc/hosts 文件解决,当然更好的解决方法是在你的网络中配置 DNS 服务器。此外,需要在三台机器上创建相同的用户帐号,如 caoyuz, 或直接使用 root 帐号亦可。

在 Hadoop 分布式环境中,Name Node (主节点) 需要通过 SSH 来启动和停止 Data Node (从结点)上的各类进程。我们需要保证环境中的各台机器均可以通过 SSH 登录访问,并且 Name Node 用 SSH 登录 Data Node 时,不需要输入密码,这样 Name Node 才能在后台自如地控制其它结点。可以将各台机器上的 SSH 配置为使用无密码公钥认证方式来实现。

现在流行的各类 Linux 发行版一般都安装了 SSH 协议的开源实现 OpenSSH, 并且已经启动了 SSH 服务, 即这些机器缺省应该就是支持 SSH 登录的。如果你的机器缺省不支持 SSH, 请下载安装 OpenSSH。

以下是配置 SSH 的无密码公钥认证的过程。首先,在 homer06 机器上执行命令,如代码清单 1 所示:

代码清单1



homer06: $ ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/home/caoyuz/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/caoyuz/.ssh/id_rsa.
Your public key has been saved in /home/caoyuz/.ssh/id_rsa.pub.
The key fingerprint is:
2e:57:e2:bf:fd:d4:45:5c:a7:51:3d:f1:51:3c:69:68 root@krusty04

这个命令将为 homer06 上的当前用户 caoyuz 生成其密钥对,密钥对的保存路径使用缺省的 /home/caoyuz/.ssh/id_rsa, 要求输入 passphrase 的时候,直接回车。这样生成的证书以及公钥将存储在 /home/caoyuz/.ssh 目录,形成两个文件 id_rsa,id_rsa.pub。然后将 id_rsa.pub 文件的内容复制到每一台机器(包括本机 homer06)的  /home/caoyuz/.ssh/authorized_keys 文件的尾部,如果机器上不存在  /home/caoyuz/.ssh/authorized_keys 文件,可以自行创建一个。请注意 id_rsa.pub 文件的内容是长长的一行,复制时需注意,不要遗漏字符或混入了多余换行符。

接下来可以做一下 SSH 连接测试,从 homer06 分别向 homer06, homer07, homer08 发起 SSH 连接请求,确保不需要输入密码就能 SSH 连接成功。注意第一次 SSH 连接时会出现如下提示信息:

The authenticity of host [homer06] can't be established. The key fingerprint is: 74:32:91:f2:9c:dc:2e:80:48:73:d4:53:ab:e4:d3:1a Are you sure you want to continue connecting (yes/no)?

请输入 yes, 这样 OpenSSH 会把连接过来的这台主机的信息自动加到 /home/caoyuz/.ssh/know_hosts 文件中去,第二次再连接时,就不会有这样的提示信息了。

Linux hard link

发表于 2009-05-06 | 分类于 未分类

You may confused what the hard link is, and why the delete file use the unlink(2) system call. this example will help you somehow.

assumption, there is a file in /tmp/a.txt

$ cat /tmp/a.txt
hello, i'm a.txt

then, we write this test.c
test.c:

#include <unistd.h>
int main(void)
{
link("/tmp/a.txt", "/tmp/new_a.txt");
/* in this moment, the a.txt have same content with new_a.txt */
unlink("/tmp/a.txt");
/* in this moment, a.txt will be delete */
}

we compile the test.c
$gcc test.c

and run it
$./a.out

$ cat /tmp/new_a.txt
hello, i'm a.txt

Use hard link is the simplest and the fastest way move files within the same filesystem, since hard link cann't across filesystem.

More infomation you need check the link(2) or the unix filesystem design.

使用libjpeg直接转换YUV420图像到JPEG

发表于 2009-04-03 | 分类于 未分类

这篇文章主要介绍如何把yuv420的图像(当然也可以是422p的图像,但是如果不是planner格式的YUV数据,就不能直接作转换)用libjpeg转换成jpeg的图像。在网上看到许多介绍如何压缩jpeg图像的文章都是通过现把yuv数据转换成rgb格式,然后再通过libjpeg来压缩。但是jpeg内部是基于yuv的颜色空间的,所以这样会进行2次多余的颜色空间转换,这些都是不必要的。

看了jpeg的spec以后和libjpeg,还有mjpg-streamer的代码以后,自己尝试作一个能够转换的小程序。 现在把这个程序贡献出来给大家作为参考,有兴趣的可以下来代码看看, 100多行代码,很简单的。

source code: http://sites.google.com/site/kzjeef/files/still.bz2?attredirects=0

$ tar jxvf still.bz2
$ cd yuv2jpg
$ make

建议先阅读 README, 经过转换的图像放在/tmp/still.jpg

谢谢。

About Prelink & Enable prelink in Unbuntu

发表于 2008-11-09 | 分类于 未分类

What is Prelinking?

Prelinking is a process that allows you to speed up the process of dynamic linking. What it does is, through a process of magic that is incredibly complex, but basically it speeds up the relocation process, in loading shared libraries.. by setting preferred load addresses.

What is he talking about, you ask? Well, here's how it goes. Dynamically linked libraries (often referred to as DSO's or in the windows world DLLs) are libraries which are loaded at runtime. The advantage of shared libraries versus static libraries is, that it reduces the size of executables, since each program doesn't need its libraries built in, but will dynamically locate and use it at run time. This adds some overhead, because the address space of the library, instead of being known in advance, has to be mapped at runtime. Another advantage of shared libraries is that due to the magic of copy-on-write, you can save memory when you have multiple programs sharing the same library (though it's not as simple as that.. it is pretty complicated)

Pre-linking sets preferred address space for the libraries to be loaded at. Note they are _preferred_ spaces, and if that address space is not available, then it will use relocation to load it dynamically.. hence pre-linking is not 100% foolproof.

What do I need to prelink?

To prelink, you need at least:
- glibc 2.3.1 or greater
- libelf or elfutils 0.7, though 0.8 is preferred. Latest version as of this writing is 0.84
- Your shared libraries have to be compiled with -fPIC
- the prelink program from ftp://people.redhat.com/jakub/prelink


HOWTO: Enable prelink

Prelink is in universe. I use it on my Ubuntu system without issues, but do google about Prelink and do your research before trying it out.

How to enable prelink:

1. Activate Ubuntu universe sources. The procedure is well-documented by Ubuntu.
2. use apt-get or synaptic to install prelink.
3. Open /etc/default/prelink with your favorite editor, as sudo/root.
4. Change PRELINKING=unknown from unknown to yes.
5. Adjust the other options if you know what the heck you're doing. Defaults work well.
6. To start the first prelink (the longest one!), run sudo /etc/cron.daily/prelink

In the future, prelink performs a quick prelink (a less-than-1-minute procedure on most systems) daily, usually at midnight. Every 14 days, or whatever you changed it to be, a full prelink will run.

If you just did a major apt-get upgrade that changed systemwide libraries (i.e. libc6, glibc, major gnome/X libs, etc etc etc) and experience cryptic errors about libs, rerun step 6.

To undo prelink, change step 4 from yes to no, then rerun step 6.

WARNING:
An amd64 users in another thread (http://ubuntuforums.org/showpost.php...3&postcount;=16) reports having experienced trouble with AMD64 prelinking. I have NOT tried prelinking on anything other than an i386!

移植 tslib 到Android 的注意事项

发表于 2008-11-05 | 分类于 未分类

tslib是这样的结构, tslib只是一个上层, 他主要是调用它的那些插件来处理从驱动那里得到的点,
插件的顺序是你的 ts.conf 里面的插件的顺序。
昨天发现能够从/dev/input/event1里面读出东西来, 却不能够用ts_test来响应,原来是驱动送上来的点不符合规则, (更搞笑的是他还没意识到这点, 坚持说自己是对的), 我用的插件是 input 插件, 找到问题以后就好办了, 改configure.ac里面, 给自己加一个插件, 就叫ac97吧, 然后创建文件 tslib_src/plugins/ac97-raw.c , 把你“特殊”的驱动加进去就可以了。

测试的时候, 发现很慢, 奇怪驱动送上来的点是很快的啊, 结果发现是因为我在读入每个点之后都打印了调试信息, 所以导致tslib的延迟, 如果你的系统也存在同样的问题, 最好去掉debug试一试, 毕竟终端是比较慢的设备。

其实把tslib放入Android里面首先要解决的问题是Android的编译系统, 你要把tslib, 和那些你用到的插件们都编译成动态库的形式, 可以参考其他库的Android.mk文件来编写, 还有就是你要把ts_test, ts_calebrate编译成可执行文件, 同样是编写Android.mk。

因为Android是从/dev/input/event0读入鼠标事件, 我们的想法就是不要去改动他的代码, 在后台运行一个daemon, 它就是把 /dev/input/event1中读出来的坐标(经过校准的),inject到event0里面, 这样一来就可以不修改Android的代码, 而加入我们的东西。
我在想, 要不要把tslib的东西checkin到Android的baseline中呢。 这是一个好想法。 毕竟不是所有人都用电容触摸屏的。

移植进入Android里面可能会有这样的问题, 就是点击触摸屏和键盘会没有反应, 其实这是因为电源管理相关的东西导致的, 如果你也遇到同样的问题, 那么请屏蔽掉 _android_src_/hardware/libhardware/power.c中的函数(关键是最后一个函数, 函数名我忘了呵呵)。

PS:fprintf到stderr上严重影响性能, 用打log调试确实不是一个好的方法, 在tiny loop和相应用户事件的地方不要大量的打log, 这也是为什么让我体会到了Android中的logger设备的优点啊。

android eabi lib

发表于 2008-10-21 | 分类于 未分类

Android SDK里面所带的二进制库生成全部是EABI兼容的, 那么什么是EABI? 对
我们有什么意义呢?

1.这里现介绍一下什么是EABI(Embedded Application Binary Interface)

EABI最早是从powerPC 在 UNIX V5 的ABI(Application Binary
Interface)的补充的基础上构建的, 他的木表示减少内存使用和优化执
行速度, 而且这些都是特别针对于嵌入式系统软件的。
EABI规定了一下几个内容:
* 文件的格式
* 数据类型和对齐
* 寄存器用法的规定
* 堆栈布局和组织
* 函数参数传递顺序
* 小数据区域(data area)的用法和组织

2. 对我们有什么意义呢?
EABI兼容的二进制库除了在速度上有所优化以外, 它还保证了一样东
西。
就是让软件开发着可以混合使用EABI兼容的不同厂商之间的二进制库,
只要他们是EABI兼容的, 并且开发者的软件也是用EABI兼容的编译起编
译的, 就可以正确的一起使用。

这样, 我们就可以在不能拿到代码, 或者是专门为本平台编译过的二进
制库的情况下来使用这些二进制库。

就近来讲, 就是Android SDK 里面带的.so文件, 有一些我们可以不必
要拿到代码,或者重新编译。 但是为了能够正确的使用这些库。 可能还
是需要一些支持工作。 比如这些库和驱动之间的接口之类的。

leaving hometown

发表于 2008-07-10 | 分类于 未分类

5号到家,11号走,没在家住几天。就要去公司了,今天和同学吃了个饭以后发现大家都是各走各的路了,包括今天早上想联系王志平也联系不上。大学真的是一散就真的散了。 看到他在博客里写走的那天窗户只开了一点点小窗户的感觉真的很伤心。我不懂这些礼仪这些的,我认为只要心意在那里就可以了,可是我知道我错了,有时候会伤了别人的心。

今天家里的叔叔姑姑们给我和龙哥饯行,喝酒的时候我很后悔在毕业饭的时候没有喝多,回去还那么清醒。现在距离才多长时间,兄弟们都已经天南地北各奔东西了。现在我只能说,兄弟走好。。。多联系。

Different life, not same world, not same dream!

1…678…11
Jiejing Zhang

Jiejing Zhang

110 日志
11 分类
24 标签
© 2017 Jiejing Zhang
由 Hexo 强力驱动
主题 - NexT.Pisces