profile_result.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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. /////////////////////////////////////////////////////////////////////////EA-V1
  19. // $File: //depot/GeneralsMD/Staging/code/Libraries/Source/profile/profile_result.cpp $
  20. // $Author: mhoffe $
  21. // $Revision: #2 $
  22. // $DateTime: 2003/08/12 15:05:00 $
  23. //
  24. // ©2003 Electronic Arts
  25. //
  26. // Result function interface and result functions
  27. //////////////////////////////////////////////////////////////////////////////
  28. #include "_pch.h"
  29. #include <new>
  30. #include <stdio.h>
  31. #include <stdlib.h>
  32. //////////////////////////////////////////////////////////////////////////////
  33. // ProfileResultFileCSV
  34. ProfileResultInterface *ProfileResultFileCSV::Create(int, const char * const *)
  35. {
  36. return new (ProfileAllocMemory(sizeof(ProfileResultFileCSV))) ProfileResultFileCSV();
  37. }
  38. void ProfileResultFileCSV::WriteThread(ProfileFuncLevel::Thread &thread)
  39. {
  40. char help[40];
  41. sprintf(help,"prof%08x-all.csv",thread.GetId());
  42. FILE *f=fopen(help,"wt");
  43. // CSV file header
  44. fprintf(f,"Function\tFile\tCall count\tPTT (all)\tGTT (all)\tPT/C (all)\tGT/C (all)\tCaller (all)");
  45. for (unsigned k=0;k<Profile::GetFrameCount();k++)
  46. {
  47. const char *s=Profile::GetFrameName(k);
  48. fprintf(f,"\tCall (%s)\tPTT (%s)\tGTT (%s)\tPT/C (%s)\tGT/C (%s)\tCaller (%s)",s,s,s,s,s,s);
  49. }
  50. fprintf(f,"\n");
  51. // now show all profile IDs (functions)
  52. ProfileFuncLevel::Id id;
  53. for (k=0;thread.EnumProfile(k,id);k++)
  54. {
  55. fprintf(f,"%s[%08x]\t%s, %i",id.GetFunction(),id.GetAddress(),
  56. id.GetSource(),id.GetLine());
  57. for (unsigned i=ProfileFuncLevel::Id::Total;i!=Profile::GetFrameCount();i++)
  58. {
  59. if (!id.GetCalls(i))
  60. {
  61. // early skip...
  62. fprintf(f,"\t\t\t\t\t\t");
  63. continue;
  64. }
  65. // call count
  66. fprintf(f,"\t%I64i",id.GetCalls(i));
  67. // pure total time
  68. fprintf(f,"\t%I64i",id.GetFunctionTime(i));
  69. // global total time
  70. fprintf(f,"\t%I64i",id.GetTime(i));
  71. // pure time per call
  72. fprintf(f,"\t%I64i",id.GetFunctionTime(i)/id.GetCalls(i));
  73. // global time per call
  74. fprintf(f,"\t%I64i",id.GetTime(i)/id.GetCalls(i));
  75. // list of callers
  76. ProfileFuncLevel::IdList idlist=id.GetCaller(i);
  77. fprintf(f,"\t");
  78. ProfileFuncLevel::Id callid;
  79. unsigned count;
  80. for (unsigned j=0;idlist.Enum(j,callid,&count);j++)
  81. fprintf(f," %s[%08x](%i)",callid.GetFunction(),callid.GetAddress(),count);
  82. }
  83. fprintf(f,"\n");
  84. }
  85. fclose(f);
  86. }
  87. void ProfileResultFileCSV::WriteResults(void)
  88. {
  89. ProfileFuncLevel::Thread t;
  90. for (unsigned k=0;ProfileFuncLevel::EnumThreads(k,t);k++)
  91. WriteThread(t);
  92. FILE *f=fopen("profile-high.csv","wt");
  93. // CSV file header
  94. fprintf(f,"Profile\tUnit\ttotal");
  95. for (k=0;k<Profile::GetFrameCount();k++)
  96. fprintf(f,"\t%s",Profile::GetFrameName(k));
  97. fprintf(f,"\n");
  98. // now show all high level profile IDs
  99. ProfileHighLevel::Id id;
  100. for (k=0;ProfileHighLevel::EnumProfile(k,id);k++)
  101. {
  102. fprintf(f,"%s\t%s\t%s",id.GetName(),id.GetUnit(),id.GetTotalValue());
  103. for (unsigned i=0;i<Profile::GetFrameCount();i++)
  104. {
  105. const char *p=id.GetValue(i);
  106. fprintf(f,"\t%s",p?p:"");
  107. }
  108. fprintf(f,"\n");
  109. }
  110. fclose(f);
  111. }
  112. void ProfileResultFileCSV::Delete(void)
  113. {
  114. this->~ProfileResultFileCSV();
  115. ProfileFreeMemory(this);
  116. }
  117. //////////////////////////////////////////////////////////////////////////////
  118. // ProfileResultFileDOT
  119. ProfileResultInterface *ProfileResultFileDOT::Create(int argn, const char * const *argv)
  120. {
  121. return new (ProfileAllocMemory(sizeof(ProfileResultFileDOT)))
  122. ProfileResultFileDOT(argn>0?argv[0]:NULL,
  123. argn>1?argv[1]:NULL,
  124. argn>2?atoi(argv[2]):NULL);
  125. }
  126. ProfileResultFileDOT::ProfileResultFileDOT(const char *fileName, const char *frameName, int foldThreshold)
  127. {
  128. if (!fileName)
  129. fileName="profile.dot";
  130. m_fileName=(char *)ProfileAllocMemory(strlen(fileName)+1);
  131. strcpy(m_fileName,fileName);
  132. if (frameName)
  133. {
  134. m_frameName=(char *)ProfileAllocMemory(strlen(frameName)+1);
  135. strcpy(m_frameName,frameName);
  136. }
  137. else
  138. m_frameName=NULL;
  139. m_foldThreshold=foldThreshold;
  140. }
  141. void ProfileResultFileDOT::WriteResults(void)
  142. {
  143. // search "main" thread
  144. ProfileFuncLevel::Thread t,tMax;
  145. if (!ProfileFuncLevel::EnumThreads(0,tMax))
  146. return;
  147. unsigned curMax=0;
  148. for (unsigned k=1;ProfileFuncLevel::EnumThreads(k,t);k++)
  149. {
  150. for (;curMax++;)
  151. {
  152. ProfileFuncLevel::Id help;
  153. if (!tMax.EnumProfile(curMax,help))
  154. {
  155. tMax=t;
  156. break;
  157. }
  158. if (!t.EnumProfile(curMax,help))
  159. break;
  160. curMax++;
  161. }
  162. }
  163. // search frame
  164. unsigned frame=ProfileFuncLevel::Id::Total;
  165. if (m_frameName)
  166. {
  167. for (unsigned k=0;k<Profile::GetFrameCount();k++)
  168. if (!strcmp(Profile::GetFrameName(k),m_frameName))
  169. {
  170. frame=k;
  171. break;
  172. }
  173. }
  174. // determine number of active functions
  175. unsigned active=0;
  176. ProfileFuncLevel::Id id;
  177. for (k=0;tMax.EnumProfile(k,id);k++)
  178. if (id.GetCalls(frame))
  179. active++;
  180. FILE *f=fopen(m_fileName,"wt");
  181. if (!f)
  182. return;
  183. // DOT header
  184. fprintf(f,"digraph G { rankdir=\"LR\";\n");
  185. fprintf(f,"node [shape=box, fontname=Arial]\n");
  186. fprintf(f,"edge [arrowhead=%s, labelfontname=Arial, labelfontsize=10, labelangle=0, labelfontcolor=blue]\n",
  187. active>m_foldThreshold?"closed":"none");
  188. // fold or not?
  189. if (active>m_foldThreshold)
  190. {
  191. // folding version
  192. // build source code clusters first
  193. FoldHelper *fold=NULL;
  194. for (k=0;tMax.EnumProfile(k,id);k++)
  195. {
  196. const char *source=id.GetSource();
  197. for (FoldHelper *cur=fold;cur;cur=cur->next)
  198. if (!strcmp(source,cur->source))
  199. {
  200. if (cur->numId<MAX_FUNCTIONS_PER_FILE)
  201. cur->id[cur->numId++]=id;
  202. break;
  203. }
  204. if (!cur)
  205. {
  206. cur=(FoldHelper *)ProfileAllocMemory(sizeof(FoldHelper));
  207. cur->next=fold;
  208. fold=cur;
  209. cur->source=source;
  210. cur->numId=1;
  211. cur->id[0]=id;
  212. }
  213. }
  214. // now write data
  215. for (FoldHelper *cur=fold;cur;cur=cur->next)
  216. {
  217. for (FoldHelper *cur2=fold;cur2;cur2=cur2->next)
  218. cur2->mark=false;
  219. for (k=0;k<cur->numId;k++)
  220. {
  221. ProfileFuncLevel::IdList idlist=id.GetCaller(frame);
  222. ProfileFuncLevel::Id caller;
  223. for (unsigned i=0;idlist.Enum(i,caller);i++)
  224. {
  225. const char *s=caller.GetSource();
  226. for (FoldHelper *cur2=fold;cur2;cur2=cur2->next)
  227. if (!strcmp(cur2->source,s))
  228. break;
  229. if (!cur2||cur2->mark)
  230. continue;
  231. cur2->mark=true;
  232. fprintf(f,"\"%s\" -> \"%s\"\n",s,cur->source);
  233. }
  234. }
  235. }
  236. // cleanup
  237. while (fold)
  238. {
  239. FoldHelper *next=fold->next;
  240. ProfileFreeMemory(fold);
  241. fold=next;
  242. }
  243. }
  244. else
  245. {
  246. // non-folding version
  247. for (k=0;tMax.EnumProfile(k,id);k++)
  248. if (id.GetCalls(frame))
  249. fprintf(f,"f%08x [label=\"%s\"]\n",id.GetAddress(),id.GetFunction());
  250. for (k=0;tMax.EnumProfile(k,id);k++)
  251. {
  252. ProfileFuncLevel::IdList idlist=id.GetCaller(frame);
  253. ProfileFuncLevel::Id caller;
  254. unsigned count;
  255. for (unsigned i=0;idlist.Enum(i,caller,&count);i++)
  256. fprintf(f,"f%08x -> f%08x [headlabel=\"%i\"];\n",caller.GetAddress(),id.GetAddress(),count);
  257. }
  258. }
  259. fprintf(f,"}\n");
  260. fclose(f);
  261. }
  262. void ProfileResultFileDOT::Delete(void)
  263. {
  264. this->~ProfileResultFileDOT();
  265. ProfileFreeMemory(this);
  266. }