bumping version to 3.5-rc1
[supercollider.git] / server / supernova / utilities / cpu_timer.hpp
bloba8e3200bd5fe9d3dde7904f139d9870ee5eea863
1 // cpu utilization timer
2 // Copyright (C) 2006, 2008 Tim Blechmann
3 //
4 // This program is free software; you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation; either version 2 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with this program; see the file COPYING. If not, write to
16 // the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 // Boston, MA 02111-1307, USA.
21 #ifndef CPU_TIMER_HPP
22 #define CPU_TIMER_HPP
24 #if defined(__linux__) || defined(__APPLE__) || defined(HAVE_TIMES_H)
25 #define USE_TIMES_H
26 #include <sys/times.h>
27 #elif defined(WIN32)
28 #include <Windows.h>
29 #else
30 #error not implemented
31 #endif
34 namespace nova
37 class cpu_timer
39 public:
40 void set(void)
42 #ifdef USE_TIMES_H
43 static const double inv_clock_per_ms = 1. / (CLOCKS_PER_SEC * 1000);
44 tms time;
45 times(&time);
46 ms = (time.tms_utime + time.tms_stime) * inv_clock_per_ms;
47 #elif defined(WIN32)
48 FILETIME ignorethis, ignorethat;
50 bool status = GetProcessTimes(GetCurrentProcess(), &ignorethis, &ignorethat,
51 &m_kerneltime, &m_usertime);
52 assert(status);
53 #else
54 #error not implemented
55 #endif
58 double get(void)
60 #ifdef USE_TIMES_H
61 static const double inv_clock_per_ms = 1. / (CLOCKS_PER_SEC * 1000);
62 tms time;
63 times(&time);
64 double now = (time.tms_utime + time.tms_stime) * inv_clock_per_ms;
65 return now - ms;
66 #elif defined(WIN32)
67 FILETIME ignorethis, ignorethat;
68 FILETIME usertime, kerneltime;
70 bool status = retval = GetProcessTimes(GetCurrentProcess(), &ignorethis, &ignorethat,
71 &kerneltime, &usertime);
73 assert(status);
75 /* ignore high word */
76 double low = ( (kerneltime.dwLowDateTime - m_kerneltime.dwLowDateTime) +
77 (usertime.dwLowDateTime - m_usertime.dwLowDateTime) ) * 0.0001;
78 return low;
79 #else
80 #error not implemented
81 #endif
84 #ifdef USE_TIMES_H
85 double ms;
86 #elif defined(WIN32)
87 FILETIME m_kerneltime;
88 FILETIME m_usertime;
89 #endif
92 } /* namespace nova */
94 #endif /* CPU_TIMER_HPP */