Unix程式設計/應用問答中文版 ---4.系統資源相關問題(轉)

post0發表於2007-08-10
Unix程式設計/應用問答中文版 ---4.系統資源相關問題(轉)[@more@]

作者:不祥 [文章出自: ]

4. 系統資源相關問題

4.1 主流Unix作業系統上如何程式設計獲取程式的記憶體、CPU利用狀況

4.2 Solaris下如何獲知CPU速率

4.3 如何程式設計獲取Solaris系統當前記憶體大小

--------------------------------------------------------------------------

4. 系統資源相關問題

4.1 主流Unix作業系統上如何程式設計獲取程式的記憶體、CPU利用狀況

Q: Solaris下如何程式設計獲知CPU佔用率和記憶體佔用資訊呢,可移植嗎?

Q: 我想寫個程式遍歷當前執行中的活動程式,Solaris提供相應系統呼叫了嗎

A: Nicholas Dronen

不可移植。man -s 4 proc,man -s 3k kstat

如果不是程式設計,可以用top、mpstat、vmstat、sar(1)等等,還有

/usr/ucb/ps -aux,對於Solaris來說,後者更直接精煉,top不是標準配置。

# /usr/bin/prstat (Solaris 8 prstat(1M)手冊頁)

# /usr/ucb/ps -aux | head (Solaris 2.x)

Q: 主流Unix作業系統上如何程式設計獲取程式的記憶體、CPU利用狀況,AIX、HP、SUN

process memory usage

process cpu time usage

A: Nate Eldredge

man -s 3C getrusage

D: 小四

在SPARC/Solaris 2.6/7下結論一致,只支援了ru_utime和ru_stime成員,其他成員

被設定成0。FreeBSD 4.3-RELEASE上測試,則不只支援ru_utime和ru_stime成員。從

FreeBSD的getrusage(2)手冊頁可以看到,這個函式源自4.2 BSD。

至少對於SPARC/Solaris 2.6/7,getrusage(3C)並無多大意義。

A: Robert Owen Thomas

對於Solaris,可以利用procfs介面,下面的例子獲取指定程式的記憶體佔用情況

--------------------------------------------------------------------------

/*

* @(#)memlook.c 1.0 10 Nov 1997

* Robert Owen Thomas robt@cymru.com

* memlook.c -- A process memory utilization reporting tool.

*

* gcc -Wall -O3 -o memlook memlook.c

*/

#pragma ident "@(#)memlook.c 1.0 10 Nov 1997 Robert Owen Thomas robt@cymru.com"

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

int counter = 10;

int showUsage ( const char * );

void getInfo ( int, int );

int main ( int argc, char * argv[] )

{

int fd, pid, timeloop = 0;

char pidpath[BUFSIZ]; /* /usr/include/stdio.h: #define BUFSIZ 1024 */

switch ( argc )

{

case 2:

break;

case 3:

timeloop = atoi( argv[2] );

break;

default:

showUsage( argv[0] );

break;

} /* end of switch */

pid = atoi( argv[1] );

sprintf( pidpath, "/proc/%-d", pid ); /* -表示向左靠 */

/*

* /proc/1/是目錄,但在這種用法中,就是直接開啟目錄,不是開啟檔案

*/

if ( ( fd = open( pidpath, O_RDONLY ) ) < 0 )

{

perror( pidpath );

exit( 1 );

}

if ( 0 < timeloop )

{

for ( ; ; )

{

getInfo( fd, pid );

sleep( timeloop );

}

}

getInfo( fd, pid );

close( fd );

exit( 0 );

} /* end of main */

int showUsage ( const char * progname )

{

fprintf( stderr, "%s: usage: %s < PID > [time delay] ", progname, progname

);

exit( 3 );

} /* end of showUsage */

void getInfo ( int fd, int pid )

{

prpsinfo_t prp;

prstatus_t prs;

if ( ioctl( fd, PIOCPSINFO, &prp ) < 0 )

{

perror( "ioctl" );

exit( 5 );

}

if ( ioctl( fd, PIOCSTATUS, &prs ) < 0 )

{

perror( "ioctl" );

exit( 7 );

}

if ( counter > 9 )

{

fprintf( stdout, "PID IMAGE RSS HEAP STACK " );

counter = 0;

}

fprintf( stdout, "%u %-9u %-9u %-15u %-15u ", pid,

( unsigned int )prp.pr_bysize, ( unsigned int )prp.pr_byrssize,

( unsigned int )prs.pr_brksize, ( unsigned int )prs.pr_stksize );

counter++;

} /* end of getInfo */

