Bash Color Support for Bti

Bti is well known as a command line twitter/identica/statusnet client, by Greg Kroah-Hartman.

I have been using this tool in CLI environment for a long time, found it difficult for human to parse output by eyes. So I forked it and added this bash color support, to provide some highlight on tweets.
bti-bash-color

To enable color support, add an option –auto-color to bti command, or append auto-color=yes to your configuration file($HOME/.bti)

I have sent pull request to Greg, hope the patch can be merged into master. Before approved, you can check out my fork and compile it yourself. (If liboauth not found, find it here.)

Redis Data Struct

Redis的几个核心数据结构定义在redis.h和dict.h中。Redis服务器总的数据结构里的根节点是redisServer,下面包含一个redisDb结构数组。数组的大小定义在redis.conf中的databases项,客户端可以通过select命令选择相应的DB。

一个redisDb定义了redis数据库的基本数据结构,redisDb结构定义如下

typedef struct redisDb {
    dict *dict;                 /* The keyspace for this DB */
    dict *expires;              /* Timeout of keys with a timeout set */
    dict *blocking_keys;        /* Keys with clients waiting for data (BLPOP) */
    dict *io_keys;              /* Keys with clients waiting for VM I/O */
    dict *watched_keys;         /* WATCHED keys for MULTI/EXEC CAS */
    int id;
} redisDb;

其中

  • dict 用于存储键值的哈希表结构
  • expires 用于存储键、过期时间的哈希表结构
  • blocking_keys 用于存储键、阻塞数据的哈希表结构。目前仅仅在阻塞式的blpop brpop中使用。
  • io_keys 存储等待将数据从swap读出的key (vmThreadedIOCompletedJob, dontWaitForSwappedKey, waitForSwappedKey)
  • watched_keys 用于支持WATCH命令,实现乐观锁功能
  • id 数据库编号

dict结构定义在dict.h中,主要的定义有:

typedef struct dictEntry {
    void *key;
    void *val;
    struct dictEntry *next;
} dictEntry;

typedef struct dictht {
    dictEntry **table;
    unsigned long size;
    unsigned long sizemask;
    unsigned long used;
} dictht;

typedef struct dict {
    dictType *type;
    void *privdata;
    dictht ht[2];
    int rehashidx; /* rehashing not in progress if rehashidx == -1 */
    int iterators; /* number of iterators currently running */
} dict;

每个dict中包含两个dictht结构,用来在rehash的时候进行数据交换。rehashidx用于记录rehash的进程,值是当前rehash的table索引。dictht中table二维数组,以hash值为索引。size是哈希表的大小,初始值是4,随着rehash的进行,变成大于size的最小2n。sizemask是size-1,用来与哈希值做&运算。

redis在引如virtual memory之前,就是以这样的数据结构运行在内存中的。

Visualize call tree of a C function

Requirement

You want to visualize a call hierarchy of a C function.

Solution

Utilities you need are listed below:

Take ‘rdbSaveBackground’ (redis/rdb.c) for example:

cflow --format=posix --omit-arguments --level-indent='0=\t' --level-indent='1=\t' --level-indent=start='\t' -m 'rdbSaveBackground' ~/osprojects/redis/src/rdb.c | cflow2dot | dot -Tjpg -o rdb.jpg

Output:
visualization of a call tree

Source: unix diary

The post is brought to you by lekhonee v0.7

Lua script for Geany to view manpage of functions

Requirement

When editing when Geany, you need some document of system library functions at hand. So you want to browse manpage at any time.

Solution

Just two lines:

sel=geany.selection()
geany.launch('rxvt-unicode', '-e', 'man', sel)

http://gist.github.com/591279

Install

Make sure you have geany-plugin-lua installed:
sudo apt-get install geany-plugin-lua

Create a lua source file named ‘ShowMan.lua’ in ~/.config/geany/plugins/geanylua/ . If the direcotry does not exist, you should create it first.

Copy two lines of lua code into this file, save and close.

Create a file named hotkeys.cfg in ~/.config/geany/plugins/geanylua/, add following content:
ShowMan.lua

Restart geany, then open Preferences->Keybindings, Lua Script-> ShowMan, bind a key, for example, Alt+m

Usage

Select the function name, and press Alt+m (or from menu Tools->Lua Scripts->ShowMan), then you see the terminal and the manpage.

geany-lua-manpage

The post is brought to you by lekhonee v0.7

GUI Debugging tools

想了解一个C程序的运行,打算用gdb来单步看一下流程,发现直接用gdb不太方便,然后模仿偶像用emacs的gdb支持(M-x gdb)。但是境界实在是比不上偶像,emacs的操作都还不熟练。最后选择了geany。geany是一个简单的C/C++ IDE,它有一个geanygdb插件可以帮助你在geany的图形界面里调试。

但是最近安装在仓库里的geanygdb插件有一些问题,会导致很高的CPU占用。不过这个问题被报告在这里,现在已经修复了。因为新版本还没有release,所以如果急着用的话,可以从svn签出代码:
svn co https://geany-plugins.svn.sourceforge.net/svnroot/geany-plugins/trunk/geany-plugins
然后执行:

cd geany-plugins
./autogen.sh
./configure
cd geanygdb
make
sudo make install

重启geany,打开plugin manager,看到geanygdb的版本已经变成0.20就可以使用了。

geany现在还不支持ctags,是因为它内部有一套自己的tags实现。代码导航可以用过上下文菜单 Go to tag Definition 或者 Go to tag Declaration 实现,最好是到Preference中给这两个命令设置一个快捷键,比如Ctrl+],再对Navigate back 设置一个Ctrl+t,就如同vim+ctags一样了。

实际上在GNOME桌面还有一个专门的GDB图形界面,叫做Nemiver。Nemiver对gdb的常用命令都有快捷键操作,把鼠标移动到代码上还可以查看变量的值,效果和eclipse调试java程序一样,非常强大。

The post is brought to you by lekhonee v0.7