SimpleKernel 1.17.0
Loading...
Searching...
No Matches
test_env::TestEnvironmentState Class Reference

测试环境状态 More...

#include <test_environment_state.hpp>

Collaboration diagram for test_env::TestEnvironmentState:
Collaboration graph

Public Member Functions

 TestEnvironmentState ()=default
 
 ~TestEnvironmentState ()=default
 
 TestEnvironmentState (const TestEnvironmentState &)=delete
 
 TestEnvironmentState (TestEnvironmentState &&)=delete
 
auto operator= (const TestEnvironmentState &) -> TestEnvironmentState &=delete
 
auto operator= (TestEnvironmentState &&) -> TestEnvironmentState &=delete
 
void InitializeCores (size_t num_cores)
 初始化指定数量的核心
 
void ResetAllCores ()
 重置所有核心状态(中断、页表、历史记录)
 
auto GetCore (size_t core_id) -> CoreEnvironment &
 获取指定核心的环境
 
auto GetCoreCount () const -> size_t
 获取核心数量
 
void BindThreadToCore (std::thread::id tid, size_t core_id)
 将指定线程绑定到核心
 
auto GetCoreIdForThread (std::thread::id tid) -> size_t
 获取线程对应的核心 ID
 
auto GetCurrentCoreEnv () -> CoreEnvironment &
 获取当前线程的核心环境
 
void RegisterTaskContext (void *context_ptr, TaskControlBlock *task)
 注册任务上下文,建立上下文指针到任务的映射
 
void UnregisterTaskContext (void *context_ptr)
 注销任务上下文
 
auto FindTaskByContext (void *context_ptr) -> TaskControlBlock *
 通过上下文指针查找任务
 
void DumpAllCoreStates () const
 打印所有核心的状态信息
 
auto GetAllSwitchHistory () const -> std::vector< CoreEnvironment::SwitchEvent >
 获取所有核心的切换历史,按时间戳排序
 
void ClearSwitchHistory ()
 清空所有核心的切换历史
 
void SetCurrentThreadEnvironment ()
 设置当前线程的环境实例指针
 
void ClearCurrentThreadEnvironment ()
 清除当前线程的环境实例指针
 

Static Public Member Functions

static auto GetCurrentThreadEnvironment () -> TestEnvironmentState *
 获取当前线程的环境实例指针(供 Mock 层调用)
 

Private Attributes

std::vector< CoreEnvironmentcores_
 
std::unordered_map< std::thread::id, size_t > thread_to_core_map_
 
std::unordered_map< void *, TaskControlBlock * > context_to_task_map_
 
std::mutex map_mutex_
 
size_t next_core_id_ = 0
 

Detailed Description

测试环境状态

设计说明:

  • 每个测试 Fixture 持有自己独立的 TestEnvironmentState 实例
  • 通过线程局部指针 (thread_local) 让 Mock 层能访问当前测试的环境
  • 这样既保证了测试隔离,又让 Mock 层无需显式传递环境参数

工作流程:

  1. 测试 Fixture 创建 TestEnvironmentState 实例(成员变量)
  2. SetUp() 中调用 SetCurrentThreadEnvironment() 设置线程局部指针
  3. Mock 层通过 GetCurrentThreadEnvironment() 获取当前环境
  4. TearDown() 中调用 ClearCurrentThreadEnvironment() 清理

示例:

class MyTest : public ::testing::Test {
protected:
void SetUp() override {
env_.InitializeCores(2);
env_.SetCurrentThreadEnvironment(); // 关键:设置线程局部指针
env_.BindThreadToCore(std::this_thread::get_id(), 0);
}
void TearDown() override {
env_.ClearCurrentThreadEnvironment(); // 清理
}
TestEnvironmentState env_; // 每个测试独立的环境
};

Definition at line 86 of file test_environment_state.hpp.

Constructor & Destructor Documentation

◆ TestEnvironmentState() [1/3]

