Embedded 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 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 – Optimizing memory embedded software based on International Conference on Graphic and Image Processing (ICGIP 2012), edited by Zeng Zhu, Proc of SPIE Vol 8768, 876849 · © 2013 SPIE CCC code: 0277-786X/13/$18 · doi: 10.1117/12.2011266 Proc of SPIE Vol 8768 876849-1 Downloaded From: http://proceedings.spiedigitallibrary.org/ on 07/04/2016 Terms of Use: http://spiedigitallibrary.org/ss/TermsOfUse.aspx Topological sort; Section – Develop the framework of DSL, T4 and implement the program that supports optimizing; Section – Experimental; Section – Conclusion and future work 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 ti to tj} (1) where: • Each node ti 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 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 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 Suppose that the program has N Tasks and ti is the Task i in the Topo chain Task i has the returned data type ri When executing ti, local variables and parameters of ti will be allocated in the stack memory space and it will be released when ti returns The total of used memory size in the execution of Task chain is the same in the all of Topo chains heap caIcSizeO myabj = new Object() I 1 I stack I main ID int y static data code Figure Layout of memory in the program execution Proc of SPIE Vol 8768 876849-2 Downloaded From: http://proceedings.spiedigitallibrary.org/ on 07/04/2016 Terms of Use: http://spiedigitallibrary.org/ss/TermsOfUse.aspx Higher memory Frame for main( ) Frame f or maint ) Frame f or a() Frame Frame f or main( ) Frame for main() Frame for a() Frame f or main( ) Frame for a( ) for a ( ) Frame Lower memory for Frame Frame main( ) Frame main() for Frame return from for c ( ) aU return framb() calls a() for a ( ) b() main° for ueturn foam cQ a0 calls bQ a() calls cQ Figure 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 ti 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: • Fm is the evaluation function of the amount of memory occupied • N is the number of Tasks of the program • ri is the data type returned by Task i GetFeature:+«ay GetTOta I C`C h a rs EvaluateCcrrec:Pecen: -etNumOfCharNotInDIC DispIayCcrrec:Percer: Di sp I ayTctalOfChar DisplayNumC`CharIncorrect Figure Task flow of the Nôm character recognition module Proc of SPIE Vol 8768 876849-3 Downloaded From: http://proceedings.spiedigitallibrary.org/ on 07/04/2016 Terms of Use: http://spiedigitallibrary.org/ss/TermsOfUse.aspx 2.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 Fm 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 Fm for each chain, and select the best Topo chain on which the Fm 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 ci chain in the factorial of N chains { Assign the variable isTopo to true For each egde ej,k in the set E { If tk is front of tj then { Assign isTopo to false break } } If isTopo is equal true{ Add ci to the set of Topo chain } } 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 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 Proc of SPIE Vol 8768 876849-4 Downloaded From: http://proceedings.spiedigitallibrary.org/ on 07/04/2016 Terms of Use: http://spiedigitallibrary.org/ss/TermsOfUse.aspx 3.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 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 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 statistic of occupied memory amount of each program To 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 We transform this model to the mathematical expression of the dependence graphs and optimization When executing the optimization program, the dependency graph of the nine of Tasks in the Figure 3, there are factorial of permutations, 294 Topo chains obtained and 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 tests on the simulation device of Pocket PC on the configuration To 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 Then, we draw the chart of actual occupied memory amount shown in Figure The experimental result shown in Figure matches the evaluation of occupied memory at model level 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 Proc of SPIE Vol 8768 876849-5 Downloaded From: http://proceedings.spiedigitallibrary.org/ on 07/04/2016 Terms of Use: http://spiedigitallibrary.org/ss/TermsOfUse.aspx Correct percelta N of correct c Nmof chu not nnaaavi Taal of chus ÁúÁQúRÁ9 Figure Nôm N character recognition module m on Mobile Figure Sh how Nôm characcter recognittion processes on n Mobille by TaskMan Figuure Chart of acctual memory occcupied by each Nôm characcter recognition pprocess Table Am mount of actual Occupied Memo ory of the prograam correspondinng to each topo cchain ACKNO OWLEDGM MENTS This research is partly supported s NAF FOSTED and also a by a VNU scientific project (group A) ffor 2012-2013 REF FERENCES [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] Chris Th hompson, Juless White, Brian Dougherty and d Douglas C S Schmidt,“Optim mizing Mobile Application Performa nce with h Model‐Driv ven Engineering g”, 1-10, (2008 8) Michaliss Anastasopoullos, Thomas Forster, F and Dirk Muthig, “O Optimizing Moodel-driven Devvelopment by deriving Code Geeneration Patterrns from Produ uct line architectures”, D-676661 Kaiserslaut utern, Germanyy, 1-9, (2005) Sanna Sivonen, “Dom main-specific modelling m langu uage and code generator for developing reepository-basedd Eclipse plug-ins””, ESPOO,1-13 3, (2008) Armita Peymandoust, P Tajana T Simuniic, “Low Poweer Embedded S Software Optim mization using Symbolic Algebra”, 18, (2002) Sangyoo on Oh, Mehm met S Aktas, Marlon Pierrce, Geoffrey C Fox, “Opptimizing Weeb Service M Messaging Performaance Using a Context C Store for f Static Data””, 2008, pp.1-1 Michaliss Anastasopoullos, Thomas Forster, F and Dirk Muthig, “O Optimizing Moodel-driven Devvelopment by deriving Code Geeneration Patterrns from Produ uct line architectures”, Sauerw wiesen 6, Germ many, 50-61, (22007) Kahn, “T Topological sorting of large networks”, n Com mmunications of the ACM 5,, 558-562, (19662) Lloyd G G Williams “Peerformance Evaaluation of Sofftware Architecctures”, 1-12, ((1998) Dorin B Petriu, “A Metamodel M for Generating G Perrformance Moddels from UML L Designs”, Ottawa K1S, Caanada, 111, (2003) P.V Huo ong, V.T Nhan n, N.N Binh, T.A T Hoang, “S Some Approacches to Nôm Optical Characteer Recognitionn”, VNU, Hanoi J of Science, Natural N Sciencees and Technology, Vol 24, N No 3S, pp 90 99, (2008) Proc of SPIE Vol 8768 876849-6 Downloaded From: http://proceedings.spiedigitallibrary.org/ on 07/04/2016 Terms of Use: http://spiedigitallibrary.org/ss/TermsOfUse.aspx ... 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. .. not the best Topo chain and selected randomly from the 294 Topo chains And we run the tests on the same configuration Here, we tests on the simulation device of Pocket PC on the configuration... 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