Pol  Revision:cb584c9
scrstore.cpp
Go to the documentation of this file.
1 
8 #include "scrstore.h"
9 
10 #include "../bscript/eprog.h"
11 #include "../clib/logfacility.h"
12 #include "../clib/rawtypes.h"
13 #include "../clib/strutil.h"
14 #include "../plib/systemstate.h"
16 #include "globals/state.h"
17 #include "polcfg.h"
18 #include "profile.h"
19 #include "scrdef.h"
20 #include <format/format.h>
21 
22 
23 namespace Pol
24 {
25 namespace Items
26 {
28 void preload_test_scripts( const std::string& );
29 }
30 namespace Core
31 {
33 {
34  ScriptStorage::iterator itr = scriptScheduler.scrstore.find( sd.name().c_str() );
35  return ( itr != scriptScheduler.scrstore.end() );
36 }
37 
38 ref_ptr<Bscript::EScriptProgram> find_script( const std::string& name, bool complain_if_not_found,
39  bool cache_script )
40 {
41  ScriptStorage::iterator itr = scriptScheduler.scrstore.find( name.c_str() );
42  if ( itr != scriptScheduler.scrstore.end() )
43  {
44  if ( cache_script )
45  {
46  return ( *itr ).second;
47  }
48  else
49  {
50  ref_ptr<Bscript::EScriptProgram> res( ( *itr ).second );
51  scriptScheduler.scrstore.erase( itr );
52  return res;
53  }
54  }
55 
57  std::string pathname = "scripts/";
58  pathname += name.c_str();
59  if ( name.find( ".ecl" ) == std::string::npos )
60  pathname += ".ecl";
61 
62  if ( program->read( pathname.c_str() ) != 0 )
63  {
64  if ( complain_if_not_found )
65  {
66  POLLOG_ERROR << "Unable to read script '" << pathname << "'\n";
67  }
69  }
70 
71  if ( cache_script )
72  {
73  std::string tmpname = name;
74  Clib::mklower( tmpname );
75  scriptScheduler.scrstore.insert( ScriptStorage::value_type( tmpname.c_str(), program ) );
76  }
77 
78  return program;
79 }
80 
81 // NOTE,we assume this has directory info (including scripts/ or pkg/xx)
82 // as well as ".ecl" on the end.
83 ref_ptr<Bscript::EScriptProgram> find_script2( const ScriptDef& script, bool complain_if_not_found,
84  bool cache_script )
85 {
86  ScriptStorage::iterator itr = scriptScheduler.scrstore.find( script.c_str() );
87  if ( itr != scriptScheduler.scrstore.end() )
88  return ( *itr ).second;
89 
91  program->pkg = script.pkg();
92 
93  if ( program->read( script.c_str() ) != 0 )
94  {
95  if ( complain_if_not_found )
96  {
97  POLLOG_ERROR << "Unable to read script '" << script.name() << "'\n";
98  }
100  }
101 
102  if ( cache_script )
103  {
104  std::string tmpname = script.name();
105  Clib::mklower( tmpname );
106  scriptScheduler.scrstore.insert( ScriptStorage::value_type( tmpname.c_str(), program ) );
107  }
108 
109  return program;
110 }
111 
112 
113 int unload_script( const std::string& name_in )
114 {
115  int n = 0;
116  ScriptStorage::iterator itr = scriptScheduler.scrstore.begin();
117  while ( itr != scriptScheduler.scrstore.end() )
118  {
119  ScriptStorage::iterator cur = itr;
120  ++itr;
121 
122  if ( strstr( cur->first.c_str(), name_in.c_str() ) )
123  {
124  std::string nm = cur->first;
125  const char* nm_cstr = nm.c_str();
126  INFO_PRINT << "Unloading " << nm_cstr << "\n";
127  scriptScheduler.scrstore.erase( cur );
128  ++n;
129 
130  // dave added 1/30/3, FIXME: if in the future we have to add any other scripts to
131  // auto-reload, put the names in a data structure :P
132  if ( strstr( nm_cstr, "unequiptest.ecl" ) )
133  {
134  Items::preload_test_scripts( std::string( "unequiptest.ecl" ) );
135  continue;
136  }
137 
138  if ( strstr( nm_cstr, "equiptest.ecl" ) )
139  {
140  Items::preload_test_scripts( std::string( "equiptest.ecl" ) );
141  continue;
142  }
143  }
144  }
145  return n;
146 }
147 
149 {
150  int n = static_cast<int>( scriptScheduler.scrstore.size() );
151  scriptScheduler.scrstore.clear();
152  Items::preload_test_scripts(); // dave added 1/30/3, no other time do we reload unequiptest and
153  // equiptest
154  return n;
155 }
156 
157 void log_all_script_cycle_counts( bool clear_counters )
158 {
159  u64 total_instr = 0;
160  for ( const auto& scr : scriptScheduler.scrstore )
161  {
162  total_instr += scr.second->instr_cycles;
163  }
164 
165  if ( Plib::systemstate.config.multithread )
166  {
167  POLLOG.Format( "Scheduler passes: {}\nScript passes: {}\n" )
168  << ( GET_PROFILEVAR( scheduler_passes ) ) << stateManager.profilevars.script_passes;
169  }
170  else
171  {
172  POLLOG.Format( "Total gameloop iterations: {}\n" ) << stateManager.profilevars.rotations;
173  }
174 
175  fmt::Writer tmp;
176  tmp.Format( "{:<38} {:>12} {:>6} {:>12} {:>6}\n" ) << "Script"
177  << "cycles"
178  << "incov"
179  << "cyc/invoc"
180  << "%";
181  for ( const auto& scr : scriptScheduler.scrstore )
182  {
183  Bscript::EScriptProgram* eprog = scr.second.get();
184  double cycle_percent =
185  total_instr != 0 ? ( static_cast<double>( eprog->instr_cycles ) / total_instr * 100.0 ) : 0;
186  tmp.Format( "{:<38} {:>12} {:>6} {:>12} {:>6}\n" )
187  << eprog->name << eprog->instr_cycles << eprog->invocations
188  << ( eprog->instr_cycles / ( eprog->invocations ? eprog->invocations : 1 ) )
189  << cycle_percent;
190  if ( clear_counters )
191  {
192  eprog->instr_cycles = 0;
193  eprog->invocations = eprog->count() - 1; // 1 count is the scrstore's
194  }
195  }
196  POLLOG << tmp.str();
197  if ( clear_counters )
198  POLLOG << "Profiling counters cleared.\n";
199 }
200 
201 
203 {
204  for ( const auto& scr : scriptScheduler.scrstore )
205  {
206  Bscript::EScriptProgram* eprog = scr.second.get();
207  eprog->instr_cycles = 0;
208  eprog->invocations = eprog->count() - 1; // 1 count is the scrstore's
209  }
210 
211  POLLOG << "Profiling counters cleared.\n";
212 }
213 }
214 }
#define GET_PROFILEVAR(counter)
Definition: profile.h:99
bool script_loaded(ScriptDef &sd)
Definition: scrstore.cpp:32
const Plib::Package * pkg() const
Definition: scrdef.h:46
ref_ptr< Bscript::EScriptProgram > find_script2(const ScriptDef &script, bool complain_if_not_found, bool cache_script)
Definition: scrstore.cpp:83
SystemState systemstate
Definition: systemstate.cpp:12
int unload_all_scripts()
Definition: scrstore.cpp:148
void log_all_script_cycle_counts(bool clear_counters)
Definition: scrstore.cpp:157
void preload_test_scripts(const std::string &script_ecl)
Definition: item.cpp:1082
void clear_script_profile_counters()
Definition: scrstore.cpp:202
boost_utils::script_name_flystring name
Definition: eprog.h:91
#define POLLOG_ERROR
Definition: logfacility.h:207
ProfileVars profilevars
Definition: state.h:47
int unload_script(const std::string &name_in)
Definition: scrstore.cpp:113
const std::string & name() const
Definition: scrdef.h:45
unsigned long long u64
Definition: rawtypes.h:38
#define POLLOG
Definition: logfacility.h:219
unsigned int count() const
Definition: refptr.h:130
StateManager stateManager
Definition: state.cpp:8
ScriptScheduler scriptScheduler
const char * c_str() const
Definition: scrdef.h:42
std::string name
Definition: osmod.cpp:943
void mklower(std::string &str)
Definition: strutil.cpp:266
ref_ptr< Bscript::EScriptProgram > find_script(const std::string &name, bool complain_if_not_found, bool cache_script)
Definition: scrstore.cpp:38
#define INFO_PRINT
Definition: logfacility.h:223
unsigned int invocations
Definition: eprog.h:121
Definition: berror.cpp:12