SimpleKernel 1.17.0
Loading...
Searching...
No Matches
test_environment_state.hpp
Go to the documentation of this file.
1
5#ifndef SIMPLEKERNEL_TESTS_UNIT_TEST_MOCKS_TEST_ENVIRONMENT_STATE_HPP_
6#define SIMPLEKERNEL_TESTS_UNIT_TEST_MOCKS_TEST_ENVIRONMENT_STATE_HPP_
7
8#include <cstddef>
9#include <cstdint>
10#include <mutex>
11#include <thread>
12#include <unordered_map>
13#include <vector>
14
15// 前向声明,防止循环依赖
16struct TaskControlBlock;
17struct CpuSchedData;
18
19namespace test_env {
20
25 // 核心标识
26 size_t core_id = 0;
27
28 // 中断使能状态
29 bool interrupt_enabled = true;
30
31 // 当前页表基地址
32 uint64_t page_directory = 0;
33 // 分页是否启用
34 bool paging_enabled = false;
35
36 // 上下文切换历史
37 struct SwitchEvent {
38 // 切换发生的时刻(从 sched_data->local_tick)
39 uint64_t timestamp;
40 // 源任务
42 // 目标任务
44 // 执行切换的核心
45 size_t core_id;
46 };
47 std::vector<SwitchEvent> switch_history;
48
49 // 关联的 std::thread ID
50 std::thread::id thread_id;
51
52 // 构造函数
53 explicit CoreEnvironment(size_t id = 0) : core_id(id) {}
54};
55
87 public:
90
95
101 void InitializeCores(size_t num_cores);
102
107 void ResetAllCores();
108
115 auto GetCore(size_t core_id) -> CoreEnvironment&;
116
120 auto GetCoreCount() const -> size_t;
121
128 void BindThreadToCore(std::thread::id tid, size_t core_id);
129
135 auto GetCoreIdForThread(std::thread::id tid) -> size_t;
136
142
149 void RegisterTaskContext(void* context_ptr, TaskControlBlock* task);
150
154 void UnregisterTaskContext(void* context_ptr);
155
160 auto FindTaskByContext(void* context_ptr) -> TaskControlBlock*;
161
165 void DumpAllCoreStates() const;
166
171 auto GetAllSwitchHistory() const -> std::vector<CoreEnvironment::SwitchEvent>;
172
176 void ClearSwitchHistory();
177
187
193
202
203 private:
204 // 核心环境列表(每个核心一个)
206
207 // 线程 ID 到核心 ID 的映射
208 std::unordered_map<std::thread::id, size_t> thread_to_core_map_;
209
210 // 上下文指针到任务的映射
212
213 // 保护以上数据结构的互斥锁
214 mutable std::mutex map_mutex_;
215
216 // 下一个自动分配的核心 ID
217 size_t next_core_id_ = 0;
218};
219
220} // namespace test_env
221
222#endif /* SIMPLEKERNEL_TESTS_UNIT_TEST_MOCKS_TEST_ENVIRONMENT_STATE_HPP_ */
void ClearCurrentThreadEnvironment()
清除当前线程的环境实例指针
void UnregisterTaskContext(void *context_ptr)
注销任务上下文
TestEnvironmentState(const TestEnvironmentState &)=delete
auto FindTaskByContext(void *context_ptr) -> TaskControlBlock *
通过上下文指针查找任务
auto GetCore(size_t core_id) -> CoreEnvironment &
获取指定核心的环境
std::unordered_map< std::thread::id, size_t > thread_to_core_map_
void BindThreadToCore(std::thread::id tid, size_t core_id)
将指定线程绑定到核心
void InitializeCores(size_t num_cores)
初始化指定数量的核心
auto GetAllSwitchHistory() const -> std::vector< CoreEnvironment::SwitchEvent >
获取所有核心的切换历史,按时间戳排序
static auto GetCurrentThreadEnvironment() -> TestEnvironmentState *
获取当前线程的环境实例指针(供 Mock 层调用)
TestEnvironmentState(TestEnvironmentState &&)=delete
void ClearSwitchHistory()
清空所有核心的切换历史
void DumpAllCoreStates() const
打印所有核心的状态信息
void ResetAllCores()
重置所有核心状态(中断、页表、历史记录)
std::unordered_map< void *, TaskControlBlock * > context_to_task_map_
auto GetCurrentCoreEnv() -> CoreEnvironment &
获取当前线程的核心环境
auto GetCoreIdForThread(std::thread::id tid) -> size_t
获取线程对应的核心 ID
auto GetCoreCount() const -> size_t
获取核心数量
void SetCurrentThreadEnvironment()
设置当前线程的环境实例指针
auto operator=(const TestEnvironmentState &) -> TestEnvironmentState &=delete
void RegisterTaskContext(void *context_ptr, TaskControlBlock *task)
注册任务上下文,建立上下文指针到任务的映射
auto operator=(TestEnvironmentState &&) -> TestEnvironmentState &=delete
std::vector< CoreEnvironment > cores_
size_t core_id
核心 ID
Definition per_cpu.hpp:1
每个核心的调度数据 (RunQueue)
任务控制块,管理进程/线程的核心数据结构
单个核心的环境状态
std::vector< SwitchEvent > switch_history