1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/ossrv/genericopenlibs/liboil/src/liboilcpu.c Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,202 @@
1.4 +/*
1.5 + * LIBOIL - Library of Optimized Inner Loops
1.6 + * Copyright (c) 2003,2004 David A. Schleef <ds@schleef.org>
1.7 + * All rights reserved.
1.8 + *
1.9 + * Redistribution and use in source and binary forms, with or without
1.10 + * modification, are permitted provided that the following conditions
1.11 + * are met:
1.12 + * 1. Redistributions of source code must retain the above copyright
1.13 + * notice, this list of conditions and the following disclaimer.
1.14 + * 2. Redistributions in binary form must reproduce the above copyright
1.15 + * notice, this list of conditions and the following disclaimer in the
1.16 + * documentation and/or other materials provided with the distribution.
1.17 + *
1.18 + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
1.19 + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
1.20 + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1.21 + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
1.22 + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
1.23 + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
1.24 + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
1.25 + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
1.26 + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
1.27 + * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
1.28 + * POSSIBILITY OF SUCH DAMAGE.
1.29 + */
1.30 +//Portions Copyright (c) 2008-2009 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.
1.31 +
1.32 +#ifdef HAVE_CONFIG_H
1.33 +#include "config.h"
1.34 +#endif
1.35 +#include <liboil/liboilfunction.h>
1.36 +#include <liboil/liboildebug.h>
1.37 +#include <liboil/liboilcpu.h>
1.38 +#include <liboil/liboilfault.h>
1.39 +#include <liboil/liboilutils.h>
1.40 +
1.41 +#ifdef HAVE_UNISTD_H
1.42 +#include <unistd.h>
1.43 +#endif
1.44 +#include <fcntl.h>
1.45 +#include <stdlib.h>
1.46 +#include <string.h>
1.47 +#include <stdio.h>
1.48 +#include <setjmp.h>
1.49 +#include <signal.h>
1.50 +#ifdef HAVE_SYS_TIME_H
1.51 +#include <sys/time.h>
1.52 +#endif
1.53 +#include <time.h>
1.54 +
1.55 +#if defined(__FreeBSD__)
1.56 +#include <sys/types.h>
1.57 +#include <sys/sysctl.h>
1.58 +#endif
1.59 +
1.60 +#ifdef __sun
1.61 +#include <sys/auxv.h>
1.62 +#endif
1.63 +
1.64 +
1.65 +
1.66 +/**
1.67 + * SECTION:liboilcpu
1.68 + * @title: CPU
1.69 + * @short_description: Check the capabilities of the current CPU
1.70 + *
1.71 + */
1.72 +
1.73 +//void oil_cpu_detect_arch(void);
1.74 +
1.75 +unsigned long oil_cpu_flags;
1.76 +
1.77 +extern unsigned long (*_oil_profile_stamp)(void);
1.78 +
1.79 +#ifdef HAVE_GETTIMEOFDAY
1.80 +static unsigned long
1.81 +oil_profile_stamp_gtod (void)
1.82 +{
1.83 + struct timeval tv;
1.84 + gettimeofday(&tv,NULL);
1.85 + return 1000000*(unsigned long)tv.tv_sec + (unsigned long)tv.tv_usec;
1.86 +}
1.87 +#endif
1.88 +
1.89 +#if defined(HAVE_CLOCK_GETTIME) && defined(HAVE_MONOTONIC_CLOCK)
1.90 +static unsigned long
1.91 +oil_profile_stamp_clock_gettime (void)
1.92 +{
1.93 + struct timespec ts;
1.94 + clock_gettime (CLOCK_MONOTONIC, &ts);
1.95 + return 1000000000*ts.tv_sec + ts.tv_nsec;
1.96 +}
1.97 +#endif
1.98 +
1.99 +static unsigned long
1.100 +oil_profile_stamp_zero (void)
1.101 +{
1.102 + return 0;
1.103 +}
1.104 +
1.105 +#ifdef __SYMBIAN32__
1.106 +
1.107 +#endif
1.108 +void
1.109 +_oil_cpu_init (void)
1.110 +{
1.111 + const char *envvar;
1.112 +
1.113 + oil_cpu_detect_arch();
1.114 +
1.115 + envvar = getenv ("OIL_CPU_FLAGS");
1.116 + if (envvar != NULL) {
1.117 + char *end = NULL;
1.118 + unsigned long flags;
1.119 +
1.120 + flags = strtoul (envvar, &end, 0);
1.121 + if (end > envvar) {
1.122 + oil_cpu_flags = flags;
1.123 + }
1.124 + OIL_INFO ("cpu flags from environment %08lx", oil_cpu_flags);
1.125 + }
1.126 +
1.127 + OIL_INFO ("cpu flags %08lx", oil_cpu_flags);
1.128 +
1.129 +#if defined(HAVE_CLOCK_GETTIME) && defined(HAVE_MONOTONIC_CLOCK)
1.130 + if (_oil_profile_stamp == NULL) {
1.131 + _oil_profile_stamp = oil_profile_stamp_clock_gettime;
1.132 + OIL_INFO("Using clock_gettime() as a timestamp function.");
1.133 + }
1.134 +#endif
1.135 +
1.136 +#ifdef HAVE_GETTIMEOFDAY
1.137 + if (_oil_profile_stamp == NULL) {
1.138 + _oil_profile_stamp = oil_profile_stamp_gtod;
1.139 + OIL_WARNING("Using gettimeofday() as a timestamp function.");
1.140 + }
1.141 +#endif
1.142 + if (_oil_profile_stamp == NULL) {
1.143 + _oil_profile_stamp = oil_profile_stamp_zero;
1.144 + OIL_ERROR("No timestamping function. This is kinda bad.");
1.145 + }
1.146 +}
1.147 +
1.148 +/**
1.149 + * oil_cpu_get_flags:
1.150 + *
1.151 + * Returns a bitmask containing the available CPU features.
1.152 + *
1.153 + * Returns: the CPU features.
1.154 + */
1.155 +#ifdef __SYMBIAN32__
1.156 +EXPORT_C
1.157 +#endif
1.158 +unsigned int
1.159 +oil_cpu_get_flags (void)
1.160 +{
1.161 + return oil_cpu_flags;
1.162 +}
1.163 +
1.164 +
1.165 +#if 0
1.166 +/**
1.167 + * oil_cpu_get_ticks_per_second:
1.168 + *
1.169 + * Returns the estimated number of ticks per second. This feature
1.170 + * is currently unimplemented.
1.171 + *
1.172 + * This function may take several milliseconds or more to execute
1.173 + * in order to calculate a good estimate of the number of ticks (as
1.174 + * measured by the profiling functions) per second. Note that the
1.175 + * number of ticks per second is often dependent on the CPU frequency,
1.176 + * which can change dynamically. Thus the value returned by this
1.177 + * function may be incorrect as soon as it is returned.
1.178 + *
1.179 + * Returns: a double
1.180 + */
1.181 +double
1.182 +oil_cpu_get_ticks_per_second (void)
1.183 +{
1.184 + return _oil_ticks_per_second;
1.185 +}
1.186 +#endif
1.187 +
1.188 +#ifdef __SYMBIAN32__
1.189 +EXPORT_C
1.190 +#endif
1.191 +double
1.192 +oil_cpu_get_frequency (void)
1.193 +{
1.194 +#if defined(__linux__)
1.195 + int freq;
1.196 + if (get_file_int ("/sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq",
1.197 + &freq)) {
1.198 + return 1000.0 * freq;
1.199 + }
1.200 + return 0;
1.201 +#else
1.202 + return 0;
1.203 +#endif
1.204 +}
1.205 +