blob: b5361d0a4c7b0544c41feaae581905abeed64e31 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
|
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>
#include "dietfeatures.h"
/*
* by Olaf Dreesen
*
* arm NO SMP ?!? (return 1)
*
* alpha -> cpus detected\t\t: <nr>\n
* sparc -> ncpus active\t: <nr>\n
*
* default -> processor\t: <cpunr>\n (one per cpu)
*/
#ifdef SLASH_PROC_OK
int __sc_nr_cpus(void);
int __sc_nr_cpus() {
#if defined(__arm__)
return 1;
#else
int fd;
fd = open("/proc/cpuinfo", O_RDONLY);
if (fd==-1) return 1; /* fallback if no proc-fs mounted */
else {
int n,nr=0;
char buf[2048]; /* holds ~6 cpuinfos */
while((n=read(fd,buf,sizeof(buf)))>0) {
register int i=0;
while (i<n) {
#if defined(__alpha__)
if ((buf[i]=='c')&&(!memcmp(buf+i,"cpus detected",13))) {
i+=17;
nr=atoi(buf+i);
break;
}
#elif defined(__sparc__)
if ((buf[i]=='n')&&(!memcmp(buf+i,"ncpus active",12))) {
i+=15;
nr=atoi(buf+i);
break;
}
#else /* DEFAULT */
if ((buf[i]=='p')&&(!memcmp(buf+i,"processor",9))) {
++nr;
i+=9;
}
#endif
while(buf[i++]!='\n'); /* skip rest of line */
}
}
close(fd);
return nr;
}
#endif
}
#else
int __sc_nr_cpus() {
return 1; /* kludge kludge ;-) */
}
#endif
|