1 /*
2  * ECOS - Embedded Conic Solver.
3  * Copyright (C) 2012-2015 A. Domahidi [domahidi@embotech.com],
4  * Automatic Control Lab, ETH Zurich & embotech GmbH, Zurich, Switzerland.
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 
21 
22 /*
23  * Implements for the built-in timer of ECOS.
24  *
25  * Under Windows, we use QueryPerformanceCounter to obtain the counter value.
26  *
27  * Under Unix systems, we use clock_gettime function from <time.h>
28  */
29 
30 
31 #include "timer.h"
32 
33 #if PROFILING > 0
34 
35 #if (defined WIN32 || _WIN64)
36 
37 #include <windows.h>
38 
tic(timer * t)39 void tic(timer* t)
40 {
41 	QueryPerformanceFrequency((LARGE_INTEGER*)&t->freq);
42 	QueryPerformanceCounter((LARGE_INTEGER*)&t->tic);
43 }
44 
toc(timer * t)45 pfloat toc(timer* t)
46 {
47 	QueryPerformanceCounter((LARGE_INTEGER*)&t->toc);
48 	return ((t->toc - t->tic) / (pfloat)t->freq);
49 }
50 
51 
52 #elif (defined __APPLE__)
53 
tic(timer * t)54 void tic(timer* t)
55 {
56     /* read current clock cycles */
57     t->tic = mach_absolute_time();
58 }
59 
toc(timer * t)60 pfloat toc(timer* t)
61 {
62 
63     uint64_t duration; /* elapsed time in clock cycles*/
64 
65     t->toc = mach_absolute_time();
66     duration = t->toc - t->tic;
67 
68     /*conversion from clock cycles to nanoseconds*/
69     mach_timebase_info(&(t->tinfo));
70     duration *= t->tinfo.numer;
71     duration /= t->tinfo.denom;
72 
73     return (pfloat)duration / 1000000000;
74 }
75 
76 
77 
78 #else
79 
80 /* read current time */
tic(timer * t)81 void tic(timer* t)
82 {
83 	clock_gettime(CLOCK_MONOTONIC, &t->tic);
84 }
85 
86 
87 /* return time passed since last call to tic on this timer */
toc(timer * t)88 double toc(timer* t)
89 {
90 	struct timespec temp;
91 
92 	clock_gettime(CLOCK_MONOTONIC, &t->toc);
93 
94 	if ((t->toc.tv_nsec - t->tic.tv_nsec)<0) {
95 		temp.tv_sec = t->toc.tv_sec - t->tic.tv_sec-1;
96 		temp.tv_nsec = 1000000000+t->toc.tv_nsec - t->tic.tv_nsec;
97 	} else {
98 		temp.tv_sec = t->toc.tv_sec - t->tic.tv_sec;
99 		temp.tv_nsec = t->toc.tv_nsec - t->tic.tv_nsec;
100 	}
101 	return (pfloat)temp.tv_sec + (pfloat)temp.tv_nsec / 1000000000;
102 }
103 
104 #endif
105 
106 
107 #endif
108