Wednesday, March 18, 2009

Howto enable tab completion with sudo in Gentoo

The problem:
After I installed Gentoo, I found that tab completion with sudo doesn't work. i.e., type:
$sudo emer[tab]
doesn't shows or completes with the word "emerge". Without sudo, the tab completion works.

Solution:
install bash-completion package, and
$eselect bashcomp base gentoo ssh

...
Read More

Wednesday, August 20, 2008

likely/unlikely macros in Linux Kernel

http://kerneltrap.org/node/4705

Ever wondered what the likely and unlikely macros in the linux kernel are ?
The macros are defined as :
#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. 

To check it out how it could be beneficial, an excerpt from "info gcc" :
  if (__builtin_expect (x, 0))
  foo ();


  [This] would indicate that we do not expect to call `foo', since we
  expect `x' to be zero. 

Based on this information the compiler generates intelligent code, such that the most expected result is favored.

Let us consider it with a simple example function :

[kedar@ashwamedha ~]$ cat abc.c
int
testfun(int x)
{
  if(__builtin_expect(x, 0)) {
  ^^^--- We instruct the compiler, "else" block is more probable
  x = 5;
  x = x * x;
  } else {
  x = 6;
  }
  return x;
}
 
[kedar@ashwamedha ~]$ gcc -O2 -c abc.c
[kedar@ashwamedha ~]$ objdump -d abc.o
 
abc.o: file format elf32-i386
 
Disassembly of section .text:
 
00000000 :
  0: 55 push %ebp
  1: 89 e5 mov %esp,%ebp
  3: 8b 45 08 mov 0x8(%ebp),%eax
  6: 85 c0 test %eax,%eax
  8: 75 07 jne 11 <>
  ^^^ --- The compiler branches the "if" block and keeps "else" sequential
  a: b8 06 00 00 00 mov $0x6,%eax
  f: c9 leave
  10: c3 ret
  11: b8 19 00 00 00 mov $0x19,%eax
  16: eb f7 jmp f <>


And let us see what happens if we make the "if" block more likely.

[kedar@ashwamedha ~]$ cat abc.c
int
testfun(int x)
{
  if(__builtin_expect(x, 1)) {
  ^^^ --- We instruct the compiler, "if" block is more probable
  x = 5;
  x = x * x;
  } else {
  x = 6;
  }
  return x;
}
   
[kedar@ashwamedha ~]$ gcc -O2 -c abc.c
[kedar@ashwamedha ~]$ objdump -d abc.o
   
abc.o: file format elf32-i386
   
Disassembly of section .text:
   
00000000 :
  0: 55 push %ebp
  1: 89 e5 mov %esp,%ebp
  3: 8b 45 08 mov 0x8(%ebp),%eax
  6: 85 c0 test %eax,%eax
  8: 74 07 je 11 <>
  ^^^ --- The compiler branches the "else" block and keeps "if" sequential 
  a: b8 19 00 00 00 mov $0x19,%eax
  f: c9 leave
  10: c3 ret
  11: b8 06 00 00 00 mov $0x6,%eax
  16: eb f7 jmp f <>


...
Read More

Wednesday, July 23, 2008

screen goes black when use vesafb

When I use vesafb and set vga=xxx as kernel option to change the resolution, the screen goes blank at boot time. I know the system is still booting, nothing is wrong except the screen.

I finally found the solution. I appears the framebuffer module is not loaded with the kernel.

The walkthough is to edit /etc/modules.autoload.d/kernel-2.6   (in Gentoo, other distros may have different path) to add these two lines:

fbcon
vesafb

...
Read More

Saturday, July 12, 2008

升级x11-misc/shared-mime-info-0.40后mime数据出问题

升级x11-misc/shared-mime-info-0.40后打不开pdf、zip等文件,所有格式均识别为mime/plain-text。

Gentoo Bug #228885

解决办法为:

$ update-mime-database ~/.local/share/mime/
# update-mime-database /usr/local/share/mime/

...
Read More

Thursday, July 10, 2008

关于Sql Server Compact程序运行结束后更改丢失的问题

最近用Linq to SQL + Sql Server Compact 3.5写东西

