1 /*
 2  * Copyright (c) 2016, 2021, Oracle and/or its affiliates. All rights reserved.
 3  * Copyright (c) 2020, 2022, Huawei Technologies Co., Ltd. All rights reserved.
 4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 5  *
 6  * This code is free software; you can redistribute it and/or modify it
 7  * under the terms of the GNU General Public License version 2 only, as
 8  * published by the Free Software Foundation.
 9  *
10  * This code is distributed in the hope that it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
13  * version 2 for more details (a copy is included in the LICENSE file that
14  * accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License version
17  * 2 along with this work; if not, write to the Free Software Foundation,
18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
19  *
20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
21  * or visit www.oracle.com if you need additional information or have any
22  * questions.
23  *
24  */
25 
26 #include "precompiled.hpp"
27 #include "memory/allocation.hpp"
28 #include "memory/allocation.inline.hpp"
29 #include "runtime/os.inline.hpp"
30 #include "vm_version_ext_riscv.hpp"
31 
32 // VM_Version_Ext statics
33 int VM_Version_Ext::_no_of_threads = 0;
34 int VM_Version_Ext::_no_of_cores = 0;
35 int VM_Version_Ext::_no_of_sockets = 0;
36 bool VM_Version_Ext::_initialized = false;
37 char VM_Version_Ext::_cpu_name[CPU_TYPE_DESC_BUF_SIZE] = {0};
38 char VM_Version_Ext::_cpu_desc[CPU_DETAILED_DESC_BUF_SIZE] = {0};
39 
40 void VM_Version_Ext::initialize_cpu_information(void) {
41   // do nothing if cpu info has been initialized
42   if (_initialized) {
43     return;
44   }
45 
46   _no_of_cores  = os::processor_count();
47   _no_of_threads = _no_of_cores;
48   _no_of_sockets = _no_of_cores;
49   snprintf(_cpu_name, CPU_TYPE_DESC_BUF_SIZE - 1, "RISCV64");
50   snprintf(_cpu_desc, CPU_DETAILED_DESC_BUF_SIZE, "RISCV64 %s", _features_string);
51   _initialized = true;
52 }
53 
54 int VM_Version_Ext::number_of_threads(void) {
55   initialize_cpu_information();
56   return _no_of_threads;
57 }
58 
59 int VM_Version_Ext::number_of_cores(void) {
60   initialize_cpu_information();
61   return _no_of_cores;
62 }
63 
64 int VM_Version_Ext::number_of_sockets(void) {
65   initialize_cpu_information();
66   return _no_of_sockets;
67 }
68 
69 const char* VM_Version_Ext::cpu_name(void) {
70   initialize_cpu_information();
71   char* tmp = NEW_C_HEAP_ARRAY_RETURN_NULL(char, CPU_TYPE_DESC_BUF_SIZE, mtTracing);
72   if (NULL == tmp) {
73     return NULL;
74   }
75   strncpy(tmp, _cpu_name, CPU_TYPE_DESC_BUF_SIZE);
76   return tmp;
77 }
78 
79 const char* VM_Version_Ext::cpu_description(void) {
80   initialize_cpu_information();
81   char* tmp = NEW_C_HEAP_ARRAY_RETURN_NULL(char, CPU_DETAILED_DESC_BUF_SIZE, mtTracing);
82   if (NULL == tmp) {
83     return NULL;
84   }
85   strncpy(tmp, _cpu_desc, CPU_DETAILED_DESC_BUF_SIZE);
86   return tmp;
87 }