Permalink
Browse files

first revision of code

  • Loading branch information...
0 parents commit 8b4e2ca9063c67d9bbc279d62ba1654b00a5ca18 @ardentperf committed Aug 19, 2011
Showing with 325 additions and 0 deletions.
  1. +21 −0 tracefile_cleanup.txt
  2. +251 −0 tracefile_find.txt
  3. +53 −0 tracefile_get.txt
@@ -0,0 +1,21 @@
+-- remove/cleanup tracefile_helper
+
+
+drop function textfile;
+drop type varchar2_array;
+
+drop function tracefile;
+
+drop function trace_file_scan;
+drop type trace_file_scan_impl;
+drop type trace_file_list;
+drop type trace_file_type;
+drop java source trace_file_helper;
+drop view all_trace_files;
+
+
+
+drop directory UDUMP;
+drop directory BDUMP;
+call dbms_java.revoke_permission('APP', 'java.io.FilePermission', '/u0001/app/oracle/admin/ardentp/udump', 'read');
+call dbms_java.revoke_permission('APP', 'java.io.FilePermission', '/u0001/app/oracle/admin/ardentp/bdump', 'read');
@@ -0,0 +1,251 @@
+-- java pipelined function to scan all tracefiles in a directory and get metadata from top 30 lines of each file
+
+CREATE TYPE trace_file_type AS OBJECT
+(
+ file_name VARCHAR2(400),
+ file_time DATE,
+ file_bytes NUMBER,
+ file_traceid VARCHAR2(100),
+ first_time DATE,
+ first_action VARCHAR2(100),
+ first_module VARCHAR2(100),
+ first_service VARCHAR2(100),
+ sid NUMBER,
+ serial NUMBER,
+ error VARCHAR2(400)
+);
+/
+
+create type trace_file_list as table of trace_file_type;
+/
+
+CREATE TYPE trace_file_scan_impl AS OBJECT
+(
+ key INTEGER,
+
+ STATIC FUNCTION ODCITableStart(sctx OUT trace_file_scan_impl, dir VARCHAR2, regex VARCHAR2)
+ RETURN NUMBER
+ AS LANGUAGE JAVA
+ NAME 'com.ardentperf.trace_file_helper.ODCITableStart(oracle.sql.STRUCT[], java.lang.String, java.lang.String) return java.math.BigDecimal',
+
+ MEMBER FUNCTION ODCITableFetch(self IN OUT trace_file_scan_impl, nrows IN NUMBER,
+ outSet OUT trace_file_list) RETURN NUMBER
+ AS LANGUAGE JAVA
+ NAME 'com.ardentperf.trace_file_helper.ODCITableFetch(java.math.BigDecimal, oracle.sql.ARRAY[]) return java.math.BigDecimal',
+
+ MEMBER FUNCTION ODCITableClose(self IN trace_file_scan_impl) RETURN NUMBER
+ AS LANGUAGE JAVA
+ NAME 'com.ardentperf.trace_file_helper.ODCITableClose() return java.math.BigDecimal'
+);
+/
+
+CREATE FUNCTION trace_file_scan(dir varchar2, regex VARCHAR2) RETURN trace_file_list
+PIPELINED USING trace_file_scan_impl;
+/
+
+
+set define off
+
+create and compile java source named trace_file_helper as
+package com.ardentperf;
+import java.io.*;
+import oracle.CartridgeServices.*;
+import java.math.BigDecimal;
+import oracle.sql.*;
+import java.sql.*;
+import java.util.*;
+import java.lang.*;
+import java.text.*;
+// stored context type
+
+public class StoredCtx
+{
+ Iterator files;
+ String search;
+ public StoredCtx(Iterator f, String s) { files=f; search=s; }
+}
+
+public class trace_file_helper implements SQLData
+{
+ private BigDecimal key;
+
+ final static BigDecimal SUCCESS = new BigDecimal(0);
+ final static BigDecimal ERROR = new BigDecimal(1);
+
+ // Implement SQLData interface.
+
+ String sql_type;
+ public String getSQLTypeName() throws SQLException
+ {
+ return sql_type;
+ }
+
+ public void readSQL(SQLInput stream, String typeName) throws SQLException
+ {
+ sql_type = typeName;
+ key = stream.readBigDecimal();
+ }
+
+ public void writeSQL(SQLOutput stream) throws SQLException
+ {
+ stream.writeBigDecimal(key);
+ }
+
+
+ // type methods implementing ODCITable interface
+
+ static public BigDecimal ODCITableStart(STRUCT[] sctx,String dir,String regex)
+ throws SQLException
+ {
+ Connection conn = DriverManager.getConnection("jdbc:default:connection:");
+
+ // create a stored context and store the file collection in it
+ File topdir=new File(dir);
+ StoredCtx ctx=new StoredCtx(Arrays.asList(topdir.listFiles()).iterator(),regex);
+
+ // register stored context with cartridge services
+ int key;
+ try {
+ key = ContextManager.setContext(ctx);
+ } catch (CountException ce) {
+ return ERROR;
+ }
+
+ // create a trace_file_scan_impl instance and store the key in it
+ Object[] impAttr = new Object[1];
+ impAttr[0] = new BigDecimal(key);
+ StructDescriptor sd = new StructDescriptor("TRACE_FILE_SCAN_IMPL",conn);
+ sctx[0] = new STRUCT(sd,conn,impAttr);
+
+ return SUCCESS;
+ }
+
+ public BigDecimal ODCITableFetch(BigDecimal nrows, ARRAY[] outSet)
+ throws SQLException
+ {
+ Connection conn = DriverManager.getConnection("jdbc:default:connection:");
+
+ // retrieve stored context using the key
+ StoredCtx ctx;
+ try {
+ ctx=(StoredCtx)ContextManager.getContext(key.intValue());
+ } catch (InvalidKeyException ik ) {
+ return ERROR;
+ }
+
+ // get the nrows parameter
+ int nrowsval = nrows.intValue();
+
+ // create a vector for the fetched rows
+ Vector v = new Vector(nrowsval);
+ int i=0;
+
+ StructDescriptor outDesc =
+ StructDescriptor.createDescriptor("TRACE_FILE_TYPE", conn);
+ Object[] out_attr = new Object[11];
+
+ while(nrowsval>0 && ctx.files.hasNext()){
+ File f = (File)ctx.files.next();
+ String fn = f.getName();
+ if(!fn.matches(ctx.search)) continue;
+ out_attr[0] = (Object)fn; // file_name
+ out_attr[1] = (Object)new Timestamp(f.lastModified()); // file_time
+ out_attr[2] = (Object)new BigDecimal(f.length()); // file_bytes
+ String[] fn_s = fn.split("_",4);
+ out_attr[3] = (Object)(fn_s.length==4?fn_s[3].substring(0,fn_s[3].length()-4):""); // file_traceid
+
+ // now we scan the file...
+ BufferedReader r;
+ try {
+ r=new BufferedReader(new FileReader(f));
+ try {
+ int nLines=0;
+ String line;
+ while(nLines++<30 && ((line=r.readLine())!=null)) {
+ if(line.startsWith("*** ACTION NAME:")) {
+ out_attr[5] = (Object)line.substring(17,line.indexOf(')')); // first_action
+ } else if(line.startsWith("*** MODULE NAME:")) {
+ out_attr[6] = (Object)line.substring(17,line.indexOf(')')); // first_module
+ } else if(line.startsWith("*** SERVICE NAME:")) {
+ out_attr[7] = (Object)line.substring(18,line.indexOf(')')); // first_service
+ } else if(line.startsWith("*** SESSION ID:")) {
+ out_attr[8] = (Object)new BigDecimal(line.substring(16,line.indexOf('.'))); // sid
+ out_attr[9] = (Object)new BigDecimal(line.substring(line.indexOf('.')+1,line.indexOf(')'))); // serial
+ } else if(line.matches(".* [0-9-]{10} [0-9:.]{12}$")) {
+ SimpleDateFormat df= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
+ Timestamp t=new Timestamp(df.parse(line.substring(line.length()-23)).getTime());
+ out_attr[4] = (Object)t; // first_time
+ } else if(line.startsWith("*** TRACE DUMP CONTINUED FROM FILE")) {
+ String new_fn = line.substring(35,line.length()-4);
+ r.close();
+ r=new BufferedReader(new FileReader(new_fn));
+ nLines=0;
+ }
+
+ }
+ } catch(IOException e) {
+ out_attr[10]=e.toString();
+ } catch(ParseException e) {
+ out_attr[10]=e.toString();
+ };
+
+ try {
+ r.close();
+ } catch(IOException e) {
+ out_attr[10]=e.toString();
+ };
+ } catch(FileNotFoundException e) {
+ out_attr[10]=e.toString();
+ }
+
+ v.add((Object)new STRUCT(outDesc, conn, out_attr));
+ i+=1;
+ nrowsval-=1;
+ }
+
+ // return if no files found
+ if(i==0) return SUCCESS;
+
+ // create the output ARRAY using the vector
+ Object out_arr[] = v.toArray();
+ ArrayDescriptor ad = new ArrayDescriptor("TRACE_FILE_LIST",conn);
+ outSet[0] = new ARRAY(ad,conn,out_arr);
+
+ return SUCCESS;
+ }
+
+ public BigDecimal ODCITableClose() throws SQLException {
+
+ // retrieve stored context using the key, and remove from ContextManager
+ StoredCtx ctx;
+ try {
+ ctx=(StoredCtx)ContextManager.clearContext(key.intValue());
+ } catch (InvalidKeyException ik ) {
+ return ERROR;
+ }
+
+ // any needed cleanup
+
+ return SUCCESS;
+ }
+}
+/
+
+
+
+create view all_trace_files as
+select 'UDUMP' directory, t.* from table(trace_file_scan('/u0001/app/oracle/admin/ardentp/udump','ardentp.*')) t
+UNION ALL
+select 'BDUMP' directory, t.* from table(trace_file_scan('/u0001/app/oracle/admin/ardentp/bdump','ardentp.*')) t;
+/
+
+
+
+--- permissions
+
+call dbms_java.grant_permission('DEVELOPER', 'java.io.FilePermission', '/u0001/app/oracle/admin/ardentp/udump', 'read');
+call dbms_java.grant_permission('DEVELOPER', 'java.io.FilePermission', '/u0001/app/oracle/admin/ardentp/bdump', 'read');
+create directory UDUMP as '/u0001/app/oracle/admin/ardentp/udump';
+create directory BDUMP as '/u0001/app/oracle/admin/ardentp/bdump';
+grant read on directory UDUMP to DEVELOPER;
+grant read on directory BDUMP to DEVELOPER;
@@ -0,0 +1,53 @@
+-- fast pipelined function to read a file
+-- originally writted by dion cho, jeremy schneider added dir_name parameter
+-- http://dioncho.wordpress.com/2009/03/19/another-way-to-use-trace-file/
+
+create type varchar2_array as table of varchar2(4000);
+/
+
+create function textfile(file_name in varchar2, dir_name in varchar2 default 'UDUMP')
+return varchar2_array
+pipelined
+as
+ v_handle utl_file.file_type;
+ v_line varchar2(4000);
+begin
+ v_handle := utl_file.fopen(dir_name, file_name, 'R', 4000);
+ loop
+ begin
+ utl_file.get_line(v_handle, v_line);
+ exception when no_data_found then
+ exit;
+ end;
+ pipe row(v_line);
+ end loop;
+ return;
+end;
+/
+
+
+-- function to get name of current tracefile (for currently logged in session)
+-- or to get name of tracefile for another currently logged in session SID
+--
+-- this one written by jeremy schneider
+-- with ideas from Ren� Nyffenegger
+-- http://www.adp-gmbh.ch/ora/misc/find_trace_file.html
+--
+-- you can get your current session's tracefile with this SQL:
+-- select * from table(textfile(tracefile));
+
+create function tracefile(p_sid number default userenv('sid'))
+return varchar2
+as
+ v_instance varchar2(400);
+ v_pid varchar2(40);
+ v_trace_id varchar2(400);
+begin
+ select value into v_instance from v$parameter where name = 'instance_name';
+ select spid, nvl2(traceid, '_'||traceid, null) into v_pid, v_trace_id from v$process p, v$session s
+ where p.addr = s.paddr and s.sid = p_sid;
+ return v_instance || '_ora_' || v_pid || v_trace_id || '.trc';
+end;
+/
+
+

0 comments on commit 8b4e2ca

Please sign in to comment.