PandA-2024.02
basic_blocks_profiling.cpp
Go to the documentation of this file.
1 /*
2  *
3  * _/_/_/ _/_/ _/ _/ _/_/_/ _/_/
4  * _/ _/ _/ _/ _/_/ _/ _/ _/ _/ _/
5  * _/_/_/ _/_/_/_/ _/ _/_/ _/ _/ _/_/_/_/
6  * _/ _/ _/ _/ _/ _/ _/ _/ _/
7  * _/ _/ _/ _/ _/ _/_/_/ _/ _/
8  *
9  * ***********************************************
10  * PandA Project
11  * URL: http://panda.dei.polimi.it
12  * Politecnico di Milano - DEIB
13  * System Architectures Group
14  * ***********************************************
15  * Copyright (C) 2015-2024 Politecnico di Milano
16  *
17  * This file is part of the PandA framework.
18  *
19  * The PandA framework is free software; you can redistribute it and/or modify
20  * it under the terms of the GNU General Public License as published by
21  * the Free Software Foundation; either version 3 of the License, or
22  * (at your option) any later version.
23  *
24  * This program is distributed in the hope that it will be useful,
25  * but WITHOUT ANY WARRANTY; without even the implied warranty of
26  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27  * GNU General Public License for more details.
28  *
29  * You should have received a copy of the GNU General Public License
30  * along with this program. If not, see <http://www.gnu.org/licenses/>.
31  *
32  */
41 
42 #include "Parameter.hpp"
43 #include "application_manager.hpp"
44 #include "behavioral_helper.hpp"
45 #include "c_backend.hpp"
48 #include "call_graph_manager.hpp"
49 #include "compiler_wrapper.hpp"
50 #include "custom_set.hpp"
51 #include "design_flow_graph.hpp"
52 #include "design_flow_manager.hpp"
53 #include "fileIO.hpp"
54 #include "function_behavior.hpp"
55 #include "hash_helper.hpp"
56 #include "hls_step.hpp"
59 #include "string_manipulation.hpp"
60 
61 #include <string>
62 #include <utility>
63 #include <vector>
64 
66  const DesignFlowManagerConstRef _design_flow_manager,
67  const ParameterConstRef _parameters)
68  : ApplicationFrontendFlowStep(_AppM, BASIC_BLOCKS_PROFILING, _design_flow_manager, _parameters),
69  profiling_source_file(parameters->getOption<std::string>(OPT_output_temporary_directory) + "/host_profiling.c")
70 {
71  debug_level = parameters->get_class_debug_level(GET_CLASS(*this), DEBUG_LEVEL_NONE);
72 }
73 
76 {
78  return relationships;
79 }
80 
82  const DesignFlowStep::RelationshipType relationship_type)
83 {
84  if(relationship_type == DEPENDENCE_RELATIONSHIP)
85  {
86  const auto c_backend_factory =
87  GetPointer<const CBackendStepFactory>(design_flow_manager.lock()->CGetDesignFlowStepFactory("CBackend"));
88  relationship.insert(c_backend_factory->CreateCBackendStep(
89  CBackendInformationConstRef(new CBackendInformation(CBackendInformation::CB_BBP, profiling_source_file))));
90  }
91  ApplicationFrontendFlowStep::ComputeRelationships(relationship, relationship_type);
92 }
93 
95 {
96  const auto functions = AppM->CGetCallGraphManager()->GetReachedBodyFunctions();
97  for(const auto function : functions)
98  {
99  AppM->GetFunctionBehavior(function)->profiling_information->Clear();
100  }
101 }
102 
104 {
105  std::filesystem::path temporary_path(parameters->getOption<std::string>(OPT_output_temporary_directory));
106 
107  std::filesystem::path run_name = temporary_path / ("run.tmp");
108  std::filesystem::path profile_data_name = temporary_path / STR_CST_host_profiling_data;
109 
110  const CompilerWrapperConstRef compiler_wrapper(
111  new CompilerWrapper(this->parameters, parameters->getOption<CompilerWrapper_CompilerTarget>(OPT_host_compiler),
113  CustomSet<std::string> tp_files;
114  tp_files.insert(profiling_source_file);
115  compiler_wrapper->CreateExecutable(tp_files, run_name.string(), "");
116  std::string change_directory;
117  INDENT_OUT_MEX(OUTPUT_LEVEL_MINIMUM, output_level, "-->Starting dynamic profiling");
118  if(parameters->isOption(OPT_path))
119  {
120  change_directory = "cd \"" + parameters->getOption<std::string>(OPT_path) + "\" && ";
122  "---Changing working directory to " + parameters->getOption<std::string>(OPT_path));
123  }
124  const auto exec_argvs = parameters->getOption<CustomSet<std::string>>(OPT_exec_argv);
125  for(const auto& exec_argv : exec_argvs)
126  {
127  INDENT_OUT_MEX(OUTPUT_LEVEL_MINIMUM, output_level, "---Running with parameters: " + exec_argv);
128  // The argument
129  std::filesystem::remove(profile_data_name);
130 
131  const auto command = change_directory + "\"" + run_name.string() + "\" " + exec_argv + " ";
132  const auto ret = PandaSystem(parameters, command, false, temporary_path.string() + STR_CST_host_profiling_output);
133  if(IsError(ret))
134  {
135  if(errno and not parameters->getOption<bool>(OPT_no_return_zero))
136  {
137  THROW_ERROR_CODE(PROFILING_EC, "Error " + std::to_string(errno) + " during dynamic profiling");
138  }
139  }
140 
141  std::ifstream profilefile(profile_data_name.string().c_str());
142  if(profilefile.is_open())
143  {
144  std::string line;
145  ProfilingInformationRef profiling_information;
146  decltype(BBGraphInfo::bb_index_map) bb_index_map;
147  while(!profilefile.eof())
148  {
149  getline(profilefile, line);
151  if(line.size())
152  {
153  std::vector<std::string> splitted = SplitString(line, " ");
154  THROW_ASSERT(splitted.size() == 2, line);
155  if(line.find("Function") != std::string::npos)
156  {
157  const auto function_behavior =
158  AppM->CGetFunctionBehavior(static_cast<unsigned>(std::stoul(splitted[1])));
160  "---Found data of function " +
161  function_behavior->CGetBehavioralHelper()->get_function_name());
162  profiling_information = function_behavior->profiling_information;
163  bb_index_map = function_behavior->CGetBBGraph(FunctionBehavior::FBB)->CGetBBGraphInfo()->bb_index_map;
164  }
165  else
166  {
167  const auto bb_index = static_cast<unsigned>(std::stoul(splitted[0]));
168  if(bb_index_map.find(bb_index) != bb_index_map.end())
169  {
170  const auto bb_vertex = bb_index_map.find(bb_index)->second;
171  profiling_information->bb_executions[bb_vertex] = std::stoull(splitted[1]);
172  }
173  else
174  {
175  THROW_ASSERT(splitted[1] == "0", splitted[1]);
176  }
177  }
178  }
179  }
180  profilefile.close();
181  }
182  else
183  {
184  THROW_ERROR_CODE(PROFILING_EC, "Error during opening of profile data file " + profile_data_name.string());
185  }
186  }
187  INDENT_OUT_MEX(OUTPUT_LEVEL_MINIMUM, output_level, "<--Ended dynamic profiling");
188 
189  if(parameters->getOption<bool>(OPT_print_dot))
190  {
191  const auto functions = AppM->CGetCallGraphManager()->GetReachedBodyFunctions();
192  for(const auto function : functions)
193  {
194  AppM->CGetFunctionBehavior(function)->CGetBBGraph(FunctionBehavior::FBB)->WriteDot("BB_profiling.dot");
195  }
196  }
198 }
error during profiling
Definition: exceptions.hpp:331
#define DEBUG_LEVEL_VERY_PEDANTIC
extremely verbose debugging print is performed.
#define INDENT_DBG_MEX(dbgLevel, curDbgLevel, mex)
We are producing a debug version of the program, so the message is printed;.
Factory class to create c backend.
#define GET_CLASS(obj)
Macro returning the actual type of an object.
Definition of the class representing a generic C application.
const int output_level
The output level.
const std::vector< std::string > SplitString(const std::string &input, const std::string &separators)
Function which splits a string into tokens.
constants used in host profiling library
RelationshipType
The relationship type.
const std::string profiling_source_file
The instrumented source code.
#define INDENT_OUT_MEX(outLevel, curOutLevel, mex)
#define STR_CST_host_profiling_data
The file where profiling data are written by instrumented executable.
Definition of hash function for EdgeDescriptor.
Definition: graph.hpp:1321
#define OUTPUT_LEVEL_MINIMUM
minimum debugging print is performed.
Base class to pass information to a c backend.
Simple class used to drive the backend in order to be able to print c source code.
void line(int x1, int y1, int x2, int y2, unsigned int color)
Definition: main.c:110
Auxiliary methods for manipulating string.
bool IsError(const int error_value)
Utility include.
Definition: exceptions.cpp:58
const CustomUnorderedSet< std::pair< FrontendFlowStepType, FunctionRelationship > > ComputeFrontendRelationships(const DesignFlowStep::RelationshipType relationship_type) const override
Return the set of analyses in relationship with this design step.
Class specification for storing profiling information.
Analysis step performing a dynamic profiling of basic blocks execution.
#define STR_CST_host_profiling_output
The file where output of instrumented executable is saved.
BasicBlocksProfiling(const application_managerRef AppM, const DesignFlowManagerConstRef design_flow_manager, const ParameterConstRef parameters)
Constructor.
Classes to describe design flow graph.
Basic block control flow graph with feedback.
redefinition of set to manage ordered/unordered structures
utility function used to read files.
CompilerWrapper_CompilerTarget
target of the compiler
const Wrefcount< const DesignFlowManager > design_flow_manager
The design flow manager.
const ParameterConstRef parameters
Set of input parameters.
DesignFlowStep_Status
The status of a step.
#define DEBUG_LEVEL_NONE
no debugging print is performed.
Wrapper of design_flow.
list command
Definition: test_panda.py:921
int PandaSystem(const ParameterConstRef Param, const std::string &system_command, bool host_exec, const std::string &output, const unsigned int type, const bool background, const size_t timeout)
System call forcing execution with bash.
Definition: fileIO.cpp:78
refcount< T > lock() const
Definition: refcount.hpp:212
void ComputeRelationships(DesignFlowStepSet &relationship, const DesignFlowStep::RelationshipType relationship_type) override
Compute the relationships of a step with other steps.
const application_managerRef AppM
The application manager.
void ComputeRelationships(DesignFlowStepSet &relationship, const DesignFlowStep::RelationshipType relationship_type) final
Compute the relationships of a step with other steps.
This file collects some hash functors.
Template borrowed from the ANTLR library by Terence Parr (http://www.jGuru.com - Software rights: htt...
Definition: refcount.hpp:94
#define THROW_ERROR_CODE(code, str_expr)
helper function used to throw an error with a code error
Definition: exceptions.hpp:266
this class is used to manage the command-line or XML options.
Main class for wrapping the frontend compiler.
Wrapper to call graph.
int debug_level
The debug level.
CustomUnorderedMap< unsigned int, vertex > bb_index_map
DesignFlowStep_Status Exec() final
Execute this step.
Base class to pass information to a c backend.
A brief description of the C++ Header File.
void Initialize() final
Initialize the step (i.e., like a constructor, but executed just before exec.
#define THROW_ASSERT(cond, str_expr)
helper function used to check an assert and if needed to throw an error in a standard way ...
Definition: exceptions.hpp:289
Implementation of the wrapper to Gcc for C sources.

Generated on Mon Feb 12 2024 13:02:51 for PandA-2024.02 by doxygen 1.8.13