test_env::TestEnvironmentState::TestEnvironmentState ( )
default

◆ ~TestEnvironmentState()

test_env::TestEnvironmentState::~TestEnvironmentState ( )
default

◆ TestEnvironmentState() [2/3]

test_env::TestEnvironmentState::TestEnvironmentState ( const TestEnvironmentState )
delete

◆ TestEnvironmentState() [3/3]

test_env::TestEnvironmentState::TestEnvironmentState ( TestEnvironmentState &&  )
delete

Member Function Documentation

◆ BindThreadToCore()

void test_env::TestEnvironmentState::BindThreadToCore ( std::thread::id  tid,
size_t  core_id 
)

将指定线程绑定到核心

Parameters
tid线程 ID
core_id核心 ID
Note
用于多核测试,每个工作线程需要绑定到不同核心

Definition at line 66 of file test_environment_state.cpp.

67 {
68 std::lock_guard<std::mutex> lock(map_mutex_);
69 if (core_id >= cores_.size()) {
70 throw std::out_of_range("Invalid core_id: " + std::to_string(core_id));
71 }
73 cores_[core_id].thread_id = tid;
74}
std::unordered_map< std::thread::id, size_t > thread_to_core_map_
std::vector< CoreEnvironment > cores_
size_t core_id
核心 ID
Definition per_cpu.hpp:1
Here is the caller graph for this function:

◆ ClearCurrentThreadEnvironment()

void test_env::TestEnvironmentState::ClearCurrentThreadEnvironment ( )

清除当前线程的环境实例指针

Note
应在 TearDown() 中调用

Definition at line 22 of file test_environment_state.cpp.

22 {
23 current_thread_env = nullptr;
24}
thread_local TestEnvironmentState * current_thread_env
Here is the caller graph for this function:

◆ ClearSwitchHistory()

void test_env::TestEnvironmentState::ClearSwitchHistory ( )

清空所有核心的切换历史

Definition at line 162 of file test_environment_state.cpp.

162 {
163 std::lock_guard<std::mutex> lock(map_mutex_);
164 for (auto& core : cores_) {
165 core.switch_history.clear();
166 }
167}
Here is the caller graph for this function:

◆ DumpAllCoreStates()

void test_env::TestEnvironmentState::DumpAllCoreStates ( ) const

打印所有核心的状态信息

Definition at line 127 of file test_environment_state.cpp.

127 {
128 std::lock_guard<std::mutex> lock(map_mutex_);
129 std::cout << "\n=== Test Environment State Dump ===" << std::endl;
130 std::cout << "Total cores: " << cores_.size() << std::endl;
131
132 for (const auto& core : cores_) {
133 std::cout << "\nCore " << core.core_id << ":" << std::endl;
134 std::cout << " Interrupt enabled: " << core.interrupt_enabled << std::endl;
135 std::cout << " Page directory: 0x" << std::hex << core.page_directory
136 << std::dec << std::endl;
137 std::cout << " Paging enabled: " << core.paging_enabled << std::endl;
138 std::cout << " Switch history size: " << core.switch_history.size()
139 << std::endl;
140 }
141 std::cout << "==================================\n" << std::endl;
142}

◆ FindTaskByContext()

auto test_env::TestEnvironmentState::FindTaskByContext ( void *  context_ptr) -> TaskControlBlock*

通过上下文指针查找任务

Returns
任务指针,未找到返回 nullptr

Definition at line 117 of file test_environment_state.cpp.

118 {
119 std::lock_guard<std::mutex> lock(map_mutex_);
120 auto it = context_to_task_map_.find(context_ptr);
121 if (it != context_to_task_map_.end()) {
122 return it->second;
123 }
124 return nullptr;
125}
std::unordered_map< void *, TaskControlBlock * > context_to_task_map_

◆ GetAllSwitchHistory()

auto test_env::TestEnvironmentState::GetAllSwitchHistory ( ) const -> std::vector<CoreEnvironment::SwitchEvent>

