1 /*
 2  * Copyright (c) 2016, 2018, Oracle and/or its affiliates. All rights reserved.
 3  * Copyright (c) 2020, 2021, 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 "memory/allocation.hpp"
27 #include "memory/allocation.inline.hpp"
28 #include "runtime/os.inline.hpp"
29 #include "vm_version_ext_riscv.hpp"
30 
31 // VM_Version_Ext statics
32 int VM_Version_Ext::_no_of_threads = 0;
33 int VM_Version_Ext::_no_of_cores = 0;
34 int VM_Version_Ext::_no_of_sockets = 0;
35 bool VM_Version_Ext::_initialized = false;
36 char VM_Version_Ext::_cpu_name[CPU_TYPE_DESC_BUF_SIZE] = {0};
37 char VM_Version_Ext::_cpu_desc[CPU_DETAILED_DESC_BUF_SIZE] = {0};
38 
39 void VM_Version_Ext::initialize_cpu_information(void) {
40   // do nothing if cpu info has been initialized
41   if (_initialized) {
42     return;
43   }
44 
45   int core_id = -1;
46   int chip_id = -1;
47   int len = 0;
48   char* src_string = NULL;
49 
50   _no_of_cores  = os::processor_count();
51   _no_of_threads = _no_of_cores;
52   _no_of_sockets = _no_of_cores;
53   snprintf(_cpu_name, CPU_TYPE_DESC_BUF_SIZE - 1, "Riscv64");
54   snprintf(_cpu_desc, CPU_DETAILED_DESC_BUF_SIZE, "Riscv64 %s", _features_string);
55   _initialized = true;
56 }
57 
58 int VM_Version_Ext::number_of_threads(void) {
59   initialize_cpu_information();
60   return _no_of_threads;
61 }
62 
63 int VM_Version_Ext::number_of_cores(void) {
64   initialize_cpu_information();
65   return _no_of_cores;
66 }
67 
68 int VM_Version_Ext::number_of_sockets(void) {
69   initialize_cpu_information();
70   return _no_of_sockets;
71 }
72 
73 const char* VM_Version_Ext::cpu_name(void) {
74   initialize_cpu_information();
75   char* tmp = NEW_C_HEAP_ARRAY_RETURN_NULL(char, CPU_TYPE_DESC_BUF_SIZE, mtTracing);
76   if (NULL == tmp) {
77     return NULL;
78   }
79   strncpy(tmp, _cpu_name, CPU_TYPE_DESC_BUF_SIZE);
80   return tmp;
81 }
82 
83 const char* VM_Version_Ext::cpu_description(void) {
84   initialize_cpu_information();
85   char* tmp = NEW_C_HEAP_ARRAY_RETURN_NULL(char, CPU_DETAILED_DESC_BUF_SIZE, mtTracing);
86   if (NULL == tmp) {
87     return NULL;
88   }
89   strncpy(tmp, _cpu_desc, CPU_DETAILED_DESC_BUF_SIZE);
90   return tmp;
91 }