Initialize libbullet git in 2.0_beta.
[platform/upstream/libbullet.git] / Extras / PhysicsEffects / include / physics_effects / base_level / base / pfx_perf_counter.h
1 /*\r
2 Physics Effects Copyright(C) 2010 Sony Computer Entertainment Inc.\r
3 All rights reserved.\r
4 \r
5 Physics Effects is open software; you can redistribute it and/or\r
6 modify it under the terms of the BSD License.\r
7 \r
8 Physics Effects is distributed in the hope that it will be useful,\r
9 but WITHOUT ANY WARRANTY; without even the implied warranty of\r
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\r
11 See the BSD License for more details.\r
12 \r
13 A copy of the BSD License is distributed with\r
14 Physics Effects under the filename: physics_effects_license.txt\r
15 */\r
16 \r
17 #ifndef _SCE_PFX_PERF_COUNTER_H\r
18 #define _SCE_PFX_PERF_COUNTER_H\r
19 \r
20 #include "pfx_common.h"\r
21 \r
22 // ARA begin insert new code\r
23 #ifndef _WIN32\r
24 #include <time.h>\r
25 #endif\r
26 // ARA end\r
27 \r
28 //J パフォーマンス測定する場合はPFX_USE_PERFCOUNTERを定義\r
29 //J ブックマークを使用する場合はPFX_USE_BOOKMARKを定義\r
30 \r
31 //E Define SCE_PFX_USE_PERFCOUNTER to check performance\r
32 //E Define SCE_PFX_USE_BOOKMARK to use bookmark\r
33 \r
34 \r
35 #define SCE_PFX_MAX_PERF_STR    32\r
36 #define SCE_PFX_MAX_PERF_COUNT  20\r
37 \r
38 //#define SCE_PFX_USE_PERFCOUNTER\r
39 //#define SCE_PFX_USE_BOOKMARK\r
40 \r
41 namespace sce {\r
42 namespace PhysicsEffects {\r
43 #ifdef SCE_PFX_USE_PERFCOUNTER\r
44 \r
45 class PfxPerfCounter\r
46 {\r
47 private:\r
48         int   m_count,m_strCount;\r
49         char  m_str[SCE_PFX_MAX_PERF_COUNT][SCE_PFX_MAX_PERF_STR];\r
50         float m_freq;\r
51 \r
52         SCE_PFX_PADDING(1,4)\r
53 #ifdef _WIN32\r
54         LONGLONG  m_cnt[SCE_PFX_MAX_PERF_COUNT*2];\r
55 #else\r
56 // ARA begin insert new code\r
57         timespec m_cnt[SCE_PFX_MAX_PERF_COUNT*2];\r
58 // ARA end\r
59 #endif\r
60 \r
61         void count(int i)\r
62         {\r
63 #ifdef _WIN32\r
64                 QueryPerformanceCounter( (LARGE_INTEGER *)&m_cnt[i] );\r
65 #else\r
66 // ARA begin insert new code\r
67                 clock_gettime(CLOCK_MONOTONIC, &m_cnt[i]);\r
68 // ARA end\r
69 #endif\r
70         }\r
71 \r
72 public:\r
73         PfxPerfCounter()\r
74         {\r
75 #ifdef _WIN32\r
76                 LARGE_INTEGER sPerfCountFreq;\r
77                 QueryPerformanceFrequency(&sPerfCountFreq);\r
78                 m_freq = (float)sPerfCountFreq.QuadPart;\r
79 #else\r
80 // ARA begin insert new code \r
81                 m_freq = 1000000000.0f; // clock_gettime reports time in nanoseconds (though accuracy is platform dependent)\r
82 // ARA end\r
83 #endif\r
84                 resetCount();\r
85         }\r
86 \r
87         ~PfxPerfCounter()\r
88         {\r
89                 //printCount();\r
90         }\r
91 \r
92         void countBegin(const char *name)\r
93         {\r
94                 SCE_PFX_ASSERT(m_strCount < SCE_PFX_MAX_PERF_COUNT);\r
95                 strncpy(m_str[m_strCount],name,SCE_PFX_MAX_PERF_STR-1);\r
96                 m_str[m_strCount][SCE_PFX_MAX_PERF_STR-1] = 0x00;\r
97                 m_strCount++;\r
98                 count(m_count++);\r
99         }\r
100         \r
101         void countEnd()\r
102         {\r
103                 count(m_count++);\r
104         }\r
105 \r
106         void resetCount()\r
107         {\r
108                 m_strCount = 0;\r
109                 m_count = 0;\r
110         }\r
111 \r
112         float getCountTime(int i)\r
113         {\r
114 #if _WIN32\r
115         return (float)(m_cnt[i+1]-m_cnt[i]) / m_freq * 1000.0f;\r
116 #else\r
117 // ARA begin insert new code \r
118         return float(m_cnt[i+1].tv_sec - m_cnt[i].tv_sec) +\r
119                                  (float(m_cnt[i+1].tv_nsec - m_cnt[i].tv_nsec) / m_freq);\r
120 // ARA end\r
121 #endif  \r
122 }\r
123 \r
124         void printCount()\r
125         {\r
126                 if(m_count%2 != 0) countEnd();\r
127                 SCE_PFX_PRINTF("*** PfxPerfCounter results ***\n");\r
128                 float total = 0.0f;\r
129                 for(int i=0;i+1<m_count;i+=2) {\r
130                         total += getCountTime(i);\r
131                 }\r
132                 for(int i=0;i+1<m_count;i+=2) {\r
133                         SCE_PFX_PRINTF(" -- %s %fms(%.2f%%)\n",m_str[i>>1],getCountTime(i),getCountTime(i)/total*100.0f);\r
134                 }\r
135                 SCE_PFX_PRINTF(" -- Total %fms\n",total);\r
136         }\r
137 };\r
138 \r
139 #else /* SCE_PFX_USE_PERFCOUNTER */\r
140 \r
141 class PfxPerfCounter\r
142 {\r
143 public:\r
144         PfxPerfCounter() {}\r
145         ~PfxPerfCounter() {}\r
146         void countBegin(const char *name) {(void) name;}\r
147         void countEnd() {}\r
148         void resetCount() {}\r
149         float getCountTime(int i) {(void)i;return 0.0f;}\r
150         void printCount() {}\r
151 };\r
152 \r
153 #endif /* SCE_PFX_USE_PERFCOUNTER */\r
154 \r
155 #define pfxInsertBookmark(bookmark)\r
156 \r
157 #ifdef SCE_PFX_USE_BOOKMARK\r
158         #define SCE_PFX_PUSH_MARKER(name)\r
159         #define SCE_PFX_POP_MARKER()\r
160 #else\r
161         #define SCE_PFX_PUSH_MARKER(name)\r
162         #define SCE_PFX_POP_MARKER()\r
163 #endif\r
164 \r
165 } //namespace PhysicsEffects\r
166 } //namespace sce\r
167 \r
168 #endif // _SCE_PFX_PERF_COUNTER_H\r