MCPcopy Create free account
hub / github.com/apache/arrow / OsRetrieveCacheSize

Function OsRetrieveCacheSize

cpp/src/arrow/util/cpu_info.cc:119–167  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

117//------------------------------ WINDOWS ------------------------------//
118template <std::size_t N>
119void OsRetrieveCacheSize(std::array<int64_t, N>* cache_sizes) {
120 static_assert(N >= 3);
121 PSYSTEM_LOGICAL_PROCESSOR_INFORMATION buffer = nullptr;
122 PSYSTEM_LOGICAL_PROCESSOR_INFORMATION buffer_position = nullptr;
123 DWORD buffer_size = 0;
124 size_t offset = 0;
125 typedef BOOL(WINAPI * GetLogicalProcessorInformationFuncPointer)(void*, void*);
126 GetLogicalProcessorInformationFuncPointer func_pointer =
127 (GetLogicalProcessorInformationFuncPointer)GetProcAddress(
128 GetModuleHandleW(L"kernel32"), "GetLogicalProcessorInformation");
129
130 if (!func_pointer) {
131 ARROW_LOG(WARNING) << "Failed to find procedure GetLogicalProcessorInformation";
132 return;
133 }
134
135 // Get buffer size
136 if (func_pointer(buffer, &buffer_size) && GetLastError() != ERROR_INSUFFICIENT_BUFFER) {
137 ARROW_LOG(WARNING) << "Failed to get size of processor information buffer";
138 return;
139 }
140
141 buffer = (PSYSTEM_LOGICAL_PROCESSOR_INFORMATION)malloc(buffer_size);
142 if (!buffer) {
143 return;
144 }
145
146 if (!func_pointer(buffer, &buffer_size)) {
147 ARROW_LOG(WARNING) << "Failed to get processor information";
148 free(buffer);
149 return;
150 }
151
152 buffer_position = buffer;
153 while (offset + sizeof(SYSTEM_LOGICAL_PROCESSOR_INFORMATION) <= buffer_size) {
154 if (RelationCache == buffer_position->Relationship) {
155 PCACHE_DESCRIPTOR cache = &buffer_position->Cache;
156 using level_t = decltype(cache->Level);
157 if (cache->Level >= 1 && cache->Level <= static_cast<level_t>(N)) {
158 const int64_t current = (*cache_sizes)[cache->Level - 1];
159 (*cache_sizes)[cache->Level - 1] = std::max<int64_t>(current, cache->Size);
160 }
161 }
162 offset += sizeof(SYSTEM_LOGICAL_PROCESSOR_INFORMATION);
163 buffer_position++;
164 }
165
166 free(buffer);
167}
168
169#elif defined(__APPLE__)
170//------------------------------ MACOS ------------------------------//

Callers 1

CpuInfoMethod · 0.85

Calls 2

IntegerSysCtlByNameFunction · 0.85
LinuxGetCacheSizeFunction · 0.85

Tested by

no test coverage detected