发现往数据库里加东西以后,程序运行时是有效的,但是重编译工程后这些更改又丢失了。

后来得知,这是因为数据库文件默认的生成方式是“总是复制”(always copy),这样每次build都会把空的数据库文件拷到output目录。改成“copy if newer”就好了。

...
Read More

Tuesday, July 1, 2008

[转]Linux之父炮轰C++:糟糕程序员的垃圾语言

...
Read More

C99的新特性

C99的语法还是很爽的,单行注释、bool、随处声明变量,都很必要。

最近心血来潮想查查C99的全部新特性,发现还有很多我不知道的。

以下是C99之于C89的新特性,摘自http://www.kuro5hin.org/?op=displaystory;sid=2001/2/23/194544/139

Interesting New Features

A list of features that should[0] have made it into the C99 standard is available on Thomas Wolf's webpage. Listed below are the changes that most developers would notice or care about at first use. 
Increased identifier size limits: specifically 63 significant initial characters in an internal identifier or macro name, 31 significant initial characters in an external identifier, and 4095 characters in a logical source line. These values were 31, 6, and 509, respectively, in C89. 

The small identifier size is the reason so many ANSI functions had terse names (strcpy, strstr, strchr, etc) since the standard only guaranteed that the first six characters would be used to uniquely identify the functions. 

C++ style/line comments: The characters '//' can now be used to delineate a line of commented text, just like in C++. 

Macros take variable arguments denoted by elipsees: Function-like macros will accept variable arguments denoted by using the ellipsis (...) notation. For replacement, the variable arguments (including the separating commas) are "collected" into one single extra argument that can be referenced as __VA_ARGS__ within the macro's replacement list. 

Inline functions: The C language now supports the inline keyword which allows functions to be defined as inline, which is a hint to the compiler that invocations of such functions can be replaced with inline code expansions rather than actual function calls.

Restricted pointers: The C language now supports the restrict keyword which allows pointers to be defined as restricted, which is a hint to the compiler to disallow two restricted pointers from being aliases to the same object which allows for certain optimizations when dealing with the said pointers. 

_Bool Macro: There is a _Bool type which is a actually two valued integer type. True is defined as 
#define true (_Bool)1 
while false is defined as 
#define false (_Bool)0 
Variable Declarations can appear anywhere in the code block: No longer do variables have to be defined at the top of the code block. 

Variable length arrays: These are arrays whose size is determined at runtime. 

Variable declarations in for loops: Variables can now be declared and initialized in for loops just like in C++ and Java. 

Named initialization of structs: The members of a struct can now be initialized by name such as is done in the code block below 
struct {float x, y, z;} s = { .y = 1.0, .x = 3.5, .z = 12.8}; 
New long long type: There is a new type called long long which is at least 64 bits and can be both signed or unsigned. The new suffixes "LL" or "ll" (and "ULL" or "ull") are used for constants of the new long long type.

Functions must declare a return value: Function return types no longer defaults to int if the function declares no return type. 

Last member of a struct may be an incomplete array type. : This is to support the "struct hack" which works on most existing C compilers already. A code example of the struct hack is shown on Thomas's site. 

Addition of _Complex and _Imaginary number types: A boon for programmers doing any sort of advanced math in their programs. 

Multiple uses of a type qualifier are ignored after the first occurence: If a type qualifier appears several times (either directly or indirectly through typedefs) in a type specification, it's treated as if it appeared only once. E.g. 
const const int x; 
is the same as 
const int x; 
C++ Incompatibilities

The aforementioned features are rather impressive but one soon realizes that this means that C is no longer a subset of C++. There is a list of the major incompatibilities between the current ISO standards for C and C++ on David Tribble's webpage. At this point it is still too early to see if either C or C++ will be harmed by this development but it is clear that there will be some growing pains once C99 adoption becomes widespread. 

Bjarne Stroustrup mentioned in a recent interview with LinuxWorld that he would have liked for both languages to be compatible and would favor a technical committee whose express purpose would be integrating both languages, but doubts the possibility of this coming to pass. Stroustrup also contrasted how C's technical commitee decided to implement most of the added functionality via changes to the actual C language against the C++ approach which was by adding additional libraries. 

Compiler support