获取所有核心的切换历史,按时间戳排序

Returns
切换事件列表

Definition at line 144 of file test_environment_state.cpp.

145 {
146 std::lock_guard<std::mutex> lock(map_mutex_);
147 std::vector<CoreEnvironment::SwitchEvent> all_events;
148
149 for (const auto& core : cores_) {
150 all_events.insert(all_events.end(), core.switch_history.begin(),
151 core.switch_history.end());
152 }
153
154 // 按时间戳排序
155 std::sort(
156 all_events.begin(), all_events.end(),
157 [](const auto& a, const auto& b) { return a.timestamp < b.timestamp; });
158
159 return all_events;
160}

◆ GetCore()

auto test_env::TestEnvironmentState::GetCore ( size_t  core_id) -> CoreEnvironment&

获取指定核心的环境

Parameters
core_id核心 ID
Returns
核心环境引用
Exceptions
std::out_of_range如果 core_id 无效

Definition at line 53 of file test_environment_state.cpp.

53 {
54 std::lock_guard<std::mutex> lock(map_mutex_);
55 if (core_id >= cores_.size()) {
56 throw std::out_of_range("Invalid core_id: " + std::to_string(core_id));
57 }
58 return cores_[core_id];
59}

◆ GetCoreCount()

auto test_env::TestEnvironmentState::GetCoreCount ( ) const -> size_t

获取核心数量

Definition at line 61 of file test_environment_state.cpp.

61 {
62 std::lock_guard<std::mutex> lock(map_mutex_);
63 return cores_.size();
64}

◆ GetCoreIdForThread()

auto test_env::TestEnvironmentState::GetCoreIdForThread ( std::thread::id  tid) -> size_t

获取线程对应的核心 ID

Parameters
tid线程 ID
Returns
核心 ID,如果未绑定则自动分配

Definition at line 76 of file test_environment_state.cpp.

76 {
77 std::lock_guard<std::mutex> lock(map_mutex_);
78 auto it = thread_to_core_map_.find(tid);
79 if (it != thread_to_core_map_.end()) {
80 return it->second;
81 }
82
83 // 自动分配一个核心 ID (兼容旧行为)
84 if (next_core_id_ >= cores_.size()) {
85 // 如果核心未初始化,默认创建单核环境
86 if (cores_.empty()) {
87 cores_.emplace_back(0);
88 }
89 next_core_id_ = 0;
90 }
91
92 size_t new_id = next_core_id_;
93 thread_to_core_map_[tid] = new_id;
94 cores_[new_id].thread_id = tid;
95 next_core_id_ = (next_core_id_ + 1) % cores_.size();
96 return new_id;
97}

◆ GetCurrentCoreEnv()

auto test_env::TestEnvironmentState::GetCurrentCoreEnv ( ) -> CoreEnvironment&

获取当前线程的核心环境

Returns
当前线程对应的核心环境引用

Definition at line 99 of file test_environment_state.cpp.

99 {
100 auto tid = std::this_thread::get_id();
101 size_t core_id = GetCoreIdForThread(tid);
102 std::lock_guard<std::mutex> lock(map_mutex_);
103 return cores_[core_id];
104}
auto GetCoreIdForThread(std::thread::id tid) -> size_t
获取线程对应的核心 ID

◆ GetCurrentThreadEnvironment()

auto test_env::TestEnvironmentState::GetCurrentThreadEnvironment ( ) -> TestEnvironmentState*
static

获取当前线程的环境实例指针(供 Mock 层调用)

Returns
环境指针,如果未设置则返回 nullptr

用法:Mock 层(cpu_io.h、arch.cpp)通过这个函数获取当前测试的环境 例如:auto* env = TestEnvironmentState::GetCurrentThreadEnvironment();

Definition at line 26 of file test_environment_state.cpp.

27 {
28 return current_thread_env;
29}
Here is the caller graph for this function:

