-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
830 additions
and
82 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
/* | ||
* Copyright (c) Facebook, Inc. and its affiliates. | ||
* | ||
* Licensed 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 "velox/dwio/common/QplJobPool.h" | ||
#include <folly/Random.h> | ||
#include <iostream> | ||
#include "velox/common/base/Exceptions.h" | ||
|
||
namespace facebook::velox::dwio::common { | ||
|
||
std::array<qpl_job*, QplJobHWPool::MAX_JOB_NUMBER> | ||
QplJobHWPool::hw_job_ptr_pool; | ||
std::array<std::atomic<bool>, QplJobHWPool::MAX_JOB_NUMBER> | ||
QplJobHWPool::hw_job_ptr_locks; | ||
bool QplJobHWPool::iaa_job_ready = false; | ||
std::unique_ptr<uint8_t[]> QplJobHWPool::hw_jobs_buffer; | ||
|
||
QplJobHWPool& QplJobHWPool::GetInstance() { | ||
static QplJobHWPool pool; | ||
return pool; | ||
} | ||
|
||
QplJobHWPool::QplJobHWPool() { | ||
if (!iaa_job_ready) { | ||
(void)AllocateQPLJob(); | ||
} | ||
} | ||
|
||
QplJobHWPool::~QplJobHWPool() { | ||
for (uint32_t i = 0; i < MAX_JOB_NUMBER; ++i) { | ||
if (hw_job_ptr_pool[i]) { | ||
qpl_fini_job(hw_job_ptr_pool[i]); | ||
hw_job_ptr_pool[i] = nullptr; | ||
} | ||
} | ||
iaa_job_ready = false; | ||
} | ||
|
||
bool QplJobHWPool::AllocateQPLJob() { | ||
uint32_t job_size = 0; | ||
|
||
// Get size required for saving a single qpl job object | ||
qpl_get_job_size(qpl_path, &job_size); | ||
// Allocate entire buffer for storing all job objects | ||
hw_jobs_buffer = std::make_unique<uint8_t[]>(job_size * MAX_JOB_NUMBER); | ||
// Initialize pool for storing all job object pointers | ||
// Allocate buffer by shifting address offset for each job object. | ||
for (uint32_t index = 0; index < MAX_JOB_NUMBER; ++index) { | ||
qpl_job* qpl_job_ptr = | ||
reinterpret_cast<qpl_job*>(hw_jobs_buffer.get() + index * job_size); | ||
auto status = qpl_init_job(qpl_path, qpl_job_ptr); | ||
if (status != QPL_STS_OK) { | ||
iaa_job_ready = false; | ||
LOG(WARNING) << "Initialization of hardware IAA failed, statsu: " | ||
<< status << ". Please check if Intel \ | ||
In-Memory Analytics Accelerator (IAA) is properly set up!"; | ||
return false; | ||
} | ||
this->hw_job_ptr_pool[index] = qpl_job_ptr; | ||
hw_job_ptr_locks[index].store(false); | ||
} | ||
|
||
iaa_job_ready = true; | ||
return true; | ||
} | ||
|
||
qpl_job* QplJobHWPool::AcquireDeflateJob(int& job_id) { | ||
job_id = -1; | ||
if (!job_ready()) { | ||
return nullptr; | ||
} | ||
uint32_t retry = 0; | ||
auto index = folly::Random::rand32(1, MAX_JOB_NUMBER - 1); | ||
while (!tryLockJob(index)) { | ||
index = folly::Random::rand32(1, MAX_JOB_NUMBER - 1); | ||
retry++; | ||
if (retry > MAX_JOB_NUMBER) { | ||
return nullptr; | ||
} | ||
} | ||
job_id = index; | ||
if (index >= MAX_JOB_NUMBER) { | ||
return nullptr; | ||
} | ||
|
||
return hw_job_ptr_pool[index]; | ||
} | ||
|
||
void QplJobHWPool::ReleaseJob(int job_id) { | ||
if (job_id >= MAX_JOB_NUMBER || job_id <= 0) { | ||
return; | ||
} | ||
assert(job_id < MAX_JOB_NUMBER); | ||
hw_job_ptr_locks[job_id].store(false); | ||
return; | ||
} | ||
|
||
bool QplJobHWPool::tryLockJob(uint32_t index) { | ||
bool expected = false; | ||
assert(index < MAX_JOB_NUMBER); | ||
return hw_job_ptr_locks[index].compare_exchange_strong(expected, true); | ||
} | ||
|
||
} // namespace facebook::velox::dwio::common |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
/* | ||
* Copyright (c) Facebook, Inc. and its affiliates. | ||
* | ||
* Licensed 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. | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include <memory> | ||
#include <mutex> | ||
#include <random> | ||
#include <vector> | ||
|
||
#include "qpl/qpl.h" | ||
|
||
namespace facebook::velox::dwio::common { | ||
|
||
// QplJobHWPool is resource pool to provide the job objects, which is | ||
// used for storing context information. | ||
// Memory for QPL job will be allocated when the QPLJobHWPool instance is | ||
// created | ||
class QplJobHWPool { | ||
public: | ||
static QplJobHWPool& GetInstance(); | ||
QplJobHWPool(); | ||
~QplJobHWPool(); | ||
|
||
// Release QPL job by the job_id. | ||
void ReleaseJob(int job_id); | ||
|
||
// Return if the QPL job is allocated sucessfully. | ||
const bool& job_ready() { | ||
return iaa_job_ready; | ||
} | ||
|
||
qpl_job* AcquireDeflateJob(int& job_id); | ||
qpl_job* GetJobById(int job_id) { | ||
if (job_id >= MAX_JOB_NUMBER || job_id <= 0) { | ||
return nullptr; | ||
} | ||
return hw_job_ptr_pool[job_id]; | ||
} | ||
|
||
static constexpr auto MAX_JOB_NUMBER = 1024; | ||
|
||
private: | ||
bool tryLockJob(uint32_t index); | ||
bool AllocateQPLJob(); | ||
|
||
static constexpr qpl_path_t qpl_path = qpl_path_hardware; | ||
// Max jobs in QPL_JOB_POOL | ||
// Entire buffer for storing all job objects | ||
static std::unique_ptr<uint8_t[]> hw_jobs_buffer; | ||
|
||
// Job pool for storing all job object pointers | ||
static std::array<qpl_job*, MAX_JOB_NUMBER> hw_job_ptr_pool; | ||
|
||
// Locks for accessing each job object pointers | ||
static bool iaa_job_ready; | ||
static std::array<std::atomic<bool>, MAX_JOB_NUMBER> hw_job_ptr_locks; | ||
}; | ||
|
||
} // namespace facebook::velox::dwio::common |
Oops, something went wrong.