aocsa commented on a change in pull request #11426: URL: https://github.com/apache/arrow/pull/11426#discussion_r739654690
########## File path: cpp/src/arrow/compute/memory_resources.cc ########## @@ -0,0 +1,307 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +#include "arrow/compute/memory_resources.h" +#include "arrow/compute/exec.h" +#include "arrow/record_batch.h" +#include "arrow/table.h" +#include "arrow/util/make_unique.h" + +#include <memory> +#include <mutex> +#include <random> +#include <unordered_map> + +#include <arrow/filesystem/filesystem.h> +#include <arrow/ipc/feather.h> +#include <arrow/ipc/reader.h> +#include <arrow/ipc/writer.h> +#include "arrow/io/file.h" + +#ifdef __APPLE__ +#include <sys/sysctl.h> +#include <sys/types.h> +#endif + +#ifdef __linux__ +#include <sys/statvfs.h> +#include <sys/sysinfo.h> +#endif + +// Windows APIs +#include "arrow/util/windows_compatibility.h" + +namespace arrow { + +namespace compute { + +std::string MemoryLevelName(MemoryLevel memory_level) { + static const char* MemoryLevelNames[] = {ARROW_STRINGIFY(MemoryLevel::kDiskLevel), + ARROW_STRINGIFY(MemoryLevel::kCPULevel), + ARROW_STRINGIFY(MemoryLevel::kGPULevel)}; + + return MemoryLevelNames[static_cast<int>(memory_level)]; +} + +std::string MemoryResource::ToString() const { return MemoryLevelName(memory_level_); } + +class CPUDataHolder : public DataHolder { + public: + explicit CPUDataHolder(const std::shared_ptr<RecordBatch>& record_batch) + : DataHolder(MemoryLevel::kCPULevel), record_batch_(std::move(record_batch)) {} + + Result<ExecBatch> Get() override { return ExecBatch(*record_batch_); } + + private: + std::shared_ptr<RecordBatch> record_batch_; +}; + +namespace { + +std::string RandomString(std::size_t length) { + const std::string characters = + "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; + std::random_device random_device; + std::mt19937 generator(random_device()); + std::uniform_int_distribution<> distribution(0, characters.size() - 1); + std::string random_string; + for (std::size_t i = 0; i < length; ++i) { + random_string += characters[distribution(generator)]; + } + return random_string; +} + +} // namespace + +Status StoreRecordBatch(const std::shared_ptr<RecordBatch>& record_batch, + const std::shared_ptr<fs::FileSystem>& filesystem, + const std::string& file_path) { + auto output = filesystem->OpenOutputStream(file_path).ValueOrDie(); + auto writer = + arrow::ipc::MakeFileWriter(output.get(), record_batch->schema()).ValueOrDie(); + ARROW_RETURN_NOT_OK(writer->WriteRecordBatch(*record_batch)); + return writer->Close(); +} +Result<std::shared_ptr<RecordBatch>> RecoverRecordBatch( + const std::shared_ptr<fs::FileSystem>& filesystem, const std::string& file_path) { + ARROW_ASSIGN_OR_RAISE(auto input, filesystem->OpenInputFile(file_path)); + ARROW_ASSIGN_OR_RAISE(auto reader, arrow::ipc::feather::Reader::Open(input)); + std::shared_ptr<Table> table; + ARROW_RETURN_NOT_OK(reader->Read(&table)); + TableBatchReader batch_iter(*table); + ARROW_ASSIGN_OR_RAISE(auto batch, batch_iter.Next()); + return batch; +} + +class DiskDataHolder : public DataHolder { + public: + DiskDataHolder(const std::shared_ptr<RecordBatch>& record_batch, + MemoryPool* memory_pool) + : DataHolder(MemoryLevel::kDiskLevel), memory_pool_(memory_pool) { + std::string root_path; + std::string file_name = "data-holder-temp-" + RandomString(64) + ".feather"; + + filesystem_ = + arrow::fs::FileSystemFromUri(cache_storage_root_path, &root_path).ValueOrDie(); + + file_path_ = root_path + file_name; + status_ = StoreRecordBatch(record_batch, filesystem_, file_path_); + } + + Result<ExecBatch> Get() override { + ARROW_RETURN_NOT_OK(status_); + ARROW_ASSIGN_OR_RAISE(auto record_batch, RecoverRecordBatch(filesystem_, file_path_)); + return ExecBatch(*record_batch); + } + + private: + std::string file_path_; + Status status_; + MemoryPool* memory_pool_; + std::shared_ptr<arrow::fs::FileSystem> filesystem_; + const std::string cache_storage_root_path = "file:///tmp/"; +}; + +class MemoryResources::MemoryResourcesImpl { + public: + Status AddMemoryResource(std::unique_ptr<MemoryResource> resource) { + std::lock_guard<std::mutex> mutation_guard(lock_); + auto level = resource->memory_level(); + auto it = stats_.find(level); + if (it != stats_.end()) { + return Status::KeyError("Already have a resource type registered with name: ", + resource->ToString()); + } + stats_[level] = std::move(resource); + return Status::OK(); + } + + size_t size() const { return stats_.size(); } + + Result<int64_t> memory_limit(MemoryLevel level) const { + auto it = stats_.find(level); + if (it == stats_.end()) { + return Status::KeyError("No memory resource registered with level: ", + MemoryLevelName(level)); + } + return it->second->memory_limit(); + } + + Result<int64_t> memory_used(MemoryLevel level) const { + auto it = stats_.find(level); + if (it == stats_.end()) { + return Status::KeyError("No memory resource registered with level: ", + MemoryLevelName(level)); + } + return it->second->memory_used(); + } + + Result<MemoryResource*> memory_resource(MemoryLevel level) const { + auto it = stats_.find(level); + if (it == stats_.end()) { + return Status::KeyError("No memory resource registered with level: ", + MemoryLevelName(level)); + } + return it->second.get(); + } + + private: + std::mutex lock_; + + std::unordered_map<MemoryLevel, std::unique_ptr<MemoryResource>> stats_; Review comment: Sure -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
