1 /*
2 * Copyright (c) 2017, Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included
12 * in all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 * OTHER DEALINGS IN THE SOFTWARE.
21 */
22 //
23 // cm_performance.cpp
24 // Include the standard header and generate the precompiled header.
25 //
26 #include <ctime>
27 #include "cm_include.h"
28
QueryPerformanceFrequency(LARGE_INTEGER * frequency)29 extern "C" int32_t QueryPerformanceFrequency(LARGE_INTEGER *frequency)
30 {
31 struct timespec res;
32 int32_t ret;
33
34 if ( (ret = clock_getres(CLOCK_MONOTONIC, &res)) != 0 )
35 {
36 return -1;
37 }
38
39 // resolution (precision) can't be in seconds for current machine and OS
40 if (res.tv_sec != 0)
41 {
42 return -1;
43 }
44 frequency->QuadPart = (1000000000LL) / res.tv_nsec;
45
46 return 0;
47 }
48
QueryPerformanceCounter(LARGE_INTEGER * performanceCount)49 extern "C" int32_t QueryPerformanceCounter(LARGE_INTEGER *performanceCount)
50 {
51 struct timespec res;
52 struct timespec t;
53 int32_t ret;
54
55 if ( (ret = clock_getres (CLOCK_MONOTONIC, &res)) != 0 )
56 {
57 return -1;
58 }
59 if (res.tv_sec != 0)
60 { // resolution (precision) can't be in seconds for current machine and OS
61 return -1;
62 }
63 if( (ret = clock_gettime(CLOCK_MONOTONIC, &t)) != 0)
64 {
65 return -1;
66 }
67 performanceCount->QuadPart = (1000000000LL * t.tv_sec +
68 t.tv_nsec) / res.tv_nsec;
69
70 return 0;
71 }
72
73