wwprofile.cpp 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138
  1. /*
  2. ** Command & Conquer Generals Zero Hour(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  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. /***********************************************************************************************
  19. *** C O N F I D E N T I A L --- W E S T W O O D S T U D I O S ***
  20. ***********************************************************************************************
  21. * *
  22. * Project Name : WWDebug *
  23. * *
  24. * $Archive:: /Commando/Code/wwdebug/wwprofile.cpp $*
  25. * *
  26. * $Author:: Jani_p $*
  27. * *
  28. * $Modtime:: 4/01/02 10:30a $*
  29. * *
  30. * $Revision:: 20 $*
  31. * *
  32. *---------------------------------------------------------------------------------------------*
  33. * WWProfile_Get_Ticks -- Retrieves the cpu performance counter *
  34. * WWProfileHierachyNodeClass::WWProfileHierachyNodeClass -- Constructor *
  35. * WWProfileHierachyNodeClass::~WWProfileHierachyNodeClass -- Destructor *
  36. * WWProfileHierachyNodeClass::Get_Sub_Node -- Searches for a child node by name (pointer) *
  37. * WWProfileHierachyNodeClass::Reset -- Reset all profiling data in the tree *
  38. * WWProfileHierachyNodeClass::Call -- Start timing *
  39. * WWProfileHierachyNodeClass::Return -- Stop timing, record results *
  40. * WWProfileManager::Start_Profile -- Begin a named profile *
  41. * WWProfileManager::Stop_Profile -- Stop timing and record the results. *
  42. * WWProfileManager::Reset -- Reset the contents of the profiling system *
  43. * WWProfileManager::Increment_Frame_Counter -- Increment the frame counter *
  44. * WWProfileManager::Get_Time_Since_Reset -- returns the elapsed time since last reset *
  45. * WWProfileManager::Get_Iterator -- Creates an iterator for the profile tree *
  46. * WWProfileManager::Release_Iterator -- Return an iterator for the profile tree *
  47. * WWProfileManager::Get_In_Order_Iterator -- Creates an "in-order" iterator for the profile *
  48. * WWProfileManager::Release_In_Order_Iterator -- Return an "in-order" iterator *
  49. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  50. #include "always.h"
  51. #include "wwprofile.h"
  52. #include "fastallocator.h"
  53. #include "wwdebug.h"
  54. #include <windows.h>
  55. //#include "systimer.h"
  56. #include "systimer.h"
  57. #include "rawfile.h"
  58. #include "ffactory.h"
  59. #include "simplevec.h"
  60. #include "cpudetect.h"
  61. #include "hashtemplate.h"
  62. #include <stdio.h>
  63. static SimpleDynVecClass<WWProfileHierachyNodeClass*> ProfileCollectVector;
  64. static double TotalFrameTimes;
  65. static bool ProfileCollecting;
  66. static HashTemplateClass<StringClass, unsigned> ProfileStringHash;
  67. static unsigned ProfileStringCount;
  68. unsigned WWProfile_Get_System_Time()
  69. {
  70. return TIMEGETTIME();
  71. }
  72. WWINLINE double WWProfile_Get_Inv_Processor_Ticks_Per_Second(void)
  73. {
  74. #ifdef WIN32
  75. return CPUDetectClass::Get_Inv_Processor_Ticks_Per_Second();
  76. #elif defined (_UNIX)
  77. return 0.001;
  78. #endif
  79. }
  80. /***********************************************************************************************
  81. * WWProfile_Get_Ticks -- Retrieves the cpu performance counter *
  82. * *
  83. * INPUT: *
  84. * *
  85. * OUTPUT: *
  86. * *
  87. * WARNINGS: *
  88. * *
  89. * HISTORY: *
  90. * 9/24/2000 gth : Created. *
  91. *=============================================================================================*/
  92. inline void WWProfile_Get_Ticks(_int64 * ticks)
  93. {
  94. #ifdef _UNIX
  95. *ticks = TIMEGETTIME();
  96. #else
  97. __asm
  98. {
  99. push edx;
  100. push ecx;
  101. push eax;
  102. mov ecx,ticks;
  103. _emit 0Fh
  104. _emit 31h
  105. mov [ecx],eax;
  106. mov [ecx+4],edx;
  107. pop eax;
  108. pop ecx;
  109. pop edx;
  110. }
  111. #endif
  112. }
  113. /***********************************************************************************************
  114. * WWProfileHierachyNodeClass::WWProfileHierachyNodeClass -- Constructor *
  115. * *
  116. * *
  117. * INPUT: *
  118. * name - pointer to a static string which is the name of this profile node *
  119. * parent - parent pointer *
  120. * *
  121. * OUTPUT: *
  122. * *
  123. * WARNINGS: *
  124. * The name is assumed to be a static pointer, only the pointer is stored and compared for *
  125. * efficiency reasons. *
  126. * *
  127. * HISTORY: *
  128. * 9/24/2000 gth : Created. *
  129. *=============================================================================================*/
  130. WWProfileHierachyNodeClass::WWProfileHierachyNodeClass( const char * name, WWProfileHierachyNodeClass * parent ) :
  131. Name( name ),
  132. TotalCalls( 0 ),
  133. TotalTime( 0 ),
  134. StartTime( 0 ),
  135. RecursionCounter( 0 ),
  136. Parent( parent ),
  137. Child( NULL ),
  138. Sibling( NULL )
  139. {
  140. Reset();
  141. if (!ProfileStringHash.Get(name, ProfileStringID)) {
  142. ProfileStringID=ProfileStringCount++;
  143. ProfileStringHash.Insert(name,ProfileStringID);
  144. }
  145. }
  146. WWProfileHierachyNodeClass::WWProfileHierachyNodeClass( unsigned id, WWProfileHierachyNodeClass * parent ) :
  147. Name( NULL ),
  148. TotalCalls( 0 ),
  149. TotalTime( 0 ),
  150. StartTime( 0 ),
  151. RecursionCounter( 0 ),
  152. Parent( parent ),
  153. Child( NULL ),
  154. Sibling( NULL ),
  155. ProfileStringID(id)
  156. {
  157. Reset();
  158. }
  159. /***********************************************************************************************
  160. * WWProfileHierachyNodeClass::~WWProfileHierachyNodeClass -- Destructor *
  161. * *
  162. * INPUT: *
  163. * *
  164. * OUTPUT: *
  165. * *
  166. * WARNINGS: *
  167. * *
  168. * HISTORY: *
  169. * 9/24/2000 gth : Created. *
  170. *=============================================================================================*/
  171. WWProfileHierachyNodeClass::~WWProfileHierachyNodeClass( void )
  172. {
  173. delete Child;
  174. delete Sibling;
  175. }
  176. WWProfileHierachyNodeClass* WWProfileHierachyNodeClass::Clone_Hierarchy(WWProfileHierachyNodeClass* parent)
  177. {
  178. WWProfileHierachyNodeClass* node=new WWProfileHierachyNodeClass(Name,parent);
  179. node->TotalCalls=TotalCalls;
  180. node->TotalTime=TotalTime;
  181. node->StartTime=StartTime;
  182. node->RecursionCounter=RecursionCounter;
  183. if (Child) {
  184. node->Child=Child->Clone_Hierarchy(this);
  185. }
  186. if (Sibling) {
  187. node->Sibling=Sibling->Clone_Hierarchy(parent);
  188. }
  189. return node;
  190. }
  191. void WWProfileHierachyNodeClass::Write_To_File(FileClass* file,int recursion)
  192. {
  193. if (TotalTime!=0.0f) {
  194. int i;
  195. StringClass string;
  196. StringClass work;
  197. for (i=0;i<recursion;++i) { string+="\t"; }
  198. work.Format("%s\t%d\t%f\r\n",Name,TotalCalls,TotalTime*1000.0f);
  199. string+=work;
  200. file->Write(string.Peek_Buffer(),string.Get_Length());
  201. }
  202. if (Child) {
  203. Child->Write_To_File(file,recursion+1);
  204. }
  205. if (Sibling) {
  206. Sibling->Write_To_File(file,recursion);
  207. }
  208. }
  209. void WWProfileHierachyNodeClass::Add_To_String_Compact(StringClass& string,int recursion)
  210. {
  211. if (TotalTime!=0.0f) {
  212. StringClass work;
  213. work.Format("%d %d %2.2f;",ProfileStringID,TotalCalls,TotalTime*1000.0f);
  214. string+=work;
  215. }
  216. if (Child) {
  217. StringClass work;
  218. Child->Add_To_String_Compact(work,recursion+1);
  219. if (work.Get_Length()!=0) {
  220. string+="{";
  221. string+=work;
  222. string+="}";
  223. }
  224. }
  225. if (Sibling) {
  226. Sibling->Add_To_String_Compact(string,recursion);
  227. }
  228. }
  229. /***********************************************************************************************
  230. * WWProfileHierachyNodeClass::Get_Sub_Node -- Searches for a child node by name (pointer) *
  231. * *
  232. * INPUT: *
  233. * name - static string pointer to the name of the node we are searching for *
  234. * *
  235. * OUTPUT: *
  236. * *
  237. * WARNINGS: *
  238. * All profile names are assumed to be static strings so this function uses pointer compares *
  239. * to find the named node. *
  240. * *
  241. * HISTORY: *
  242. * 9/24/2000 gth : Created. *
  243. *=============================================================================================*/
  244. WWProfileHierachyNodeClass * WWProfileHierachyNodeClass::Get_Sub_Node( const char * name )
  245. {
  246. // Try to find this sub node
  247. WWProfileHierachyNodeClass * child = Child;
  248. while ( child ) {
  249. if ( child->Name == name ) {
  250. return child;
  251. }
  252. child = child->Sibling;
  253. }
  254. // We didn't find it, so add it
  255. WWProfileHierachyNodeClass * node = W3DNEW WWProfileHierachyNodeClass( name, this );
  256. node->Sibling = Child;
  257. Child = node;
  258. return node;
  259. }
  260. /***********************************************************************************************
  261. * WWProfileHierachyNodeClass::Reset -- Reset all profiling data in the tree *
  262. * *
  263. * INPUT: *
  264. * *
  265. * OUTPUT: *
  266. * *
  267. * WARNINGS: *
  268. * *
  269. * HISTORY: *
  270. * 9/24/2000 gth : Created. *
  271. *=============================================================================================*/
  272. void WWProfileHierachyNodeClass::Reset( void )
  273. {
  274. TotalCalls = 0;
  275. TotalTime = 0.0f;
  276. if ( Child ) {
  277. Child->Reset();
  278. }
  279. if ( Sibling ) {
  280. Sibling->Reset();
  281. }
  282. }
  283. /***********************************************************************************************
  284. * WWProfileHierachyNodeClass::Call -- Start timing *
  285. * *
  286. * INPUT: *
  287. * *
  288. * OUTPUT: *
  289. * *
  290. * WARNINGS: *
  291. * *
  292. * HISTORY: *
  293. * 9/24/2000 gth : Created. *
  294. *=============================================================================================*/
  295. void WWProfileHierachyNodeClass::Call( void )
  296. {
  297. TotalCalls++;
  298. if (RecursionCounter++ == 0) {
  299. WWProfile_Get_Ticks(&StartTime);
  300. }
  301. }
  302. /***********************************************************************************************
  303. * WWProfileHierachyNodeClass::Return -- Stop timing, record results *
  304. * *
  305. * INPUT: *
  306. * *
  307. * OUTPUT: *
  308. * *
  309. * WARNINGS: *
  310. * *
  311. * HISTORY: *
  312. * 9/24/2000 gth : Created. *
  313. *=============================================================================================*/
  314. bool WWProfileHierachyNodeClass::Return( void )
  315. {
  316. if (--RecursionCounter == 0) {
  317. if ( TotalCalls != 0 ) {
  318. __int64 time;
  319. WWProfile_Get_Ticks(&time);
  320. time-=StartTime;
  321. TotalTime += float(double(time)*WWProfile_Get_Inv_Processor_Ticks_Per_Second());
  322. }
  323. }
  324. return RecursionCounter == 0;
  325. }
  326. /***************************************************************************************************
  327. **
  328. ** WWProfileManager Implementation
  329. **
  330. ***************************************************************************************************/
  331. bool WWProfileManager::IsProfileEnabled=false;
  332. WWProfileHierachyNodeClass WWProfileManager::Root( "Root", NULL );
  333. WWProfileHierachyNodeClass * WWProfileManager::CurrentNode = &WWProfileManager::Root;
  334. WWProfileHierachyNodeClass * WWProfileManager::CurrentRootNode = &WWProfileManager::Root;
  335. int WWProfileManager::FrameCounter = 0;
  336. __int64 WWProfileManager::ResetTime = 0;
  337. static unsigned int ThreadID = static_cast<unsigned int>(-1);
  338. /***********************************************************************************************
  339. * WWProfileManager::Start_Profile -- Begin a named profile *
  340. * *
  341. * Steps one level deeper into the tree, if a child already exists with the specified name *
  342. * then it accumulates the profiling; otherwise a new child node is added to the profile tree. *
  343. * *
  344. * INPUT: *
  345. * name - name of this profiling record *
  346. * *
  347. * OUTPUT: *
  348. * *
  349. * WARNINGS: *
  350. * The string used is assumed to be a static string; pointer compares are used throughout *
  351. * the profiling code for efficiency. *
  352. * *
  353. * HISTORY: *
  354. * 9/24/2000 gth : Created. *
  355. *=============================================================================================*/
  356. void WWProfileManager::Start_Profile( const char * name )
  357. {
  358. if (::GetCurrentThreadId() != ThreadID) {
  359. return;
  360. }
  361. // int current_thread = ::GetCurrentThreadId();
  362. if (name != CurrentNode->Get_Name()) {
  363. CurrentNode = CurrentNode->Get_Sub_Node( name );
  364. }
  365. CurrentNode->Call();
  366. }
  367. void WWProfileManager::Start_Root_Profile( const char * name )
  368. {
  369. if (::GetCurrentThreadId() != ThreadID) {
  370. return;
  371. }
  372. if (name != CurrentRootNode->Get_Name()) {
  373. CurrentRootNode = CurrentRootNode->Get_Sub_Node( name );
  374. }
  375. CurrentRootNode->Call();
  376. }
  377. /***********************************************************************************************
  378. * WWProfileManager::Stop_Profile -- Stop timing and record the results. *
  379. * *
  380. * INPUT: *
  381. * *
  382. * OUTPUT: *
  383. * *
  384. * WARNINGS: *
  385. * *
  386. * HISTORY: *
  387. * 9/24/2000 gth : Created. *
  388. *=============================================================================================*/
  389. void WWProfileManager::Stop_Profile( void )
  390. {
  391. if (::GetCurrentThreadId() != ThreadID) {
  392. return;
  393. }
  394. // Return will indicate whether we should back up to our parent (we may
  395. // be profiling a recursive function)
  396. if (CurrentNode->Return()) {
  397. CurrentNode = CurrentNode->Get_Parent();
  398. }
  399. }
  400. void WWProfileManager::Stop_Root_Profile( void )
  401. {
  402. if (::GetCurrentThreadId() != ThreadID) {
  403. return;
  404. }
  405. // Return will indicate whether we should back up to our parent (we may
  406. // be profiling a recursive function)
  407. if (CurrentRootNode->Return()) {
  408. CurrentRootNode = CurrentRootNode->Get_Parent();
  409. }
  410. }
  411. /***********************************************************************************************
  412. * WWProfileManager::Reset -- Reset the contents of the profiling system *
  413. * *
  414. * This resets everything except for the tree structure. All of the timing data is reset. *
  415. * *
  416. * *
  417. * INPUT: *
  418. * *
  419. * OUTPUT: *
  420. * *
  421. * WARNINGS: *
  422. * *
  423. * HISTORY: *
  424. * 9/24/2000 gth : Created. *
  425. *=============================================================================================*/
  426. void WWProfileManager::Reset( void )
  427. {
  428. ThreadID = ::GetCurrentThreadId();
  429. Root.Reset();
  430. FrameCounter = 0;
  431. WWProfile_Get_Ticks(&ResetTime);
  432. }
  433. /***********************************************************************************************
  434. * WWProfileManager::Increment_Frame_Counter -- Increment the frame counter *
  435. * *
  436. * INPUT: *
  437. * *
  438. * OUTPUT: *
  439. * *
  440. * WARNINGS: *
  441. * *
  442. * HISTORY: *
  443. * 9/24/2000 gth : Created. *
  444. *=============================================================================================*/
  445. void WWProfileManager::Increment_Frame_Counter( void )
  446. {
  447. if (ProfileCollecting) {
  448. float time=Get_Time_Since_Reset();
  449. TotalFrameTimes+=time;
  450. WWProfileHierachyNodeClass* new_root=Root.Clone_Hierarchy(NULL);
  451. new_root->Set_Total_Time(time);
  452. new_root->Set_Total_Calls(1);
  453. ProfileCollectVector.Add(new_root);
  454. Reset();
  455. }
  456. FrameCounter++;
  457. }
  458. /***********************************************************************************************
  459. * WWProfileManager::Get_Time_Since_Reset -- returns the elapsed time since last reset *
  460. * *
  461. * INPUT: *
  462. * *
  463. * OUTPUT: *
  464. * *
  465. * WARNINGS: *
  466. * *
  467. * HISTORY: *
  468. * 9/24/2000 gth : Created. *
  469. *=============================================================================================*/
  470. float WWProfileManager::Get_Time_Since_Reset( void )
  471. {
  472. __int64 time;
  473. WWProfile_Get_Ticks(&time);
  474. time -= ResetTime;
  475. return float(double(time) * WWProfile_Get_Inv_Processor_Ticks_Per_Second());
  476. }
  477. /***********************************************************************************************
  478. * WWProfileManager::Get_Iterator -- Creates an iterator for the profile tree *
  479. * *
  480. * INPUT: *
  481. * *
  482. * OUTPUT: *
  483. * *
  484. * WARNINGS: *
  485. * *
  486. * HISTORY: *
  487. * 9/24/2000 gth : Created. *
  488. *=============================================================================================*/
  489. WWProfileIterator * WWProfileManager::Get_Iterator( void )
  490. {
  491. return W3DNEW WWProfileIterator( &Root );
  492. }
  493. /***********************************************************************************************
  494. * WWProfileManager::Release_Iterator -- Return an iterator for the profile tree *
  495. * *
  496. * INPUT: *
  497. * *
  498. * OUTPUT: *
  499. * *
  500. * WARNINGS: *
  501. * *
  502. * HISTORY: *
  503. * 9/24/2000 gth : Created. *
  504. *=============================================================================================*/
  505. void WWProfileManager::Release_Iterator( WWProfileIterator * iterator )
  506. {
  507. delete iterator;
  508. }
  509. void WWProfileManager::Begin_Collecting()
  510. {
  511. Reset();
  512. ProfileCollecting=true;
  513. TotalFrameTimes=0.0;
  514. }
  515. void WWProfileManager::End_Collecting(const char* filename)
  516. {
  517. int i;
  518. if (filename && ProfileCollectVector.Count()!=0) {
  519. FileClass * file= _TheWritingFileFactory->Get_File(filename);
  520. if (file != NULL) {
  521. //
  522. // Open or create the file
  523. //
  524. file->Open (FileClass::WRITE);
  525. StringClass str;
  526. float avg_frame_time=TotalFrameTimes/float(ProfileCollectVector.Count());
  527. str.Format(
  528. "Total frames: %d, average frame time: %fms\r\n"
  529. "All frames taking more than twice the average frame time are marked with keyword SPIKE.\r\n\r\n",
  530. ProfileCollectVector.Count(),avg_frame_time*1000.0f);
  531. file->Write(str.Peek_Buffer(),str.Get_Length());
  532. HashTemplateIterator<StringClass,unsigned> ite(ProfileStringHash);
  533. for (ite.First();!ite.Is_Done();ite.Next()) {
  534. StringClass name=ite.Peek_Key();
  535. // Remove spaces, tabs and commas from the name as it would confuse the profile browser
  536. int i;
  537. for (i=0;i<name.Get_Length();++i) {
  538. if (name[i]=='\t') name[i]='-';
  539. if (name[i]==' ') name[i]='_';
  540. if (name[i]==',') name[i]='.';
  541. if (name[i]==';') name[i]=':';
  542. }
  543. str.Format("ID: %d %s\r\n",ite.Peek_Value(),name);
  544. file->Write(str.Peek_Buffer(),str.Get_Length());
  545. }
  546. str.Format("\r\n\r\n");
  547. file->Write(str.Peek_Buffer(),str.Get_Length());
  548. for (i=0;i<ProfileCollectVector.Count();++i) {
  549. float frame_time=ProfileCollectVector[i]->Get_Total_Time();
  550. str.Format("FRAME: %d %2.2f %s ",i,frame_time*1000.0f,frame_time>avg_frame_time*2.0f ? "SPIKE" : "OK");
  551. ProfileCollectVector[i]->Add_To_String_Compact(str,0);
  552. str+="\r\n";
  553. file->Write(str.Peek_Buffer(),str.Get_Length());
  554. }
  555. //
  556. // Close the file
  557. //
  558. file->Close ();
  559. _TheWritingFileFactory->Return_File (file);
  560. }
  561. }
  562. for (i=0;i<ProfileCollectVector.Count();++i) {
  563. delete ProfileCollectVector[i];
  564. ProfileCollectVector[i]=0;
  565. }
  566. ProfileCollectVector.Delete_All();
  567. ProfileCollecting=false;
  568. }
  569. static unsigned Read_Line(char* memory,unsigned pos,unsigned maxpos)
  570. {
  571. while (pos<maxpos) {
  572. if (memory[pos++]=='\n') {
  573. return pos;
  574. }
  575. }
  576. return pos;
  577. }
  578. static unsigned Seek_Char(char* memory,unsigned pos,unsigned maxpos,char c)
  579. {
  580. while (pos<maxpos) {
  581. if (memory[pos++]==c) {
  582. return pos;
  583. }
  584. }
  585. return pos;
  586. }
  587. unsigned Read_Int(char* memory,unsigned pos,unsigned maxpos,unsigned& number)
  588. {
  589. number=0;
  590. while (pos<maxpos) {
  591. char c=memory[pos++];
  592. if (isdigit(c)) {
  593. number*=10;
  594. number+=c-'0';
  595. }
  596. else break;
  597. }
  598. return pos;
  599. }
  600. unsigned Read_String(char* memory,unsigned pos,unsigned maxpos,char* string)
  601. {
  602. int ccount=0;
  603. while (pos<maxpos) {
  604. char c=memory[pos++];
  605. if (c>' ') {
  606. string[ccount++]=c;
  607. }
  608. else break;
  609. }
  610. string[ccount]='\0';
  611. return pos;
  612. }
  613. unsigned Read_Float(char* memory,unsigned pos,unsigned maxpos,float& fnumber)
  614. {
  615. unsigned ftint=0;
  616. float ftfloat=0.0f;
  617. while (pos<maxpos) {
  618. char c=memory[pos++];
  619. if (isdigit(c)) {
  620. ftint*=10;
  621. ftint+=c-'0';
  622. }
  623. else {
  624. if (c=='.') {
  625. float divider=1.0f;
  626. while (pos<maxpos) {
  627. char c=memory[pos++];
  628. if (isdigit(c)) {
  629. ftfloat*=10.0f;
  630. ftfloat+=float(c-'0');
  631. divider*=10.0f;
  632. }
  633. else break;
  634. }
  635. ftfloat/=divider;
  636. }
  637. break;
  638. }
  639. }
  640. ftfloat+=float(ftint);
  641. fnumber=ftfloat;
  642. return pos;
  643. }
  644. static unsigned Read_ID(char* memory,unsigned pos,unsigned maxpos,StringClass& string,unsigned& id)
  645. {
  646. char idstring[256];
  647. unsigned idnumber=0;
  648. pos+=4; // "ID: "
  649. // Read idnumber
  650. pos=Read_Int(memory,pos,maxpos,idnumber);
  651. // Read name
  652. pos=Read_String(memory,pos,maxpos,idstring);
  653. string=idstring;
  654. id=idnumber;
  655. return Read_Line(memory,pos,maxpos);
  656. }
  657. static unsigned Read_Frame(char* memory,unsigned pos,unsigned maxpos,WWProfileHierachyInfoClass*& root,HashTemplateClass<unsigned, StringClass>& id_hash)
  658. {
  659. char statusstring[256];
  660. unsigned framenumber=0;
  661. float frametime;
  662. root=NULL;
  663. WWProfileHierachyInfoClass* parent=NULL;
  664. WWProfileHierachyInfoClass* latest=NULL;
  665. pos+=7; // "FRAME: "
  666. // Read frame number
  667. pos=Read_Int(memory,pos,maxpos,framenumber);
  668. // Read frametime
  669. pos=Read_Float(memory,pos,maxpos,frametime);
  670. // Read statusstring
  671. pos=Read_String(memory,pos,maxpos,statusstring);
  672. // Done with the header, read the fram data
  673. unsigned lineend=Read_Line(memory,pos,maxpos);
  674. while (pos<lineend) {
  675. // A Digit?
  676. if (isdigit(memory[pos])) {
  677. unsigned id;
  678. unsigned count;
  679. float time;
  680. // Read id
  681. pos=Read_Int(memory,pos,maxpos,id);
  682. // Read count
  683. pos=Read_Int(memory,pos,maxpos,count);
  684. // Read time
  685. pos=Read_Float(memory,pos,maxpos,time);
  686. StringClass name="Unknown";
  687. id_hash.Get(id,name);
  688. WWProfileHierachyInfoClass* new_node=new WWProfileHierachyInfoClass(name,parent);
  689. if (parent) {
  690. new_node->Set_Sibling(parent->Get_Child());
  691. parent->Set_Child(new_node);
  692. }
  693. new_node->Set_Total_Time(time);
  694. new_node->Set_Total_Calls(count);
  695. latest=new_node;
  696. if (root==NULL) root=new_node;
  697. }
  698. else if (memory[pos]=='{') {
  699. parent=latest;
  700. pos++;
  701. }
  702. else if (memory[pos]=='}') {
  703. if (parent) {
  704. parent=parent->Get_Parent();
  705. }
  706. pos++;
  707. }
  708. else {
  709. pos++;
  710. }
  711. }
  712. return Read_Line(memory,pos,maxpos);
  713. }
  714. void WWProfileManager::Load_Profile_Log(const char* filename, WWProfileHierachyInfoClass**& array, unsigned& count)
  715. {
  716. array=NULL;
  717. count=0;
  718. unsigned i;
  719. FileClass * file= _TheFileFactory->Get_File(filename);
  720. if (file != NULL && file->Is_Available()) {
  721. HashTemplateClass<StringClass, unsigned> string_hash;
  722. HashTemplateClass<unsigned, StringClass> id_hash;
  723. SimpleDynVecClass<WWProfileHierachyInfoClass*> vec;
  724. //
  725. // Open or create the file
  726. //
  727. file->Open (FileClass::READ);
  728. char tmp[256];
  729. unsigned size=file->Size();
  730. char* memory=new char[size];
  731. file->Read(memory,size);
  732. file->Close();
  733. unsigned pos=0;
  734. while (pos<size) {
  735. Read_String(memory,pos,size,tmp);
  736. if (tmp[0]=='I' && tmp[1]=='D' && tmp[2]==':') {
  737. StringClass string;
  738. unsigned id;
  739. pos=Read_ID(memory,pos,size,string,id);
  740. string_hash.Insert(string,id);
  741. id_hash.Insert(id,string);
  742. }
  743. else if (tmp[0]=='F' && tmp[1]=='R' && tmp[2]=='A' && tmp[3]=='M' && tmp[4]=='E' && tmp[5]==':') {
  744. WWProfileHierachyInfoClass* node=NULL;
  745. pos=Read_Frame(memory,pos,size,node,id_hash);
  746. if (node) {
  747. vec.Add(node);
  748. }
  749. }
  750. else {
  751. pos=Read_Line(memory,pos,size);
  752. }
  753. }
  754. delete[] memory;
  755. if (vec.Count()) {
  756. count=vec.Count();
  757. array=new WWProfileHierachyInfoClass*[count];
  758. for (i=0;i<count;++i) {
  759. array[i]=vec[i];
  760. }
  761. }
  762. }
  763. }
  764. /***********************************************************************************************
  765. * WWProfileManager::Get_In_Order_Iterator -- Creates an "in-order" iterator for the profile t *
  766. * *
  767. * INPUT: *
  768. * *
  769. * OUTPUT: *
  770. * *
  771. * WARNINGS: *
  772. * *
  773. * HISTORY: *
  774. * 9/24/2000 gth : Created. *
  775. *=============================================================================================*/
  776. WWProfileInOrderIterator * WWProfileManager::Get_In_Order_Iterator( void )
  777. {
  778. return W3DNEW WWProfileInOrderIterator;
  779. }
  780. /***********************************************************************************************
  781. * WWProfileManager::Release_In_Order_Iterator -- Return an "in-order" iterator *
  782. * *
  783. * INPUT: *
  784. * *
  785. * OUTPUT: *
  786. * *
  787. * WARNINGS: *
  788. * *
  789. * HISTORY: *
  790. * 9/24/2000 gth : Created. *
  791. *=============================================================================================*/
  792. void WWProfileManager::Release_In_Order_Iterator( WWProfileInOrderIterator * iterator )
  793. {
  794. delete iterator;
  795. }
  796. /***************************************************************************************************
  797. **
  798. ** WWProfileIterator Implementation
  799. **
  800. ***************************************************************************************************/
  801. WWProfileIterator::WWProfileIterator( WWProfileHierachyNodeClass * start )
  802. {
  803. CurrentParent = start;
  804. CurrentChild = CurrentParent->Get_Child();
  805. }
  806. void WWProfileIterator::First(void)
  807. {
  808. CurrentChild = CurrentParent->Get_Child();
  809. }
  810. void WWProfileIterator::Next(void)
  811. {
  812. CurrentChild = CurrentChild->Get_Sibling();
  813. }
  814. bool WWProfileIterator::Is_Done(void)
  815. {
  816. return CurrentChild == NULL;
  817. }
  818. void WWProfileIterator::Enter_Child( void )
  819. {
  820. CurrentParent = CurrentChild;
  821. CurrentChild = CurrentParent->Get_Child();
  822. }
  823. void WWProfileIterator::Enter_Child( int index )
  824. {
  825. CurrentChild = CurrentParent->Get_Child();
  826. while ( (CurrentChild != NULL) && (index != 0) ) {
  827. index--;
  828. CurrentChild = CurrentChild->Get_Sibling();
  829. }
  830. if ( CurrentChild != NULL ) {
  831. CurrentParent = CurrentChild;
  832. CurrentChild = CurrentParent->Get_Child();
  833. }
  834. }
  835. void WWProfileIterator::Enter_Parent( void )
  836. {
  837. if ( CurrentParent->Get_Parent() != NULL ) {
  838. CurrentParent = CurrentParent->Get_Parent();
  839. }
  840. CurrentChild = CurrentParent->Get_Child();
  841. }
  842. /***************************************************************************************************
  843. **
  844. ** WWProfileInOrderIterator Implementation
  845. **
  846. ***************************************************************************************************/
  847. WWProfileInOrderIterator::WWProfileInOrderIterator( void )
  848. {
  849. CurrentNode = &WWProfileManager::Root;
  850. }
  851. void WWProfileInOrderIterator::First(void)
  852. {
  853. CurrentNode = &WWProfileManager::Root;
  854. }
  855. void WWProfileInOrderIterator::Next(void)
  856. {
  857. if ( CurrentNode->Get_Child() ) { // If I have a child, go to child
  858. CurrentNode = CurrentNode->Get_Child();
  859. } else if ( CurrentNode->Get_Sibling() ) { // If I have a sibling, go to sibling
  860. CurrentNode = CurrentNode->Get_Sibling();
  861. } else { // if not, go to my parent's sibling, or his.......
  862. // Find a parent with a sibling....
  863. bool done = false;
  864. while ( CurrentNode != NULL && !done ) {
  865. // go to my parent
  866. CurrentNode = CurrentNode->Get_Parent();
  867. // If I have a sibling, go there
  868. if ( CurrentNode != NULL && CurrentNode->Get_Sibling() != NULL ) {
  869. CurrentNode = CurrentNode->Get_Sibling();
  870. done = true;
  871. }
  872. }
  873. }
  874. }
  875. bool WWProfileInOrderIterator::Is_Done(void)
  876. {
  877. return CurrentNode == NULL;
  878. }
  879. /*
  880. **
  881. */
  882. WWTimeItClass::WWTimeItClass( const char * name )
  883. {
  884. Name = name;
  885. WWProfile_Get_Ticks( &Time );
  886. }
  887. WWTimeItClass::~WWTimeItClass( void )
  888. {
  889. __int64 End;
  890. WWProfile_Get_Ticks( &End );
  891. End -= Time;
  892. #ifdef WWDEBUG
  893. float time = End * WWProfile_Get_Inv_Processor_Ticks_Per_Second();
  894. WWDEBUG_SAY(( "*** WWTIMEIT *** %s took %1.9f\n", Name, time ));
  895. #endif
  896. }
  897. /*
  898. **
  899. */
  900. WWMeasureItClass::WWMeasureItClass( float * p_result )
  901. {
  902. WWASSERT(p_result != NULL);
  903. PResult = p_result;
  904. WWProfile_Get_Ticks( &Time );
  905. }
  906. WWMeasureItClass::~WWMeasureItClass( void )
  907. {
  908. __int64 End;
  909. WWProfile_Get_Ticks( &End );
  910. End -= Time;
  911. WWASSERT(PResult != NULL);
  912. *PResult = End * WWProfile_Get_Inv_Processor_Ticks_Per_Second();
  913. }
  914. // ----------------------------------------------------------------------------
  915. //
  916. //
  917. //
  918. // ----------------------------------------------------------------------------
  919. unsigned WWMemoryAndTimeLog::TabCount;
  920. WWMemoryAndTimeLog::WWMemoryAndTimeLog(const char* name)
  921. :
  922. Name(name),
  923. TimeStart(WWProfile_Get_System_Time()),
  924. AllocCountStart(FastAllocatorGeneral::Get_Allocator()->Get_Total_Allocation_Count()),
  925. AllocSizeStart(FastAllocatorGeneral::Get_Allocator()->Get_Total_Allocated_Size())
  926. {
  927. IntermediateTimeStart=TimeStart;
  928. IntermediateAllocCountStart=AllocCountStart;
  929. IntermediateAllocSizeStart=AllocSizeStart;
  930. StringClass tmp(0,true);
  931. for (unsigned i=0;i<TabCount;++i) tmp+="\t";
  932. WWRELEASE_SAY(("%s%s {\n",tmp,name));
  933. TabCount++;
  934. }
  935. WWMemoryAndTimeLog::~WWMemoryAndTimeLog()
  936. {
  937. if (TabCount>0) TabCount--;
  938. StringClass tmp(0,true);
  939. for (unsigned i=0;i<TabCount;++i) tmp+="\t";
  940. WWRELEASE_SAY(("%s} ",tmp));
  941. unsigned current_time=WWProfile_Get_System_Time();
  942. int current_alloc_count=FastAllocatorGeneral::Get_Allocator()->Get_Total_Allocation_Count();
  943. int current_alloc_size=FastAllocatorGeneral::Get_Allocator()->Get_Total_Allocated_Size();
  944. WWRELEASE_SAY(("IN TOTAL %s took %d.%3.3d s, did %d memory allocations of %d bytes\n",
  945. Name,
  946. (current_time - TimeStart)/1000, (current_time - TimeStart)%1000,
  947. current_alloc_count - AllocCountStart,
  948. current_alloc_size - AllocSizeStart));
  949. WWRELEASE_SAY(("\n"));
  950. }
  951. void WWMemoryAndTimeLog::Log_Intermediate(const char* text)
  952. {
  953. unsigned current_time=WWProfile_Get_System_Time();
  954. int current_alloc_count=FastAllocatorGeneral::Get_Allocator()->Get_Total_Allocation_Count();
  955. int current_alloc_size=FastAllocatorGeneral::Get_Allocator()->Get_Total_Allocated_Size();
  956. StringClass tmp(0,true);
  957. for (unsigned i=0;i<TabCount;++i) tmp+="\t";
  958. WWRELEASE_SAY(("%s%s took %d.%3.3d s, did %d memory allocations of %d bytes\n",
  959. tmp,
  960. text,
  961. (current_time - IntermediateTimeStart)/1000, (current_time - IntermediateTimeStart)%1000,
  962. current_alloc_count - IntermediateAllocCountStart,
  963. current_alloc_size - IntermediateAllocSizeStart));
  964. IntermediateTimeStart=current_time;
  965. IntermediateAllocCountStart=current_alloc_count;
  966. IntermediateAllocSizeStart=current_alloc_size;
  967. }
  968. /***********************************************************************************************
  969. * WWProfileHierachyInfoClass::WWProfileHierachyInfoClass -- Constructor *
  970. * *
  971. * *
  972. * INPUT: *
  973. * name - pointer to a static string which is the name of this profile node *
  974. * parent - parent pointer *
  975. * *
  976. * OUTPUT: *
  977. * *
  978. * WARNINGS: *
  979. * *
  980. * HISTORY: *
  981. *=============================================================================================*/
  982. WWProfileHierachyInfoClass::WWProfileHierachyInfoClass( const char * name, WWProfileHierachyInfoClass * parent ) :
  983. Name( name ),
  984. TotalCalls( 0 ),
  985. TotalTime( 0 ),
  986. Parent( parent ),
  987. Child( NULL ),
  988. Sibling( NULL )
  989. {
  990. }
  991. /***********************************************************************************************
  992. * WWProfileHierachyNodeClass::~WWProfileHierachyNodeClass -- Destructor *
  993. * *
  994. * INPUT: *
  995. * *
  996. * OUTPUT: *
  997. * *
  998. * WARNINGS: *
  999. * *
  1000. * HISTORY: *
  1001. * 9/24/2000 gth : Created. *
  1002. *=============================================================================================*/
  1003. WWProfileHierachyInfoClass::~WWProfileHierachyInfoClass( void )
  1004. {
  1005. delete Child;
  1006. delete Sibling;
  1007. }