| 1 | /* |
| 2 | * Descent 3 |
| 3 | * Copyright (C) 2024 Parallax Software |
| 4 | * |
| 5 | * This program is free software: you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation, either version 3 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | |
| 18 | --- HISTORICAL COMMENTS FOLLOW --- |
| 19 | |
| 20 | * $Logfile: /DescentIII/Main/rtperformance/rtperformance.cpp $ |
| 21 | * $Revision: 9 $ |
| 22 | * $Date: 10/21/99 3:21p $ |
| 23 | * $Author: Kevin $ |
| 24 | * |
| 25 | * Run time performance lib |
| 26 | * |
| 27 | * $Log: /DescentIII/Main/rtperformance/rtperformance.cpp $ |
| 28 | * |
| 29 | * 9 10/21/99 3:21p Kevin |
| 30 | * Mac merge |
| 31 | * |
| 32 | * 8 5/07/99 6:21p Jeff |
| 33 | * hooked up fvi to rtperformance |
| 34 | * |
| 35 | * 7 4/16/99 4:04a Jeff |
| 36 | * fixed up for non-window's builds |
| 37 | * |
| 38 | * 6 4/16/99 1:59a Jeff |
| 39 | * ifdef out windows.h for now windows builds |
| 40 | * |
| 41 | * 5 10/19/98 7:51p Kevin |
| 42 | * performance testing |
| 43 | * |
| 44 | * 4 10/19/98 2:48p Jeff |
| 45 | * changed define for rtp performance...just define USE_RTP in |
| 46 | * rtperformance.h in order to enable it |
| 47 | * |
| 48 | * 3 10/17/98 6:05p Jeff |
| 49 | * it helps to write the heading to file |
| 50 | * |
| 51 | * 2 10/17/98 5:58p Jeff |
| 52 | * put in heading |
| 53 | * |
| 54 | * $NoKeywords: $ |
| 55 | */ |
| 56 | |
| 57 | #include <algorithm> |
| 58 | #include <cstdio> |
| 59 | #include <cstdlib> |
| 60 | #include <cstring> |
| 61 | |
| 62 | #if defined(WIN32) |
| 63 | #include <windows.h> |
| 64 | #endif |
| 65 | |
| 66 | #include "cfile.h"cxx . . $ `<file>/cfile/cfile.h`/ |
| 67 | #include "ddio.h"cxx . . $ `<file>/ddio/ddio.h`/ |
| 68 | #include "manage.h"cxx . . $ `<file>/lib/manage.h`/ |
| 69 | #include "mono.h"cxx . . $ `<file>/ddebug/mono.h`/ |
| 70 | #include "rtperformance.h"cxx . . $ `<file>/rtperformance/rtperformance.h`/ |
| 71 | |
| 72 | float rtp_startlog_timecxx . . $ rtp_startlog_time.; |
| 73 | |
| 74 | // maximum number of samples before we stop and autoflush |
| 75 | #define MAX_RTP_SAMPLEScxx . . $ `rtperformance/rtperformance.cpp:75:9`! 3800 // this is a little more than whats needed for 2 minutes at 30fps |
| 76 | |
| 77 | // Internal Global Vars |
| 78 | // ------------------------------ |
| 79 | INT64cxx . . $ `rtperformance/rtperformance.h:44:9`! Runtime_performance_flagscxx . . $ Runtime_performance_flags. = RTI_FRAMETIMEcxx . . $ `rtperformance/rtperformance.h:92:9`!; |
| 80 | INT64cxx . . $ `rtperformance/rtperformance.h:44:9`! Runtime_performance_clockfreqcxx . . $ Runtime_performance_clockfreq. = 0; |
| 81 | INT64cxx . . $ `rtperformance/rtperformance.h:44:9`! Runtime_performance_frame_countercxx . . $ Runtime_performance_frame_counter. = 0; |
| 82 | uint32_tcxx . . $ uint32_t# Runtime_performance_countercxx . . $ Runtime_performance_counter. = 0; |
| 83 | uint8_tcxx . . $ uint8_t# Runtime_performance_enabledcxx . . $ Runtime_performance_enabled. = 0; |
| 84 | |
| 85 | tRTFrameInfocxx . . $ tRTFrameInfo# RTP_SingleFramecxx . . $ RTP_SingleFrame.; |
| 86 | #ifdef USE_RTP |
| 87 | tRTFrameInfo RTP_FrameBuffer[MAX_RTP_SAMPLES]; |
| 88 | #endif |
| 89 | |
| 90 | void rtp_Closecxx . . $ rtp_Close(49f6e7a06ebc5aa8).(); |
| 91 | |
| 92 | /* |
| 93 | void rtp_WriteBufferLog |
| 94 | Writes the buffer of frames to file... |
| 95 | */ |
| 96 | void rtp_WriteBufferLogcxx . . $ rtp_WriteBufferLog(49f6e7a06ebc5aa8).() { |
| 97 | #ifdef USE_RTP |
| 98 | Runtime_performance_enabled = 1; // make sure it's enabled for the macros |
| 99 | // determine how many frames to write out |
| 100 | uint32_t Num_frames; |
| 101 | uint32_t counter; |
| 102 | char buffer[4096]; |
| 103 | |
| 104 | Num_frames = std::min<unsigned>(Runtime_performance_counter, MAX_RTP_SAMPLES); |
| 105 | |
| 106 | // Open the log file for writing |
| 107 | CFILE *file = cfopen(std::filesystem::path(LocalD3Dir) / "D3Performance.txt", "wt"); |
| 108 | |
| 109 | if (file) { |
| 110 | mprintf(0, "RTP: Recording Log\n"); |
| 111 | |
| 112 | strcpy(buffer, "FrameNum,FrameTime,RenderFrameTime,MultiFrameTime,MusicFrameTime,AmbientSoundTime,WeatherFrameTime," |
| 113 | "PlayerFrameTime,DoorwayFrameTime,LevelGoalFrameTime,MatCenFrameTime,ObjectFrameTime,AIFrameAllTime," |
| 114 | "ProcessKeysTime,REN:NumTexturesUploaded,REN:PolysDrawn,OBJ:CT_FlyingTime,OBJ:CT_AIDoFrameTime,OBJ:" |
| 115 | "CT_WeaponFrameTime,OBJ:CT_ExplosionFrameTime,OBJ:CT_DebrisFrameTime,OBJ:CT_SplinterFrameTime,OBJ:" |
| 116 | "MT_PhsyicsFrameTime,OBJ:MT_WalkingFrame,OBJ:MT_ShockWaveTime,OBJ:DoEffectTime,OBJ:MovePlayerTime," |
| 117 | "OBJ:D3XIntervalTime,OBJ:ObjLightTime,FRAME:NormalEventTime,AnimCycle,VisEffectMoveAll," |
| 118 | "DoPhysLinkedFrame,ObjDoFrame,NumFVICalls,FVITime"); |
| 119 | cf_WriteString(file, buffer); |
| 120 | |
| 121 | // Loop through all the frames, and write out the data for each frame |
| 122 | for (counter = 0; counter < Num_frames; counter++) { |
| 123 | tRTFrameInfo *fi = &RTP_FrameBuffer[counter]; |
| 124 | double renderframe_time; |
| 125 | double multiframe_time; |
| 126 | double musicframe_time; |
| 127 | double ambsound_frame_time; |
| 128 | double weatherframe_time; |
| 129 | double playerframe_time; |
| 130 | double doorframe_time; |
| 131 | double levelgoal_time; |
| 132 | double matcenframe_time; |
| 133 | double objframe_time; |
| 134 | double aiframeall_time; |
| 135 | double processkeys_time; |
| 136 | double ct_flying_time; |
| 137 | double ct_aidoframe_time; |
| 138 | double ct_weaponframe_time; |
| 139 | double ct_explosionframe_time; |
| 140 | double ct_debrisframe_time; |
| 141 | double ct_splinterframe_time; |
| 142 | double mt_physicsframe_time; |
| 143 | double mt_walkingframe_time; |
| 144 | double mt_shockwave_time; |
| 145 | double obj_doeffect_time; |
| 146 | double obj_move_player_time; |
| 147 | double obj_d3xint_time; |
| 148 | double obj_objlight_time; |
| 149 | double normalevent_time; |
| 150 | double cycle_anim; |
| 151 | double vis_eff_move; |
| 152 | double phys_link; |
| 153 | double obj_do_frm; |
| 154 | double fvi_time; |
| 155 | |
| 156 | RTP_CLOCKSECONDS(fi->renderframe_time, renderframe_time); |
| 157 | RTP_CLOCKSECONDS(fi->multiframe_time, multiframe_time); |
| 158 | RTP_CLOCKSECONDS(fi->musicframe_time, musicframe_time); |
| 159 | RTP_CLOCKSECONDS(fi->ambsound_frame_time, ambsound_frame_time); |
| 160 | RTP_CLOCKSECONDS(fi->weatherframe_time, weatherframe_time); |
| 161 | RTP_CLOCKSECONDS(fi->playerframe_time, playerframe_time); |
| 162 | RTP_CLOCKSECONDS(fi->doorframe_time, doorframe_time); |
| 163 | RTP_CLOCKSECONDS(fi->levelgoal_time, levelgoal_time); |
| 164 | RTP_CLOCKSECONDS(fi->matcenframe_time, matcenframe_time); |
| 165 | RTP_CLOCKSECONDS(fi->objframe_time, objframe_time); |
| 166 | RTP_CLOCKSECONDS(fi->aiframeall_time, aiframeall_time); |
| 167 | RTP_CLOCKSECONDS(fi->processkeys_time, processkeys_time); |
| 168 | RTP_CLOCKSECONDS(fi->ct_flying_time, ct_flying_time); |
| 169 | RTP_CLOCKSECONDS(fi->ct_aidoframe_time, ct_aidoframe_time); |
| 170 | RTP_CLOCKSECONDS(fi->ct_weaponframe_time, ct_weaponframe_time); |
| 171 | RTP_CLOCKSECONDS(fi->ct_explosionframe_time, ct_explosionframe_time); |
| 172 | RTP_CLOCKSECONDS(fi->ct_debrisframe_time, ct_debrisframe_time); |
| 173 | RTP_CLOCKSECONDS(fi->ct_splinterframe_time, ct_splinterframe_time); |
| 174 | RTP_CLOCKSECONDS(fi->mt_physicsframe_time, mt_physicsframe_time); |
| 175 | RTP_CLOCKSECONDS(fi->mt_walkingframe_time, mt_walkingframe_time); |
| 176 | RTP_CLOCKSECONDS(fi->mt_shockwave_time, mt_shockwave_time); |
| 177 | RTP_CLOCKSECONDS(fi->obj_doeffect_time, obj_doeffect_time); |
| 178 | RTP_CLOCKSECONDS(fi->obj_move_player_time, obj_move_player_time); |
| 179 | RTP_CLOCKSECONDS(fi->obj_d3xint_time, obj_d3xint_time); |
| 180 | RTP_CLOCKSECONDS(fi->obj_objlight_time, obj_objlight_time); |
| 181 | RTP_CLOCKSECONDS(fi->normalevent_time, normalevent_time); |
| 182 | RTP_CLOCKSECONDS(fi->cycle_anim, cycle_anim); |
| 183 | RTP_CLOCKSECONDS(fi->vis_eff_move, vis_eff_move); |
| 184 | RTP_CLOCKSECONDS(fi->phys_link, phys_link); |
| 185 | RTP_CLOCKSECONDS(fi->obj_do_frm, obj_do_frm); |
| 186 | RTP_CLOCKSECONDS(fi->fvi_time, fvi_time); |
| 187 | |
| 188 | snprintf(buffer, sizeof(buffer), |
| 189 | "%d,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%d,%d,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f " |
| 190 | "%d %lld", |
| 191 | (int)fi->frame_num, fi->frame_time, renderframe_time, multiframe_time, musicframe_time, |
| 192 | ambsound_frame_time, weatherframe_time, playerframe_time, doorframe_time, levelgoal_time, |
| 193 | matcenframe_time, objframe_time, aiframeall_time, processkeys_time, fi->texture_uploads, fi->polys_drawn, |
| 194 | ct_flying_time, ct_aidoframe_time, ct_weaponframe_time, ct_explosionframe_time, ct_debrisframe_time, |
| 195 | ct_splinterframe_time, mt_physicsframe_time, mt_walkingframe_time, mt_shockwave_time, obj_doeffect_time, |
| 196 | obj_move_player_time, obj_d3xint_time, obj_objlight_time, normalevent_time, cycle_anim, vis_eff_move, |
| 197 | phys_link, obj_do_frm, fi->fvi_calls, fi->fvi_time); |
| 198 | |
| 199 | cf_WriteString(file, buffer); |
| 200 | } |
| 201 | |
| 202 | // Close the log file |
| 203 | cfclose(file); |
| 204 | } else |
| 205 | mprintf(0, "RTP: Unable to open log for writing\n"); |
| 206 | #endif |
| 207 | } |
| 208 | |
| 209 | /* |
| 210 | void rtp_RecordFrame |
| 211 | Calling this will record the data of the frame into the internal log, and prepare for |
| 212 | the next frame |
| 213 | */ |
| 214 | void rtp_RecordFramecxx . . $ rtp_RecordFrame(49f6e7a06ebc5aa8).() { |
| 215 | #ifdef USE_RTP |
| 216 | if (Runtime_performance_enabled) { |
| 217 | // do our saving of information |
| 218 | // -------------------------------- |
| 219 | |
| 220 | RTP_SingleFrame.frame_num = Runtime_performance_frame_counter; // save the frame num |
| 221 | |
| 222 | // Copy everything into the buffer, and advance the buffer |
| 223 | memcpy(&RTP_FrameBuffer[Runtime_performance_counter], &RTP_SingleFrame, sizeof(tRTFrameInfo)); |
| 224 | |
| 225 | Runtime_performance_counter++; |
| 226 | |
| 227 | if (Runtime_performance_counter >= MAX_RTP_SAMPLES) { |
| 228 | // uh oh, we hit the end of our samples, flush the buffer and stop |
| 229 | rtp_StopLog(); |
| 230 | } |
| 231 | |
| 232 | // reset our global struct to zero everything out |
| 233 | // ------------------------------------------------ |
| 234 | memset(&RTP_SingleFrame, 0, sizeof(tRTFrameInfo)); |
| 235 | } |
| 236 | |
| 237 | // Update the frame counter to the next frame, always |
| 238 | Runtime_performance_frame_counter++; |
| 239 | #endif |
| 240 | } |
| 241 | |
| 242 | // External Functions |
| 243 | // ----------------------------- |
| 244 | /* |
| 245 | void rtp_Init |
| 246 | This function initilizes the runtime performance system so it's ready to be used |
| 247 | */ |
| 248 | void rtp_Initcxx . . $ rtp_Init(49f6e7a06ebc5aa8).() { |
| 249 | #ifdef USE_RTP |
| 250 | Runtime_performance_flags = 0; |
| 251 | Runtime_performance_clockfreq = 0; |
| 252 | Runtime_performance_frame_counter = 0; |
| 253 | Runtime_performance_counter = 0; |
| 254 | Runtime_performance_enabled = 0; |
| 255 | |
| 256 | LARGE_INTEGER freq; |
| 257 | if (!QueryPerformanceFrequency(&freq)) { |
| 258 | // there is no hi-res clock available....ummmm |
| 259 | mprintf(0, "RTP: No Hi-Resolution clock available on this system!!!!!!\n"); |
| 260 | // well, this isn't good...what to do, what to do? |
| 261 | } |
| 262 | |
| 263 | // Setup the clock freq....we'll need this info later I guess when converting to seconds |
| 264 | Runtime_performance_clockfreq = freq.QuadPart; |
| 265 | rtp_EnableFlags(RTI_FRAMETIME); |
| 266 | |
| 267 | atexit(rtp_Close); |
| 268 | #endif |
| 269 | } |
| 270 | |
| 271 | /* |
| 272 | void rtp_Close |
| 273 | This function shuts down the runtime performance system when the game is done |
| 274 | */ |
| 275 | void rtp_Closecxx . . $ rtp_Close(49f6e7a06ebc5aa8).() { |
| 276 | #ifdef USE_RTP |
| 277 | if (Runtime_performance_enabled) { |
| 278 | // Save the log out, since it was currently logging |
| 279 | rtp_StopLog(); |
| 280 | } |
| 281 | #endif |
| 282 | } |
| 283 | |
| 284 | /* |
| 285 | void rtp_EnableFlags |
| 286 | This function enables the performance profiling of whatever flags you pass in |
| 287 | */ |
| 288 | void rtp_EnableFlagscxx . . $ rtp_EnableFlags(ac50b47256ae7414).(INT64cxx . . $ `rtperformance/rtperformance.h:44:9`! flags) { |
| 289 | #ifdef USE_RTP |
| 290 | Runtime_performance_flags |= flags; |
| 291 | #endif |
| 292 | } |
| 293 | |
| 294 | /* |
| 295 | void rtp_DisableFlags |
| 296 | This function disables the performance profiling of whatever flags you pass in |
| 297 | */ |
| 298 | void rtp_DisableFlagscxx . . $ rtp_DisableFlags(ac50b47256ae7414).(INT64cxx . . $ `rtperformance/rtperformance.h:44:9`! flags) { |
| 299 | #ifdef USE_RTP |
| 300 | Runtime_performance_flags &= ~flags; |
| 301 | #endif |
| 302 | } |
| 303 | |
| 304 | /* |
| 305 | void rtp_StartLog |
| 306 | Calling this function will reset the log and start a new log, recording immediatly |
| 307 | */ |
| 308 | void rtp_StartLogcxx . . $ rtp_StartLog(49f6e7a06ebc5aa8).() { |
| 309 | #ifdef USE_RTP |
| 310 | mprintf(0, "RTP: Starting Log\n"); |
| 311 | Runtime_performance_counter = 0; |
| 312 | Runtime_performance_enabled = 1; |
| 313 | memset(&RTP_SingleFrame, 0, sizeof(tRTFrameInfo)); |
| 314 | rtp_startlog_time = timer_GetTime(); |
| 315 | #endif |
| 316 | } |
| 317 | |
| 318 | /* |
| 319 | void rtp_StopLog |
| 320 | Calling this function will stop the currently processing log and write it out to |
| 321 | file. |
| 322 | */ |
| 323 | void rtp_StopLogcxx . . $ rtp_StopLog(49f6e7a06ebc5aa8).() { |
| 324 | #ifdef USE_RTP |
| 325 | mprintf(0, "Recorded performance for %f seconds\n", timer_GetTime() - rtp_startlog_time); |
| 326 | mprintf(0, "RTP: Stopping Log\n"); |
| 327 | |
| 328 | // Save out the log now |
| 329 | rtp_WriteBufferLog(); |
| 330 | |
| 331 | Runtime_performance_enabled = 0; |
| 332 | #endif |
| 333 | } |
| 334 | |
| 335 | /* |
| 336 | void rtp_PauseLog |
| 337 | Calling this function will pause the log recording until rtp_ResumeLog is called |
| 338 | */ |
| 339 | void rtp_PauseLogcxx . . $ rtp_PauseLog(49f6e7a06ebc5aa8).() { |
| 340 | #ifdef USE_RTP |
| 341 | mprintf(0, "RTP: Pausing Log\n"); |
| 342 | Runtime_performance_enabled = 0; |
| 343 | #endif |
| 344 | } |
| 345 | |
| 346 | /* |
| 347 | void rtp_ResumeLog |
| 348 | Calling this will resume a paused log, starting at where it left off |
| 349 | */ |
| 350 | void rtp_ResumeLogcxx . . $ rtp_ResumeLog(49f6e7a06ebc5aa8).() { |
| 351 | #ifdef USE_RTP |
| 352 | mprintf(0, "RTP: Resuming Log\n"); |
| 353 | Runtime_performance_enabled = 1; |
| 354 | #endif |
| 355 | } |
| 356 | |
| 357 | // Internal Functions |
| 358 | // ------------------------------- |
| 359 | /* |
| 360 | INT64 rtp_GetClock |
| 361 | Returns the current hi-resolution clock value...no checking for overflow |
| 362 | */ |
| 363 | INT64cxx . . $ `rtperformance/rtperformance.h:44:9`! rtp_GetClockcxx . . $ rtp_GetClock(7c4be072098b358f).() { |
| 364 | #ifdef USE_RTP |
| 365 | LARGE_INTEGER t; |
| 366 | QueryPerformanceCounter(&t); |
| 367 | return (INT64)t.QuadPart; |
| 368 | #else |
| 369 | return 0; |
| 370 | #endif |
| 371 | } |