Vous êtes sur la page 1sur 11

PL/SQL Hierarchical Profiler Oracle Database 11G release 2 (11.2) | D...

1 of 11

http://dbaora.com/plsql-hierarchical-profiler-oracle-database-11g-release...

PL/SQL Hierarchical Profiler Oracle Database 11G


release 2 (11.2)
Posted on January 14, 2014
With 11g release 1 Oracle has introduced new package DBMS_HPROF. Its very useful for DBAs and developers to
analyze execution of PL/SQL code. It enables to gather, store and organize a lot of information about executed code.
Informations gathered by this package are supported by Oracle sqldeveloper which is another great news :).
The PL/SQL hierarchical profiler features:
easy to install and use
collect execution times for SQL and PL/SQL code
keeps results in database tables
Provides subprogram-level execution summary information, such as:
Number of calls to the subprogram
Time spent in the subprogram itself
Time spent in the subprogram itself and in its descendents subprograms
Detailed parent-children information, for example:
All callers of a given subprogram (parents)
All subprograms that a given subprogram called (children)
How much time was spent in subprogram x when called from y
How many calls to subprogram x were from y
Installation
Installation consists of three steps
1. Grant execute privilege on package DBMS_HPROF to a database user

grant execute on DBMS_HPROF to tomasz;

2. Grant read, write privilege on a directory object to a database user

3/29/2015 6:32 PM

PL/SQL Hierarchical Profiler Oracle Database 11G release 2 (11.2) | D...

2 of 11

http://dbaora.com/plsql-hierarchical-profiler-oracle-database-11g-release...

NOTE Oracle sqldeveloper as default use directory plshprof_dir for profiler so I use the name to be consistent with
Oracle sqldeveloper

connect / as sysdba
create directory plshprof_dir as 'c:\temp';
grant read, write on directory plshprof_dir to tomasz;

3. Create hierarchical profiler tables in user schema. Call script dbmshptab.sql from $ORACLE_HOME.

connect tomasz@ora11g
@?/rdbms/admin/dbmshptab.sql

Verification
New objects are created in user schema

select object_name, object_type


from user_objects
where object_name like 'DBMSH%'
order by object_name;
OBJECT_NAME
--------------------------DBMSHP_FUNCTION_INFO
DBMSHP_PARENT_CHILD_INFO
DBMSHP_RUNNUMBER
DBMSHP_RUNS

OBJECT_TYPE
------------------TABLE
TABLE
SEQUENCE
TABLE

Table

Description

DBMSHP_RUNS

Contains information about runs

DBMSHP_FUNCTION_INFO

Contains information about each profiled function

DBMSHP_PARENT_CHILD_INFO

Contains parent-child relation for profiled functions

Testing
First we need to create test code

3/29/2015 6:32 PM

PL/SQL Hierarchical Profiler Oracle Database 11G release 2 (11.2) | D...

3 of 11

http://dbaora.com/plsql-hierarchical-profiler-oracle-database-11g-release...

create or replace package test_pkg


is
procedure test_1_prc;
procedure test_2_prc;
procedure test_3_prc;
end;
/
create or replace package body test_pkg
is
procedure test_1_prc
is
begin
test_2_prc;
end;
procedure test_2_prc
is
v_cnt number;
begin
test_3_prc;
for i in 1..5
loop
select count(*)
into v_cnt
from user_objects;
end loop;
end;
procedure test_3_prc
is
v_cnt number;
begin
for i in 1..5
loop
select count(*)
into v_cnt
from user_objects;
end loop;
end;
end;
/

Now we can run hierarchical profiler. It will create file with collected statistics. File is created in directory specified by

3/29/2015 6:32 PM

PL/SQL Hierarchical Profiler Oracle Database 11G release 2 (11.2) | D...

4 of 11

http://dbaora.com/plsql-hierarchical-profiler-oracle-database-11g-release...

parameter location.

begin
dbms_hprof.start_profiling
( location => 'PLSHPROF_DIR',
filename => 'plshprof_file.txt' );
test_pkg.test_1_prc;
dbms_hprof.stop_profiling;
end;
/

Here is part of the generated file. Remember it has been created in directory PLSHPROF_DIR so you need to have access
to review it.

P#V
P#!
P#C
P#X
P#C
P#X
P#C
P#X
P#C
P#X
P#R
P#X
P#C
P#X
P#R

PLSHPROF Internal Version 1.0


