HPCToolkit
main.cpp
Go to the documentation of this file.
1 // -*-Mode: C++;-*-
2 
3 // * BeginRiceCopyright *****************************************************
4 //
5 // $HeadURL$
6 // $Id$
7 //
8 // --------------------------------------------------------------------------
9 // Part of HPCToolkit (hpctoolkit.org)
10 //
11 // Information about sources of support for research and development of
12 // HPCToolkit is at 'hpctoolkit.org' and in 'README.Acknowledgments'.
13 // --------------------------------------------------------------------------
14 //
15 // Copyright ((c)) 2002-2019, Rice University
16 // All rights reserved.
17 //
18 // Redistribution and use in source and binary forms, with or without
19 // modification, are permitted provided that the following conditions are
20 // met:
21 //
22 // * Redistributions of source code must retain the above copyright
23 // notice, this list of conditions and the following disclaimer.
24 //
25 // * Redistributions in binary form must reproduce the above copyright
26 // notice, this list of conditions and the following disclaimer in the
27 // documentation and/or other materials provided with the distribution.
28 //
29 // * Neither the name of Rice University (RICE) nor the names of its
30 // contributors may be used to endorse or promote products derived from
31 // this software without specific prior written permission.
32 //
33 // This software is provided by RICE and contributors "as is" and any
34 // express or implied warranties, including, but not limited to, the
35 // implied warranties of merchantability and fitness for a particular
36 // purpose are disclaimed. In no event shall RICE or contributors be
37 // liable for any direct, indirect, incidental, special, exemplary, or
38 // consequential damages (including, but not limited to, procurement of
39 // substitute goods or services; loss of use, data, or profits; or
40 // business interruption) however caused and on any theory of liability,
41 // whether in contract, strict liability, or tort (including negligence
42 // or otherwise) arising in any way out of the use of this software, even
43 // if advised of the possibility of such damage.
44 //
45 // ******************************************************* EndRiceCopyright *
46 
47 //************************ System Include Files ******************************
48 
49 #include <iostream>
50 #include <fstream>
51 
52 #include <string>
53 using std::string;
54 
55 #include <exception>
56 
57 #include <unistd.h> /* for unlink() */
58 
59 //*********************** Xerces Include Files *******************************
60 
61 //************************* User Include Files *******************************
62 
63 #include "Args.hpp"
64 #include "ConfigParser.hpp"
65 
67 
70 
72 #include <lib/support/NaN.h>
73 #include <lib/support/IOUtil.hpp>
74 
75 //************************ Forward Declarations ******************************
76 
77 static void
79 
80 //****************************************************************************
81 
82 static int
83 realmain(int argc, char* const* argv);
84 
85 int
86 main(int argc, char* const* argv)
87 {
88  int ret;
89 
90  try {
91  ret = realmain(argc, argv);
92  }
93  catch (const Diagnostics::Exception& x) {
94  DIAG_EMsg(x.message());
95  exit(1);
96  }
97  catch (const std::bad_alloc& x) {
98  DIAG_EMsg("[std::bad_alloc] " << x.what());
99  exit(1);
100  }
101  catch (const std::exception& x) {
102  DIAG_EMsg("[std::exception] " << x.what());
103  exit(1);
104  }
105  catch (...) {
106  DIAG_EMsg("Unknown exception encountered!");
107  exit(2);
108  }
109 
110  return ret;
111 }
112 
113 
114 static int
115 realmain(int argc, char* const* argv)
116 {
117  Args args(argc, argv); // exits if error on command line
118 
119  //-------------------------------------------------------
120  // Create metric descriptors (and for conf file, rest of the args)
121  //-------------------------------------------------------
122  Prof::Metric::Mgr metricMgr;
123 
124  if (args.configurationFileMode) {
125  readConfFile(args, metricMgr); // exits on failure
126  }
127  else {
128  metricMgr.makeRawMetrics(args.profileFiles);
129  }
130 
131  //-------------------------------------------------------
132  // Correlate metrics with program structure and Generate output
133  //-------------------------------------------------------
135 
136  Prof::Struct::Tree structure("", new Prof::Struct::Root(""));
137 
138  Analysis::Flat::Driver driver(args, metricMgr, structure);
139  int ret = driver.run();
140 
141  return ret;
142 }
143 
144 
145 //****************************************************************************
146 //
147 //****************************************************************************
148 
149 #define NUM_PREFIX_LINES 2
150 
151 static string
152 buildConfFile(const string& hpcHome, const string& confFile);
153 
154 static void
155 appendContents(std::ofstream &dest, const char *srcFile);
156 
157 static void
159 {
160  InitXerces(); // exits iff failure
161 
162  const string& cfgFile = args.configurationFile;
163  DIAG_Msg(2, "Initializing from: " << cfgFile);
164 
165  string tmpFile = buildConfFile(args.hpcHome, cfgFile);
166 
167  try {
168  XercesErrorHandler errHndlr(cfgFile, tmpFile, NUM_PREFIX_LINES, true);
169  ConfigParser parser(tmpFile, errHndlr);
170  parser.parse(args, metricMgr);
171  }
172  catch (const SAXParseException& /*ex*/) {
173  unlink(tmpFile.c_str());
174  //DIAG_EMsg(XMLString::transcode(ex.getMessage()));
175  exit(1);
176  }
177  catch (const ConfigParserException& ex) {
178  unlink(tmpFile.c_str());
179  DIAG_EMsg(ex.message());
180  exit(1);
181  }
182  catch (...) {
183  unlink(tmpFile.c_str());
184  DIAG_EMsg("While processing '" << cfgFile << "'...");
185  throw;
186  };
187 
188  unlink(tmpFile.c_str());
189 
190  FiniXerces();
191 }
192 
193 
194 static string
195 buildConfFile(const string& hpcHome, const string& confFile)
196 {
197  string tmpFile = FileUtil::tmpname();
198  string hpcloc = hpcHome;
199  if (hpcloc[hpcloc.length()-1] != '/') {
200  hpcloc += "/";
201  }
202  std::ofstream os(tmpFile.c_str(), std::ios_base::out);
203 
204  if (os.fail()) {
205  DIAG_Throw("Unable to write temporary file: " << tmpFile);
206  }
207 
208  // the number of lines added below must equal NUM_PREFIX_LINES
209  os << "<?xml version=\"1.0\"?>" << std::endl
210  << "<!DOCTYPE HPCPROF SYSTEM \"" << hpcloc // has trailing '/'
211  << "share/hpctoolkit/dtd/hpcprof-config.dtd\">" << std::endl;
212 
213  //std::cout << "TMP DTD file: '" << tmpFile << "'" << std::std::endl;
214  //std::cout << " " << hpcloc << std::endl;
215 
216  appendContents(os, confFile.c_str());
217  os.close();
218  return tmpFile;
219 }
220 
221 
222 static void
223 appendContents(std::ofstream &dest, const char *srcFile)
224 {
225 #define MAX_IO_SIZE (64 * 1024)
226 
227  std::ifstream src(srcFile);
228  if (src.fail()) {
229  DIAG_Throw("Unable to read file: " << srcFile);
230  }
231 
232  char buf[MAX_IO_SIZE];
233  for(; !src.eof(); ) {
234  src.read(buf, MAX_IO_SIZE);
235 
236  ssize_t nRead = src.gcount();
237  if (nRead == 0) break;
238  dest.write(buf, nRead);
239  if (dest.fail()) {
240  DIAG_Throw("appendContents: failed!");
241  }
242  }
243  src.close();
244 }
245 
Definition: Args.hpp:79
void makeRawMetrics(const std::vector< std::string > &profileFiles, bool isUnitsEvents=true, bool doDispPercent=true)
Definition: Metric-Mgr.cpp:98
#define DIAG_EMsg(...)
Definition: diagnostics.h:251
const char * tmpname()
Definition: FileUtil.cpp:418
virtual std::string message() const
static int realmain(int argc, char *const *argv)
Definition: main.cpp:115
Definition: fmt.c:108
static RealPathMgr & singleton()
std::string searchPathStr
Definition: Args.hpp:119
bool configurationFileMode
Definition: Args.hpp:117
void InitXerces()
Definition: XercesUtil.cpp:78
static string buildConfFile(const string &hpcHome, const string &confFile)
Definition: main.cpp:195
virtual std::string message() const
Definition: Exception.hpp:134
void parse(Analysis::Args &args, Prof::Metric::Mgr &metricMgr)
exit
Definition: names.cpp:1
std::string configurationFile
Definition: Args.hpp:118
static void readConfFile(Args &args, Prof::Metric::Mgr &metricMgr)
Definition: main.cpp:158
int main(int argc, char *argv[])
Definition: main.cpp:125
std::vector< std::string > profileFiles
Definition: Args.hpp:138
#define MAX_IO_SIZE
const std::string & searchPaths() const
#define DIAG_Msg(level,...)
Definition: diagnostics.h:241
#define NUM_PREFIX_LINES
Definition: main.cpp:149
std::string hpcHome
Definition: Args.hpp:111
static void appendContents(std::ofstream &dest, const char *srcFile)
Definition: main.cpp:223
void FiniXerces()
Definition: XercesUtil.cpp:92