After describing all the exciting new features of C99, one would expect there to be more awareness of the standard by now but there isn't. The primary reason for the lack of awareness of C99 is the fact that compiler support at the current time is practically non-existent. Here is the status of the major compiler vendors for the development platforms that I'm interested in: 
Microsoft: A search on Microsoft's site for C99 draws a total blank. It looks like Microsoft does not plan to upgrade the C compiler that ships with Visual C++ to cover the C99 standard. 

Borland: A search on Borland's site for C99 also draws a blank. Again it looks like exclusive C++ development has won over keeping their C compiler up to date. 

Comeau Computing: The most recent version of their compiler (version 4.2.45.1) which is available for online testing claims to support a large number of features from C99. The compiler has not yet been released but can be tested online by submitting code snippets in an HTML form. 

SAS Institute: SAS/C version 7.0 supports the "long long" type, inline functions and a few of the preprocessor directives. 

Edison Design Group: The EDG C++ front end supposedly supports both C89 and C99 as well as Microsoft C++ and ANSI/ISO C++. 

GNU: GCC has a status page of C99 compliance which shows that they are quite close to becoming fully compliant with the C99 standard. 


Where To Get It

If you are interested in being the first kid on your block who knows the finer points of restricted pointers and variable argument macros, you can purchase the C99 standard online from ANSI. Finally no discussion of C99 is complete without a reference to Dennis Ritchie's opinion of the C99 standard. 

[0] I didn't buy a copy of the standard, so I cannot guarantee that everything on that site made it into the standard although there is a good chance that everything did.

C99标志着C与C++分道扬镳,C不再是C++的子集了。M$不仅目前不太支持,似乎未来也不打算支持(除非未来C++0x会采纳一些C99)。而GCC已经完美支持C99了。

...
Read More

Thursday, June 5, 2008

关于用ObjectInputStream封装socket输入流后程序阻塞的问题

下面的代码运行后在(2)和(3)处会阻塞住

public class Server {
public static void main(String[] args)throws Exception{
ServerSocket ssock=new ServerSocket(3000);
Socket sock=ssock.accept();

ObjectInputStream in=new ObjectInputStream(sock.getInputStream()); //(2)
ObjectOutputStream out=new ObjectOutputStream(sock.getOutputStream()); //(1)
while(true){
out.writeObject("s");
out.flush();
}
}
}

public class Client {
public static void main(String[] args)throws Exception{
Socket sock=new Socket("127.0.0.1",3000);
ObjectOutputStream out=new ObjectOutputStream(sock.getOutputStream());//(3)
ObjectInputStream in=new ObjectInputStream(sock.getInputStream()); //(4)
while(true){
out.writeObject("s");
System.out.println((String)in.readObject());
}
}
}

而把(1)和(2),(3)和(4)对调后程序运行正常。

原因如下:
使用缺省的serializetion的实现时,一个ObjectOutputStream的构造和一个ObjectInputStream的构造必须一一对应.ObjectOutputStream的构造函数会向输出流中写入一个标识头,而ObjectInputStream会首先读入这个标识头!

...
Read More

Saturday, April 19, 2008

wine强制全屏游戏在窗口模式下运行

wine explorer /desktop=1024x768 whatever.exe

...
Read More

Monday, April 7, 2008

用psyco优化python程序

psyco是python扩展通过编译python代码提升python程序速度

使用方法简单在python程序加上下面可以

import psyco
psyco.full()

psyco程序运行编译部分代码具体怎么编译清楚因此程序初始时间增加运行速度

篇gentoo-wiki介绍用psyco优化portage速度

http://gentoo-wiki.com/TIP_Speed_up_portage_with_Psyco

文章了psyco以后,portage性能可以提升2-5同时警告说psyco现在稳定可能带来很多问题因此推荐使用

了psyco的emerge速度看来2-5影响使用问题比如sudoemerge异常确认删除“no异常猜测因为psyco对try-except语句支持原来程序里catch异常现在引发了psyco异常

不过问题影响正常使用继续观望

另外的python程序支持psyco像Frets On Fire源码初试使用psyco了psyco以后玩Frets On Fire感觉不过没有portage那么明显

...
Read More
 
/* google analytics */