Loading

Paste #pz2hnzzxc

  1. /** Finish a cycle of a measured element and store the measurement taken. */
  2. PerformanceMeasurer::~PerformanceMeasurer()
  3. {
  4.     if (this->elem == PFE_ALLSCRIPTS) {
  5.         /* Hack to not record scripts total when no scripts are active */
  6.         bool any_active = _pf_data[PFE_GAMESCRIPT].num_valid > 0;
  7.         for (uint e = PFE_AI0; e < PFE_MAX; e++) any_active |= _pf_data[e].num_valid > 0;
  8.         if (!any_active) {
  9.             PerformanceMeasurer::SetInactive(PFE_ALLSCRIPTS);
  10.             return;
  11.         }
  12.     }
  13.     _pf_data[this->elem].Add(this->start_time, GetPerformanceTimer());
  14.  
  15.     /* Self-adjust max opcodes for active scripts */
  16.     if (this->elem >= PFE_GAMESCRIPT && this->elem <= PFE_AI14) {
  17.         uint active_scripts = Game::GetInstance() != NULL && !Game::GetInstance()->IsDead() && !Game::GetInstance()->IsPaused();
  18.         Company *c;
  19.         FOR_ALL_COMPANIES(c) {
  20.             if (Company::IsValidAiID(c->index) && Company::Get(c->index)->ai_instance != NULL && !Company::Get(c->index)->ai_instance->IsDead() && !Company::Get(c->index)->ai_instance->IsPaused()) {
  21.                 active_scripts++;
  22.             }
  23.         }
  24.  
  25.         if (active_scripts != 0 && (this->elem == PFE_GAMESCRIPT ? Game::GetInstance() != NULL && !Game::GetInstance()->IsDead() && !Game::GetInstance()->IsPaused() : Company::IsValidAiID((CompanyID)(this->elem - PFE_AI0)) && Company::Get((CompanyID)(this->elem - PFE_AI0))->ai_instance != NULL && !Company::Get((CompanyID)(this->elem - PFE_AI0))->ai_instance->IsDead() && !Company::Get((CompanyID)(this->elem - PFE_AI0))->ai_instance->IsPaused())) {
  26.             uint dummy; // unused
  27.             const SettingDesc *sd = GetSettingFromName("script_max_opcode_till_suspend", &dummy);
  28.             assert(sd != NULL);
  29.             uint opcodes = this->elem == PFE_GAMESCRIPT ? Game::GetMaxOpCodes() : AI::GetMaxOpCodes((CompanyID)(this->elem - PFE_AI0));
  30.             uint value = opcodes;
  31.             double avg = min(9999.99, _pf_data[this->elem].GetAverageDurationMilliseconds(GL_RATE));
  32.             double all = min(9999.99, _pf_data[PFE_ALLSCRIPTS].GetAverageDurationMilliseconds(GL_RATE));
  33.             if (avg * active_scripts > GL_RATE && all > GL_RATE) {
  34.                 value = Clamp(opcodes - (avg * active_scripts - GL_RATE) * (avg * active_scripts - GL_RATE), sd->desc.min, GetGameSettings().script.script_max_opcode_till_suspend);
  35.             } else if (avg > 0 && avg < GL_RATE / 3 || all < GL_RATE / 3) {
  36.                 value = Clamp(opcodes + GL_RATE / 3 - avg, sd->desc.min, GetGameSettings().script.script_max_opcode_till_suspend);
  37.             }
  38.             if (value != opcodes) {
  39.                 if (this->elem == PFE_GAMESCRIPT) {
  40.                     Game::SetMaxOpCodes(value);
  41.                 } else {
  42.                     AI::SetMaxOpCodes((CompanyID)(this->elem - PFE_AI0), value);
  43.                 }
  44.             }
  45.         }
  46.     }
  47. }

Comments