aboutsummaryrefslogtreecommitdiff
path: root/platform/linux-generic/arch/x86/odp_sysinfo_parse.c
blob: c74c5204530f25c76f358105de45ee361695c346 (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
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
/* Copyright (c) 2016-2018, Linaro Limited
 * All rights reserved.
 *
 * SPDX-License-Identifier:     BSD-3-Clause
 */

#include <odp_sysinfo_internal.h>
#include "cpu_flags.h"
#include <string.h>

int _odp_cpuinfo_parser(FILE *file, system_info_t *sysinfo)
{
	char str[1024];
	char *pos, *pos_end;
	double ghz = 0.0;
	double mhz = 0.0;
	uint64_t hz;
	int id = 0;
	bool freq_set = false;

	sysinfo->cpu_arch = ODP_CPU_ARCH_X86;
	sysinfo->cpu_isa_sw.x86 = ODP_CPU_ARCH_X86_UNKNOWN;
	sysinfo->cpu_isa_hw.x86 = ODP_CPU_ARCH_X86_UNKNOWN;

	#if defined __x86_64 || defined __x86_64__
	sysinfo->cpu_isa_sw.x86 = ODP_CPU_ARCH_X86_64;
	#elif defined __i686 || defined __i686__
	sysinfo->cpu_isa_sw.x86 = ODP_CPU_ARCH_X86_I686;
	#endif

	strcpy(sysinfo->cpu_arch_str, "x86");
	while (fgets(str, sizeof(str), file) != NULL && id < CONFIG_NUM_CPU_IDS) {
		pos = strstr(str, "model name");
		if (pos) {
			freq_set = false;

			/* Copy model name between : and @ characters */
			pos     = strchr(str, ':');
			pos_end = strchr(str, '@');
			if (pos == NULL)
				continue;

			if (pos_end != NULL)
				*(pos_end - 1) = '\0';

			strncpy(sysinfo->model_str[id], pos + 2,
				MODEL_STR_SIZE - 1);

			if (sysinfo->cpu_hz_max[id]) {
				freq_set = true;
				id++;
				continue;
			}

			/* max frequency needs to be set */
			if (pos_end != NULL &&
			    sscanf(pos_end, "@ %lfGHz", &ghz) == 1) {
				hz = (uint64_t)(ghz * 1000000000.0);
				sysinfo->cpu_hz_max[id++] = hz;
				freq_set = true;
			}
		} else if (!freq_set &&
			   strstr(str, "bogomips") != NULL) {
			pos     = strchr(str, ':');
			if (pos == NULL)
				continue;

			if (sscanf(pos + 2, "%lf", &mhz) == 1) {
				/* On typical x86 BogoMIPS is freq * 2 */
				hz = (uint64_t)(mhz * 1000000.0 / 2);
				sysinfo->cpu_hz_max[id++] = hz;
				freq_set = true;
			}
		}
	}

	return 0;
}

void _odp_sys_info_print_arch(void)
{
	_odp_cpu_flags_print_all();
}

uint64_t odp_cpu_arch_hz_current(int id)
{
	char str[1024];
	FILE *file;
	int cpu;
	char *pos;
	double mhz = 0.0;

	file = fopen("/proc/cpuinfo", "rt");
	if (!file)
		return 0;

	/* find the correct processor instance */
	while (fgets(str, sizeof(str), file) != NULL) {
		pos = strstr(str, "processor");
		if (pos) {
			if (sscanf(pos, "processor : %d", &cpu) == 1)
				if (cpu == id)
					break;
		}
	}

	/* extract the cpu current speed */
	while (fgets(str, sizeof(str), file) != NULL) {
		pos = strstr(str, "cpu MHz");
		if (pos) {
			if (sscanf(pos, "cpu MHz : %lf", &mhz) == 1)
				break;
		}
	}

	fclose(file);
	if (mhz)
		return (uint64_t)(mhz * 1000000.0);

	return 0;
}