---
title: "linux 共享内存段研究"
author: "itspace"
date: "2009-12-09T14:11:27Z"
tags: []
categories: ["操作系统"]
slug: "linux"
draft: false
lastmod: "2009-12-09T14:16:21Z"
id: 544723
url: "https://dbzone.iteye.com/blog/544723"
---

无论是在unix或者是linux都有命令查看主机共享内存段的使用情况，以下研究来自linux平台。
一、查看Oracle用户共享内存段使用情况

> [oracle@capaa ~]$ ipcs
> 
> ------ Shared Memory Segments --------
> key        shmid      owner      perms      bytes      nattch     status      
> 0x00000000 917504     oracle    600        196608     2          dest         
> 0x00000000 950273     oracle    600        196608     2          dest         
> 0x00000000 983042     oracle    600        196608     2          dest         
> 0x00000000 1015811    oracle    600        196608     2          dest         
> 0x00000000 1081349    oracle    600        196608     2          dest         
> 0x00000000 1179656    oracle    600        196608     2          dest         
> 0x4cd26024 524300     oracle    640        562044928  55                      
> 0x00000000 1900559    oracle    600        12288      2          dest         
> 0x00000506 1802260    oracle    666        16384      0                       
> 0x00000000 1835029    oracle    600        196608     2          dest         
> 
> ------ Semaphore Arrays --------
> key        semid      owner      perms      nsems     
> 0xc7ef5bb0 98304      oracle    640        152       
> 0xc7ef5bb1 131073     oracle    640        152       
> 0xc7ef5bb2 163842     oracle    640        152       
> 
> ------ Message Queues --------
> key        msqid      owner      perms      used-bytes   messages  
  
简单说明：
shmid：表示共享内存段id
owner：表示共享内存段属于哪个用户，需要注意的是默认情况下600表示除root用户外不允许其他用户查看
bytes：表示共享内存段大小
nattch：表示连接到共享内存段的进程数
status：表示共享内存段的状态，dest表示该内存段被标记为destroyed状态。
二、查看共享内存段进程连接信息
1、指定shmid，查看进程连接信息
查看shmid为524300的具体信息，可用如下命令

> [oracle@capaa ~]$ ipcs -m -i 524300
> 
> Shared memory Segment shmid=524300
> uid=505 gid=505 cuid=505        cgid=505
> mode=0640       access_perms=0640
> bytes=562044928 lpid=15216      cpid=16790      nattch=55
> att_time=Wed Dec  9 13:44:45 2009  
> det_time=Wed Dec  9 13:44:45 2009  
> change_time=Fri Nov 27 15:58:44 2009 

2、查看所有共享内存段，进程连接信息


> [oracle@capaa ~]$  ipcs -pm
> 
> ------ Shared Memory Creator/Last-op --------
> shmid      owner      cpid       lpid      
> 917504     oracle     30438      30967     
> 950273     oracle     30440      1286      
> 983042     oracle     31234      30967     
> 1015811    oracle     31236      30967     
> 1081349    oracle     31708      1275      
> 1179656    oracle     31661      30967     
> 524300     oracle     16790      15410     
> 1900559    oracle     1315       29959     
> 1802260    oracle     1286       0         
> 1835029    oracle     1315       29959


简单说明：
cpid表示创建共享内存段进程的id
lpid表示上一次访问该共享内存的进程id

三、查看Oracle使用共享内存段使用情况
1、使用Oracle小工具查看共享内存段使用情况
Oracle软件装好之后自带一个工具（sysresv），可以清晰的看到每个实例的内存使用情况，默认安装在$ORACLE_HOME/bin目录下，以下是sysresv的输出情况

> [oracle@capaa ~]$ sysresv
> 
> IPC Resources for ORACLE_SID "xe" :
> Shared Memory:
> ID              KEY
> 524300          0x4cd26024
> Semaphores:
> ID              KEY
> 98304           0xc7ef5bb0
> 131073          0xc7ef5bb1
> 163842          0xc7ef5bb2
> Oracle Instance alive for sid "xe"

可以看到SID为xe的实例使用了shmid为524300的共享内存段
2、使用oradebug命令行查看共享内存段使用情况
1）连接到需要查看的数据库中
2）取得连接的os pid

> [oracle@capaa ~]$ ps -ef|grep LOCAL=YES
> oracle   16497 16496  0 13:57 ?        00:00:00 oraclexe (DESCRIPTION=(LOCAL=YES)(ADDRESS=(PROTOCOL=beq)))
> 3）根据os pid利用oradebug产生跟踪文件
> 
> SQL>  oradebug setospid 16497
> Statement processed.
> SQL> oradebug ipc
> Information written to trace file.
> SQL> oradebug tracefile_name
> /var/oracle/admin/xe/udump/xe_ora_16497.trc

4）打开跟踪文件可以看到该实例共享内存使用情况，其中shmid为524300

