SimpleKernel 1.17.0
Loading...
Searching...
No Matches
test_environment_state.cpp
Go to the documentation of this file.
1
6
7#include <algorithm>
8#include <iostream>
9#include <stdexcept>
10
12
13namespace test_env {
14
15// 线程局部存储:每个线程指向当前测试的环境实例
17
21
25
30
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}
39
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}
52
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}
60
61auto TestEnvironmentState::GetCoreCount() const -> size_t {
62 std::lock_guard<std::mutex> lock(map_mutex_);
63 return cores_.size();
64}
65
67 size_t core_id) {
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}
75
76auto TestEnvironmentState::GetCoreIdForThread(std::thread::id tid) -> size_t {
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}
98
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}
105
107 TaskControlBlock* task) {
108 std::lock_guard<std::mutex> lock(map_mutex_);
109 context_to_task_map_[context_ptr] = task;
110}
111
113 std::lock_guard<std::mutex> lock(map_mutex_);
114 context_to_task_map_.erase(context_ptr);
115}
116
118 -> TaskControlBlock* {
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}
126
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}
143
145 -> std::vector<CoreEnvironment::SwitchEvent> {
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}
161
163 std::lock_guard<std::mutex> lock(map_mutex_);
164 for (auto& core : cores_) {
165 core.switch_history.clear();
166 }
167}
168
169} // namespace test_env
void ClearCurrentThreadEnvironment()
清除当前线程的环境实例指针
void UnregisterTaskContext(void *context_ptr)
注销任务上下文
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 层调用)
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()
设置当前线程的环境实例指针
void RegisterTaskContext(void *context_ptr, TaskControlBlock *task)
注册任务上下文,建立上下文指针到任务的映射
std::vector< CoreEnvironment > cores_
thread_local TestEnvironmentState * current_thread_env
size_t core_id
核心 ID
Definition per_cpu.hpp:1
任务控制块,管理进程/线程的核心数据结构
单个核心的环境状态