Line data Source code
1 : /* Copyright (C) 2026 Wildfire Games. 2 : * This file is part of 0 A.D. 3 : * 4 : * 0 A.D. is free software: you can redistribute it and/or modify 5 : * it under the terms of the GNU General Public License as published by 6 : * the Free Software Foundation, either version 2 of the License, or 7 : * (at your option) any later version. 8 : * 9 : * 0 A.D. is distributed in the hope that it will be useful, 10 : * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 : * GNU General Public License for more details. 13 : * 14 : * You should have received a copy of the GNU General Public License 15 : * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>. 16 : */ 17 : 18 : #include "precompiled.h" 19 : 20 : #include "Stats.h" 21 : 22 : #include "scriptinterface/Interface.h" 23 : 24 : #include <cstdint> 25 : #include <js/GCAPI.h> 26 : 27 : Script::CScriptStatsTable* g_ScriptStatsTable; 28 : 29 : namespace Script 30 : { 31 : 32 : enum 33 : { 34 : Row_MaxBytes, 35 : Row_Bytes, 36 : Row_NumberGC, 37 : NumberRows 38 : }; 39 : 40 0 : CScriptStatsTable::CScriptStatsTable() 41 : { 42 0 : } 43 : 44 0 : void CScriptStatsTable::Add(const Interface* scriptInterface, const std::string& title) 45 : { 46 0 : m_ScriptInterfaces.emplace_back(scriptInterface, title); 47 0 : } 48 : 49 0 : void CScriptStatsTable::Remove(const Interface* scriptInterface) 50 : { 51 0 : for (size_t i = 0; i < m_ScriptInterfaces.size(); ) 52 : { 53 0 : if (m_ScriptInterfaces[i].first == scriptInterface) 54 0 : m_ScriptInterfaces.erase(m_ScriptInterfaces.begin() + i); 55 : else 56 0 : ++i; 57 : } 58 0 : } 59 : 60 0 : CStr CScriptStatsTable::GetName() 61 : { 62 0 : return "script"; 63 : } 64 : 65 0 : CStr CScriptStatsTable::GetTitle() 66 : { 67 0 : return "Script statistics"; 68 : } 69 : 70 0 : size_t CScriptStatsTable::GetNumberRows() 71 : { 72 0 : return NumberRows; 73 : } 74 : 75 0 : const std::vector<ProfileColumn>& CScriptStatsTable::GetColumns() 76 : { 77 0 : m_ColumnDescriptions.clear(); 78 0 : m_ColumnDescriptions.push_back(ProfileColumn("Name", 200)); 79 0 : for (size_t i = 0; i < m_ScriptInterfaces.size(); ++i) 80 0 : m_ColumnDescriptions.push_back(ProfileColumn(m_ScriptInterfaces[i].second, 80)); 81 0 : return m_ColumnDescriptions; 82 : } 83 : 84 0 : CStr CScriptStatsTable::GetCellText(size_t row, size_t col) 85 : { 86 0 : switch(row) 87 : { 88 0 : case Row_MaxBytes: 89 0 : { 90 0 : if (col == 0) 91 0 : return "max nominal heap bytes"; 92 0 : uint32_t n = JS_GetGCParameter(m_ScriptInterfaces.at(col-1).first->GetGeneralJSContext(), JSGC_MAX_BYTES); 93 0 : return std::to_string(n); 94 : } 95 0 : case Row_Bytes: 96 0 : { 97 0 : if (col == 0) 98 0 : return "allocated bytes"; 99 0 : uint32_t n = JS_GetGCParameter(m_ScriptInterfaces.at(col-1).first->GetGeneralJSContext(), JSGC_BYTES); 100 0 : return std::to_string(n); 101 : } 102 0 : case Row_NumberGC: 103 0 : { 104 0 : if (col == 0) 105 0 : return "number of GCs"; 106 0 : uint32_t n = JS_GetGCParameter(m_ScriptInterfaces.at(col-1).first->GetGeneralJSContext(), JSGC_NUMBER); 107 0 : return std::to_string(n); 108 : } 109 0 : default: 110 0 : return "???"; 111 : } 112 : } 113 : 114 0 : AbstractProfileTable* CScriptStatsTable::GetChild(size_t /*row*/) 115 : { 116 0 : return 0; 117 : } 118 : 119 : } // namespace Script