> [oracle@capaa ~]$ cat /var/oracle/admin/xe/udump/xe_ora_16497.trc
> /var/oracle/admin/xe/udump/xe_ora_16497.trc
> Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - 64bit Production
> With the Partitioning, OLAP and Data Mining options
> ORACLE_HOME = /var/oracle/product/10.2.0/db_1
> System name:    Linux
> Node name:      capaa
> Release:        2.6.18-53.el5xen
> Version:        #1 SMP Mon Nov 12 02:46:57 EST 2007
> Machine:        x86_64
> Instance name: xe
> Redo thread mounted by this instance: 1
> Oracle process number: 55
> Unix process pid: 16497, image: oracle@capaa (TNS V1-V3)
> 
> *** 2009-12-09 14:02:27.230
> *** SERVICE NAME:(SYS$USERS) 2009-12-09 14:02:27.171
> *** SESSION ID:(262.2351) 2009-12-09 14:02:27.171
> Dump of unix-generic skgm context
> areaflags            000000a7
> realmflags           0000000f
> mapsize              00001000
> protectsize          00001000
> lcmsize              00001000
> seglen               00001000
> largestsize  0000001000000000
> smallestsize 0000000000400000
> stacklimit     0x7fff2207dac0
> stackdir                   -1
> mode                      640
> magic                acc01ade
> Handle:            0x185252d0 `/var/oracle/product/10.2.0/db_1xe'
> Dump of unix-generic realm handle `/var/oracle/product/10.2.0/db_1xe', flags = 00000000
>  Area #0 `Fixed Size' containing Subareas 0-0
>   Total size 00000000001edba8 Minimum Subarea size 00000000
>    Area  Subarea    Shmid      Stable Addr      Actual Addr
>       0        0   524300 0x00000060000000 0x00000060000000
>                               Subarea size     Segment size
>                           00000000001ee000 0000000021802000
>  Area #1 `Variable Size' containing Subareas 2-2
>   Total size 0000000021000000 Minimum Subarea size 00400000
>    Area  Subarea    Shmid      Stable Addr      Actual Addr
>       1        2   524300 0x00000060800000 0x00000060800000
>                               Subarea size     Segment size
>                           0000000021000000 0000000021802000
>  Area #2 `Redo Buffers' containing Subareas 1-1
>   Total size 0000000000612000 Minimum Subarea size 00000000
>    Area  Subarea    Shmid      Stable Addr      Actual Addr
>       2        1   524300 0x000000601ee000 0x000000601ee000
>                               Subarea size     Segment size
>                           0000000000612000 0000000021802000
>  Area #3 `skgm overhead' containing Subareas 3-3
>   Total size 0000000000002000 Minimum Subarea size 00000000
>    Area  Subarea    Shmid      Stable Addr      Actual Addr
>       3        3   524300 0x00000081800000 0x00000081800000
>                               Subarea size     Segment size
>                           0000000000002000 0000000021802000
> Dump of Solaris-specific skgm context
> sharedmmu 00000000
> shareddec        0
> used region        0: start 0000000040000000 length 000000007fff80000000
> Maximum processes:               = 300
> Number of semaphores per set:    = 152
> Semaphores key overhead per set: = 4
> User Semaphores per set:         = 148
> Number of semaphore sets:        = 3
> Semaphore identifiers:           = 3
> Semaphore List=
> 98304
> -------------- system semaphore information -------------
> ------ Shared Memory Segments --------
> key        shmid      owner      perms      bytes      nattch     status      
> 0x00000000 917504     oracle    600        196608     2          dest         
> 0x00000000 950273     oracle    600        196608     2          dest         
> 0x00000000 983042     oracle    600        196608     2          dest         
> 0x00000000 1015811    oracle    600        196608     2          dest         
> 0x00000000 1081349    oracle    600        196608     2          dest         
> 0x00000000 1179656    oracle    600        196608     2          dest         
> 0x4cd26024 524300     oracle    640        562044928  55                      
> 0x00000000 1900559    oracle    600        12288      2          dest         
> 0x00000506 1802260    oracle    666        16384      0                       
> 0x00000000 1835029    oracle    600        196608     2          dest         
> ------ Semaphore Arrays --------
> key        semid      owner      perms      nsems     
> 0xc7ef5bb0 98304      oracle    640        152       
> 0xc7ef5bb1 131073     oracle    640        152       
> 0xc7ef5bb2 163842     oracle    640        152       
> ------ Message Queues --------
> key        msqid      owner      perms      used-bytes   messages   
> 
> 四、进一步研究
> 通过以上命令可以查看创建Oracle实例的初始进程
> [oracle@capaa ~]$ ipcs -mi 524300
> 
> Shared memory Segment shmid=524300
> uid=505 gid=505 cuid=505        cgid=505
> mode=0640       access_perms=0640
> bytes=562044928 lpid=19796      cpid=16790      nattch=54
> att_time=Wed Dec  9 14:10:26 2009  
> det_time=Wed Dec  9 14:10:35 2009  
> change_time=Fri Nov 27 15:58:44 2009  

通过cpid=16790 可以知道pid为16790启动了Oracle实例，该进程只负责启动Oracle实例，启动完成后通过“自杀”结束进程，可以从两方面得到验证
1）启动日志

> PMON started with pid=2, OS id=30345
> PSP0 started with pid=3, OS id=30347
> MMAN started with pid=4, OS id=30349
> DBW0 started with pid=5, OS id=30351
> LGWR started with pid=6, OS id=30353
> CKPT started with pid=7, OS id=30355
> SMON started with pid=8, OS id=30357
> RECO started with pid=9, OS id=30359
> CJQ0 started with pid=10, OS id=30361
> MMON started with pid=11, OS id=30363

2）从v$process表中查相关信息

> SQL> select addr,program from v$process where pid=1;
> 
> ADDR             PROGRAM
> ---------------- ------------------------------------------------
> 00000000817DA930 PSEUDO

PSEUDO表示该进程已被killed