'////////////////////////////////////////////////////////////////////////////// '// '// File: PgoExample.vbs '// '// Description: This script demonstrates using the VisualDSP++ Automation '// API to perform profile guided optimization on the tutorial '// project. This script performs many of the actions of the '// tutorial and serves as a good starting point for developing '// more customized and complex scripts to better suite your '// development needs. '// '// To use this script you must be connected to a simulator '// target that supports PGO, you must have a project open '// and you must have VBScript set as your current scripting '// language in the console window of the IDDE. The script can '// then be run by loading it via File->Load Script... or by '// opening it in an editor window and selecting Load Script '// from the editor context menu. '// '////////////////////////////////////////////////////////////////////////////// If VarType(Idde) = vbEmpty Then Set Idde = CreateObject( "VisualDSP.ADspApplication" ) End If ' get the active project Set Project = Idde.ProjectList.ActiveProject ' get the active processor Set Processor = Idde.ActiveSession.ActiveProcessor ' get the compiler tool Set Compiler = Project.ToolList.Item("Compiler") ' the names of the data sets that we'll operate on DataSets = Array( "dataset_1", "dataset_2", "dataset_3" ) '////////////////////////////////////////////////////////////////////////////// '// '// Sub Print '// '// Helper function that prints text to the console window of the IDDE '// '////////////////////////////////////////////////////////////////////////////// Sub Print( Text ) If VarType(WScript) <> vbEmpty Then WScript.StdOut.Write Text & vbCRLF End If Idde.OutputWindow.ActiveTab = 0 Idde.OutputWindow.PrintText Text, 0 End Sub '////////////////////////////////////////////////////////////////////////////// '// '// Sub BuildApplication '// '// Builds the active project with the specified compiler options '// '////////////////////////////////////////////////////////////////////////////// Sub BuildApplication( Project, Compiler, Options ) ' add the specified switches to the compiler Compiler.AddSwitch "Debug", "AdditionalOptions", Options ' build the application Project.BuildAll True ' remove the specified switches to the compiler Compiler.RemoveSwitch "Debug", "AdditionalOptions", Options ' don't mark as modified since we don't want to save the changes Project.Modified = False End Sub '////////////////////////////////////////////////////////////////////////////// '// '// Function ProfileDataSet '// '// Loads the program to the target and runs it to collect the cycle count '// '////////////////////////////////////////////////////////////////////////////// Function ProfileDataSet( Processor, DataSet, GeneratePgoData ) ' get the list of active streams Set StreamList = Idde.ActiveSession.StreamList Set BreakList = Processor.BreakpointList ' load the dxe to the target Processor.LoadProgram Project.ProjectDirectory & "\Debug\PgoExample.dxe" ' remove any currently defined breakpoints While BreakList.Count > 0 BreakList.RemoveBreakpointByIndex 0 WEnd ' set a breakpoint at the end of the program StopAddress = Processor.MemoryTypeList.FindSymbol("___lib_prog_term")(0).Address BreakList.SetBreakpointAtAddress 0, StopAddress, 0, "", False, True, 0 ' remove any currently defined streams While StreamList.Count > 0 StreamList.RemoveStream 0 WEnd ' point the stream to the new input data file StreamList.AddFileToDeviceStream DataSet & ".dat", "Hexadecimal", False, False, Processor.Name, "0xFFD00000-0xFFD00FFF", &HFFD00000 If GeneratePgoData = True Then ' get the pgo plugin Set PgoPlugin = Idde.PluginList.Item("PGO Engine").Open ' aquire the pgo manager object for the active processor Set PgoManager = PgoPlugin.PgoManager(Processor) ' tell the target to create the .pgo file associated ' with this input data file PgoManager.OutputFileName = DataSet & ".pgo" ' tell the target to start collecting data PgoManager.Enabled = True End If ' create a new profile session Set ProfileSession = Processor.ProfileSessionList.CreateSession( "PGO Profiler" ) ProfileSession.Visible = True ProfileSession.Enabled = True ' run to the end of the program Processor.Run True ' get the total number of cycles executed TotalCycles = ProfileSession.TotalSampleCount If GeneratePgoData Then ' tell the target to stop collecting data PgoManager.Enabled = False End If ' clean up Processor.ProfileSessionList.RemoveSession "PGO Profiler" StreamList.RemoveStream 0 ' return the cycle count ProfileDataSet = TotalCycles End Function '////////////////////////////////////////////////////////////////////////////// '// '// Main Application '// '////////////////////////////////////////////////////////////////////////////// ' print the banner Print "-----------------------------------------------------------" Print " VisualDSP++ Profile Guided Optimization Demonstration" Print "-----------------------------------------------------------" & vbCRLF ' set the current working directory of the idde Idde.CurrentDirectory = Project.ProjectDirectory ' tell the compiler to optimize for speed Compiler.AddSwitch "Debug", "-Ov", "100" Print "Building application with PGO support..." & vbCrlf ' build the application with the -pguide switch BuildApplication Project, Compiler, "-pguide" CyclesBefore = 0 For Each DataSet In DataSets Print "Profiling data set: " & DataSet ' execute and profile each data set CycleCount = ProfileDataSet( Processor, DataSet, True ) Print " [" & CycleCount & " cycles] " & vbCRLF CyclesBefore = CyclesBefore + CycleCount Next Print "Total cycles before optimization: " & CyclesBefore & vbCrlf Print "Building application with PGO results..." & vbCrlf ' concatenate all the .pgo filenames together into one string PgoFiles = Join( DataSets, ".pgo " ) & ".pgo" ' rebuild the application with the profile results BuildApplication Project, Compiler, PgoFiles CyclesAfter = 0 For Each DataSet In DataSets Print "Profiling data set: " & DataSet ' execute and profile each data set CycleCount = ProfileDataSet( Processor, DataSet, False ) Print " [" & CycleCount & " cycles] " & vbCRLF CyclesAfter = CyclesAfter + CycleCount Next Print "Total cycles after optimization: " & CyclesAfter & vbCrlf ' calculate the cycle count savings Print "Results: " & Round(((1-(CyclesAfter/CyclesBefore))*100), 2) & "% savings in raw cycle count" & vbCrlf