Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[GH #830] Implement platform Parrot_get_num_cpus
Parrot_get_num_cpus is the new default instead of MAX_THREADS. Initialized at startup
- Loading branch information
Reini Urban
committed
Sep 16, 2012
1 parent
5412296
commit fba6c79
Showing
9 changed files
with
228 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -47,6 +47,7 @@ sub _set_implementations { | |
encoding.c | ||
env.c | ||
cpu_type.c | ||
num_cpus.c | ||
dl.c | ||
math.c | ||
itimer.c | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
/* | ||
* Copyright (C) 2012, Parrot Foundation. | ||
*/ | ||
|
||
/* | ||
=head1 NAME | ||
src/platform/generic/num_cpus.c | ||
=head1 DESCRIPTION | ||
Returns the number of CPUs for non-win32 systems | ||
For win32, look in platform/win32/num_cpus.c | ||
=head2 Functions | ||
=cut | ||
*/ | ||
|
||
#include "parrot/parrot.h" | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <string.h> | ||
|
||
#ifdef PARROT_HAS_HEADER_UNISTD | ||
# include <unistd.h> | ||
#endif | ||
|
||
/* HEADERIZER HFILE: none */ | ||
|
||
/* | ||
=over 4 | ||
=item C<INTVAL Parrot_get_num_cpus(Parrot_Interp)> | ||
Returns the number of CPUs | ||
=back | ||
=cut | ||
*/ | ||
|
||
INTVAL | ||
Parrot_get_num_cpus(Parrot_Interp interp) { | ||
INTVAL nprocs = -1; | ||
|
||
#ifdef _SC_NPROCESSORS_ONLN | ||
nprocs = sysconf(_SC_NPROCESSORS_ONLN); | ||
#elif defined (PARROT_HAS_HEADER_LIBCPUID) | ||
struct cpu_raw_data_t raw; | ||
struct cpu_id_t data; | ||
|
||
if (!cpuid_present()) { | ||
printf("cpuid_present failed\n"); | ||
exit(EXIT_FAILURE); | ||
} | ||
if (cpuid_get_raw_data(&raw) < 0) { | ||
printf("cpuid_get_raw_data failed\n"); | ||
printf("Error: %s\n", cpuid_error()); | ||
exit(EXIT_FAILURE); | ||
} | ||
if (cpu_identify(&raw, &data) < 0) { | ||
printf("cpu_identify failed\n"); | ||
printf("Error: %s\n", cpuid_error()); | ||
exit(EXIT_FAILURE); | ||
} | ||
nprocs = data.num_cores; | ||
#else | ||
FILE *f; | ||
char line[128]; | ||
|
||
if (!fopen("/proc/cpuinfo", "rb")) | ||
exit(EXIT_FAILURE); | ||
|
||
while (!feof(f)) { | ||
fgets(line, 128, f); | ||
if (strlen(line) == 1) continue; | ||
if (strncmp(line, "cpu cores", 8) == 0) { | ||
sscanf(line, "cpu cores\t: %d", &nprocs); | ||
fclose (f); | ||
return nprocs; | ||
} | ||
} | ||
fclose (f); | ||
#endif | ||
return nprocs; | ||
} | ||
|
||
/* | ||
* Local variables: | ||
* c-file-style: "parrot" | ||
* End: | ||
* vim: expandtab shiftwidth=4 cinoptions='\:2=2' : | ||
*/ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/* | ||
* Copyright (C) 2012, Parrot Foundation. | ||
*/ | ||
|
||
/* | ||
=head1 NAME | ||
src/platform/win32/num_cpus.c | ||
=head1 DESCRIPTION | ||
Returns the number of CPUs for win32 systems | ||
=head2 Functions | ||
=cut | ||
*/ | ||
|
||
#include "parrot/parrot.h" | ||
|
||
#ifdef _WIN32 | ||
#define WIN32_LEAN_AND_MEAN | ||
#include <windows.h> | ||
#endif | ||
|
||
/* HEADERIZER HFILE: none */ | ||
|
||
/* | ||
=over 4 | ||
=item C<INTVAL Parrot_get_num_cpus(Parrot_Interp)> | ||
Returns the number of CPUs | ||
=back | ||
=cut | ||
*/ | ||
|
||
INTVAL | ||
Parrot_get_num_cpus(Parrot_Interp interp) | ||
{ | ||
INTVAL nprocs = -1; | ||
SYSTEM_INFO info; | ||
|
||
GetSystemInfo(&info); | ||
nprocs = (INTVAL)info.dwNumberOfProcessors; | ||
return nprocs; | ||
} | ||
|
||
/* | ||
* Local variables: | ||
* c-file-style: "parrot" | ||
* End: | ||
* vim: expandtab shiftwidth=4 cinoptions='\:2=2' : | ||
*/ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters