OpendTect  6.3
_execbatch.h
Go to the documentation of this file.
1 #pragma once
2 
3 /*
4 ________________________________________________________________________
5 
6  (C) dGB Beheer B.V.; (LICENSE) http://opendtect.org/OpendTect_license.txt
7  Author: A.H. Lammertink
8  Date: 30-10-2003
9 ________________________________________________________________________
10 
11  The implementation of Execute_batch should be in the executable on
12  windows, but can be in a .so on *nix.
13  In order not to pollute batchprog.h, I've placed the implementation
14  into a separate file, which is included trough batchprog.h on win32
15  and included in batchprog.cc on *nix.
16 
17 */
18 
19 #include "commandlineparser.h"
20 #include "envvars.h"
21 #include "od_ostream.h"
22 #include "oddirs.h"
23 #include "oscommand.h"
24 #include "strmprov.h"
25 #include "hostdata.h"
26 
27 #ifdef __win__
28 # include <tchar.h>
29 # include <tlhelp32.h>
30 # include <Windows.h>
31 #else
32 # include "sys/resource.h"
33 #endif
34 
35 
36 #ifdef __win__
37 static void setBatchPriority( int argc, char** argv )
38 #else
39 static void setBatchPriority( int argc, char** argv, int pid )
40 #endif
41 {
42  const CommandLineParser clp( argc, argv );
43  float priority = mUdf(float);
44  clp.getVal( "priority", priority );
45 #ifdef __unix__
46  if ( mIsUdf(priority) )
47  {
48  int nicelvl = mUdf(int);
49  if ( !clp.getVal("nice",nicelvl) )
50  return;
51 
52  setpriority( PRIO_PROCESS, pid, nicelvl );
53  }
54  else
55  {
56  const int machprio =
57  OS::CommandExecPars::getMachinePriority( priority, false );
58  setpriority( PRIO_PROCESS, pid, machprio );
59  }
60 #else
61  if ( mIsUdf(priority) )
62  return;
63 
64  const int machprio =
66  const DWORD threadpriority =
67  machprio == 8 ? THREAD_PRIORITY_NORMAL
68  : ( machprio == 7 ? THREAD_PRIORITY_BELOW_NORMAL
69  : THREAD_PRIORITY_LOWEST );
70  if ( threadpriority != THREAD_PRIORITY_NORMAL )
71  SetPriorityClass( GetCurrentProcess(), BELOW_NORMAL_PRIORITY_CLASS );
72 
73  HANDLE curthread = INVALID_HANDLE_VALUE;
74  THREADENTRY32 threadlist;
75 
76  const DWORD dwOwnerPID( GetCurrentProcessId() );
77  curthread = CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, dwOwnerPID );
78  if ( curthread == INVALID_HANDLE_VALUE )
79  return;
80 
81  threadlist.dwSize = sizeof(THREADENTRY32);
82  if ( !Thread32First(curthread,&threadlist) )
83  { CloseHandle(curthread); return; }
84 
85  do
86  {
87  if ( threadlist.th32OwnerProcessID != dwOwnerPID )
88  continue;
89 
90  SetThreadPriority(curthread,threadpriority );
91  } while ( Thread32Next(curthread,&threadlist) );
92 
93  CloseHandle( curthread );
94 #endif
95 }
96 
97 int Execute_batch( int* pargc, char** argv )
98 {
99  PIM().loadAuto( false );
100 
101  BP().init();
102  if ( !BP().stillok_ )
103  return 1;
104  if ( BP().inbg_ )
105  ForkProcess();
106 
107  BatchProgram& bp = BP();
108  bool allok = bp.initOutput();
109  if ( allok )
110  {
111  od_ostream logstrm( *bp.sdout_.oStrm() );
112  const int pid = GetPID();
113 #ifdef __win__
114  setBatchPriority( *pargc, argv );
115 #else
116  setBatchPriority( *pargc, argv, pid );
117 #endif
118  logstrm << "Starting program: " << argv[0] << " " << bp.name() << "\n";
119  logstrm << "Processing on: " << HostData::localHostName() << "\n";
120  logstrm << "Process ID: " << pid << "\n";
121  allok = bp.go( logstrm );
122  }
123 
124  bp.stillok_ = allok;
125  BatchProgram::deleteInstance();
126 
127  return allok ? 0 : 1; // never reached.
128 }
#define mIsUdf(val)
Use mIsUdf to check for undefinedness of simple types.
Definition: undefval.h:285
OD class for stream write common access to the user log file, or std::cout in batch progs...
Definition: od_ostream.h:25
void ForkProcess(void)
bool go(od_ostream &log_stream)
This method must be defined by user.
StreamData & sdout_
Definition: batchprog.h:95
static int getMachinePriority(float priolevel, bool iswin)
void loadAuto(bool late)
see class comments
BatchProgram & BP()
Definition: commandlineparser.h:49
static void setBatchPriority(int argc, char **argv, int pid)
Definition: _execbatch.h:39
#define mUdf(type)
Use this macro to get the undefined for simple types.
Definition: undefval.h:270
std::ostream * oStrm() const
Definition: strmdata.h:51
Main object for &#39;standard&#39; batch programs.
Definition: batchprog.h:48
int Execute_batch(int *pargc, char **argv)
Definition: _execbatch.h:97
bool getVal(const char *key, BufferString &, bool acceptnone=false, int valnr=1) const
int GetPID(void)
bool stillok_
Definition: batchprog.h:93
PluginManager & PIM()
virtual const OD::String & name() const
Definition: namedobj.h:33
static const char * localHostName()

Generated at for the OpendTect seismic interpretation project. Copyright (C): dGB Beheer B. V. 2017