os/ossrv/genericopenlibs/liboil/src/liboilcpu.c
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
sl@0
     1
/*
sl@0
     2
 * LIBOIL - Library of Optimized Inner Loops
sl@0
     3
 * Copyright (c) 2003,2004 David A. Schleef <ds@schleef.org>
sl@0
     4
 * All rights reserved.
sl@0
     5
 *
sl@0
     6
 * Redistribution and use in source and binary forms, with or without
sl@0
     7
 * modification, are permitted provided that the following conditions
sl@0
     8
 * are met:
sl@0
     9
 * 1. Redistributions of source code must retain the above copyright
sl@0
    10
 *    notice, this list of conditions and the following disclaimer.
sl@0
    11
 * 2. Redistributions in binary form must reproduce the above copyright
sl@0
    12
 *    notice, this list of conditions and the following disclaimer in the
sl@0
    13
 *    documentation and/or other materials provided with the distribution.
sl@0
    14
 * 
sl@0
    15
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
sl@0
    16
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
sl@0
    17
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
sl@0
    18
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
sl@0
    19
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
sl@0
    20
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
sl@0
    21
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
sl@0
    22
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
sl@0
    23
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
sl@0
    24
 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
sl@0
    25
 * POSSIBILITY OF SUCH DAMAGE.
sl@0
    26
 */
sl@0
    27
//Portions Copyright (c)  2008-2009 Nokia Corporation and/or its subsidiary(-ies). All rights reserved. 
sl@0
    28
sl@0
    29
#ifdef HAVE_CONFIG_H
sl@0
    30
#include "config.h"
sl@0
    31
#endif
sl@0
    32
#include <liboil/liboilfunction.h>
sl@0
    33
#include <liboil/liboildebug.h>
sl@0
    34
#include <liboil/liboilcpu.h>
sl@0
    35
#include <liboil/liboilfault.h>
sl@0
    36
#include <liboil/liboilutils.h>
sl@0
    37
sl@0
    38
#ifdef HAVE_UNISTD_H
sl@0
    39
#include <unistd.h>
sl@0
    40
#endif
sl@0
    41
#include <fcntl.h>
sl@0
    42
#include <stdlib.h>
sl@0
    43
#include <string.h>
sl@0
    44
#include <stdio.h>
sl@0
    45
#include <setjmp.h>
sl@0
    46
#include <signal.h>
sl@0
    47
#ifdef HAVE_SYS_TIME_H
sl@0
    48
#include <sys/time.h>
sl@0
    49
#endif
sl@0
    50
#include <time.h>
sl@0
    51
sl@0
    52
#if defined(__FreeBSD__)
sl@0
    53
#include <sys/types.h>
sl@0
    54
#include <sys/sysctl.h>
sl@0
    55
#endif
sl@0
    56
sl@0
    57
#ifdef __sun
sl@0
    58
#include <sys/auxv.h>
sl@0
    59
#endif
sl@0
    60
sl@0
    61
sl@0
    62
sl@0
    63
/**
sl@0
    64
 * SECTION:liboilcpu
sl@0
    65
 * @title: CPU
sl@0
    66
 * @short_description: Check the capabilities of the current CPU
sl@0
    67
 *
sl@0
    68
 */
sl@0
    69
sl@0
    70
//void oil_cpu_detect_arch(void);
sl@0
    71
sl@0
    72
unsigned long oil_cpu_flags;
sl@0
    73
sl@0
    74
extern unsigned long (*_oil_profile_stamp)(void);
sl@0
    75
sl@0
    76
#ifdef HAVE_GETTIMEOFDAY
sl@0
    77
static unsigned long
sl@0
    78
oil_profile_stamp_gtod (void)
sl@0
    79
{
sl@0
    80
  struct timeval tv;
sl@0
    81
  gettimeofday(&tv,NULL);
sl@0
    82
  return 1000000*(unsigned long)tv.tv_sec + (unsigned long)tv.tv_usec;
sl@0
    83
}
sl@0
    84
#endif
sl@0
    85
sl@0
    86
#if defined(HAVE_CLOCK_GETTIME) && defined(HAVE_MONOTONIC_CLOCK)
sl@0
    87
static unsigned long
sl@0
    88
oil_profile_stamp_clock_gettime (void)
sl@0
    89
{
sl@0
    90
  struct timespec ts;
sl@0
    91
  clock_gettime (CLOCK_MONOTONIC, &ts);
sl@0
    92
  return 1000000000*ts.tv_sec + ts.tv_nsec;
sl@0
    93
}
sl@0
    94
#endif
sl@0
    95
sl@0
    96
static unsigned long
sl@0
    97
oil_profile_stamp_zero (void)
sl@0
    98
{
sl@0
    99
  return 0;
sl@0
   100
}
sl@0
   101
