Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openj9
Path: blob/master/runtime/gc_realtime/ProcessorInfo.cpp
5985 views
1
/*******************************************************************************
2
* Copyright (c) 1991, 2019 IBM Corp. and others
3
*
4
* This program and the accompanying materials are made available under
5
* the terms of the Eclipse Public License 2.0 which accompanies this
6
* distribution and is available at https://www.eclipse.org/legal/epl-2.0/
7
* or the Apache License, Version 2.0 which accompanies this distribution and
8
* is available at https://www.apache.org/licenses/LICENSE-2.0.
9
*
10
* This Source Code may also be made available under the following
11
* Secondary Licenses when the conditions for such availability set
12
* forth in the Eclipse Public License, v. 2.0 are satisfied: GNU
13
* General Public License, version 2 with the GNU Classpath
14
* Exception [1] and GNU General Public License, version 2 with the
15
* OpenJDK Assembly Exception [2].
16
*
17
* [1] https://www.gnu.org/software/classpath/license.html
18
* [2] http://openjdk.java.net/legal/assembly-exception.html
19
*
20
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 OR LicenseRef-GPL-2.0 WITH Assembly-exception
21
*******************************************************************************/
22
23
#include "omr.h"
24
#include "omrcfg.h"
25
26
#include <string.h>
27
28
#include "EnvironmentBase.hpp"
29
#include "ModronAssertions.h"
30
#include "ProcessorInfo.hpp"
31
32
/**
33
* Initialization.
34
*/
35
MM_ProcessorInfo*
36
MM_ProcessorInfo::newInstance(MM_EnvironmentBase *env)
37
{
38
MM_ProcessorInfo *processorInfo = (MM_ProcessorInfo *)env->getForge()->allocate(sizeof(MM_ProcessorInfo), MM_AllocationCategory::FIXED, OMR_GET_CALLSITE());
39
if (processorInfo) {
40
new(processorInfo) MM_ProcessorInfo();
41
if (!processorInfo->initialize(env)) {
42
processorInfo->kill(env);
43
processorInfo = NULL;
44
}
45
}
46
return processorInfo;
47
}
48
49
/**
50
* Initialization.
51
*/
52
bool
53
MM_ProcessorInfo::initialize(MM_EnvironmentBase *env)
54
{
55
#if defined(AIXPPC) || defined(WIN32)
56
/* on AIX and Windows we don't use tick based timer so no need to calculate CPU clock frequency */
57
return true;
58
#else
59
_freq = readFrequency();
60
return (_freq != 0);
61
#endif
62
}
63
64
/**
65
* Initialization.
66
*/
67
void
68
MM_ProcessorInfo::kill(MM_EnvironmentBase *env)
69
{
70
tearDown(env);
71
env->getForge()->free(this);
72
}
73
74
/**
75
* Teardown
76
*/
77
void
78
MM_ProcessorInfo::tearDown(MM_EnvironmentBase *env)
79
{
80
}
81
82
U_64
83
MM_ProcessorInfo::secondToTick(double s)
84
{
85
return (U_64) (s * _freq);
86
}
87
88
double
89
MM_ProcessorInfo::tickToSecond(U_64 t)
90
{
91
return t / _freq;
92
}
93
94
double
95
MM_ProcessorInfo::readFrequency()
96
{
97
double freq = 0;
98
#if (defined(LINUX) && (defined(J9X86) || defined(J9HAMMER) || defined(LINUXPPC)))
99
char buffer[256];
100
FILE *file = fopen("/proc/cpuinfo", "r");
101
if (file != NULL) {
102
while (!feof(file)) {
103
if (NULL == fgets(buffer, sizeof(buffer), file)) {
104
break;
105
}
106
#if (defined(J9X86) || defined(J9HAMMER))
107
/* the RTC on x86 is synchronized with the core clock so find the frequency in MHz then multiply by 10^6 to determine the ticks in a second */
108
if (sscanf(buffer, "cpu MHz : %lf", &freq) == 1) {
109
freq *= 1e6;
110
break;
111
}
112
#elif defined(LINUXPPC)
113
/* the timebase register on PPC is potentially lower frequency than the core clock so get the kernel's estimation of its frequency */
114
U_64 tempBase = 0;
115
if (sscanf(buffer, "timebase : %llu", (unsigned long long*)&tempBase) == 1) {
116
/* PPC timebase is already in ticks per second so just convert it to a double for consistency with x86 implementation */
117
freq = (double)tempBase;
118
break;
119
}
120
#else
121
/* missing linux proc parsing logic */
122
Assert_MM_unimplemented();
123
#endif
124
}
125
fclose(file);
126
}
127
#elif defined(WIN32)
128
/* Should read this via Windows API */
129
#elif defined(AIXPPC)
130
/* Figure out the right thing to do */
131
freq = 1000;
132
#else
133
/* Hi-res tick implementation required */
134
Assert_MM_unimplemented();
135
#endif
136
return freq;
137
}
138
139