Skip to content

Commit

Permalink
feat(cmake): refine cache info getting method
Browse files Browse the repository at this point in the history
  • Loading branch information
ShannonBase committed Jan 9, 2025
1 parent 21033e2 commit b81d79d
Show file tree
Hide file tree
Showing 5 changed files with 194 additions and 73 deletions.
96 changes: 96 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -806,6 +806,102 @@ IF(LINUX)
ENDIF()
ENDIF()

# detect cpu type.
if(WIN32)
execute_process(
COMMAND wmic cpu get manufacturer
OUTPUT_VARIABLE CPU_MANUFACTURER
OUTPUT_STRIP_TRAILING_WHITESPACE
)
elseif (APPLE)
execute_process(
COMMAND sysctl -n machdep.cpu.vendor
OUTPUT_VARIABLE CPU_MANUFACTURER
OUTPUT_STRIP_TRAILING_WHITESPACE
)
elseif (UNIX AND NOT APPLE)
execute_process(
COMMAND lscpu
COMMAND grep "Vendor ID"
OUTPUT_VARIABLE CPU_MANUFACTURER
OUTPUT_STRIP_TRAILING_WHITESPACE
)
else()
message(WARNING "Unsupported platform for CPU vendor detection")
set(CPU_MANUFACTURER "Unknown")
endif()

string(TOLOWER "${CPU_MANUFACTURER}" CPU_MANUFACTURER_LOWER)

if(CPU_MANUFACTURER_LOWER MATCHES "intel")
set(CPU_TYPE "intel")
set(CACHE_SIZE_DETECTOR_SOURCE "${CMAKE_SOURCE_DIR}/storage/rapid_engine/utils/cpu_cache_size_intel.cpp")
set(CACHE_LINE_SIZE_DETECTOR_SOURCE "${CMAKE_SOURCE_DIR}/storage/rapid_engine/utils/cache_line_size_intel.cpp")
elseif(CPU_MANUFACTURER_LOWER MATCHES "amd")
set(CPU_TYPE "amd")
set(CACHE_SIZE_DETECTOR_SOURCE "${CMAKE_SOURCE_DIR}/storage/rapid_engine/utils/cpu_cache_size_amd.cpp")
set(CACHE_LINE_SIZE_DETECTOR_SOURCE "${CMAKE_SOURCE_DIR}/storage/rapid_engine/utils/cache_line_size_amd.cpp")
elseif(CPU_MANUFACTURER_LOWER MATCHES "apple")
set(CPU_TYPE "apple")
set(CACHE_SIZE_DETECTOR_SOURCE "${CMAKE_SOURCE_DIR}/storage/rapid_engine/utils/cpu_cache_size_apple.cpp")
set(CACHE_LINE_SIZE_DETECTOR_SOURCE "${CMAKE_SOURCE_DIR}/storage/rapid_engine/utils/cache_line_size_apple.cpp")
else()
message(FATAL_ERROR "Unsupported CPU manufacturer: ${CPU_MANUFACTURER}")
endif()

message(STATUS "Detected CPU type: ${CPU_TYPE}")

#get cache line size.
ADD_EXECUTABLE(get_cache_line_size ${CACHE_LINE_SIZE_DETECTOR_SOURCE})
execute_process(
COMMAND ${CMAKE_BINARY_DIR}/get_cache_line_size
OUTPUT_VARIABLE CACHE_LINE_SIZE
ERROR_VARIABLE CACHE_ERROR
OUTPUT_STRIP_TRAILING_WHITESPACE
)

IF (NOT CACHE_LINE_SIZE OR CACHE_LINE_SIZE STREQUAL "")
SET(CACHE_LINE_SIZE "64")
ENDIF()

MESSAGE(STATUS "Cache Line Size: ${CACHE_LINE_SIZE} bytes")

#gets cache size
ADD_EXECUTABLE(get_cache_size ${CACHE_SIZE_DETECTOR_SOURCE})
execute_process(
COMMAND ${CMAKE_BINARY_DIR}/get_cache_size
OUTPUT_VARIABLE CACHE_OUTPUT
ERROR_VARIABLE CACHE_ERROR
OUTPUT_STRIP_TRAILING_WHITESPACE
)

MESSAGE(STATUS "Cache Size Msg: ${CACHE_OUTPUT}")