PL/SQL Timer Started
PLSQL."TOMASZ"."TEST_PKG"::11."TEST_1_PRC"#980980e97e42f8ec #3
2
PLSQL."TOMASZ"."TEST_PKG"::11."TEST_2_PRC"#980980e97e42f8ec #9
2
PLSQL."TOMASZ"."TEST_PKG"::11."TEST_3_PRC"#980980e97e42f8ec #23
15
SQL."TOMASZ"."TEST_PKG"::11."__static_sql_exec_line29" #29
67470
48
SQL."TOMASZ"."TEST_PKG"::11."__static_sql_exec_line29" #29
43162

...
P#C
P#X
P#R
P#X
P#C
P#X
P#R
P#X
P#R

SQL."TOMASZ"."TEST_PKG"::11."__static_sql_exec_line17" #17
37831
17
SQL."TOMASZ"."TEST_PKG"::11."__static_sql_exec_line17" #17
40269
11

3/29/2015 6:32 PM

PL/SQL Hierarchical Profiler Oracle Database 11G release 2 (11.2) | D...

5 of 11

http://dbaora.com/plsql-hierarchical-profiler-oracle-database-11g-release...

P#X 1
P#R
P#C PLSQL."SYS"."DBMS_HPROF"::11."STOP_PROFILING"#980980e97e42f8ec #59
P#R
P#! PL/SQL Timer Stopped

These are meanings for prefixes in the file


P#V PLSHPROF banner with version number
P#C call to subprogram (call event)
P#R return from subprogram
P#X elapsed time between preceding and following events
P#! Comment
Instead of reviewing file you can analyze it and load to Oracle profiler tables. The code is returning on display run identifier
which will be used later for reviewing data in profiler tables.

set serveroutput on
declare
v_runid number;
begin
v_runid := dbms_hprof.analyze
( location
=> 'PLSHPROF_DIR',
filename
=> 'plshprof_file.txt',
run_comment => 'First run' );
dbms_output.put_line('Run id: '||v_runid);
end;
/
anonymous block completed
Run id: 1

more details about parameters

DBMS_HPROF.ANALYZE (
location
VARCHAR2,
filename
VARCHAR2,
summary_mode
BOOLEAN
trace
VARCHAR2
skip
PLS_INTEGER
collect
PLS_INTEGER

DEFAULT
DEFAULT
DEFAULT
DEFAULT

FALSE,
NULL,
0,
NULL,

3/29/2015 6:32 PM

PL/SQL Hierarchical Profiler Oracle Database 11G release 2 (11.2) | D...

6 of 11

run_comment
RETURN NUMBER;

VARCHAR2

http://dbaora.com/plsql-hierarchical-profiler-oracle-database-11g-release...

DEFAULT NULL)

Parameters description
location directory for profiler data
filename file name with profiler data
summary_mode default FALSE detailed analysis are done. If TRUE only top-level analysis are done
trace analyze only specified part of code. As default is NULL so analyzes are done for entire run. The trace entry
must be specified in a special quoted qualified format (including the schema name, module name & function name) as
in for example, SCOTT.PKG.FOO or ..__plsql_vm. If multiple overloads exist for the specified name, all of
them will be analyzed.
skip used only if trace parameter is specified. Specifies how much first invocations are skipped. The default is 0.
collect used only if trace is specified. Specified how much invocations are traced. Starts from skip+1th invocation.
The default is 1.
run_comment user comment
Review data in tables
DBMSHP_RUNS shows in micro seconds how much time it took to complete our procedure

select runid, total_elapsed_time, run_comment


from dbmshp_runs
where runid=1;
RUNID TOTAL_ELAPSED_TIME RUN_COMMENT
------ ------------------ -----------1
5392417 First run

DBMSHP_FUNCTION_INFO shows aggregated information for each profiled function. For example you can find out how
much time was consumed by a function or subprogram calls from the function.

select
owner, module, type, function, line#, namespace, calls,
function_elapsed_time, subtree_elapsed_time
from dbmshp_function_info
where runid=1;

OWNER MODULE
TYPE
FUNCTION
LINE# NAMESPACE CALLS FUNCTION_ELAPSED
------ ---------- ------------ ------------------------ ----- --------- ----- ---------------SYS
DBMS_HPROF PACKAGE BODY STOP_PROFILING
59 PLSQL
1

3/29/2015 6:32 PM

PL/SQL Hierarchical Profiler Oracle Database 11G release 2 (11.2) | D...

7 of 11

SYS
TOMASZ
TOMASZ
TOMASZ
TOMASZ
TOMASZ

DBMS_LOCK
TEST_PKG
TEST_PKG
TEST_PKG
TEST_PKG
TEST_PKG