--------------------------------------------------------------------------

4.2 Solaris下如何獲知CPU速率

A: Philip Brown

psrinfo -v

psrinfo | grep on-line | wc -l 簡單給出CPU數目

A: scz

# /usr/platform/`uname -i`/sbin/prtdiag -v

# /usr/platform/`uname -m`/sbin/prtdiag -v

# /usr/bin/netstat -k cpu_info0

A: Tony Walton

如果你裝了Sun Workshop,還可以嘗試fpversion命令

# /opt/SUNWspro/bin/fpversion

A SPARC-based CPU is available.

CPU's clock rate appears to be approximately 266.1 MHz.

Kernel says CPU's clock rate is 270.0 MHz.

Kernel says main memory's clock rate is 90.0 MHz.

Sun-4 floating-point controller version 0 found.

An UltraSPARC chip is available.

FPU's frequency appears to be approximately 277.1 MHz.

Use "-xtarget=ultra2i -xcache=16/32/1:256/64/1" code-generation option.

Hostid = 0x80BC3CB3.

#

4.3 如何程式設計獲取Solaris系統當前記憶體大小

Q: 如何程式設計(或者有什麼現成命令)獲取Solaris系統當前記憶體大小?

A: Nithyanandham

幾個現成命令

/usr/platform/`uname -m`/sbin/prtdiag -v | grep Memory

prtconf -v | grep Memory

如果裝了GNU top,也可以直接用top命令看到。

D: scz

truss prtconf的輸出中有如下內容

sysconfig(_CONFIG_PAGESIZE) = 8192

sysconfig(_CONFIG_PHYS_PAGES) = 16384

Memory size: 128 Megabytes

# /usr/ccs/bin/nm -nx /dev/ksyms | grep "|sysconfig$"

10626] |0x0000100ec110|0x0000000001bc|FUNC |GLOB |0 |ABS |sysconfig

# find /usr/include -type f -name "*.h" | xargs grep -l _CONFIG_PAGESIZE

/usr/include/sys/sysconfig.h

# vi -R /usr/include/sys/sysconfig.h

/*

* cmd values for _sysconfig system call.

* WARNING: This is an undocumented system call,

* therefore future compatibility can not

* guaranteed.

*/

#define _CONFIG_PAGESIZE 6 /* system page size */

#define _CONFIG_PHYS_PAGES 26 /* phys mem installed in pages */

參看sysconf(3C)手冊頁。

_SC_PAGESIZE

_SC_PAGE_SIZE

_SC_PHYS_PAGES

A: Casper Dik

--------------------------------------------------------------------------

/*

* Program to determine the size installed physical memory on Suns.

*

* Casper Dik.

*/

#define MEGABYTE 0x00100000

#define MAXMEM 0x7ff00000

#define THEMEM "/dev/mem"

#include

#include

#include

#include

int main ( int argc, char * argv[] )

{

int fd = open( THEMEM, O_RDONLY );

char c;

unsigned long pos, mapstart = 0;

int totmb = 0;

if ( fd == -1 )

{

perror( THEMEM );

exit( 1 );

}

for ( pos = 0; pos < MAXMEM; pos += MEGABYTE )

{

if (lseek( fd, pos, 0 ) == -1 )

{

perror( "lseek" );

exit( 1 );

}

if ( read( fd, &c, 1 ) == -1 )

{

int size = ( pos - mapstart ) / MEGABYTE;

if ( size != 0 )

{

printf( "found %3d MB starting at 0x%p ", size, ( void * )mapst

art );

totmb += size;

}

mapstart = pos + MEGABYTE; /* start of next possible mapping */

}

}

printf( "Total memory size: %d MB ", totmb );

exit( 0 );

}

--------------------------------------------------------------------------

由於需要讀訪問/dev/mem,普通使用者使用者無法使用該程式。

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/8225414/viewspace-940486/,如需轉載,請註明出處,否則將追究法律責任。

相關文章