HPCToolkit
DBOpener.cpp
Go to the documentation of this file.
1 // -*-Mode: C++;-*-
2 
3 // * BeginRiceCopyright *****************************************************
4 //
5 // $HeadURL: https://hpctoolkit.googlecode.com/svn/branches/hpctoolkit-hpcserver/src/tool/hpcserver/DBOpener.cpp $
6 // $Id: DBOpener.cpp 4307 2013-07-18 17:04:52Z felipet1326@gmail.com $
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 //***************************************************************************
48 //
49 // File:
50 // $HeadURL: https://hpctoolkit.googlecode.com/svn/branches/hpctoolkit-hpcserver/src/tool/hpcserver/DBOpener.cpp $
51 //
52 // Purpose:
53 // [The purpose of this file]
54 //
55 // Description:
56 // [The set of functions, macros, etc. defined in the file]
57 //
58 //***************************************************************************
59 #include <cstdio>
60 
61 #include "DBOpener.hpp"
62 #include "DebugUtils.hpp"
63 #include "MergeDataFiles.hpp"
64 #include "FileUtils.hpp"
65 #include "FileData.hpp"
67 
68 namespace TraceviewerServer
69 {
70  #define XML_FILENAME "experiment.xml"
71  #define TRACE_FILENAME "experiment.mt"
72 
74  {
75 
76  }
77 
79  {
80  //stdcl, which is the only dynamically allocated thing, gets closed later because
81  //it makes sense to have it persist even after the opener.
82  }
85  {
86  FileData location;
87  FileData* ptrLocation = &location;
88  bool hasDatabase = false;
89  hasDatabase = verifyDatabase(pathToDB, ptrLocation);
90 
91  // If it still doesn't have a database, we assume that the user doesn't
92  // want to open a database, so we return null, which makes the calling method return false.
93  if (!hasDatabase)
94  return NULL;
95 
96  stdcl = new SpaceTimeDataController(ptrLocation);
97  return stdcl;
98  }
99  /****
100  * Check if the directory is correct or not. If it is correct, it returns
101  * the XML file and the trace file
102  *
103  * @param directory
104  * (in): the input directory
105  * @param statusMgr
106  * (in): status bar
107  * @param experimentFile
108  * (out): XML file
109  * @param traceFile
110  * (out): trace file
111  * @return true if the directory is valid, false otherwise
112  *
113  */
114  bool DBOpener::verifyDatabase(string directory, FileData* location)
115  {
116 
117  DEBUGCOUT(2) << "Checking " <<directory<<endl;
118 
119  if (FileUtils::existsAndIsDir(directory))
120  {
121 
122  DEBUGCOUT(2) << "\tExists and is a directory"<<endl;
123 
124  location->fileXML = FileUtils::combinePaths(directory, XML_FILENAME);
125 
126  DEBUGCOUT(2) << "\tTrying to open "<<location->fileXML<<endl;
127 
128  if (FileUtils::exists(location->fileXML))
129  {
130 
131  DEBUGCOUT(2) <<"\tXML file is not null"<<endl;
132 
133  try
134  {
135  std::string outputFile = FileUtils::combinePaths(directory, TRACE_FILENAME);
136 
137  DEBUGCOUT(2) <<"\tTrying to open "<<outputFile<<endl;
138 
139  MergeDataAttribute att = MergeDataFiles::merge(directory, "*.hpctrace",
140  outputFile);
141 
142  DEBUGCOUT(2) <<"\tMerge resulted in "<<att<<endl;
143 
144  if (att != FAIL_NO_DATA)
145  {
146  location->fileTrace = outputFile;
148  {
149  return true;
150  }
151  else
152  {
153  cerr << "Warning! Trace file " << location->fileTrace << "is too small: "
154  << FileUtils::getFileSize(location->fileTrace) << " bytes." << endl;
155  return false;
156  }
157  }
158  else
159  {
160  cerr << "Error: trace file(s) does not exist or fail to open "
161  << outputFile << endl;
162  }
163 
164  } catch (int err)
165  {
166  cerr << "Error code: " << err << endl;
167  }
168 
169  }
170 
171  }
172  return false;
173  }
174 
175 } /* namespace TraceviewerServer */
err
Definition: names.cpp:1
SpaceTimeDataController * stdcl
Definition: DBOpener.cpp:83
static MergeDataAttribute merge(string, string, string)
SpaceTimeDataController * openDbAndCreateStdc(string)
Definition: DBOpener.cpp:84
#define XML_FILENAME
Definition: DBOpener.cpp:70
static bool existsAndIsDir(string p)
Definition: FileUtils.hpp:100
static bool verifyDatabase(string, FileData *)
Definition: DBOpener.cpp:114
static string combinePaths(string firstPart, string secondPart)
Definition: FileUtils.hpp:84
static bool exists(string p)
Definition: FileUtils.hpp:113
#define DEBUGCOUT(a)
Definition: DebugUtils.hpp:72
#define NULL
Definition: ElfHelper.cpp:85
static const unsigned int MIN_TRACE_SIZE
Definition: DBOpener.hpp:79
#define TRACE_FILENAME
Definition: DBOpener.cpp:71
static FileOffset getFileSize(string p)
Definition: FileUtils.hpp:120