| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636 |
- /*
- ** Command & Conquer Generals Zero Hour(tm)
- ** Copyright 2025 Electronic Arts Inc.
- **
- ** This program is free software: you can redistribute it and/or modify
- ** it under the terms of the GNU General Public License as published by
- ** the Free Software Foundation, either version 3 of the License, or
- ** (at your option) any later version.
- **
- ** This program is distributed in the hope that it will be useful,
- ** but WITHOUT ANY WARRANTY; without even the implied warranty of
- ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- ** GNU General Public License for more details.
- **
- ** You should have received a copy of the GNU General Public License
- ** along with this program. If not, see <http://www.gnu.org/licenses/>.
- */
- /*****************************************************************************
- ** **
- ** Westwood Studios Pacific. **
- ** **
- ** Confidential Information **
- ** Copyright (C) 2000 - All Rights Reserved **
- ** **
- ******************************************************************************
- ** **
- ** Project: Dune Emperor **
- ** **
- ** Module: <module> (<prefix>_) **
- ** **
- ** Version: $ID$ **
- ** **
- ** File name: audprof.cpp **
- ** **
- ** Created by: mm/dd/yy <author> **
- ** **
- ** Description: <description> **
- ** **
- *****************************************************************************/
- /*****************************************************************************
- ** Includes **
- *****************************************************************************/
- #define WIN32_LEAN_AND_MEAN
- #include <windows.h>
- #include <stdio.h>
- #include <wpaudio/profiler.h>
- /*****************************************************************************
- ** Externals **
- *****************************************************************************/
- /*****************************************************************************
- ** Defines **
- *****************************************************************************/
- /*****************************************************************************
- ** Private Types **
- *****************************************************************************/
- /*****************************************************************************
- ** Private Data **
- *****************************************************************************/
- /*****************************************************************************
- ** Public Data **
- *****************************************************************************/
- /*****************************************************************************
- ** Private Prototypes **
- *****************************************************************************/
- /*****************************************************************************
- ** Private Functions **
- *****************************************************************************/
- /*****************************************************************************
- ** Public Functions **
- *****************************************************************************/
- #ifndef IG_FINAL_RELEASE
- /******************************************************************/
- /* */
- /* */
- /******************************************************************/
- void ProfCacheInit ( ProfileData *prof, int pages, int pageSize )
- {
- memset ( prof, 0, sizeof ( ProfileData ));
- if ( !QueryPerformanceFrequency( (LARGE_INTEGER*) &prof->freq ))
- {
- prof->freq = 0;
- }
- prof->update = prof->freq;
- prof->cacheSize = pages*pageSize;
- prof->numPages = pages;
- prof->pageSize = pageSize;
- }
- /******************************************************************/
- /* */
- /* */
- /******************************************************************/
- void ProfCacheDeinit ( ProfileData * )
- {
- }
- /******************************************************************/
- /* */
- /* */
- /******************************************************************/
- void ProfCacheNewFrame ( ProfileData *prof )
- {
- int total;
- prof->frames++;
- if ( prof->totalTime > prof->longestFrame )
- {
- prof->nextLongestFrame = prof->longestFrame;
- prof->nextLongestFramePages = prof->longestFramePages;
- prof->nextLongestFrameBytes = prof->longestFrameBytes;
- prof->longestFrame = prof->totalTime ;
- prof->longestFrameBytes = prof->totalDataBytes;
- prof->longestFramePages = prof->pageCount;
- }
- else if ( prof->totalTime > prof->nextLongestFrame )
- {
- prof->nextLongestFrame = prof->totalTime ;
- prof->nextLongestFrameBytes = prof->totalDataBytes;
- prof->nextLongestFramePages = prof->pageCount;
- }
- prof->pageCount = 0;
- if ( (total = prof->hits + prof->misses ))
- {
- prof->HitPercent = (prof->hits *100) / total;
- }
- prof->BytesPerFrame = prof->totalFrameBytes / prof->frames;
- if ( prof->frames > 3*30 )
- {
- prof->totalFrameBytes = 0;
- prof->frames = 0;
- }
- if ( ! (prof->frames % 30) )
- {
- prof->hits = 0;
- prof->misses = 0;
- }
- if ( prof->totalTime > prof->update )
- {
- int ms;
- ms = (int) ( (prof->totalTime *1000 )/prof->freq);
- prof->TotalBytesPerSecond = (int) (((unsigned __int64) prof->totalDataBytes *1000)/ ms);
- prof->totalDataBytes = 0;
- prof->totalTime = 0;
- ms = (int) ((prof->totalDecompTime *1000 )/prof->freq) ;
- if ( ms )
- {
- prof->DecompBytesPerSecond = (int) (((unsigned __int64) prof->totalDecompBytes *1000)/ ms);
- }
- else
- {
- prof->DecompBytesPerSecond = 0;
- }
-
- prof->totalDecompBytes = 0;
- prof->totalDecompTime = 0;
- ms = (int) ((prof->totalLoadTime *1000 )/prof->freq );
- if ( ms )
- {
- prof->LoadBytesPerSecond = (int) (((unsigned __int64)prof->totalLoadBytes *1000)/ ms);
- }
- else
- {
- prof->LoadBytesPerSecond = 0;
- }
- prof->totalLoadBytes = 0;
- prof->totalLoadTime = 0;
- }
- }
- /******************************************************************/
- /* */
- /* */
- /******************************************************************/
- void ProfCacheLoadStart ( ProfileData *prof, int bytes )
- {
- QueryPerformanceCounter( (LARGE_INTEGER*)&prof->start );
- prof->dbytes = bytes;
- prof->start_decomp = 0;
- prof->pageCount++;
- }
- /******************************************************************/
- /* */
- /* */
- /******************************************************************/
- void ProfCacheAddLoadBytes ( ProfileData *prof, int bytes )
- {
- prof->dbytes += bytes;
- }
- /******************************************************************/
- /* */
- /* */
- /******************************************************************/
- void ProfCacheDecompress ( ProfileData *prof, int bytes )
- {
- QueryPerformanceCounter( (LARGE_INTEGER*)&prof->start_decomp );
- prof->lbytes = bytes;
- }
- /******************************************************************/
- /* */
- /* */
- /******************************************************************/
- void ProfCacheLoadEnd ( ProfileData *prof )
- {
- QueryPerformanceCounter( (LARGE_INTEGER*)&prof->end );
- prof->totalTime += prof->end - prof->start;
- prof->totalDataBytes += prof->dbytes;
- prof->totalFrameBytes += prof->dbytes;
- if ( prof->start_decomp )
- {
- prof->totalLoadTime += prof->start_decomp - prof->start;
- prof->totalDecompTime += prof->end - prof->start_decomp;
- prof->totalLoadBytes += prof->lbytes;
- prof->totalDecompBytes += prof->dbytes;
- }
- else
- {
- prof->totalLoadTime += prof->end - prof->start;
- prof->totalLoadBytes += prof->dbytes;
- }
- }
- /******************************************************************/
- /* */
- /* */
- /******************************************************************/
- void ProfCacheMiss ( ProfileData *prof )
- {
- prof->misses++;
- }
- /******************************************************************/
- /* */
- /* */
- /******************************************************************/
- void ProfCacheHit ( ProfileData *prof )
- {
- prof->hits++;
- }
- /******************************************************************/
- /* */
- /* */
- /******************************************************************/
- void ProfCacheAddPage ( ProfileData *prof )
- {
- prof->pagesUsed++;
- }
- /******************************************************************/
- /* */
- /* */
- /******************************************************************/
- void ProfCacheRemovePage ( ProfileData *prof )
- {
- prof->pagesUsed--;
- }
- /******************************************************************/
- /* */
- /* */
- /******************************************************************/
- void ProfCacheFill ( ProfileData *prof, int bytes )
- {
- prof->cacheUsed += bytes;
- }
- /******************************************************************/
- /* */
- /* */
- /******************************************************************/
- void ProfCacheRemove ( ProfileData *prof, int bytes )
- {
- prof->cacheUsed -= bytes;
- }
- /******************************************************************/
- /* */
- /* */
- /******************************************************************/
- void ProfCacheText ( ProfileData *prof, void ( *print) ( char *text ) )
- {
- char buf[1024];
- int used;
- int filled;
- print ("Audio Cache Stats\n");
- sprintf( buf, "Hits: %d%%\n", prof->HitPercent );
- print ( buf );
- if ( prof->numPages )
- {
- used = (prof->pagesUsed *100)/prof->numPages;
- }
- else
- {
- used = 0;
- }
- if ( prof->pagesUsed * prof->pageSize )
- {
- filled = (prof->cacheUsed *100)/ (prof->pagesUsed * prof->pageSize );
- }
- else
- {
- filled = 0;
- }
- sprintf( buf, "Used: %d%% (%d%%)\n", used, filled );
- print ( buf );
- sprintf( buf, "KbPS: %d.%02d (%d.%02d,%d.%02d)\n",
- prof->TotalBytesPerSecond/1024, ((prof->TotalBytesPerSecond%1024)*100)/1024,
- prof->LoadBytesPerSecond/1024, ((prof->LoadBytesPerSecond%1024)*100)/1024,
- prof->DecompBytesPerSecond/1024, ((prof->DecompBytesPerSecond%1024)*100)/1024);
- print ( buf );
- sprintf( buf, "KPF: %d.%02d\n",
- prof->BytesPerFrame/1024, ((prof->BytesPerFrame%1024)*100)/1024 );
- print ( buf );
- sprintf( buf, " LF: %d.%02ds; %d pages; %d Kb\n",
- (int) (prof->longestFrame/prof->freq), (int)(((prof->longestFrame % prof->freq)*100)/prof->freq),
- prof->longestFramePages, prof->longestFrameBytes/1024 );
- print ( buf );
- sprintf( buf, "NLF: %d.%02ds; %d pages; %d Kb\n",
- (int) (prof->nextLongestFrame/prof->freq), (int) (((prof->nextLongestFrame % prof->freq)*100)/prof->freq),
- prof->nextLongestFramePages, prof->nextLongestFrameBytes/1024 );
- print ( buf );
- }
- /******************************************************************/
- /* */
- /* */
- /******************************************************************/
- void ProfCacheUpdateInterval ( ProfileData *prof, int mseconds )
- {
- prof->update = (prof->freq * mseconds )/ 1000;
- }
- /******************************************************************/
- /* */
- /* */
- /******************************************************************/
- static ProfStamp calc_ticks ( ProfStamp start, ProfStamp end )
- {
- if ( start < end )
- {
- return end - start;
- }
- return ((ProfStamp)0xffffffffffffffff) - start + end ;
- }
- /******************************************************************/
- /* */
- /* */
- /******************************************************************/
- static void calc_stats ( ProfileCPU &prof )
- {
- if ( prof.lastCPU )
- {
- prof.cpuUsage = (int) ((prof.lastTicks*((ProfStamp)1000))/prof.lastCPU);
- }
- else
- {
- prof.cpuUsage = 0;
- }
- }
- /******************************************************************/
- /* */
- /* */
- /******************************************************************/
- void ProfileCPUInit ( ProfileCPU &prof )
- {
- memset ( &prof, 0, sizeof ( ProfileCPU ));
- if ( !QueryPerformanceFrequency( (LARGE_INTEGER*) &prof.freq ))
- {
- prof.freq = 0;
- }
- prof.updateInterval = SECONDS(1);
- if ( prof.freq )
- {
- prof.overflowInterval = SECONDS ( (((ProfStamp) 0xffffffffffffffff) / prof.freq) );
- if ( prof.overflowInterval < prof.updateInterval )
- {
- prof.updateInterval = prof.overflowInterval;
- }
- }
- }
- /******************************************************************/
- /* */
- /* */
- /******************************************************************/
- void ProfileCPUDeinit ( ProfileCPU &prof )
- {
- prof;
- }
- /******************************************************************/
- /* */
- /* */
- /******************************************************************/
- void ProfileCPUStart ( ProfileCPU &prof )
- {
-
- if ( prof.state == PROF_STATE_IDLE )
- {
- QueryPerformanceCounter( (LARGE_INTEGER*)&prof.start );
-
- if ( prof.lastStart )
- {
- prof.totalCPUTicks += calc_ticks ( prof.lastStart, prof.start );
- }
-
- prof.lastStart = prof.start;
- prof.state = PROF_STATE_PROFILING;
- }
- }
- /******************************************************************/
- /* */
- /* */
- /******************************************************************/
- void ProfileCPUPause ( ProfileCPU &prof )
- {
- if ( prof.state == PROF_STATE_PROFILING )
- {
- ProfStamp end;
- QueryPerformanceCounter( (LARGE_INTEGER*) &end );
- prof.totalTicks += calc_ticks ( prof.start, end );
- prof.state = PROF_STATE_PAUSED;
- }
- }
- /******************************************************************/
- /* */
- /* */
- /******************************************************************/
- void ProfileCPUResume ( ProfileCPU &prof )
- {
- if ( prof.state == PROF_STATE_PAUSED )
- {
- QueryPerformanceCounter( (LARGE_INTEGER*) &prof.start );
- prof.state = PROF_STATE_PROFILING;
- }
- }
- /******************************************************************/
- /* */
- /* */
- /******************************************************************/
- void ProfileCPUEnd ( ProfileCPU &prof )
- {
- ProfStamp end;
- QueryPerformanceCounter( (LARGE_INTEGER*) &end );
- if ( prof.state != PROF_STATE_IDLE )
- {
- if ( prof.start && prof.totalCPUTicks )
- {
- prof.totalTicks += calc_ticks ( prof.start, end );
- }
- prof.state = PROF_STATE_IDLE;
- }
- TimeStamp now = AudioGetTime();
- TimeStamp lastUpdate = now - prof.lastUpdate;
- if ( lastUpdate > prof.updateInterval )
- {
- prof.lastUpdate = now;
- if ( lastUpdate < prof.overflowInterval )
- {
- // we can use the data
- prof.lastTicks = prof.totalTicks;
- prof.lastCPU = prof.totalCPUTicks;
- calc_stats ( prof );
- }
- prof.totalTicks = 0;
- prof.totalCPUTicks = 0;
-
- }
- }
- /******************************************************************/
- /* */
- /* */
- /******************************************************************/
- int ProfileCPUUsage ( ProfileCPU &prof )
- {
- return prof.cpuUsage;
- }
- /******************************************************************/
- /* */
- /* */
- /******************************************************************/
- int ProfileCPUTicks ( ProfileCPU &prof )
- {
- prof;
- return 0;
- }
- /******************************************************************/
- /* */
- /* */
- /******************************************************************/
- void ProfileCPUSetName ( ProfileCPU &prof, const char *name )
- {
- strncpy ( prof.name, name, MAX_PROF_NAME );
- }
- /******************************************************************/
- /* */
- /* */
- /******************************************************************/
- void ProfileCPUPrint ( ProfileCPU &prof, void ( *print) ( char *text ) )
- {
- char buffer[200];
- if ( prof.freq )
- {
- sprintf ( buffer, "%s : CPU %3d.%1d / %I64d", prof.name, prof.cpuUsage/10, prof.cpuUsage%10, prof.lastTicks );
- }
- else
- {
- sprintf ( buffer, "%s : CPU (no timer)" );
- }
- print ( buffer );
- }
- /******************************************************************/
- /* */
- /* */
- /******************************************************************/
- #endif
|