cpuinfo.c 5.78 KB
Newer Older
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/*
 * Copyright © 2013 Siarhei Siamashka <siarhei.siamashka@gmail.com>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 */

#include <string.h>
#include <stdlib.h>
#include <stdio.h>

#include "cpuinfo.h"

#if defined(__linux__) || defined(ANDROID) || defined(__ANDROID__)

#define MAXBUFSIZE 16384

static const char *cpuinfo_match_prefix(const char *s, const char *prefix)
{
    const char *result;
    if (strncmp(s, prefix, strlen(prefix)) != 0)
        return NULL;
    result = strchr(s, ':');
    if (!result)
        return NULL;
    result++;
    while (*result && (*result == ' ' || *result == '\t'))
        result++;
    return result;
}

static int is_space_or_eol(char x)
{
    return (x == 0 || x == ' ' || x == '\t' || x == '\n');
}

static int find_feature(const char *buffer, const char *feature)
{
    const char *s = buffer;
    while (*s) {
        char *t = strstr(s, feature);
        if (!t)
            return 0;
        if (!is_space_or_eol(*(t + strlen(feature)))) {
            s++;
            continue;
        }
        if (t == buffer)
            return 1;
        if (t > buffer && is_space_or_eol(*(t - 1)))
            return 1;
        s++;
    }
    return 0;
}

static int parse_proc_cpuinfo(cpuinfo_t *cpuinfo)
{
    char *buffer = (char *)malloc(MAXBUFSIZE);
    FILE *fd;
    const char *val;

    if (!buffer)
        return 0;

    fd = fopen("/proc/cpuinfo", "r");
    if (!fd) {
        free(buffer);
        return 0;
    }

    while (fgets(buffer, MAXBUFSIZE, fd)) {
        if (!strchr(buffer, '\n') && !feof(fd)) {
            fclose(fd);
            free(buffer);
            return 0;
        }
        if ((val = cpuinfo_match_prefix(buffer, "Features"))) {
            cpuinfo->has_arm_edsp = find_feature(val, "edsp");
            cpuinfo->has_arm_vfp  = find_feature(val, "vfp");
            cpuinfo->has_arm_neon = find_feature(val, "neon");
        }
        else if ((val = cpuinfo_match_prefix(buffer, "CPU implementer"))) {
            if (sscanf(val, "%i", &cpuinfo->arm_implementer) != 1) {
                fclose(fd);
                free(buffer);
                return 0;
            }
        }
        else if ((val = cpuinfo_match_prefix(buffer, "CPU architecture"))) {
            if (sscanf(val, "%i", &cpuinfo->arm_architecture) != 1) {
                fclose(fd);
                free(buffer);
                return 0;
            }
        }
        else if ((val = cpuinfo_match_prefix(buffer, "CPU variant"))) {
            if (sscanf(val, "%i", &cpuinfo->arm_variant) != 1) {
                fclose(fd);
                free(buffer);
                return 0;
            }
        }
        else if ((val = cpuinfo_match_prefix(buffer, "CPU part"))) {
            if (sscanf(val, "%i", &cpuinfo->arm_part) != 1) {
                fclose(fd);
                free(buffer);
                return 0;
            }
        }
        else if ((val = cpuinfo_match_prefix(buffer, "CPU revision"))) {
            if (sscanf(val, "%x", &cpuinfo->arm_revision) != 1) {
                fclose(fd);
                free(buffer);
                return 0;
            }
        }
    }
    fclose(fd);
    free(buffer);
    return 1;
}

#else

static int parse_proc_cpuinfo(cpuinfo_t *cpuinfo)
{
    return 0;
}

#endif

cpuinfo_t *cpuinfo_init()
{
    cpuinfo_t *cpuinfo = calloc(sizeof(cpuinfo_t), 1);
    if (!cpuinfo)
        return NULL;

    if (!parse_proc_cpuinfo(cpuinfo)) {
        cpuinfo->processor_name = strdup("Unknown");
        return cpuinfo;
    }

    if (cpuinfo->arm_implementer == 0x41 && cpuinfo->arm_part == 0xC0F) {
        cpuinfo->processor_name = strdup("ARM Cortex-A15");
    } else if (cpuinfo->arm_implementer == 0x41 && cpuinfo->arm_part == 0xC09) {
        if (cpuinfo->has_arm_neon)
            cpuinfo->processor_name = strdup("ARM Cortex-A9");
        else
            cpuinfo->processor_name = strdup("ARM Cortex-A9 without NEON (Tegra2?)");
    } else if (cpuinfo->arm_implementer == 0x41 && cpuinfo->arm_part == 0xC08) {
        if (cpuinfo->arm_variant > 1)
            cpuinfo->processor_name = strdup("Late ARM Cortex-A8 (NEON can bypass L1 cache)");
        else
            cpuinfo->processor_name = strdup("Early ARM Cortex-A8");
    } else if (cpuinfo->arm_implementer == 0x41 && cpuinfo->arm_part == 0xC07) {
        cpuinfo->processor_name = strdup("ARM Cortex-A7");
    } else if (cpuinfo->arm_implementer == 0x41 && cpuinfo->arm_part == 0xC05) {
        cpuinfo->processor_name = strdup("ARM Cortex-A5");
    } else if (cpuinfo->arm_implementer == 0x41 && cpuinfo->arm_part == 0xB76) {
        cpuinfo->processor_name = strdup("ARM1176");
    } else {
        cpuinfo->processor_name = strdup("Unknown");
    }

    return cpuinfo;
}

void cpuinfo_close(cpuinfo_t *cpuinfo)
{
    free(cpuinfo->processor_name);
    free(cpuinfo);
}