PACKAGE
PACKAGE
PACKAGE
PACKAGE
PACKAGE
PACKAGE

BODY
BODY
BODY
BODY
BODY
BODY

http://dbaora.com/plsql-hierarchical-profiler-oracle-database-11g-release...

SLEEP
TEST_1_PRC
TEST_2_PRC
TEST_3_PRC
__static_sql_exec_line17
__static_sql_exec_line29

197
3
9
23
17
29

PLSQL
PLSQL
PLSQL
PLSQL
SQL
SQL

5
1
1
1
5
5

49

1
2

DBMSHP_PARENT_CHILD_INFO table enables to create tree of execution for profiled code

select
lpad(' ', level, ' ')||pf.function||' -> '||cf.function call,
pc.subtree_elapsed_time,
pc.function_elapsed_time,
pc.calls,
cf.line#
from
(select * from dbmshp_parent_child_info where runid=1) pc,
dbmshp_function_info pf,
dbmshp_function_info cf
where pc.runid=pf.runid
and pc.parentsymid=pf.symbolid
and pc.runid=cf.runid
and pc.childsymid=cf.symbolid
connect by prior pc.childsymid=pc.parentsymid
start with pc.parentsymid = 3;

CALL
SUBTREE_ELAPSED_TIME FUNCTION_ELAPSED_TIME
CAL
------------------------------------------ -------------------- --------------------- -------TEST_1_PRC -> TEST_2_PRC
5412750
93
TEST_2_PRC -> TEST_3_PRC
5228468
190
TEST_3_PRC -> SLEEP
4971896
4971896
TEST_3_PRC -> __static_sql_exec_line29
256382
256382
TEST_2_PRC -> __static_sql_exec_line17
184189
184189

plshprof tool
Another option to analyze your profiled code is to use command line utility plshprof.
It generates simple HTML reports directly from the raw profiler data, so can be used instead of loading data into Oracle
tables.
$ORACLE_HOME\bin\plshprof
PLSHPROF: Oracle Database 12c Enterprise Edition Release 12.1.0.1.0 64bit Production

3/29/2015 6:32 PM

PL/SQL Hierarchical Profiler Oracle Database 11G release 2 (11.2) | D...

8 of 11

http://dbaora.com/plsql-hierarchical-profiler-oracle-database-11g-release...

Usage: plshprof [<option>] <tracefile1> [<tracefile2>]


Options:
-trace <symbol>
-skip <count>

(no default)
(default=0)

-collect <count> (default=1)

specify function name of tree root


skip first <count> invokations
collect info for <count> invokations

-output <filename> (default=<symbol>.html or <tracefile1>.html)


-summary

print time only

plshprof c:\temp\plshprof_file.txt
PLSHPROF: Oracle Database 12c
Enterprise Edition Release 12.1.0.1.0 - 64bit Production
[7 symbols processed]
[Report written to 'c:\temp\plshprof_file.txt.html']

You can review output in browser to get details. Its much easier then review data from tables.
Example screens

3/29/2015 6:32 PM

PL/SQL Hierarchical Profiler Oracle Database 11G release 2 (11.2) | D...

9 of 11

http://dbaora.com/plsql-hierarchical-profiler-oracle-database-11g-release...

Sqldeveloper support
Sqldeveloper fully supports PL/SQL Hierarchical profiler and with a few simple clicks you can quickly get profiler data for
your code.
You just need to select code that you want to profile and select Profile option. There are some extra screen (Im not
showing it here) where you can define more details what to profile. Once data are collected you can see it in tab Profiles
for your procedure, function, package etc.
Sqldeveloper requires to create profiler tables in own schema and expects directory plshprof_dir. You are informed about
the requirements when you first try to profile your data.

3/29/2015 6:32 PM

PL/SQL Hierarchical Profiler Oracle Database 11G release 2 (11.2) | D...

10 of 11

http://dbaora.com/plsql-hierarchical-profiler-oracle-database-11g-release...

Have a fun
Tomasz

LIK E TH IS :

3/29/2015 6:32 PM

PL/SQL Hierarchical Profiler Oracle Database 11G release 2 (11.2) | D...

11 of 11

http://dbaora.com/plsql-hierarchical-profiler-oracle-database-11g-release...

Be the first to like this.

This entry was posted in Uncategorized by admin. Bookmark the permalink [http://dbaora.com/plsql-hierarchicalprofiler-oracle-database-11g-release-2-11-2/] .

3/29/2015 6:32 PM

Vous aimerez peut-être aussi