Commit 4892f939 by Ben Clayton

Yarn: Add Scheduler class

The scheduler is the core of Yarn, and has tight coupling with the OS-abstracted Scheduler::Fiber class. Added basic tests - this will be expanded in later changes. Bug: b/139010488 Change-Id: I562c61d3c4551c4347d9306a3dd87efed06e45a5 Reviewed-on: https://swiftshader-review.googlesource.com/c/SwiftShader/+/34812Tested-by: 's avatarBen Clayton <bclayton@google.com> Kokoro-Presubmit: kokoro <noreply+kokoro@google.com> Reviewed-by: 's avatarNicolas Capens <nicolascapens@google.com>
parent 65a26b64
......@@ -14,6 +14,8 @@
#include "Debug.hpp"
#include "Scheduler.hpp"
#include <cstdlib>
#include <stdarg.h>
......@@ -33,7 +35,7 @@ void fatal(const char* msg, ...)
void assert_has_bound_scheduler(const char* feature)
{
// TODO
YARN_ASSERT(Scheduler::get() != nullptr, "%s requires a yarn::Scheduler to be bound", feature);
}
} // namespace yarn
// Copyright 2019 The SwiftShader Authors. All Rights Reserved.
//
// 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 "Yarn_test.hpp"
TEST(WithoutBoundScheduler, SchedulerConstructAndDestruct)
{
auto scheduler = new yarn::Scheduler();
delete scheduler;
}
TEST(WithoutBoundScheduler, SchedulerBindGetUnbind)
{
auto scheduler = new yarn::Scheduler();
scheduler->bind();
auto got = yarn::Scheduler::get();
ASSERT_EQ(scheduler, got);
scheduler->unbind();
got = yarn::Scheduler::get();
ASSERT_EQ(got, nullptr);
delete scheduler;
}
TEST_P(WithBoundScheduler, SetAndGetWorkerThreadCount)
{
ASSERT_EQ(yarn::Scheduler::get()->getWorkerThreadCount(), GetParam().numWorkerThreads);
}
TEST_P(WithBoundScheduler, DestructWithPendingTasks)
{
for (int i = 0; i < 10000; i++)
{
yarn::schedule([] {});
}
}
......@@ -14,8 +14,17 @@
#include "Yarn_test.hpp"
INSTANTIATE_TEST_SUITE_P(SchedulerParams, WithBoundScheduler, testing::Values(
SchedulerParams{0}, // Single-threaded mode test
SchedulerParams{1}, // Single worker thread
SchedulerParams{2}, // 2 worker threads...
SchedulerParams{4},
SchedulerParams{8},
SchedulerParams{64}
));
int main(int argc, char **argv)
{
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
......@@ -15,4 +15,41 @@
#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include "Yarn/Scheduler.hpp"
// SchedulerParams holds Scheduler construction parameters for testing.
struct SchedulerParams
{
int numWorkerThreads;
friend std::ostream& operator<<(std::ostream& os, const SchedulerParams& params) {
return os << "SchedulerParams{" <<
"numWorkerThreads: " << params.numWorkerThreads <<
"}";
}
};
// WithoutBoundScheduler is a test fixture that does not bind a scheduler.
class WithoutBoundScheduler : public testing::Test {};
// WithBoundScheduler is a parameterized test fixture that performs tests with
// a bound scheduler using a number of different configurations.
class WithBoundScheduler : public testing::TestWithParam<SchedulerParams>
{
public:
void SetUp() override
{
auto &params = GetParam();
auto scheduler = new yarn::Scheduler();
scheduler->bind();
scheduler->setWorkerThreadCount(params.numWorkerThreads);
}
void TearDown() override
{
auto scheduler = yarn::Scheduler::get();
scheduler->unbind();
delete scheduler;
}
};
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment