Changeset View
Changeset View
Standalone View
Standalone View
head/lang/pocl/files/patch-lib_CL_devices_cpuinfo.c
--- lib/CL/devices/cpuinfo.c.orig 2016-11-20 11:31:19.521203000 +0100 | --- lib/CL/devices/cpuinfo.c.orig 2017-04-05 14:15:40 UTC | ||||
+++ lib/CL/devices/cpuinfo.c 2016-11-20 11:29:24.502817000 +0100 | +++ lib/CL/devices/cpuinfo.c | ||||
@@ -31,9 +31,13 @@ | @@ -34,6 +34,12 @@ | ||||
# include "vccompat.hpp" | #include "config.h" | ||||
#endif | #include "cpuinfo.h" | ||||
+#ifdef HAVE_SYSCTL_H | |||||
+#include <sys/types.h> | +# include <sys/types.h> | ||||
+#include <sys/sysctl.h> | +# include <sys/sysctl.h> | ||||
+#endif | |||||
+ | + | ||||
#include "config.h" | +#ifdef __linux__ | ||||
#include "cpuinfo.h" | static const char* cpuinfo = "/proc/cpuinfo"; | ||||
+#if 0 | |||||
const char* cpuinfo = "/proc/cpuinfo"; | |||||
#define MAX_CPUINFO_SIZE 64*1024 | #define MAX_CPUINFO_SIZE 64*1024 | ||||
//#define DEBUG_POCL_CPUINFO | //#define DEBUG_POCL_CPUINFO | ||||
@@ -152,8 +156,29 @@ pocl_cpuinfo_detect_max_clock_frequency( | @@ -153,8 +159,51 @@ pocl_cpuinfo_detect_max_clock_frequency( | ||||
} | } | ||||
return -1; | return -1; | ||||
} | } | ||||
+#endif | +#elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__) | ||||
+ | |||||
+/** | +/** | ||||
+ * Detects the number of parallel hardware threads supported by | + * Detects the maximum clock frequency of the CPU. | ||||
+ * the CPU. | |||||
+ * | + * | ||||
+ * @return The number of hardware threads. | + * Assumes all cores have the same max clock freq. | ||||
+ * | |||||
+ * @return The clock frequency in MHz. | |||||
+ */ | + */ | ||||
+ int | +int | ||||
+pocl_cpuinfo_detect_compute_unit_count() | +pocl_cpuinfo_detect_max_clock_frequency() | ||||
+{ | +{ | ||||
+ int mib[2], nocpus; | + const char mib1[] = "dev.cpu.0.freq_levels"; | ||||
+ size_t len; | + const char mib2[] = "hw.clockrate"; | ||||
+ int clockrate = 0; | |||||
+ size_t size = 0; | |||||
+ char *value = NULL; | |||||
+ mib[0] = CTL_HW; | + if (!sysctlbyname(mib1, NULL, &size, NULL, 0) && | ||||
+ mib[1] = HW_NCPU; | + (value = (char*)malloc(++size)) && | ||||
+ len = sizeof(nocpus); | + !sysctlbyname(mib1, (void*)value, &size, NULL, 0)) | ||||
+ sysctl(mib, 2, &nocpus, &len, NULL, 0); | + { | ||||
+ value[size] = '\0'; | |||||
+ return nocpus; | + sscanf(value, "%d/%*d", &clockrate); | ||||
+} | + } | ||||
+ | + else | ||||
+#if 0 | + { | ||||
+ size = sizeof(clockrate); | |||||
+ sysctlbyname(mib2, (void*)&clockrate, &size, NULL, 0); | |||||
+ } | |||||
+ if (value) | |||||
+ free(value); | |||||
+ return clockrate; | |||||
+} | |||||
+#else | |||||
+/** | |||||
+ * Unimplemented for other platforms. | |||||
+ */ | |||||
+ int | |||||
+pocl_cpuinfo_detect_max_clock_frequency() | |||||
+{ | |||||
+ return 0; | |||||
+} | |||||
+#endif | |||||
+#ifdef __linux__ | |||||
/** | /** | ||||
* Detects the number of parallel hardware threads supported by | * Detects the number of parallel hardware threads supported by | ||||
* the CPU by parsing the cpuinfo. | * the CPU by parsing the cpuinfo. | ||||
@@ -231,6 +256,27 @@ pocl_cpuinfo_detect_compute_unit_count() | @@ -232,6 +281,19 @@ pocl_cpuinfo_detect_compute_unit_count() | ||||
} | } | ||||
return -1; | return -1; | ||||
} | } | ||||
+#endif | +#else | ||||
+ | |||||
+/** | +/** | ||||
+ * Detects the maximum clock frequency of the CPU. | + * Detects the number of parallel hardware threads supported by | ||||
+ * the CPU. | |||||
+ * | + * | ||||
+ * Assumes all cores have the same max clock freq. | + * @return The number of hardware threads. | ||||
+ * | |||||
+ * @return The clock frequency in MHz. | |||||
+ */ | + */ | ||||
+int | + int | ||||
+pocl_cpuinfo_detect_max_clock_frequency() | +pocl_cpuinfo_detect_compute_unit_count() | ||||
+{ | +{ | ||||
+ //XXX PLEASE NOTE, THIS IS NOT TOO PORTABLE (AND/OR ACCURATE)! | + return sysconf(_SC_NPROCESSORS_ONLN); | ||||
+ const char mib[] = "hw.clockrate"; | |||||
+ size_t size = sizeof(int); | |||||
+ int clockrate; | |||||
+ | |||||
+ sysctlbyname(mib, (void *)&clockrate, &size, NULL, 0); | |||||
+ | |||||
+ return clockrate; | |||||
+ } | +} | ||||
+#endif | |||||
#ifdef POCL_ANDROID | #ifdef POCL_ANDROID | ||||
@@ -269,6 +315,7 @@ pocl_cpuinfo_get_cpu_name_and_vendor(cl_ | @@ -270,6 +332,7 @@ pocl_cpuinfo_get_cpu_name_and_vendor(cl_ | ||||
* short_name is in the .data anyways.*/ | * short_name is in the .data anyways.*/ | ||||
device->long_name = device->short_name; | device->long_name = device->short_name; | ||||
+#if 0 | +#ifdef __linux__ | ||||
/* default vendor and vendor_id, in case it cannot be found by other means */ | /* default vendor and vendor_id, in case it cannot be found by other means */ | ||||
device->vendor = cpuvendor_default; | device->vendor = cpuvendor_default; | ||||
if (device->vendor_id == 0) | if (device->vendor_id == 0) | ||||
@@ -317,6 +364,7 @@ pocl_cpuinfo_get_cpu_name_and_vendor(cl_ | @@ -318,7 +381,25 @@ pocl_cpuinfo_get_cpu_name_and_vendor(cl_ | ||||
char *new_name = (char*)malloc (len); | char *new_name = (char*)malloc (len); | ||||
snprintf (new_name, len, "%s-%s", device->short_name, start); | snprintf (new_name, len, "%s-%s", device->short_name, start); | ||||
device->long_name = new_name; | device->long_name = new_name; | ||||
+#endif | +#elif defined(HAVE_SYSCTL_H) | ||||
+ int mib[2]; | |||||
+ size_t len = 0; | |||||
+ char *model; | |||||
+ mib[0] = CTL_HW; | |||||
+ mib[1] = HW_MODEL; | |||||
+ if (sysctl(mib, 2, NULL, &len, NULL, 0)) | |||||
+ return; | |||||
+ if (!(model = (char*)malloc(++len))) | |||||
+ return; | |||||
+ if (sysctl(mib, 2, (void*)model, &len, NULL, 0)) | |||||
+ free(model); | |||||
+ else | |||||
+ { | |||||
+ model[len] = '\0'; | |||||
+ device->long_name = model; | |||||
+ } | |||||
+#endif | |||||
} | } | ||||
void |