First public contribution.
2 * LIBOIL - Library of Optimized Inner Loops
3 * Copyright (c) 2003,2004 David A. Schleef <ds@schleef.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
19 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
23 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
24 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25 * POSSIBILITY OF SUCH DAMAGE.
27 //Portions Copyright (c) 2008-2009 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.
32 #include <liboil/liboilfunction.h>
33 #include <liboil/liboildebug.h>
34 #include <liboil/liboilcpu.h>
35 #include <liboil/liboilfault.h>
36 #include <liboil/liboilutils.h>
47 #ifdef HAVE_SYS_TIME_H
52 #if defined(__FreeBSD__)
53 #include <sys/types.h>
54 #include <sys/sysctl.h>
66 * @short_description: Check the capabilities of the current CPU
70 //void oil_cpu_detect_arch(void);
72 unsigned long oil_cpu_flags;
74 extern unsigned long (*_oil_profile_stamp)(void);
76 #ifdef HAVE_GETTIMEOFDAY
78 oil_profile_stamp_gtod (void)
81 gettimeofday(&tv,NULL);
82 return 1000000*(unsigned long)tv.tv_sec + (unsigned long)tv.tv_usec;
86 #if defined(HAVE_CLOCK_GETTIME) && defined(HAVE_MONOTONIC_CLOCK)
88 oil_profile_stamp_clock_gettime (void)
91 clock_gettime (CLOCK_MONOTONIC, &ts);
92 return 1000000000*ts.tv_sec + ts.tv_nsec;
97 oil_profile_stamp_zero (void)
110 oil_cpu_detect_arch();
112 envvar = getenv ("OIL_CPU_FLAGS");
113 if (envvar != NULL) {
117 flags = strtoul (envvar, &end, 0);
119 oil_cpu_flags = flags;
121 OIL_INFO ("cpu flags from environment %08lx", oil_cpu_flags);
124 OIL_INFO ("cpu flags %08lx", oil_cpu_flags);
126 #if defined(HAVE_CLOCK_GETTIME) && defined(HAVE_MONOTONIC_CLOCK)
127 if (_oil_profile_stamp == NULL) {
128 _oil_profile_stamp = oil_profile_stamp_clock_gettime;
129 OIL_INFO("Using clock_gettime() as a timestamp function.");
133 #ifdef HAVE_GETTIMEOFDAY
134 if (_oil_profile_stamp == NULL) {
135 _oil_profile_stamp = oil_profile_stamp_gtod;
136 OIL_WARNING("Using gettimeofday() as a timestamp function.");
139 if (_oil_profile_stamp == NULL) {
140 _oil_profile_stamp = oil_profile_stamp_zero;
141 OIL_ERROR("No timestamping function. This is kinda bad.");
148 * Returns a bitmask containing the available CPU features.
150 * Returns: the CPU features.
156 oil_cpu_get_flags (void)
158 return oil_cpu_flags;
164 * oil_cpu_get_ticks_per_second:
166 * Returns the estimated number of ticks per second. This feature
167 * is currently unimplemented.
169 * This function may take several milliseconds or more to execute
170 * in order to calculate a good estimate of the number of ticks (as
171 * measured by the profiling functions) per second. Note that the
172 * number of ticks per second is often dependent on the CPU frequency,
173 * which can change dynamically. Thus the value returned by this
174 * function may be incorrect as soon as it is returned.
179 oil_cpu_get_ticks_per_second (void)
181 return _oil_ticks_per_second;
189 oil_cpu_get_frequency (void)
191 #if defined(__linux__)
193 if (get_file_int ("/sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq",
195 return 1000.0 * freq;