source/myhtml/perf.c

filter: Statementstmt 0.0% 0 / 20 Branchbran 0.0% 0 / 16 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 0.0% 0 / 36 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« source/myhtml/parser.c ↑ Summary source/myhtml/tag_init.c »
1 /*
2 Copyright 2015 Alexander Borisov
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15
16 Authors: insoreiges@gmail.com (Evgeny Yakovlev), lex.borisov@gmail.com (Alexander Borisov)
17 */
18  
19 /**
20 * Platform-specific hdef performance clock value.
21 */
22  
23 #include "myhtml/myhtml.h"
24 #include <time.h>
25  
26 #if !defined(IS_OS_WINDOWS)
27 #include <unistd.h>
28 #endif
29  
30 #if !defined(MyHTML_WITH_PERF)
31  
32 0 uint64_t myhtml_hperf_res(myhtml_status_t *status)
33 {
34 0 ▶ if(status)
Branch: 1 branch
BranchTrueFalseTotal
if(status) 0 0 0
35 0 *status = MyHTML_STATUS_PERF_ERROR_COMPILED_WITHOUT_PERF;
36
37 0 return 0;
38 }
39  
40 0 uint64_t myhtml_hperf_clock(myhtml_status_t *status)
41 {
42 0 ▶ if(status)
Branch: 1 branch
BranchTrueFalseTotal
if(status) 0 0 0
43 0 *status = MyHTML_STATUS_PERF_ERROR_COMPILED_WITHOUT_PERF;
44
45 0 return 0;
46 }
47  
48 #else
49  
50 #if defined(__APPLE__)
51 #include <sys/types.h>
52 #include <sys/sysctl.h>
53 #elif defined(IS_OS_WINDOWS)
54 #endif
55  
56 #if defined(MyHTML_FORCE_RDTSC) /* Force using rdtsc, useful for comparison */
57  
58 /**
59 * Get CPU rdtsc frequency.
60 *
61 * TODO: I think using rdtsc for measuring user-space counters is not correct:
62 * - rdtsc does not have a constant rate. instead ot is scaled to physical core's internal clock which changes due to power saving modes on modern CPUs
63 * - rdtsc is software-emulated in virtual machines which will introduce an inconsistency in reported ticks
64 * - user space process can be preempted between consecutive rdtsc measures but the physical clock will still tick while it is executing a different thread.
65 * also think what would happen if preempted process will be re-scheduled on a different physical core which has a different tsc value.
66 * - computing rdtsc frequency produces unreliable results (due to all of the above)
67 *
68 * Consider using platform-specific monotonic hperf timers (ftrace/dtrace) or even clock().
69 */
70 uint64_t myhtml_hperf_res(myhtml_status_t *status)
71 {
72 if(status)
73 *status = MyHTML_STATUS_OK;
74
75 #if defined(__APPLE__) && defined(CTL_HW) && defined(HW_CPU_FREQ)
76 unsigned long long freq = 0;
77
78 /* OSX kernel: sysctl(CTL_HW | HW_CPU_FREQ) */
79 size_t len = sizeof(freq);
80 int mib[2] = {CTL_HW, HW_CPU_FREQ};
81  
82 int error = sysctl(mib, 2, &freq, &len, NULL, 0);
83 if (error) {
84 if(status)
85 *status = MyHTML_STATUS_PERF_ERROR_FIND_CPU_CLOCK;
86
87 return 0;
88 }
89  
90 return freq;
91  
92 #elif defined(__linux__)
93 unsigned long long freq = 0;
94
95 /* Use procfs on linux */
96 FILE* fp = NULL;
97 fp = fopen("/proc/cpuinfo", "r");
98 if (fp == NULL) {
99 if(status)
100 *status = MyHTML_STATUS_PERF_ERROR_FIND_CPU_CLOCK;
101
102 return 0;
103 }
104  
105 /* Find 'CPU MHz :' */
106 char buf[1024] = {0};
107 double fval = 0.0;
108 while (fgets(buf, sizeof(buf), fp) != NULL) {
109 if (sscanf(buf, "cpu MHz : %lf\n", &fval) == 1) {
110 freq = (unsigned long long)(fval * 1000000ull);
111 break;
112 }
113 }
114  
115 fclose(fp);
116 return freq;
117  
118 #else
119 # warning Cant figure out cpu frequency on this platfrom
120
121 if(status)
122 *status = MyHTML_STATUS_PERF_ERROR_FIND_CPU_CLOCK;
123
124 return 0;
125 #endif /* defined __APPLE__ || __linux__ ... */
126 }
127  
128 uint64_t myhtml_hperf_clock(myhtml_status_t *status)
129 {
130 uint64_t x;
131  
132 __asm__ volatile (
133 "cpuid\n\t" /* cpuid serializes any out-of-order prefetches before executing rdtsc (clobbers ebx, ecx, edx) */
134 "rdtsc\n\t"
135 "shl $32, %%rdx\n\t"
136 "or %%rdx, %%rax"
137 : "=a" (x)
138 :
139 : "rdx", "ebx", "ecx");
140  
141 return x;
142 }
143  
144 #elif defined(_POSIX_TIMERS) && defined(_POSIX_CPUTIME) \
145 && defined(_POSIX_C_SOURCE) && (_POSIX_C_SOURCE >= 199309L) /* Do we have clock_gettime? */
146  
147 #define NSEC_PER_SECOND 1000000000ull
148 #define TIMESPEC_TO_USEC(tspec) (((uint64_t)(tspec).tv_sec * NSEC_PER_SECOND) + (tspec).tv_nsec)
149  
150 uint64_t myhtml_hperf_res(myhtml_status_t *status)
151 {
152 if(status)
153 *status = MyHTML_STATUS_OK;
154
155 struct timespec tspec;
156 int error = clock_getres(CLOCK_PROCESS_CPUTIME_ID, &tspec);
157 if (error) {
158 if(status)
159 *status = MyHTML_STATUS_PERF_ERROR_FIND_CPU_CLOCK;
160
161 return 0;
162 }
163  
164 unsigned long long ticks_per_sec = (unsigned long long)((double)NSEC_PER_SECOND / TIMESPEC_TO_USEC(tspec));
165 return ticks_per_sec;
166 }
167  
168 uint64_t myhtml_hperf_clock(myhtml_status_t *status)
169 {
170 if(status)
171 *status = MyHTML_STATUS_OK;
172
173 struct timespec tspec;
174 int error = clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &tspec);
175 if (error) {
176 if(status)
177 *status = MyHTML_STATUS_PERF_ERROR_FIND_CPU_CLOCK;
178
179 return 0;
180 }
181  
182 return TIMESPEC_TO_USEC(tspec);
183 }
184  
185 #elif defined(__APPLE__) && defined(__MACH__)
186  
187 /*
188 * TODO: on OSX we can use clock_get_time: http://stackoverflow.com/questions/5167269/clock-gettime-alternative-in-mac-os-x
189 * Or this: http://web.archive.org/web/20100517095152/http://www.wand.net.nz/~smr26/wordpress/2009/01/19/monotonic-time-in-mac-os-x/comment-page-1/
190 */
191  
192 // TODO: this is incorrect plug for mac os x
193 // look at links before this comment
194  
195 #include <mach/mach_time.h>
196  
197 uint64_t myhtml_hperf_res(myhtml_status_t *status)
198 {
199 if(status)
200 *status = MyHTML_STATUS_OK;
201
202 unsigned long long freq = 0;
203
204 size_t len = sizeof(freq);
205 int mib[2] = {CTL_HW, HW_CPU_FREQ};
206
207 int error = sysctl(mib, 2, &freq, &len, NULL, 0);
208 if (error) {
209 if(status)
210 *status = MyHTML_STATUS_PERF_ERROR_FIND_CPU_CLOCK;
211
212 return 0;
213 }
214
215 return freq;
216 }
217  
218 uint64_t myhtml_hperf_clock(myhtml_status_t *status)
219 {
220 if(status)
221 *status = MyHTML_STATUS_OK;
222
223 return mach_absolute_time();
224 }
225  
226 #else
227  
228 # warning No hperf implementation for this platform
229  
230 uint64_t myhtml_hperf_res(myhtml_status_t *status)
231 {
232 if(status)
233 *status = MyHTML_STATUS_PERF_ERROR_FIND_CPU_CLOCK;
234
235 return 0;
236 }
237  
238 uint64_t myhtml_hperf_clock(myhtml_status_t *status)
239 {
240 if(status)
241 *status = MyHTML_STATUS_PERF_ERROR_FIND_CPU_CLOCK;
242
243 return 0;
244 }
245  
246 #endif /* defined(MyHTML_FORCE_RDTSC) ... */
247 #endif /* MyHTML_WITH_PERF */
248  
249 #define _MyHTML_CHECK_STATUS_AND_PRINT_ERROR \
250 if(status == MyHTML_STATUS_PERF_ERROR_COMPILED_WITHOUT_PERF) { \
251 fprintf(fh, "MyHTML: Library compiled without perf source. Please, build library with -DMyHTML_WITH_PERF flag\n"); \
252 } \
253 else if(status) { \
254 fprintf(fh, "MyHTML: Something wrong! Perhaps, your platform does not support the measurement of performance\n"); \
255 } \
256 else
257  
258 0 myhtml_status_t myhtml_hperf_print(const char *name, uint64_t x, uint64_t y, FILE *fh) {
259 myhtml_status_t status;
260
261 0 unsigned long long freq = myhtml_hperf_res(&status);
262
263 0 ▶ if(freq) {
Branch: 1 branch
BranchTrueFalseTotal
if(freq) { 0 0 0
264 0 ▶ _MyHTML_CHECK_STATUS_AND_PRINT_ERROR {
Branch: 2 branches
BranchTrueFalseTotal
_MyHTML_CHECK_STATUS_AND_PRINT_ERROR { 0 0 0
_MyHTML_CHECK_STATUS_AND_PRINT_ERROR { 0 0 0
265 0 fprintf(fh, "%s: %0.5f\n", name, (((float)(y - x) / (float)freq)));
266 }
267 }
268
269 0 return status;
270 }
271  
272 0 myhtml_status_t myhtml_hperf_print_by_val(const char *name, uint64_t x, FILE *fh) {
273 myhtml_status_t status;
274
275 0 unsigned long long freq = myhtml_hperf_res(&status);
276
277 0 ▶ if(freq) {
Branch: 1 branch
BranchTrueFalseTotal
if(freq) { 0 0 0
278 0 ▶ _MyHTML_CHECK_STATUS_AND_PRINT_ERROR {
Branch: 2 branches
BranchTrueFalseTotal
_MyHTML_CHECK_STATUS_AND_PRINT_ERROR { 0 0 0
_MyHTML_CHECK_STATUS_AND_PRINT_ERROR { 0 0 0
279 0 fprintf(fh, "%s: %0.5f\n", name, ((float)x / (float)freq));
280 }
281 }
282
283 0 return status;
284 }
285  
286  
« source/myhtml/parser.c ↑ Summary source/myhtml/tag_init.c »