File: HardwareDetector.cpp

package info (click to toggle)
opencv 2.4.9.1%2Bdfsg-1%2Bdeb8u1
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 126,800 kB
  • ctags: 62,729
  • sloc: xml: 509,055; cpp: 490,794; lisp: 23,208; python: 21,174; java: 19,317; ansic: 1,038; sh: 128; makefile: 72
file content (182 lines) | stat: -rw-r--r-- 4,602 bytes parent folder | download | duplicates (3)
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
#include "HardwareDetector.h"
#include "TegraDetector.h"
#include "ProcReader.h"
#include "EngineCommon.h"
#include "StringUtils.h"
#include <utils/Log.h>

using namespace std;

int GetCpuID()
{
    int result = 0;
    map<string, string> cpu_info = GetCpuInfo();
    map<string, string>::const_iterator it;

#if defined(__i386__)
    LOGD("Using X86 HW detector");
    result |= ARCH_X86;
    it = cpu_info.find("flags");
    if (cpu_info.end() != it)
    {
        set<string> features = SplitString(it->second, ' ');
        if (features.end() != features.find(CPU_INFO_SSE_STR))
        {
            result |= FEATURES_HAS_SSE;
        }
        if (features.end() != features.find(CPU_INFO_SSE2_STR))
        {
            result |= FEATURES_HAS_SSE2;
        }
        if (features.end() != features.find(CPU_INFO_SSSE3_STR))
        {
            result |= FEATURES_HAS_SSSE3;
        }
    }
#elif defined(__mips)
#ifdef __SUPPORT_MIPS
    result |= ARCH_MIPS;
#else
    result = ARCH_UNKNOWN;
#endif
#else
    LOGD("Using ARM HW detector");
    it = cpu_info.find("Processor");

    if (cpu_info.end() != it)
    {
        size_t proc_name_pos = it->second.find(CPU_INFO_ARCH_X86_STR);
        if (string::npos != proc_name_pos)
        {
        }
        else
        {
            proc_name_pos = it->second.find(CPU_INFO_ARCH_ARMV7_STR);
            if (string::npos != proc_name_pos)
            {
                result |= ARCH_ARMv7;
            }
            else
            {
                proc_name_pos = it->second.find(CPU_INFO_ARCH_ARMV6_STR);
                if (string::npos != proc_name_pos)
                {
                    result |= ARCH_ARMv6;
                }
                else
                {
                    proc_name_pos = it->second.find(CPU_INFO_ARCH_ARMV5_STR);
                    if (string::npos != proc_name_pos)
                    {
                        result |= ARCH_ARMv5;
                    }
                }
            }
        }
    }
    else
    {
        return ARCH_UNKNOWN;
    }

    it = cpu_info.find("Features");
    if (cpu_info.end() != it)
    {
        set<string> features = SplitString(it->second, ' ');
        if (features.end() != features.find(CPU_INFO_NEON_STR))
        {
            result |= FEATURES_HAS_NEON;
        }
        if (features.end() != features.find(CPU_INFO_NEON2_STR))
        {
            result |= FEATURES_HAS_NEON2;
        }
        if (features.end() != features.find(CPU_INFO_VFPV4_STR))
        {
            result |= FEATURES_HAS_VFPv4;
        }
        if (features.end() != features.find(CPU_INFO_VFPV3_STR))
        {
            if (features.end () != features.find(CPU_INFO_VFPV3D16_STR))
            {
                result |= FEATURES_HAS_VFPv3d16;
            }
            else
            {
                result |= FEATURES_HAS_VFPv3;
            }
        }
    }
    #endif

    return result;
}

string GetPlatformName()
{
    map<string, string> cpu_info = GetCpuInfo();
    string hardware_name = "";
    map<string, string>::const_iterator hw_iterator = cpu_info.find("Hardware");

    if (cpu_info.end() != hw_iterator)
    {
        hardware_name = hw_iterator->second;
    }

    return hardware_name;
}

int GetProcessorCount()
{
    FILE* cpuPossible = fopen("/sys/devices/system/cpu/possible", "r");
    if(!cpuPossible)
        return 1;

    char buf[2000]; //big enough for 1000 CPUs in worst possible configuration
    char* pbuf = fgets(buf, sizeof(buf), cpuPossible);
    fclose(cpuPossible);
    if(!pbuf)
        return 1;

    //parse string of form "0-1,3,5-7,10,13-15"
        int cpusAvailable = 0;

        while(*pbuf)
        {
            const char* pos = pbuf;
            bool range = false;
            while(*pbuf && *pbuf != ',')
            {
                if(*pbuf == '-') range = true;
                ++pbuf;
            }
            if(*pbuf) *pbuf++ = 0;
            if(!range)
                ++cpusAvailable;
            else
            {
                int rstart = 0, rend = 0;
                sscanf(pos, "%d-%d", &rstart, &rend);
                cpusAvailable += rend - rstart + 1;
            }
        }
        return cpusAvailable ? cpusAvailable : 1;
}

int DetectKnownPlatforms()
{
#if defined(__arm__) && defined(USE_TEGRA_HW_DETECTOR)
    int tegra_status = DetectTegra();
#else
    int tegra_status = NOT_TEGRA;
#endif
    // All Tegra platforms since Tegra3
    if (2 < tegra_status)
    {
        return PLATFORM_TEGRA + tegra_status - 1;
    }
    else
    {
        return PLATFORM_UNKNOWN;
    }
}