Appl. Math. Inf. Sci. 7, No. ?, 1-12 (2013) / www.naturalspublishing.com/Journals.asp 3
Algorithm 1 Monitoring CPU utilization
Input:
OSGi-based applications;
Output:
CPU utilization for every bundle;
1: if Listen to the event(bundle b is initialized) then
2: Set the bundle ID of thread t as b, and the time stamp for
t ;
3: end if
4: if Listen to the event(thread ts is initialized) then
5: Get the current thread tp which is the parent of ts, and the
bundle ID of tp as bp;
6: Create a new thread ts, and Set the bundleID of ts as bp;
7: end if
8: if Listen to the event(service s
i
invoke service s
j
) then
9: Get the bundle ID of s
i
as b
i
, and the bundle ID of s
j
as
b
j
;
10: if b
i
! = b
j
then
11: Get bundleIn fo
i
of b
i
, and current thread t;
12: Calculate bundleInf o
i
.CPUTime+ =
t.CalculateCPU();
13: Set time stamp for t, and bundle ID of thread t as b
j
;
14: Execute s
j
;
15: Get bundleIn fo
j
of b
j
;
16: Calculate bundleInf o
j
.CPUTime+ =
t.CalculateCPU();
17: Set time stamp for t, and bundle ID of t as b
i
;
18: end if
19: end if
2.1.2 Monitoring static method invocation
Although the OSGi specification recommends developers
to implement the interactions between components with
service invocation, some developers used to invoke the
functions from other components with the traditional
static method invocation. Thus we propose an AOP
(Aspect-Oriented Programming) based method [
10] to
trace the thread transfer between components. The OSGi
framework analyzes the meta data file recording the
exported packages automatically, when a bundle is loaded
dynamically. We extend the original OSGi framework, so
that it reports the exported methods to our monitoring tool
when the analysis is finished. Our tool decides whether
the classes being loaded are exported by the bundle
according to the report. Then, we use AOP to insert the
monitoring points into the beginning and the end of the
public method in the class which exports methods.
We note that some exported packages are not invoked
by other components. The thread transfer does not
happen, when the invoking method and the invoked
method in the same bundle. However, the redundant
monitoring points introduce unnecessary overhead.
Therefore, we use a static code analysis method to reduce
the number of monitoring points before weaving class.
Method invocations usually take the form of
”targetObj.methodName(parameters)”; the key to
analyzing the calling relationship is to know the possible
6HUYLFH3URYLGHU%XQGOH
26*L
6HUYLFH5HJLVWU\
5HJLVWHU
*HQHUDWH
6HUYLFH 6HUYLFH
6HUYLFH3UR[\%XQGOH
63 63
6HUYLFH3URYLGHU%XQGOH
26*L
6HUYLFH5HJLVWU\
5HTXLUH
6HUYLFH
)LQG
6HUYLFH 6HUYLFH
6HUYLFH3UR[\%XQGOH
63 63
6HUYLFH
5HTXHVWHU
5HVSRQG63
,QYRNH63
DZKHQDEXQGOHLVORDGHGDQGDVHUYLFHLVUHJLVWHUHG
EZKHQDVHUYLFHLVUHTXLUHG
,QYRNH6
Fig. 1: Service proxy generation
types of the objects which the targetObj may point to. We
use the class hierarchy analysis method [
11] to gain this
knowledge. All the subtypes of targetObj’s type are
among the possible types, and we can get all the possible
target methods denoted by the methodName. With the
knowledge of the target methods in every invocation
statement, we can easily acquire the calling relationship
between methods, classes, and packages. If two packages
in two different bundles have the calling relationships in
OSGi, the corresponding imported and exported packages
should be specified in the metadata files of the two
bundles. Thus we do not weave the methods invoked in
the same bundle to reduce monitoring overhead.
The object of our AOP based method is to trace the
thread transfer between components, when a component
invokes the others with the static method invocation.
2.2 Monitoring memory utilization of a bundle
The system memory is occupied by objects, which we
will categorize into different bundles. We can also use the
JVMTI to tag every object and calculate its size in
memory. The objects created by different bundles are
tagged with bundle IDs, and thus we calculate the
memory utilization for every bundle. However, how to
distinguish which bundle the objects are subordinate to is
a key problem. With the algorithm 1, we get the relations
between bundles and threads in different periods, and
objects are created by threads. Thus we take the thread as
a bridge between objects and bundles to locate the objects
belonging to different bundles.
In essence, the CPU monitoring and memory
monitoring are both to locate the units in their bundles.
The differences are as follows: 1) the memory utilization
c
2013 NSP
Natural Sciences Publishing Cor.