STRING(REGEX MATCHALL "CACHE_L[1-3]=[0-9]+" CACHE_MATCHES "${CACHE_OUTPUT}")
FOREACH(MATCH ${CACHE_MATCHES})
string(REGEX REPLACE "CACHE_L([1-3])=([0-9]+)" "\\1;\\2" CACHE_PAIR "${MATCH}")
list(GET CACHE_PAIR 0 CACHE_LEVEL)
list(GET CACHE_PAIR 1 CACHE_SIZE)
set(CACHE_L${CACHE_LEVEL}_SIZE ${CACHE_SIZE})
ENDFOREACH()

IF (NOT CACHE_L1 OR CACHE_L1 STREQUAL "")
SET(CACHE_L1 "32768 ")
ENDIF()
MESSAGE(STATUS "L1 Cache Size: ${CACHE_L1} bytes")

IF (NOT CACHE_L2 OR CACHE_L2 STREQUAL "")
SET(CACHE_L2 "524288 ")
ENDIF()
MESSAGE(STATUS "L2 Cache Size: ${CACHE_L2} bytes")

IF (NOT CACHE_L3 OR CACHE_L3 STREQUAL "")
SET(CACHE_L3 "8388608")
ENDIF()
MESSAGE(STATUS "L3 Cache Size: ${CACHE_L3} bytes")

CONFIGURE_FILE(${CMAKE_SOURCE_DIR}/storage/rapid_engine/include/rapid_arch_inf.h.in ${CMAKE_SOURCE_DIR}/storage/rapid_engine/include/rapid_arch_inf.h)

INCLUDE(CMakePushCheckState)

# Add macros
Expand Down
71 changes: 0 additions & 71 deletions storage/rapid_engine/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -45,77 +45,6 @@ INCLUDE_DIRECTORIES( ${CMAKE_SOURCE_DIR}/extra/lightgbm/LightGBM/include)
# STRING(REPLACE "-flto" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")


# detect cpu type.
if(CMAKE_SYSTEM_NAME STREQUAL "Windows")
execute_process(
COMMAND wmic cpu get manufacturer
OUTPUT_VARIABLE CPU_MANUFACTURER
OUTPUT_STRIP_TRAILING_WHITESPACE
)
else()
execute_process(
COMMAND lscpu
COMMAND grep "Vendor ID"
OUTPUT_VARIABLE CPU_MANUFACTURER
OUTPUT_STRIP_TRAILING_WHITESPACE
)
endif()

string(TOLOWER "${CPU_MANUFACTURER}" CPU_MANUFACTURER_LOWER)

if(CPU_MANUFACTURER_LOWER MATCHES "intel")
set(CPU_TYPE "intel")
set(CACHE_SIZE_DETECTOR_SOURCE "utils/cpu_cache_size_intel.cpp")
set(CACHE_LINE_SIZE_DETECTOR_SOURCE "utils/cache_line_size_intel.cpp")
elseif(CPU_MANUFACTURER_LOWER MATCHES "amd")
set(CPU_TYPE "amd")
set(CACHE_SIZE_DETECTOR_SOURCE "utils/cpu_cache_size_amd.cpp")
set(CACHE_LINE_SIZE_DETECTOR_SOURCE "utils/cache_line_size_amd.cpp")
else()
message(FATAL_ERROR "Unsupported CPU manufacturer: ${CPU_MANUFACTURER}")
endif()

message(STATUS "Detected CPU type: ${CPU_TYPE}")

#get cache line size.
ADD_EXECUTABLE(get_cache_line_size ${CACHE_LINE_SIZE_DETECTOR_SOURCE})
execute_process(
COMMAND ${CMAKE_BINARY_DIR}/storage/rapid_engine/get_cache_line_size
OUTPUT_VARIABLE CACHE_LINE_SIZE
ERROR_VARIABLE CACHE_ERROR
OUTPUT_STRIP_TRAILING_WHITESPACE
)
IF (NOT CACHE_LINE_SIZE OR CACHE_LINE_SIZE STREQUAL "")
SET(CACHE_LINE_SIZE "64")
ENDIF()
MESSAGE(STATUS "L1 Cache Line Size: ${CACHE_LINE_SIZE} bytes")

#gets cache size
ADD_EXECUTABLE(get_cache_size ${CACHE_SIZE_DETECTOR_SOURCE})
execute_process(
COMMAND ${CMAKE_BINARY_DIR}/storage/rapid_engine/get_cache_size
OUTPUT_VARIABLE CACHE_OUTPUT
ERROR_VARIABLE CACHE_ERROR
OUTPUT_STRIP_TRAILING_WHITESPACE
)

