From the Topo chains on the directed graph, we built the memory evaluation function to find the best Topo chain which has the smallest amount of occupied memory.. Keywords: Embedded soft
Trang 1Embedded Software Memory Optimization Based on the DSL and
Topological Sort
Pham Van Huong, Nguyen Ngoc Binh and Pham Ngoc Thanh University of Engineering and Technology, Vietnam National University, Hanoi, Vietnam huongpv@gmail.com, nnbinh@vnu.edu.vn and ngocthanh2207@gmail.com
ABSTRACT
Nowadays, the powerful development of embedded software, the optimizing embedded software play an important role The evaluation and optimization in the design phase of embedded software brings many benefits In this paper, we propose a new method to optimize the occupied memory of embedded software in the design phase based on DSL, T4 and Topological sort A program is a chain of Tasks and the relationship between each Task The program is specified by the dependence graph that is a directed graph Each node in directed graph describes a Task, each Task consists of specification information such as name, input, output Each edge describes the relationship between two Tasks The programs working by order of Task in the different Topo chain does not change the result but the occupied memory and performances are difference From the dependence graph, we can find many Topo chains, and each of them will have amount of occupied memory in difference From the Topo chains on the directed graph, we built the memory evaluation function to find the best Topo chain which has the smallest amount of occupied memory
Keywords: Embedded software optimization, DSL – domain specific language, T4 – text template transformation
toolkit, topological sort, topo chain, chain of tasks, dependence graph
1 INTRODUCTION
In the development trend of information technology, embedded technology is very important The embedded system which has strongly developed appears in almost fields of social life Along to the hardware technology, embedded software engineering also has been researched and deeply developed The embedded devices are limited in CPU, memory space, battery life [1, 2] Thus, the research on optimize embedded software is especially important
Optimization problem consists design level optimization, code optimization, compiler optimization and environment optimization (co-optimization – hardware-software, assembly instruction set optimization), time execution optimization [1, 2, 3, 4, 5] Although optimization in design phase is new approach and it faces many challenges, it brings more benefits than behind phase optimization method This method is often based model driven software engineering There
are some researches in the design phase such as the research [1, 2] on “performance optimization on mobile trade-off
with the battery life time based on model driven engineering” It developed a specific domain language model, and he
base on Eclipse framework to develop mobile software structure, generate code simulating functions and run it to
evaluate performance and balance the battery life time The research [5, 6] proposed “Mobile application optimization
based on model driven engineering and code generation for production lines”
On the other hand, software engineering base DSL has been widely developed, especially, in specific domain such as embedded software, embedded systems [8, 9] Moreover, since the definition of DSL and meta-model based on XML, it has high transformation and strong code generation Thus, in this paper, we will propose and implement the memory optimization method for the embedded software based on Topological sort, DSL and T4 Firstly we define the Domain Specific Language and build meta-model for modeling embedded software The embedded software is specified by directed graph, it consists of a set of Tasks and the relationship among Tasks From this graph, we use the T4 to generate code from the model and to transform to mathematical expression of the graph From the dependence graph, the Topological sort algorithm will find different Topo chains Each Topo chain describes the order of Tasks executed in embedded software Executing the program by any Topo chains also reaches the same result but there are different sizes
of occupied memory Then, we build the occupied memory evaluation function for each Topo chain to find the best
chain The rest of paper includes following parts: Section 2 – Optimizing memory embedded software based on
Trang 2caIcSizeO
myabj = new Object()
main ID
int y
static data code
Topological sort; Section 3 – Develop the framework of DSL, T4 and implement the program that supports optimizing;
Section 4 – Experimental; Section 5 – Conclusion and future work
2 OPTIMIZING MEMORY OF EMBEDDED SOFTWARE BASED ON TOPOLOGICAL SORT 2.1 Dependence Graph of Embedded Software
Normally, the structure of the programs have only the function main() as a entry point Other functions are executed and called from the function main() With the object oriented programming language, to implement the program that able
to be executed directly, the program need to have a single class that contains the function main() that is declared by
public and static So we can describe the program design with a kind of dependent graph, in which the node represents a
Task, and the edge represents a relationship between two Tasks Each Task will be implemented as a function in the later phase and it includes name, return type and parameter list Tasks are dependent or independent Thus, program is represented by the dependence graph of Task and defined by the formalism (1)
G = {T, E} with T = {t | t is the node of graph} and E = {e | e = (i, j) is the edge from t i to t j} (1)
where:
• Each node t i corresponds to an Task i
• Each edge e = (i, j) shows that Task j is only executed when Task i finishes
2.2 Topological Sort on the Dependence Graph
As described in the previous section, the program includes a set of Tasks and it is represented by the dependence graph With the same set of Tasks but different execution order will affect to the amount of occupied memory and performance Also from this Task set, there are many different implementations by the different orders of Tasks that satisfy the dependency graph and these implementations do not change the program execution result This is the Topological order on a directed graph Therefore, from the dependence graph, we can find many ways of program execution and each Topo chain presents an execution way
2.3 The Memory Evaluation Function of the Topo Chain
From the dependence graph, there are many Topo chains presenting the different execution order of Tasks in the program Thus, to evaluate the Topo chain that has the best effectiveness of memory usage, in this section we will build the evaluation function that is used to calculate the amount of occupied memory when executing the program under that Topo chain Firstly, we need analysis the process of memory allocation and layout of memory in the program execution Figure 1 shows the layout of memory during program execution, as follows: static memory space is allocated when the program is loaded from the secondary memory and it contains the static data and source code; the stack memory space is used to store local variables, parameters for each function and, after the function ends, the memory frame allocated for the function is recovered; the heap memory space contains elements allocated and recovered dynamically in implementing the program and it contains objects created With the program that consists of a main program and sub functions, the process of automatic allocation and automatic release of memory during execution time is shown in Figure
2
Suppose that the program has N Tasks and t i is the Task i in the Topo chain Task i has the returned data type r i
When executing t i , local variables and parameters of t i will be allocated in the stack memory space and it will be released
when t i returns The total of used memory size in the execution of Task chain is the same in the all of Topo chains
Figure 1 Layout of memory in the program execution
Trang 3Higher memory Frame
for
main( )
Lower memory
Frame
f or maint ) Frame
f or
a ( )
main°
calls a()
Frame for main() Frame for
a()
Frame for
b ( )
a0 calls bQ
Frame
f or main( ) Frame for a ( )
return framb()
Frame
f or main( ) Frame for a( ) Frame for c ( )
a() calls cQ
Frame for main( ) Frame for a ( )
ueturn foam
cQ
Frame
for
main()
return from
aU
1
GetFeature:+«ay
GetTOta I C`C h a rs EvaluateCcrrec:Pecen:
-etNumOfCharNotInDIC
DispIayCcrrec:Percer:
Di sp I ayTctalOfChar DisplayNumC`CharIncorrect
Figure 2 The Stack memory space in the program execution
After the Task is done, the returned result must be stored in the local variables of the main function These variables
are allocated in the stack memory when they are declared and assigned the return value of the Task These variables are released when the program finishes And, although the execution time of a Task does not depend on the location of the Task in the Topo chain but the resident time of local variables will affect to the amount of occupied memory So with the different Topo chains, the amount of memory occupied are also different Therefore without loss of generality, we
assume each task will be done in a unit time Then, to store returned result from Task i, we need to declare a local variable of the main function and allocate memory for this variable to store the result of Task i And this variable will be
resident in memory until the main function finishes Therefore, the amount of occupied memory of the variable depends
on the type of data returned by Task i and the order of Task So, the amount of stack memory occupied by t i is equal (N - i) * size return type of Task i From analysis of occupied memory caused by each Task, we built the occupied memory
amount evaluation function in an execution under the Topo chain, such as formula (2):
= ∑ ( – ) ∗ size( ) (2)
where:
• F m is the evaluation function of the amount of memory occupied
• N is the number of Tasks of the program
• r i is the data type returned by Task i
Figure 3 Task flow of the Nôm character recognition module
Trang 42.4 Select the Optimal Topo Chain
When the program is executed under any Topo chain of Tasks, the total memory of size used by the program is the same but the resident time in memory is different Formula (2) is used to calculate the amount of occupied memory
during the program execution on a Topo chain Topo chain was chosen as the chain having minimal F m To find the best Topo chain, we implement a simple algorithm: get the input is a set of Topo chains in Section II.B; browser and calculate
F m for each chain, and select the best Topo chain on which the F m is minimal
The algorithm used to finds a Topo chain from directed graph [7] have polynomial complexity O (|T| + |E|) [7], with
T is the set of Tasks, E is the set of edges in the definition (1) However, to find all Topo chains in the set T, the complexity is O (|T|!) Here, we propose the algorithm based on the main idea such as an order chain satisfies the set of edge E is a Topo chain The algorithm is as follows:
For each c i chain in the factorial of N chains { Assign the variable isTopo to true For each egde e j,k in the set E {
If t k is front of t j then {
break
} }
If isTopo is equal true{
Add c i to the set of Topo chain }
}
3 DEVELOPING A DSL AND T4 FRAMEWORK FOR OPTIMIZATION
To implement the methodology of memory optimization based on Topological sort mentioned in the section 2, we will build a framework that enables to specify and design a model of the dependence graph of Tasks based on DSL Then
we build the T4 text template to transfer to the mathematical expression of the dependence graph from the model of the dependence graph Then we implement the program to generate all Topo chain of Task and select the optimal Topo chain which has the smallest occupied memory during the execution time of the program
3.1 Define a DSL and Build a Meta-Model File
To visually specify a model of a dependence graph, firstly we define a Domain Specific Language named by OMTS that stands for Optimizing Memory base on Topological Sort Then, based on the Visual Studio.NET 2008 SDK, we built the meta-model of OMTS With the meta-model built, designers can create a model of a dependence graph in the
graphical interface The main purpose of building DSL is to model the dependence graph And from the model, we can
automatically optimize The process of DSL definition and meta-model construction includes the following steps:
• Define the logical components: Process Class, Task class, Comment class, Rules, Constrains, etc
• Define shape symbols corresponding to each logical component above The symbols will be used to design models in the graphical interface after the DSL was compiled and deployed
• Create the meta-model formatted by XML file to define and store the mapping between the logical components and shape symbols
3.2 Transformation to Mathematical Expression of the Dependence Graph From Model
To do optimization automatically after designing the model of a dependence graph, we define the T4 Text template
to transform automatically to mathematical expression from the model of the dependence graph of the Tasks based T4 technology T4 is a powerful code generation technology, which allows automatic code generation based on the XML file of the metamodel and the XML file of the actual model The idea of the T4 code generation technology is the reading the XML file of the actual model and the XML file of metamodel that defines the logical components, the shape components and the mapping of them to analyze and generate the accordance code in with the text template
Trang 53.3 Implement the Program to Optimize Memory Usage Based on Topological Sort
In this section, we will implement the program to find the optimal amount of memory occupied during execution time This program get input is the dependence graph of the Tasks extracted from the model as described in the previous section Then, we implement the johnsonTrotter algorithm to retrieve all permutationsfrom the set of Tasks Next, we implement the algorithm finding all Topo chains of Tasks in the set of all permutations We also build the evaluation function of the memory occupied of each Task that depends on the order of the Task in the chain and build the evaluation function of the amount of memory occupied for each Topo chain of Tasks Browse and calculate the value of the evaluation function from the Topo chains to select Topo chain having the smallest memory occupanced
4 EXPERIMENT
In this experiment, we carry out in two phase: the first, we use the framework of DSL, T4 and Topological sort built
in section 3 to model the dependence graph of Task set in an application, transform to mathematical expression of the dependence graph from the model, execute the optimal program to find the Topo chains that have minimal memory
occupied during execution time of the application; the second, we implement this application following to the best Topo
chain of Tasks, each Task is implemented as a function, the functions is called in the main function, change order of functions under Topo chain but not change content of functions to reach to the different programs, run these program and
do statistic of occupied memory amount of each program To do the experiment, we use the Nôm character recognition module that is the part of Nôm character process system developed by us [10] In the first phase, we create a model of the dependence graph of Tasks in Nôm character recognition module This model is shown in Figure 4 We transform this model to the mathematical expression of the dependence graphs and do optimization When executing the optimization program, the dependency graph of the nine of Tasks in the Figure 3, there are factorial of 9 permutations,
294 Topo chains obtained and 6 Topo chains that have the smallest occupied memory amount In the second phase, we implement Nôm character recognition module Then, we permute the order of executing functions under one of the best Top chains and under 10 Topo chains which are not the best Topo chain and selected randomly from the 294 Topo chains And we run the tests on the same configuration Here, we do tests on the simulation device of Pocket PC on the configuration To do statistic of the amount of occupied memory shown in Table I, we use “Best TaskMan” program to show all of Nôm character recognition processes on Windows Mobile as in Figure 5 Then, we draw the chart of actual occupied memory amount shown in Figure 6 The experimental result shown in Figure 6 matches the evaluation of occupied memory at model level
5 CONCLUSION AND FUTURE WORK
In summary, as discussed in the previous section, in this paper, we have proposed the new method to optimize the occupied memory of the embedded software in the design phase based on DSL, T4 and Topological sort Due to the limitation of embedded system about resources and CPU, this optimization method has important role and able to be applied widely We can extend this research applies to other fields such as optimizing scheduler of processes, optimizing
the compiler The paper has the following main contributions: firstly we have built the occupied evaluation function of
Task chain that depends on the order of Tasks and proposed the new approach to optimize occupied of embedded system
based on Topological sort; secondly we have presented the method to transform automatically from the model to code based DSL and T4; thirdly we have built the framework of DSL, T4 and Topological sort to design, generate code and
optimize at model level based on Topological sort Based on this paper, we will continue to make improvements and further research such as Pareto multi-objective optimization of embedded software based Topological sort, compiler optimization, the process scheduler optimization based on Topological sort and optimization based on model transformation based on DSL and T4
Trang 6Correct percelta
N of correct c Nmof chu not
ÂúÂQúRÂ9
Figure 4 N recognition m
This rese
[1] Chris Th
nce with
[2] Michalis
Code Ge
[3] Sanna S
plug-ins”
[4] Armita P
8, (2002
[5] Sangyoo
Performa
[6] Michalis
Code Ge
[7] Kahn, “T
[8] Lloyd G
[9] Dorin B
11, (200
[10] P.V Huo
Hanoi J
Nôm character module on Mobil
Table 1 Am
arch is partly s
hompson, Jules
h Model‐Driv
s Anastasopoul eneration Patter ivonen, “Dom
”, ESPOO,1-13 Peymandoust, T )
on Oh, Mehm ance Using a C
s Anastasopoul eneration Patter Topological so
G Williams “Pe Petriu, “A M 3)
ong, V.T Nhan of Science, N
e
Figure 5 Sh recognit Mobil mount of actual
supported NAF
s White, Brian ven Engineering los, Thomas F rns from Produ main-specific m
3, (2008)
Tajana Simuni
met S Aktas, Context Store f los, Thomas F rns from Produ rting of large n erformance Eva Metamodel for G
n, N.N Binh, T Natural Science
how Nôm charac tion processes on
le by TaskMan Occupied Memo
ACKNO
FOSTED and a
REF
Dougherty and g”, 1-10, (2008 Forster, and Di uct line archite modelling langu
ic, “Low Powe
Marlon Pier for Static Data”
Forster, and Di uct line archite networks”, Com aluation of Sof Generating Per T.A Hoang, “S
es and Technol
cter
n Figu
ory of the progra
OWLEDGM
also by a VNU
FERENCES
d Douglas C S 8)
rk Muthig, “O ctures”, D-676 uage and code
er Embedded S
rce, Geoffrey
”, 2008, pp.1-1
rk Muthig, “O ctures”, Sauerw mmunications ftware Architec rformance Mod Some Approac ogy, Vol 24, N
ure 6 Chart of ac
charac
am correspondin
MENTS
scientific proj
Schmidt,“Optim
Optimizing Mo
661 Kaiserslaut generator for Software Optim
C Fox, “Op
2 Optimizing Mo wiesen 6, Germ
of the ACM 5, ctures”, 1-12, ( dels from UML ches to Nôm O
No 3S, pp
90-ctual memory oc cter recognition p
ng to each topo c
ect (group A) f
mizing Mobile
odel-driven Dev utern, Germany developing re mization using ptimizing We
odel-driven Dev many, 50-61, (2 , 558-562, (196 (1998)
L Designs”, O ptical Characte -99, (2008)
ccupied by each process
chain
for 2012-2013
Application P
velopment by
y, 1-9, (2005)
epository-based Symbolic Alg
eb Service M
velopment by 2007)
62)
ttawa K1S, Ca
er Recognition
Nôm
erforma deriving
d Eclipse ebra”, 1-Messaging deriving
anada, 1-n”, VNU,