2009-01-26 08:27:00 -08:00
|
|
|
/* EINA - EFL data type library
|
|
|
|
* Copyright (C) 2007-2008 Jorge Luis Zapata Muga
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library;
|
|
|
|
* if not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2009-01-27 08:44:45 -08:00
|
|
|
|
2009-01-26 11:35:12 -08:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
# include "config.h"
|
|
|
|
#endif
|
|
|
|
|
2010-07-13 06:51:00 -07:00
|
|
|
#ifdef EFL_HAVE_THREADS
|
2009-08-11 07:11:36 -07:00
|
|
|
# ifdef _WIN32
|
|
|
|
# define WIN32_LEAN_AND_MEAN
|
|
|
|
# include <windows.h>
|
2012-11-06 16:48:10 -08:00
|
|
|
# elif defined (__sun) || defined(__GNU__) || defined(__CYGWIN__)
|
2009-08-11 07:11:36 -07:00
|
|
|
# include <unistd.h>
|
2010-07-27 19:37:05 -07:00
|
|
|
# elif defined (__FreeBSD__) || defined (__OpenBSD__) || \
|
|
|
|
defined (__NetBSD__) || defined (__DragonFly__) || defined (__MacOSX__) || \
|
|
|
|
(defined (__MACH__) && defined (__APPLE__))
|
2009-08-11 07:11:36 -07:00
|
|
|
# include <unistd.h>
|
2009-11-07 11:44:39 -08:00
|
|
|
# include <sys/param.h>
|
2009-08-29 09:39:59 -07:00
|
|
|
# include <sys/sysctl.h>
|
2009-12-20 02:02:41 -08:00
|
|
|
# elif defined (__linux__) || defined(__GLIBC__)
|
2009-08-11 07:11:36 -07:00
|
|
|
# include <sched.h>
|
|
|
|
# endif
|
2015-02-11 03:15:43 -08:00
|
|
|
# include <pthread.h>
|
2009-07-30 09:45:15 -07:00
|
|
|
|
2015-05-15 03:07:42 -07:00
|
|
|
# define TH_MAX 32
|
2009-07-30 09:45:15 -07:00
|
|
|
#endif
|
|
|
|
|
2012-04-20 00:19:08 -07:00
|
|
|
#include <stdlib.h>
|
2009-08-11 07:11:36 -07:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
2013-11-10 00:25:16 -08:00
|
|
|
#include <unistd.h>
|
2009-08-11 07:11:36 -07:00
|
|
|
|
2013-11-10 00:25:16 -08:00
|
|
|
#include "eina_log.h"
|
2009-01-26 08:27:00 -08:00
|
|
|
#include "eina_cpu.h"
|
2009-01-27 08:44:45 -08:00
|
|
|
|
2018-11-22 08:59:47 -08:00
|
|
|
#if defined(HAVE_SYS_AUXV_H) && defined(HAVE_ASM_HWCAP_H) && (defined(__arm__) || defined(__aarch64__)) && defined(__linux__)
|
2016-10-04 12:29:36 -07:00
|
|
|
# include <sys/auxv.h>
|
|
|
|
# include <asm/hwcap.h>
|
|
|
|
#endif
|
|
|
|
|
2009-01-26 08:27:00 -08:00
|
|
|
/*============================================================================*
|
2010-07-27 19:37:05 -07:00
|
|
|
* Local *
|
|
|
|
*============================================================================*/
|
2009-01-27 08:44:45 -08:00
|
|
|
|
2013-11-10 00:25:16 -08:00
|
|
|
static void _eina_page_size(void);
|
|
|
|
|
2009-01-26 08:27:00 -08:00
|
|
|
/* FIXME this ifdefs should be replaced */
|
|
|
|
#if defined(__i386__) || defined(__x86_64__)
|
|
|
|
/* We save ebx and restore it to be PIC compatible */
|
|
|
|
static inline void _x86_cpuid(int op, int *a, int *b, int *c, int *d)
|
|
|
|
{
|
2012-05-14 22:48:27 -07:00
|
|
|
__asm__ volatile (
|
2009-01-26 11:35:12 -08:00
|
|
|
#if defined(__x86_64__)
|
2010-07-27 19:37:05 -07:00
|
|
|
"pushq %%rbx \n\t" /* save %ebx */
|
2009-01-26 11:35:12 -08:00
|
|
|
#else
|
2010-07-27 19:37:05 -07:00
|
|
|
"pushl %%ebx \n\t" /* save %ebx */
|
2009-01-26 11:35:12 -08:00
|
|
|
#endif
|
2010-07-27 19:37:05 -07:00
|
|
|
"cpuid \n\t"
|
|
|
|
"movl %%ebx, %1 \n\t" /* save what cpuid just put in %ebx */
|
2009-01-26 11:35:12 -08:00
|
|
|
#if defined(__x86_64__)
|
2010-07-27 19:37:05 -07:00
|
|
|
"popq %%rbx \n\t" /* restore the old %ebx */
|
2009-01-26 11:35:12 -08:00
|
|
|
#else
|
2010-07-27 19:37:05 -07:00
|
|
|
"popl %%ebx \n\t" /* restore the old %ebx */
|
2009-01-26 11:35:12 -08:00
|
|
|
#endif
|
2010-07-27 19:37:05 -07:00
|
|
|
: "=a" (*a), "=r" (*b), "=c" (*c), "=d" (*d)
|
|
|
|
: "a" (op)
|
|
|
|
: "cc");
|
2009-01-26 08:27:00 -08:00
|
|
|
}
|
|
|
|
|
2010-01-23 11:53:27 -08:00
|
|
|
static
|
2009-01-26 08:27:00 -08:00
|
|
|
void _x86_simd(Eina_Cpu_Features *features)
|
|
|
|
{
|
2010-07-27 19:37:05 -07:00
|
|
|
int a, b, c, d;
|
|
|
|
|
|
|
|
_x86_cpuid(1, &a, &b, &c, &d);
|
|
|
|
/*
|
|
|
|
* edx
|
|
|
|
* 18 = PN (Processor Number)
|
|
|
|
* 19 = CLFlush (Cache Line Flush)
|
|
|
|
* 23 = MMX
|
|
|
|
* 25 = SSE
|
|
|
|
* 26 = SSE2
|
|
|
|
* 28 = HTT (Hyper Threading)
|
|
|
|
* ecx
|
|
|
|
* 0 = SSE3
|
2013-10-02 02:31:10 -07:00
|
|
|
* 9 = SSSE3
|
|
|
|
* 19 = SSE4.1
|
|
|
|
* 20 = SSE4.2
|
2010-07-27 19:37:05 -07:00
|
|
|
*/
|
|
|
|
if ((d >> 23) & 1)
|
|
|
|
*features |= EINA_CPU_MMX;
|
|
|
|
|
|
|
|
if ((d >> 25) & 1)
|
|
|
|
*features |= EINA_CPU_SSE;
|
|
|
|
|
|
|
|
if ((d >> 26) & 1)
|
|
|
|
*features |= EINA_CPU_SSE2;
|
|
|
|
|
|
|
|
if (c & 1)
|
|
|
|
*features |= EINA_CPU_SSE3;
|
2013-10-02 02:31:10 -07:00
|
|
|
|
|
|
|
if ((c >> 9) & 1)
|
|
|
|
*features |= EINA_CPU_SSSE3;
|
|
|
|
|
|
|
|
if ((c >> 19) & 1)
|
|
|
|
*features |= EINA_CPU_SSE41;
|
|
|
|
|
|
|
|
if ((c >> 20) & 1)
|
|
|
|
*features |= EINA_CPU_SSE42;
|
2009-01-26 08:27:00 -08:00
|
|
|
}
|
|
|
|
#endif
|
2009-01-27 08:44:45 -08:00
|
|
|
|
2018-11-22 08:59:47 -08:00
|
|
|
#if defined(HAVE_SYS_AUXV_H) && defined(HAVE_ASM_HWCAP_H) && (defined(__arm__) || defined(__aarch64__)) && defined(__linux__)
|
2016-09-16 11:05:56 -07:00
|
|
|
static void
|
|
|
|
_arm_cpu_features(Eina_Cpu_Features *features)
|
|
|
|
{
|
|
|
|
unsigned long aux;
|
|
|
|
|
|
|
|
aux = getauxval(AT_HWCAP);
|
|
|
|
|
2018-11-22 08:59:47 -08:00
|
|
|
# if defined(__aarch64__)
|
|
|
|
*features |= EINA_CPU_NEON;
|
|
|
|
# endif
|
|
|
|
# ifdef HWCAP_NEON
|
|
|
|
if (aux & HWCAP_NEON) *features |= EINA_CPU_NEON;
|
|
|
|
# endif
|
|
|
|
# ifdef HWCAP_SVE
|
|
|
|
if (aux & HWCAP_SVE) *features |= EINA_CPU_SVE;
|
|
|
|
# endif
|
2016-09-16 11:05:56 -07:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2009-01-26 08:27:00 -08:00
|
|
|
/*============================================================================*
|
2010-07-27 19:37:05 -07:00
|
|
|
* Global *
|
|
|
|
*============================================================================*/
|
2009-01-27 08:44:45 -08:00
|
|
|
|
2009-01-26 08:27:00 -08:00
|
|
|
/*============================================================================*
|
2010-07-27 19:37:05 -07:00
|
|
|
* API *
|
|
|
|
*============================================================================*/
|
2009-01-27 08:44:45 -08:00
|
|
|
|
2009-01-26 08:27:00 -08:00
|
|
|
/* FIXME the features checks should be called when this function is called?
|
|
|
|
* or make it static by doing eina_cpu_init() and return a local var
|
|
|
|
*/
|
2013-10-02 02:31:10 -07:00
|
|
|
EAPI Eina_Cpu_Features eina_cpu_features = 0;
|
|
|
|
|
|
|
|
Eina_Bool
|
|
|
|
eina_cpu_init(void)
|
|
|
|
{
|
|
|
|
#if defined(__i386__) || defined(__x86_64__)
|
|
|
|
_x86_simd(&eina_cpu_features);
|
2018-11-22 08:59:47 -08:00
|
|
|
#elif defined(HAVE_SYS_AUXV_H) && defined(HAVE_ASM_HWCAP_H) && (defined(__arm__) || defined(__aarch64__)) && defined(__linux__)
|
2016-09-16 11:05:56 -07:00
|
|
|
_arm_cpu_features(&eina_cpu_features);
|
2013-10-02 02:31:10 -07:00
|
|
|
#endif
|
|
|
|
|
2013-11-10 00:25:16 -08:00
|
|
|
// Figure out the page size for this system
|
|
|
|
_eina_page_size();
|
|
|
|
|
2013-10-02 02:31:10 -07:00
|
|
|
return EINA_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
Eina_Bool
|
|
|
|
eina_cpu_shutdown(void)
|
|
|
|
{
|
|
|
|
return EINA_TRUE;
|
|
|
|
}
|
|
|
|
|
2009-01-26 08:27:00 -08:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
EAPI Eina_Cpu_Features eina_cpu_features_get(void)
|
|
|
|
{
|
2013-10-02 02:31:10 -07:00
|
|
|
return eina_cpu_features;
|
2009-01-26 08:27:00 -08:00
|
|
|
}
|
2009-07-30 09:45:15 -07:00
|
|
|
|
2012-04-19 02:25:54 -07:00
|
|
|
static int _cpu_count = -1;
|
|
|
|
|
|
|
|
static int
|
|
|
|
_eina_cpu_count_internal(void)
|
2009-07-30 09:45:15 -07:00
|
|
|
{
|
2010-07-13 06:51:00 -07:00
|
|
|
#ifdef EFL_HAVE_THREADS
|
2009-08-11 07:11:36 -07:00
|
|
|
|
|
|
|
# if defined (_WIN32)
|
|
|
|
SYSTEM_INFO sysinfo;
|
|
|
|
|
|
|
|
GetSystemInfo(&sysinfo);
|
|
|
|
return sysinfo.dwNumberOfProcessors;
|
|
|
|
|
2012-11-06 16:48:10 -08:00
|
|
|
# elif defined (__sun) || defined(__GNU__) || defined(__CYGWIN__)
|
2009-08-28 05:03:34 -07:00
|
|
|
/*
|
|
|
|
* _SC_NPROCESSORS_ONLN: number of processors that are online, that
|
|
|
|
is available when sysconf is called. The number
|
|
|
|
of cpu can change by admins.
|
|
|
|
* _SC_NPROCESSORS_CONF: maximum number of processors that are available
|
|
|
|
to the current OS instance. That number can be
|
|
|
|
change after a reboot.
|
|
|
|
* _SC_NPROCESSORS_MAX : maximum number of processors that are on the
|
|
|
|
motherboard.
|
|
|
|
*/
|
2009-08-11 07:11:36 -07:00
|
|
|
return sysconf(_SC_NPROCESSORS_ONLN);
|
|
|
|
|
2010-07-27 19:37:05 -07:00
|
|
|
# elif defined (__FreeBSD__) || defined (__OpenBSD__) || \
|
|
|
|
defined (__NetBSD__) || defined (__DragonFly__) || defined (__MacOSX__) || \
|
|
|
|
(defined (__MACH__) && defined (__APPLE__))
|
2009-11-09 01:07:10 -08:00
|
|
|
|
2010-07-27 19:37:05 -07:00
|
|
|
int mib[4];
|
|
|
|
int cpus;
|
2009-11-09 02:39:49 -08:00
|
|
|
size_t len = sizeof(cpus);
|
2009-08-11 07:11:36 -07:00
|
|
|
|
|
|
|
mib[0] = CTL_HW;
|
2011-01-06 10:26:40 -08:00
|
|
|
#ifdef HW_AVAILCPU
|
2009-08-29 12:59:26 -07:00
|
|
|
mib[1] = HW_AVAILCPU;
|
2011-01-06 10:26:40 -08:00
|
|
|
#else
|
2009-08-29 12:29:30 -07:00
|
|
|
mib[1] = HW_NCPU;
|
2011-01-06 10:26:40 -08:00
|
|
|
#endif
|
2009-08-11 07:11:36 -07:00
|
|
|
sysctl(mib, 2, &cpus, &len, NULL, 0);
|
|
|
|
if (cpus < 1)
|
2010-07-27 19:37:05 -07:00
|
|
|
cpus = 1;
|
2009-08-29 12:59:26 -07:00
|
|
|
|
2009-08-11 07:11:36 -07:00
|
|
|
return cpus;
|
|
|
|
|
2009-12-20 02:02:41 -08:00
|
|
|
# elif defined (__linux__) || defined(__GLIBC__)
|
2009-07-30 09:45:15 -07:00
|
|
|
cpu_set_t cpu;
|
|
|
|
int i;
|
|
|
|
static int cpus = 0;
|
|
|
|
|
2010-07-27 19:37:05 -07:00
|
|
|
if (cpus != 0)
|
|
|
|
return cpus;
|
2009-07-30 09:45:15 -07:00
|
|
|
|
|
|
|
CPU_ZERO(&cpu);
|
|
|
|
if (sched_getaffinity(0, sizeof(cpu), &cpu) != 0)
|
2009-08-11 07:11:36 -07:00
|
|
|
{
|
2010-07-27 19:37:05 -07:00
|
|
|
fprintf(stderr, "[Eina] could not get cpu affinity: %s\n",
|
|
|
|
strerror(errno));
|
|
|
|
return 1;
|
2009-08-11 07:11:36 -07:00
|
|
|
}
|
2010-07-27 19:37:05 -07:00
|
|
|
|
2009-07-30 09:45:15 -07:00
|
|
|
for (i = 0; i < TH_MAX; i++)
|
|
|
|
{
|
2010-07-27 19:37:05 -07:00
|
|
|
if (CPU_ISSET(i, &cpu))
|
2015-05-15 03:05:33 -07:00
|
|
|
cpus++;
|
2009-07-30 09:45:15 -07:00
|
|
|
}
|
|
|
|
return cpus;
|
2009-08-11 07:11:36 -07:00
|
|
|
|
|
|
|
# else
|
|
|
|
# error "eina_cpu_count() error: Platform not supported"
|
|
|
|
# endif
|
2009-07-30 09:45:15 -07:00
|
|
|
#else
|
|
|
|
return 1;
|
|
|
|
#endif
|
|
|
|
}
|
2012-04-19 02:25:54 -07:00
|
|
|
|
2013-11-10 00:25:16 -08:00
|
|
|
static int _page_size = 0;
|
|
|
|
|
|
|
|
static void
|
|
|
|
_eina_page_size(void)
|
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
|
|
|
SYSTEM_INFO si;
|
|
|
|
|
|
|
|
GetSystemInfo(&si);
|
|
|
|
|
|
|
|
_page_size = (int)si.dwPageSize;
|
|
|
|
#elif defined _SC_PAGESIZE
|
|
|
|
_page_size = (int)sysconf(_SC_PAGESIZE);
|
|
|
|
#elif defined _SC_PAGE_SIZE
|
|
|
|
_page_size = (int)sysconf(_SC_PAGE_SIZE);
|
|
|
|
#elif defined HAVE_GETPAGESIZE
|
|
|
|
_page_size = getpagesize();
|
|
|
|
#else
|
|
|
|
# warn "Falling back to a safe default page size (4K) !"
|
|
|
|
_page_size = 4096;
|
|
|
|
#endif
|
|
|
|
if (_page_size < 1)
|
|
|
|
{
|
|
|
|
EINA_LOG_ERR("system reported weird value for PAGESIZE, assuming 4096.");
|
|
|
|
_page_size = 4096;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
EAPI int eina_cpu_page_size(void)
|
|
|
|
{
|
|
|
|
if (_page_size == 0) _eina_page_size();
|
|
|
|
return _page_size;
|
|
|
|
}
|
|
|
|
|
2012-04-19 02:25:54 -07:00
|
|
|
EAPI int eina_cpu_count(void)
|
|
|
|
{
|
|
|
|
return _cpu_count;
|
|
|
|
}
|
|
|
|
|
|
|
|
void eina_cpu_count_internal(void)
|
|
|
|
{
|
|
|
|
if (getenv("EINA_CPU_FAKE"))
|
|
|
|
_cpu_count = atoi(getenv("EINA_CPU_FAKE"));
|
|
|
|
else
|
|
|
|
_cpu_count = _eina_cpu_count_internal();
|
|
|
|
}
|