MESSAGE(STATUS "cache size msg:")
MESSAGE(STATUS "${CACHE_OUTPUT}")

STRING(REGEX MATCHALL "CACHE_L[1-3]=[0-9]+" CACHE_MATCHES "${CACHE_OUTPUT}")
FOREACH(MATCH ${CACHE_MATCHES})
string(REGEX REPLACE "CACHE_L([1-3])=([0-9]+)" "\\1;\\2" CACHE_PAIR "${MATCH}")
list(GET CACHE_PAIR 0 CACHE_LEVEL)
list(GET CACHE_PAIR 1 CACHE_SIZE)
set(CACHE_L${CACHE_LEVEL}_SIZE ${CACHE_SIZE})
ENDFOREACH()
IF (NOT CACHE_L3 OR CACHE_L3 STREQUAL "")
SET(CACHE_L3 "8388608")
ENDIF()
MESSAGE(STATUS "L3 Cache Size: ${CACHE_L3} bytes")

CONFIGURE_FILE(${CMAKE_SOURCE_DIR}/storage/rapid_engine/include/rapid_arch_inf.h.in ${CMAKE_SOURCE_DIR}/storage/rapid_engine/include/rapid_arch_inf.h)

SET(SHANNON_RAPID_COMPRESS_SOURCES
compress/algorithms.cpp
compress/dictionary/dictionary.cpp
Expand Down
43 changes: 43 additions & 0 deletions storage/rapid_engine/utils/cache_line_size_apple.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/**
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2.0,
as published by the Free Software Foundation.
This program is also distributed with certain software (including
but not limited to OpenSSL) that is licensed under separate terms,
as designated in a particular file or component or in included license
documentation. The authors of MySQL hereby grant you an additional
permission to link the program and your derivative works with the
separately licensed software that they have included with MySQL.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License, version 2.0, for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
The fundmental code for imcs.
Copyright (c) 2023, Shannon Data AI and/or its affiliates.
*/

#include <sys/sysctl.h>
#include <sys/types.h>
#include <iostream>

void get_cache_lines_size() {
size_t cacheline_size = 0;
size_t len = sizeof(cacheline_size);

// cache line size
sysctlbyname("hw.cachelinesize", &cacheline_size, &len, nullptr, 0);
printf("%u \n", cache_line_size);
}

int main() {
get_cache_lines_size();
return 0;
}
4 changes: 2 additions & 2 deletions storage/rapid_engine/utils/cache_line_size_intel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

#include <stdio.h>

void get_l1_cache_line_size() {
void get_cache_lines_size() {
unsigned int eax, ebx, ecx, edx;
eax = 4; // CPUID leaf 4 for cache information
ecx = 0; // Query L1 cache (level 1 cache)
Expand All @@ -41,6 +41,6 @@ void get_l1_cache_line_size() {
}

int main() {
get_l1_cache_line_size();
get_cache_lines_size();
return 0;
}
53 changes: 53 additions & 0 deletions storage/rapid_engine/utils/cpu_cache_size_apple.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/**
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2.0,
as published by the Free Software Foundation.
This program is also distributed with certain software (including
but not limited to OpenSSL) that is licensed under separate terms,
as designated in a particular file or component or in included license
documentation. The authors of MySQL hereby grant you an additional
permission to link the program and your derivative works with the
separately licensed software that they have included with MySQL.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License, version 2.0, for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
The fundmental code for imcs. to get all level cache size of a cpu.
Copyright (c) 2023, Shannon Data AI and/or its affiliates.
*/

// gets intel cpu size of all cache level.
#include <stdio.h>
#include <sys/sysctl.h>
#include <sys/types.h>

void get_intel_cache_size() {
size_t l1icache = 0, l1dcache = 0, l2cache = 0, l3cache = 0;
size_t len = sizeof(l1icache);

// l1-instr
sysctlbyname("hw.l1icachesize", &l1icache, &len, NULL, 0);
// l2-data
sysctlbyname("hw.l1dcachesize", &l1dcache, &len, NULL, 0);
// l2 size
sysctlbyname("hw.l2cachesize", &l2cache, &len, NULL, 0);
// l3 size
sysctlbyname("hw.l3cachesize", &l3cache, &len, NULL, 0);

printf("CACHE_L1=%zu\n", l1dcache / 1024);
printf("CACHE_L2=%zu\n", l2cache / 1024);
printf("CACHE_L3=%zu\n", l3cache / 1024);
}

int main() {
get_intel_cache_size();
return 0;
}

0 comments on commit b81d79d

Please sign in to comment.