◆ InitializeCores()

void test_env::TestEnvironmentState::InitializeCores ( size_t  num_cores)

初始化指定数量的核心

Parameters
num_cores核心数量
Note
通常在 SetUp() 中调用

Definition at line 31 of file test_environment_state.cpp.

31 {
32 std::lock_guard<std::mutex> lock(map_mutex_);
33 cores_.clear();
34 cores_.reserve(num_cores);
35 for (size_t i = 0; i < num_cores; ++i) {
36 cores_.emplace_back(i);
37 }
38}
Here is the caller graph for this function:

◆ operator=() [1/2]

auto test_env::TestEnvironmentState::operator= ( const TestEnvironmentState ) -> TestEnvironmentState &=delete
delete

◆ operator=() [2/2]

auto test_env::TestEnvironmentState::operator= ( TestEnvironmentState &&  ) -> TestEnvironmentState &=delete
delete

◆ RegisterTaskContext()

void test_env::TestEnvironmentState::RegisterTaskContext ( void *  context_ptr,
TaskControlBlock task 
)

注册任务上下文,建立上下文指针到任务的映射

Parameters
context_ptr任务的上下文指针(&task.task_context)
task任务指针
Note
switch_to 需要通过上下文指针找到对应任务

Definition at line 106 of file test_environment_state.cpp.

107 {
108 std::lock_guard<std::mutex> lock(map_mutex_);
109 context_to_task_map_[context_ptr] = task;
110}

◆ ResetAllCores()

void test_env::TestEnvironmentState::ResetAllCores ( )

重置所有核心状态(中断、页表、历史记录)

Note
不会改变核心数量,只重置状态

Definition at line 40 of file test_environment_state.cpp.

40 {
41 std::lock_guard<std::mutex> lock(map_mutex_);
42 for (auto& core : cores_) {
43 core.interrupt_enabled = true;
44 core.page_directory = 0;
45 core.paging_enabled = false;
46 core.switch_history.clear();
47 }
48 thread_to_core_map_.clear();
50 next_core_id_ = 0;
51}

◆ SetCurrentThreadEnvironment()

void test_env::TestEnvironmentState::SetCurrentThreadEnvironment ( )

设置当前线程的环境实例指针

Note
必须在 SetUp() 中调用,让 Mock 层能访问这个环境

原理:设置一个 thread_local 指针指向 this 效果:同一线程中,Mock 层可以通过 GetCurrentThreadEnvironment() 获取这个环境

Definition at line 18 of file test_environment_state.cpp.

18 {
19 current_thread_env = this;
20}
Here is the caller graph for this function:

◆ UnregisterTaskContext()

void test_env::TestEnvironmentState::UnregisterTaskContext ( void *  context_ptr)

注销任务上下文

Definition at line 112 of file test_environment_state.cpp.

112 {
113 std::lock_guard<std::mutex> lock(map_mutex_);
114 context_to_task_map_.erase(context_ptr);
115}

Member Data Documentation

◆ context_to_task_map_

std::unordered_map<void*, TaskControlBlock*> test_env::TestEnvironmentState::context_to_task_map_
private

Definition at line 211 of file test_environment_state.hpp.

◆ cores_

std::vector<CoreEnvironment> test_env::TestEnvironmentState::cores_
private

Definition at line 205 of file test_environment_state.hpp.

◆ map_mutex_

std::mutex test_env::TestEnvironmentState::map_mutex_
mutableprivate

Definition at line 214 of file test_environment_state.hpp.

◆ next_core_id_

size_t test_env::TestEnvironmentState::next_core_id_ = 0
private

Definition at line 217 of file test_environment_state.hpp.

◆ thread_to_core_map_

std::unordered_map<std::thread::id, size_t> test_env::TestEnvironmentState::thread_to_core_map_
private

Definition at line 208 of file test_environment_state.hpp.


The documentation for this class was generated from the following files: