Welcome to mirror list, hosted at ThFree Co, Russian Federation.

asprofiler.cpp « angelscript « Scripting « Source - github.com/WolfireGames/overgrowth.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 09338480a047846da879ea58df7aa2ea0a50512e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
//-----------------------------------------------------------------------------
//           Name: asprofiler.cpp
//      Developer: Wolfire Games LLC
//    Description:
//        License: Read below
//-----------------------------------------------------------------------------
//
//   Copyright 2022 Wolfire Games LLC
//
//   Licensed under the Apache License, Version 2.0 (the "License");
//   you may not use this file except in compliance with the License.
//   You may obtain a copy of the License at
//
//       http://www.apache.org/licenses/LICENSE-2.0
//
//   Unless required by applicable law or agreed to in writing, software
//   distributed under the License is distributed on an "AS IS" BASIS,
//   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//   See the License for the specific language governing permissions and
//   limitations under the License.
//
//-----------------------------------------------------------------------------
#include "asprofiler.h"

#include <Compat/time.h>
#include <Scripting/angelscript/ascontext.h>
#include <Logging/logdata.h>

#include <algorithm>

std::vector<ASContext*> ASProfiler::active_contexts;

ASProfiler::ASProfiler()
    : enabled(false)
    , last_active_zone(NULL)
    , active_zone(NULL)
    , id_counter(0)
    , window_counter(0)
{}

ASProfiler::~ASProfiler() {
    for(size_t i = 0; i < zone_times.size(); ++i)
        DeleteZone(zone_times[i]);
}

void ASProfiler::Update() {
    if(window_counter == kMaxWindowSize) {
        for(size_t i = 0; i < zone_times.size(); ++i) { 
            MoveForward(zone_times[i]);
        }
        for(size_t i = 0; i < script_function_times.size(); ++i) { 
            MoveForward(&script_function_times[i]);
        }
        window_counter = 0;
    } else {
        window_counter++;
    }
}

static float TimeGetter(void* data, int idx)
{
    return (float)(((uint64_t*)data)[idx]) * 0.001f;
}

static char buffer[256];
void ASProfiler::Draw() {
    if(active_zone != NULL) {
        LOGW << active_zone->name << " isn't closed before drawing the AS profiler, have you forgotten to close it? It will be automatically closed" << std::endl;
        active_zone = NULL;
    }

    uint64_t zone_time = 0;
    for(size_t i = 0; i < zone_times.size(); ++i) { 
        zone_time += zone_times[i]->times.back();
    }

    sprintf(buffer, "%s: %f###%p", context->current_script.GetOriginalPath(), zone_time * 0.001f, (void*)context);
    if(ImGui::TreeNode(buffer)) {
        for(size_t i = 0; i < script_function_times.size(); ++i) {
            ZoneTime* zone = &script_function_times[i];
            ImGui::Text("%s", zone->name.c_str());
            ImGui::PlotHistogram("", &TimeGetter, zone->times.data(), kTimeHistorySize, 0, NULL, 0.0f, 5000.0f, ImVec2(0,40));
        }
            
        for(size_t i = 0; i < zone_times.size(); ++i) {
            DrawZone(zone_times[i]);
        }
        ImGui::TreePop();
    }
}

void ASProfiler::AddContext(ASContext* context) {
    bool add = true;
    for(size_t i = 0; i < active_contexts.size(); ++i) {
        if(active_contexts[i] == context) {
            add = false;
            break;
        }
    }

    if(add)
        active_contexts.push_back(context);
}

void ASProfiler::RemoveContext(ASContext* context) {
    for(size_t i = 0; i < active_contexts.size(); ++i) {
        if(active_contexts[i] == context) {
            active_contexts.erase(active_contexts.begin() + i);
            break;
        }
    }
}

const std::vector<ASContext*>& ASProfiler::GetActiveContexts() {
    return active_contexts;
}

void ASProfiler::SetContext(ASContext* context) {
    this->context = context;
}

