寫了個分析library_cache dump的AWK,有興趣的可以搞搞

westzq1984發表於2010-01-06

昨天無聊寫的,AWK偶只是會,不精通,寫的東西也比較爛

基於level 16的library_cache dump。發現其依賴關係。

沒什麼實際意義,只是想練習下AWK

輸出大概是這樣的

*******************************************************************************************
25ca5a80        CRSR    SELECT * FROM V$SQLAREA WHERE SQL_TEXT LIKE '%smal
  1--"25cf9d68","CRSR","CHILD_CURSOR","2","25d90710"
    2--"25d90710","TABL","PUBLIC.V$SQLAREA ","2","25f03bc4"
      3--"25f03bc4","TABL","SYS.V_$SQLAREA ","2","25dd9918"
        4--"25dd9918","TABL","SYS.V$SQLAREA ","",""
  1--"25cf9d68","CRSR","CHILD_CURSOR","2","25f03bc4"
    2--"25f03bc4","TABL","SYS.V_$SQLAREA ","2","25dd9918"
      3--"25dd9918","TABL","SYS.V$SQLAREA ","",""
  1--"25cf9d68","CRSR","CHILD_CURSOR","2","25ce85e8"
    2--"25ce85e8","TABL","CTAIS2.V$SQLAREA ","",""

#!/bin/awk
function addToArray(value,a){
  if(a==1){FULLEXTARR[i]=value;i++ }
  if(a==2){HANDLETEXTARR[j]=value;j++}
  if(a==3){ANALYZETEXTARR[k]=value;k++}
  if(a==4){FLAGARR[m]=value;m++}
}

function printDepend(handle,level){
  for(y in FULLEXTARR){
    split(FULLEXTARR[y],array,"\",\"");
    TEMPTEXT1=array[1];TEMPTEXT2=array[5];
    gsub(/\"/,"",TEMPTEXT1);gsub(/\"/,"",TEMPTEXT2)
    if (handle==TEMPTEXT1){
      TMP="";for (z=1;z<=level;z++){ TMP=TMP"  " }
      print TMP""level"--"FULLEXTARR[y];
      if(TEMPTEXT2!=""){
        printDepend(TEMPTEXT2,level+1);
      }
    }
  }
}

BEGIN{
  FLAG=0;#±êʶ¶ÁHANDLEµÄ¿ªÊ¼£¬0ÎÞHANDLE,1¼ÌÐø¶ÁHANDLE
  SEQ="";#ÓÃÀ´Ê¶±ðÏÂÒ»ÐÐÊÇ·ñΪHANDLE
  TYPE=0;
  SPLIT=0;#ÓÃÓÚ±êʶ¶ÁHANDLE£¬1¿ªÊ¼¶Á£¬0½áÊø¶Á
  i=1;#FULLTEXTARRÊý×é´óС
  j=1;#HANDLETEXTARRÊý×é´óС
  k=1;#ANALYZETEXTARRÊý×é´óС
  m=1;#FLAGARRÊý¾Ý´óС
}
{
  if($0~"LIBRARY OBJECT HANDLE:"){
     if (SPLIT==1){
       TEXT="\""HANDLE"\",\""NS"\",\""NAME"\",\"\",\"\"" ;addToArray(TEXT,1);
     }
     SPLIT=1
     HANDLE="";NAME="";NS="";
     gsub(/handle=/,"",$4);
     HANDLE=$4;
  }else if($0~"name="){
    if(length($0)==7){ getline;NAME=$0 }else{ gsub(/  name=/,"",$0);NAME=$0 }
    NAME=substr(NAME,1,50)
  }else if($0~"namespace="){
    split($1,array,"=");NS=array[2]
    TEXT=HANDLE"\t"NS"\t"NAME;addToArray(TEXT,3);addToArray(HANDLE,4);
  }else if($0~"child#    table reference   handle" || $0~"dependency#    table reference   handle position flags"){
    if($0~"child#    table reference   handle"){ TYPE=1 } else { TYPE=2 }
    if(NAME==""){ NAME="CHILD_CURSOR" }
    getline;getline;
    TEXT= "\""HANDLE"\",\""NS"\",\""NAME"\",\""TYPE"\",\""$4"\"";addToArray(TEXT,1);
    addToArray($4,2);addToArray("HANDLE_FLAG:"$4,3);addToArray(HANDLE,4);
    FLAG=1;SEQ=0; getline;
    if($1==SEQ+1){
      TEXT= "\""HANDLE"\",\""NS"\",\""NAME"\",\""TYPE"\",\""$4"\"";addToArray(TEXT,1);
      addToArray($4,2);addToArray("HANDLE_FLAG:"$4,3);addToArray(HANDLE,4);
      SEQ=SEQ+1;
    }else{
      FLAG=0;TYPE=0;SEQ="";SPLIT=0;
    }       
  }else if(FLAG==1){
    if(NAME==""){ NAME="CHILD_CURSOR" }
    if($1==SEQ+1){
      TEXT= "\""HANDLE"\",\""NS"\",\""NAME"\",\""TYPE"\",\""$4"\"";addToArray(TEXT,1);
      addToArray($4,2);addToArray("HANDLE_FLAG:"$4,3);addToArray(HANDLE,4);
    }else{
      FLAG=0;TYPE=0;SEQ="";SPLIT=0;
    }
  }
}
END{
#  print i;
#  print j;
#  print k;
#  print m;
#  for (x in FULLEXTARR){print FULLEXTARR[x]}
#  for (x in HANDLETEXTARR){print HANDLETEXTARR[x]}
#  for (x in ANALYZETEXTARR){print ANALYZETEXTARR[x]} 
#  for (x in FLAGARR){print FLAGARR[x]} 
  for(x=1;x<=m;x++){
    for(y in HANDLETEXTARR){
      if(FLAGARR[x] == HANDLETEXTARR[y]){
        ANALYZETEXTARR[x]="";
        continue;
      }
    }
  }
 
  for(x=1;x<=k;x++){
    if(ANALYZETEXTARR[x]!="" && ANALYZETEXTARR[x]!~"HANDLE_FLAG:"){
      print "*******************************************************************************************"
      print ANALYZETEXTARR[x]
    }else if(ANALYZETEXTARR[x]!="" && ANALYZETEXTARR[x]~"HANDLE_FLAG:"){
      TEXT=ANALYZETEXTARR[x];
      gsub(/HANDLE_FLAG:/,"",TEXT);
      printDepend(TEXT,1);
    }
  }
}

 

 

 

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

相關文章