os/ossrv/genericopenlibs/liboil/src/liboilcpu.c
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
     1 /*
     2  * LIBOIL - Library of Optimized Inner Loops
     3  * Copyright (c) 2003,2004 David A. Schleef <ds@schleef.org>
     4  * All rights reserved.
     5  *
     6  * Redistribution and use in source and binary forms, with or without
     7  * modification, are permitted provided that the following conditions
     8  * are met:
     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.
    14  * 
    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.
    26  */
    27 //Portions Copyright (c)  2008-2009 Nokia Corporation and/or its subsidiary(-ies). All rights reserved. 
    28 
    29 #ifdef HAVE_CONFIG_H
    30 #include "config.h"
    31 #endif
    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>
    37 
    38 #ifdef HAVE_UNISTD_H
    39 #include <unistd.h>
    40 #endif
    41 #include <fcntl.h>
    42 #include <stdlib.h>
    43 #include <string.h>
    44 #include <stdio.h>
    45 #include <setjmp.h>
    46 #include <signal.h>
    47 #ifdef HAVE_SYS_TIME_H
    48 #include <sys/time.h>
    49 #endif
    50 #include <time.h>
    51 
    52 #if defined(__FreeBSD__)
    53 #include <sys/types.h>
    54 #include <sys/sysctl.h>
    55 #endif
    56 
    57 #ifdef __sun
    58 #include <sys/auxv.h>
    59 #endif
    60 
    61 
    62 
    63 /**
    64  * SECTION:liboilcpu
    65  * @title: CPU
    66  * @short_description: Check the capabilities of the current CPU
    67  *
    68  */
    69 
    70 //void oil_cpu_detect_arch(void);
    71 
    72 unsigned long oil_cpu_flags;
    73 
    74 extern unsigned long (*_oil_profile_stamp)(void);
    75 
    76 #ifdef HAVE_GETTIMEOFDAY
    77 static unsigned long
    78 oil_profile_stamp_gtod (void)
    79 {
    80   struct timeval tv;
    81   gettimeofday(&tv,NULL);
    82   return 1000000*(unsigned long)tv.tv_sec + (unsigned long)tv.tv_usec;
    83 }
    84 #endif
    85 
    86 #if defined(HAVE_CLOCK_GETTIME) && defined(HAVE_MONOTONIC_CLOCK)
    87 static unsigned long
    88 oil_profile_stamp_clock_gettime (void)
    89 {
    90   struct timespec ts;
    91   clock_gettime (CLOCK_MONOTONIC, &ts);
    92   return 1000000000*ts.tv_sec + ts.tv_nsec;
    93 }
    94 #endif
    95 
    96 static unsigned long
    97 oil_profile_stamp_zero (void)
    98 {
    99   return 0;
   100 }
   101 
   102 #ifdef __SYMBIAN32__
   103 
   104 #endif
   105 void
   106 _oil_cpu_init (void)
   107 {
   108   const char *envvar;
   109 
   110   oil_cpu_detect_arch();
   111 
   112   envvar = getenv ("OIL_CPU_FLAGS");
   113   if (envvar != NULL) {
   114     char *end = NULL;
   115     unsigned long flags;
   116 
   117     flags = strtoul (envvar, &end, 0);
   118     if (end > envvar) {
   119       oil_cpu_flags = flags;
   120     }
   121     OIL_INFO ("cpu flags from environment %08lx", oil_cpu_flags);
   122   }
   123 
   124   OIL_INFO ("cpu flags %08lx", oil_cpu_flags);
   125 
   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.");
   130   }
   131 #endif
   132 
   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.");
   137   }
   138 #endif
   139   if (_oil_profile_stamp == NULL) {
   140     _oil_profile_stamp = oil_profile_stamp_zero;
   141     OIL_ERROR("No timestamping function.  This is kinda bad.");
   142   }
   143 }
   144 
   145 /**
   146  * oil_cpu_get_flags:
   147  *
   148  * Returns a bitmask containing the available CPU features.
   149  *
   150  * Returns: the CPU features.
   151  */
   152 #ifdef __SYMBIAN32__
   153 EXPORT_C
   154 #endif 
   155 unsigned int
   156 oil_cpu_get_flags (void)
   157 {
   158   return oil_cpu_flags;
   159 }
   160 
   161 
   162 #if 0
   163 /**
   164  * oil_cpu_get_ticks_per_second:
   165  *
   166  * Returns the estimated number of ticks per second.  This feature
   167  * is currently unimplemented.
   168  *
   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.
   175  *
   176  * Returns: a double
   177  */
   178 double
   179 oil_cpu_get_ticks_per_second (void)
   180 {
   181   return _oil_ticks_per_second;
   182 }
   183 #endif
   184 
   185 #ifdef __SYMBIAN32__
   186 EXPORT_C
   187 #endif
   188 double
   189 oil_cpu_get_frequency (void)
   190 {
   191 #if defined(__linux__)
   192   int freq;
   193   if (get_file_int ("/sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq",
   194         &freq)) {
   195     return 1000.0 * freq;
   196   }
   197   return 0;
   198 #else
   199   return 0;
   200 #endif
   201 }
   202