void ASProfiler::ASEnterTelemetryZone( const std::string& str ) {
    if(!enabled)
        return;

    bool found = false;
    if(active_zone == NULL) {
        for(size_t i = 0; i < zone_times.size(); ++i) {
            if(zone_times[i]->name == str) {
                active_zone = zone_times[i];
                found = true;
                break;
            }
        }

        if(!found) {
            ZoneTime* newZone = new ZoneTime;
            newZone->name = str;
            newZone->parent = NULL;
            newZone->times.resize(kTimeHistorySize, 0);
            newZone->id = id_counter++;
            zone_times.push_back(newZone);
            active_zone = zone_times.back();
        }
    } else {
        for(size_t i = 0; i < active_zone->children.size(); ++i) {
            if(active_zone->children[i]->name == str) {
                active_zone = active_zone->children[i];
                found = true;
                break;
            }
        }

        if(!found) {
            ZoneTime* newZone = new ZoneTime;
            newZone->name = str;
            newZone->parent = active_zone;
            newZone->times.resize(kTimeHistorySize, 0);
            newZone->id = id_counter++;
            active_zone->children.push_back(newZone);
            active_zone = active_zone->children.back();
        }
    }

    active_zone->temp_time = GetPrecisionTime();
}

void ASProfiler::ASLeaveTelemetryZone() {
    uint64_t end_time = GetPrecisionTime();
    if(!enabled) {
        return;
    } else if(active_zone == NULL) {
        if(last_active_zone == NULL)
            LOGE << "LeaveTelemetryZone called from script file without calling EnterTelemetryZone first, no last active zone" << std::endl;
        else
            LOGE << "LeaveTelemetryZone called from script file without calling EnterTelemetryZone first, last active zone was " << last_active_zone->name << std::endl;

        return;
    }

    uint64_t duration = ToNanoseconds(end_time) - ToNanoseconds(active_zone->temp_time);
    active_zone->times[kTimeHistorySize - 1] = std::max(duration, active_zone->times[kTimeHistorySize - 1]);

    last_active_zone = active_zone;
    active_zone = active_zone->parent;
}

void ASProfiler::CallScriptFunction(const char* str) {
    if(!enabled)
        return;

    ZoneTime* zone = NULL;
    for(size_t i = 0; i < script_function_times.size(); ++i) {
        if(strcmp(script_function_times[i].name.c_str(), str) == 0) {
            zone = &script_function_times[i];
            break;
        }
    }

    if(!zone) {
        ZoneTime newZone;
        newZone.name = str;
        newZone.parent = NULL;
        newZone.id = -1;
        newZone.times.resize(kTimeHistorySize);
        script_function_times.push_back(newZone);
        zone = &script_function_times.back();
    }

    last_script_function_zone = zone;
    zone->temp_time = GetPrecisionTime();
}

void ASProfiler::LeaveScriptFunction() {
    uint64_t end_time = GetPrecisionTime();
    if(!enabled)
        return;

    if(!last_script_function_zone)
        return; // Shouldn't happen

    uint64_t duration = ToNanoseconds(end_time) - ToNanoseconds(last_script_function_zone->temp_time);
    last_script_function_zone->times[kTimeHistorySize - 1] = std::max(duration, last_script_function_zone->times[kTimeHistorySize - 1]);
    last_script_function_zone = NULL;
}

void ASProfiler::DeleteZone(ZoneTime* zone) {
    for(size_t i = 0; i < zone->children.size(); ++i) {
        DeleteZone(zone->children[i]);
    }

    delete zone;
}

void ASProfiler::MoveForward(ZoneTime* zone) {
    memmove(zone->times.data(), zone->times.data() + 1, sizeof(uint64_t) * (kTimeHistorySize - 1));
    zone->times[kTimeHistorySize - 1] = 0;

    for(size_t i = 0; i < zone->children.size(); ++i) {
        MoveForward(zone->children[i]);
    }
}

void ASProfiler::DrawZone(ZoneTime* zone) {
    uint64_t zone_time = zone->times.back();

    sprintf(buffer, "%s: %f###%d", zone->name.c_str(), zone_time * 0.001f, zone->id);
    if(ImGui::TreeNode(buffer)) {
        ImGui::PlotHistogram("", &TimeGetter, zone->times.data(), kTimeHistorySize, 0, NULL, 0.0f, 5000.0f, ImVec2(0,40));
        for(size_t i = 0; i < zone->children.size(); ++i) { 
            DrawZone(zone->children[i]);
        }
        ImGui::TreePop();
    }
}