sl@0
   102
#ifdef __SYMBIAN32__
sl@0
   103
sl@0
   104
#endif
sl@0
   105
void
sl@0
   106
_oil_cpu_init (void)
sl@0
   107
{
sl@0
   108
  const char *envvar;
sl@0
   109
sl@0
   110
  oil_cpu_detect_arch();
sl@0
   111
sl@0
   112
  envvar = getenv ("OIL_CPU_FLAGS");
sl@0
   113
  if (envvar != NULL) {
sl@0
   114
    char *end = NULL;
sl@0
   115
    unsigned long flags;
sl@0
   116
sl@0
   117
    flags = strtoul (envvar, &end, 0);
sl@0
   118
    if (end > envvar) {
sl@0
   119
      oil_cpu_flags = flags;
sl@0
   120
    }
sl@0
   121
    OIL_INFO ("cpu flags from environment %08lx", oil_cpu_flags);
sl@0
   122
  }
sl@0
   123
sl@0
   124
  OIL_INFO ("cpu flags %08lx", oil_cpu_flags);
sl@0
   125
sl@0
   126
#if defined(HAVE_CLOCK_GETTIME) && defined(HAVE_MONOTONIC_CLOCK)
sl@0
   127
  if (_oil_profile_stamp == NULL) {
sl@0
   128
    _oil_profile_stamp = oil_profile_stamp_clock_gettime;
sl@0
   129
    OIL_INFO("Using clock_gettime() as a timestamp function.");
sl@0
   130
  }
sl@0
   131
#endif
sl@0
   132
sl@0
   133
#ifdef HAVE_GETTIMEOFDAY
sl@0
   134
  if (_oil_profile_stamp == NULL) {
sl@0
   135
    _oil_profile_stamp = oil_profile_stamp_gtod;
sl@0
   136
    OIL_WARNING("Using gettimeofday() as a timestamp function.");
sl@0
   137
  }
sl@0
   138
#endif
sl@0
   139
  if (_oil_profile_stamp == NULL) {
sl@0
   140
    _oil_profile_stamp = oil_profile_stamp_zero;
sl@0
   141
    OIL_ERROR("No timestamping function.  This is kinda bad.");
sl@0
   142
  }
sl@0
   143
}
sl@0
   144
sl@0
   145
/**
sl@0
   146
 * oil_cpu_get_flags:
sl@0
   147
 *
sl@0
   148
 * Returns a bitmask containing the available CPU features.
sl@0
   149
 *
sl@0
   150
 * Returns: the CPU features.
sl@0
   151
 */
sl@0
   152
#ifdef __SYMBIAN32__
sl@0
   153
EXPORT_C
sl@0
   154
#endif 
sl@0
   155
unsigned int
sl@0
   156
oil_cpu_get_flags (void)
sl@0
   157
{
sl@0
   158
  return oil_cpu_flags;
sl@0
   159
}
sl@0
   160
sl@0
   161
sl@0
   162
#if 0
sl@0
   163
/**
sl@0
   164
 * oil_cpu_get_ticks_per_second:
sl@0
   165
 *
sl@0
   166
 * Returns the estimated number of ticks per second.  This feature
sl@0
   167
 * is currently unimplemented.
sl@0
   168
 *
sl@0
   169
 * This function may take several milliseconds or more to execute
sl@0
   170
 * in order to calculate a good estimate of the number of ticks (as
sl@0
   171
 * measured by the profiling functions) per second.  Note that the
sl@0
   172
 * number of ticks per second is often dependent on the CPU frequency,
sl@0
   173
 * which can change dynamically.  Thus the value returned by this
sl@0
   174
 * function may be incorrect as soon as it is returned.
sl@0
   175
 *
sl@0
   176
 * Returns: a double
sl@0
   177
 */
sl@0
   178
double
sl@0
   179
oil_cpu_get_ticks_per_second (void)
sl@0
   180
{
sl@0
   181
  return _oil_ticks_per_second;
sl@0
   182
}
sl@0
   183
#endif
sl@0
   184
sl@0
   185
#ifdef __SYMBIAN32__
sl@0
   186
EXPORT_C
sl@0
   187
#endif
sl@0
   188
double
sl@0
   189
oil_cpu_get_frequency (void)
sl@0
   190
{
sl@0
   191
#if defined(__linux__)
sl@0
   192
  int freq;
sl@0
   193
  if (get_file_int ("/sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq",
sl@0
   194
        &freq)) {
sl@0
   195
    return 1000.0 * freq;
sl@0
   196
  }
sl@0
   197
  return 0;
sl@0
   198
#else
sl@0
   199
  return 0;
sl@0
   200
#endif
sl@0
   201
}
sl@0
   202