Pol  Revision:cb584c9
RunEclMain.cpp
Go to the documentation of this file.
1 
2 #include "pol_global_config.h"
3 
4 #include <ctime>
5 #include <iostream>
6 
7 #include "../bscript/bobject.h"
8 #include "../bscript/config.h"
9 #include "../bscript/escriptv.h"
10 #include "../bscript/execmodl.h"
11 #include "../bscript/executor.h"
12 #include "../bscript/filefmt.h"
13 #include "../bscript/object.h"
14 #include "../clib/Program/ProgramMain.h"
15 #include "../clib/boostutils.h"
16 #include "../clib/fileutil.h"
17 #include "../clib/logfacility.h"
18 #include "../clib/refptr.h"
19 #include "../plib/pkg.h"
20 #include "../plib/systemstate.h"
21 #include "RunEclMain.h"
22 #include <format/format.h>
23 
24 #include "../pol/module/basiciomod.h"
25 #include "../pol/module/basicmod.h"
26 #include "../pol/module/cfgmod.h"
27 #include "../pol/module/datastore.h"
28 #include "../pol/module/filemod.h"
29 #include "../pol/module/mathmod.h"
30 #include "../pol/module/utilmod.h"
31 #include "../pol/polcfg.h"
32 
33 namespace Pol
34 {
35 namespace Core
36 {
37 // See comment in boost_utils::flyweight_initializers
38 boost_utils::flyweight_initializers fw_inits;
39 }
40 namespace Module
41 {
42 void load_fileaccess_cfg();
43 }
44 namespace Clib
45 {
46 using namespace std;
47 using namespace Pol::Bscript;
48 using namespace Pol::Module;
49 
51 
52 RunEclMain::RunEclMain() : ProgramMain(), m_quiet( false ), m_debug( false ), m_profile( false ) {}
55 
57 {
58  ERROR_PRINT << "Usage:\n"
59  << " \n"
60  << " RUNECL [options] filespec [filespec ...]\n"
61  << " Options:\n"
62  << " -q Quiet\n"
63  << " -d Debug output\n"
64  << " -p Profile\n";
65  // TODO: what about "-v" and "-a"?
66 }
67 
68 void RunEclMain::dumpScript( std::string fileName )
69 {
70  if ( fileName.size() >= 4 )
71  fileName.replace( fileName.size() - 4, 4, ".ecl" );
72 
73  Executor exe;
74  exe.setViewMode( true );
75  exe.addModule( new BasicExecutorModule( exe ) );
76  exe.addModule( new BasicIoExecutorModule( exe ) );
77  exe.addModule( new MathExecutorModule( exe ) );
78  exe.addModule( new UtilExecutorModule( exe ) );
79  exe.addModule( new FileAccessExecutorModule( exe ) );
80  exe.addModule( new ConfigFileExecutorModule( exe ) );
81  exe.addModule( new DataFileExecutorModule( exe ) );
82 
84  program->read( fileName.c_str() );
85  exe.setProgram( program.get() );
86 
87  std::ostringstream os;
88  program->dump( os );
89  INFO_PRINT << os.str();
90 }
91 
92 int RunEclMain::runeclScript( std::string fileName )
93 {
94  // TODO: autoconvert to .ecl ?
95  bool exres;
96  double seconds;
97  size_t memory_used;
98  clock_t clocks;
99 #ifdef _WIN32
100  FILETIME dummy;
101  FILETIME kernelStart, userStart;
102  FILETIME kernelEnd, userEnd;
103 #endif
104  Executor exe;
105  exe.addModule( new BasicExecutorModule( exe ) );
106  exe.addModule( new BasicIoExecutorModule( exe ) );
107  exe.addModule( new MathExecutorModule( exe ) );
108  exe.addModule( new UtilExecutorModule( exe ) );
109  exe.addModule( new FileAccessExecutorModule( exe ) );
110  exe.addModule( new ConfigFileExecutorModule( exe ) );
111  exe.addModule( new DataFileExecutorModule( exe ) );
112 
114  if ( program->read( fileName.c_str() ) )
115  {
116  ERROR_PRINT << "Error reading " << fileName << "\n";
117  return 1;
118  }
119  exe.setProgram( program.get() );
120  // find and set pkg
121  std::string dir = fileName;
122  Clib::strip_one( dir );
123  dir = Clib::normalized_dir_form( dir );
124  Plib::load_packages( true /*quiet*/ );
125 
126  const auto& pkgs = Plib::systemstate.packages;
127  auto pkg = std::find_if( pkgs.begin(), pkgs.end(), [&dir]( Plib::Package* p ) {
128  return Clib::stringicmp( p->dir(), dir ) == 0;
129  } );
130  if ( pkg != pkgs.end() )
131  {
132  program->pkg = *pkg;
133  }
134  Module::load_fileaccess_cfg(); // after pkg load
135 
137  clock_t start = clock();
138 #ifdef _WIN32
139  GetThreadTimes( GetCurrentThread(), &dummy, &dummy, &kernelStart, &userStart );
140 #endif
141 
142  exres = exe.exec();
143 
144 #ifdef _WIN32
145  GetThreadTimes( GetCurrentThread(), &dummy, &dummy, &kernelEnd, &userEnd );
146 #endif
147  clocks = clock() - start;
148  seconds = static_cast<double>( clocks ) / CLOCKS_PER_SEC;
149 
150  memory_used = exe.sizeEstimate();
151 
152  if ( m_profile )
153  {
154  fmt::Writer tmp;
155  tmp << "Profiling information: \n"
156  << "\tEObjectImp constructions: " << eobject_imp_constructions << "\n";
157  if ( eobject_imp_count )
158  tmp << "\tRemaining BObjectImps: " << eobject_imp_count << "\n";
159  tmp << "\tInstruction cycles: " << escript_instr_cycles << "\n"
160  << "\tInnerExec calls: " << escript_execinstr_calls << "\n"
161  << "\tClocks: " << clocks << " (" << seconds << " seconds)\n"
162 #ifdef _WIN32
163  << "\tKernel Time: " << ( *(__int64*)&kernelEnd ) - ( *(__int64*)&kernelStart ) << "\n"
164  << "\tUser Time: " << ( *(__int64*)&userEnd ) - ( *(__int64*)&userStart ) << "\n"
165 #endif
166  << "\tSpace used: " << memory_used << "\n\n"
167  << "\tCycles Per Second: " << escript_instr_cycles / seconds << "\n"
168  << "\tCycles Per Minute: " << 60.0 * escript_instr_cycles / seconds << "\n"
169  << "\tCycles Per Hour: " << 3600.0 * escript_instr_cycles / seconds << "\n";
170 #if BOBJECTIMP_DEBUG
172 #endif
173 #ifdef ESCRIPT_PROFILE
174  tmp << "FuncName,Count,Min,Max,Sum,Avarage\n";
175  for ( escript_profile_map::iterator itr = EscriptProfileMap.begin();
176  itr != EscriptProfileMap.end(); ++itr )
177  {
178  tmp << itr->first << "," << itr->second.count << "," << itr->second.min << ","
179  << itr->second.max << "," << itr->second.sum << ","
180  << ( itr->second.sum / ( 1.0 * itr->second.count ) ) << "\n";
181  }
182 #endif
183  INFO_PRINT << tmp.str();
184  }
185  return exres ? 0 : 1;
186 }
187 
189 {
190  const std::vector<std::string>& binArgs = programArgs();
191 
192  for ( size_t i = 1; i < binArgs.size(); i++ )
193  {
194  switch ( binArgs[i][0] )
195  {
196  case '/':
197  case '-':
198  switch ( binArgs[i][1] )
199  {
200  case 'd':
201  case 'D':
202  case 'v':
203  case 'V':
204  case 'q':
205  case 'Q':
206  case 'p':
207  case 'P':
208  break;
209  default:
210  ERROR_PRINT << "Unknown option: " << binArgs[i] << "\n";
211  return 1;
212  }
213  break;
214  default:
215  return runeclScript( binArgs[i] );
216  }
217  }
218  return 0;
219 }
220 
222 {
223  const std::vector<std::string>& binArgs = programArgs();
225  m_quiet = programArgsFind( "q" );
226  m_debug = programArgsFind( "d" );
227  m_profile = programArgsFind( "p" );
228 
229  /**********************************************
230  * show copyright
231  **********************************************/
232  if ( !m_quiet )
233  {
234  double vernum = 1 + (double)( ESCRIPT_FILE_VER_CURRENT / 100.0f );
235  ERROR_PRINT << "EScript Executor v" << vernum << "\n"
236  << POL_COPYRIGHT << "\n\n";
237  }
238 
239  /**********************************************
240  * show help
241  **********************************************/
242  if ( binArgs.size() == 1 )
243  {
244  showHelp();
245  return 0; // return "okay"
246  }
247 
248  /**********************************************
249  * dump script
250  **********************************************/
251  std::string fileName;
252  if ( programArgsFind( "v", &fileName ) && fileName != "" )
253  {
254  dumpScript( fileName );
255  return 0; // return "okay"
256  }
257 
258  /**********************************************
259  * execute all given scripts
260  **********************************************/
261  return runecl();
262 }
263 }
264 } // namespaces
265 
269 
270 int main( int argc, char* argv[] )
271 {
273  RunEclMain->start( argc, argv );
274 }
std::atomic< int > eobject_imp_count
int escript_execinstr_calls
Definition: escriptv.cpp:23
virtual void showHelp()
Definition: RunEclMain.cpp:56
bool programArgsFind(const std::string &filter, std::string *rest=nullptr) const
virtual int main()
Definition: RunEclMain.cpp:221
int strip_one(std::string &direc)
Definition: fileutil.cpp:71
const std::vector< std::string > & programArgs() const
void setDebugLevel(DEBUG_LEVEL level)
Definition: executor.h:369
void dumpScript(std::string fileName)
Definition: RunEclMain.cpp:68
SystemState systemstate
Definition: systemstate.cpp:12
#define ESCRIPT_FILE_VER_CURRENT
Definition: filefmt.h:44
void addModule(ExecutorModule *module)
Definition: executor.cpp:3032
std::atomic< int > eobject_imp_constructions
T * get() const
Definition: refptr.h:176
STL namespace.
EScriptConfig escript_config
void start(int argc, char *argv[])
Definition: ProgramMain.cpp:42
u64 escript_instr_cycles
Definition: escriptv.cpp:22
Definition: refptr.h:65
boost_utils::flyweight_initializers fw_inits
Definition: uvars.cpp:72
void display_bobjectimp_instances()
Definition: object.cpp:170
bool setProgram(EScriptProgram *prog)
Definition: executor.cpp:731
void load_fileaccess_cfg()
Definition: filemod.cpp:695
void dummy()
Definition: testmisc.cpp:23
void dump(std::ostream &os)
Definition: eprog3.cpp:19
unsigned int max_call_depth
Definition: config.h:15
int runeclScript(std::string fileName)
Definition: RunEclMain.cpp:92
int stringicmp(const S1 &str1, const S2 &str2)
Definition: stlutil.h:46
Plib::Package const * pkg
Definition: eprog.h:123
int read(const char *fname)
Definition: eprog_read.cpp:33
#define ERROR_PRINT
Definition: logfacility.h:230
std::string normalized_dir_form(const std::string &istr)
Definition: fileutil.cpp:25
void setViewMode(bool vm)
Definition: executor.h:370
#define INFO_PRINT
Definition: logfacility.h:223
virtual size_t sizeEstimate() const
Definition: executor.cpp:3099
Definition: berror.cpp:12
void load_packages(const std::string &basedir, bool quiet)
Definition: pkg.cpp:312