2009年12月1日星期二

How to bind DB2 process on given CPU set on AIX.

The key point is export the DB2_RESOURCE_POLICY environment variable.

1. Using the following script to create db2:
#!/bin/sh
# Create a reset to set db2 running on cpu 22-31.
mkrset -c 22-31 -m 0 dT/db2
# Export the DB2_RESOURCE_POLICY to the give policy file.
export DB2_RESOURCE_POLICY=/home/db2inst2/dbscripts/db2policy

Then create your data base here...


2. db2policy :

2009年4月29日星期三

统计java程序运行时间

在java程序统计某段代码的运行时间至少有如下两种方法:

1. 通过System.currentTimeMillis()来获取当前时间,以毫秒数形式返回。 long java.lang.System.currentTimeMillis()
currentTimeMillispublic static long currentTimeMillis()
Returns the current time in milliseconds. Note that while the unit of time of the return value is a millisecond, the granularity of the value depends on the underlying operating system and may be larger. For example, many operating systems measure time in units of tens of milliseconds.
See the description of the class Date for a discussion of slight discrepancies that may arise between "computer time" and coordinated universal time (UTC).
Returns:
the difference, measured in milliseconds, between the current time and midnight, January 1, 1970 UTC.


所以我们可以这样统计代码段运行时间(毫秒):
long start=System.currentTimeMillis();
//your code
long end=System.currentTimeMillis();
long time = end -start;
这就是代码运行的毫秒数了。


public class CountTime {
public static void main(String args[])
{
long start=System.currentTimeMillis();

for (int i=0; i<1000000; i++)
{
for (int j=0; j<100000; j++)
{
for (int k=0; k<10000; k++)
{
int a = i*j;
a++;
}
}
}

try
{
Thread.sleep(1000);
} catch (InterruptedException e)
{
System.out.println("Interrupted");
}
long end=System.currentTimeMillis();
System.out.println("Run Time:"+(end-start));
}
}


2. 通过import java.util.Date; 中的Date对象来获取执行时间。
Date startDate= new Date();
//your code
Date endDate= new Date();
long time=endDate.getTime()-startDate.getTime();
这就是代码运行时间,也是毫秒为单位。

2009年3月26日星期四

AIX set processor affinity!

问题:
前两天在PPC上做性能优化的时候,需要确切的知道某些函数的执行时间以求分析性能瓶颈(bottle neck)。于是用汇编指令去读取一个特定的寄存器获取一个cycle值,当进入一个代码区域时读取一下,出去时读取一下,两个值想减就获得整个代码区域的执行cycle。 可是在多核环境下,很可能程序会在不同的cpu上执行,而各个cpu的频率有可能不一样,最后得到的cycle数就不准确。为了得到准确的值,只有在程序执行之前将执行程序的进程绑定到某个特定的cpu上。怎么绑定?在pthread.h里面有标准的函数,但是我发现在AIX上这些函数没有定义。Google了一把,终于找到了AIX上绑定cpu的命令和API。

解决办法:
在AIX上有两种方法可以绑定cpu,第一种是通过执行命令绑定;第二种是在你的程序中调用它提供的API。
1. command:
To query for the available processors on the machine, try:
查看可用的cpu:
# bindprocessor -q
The available processors are: 0 1

绑定进程到某个cpu:
Bind the p1 processes, whose process id is 14662 to processor 1:
# bindprocessor 14662 1

查看绑定结果:
Check to make sure the process is bound to the specified processor:
#ps -emo THREAD grep p1
USER PID PPID TID ST CP PRI SC WCHAN F TT BND COMMAND
root 4460 5428 - A 0 60 7 f0145c10 240001 - - /usr/sbin/rpc.mountd
root 4710 5428 - A 0 60 4 * 240001 - - /usr/sbin/tftpd -n
root 14662 10566 - A 120 126 0 - 200001 pts/0 1 ./p1

取消绑定:
Unbind the process:
# bindprocessor -u 14662
Check the result of the above command:
# ps -emo THREAD grep p1
USER PID PPID TID ST CP PRI SC WCHAN F TT BND COMMAND
root 12672 10566 - A 1 60 1 50a05e84 200001 pts/0 - grep p1
root 14662 10566 - A 78 111 0 - 200001 pts/0 - ./p1

2. 调用AIX上的API:
首先所有的API定义在: #include
1) sysconf(_SC_NPROCESSORS_CONF); //获取当前配置的cpu数目
2) sysconf(_SC_NPROCESSORS_ONLN);//获取当前可用的cpu数目
3) bindprocessor(BINDPROCESS, pid, bindparameter);//绑定当前进程到指定cpu。 其中pid是当前进程的pid, bindparameter是想绑定的cpu id。
在做绑定之前一定要查询当前可用的cpu数,如果为0则无法绑定。

当你执行程序时,可以用前面提供的命令去查询你的进程是否绑定成功。

2009年3月16日星期一

手动控制AIX下应用程序可用堆内存大小

export LDR_CNTRL="MAXDATA=0x40000000"; ./app

Working with the LDR_CNTRL environment variable

The LDR_CNTRL environment variable controls the way AIX® handles the memory space available to a 32-bit program and the page sizes used within each segment. The POWER4™ and later PowerPC® processors support the use of 16 MB large pages in addition to the default 4 KB pages. The POWER5+™ and later PowerPC processors add support for two new page sizes, 64 KB and 16 GB.

Controlling the available memory space

The SDK normally sets appropriate LDR_CNTRL=MAXDATA settings automatically for the size of the Java™ heap. However, if you explicitly set the LDR_CNTRL=MAXDATA environment variable the SDK will not override the value you have specified. Do not override the automatic settings unless you have a complete understanding of the AIX memory models. For more information about the AIX memory models and the automatic settings used by the SDK.

2009年2月17日星期二

dbx 调试小结

dbx是一个UNIX下的代码调试工具,它可以在 Solaris, AIX,BSD UNIX系统上使用。更加详细的信息可以参见:dbx。
在使用dbx之前需要在编译源代码时加上 -g 选项。
常用的调试命令:
1. 加载程序:dbx -d [2000] obj [core]。
> Use the -d option to increase nesting depth limit.
>obj: 是编译出来的可执行文件
>core:core dump 文件
2. 运行程序:run(r)
3. 单步:next(n), step(s)
4. 寻求帮助:help [topic]
5. 查看内存栈:where
6. 搜索符号定义: whereis []
7. 设置断点:stop at [line number], stop in [function]... 详细:help stop
8. 跟踪变量:trace variable in [function], trace line number.... 详细:help trace
9. 查看断点,跟踪状态:status
10. 移除断点,跟踪: delete [num]。 先执行status得到num.
11.线程信息:thread
12.退出:quit(q)

13.按指令级别调试: nexti, setpi
14.打印寄存器的值:print $r