core_timing.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553
  1. // Copyright (c) 2012- PPSSPP Project / Dolphin Project.
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <atomic>
  5. #include <cinttypes>
  6. #include <mutex>
  7. #include <vector>
  8. #include "common/chunk_file.h"
  9. #include "common/logging/log.h"
  10. #include "common/string_util.h"
  11. #include "core/arm/arm_interface.h"
  12. #include "core/core.h"
  13. #include "core/core_timing.h"
  14. int g_clock_rate_arm11 = 268123480;
  15. // is this really necessary?
  16. #define INITIAL_SLICE_LENGTH 20000
  17. #define MAX_SLICE_LENGTH 100000000
  18. namespace CoreTiming {
  19. struct EventType {
  20. EventType() {}
  21. EventType(TimedCallback cb, const char* n) : callback(cb), name(n) {}
  22. TimedCallback callback;
  23. const char* name;
  24. };
  25. static std::vector<EventType> event_types;
  26. struct BaseEvent {
  27. s64 time;
  28. u64 userdata;
  29. int type;
  30. };
  31. typedef LinkedListItem<BaseEvent> Event;
  32. static Event* first;
  33. static Event* ts_first;
  34. static Event* ts_last;
  35. // event pools
  36. static Event* event_pool = nullptr;
  37. static Event* event_ts_pool = nullptr;
  38. static int allocated_ts_events = 0;
  39. // Optimization to skip MoveEvents when possible.
  40. static std::atomic<bool> has_ts_events(false);
  41. int g_slice_length;
  42. static s64 global_timer;
  43. static s64 idled_cycles;
  44. static s64 last_global_time_ticks;
  45. static s64 last_global_time_us;
  46. static std::recursive_mutex external_event_section;
  47. // Warning: not included in save state.
  48. using AdvanceCallback = void(int cycles_executed);
  49. static AdvanceCallback* advance_callback = nullptr;
  50. static std::vector<MHzChangeCallback> mhz_change_callbacks;
  51. static void FireMhzChange() {
  52. for (auto callback : mhz_change_callbacks)
  53. callback();
  54. }
  55. void SetClockFrequencyMHz(int cpu_mhz) {
  56. // When the mhz changes, we keep track of what "time" it was before hand.
  57. // This way, time always moves forward, even if mhz is changed.
  58. last_global_time_us = GetGlobalTimeUs();
  59. last_global_time_ticks = GetTicks();
  60. g_clock_rate_arm11 = cpu_mhz * 1000000;
  61. // TODO: Rescale times of scheduled events?
  62. FireMhzChange();
  63. }
  64. int GetClockFrequencyMHz() {
  65. return g_clock_rate_arm11 / 1000000;
  66. }
  67. u64 GetGlobalTimeUs() {
  68. s64 ticks_since_last = GetTicks() - last_global_time_ticks;
  69. int freq = GetClockFrequencyMHz();
  70. s64 us_since_last = ticks_since_last / freq;
  71. return last_global_time_us + us_since_last;
  72. }
  73. static Event* GetNewEvent() {
  74. if (!event_pool)
  75. return new Event;
  76. Event* event = event_pool;
  77. event_pool = event->next;
  78. return event;
  79. }
  80. static Event* GetNewTsEvent() {
  81. allocated_ts_events++;
  82. if (!event_ts_pool)
  83. return new Event;
  84. Event* event = event_ts_pool;
  85. event_ts_pool = event->next;
  86. return event;
  87. }
  88. static void FreeEvent(Event* event) {
  89. event->next = event_pool;
  90. event_pool = event;
  91. }
  92. static void FreeTsEvent(Event* event) {
  93. event->next = event_ts_pool;
  94. event_ts_pool = event;
  95. allocated_ts_events--;
  96. }
  97. int RegisterEvent(const char* name, TimedCallback callback) {
  98. event_types.emplace_back(callback, name);
  99. return (int)event_types.size() - 1;
  100. }
  101. static void AntiCrashCallback(u64 userdata, int cycles_late) {
  102. LOG_CRITICAL(Core_Timing, "Savestate broken: an unregistered event was called.");
  103. Core::Halt("invalid timing events");
  104. }
  105. void RestoreRegisterEvent(int event_type, const char* name, TimedCallback callback) {
  106. if (event_type >= (int)event_types.size())
  107. event_types.resize(event_type + 1, EventType(AntiCrashCallback, "INVALID EVENT"));
  108. event_types[event_type] = EventType(callback, name);
  109. }
  110. void UnregisterAllEvents() {
  111. if (first)
  112. LOG_ERROR(Core_Timing, "Cannot unregister events with events pending");
  113. event_types.clear();
  114. }
  115. void Init() {
  116. Core::g_app_core->down_count = INITIAL_SLICE_LENGTH;
  117. g_slice_length = INITIAL_SLICE_LENGTH;
  118. global_timer = 0;
  119. idled_cycles = 0;
  120. last_global_time_ticks = 0;
  121. last_global_time_us = 0;
  122. has_ts_events = 0;
  123. mhz_change_callbacks.clear();
  124. first = nullptr;
  125. ts_first = nullptr;
  126. ts_last = nullptr;
  127. event_pool = nullptr;
  128. event_ts_pool = nullptr;
  129. allocated_ts_events = 0;
  130. advance_callback = nullptr;
  131. }
  132. void Shutdown() {
  133. MoveEvents();
  134. ClearPendingEvents();
  135. UnregisterAllEvents();
  136. while (event_pool) {
  137. Event* event = event_pool;
  138. event_pool = event->next;
  139. delete event;
  140. }
  141. std::lock_guard<std::recursive_mutex> lock(external_event_section);
  142. while (event_ts_pool) {
  143. Event* event = event_ts_pool;
  144. event_ts_pool = event->next;
  145. delete event;
  146. }
  147. }
  148. u64 GetTicks() {
  149. return (u64)global_timer + g_slice_length - Core::g_app_core->down_count;
  150. }
  151. u64 GetIdleTicks() {
  152. return (u64)idled_cycles;
  153. }
  154. // This is to be called when outside threads, such as the graphics thread, wants to
  155. // schedule things to be executed on the main thread.
  156. void ScheduleEvent_Threadsafe(s64 cycles_into_future, int event_type, u64 userdata) {
  157. std::lock_guard<std::recursive_mutex> lock(external_event_section);
  158. Event* new_event = GetNewTsEvent();
  159. new_event->time = GetTicks() + cycles_into_future;
  160. new_event->type = event_type;
  161. new_event->next = nullptr;
  162. new_event->userdata = userdata;
  163. if (!ts_first)
  164. ts_first = new_event;
  165. if (ts_last)
  166. ts_last->next = new_event;
  167. ts_last = new_event;
  168. has_ts_events = true;
  169. }
  170. // Same as ScheduleEvent_Threadsafe(0, ...) EXCEPT if we are already on the CPU thread
  171. // in which case the event will get handled immediately, before returning.
  172. void ScheduleEvent_Threadsafe_Immediate(int event_type, u64 userdata) {
  173. if (false) // Core::IsCPUThread())
  174. {
  175. std::lock_guard<std::recursive_mutex> lock(external_event_section);
  176. event_types[event_type].callback(userdata, 0);
  177. } else
  178. ScheduleEvent_Threadsafe(0, event_type, userdata);
  179. }
  180. void ClearPendingEvents() {
  181. while (first) {
  182. Event* event = first->next;
  183. FreeEvent(first);
  184. first = event;
  185. }
  186. }
  187. static void AddEventToQueue(Event* new_event) {
  188. Event* prev_event = nullptr;
  189. Event** next_event = &first;
  190. for (;;) {
  191. Event*& next = *next_event;
  192. if (!next || new_event->time < next->time) {
  193. new_event->next = next;
  194. next = new_event;
  195. break;
  196. }
  197. prev_event = next;
  198. next_event = &prev_event->next;
  199. }
  200. }
  201. void ScheduleEvent(s64 cycles_into_future, int event_type, u64 userdata) {
  202. Event* new_event = GetNewEvent();
  203. new_event->userdata = userdata;
  204. new_event->type = event_type;
  205. new_event->time = GetTicks() + cycles_into_future;
  206. AddEventToQueue(new_event);
  207. }
  208. s64 UnscheduleEvent(int event_type, u64 userdata) {
  209. s64 result = 0;
  210. if (!first)
  211. return result;
  212. while (first) {
  213. if (first->type == event_type && first->userdata == userdata) {
  214. result = first->time - GetTicks();
  215. Event* next = first->next;
  216. FreeEvent(first);
  217. first = next;
  218. } else {
  219. break;
  220. }
  221. }
  222. if (!first)
  223. return result;
  224. Event* prev_event = first;
  225. Event* ptr = prev_event->next;
  226. while (ptr) {
  227. if (ptr->type == event_type && ptr->userdata == userdata) {
  228. result = ptr->time - GetTicks();
  229. prev_event->next = ptr->next;
  230. FreeEvent(ptr);
  231. ptr = prev_event->next;
  232. } else {
  233. prev_event = ptr;
  234. ptr = ptr->next;
  235. }
  236. }
  237. return result;
  238. }
  239. s64 UnscheduleThreadsafeEvent(int event_type, u64 userdata) {
  240. s64 result = 0;
  241. std::lock_guard<std::recursive_mutex> lock(external_event_section);
  242. if (!ts_first)
  243. return result;
  244. while (ts_first) {
  245. if (ts_first->type == event_type && ts_first->userdata == userdata) {
  246. result = ts_first->time - GetTicks();
  247. Event* next = ts_first->next;
  248. FreeTsEvent(ts_first);
  249. ts_first = next;
  250. } else {
  251. break;
  252. }
  253. }
  254. if (!ts_first) {
  255. ts_last = nullptr;
  256. return result;
  257. }
  258. Event* prev_event = ts_first;
  259. Event* next = prev_event->next;
  260. while (next) {
  261. if (next->type == event_type && next->userdata == userdata) {
  262. result = next->time - GetTicks();
  263. prev_event->next = next->next;
  264. if (next == ts_last)
  265. ts_last = prev_event;
  266. FreeTsEvent(next);
  267. next = prev_event->next;
  268. } else {
  269. prev_event = next;
  270. next = next->next;
  271. }
  272. }
  273. return result;
  274. }
  275. // Warning: not included in save state.
  276. void RegisterAdvanceCallback(AdvanceCallback* callback) {
  277. advance_callback = callback;
  278. }
  279. void RegisterMHzChangeCallback(MHzChangeCallback callback) {
  280. mhz_change_callbacks.push_back(callback);
  281. }
  282. bool IsScheduled(int event_type) {
  283. if (!first)
  284. return false;
  285. Event* event = first;
  286. while (event) {
  287. if (event->type == event_type)
  288. return true;
  289. event = event->next;
  290. }
  291. return false;
  292. }
  293. void RemoveEvent(int event_type) {
  294. if (!first)
  295. return;
  296. while (first) {
  297. if (first->type == event_type) {
  298. Event* next = first->next;
  299. FreeEvent(first);
  300. first = next;
  301. } else {
  302. break;
  303. }
  304. }
  305. if (!first)
  306. return;
  307. Event* prev = first;
  308. Event* next = prev->next;
  309. while (next) {
  310. if (next->type == event_type) {
  311. prev->next = next->next;
  312. FreeEvent(next);
  313. next = prev->next;
  314. } else {
  315. prev = next;
  316. next = next->next;
  317. }
  318. }
  319. }
  320. void RemoveThreadsafeEvent(int event_type) {
  321. std::lock_guard<std::recursive_mutex> lock(external_event_section);
  322. if (!ts_first)
  323. return;
  324. while (ts_first) {
  325. if (ts_first->type == event_type) {
  326. Event* next = ts_first->next;
  327. FreeTsEvent(ts_first);
  328. ts_first = next;
  329. } else {
  330. break;
  331. }
  332. }
  333. if (!ts_first) {
  334. ts_last = nullptr;
  335. return;
  336. }
  337. Event* prev = ts_first;
  338. Event* next = prev->next;
  339. while (next) {
  340. if (next->type == event_type) {
  341. prev->next = next->next;
  342. if (next == ts_last)
  343. ts_last = prev;
  344. FreeTsEvent(next);
  345. next = prev->next;
  346. } else {
  347. prev = next;
  348. next = next->next;
  349. }
  350. }
  351. }
  352. void RemoveAllEvents(int event_type) {
  353. RemoveThreadsafeEvent(event_type);
  354. RemoveEvent(event_type);
  355. }
  356. // This raise only the events required while the fifo is processing data
  357. void ProcessFifoWaitEvents() {
  358. while (first) {
  359. if (first->time <= (s64)GetTicks()) {
  360. Event* evt = first;
  361. first = first->next;
  362. event_types[evt->type].callback(evt->userdata, (int)(GetTicks() - evt->time));
  363. FreeEvent(evt);
  364. } else {
  365. break;
  366. }
  367. }
  368. }
  369. void MoveEvents() {
  370. has_ts_events = false;
  371. std::lock_guard<std::recursive_mutex> lock(external_event_section);
  372. // Move events from async queue into main queue
  373. while (ts_first) {
  374. Event* next = ts_first->next;
  375. AddEventToQueue(ts_first);
  376. ts_first = next;
  377. }
  378. ts_last = nullptr;
  379. // Move free events to threadsafe pool
  380. while (allocated_ts_events > 0 && event_pool) {
  381. Event* event = event_pool;
  382. event_pool = event->next;
  383. event->next = event_ts_pool;
  384. event_ts_pool = event;
  385. allocated_ts_events--;
  386. }
  387. }
  388. void ForceCheck() {
  389. s64 cycles_executed = g_slice_length - Core::g_app_core->down_count;
  390. global_timer += cycles_executed;
  391. // This will cause us to check for new events immediately.
  392. Core::g_app_core->down_count = 0;
  393. // But let's not eat a bunch more time in Advance() because of this.
  394. g_slice_length = 0;
  395. }
  396. void Advance() {
  397. s64 cycles_executed = g_slice_length - Core::g_app_core->down_count;
  398. global_timer += cycles_executed;
  399. Core::g_app_core->down_count = g_slice_length;
  400. if (has_ts_events)
  401. MoveEvents();
  402. ProcessFifoWaitEvents();
  403. if (!first) {
  404. if (g_slice_length < 10000) {
  405. g_slice_length += 10000;
  406. Core::g_app_core->down_count += g_slice_length;
  407. }
  408. } else {
  409. // Note that events can eat cycles as well.
  410. int target = (int)(first->time - global_timer);
  411. if (target > MAX_SLICE_LENGTH)
  412. target = MAX_SLICE_LENGTH;
  413. const int diff = target - g_slice_length;
  414. g_slice_length += diff;
  415. Core::g_app_core->down_count += diff;
  416. }
  417. if (advance_callback)
  418. advance_callback(static_cast<int>(cycles_executed));
  419. }
  420. void LogPendingEvents() {
  421. Event* event = first;
  422. while (event) {
  423. // LOG_TRACE(Core_Timing, "PENDING: Now: %lld Pending: %lld Type: %d", globalTimer,
  424. // next->time, next->type);
  425. event = event->next;
  426. }
  427. }
  428. void Idle(int max_idle) {
  429. s64 cycles_down = Core::g_app_core->down_count;
  430. if (max_idle != 0 && cycles_down > max_idle)
  431. cycles_down = max_idle;
  432. if (first && cycles_down > 0) {
  433. s64 cycles_executed = g_slice_length - Core::g_app_core->down_count;
  434. s64 cycles_next_event = first->time - global_timer;
  435. if (cycles_next_event < cycles_executed + cycles_down) {
  436. cycles_down = cycles_next_event - cycles_executed;
  437. // Now, now... no time machines, please.
  438. if (cycles_down < 0)
  439. cycles_down = 0;
  440. }
  441. }
  442. LOG_TRACE(Core_Timing, "Idle for %" PRId64 " cycles! (%f ms)", cycles_down,
  443. cycles_down / (float)(g_clock_rate_arm11 * 0.001f));
  444. idled_cycles += cycles_down;
  445. Core::g_app_core->down_count -= cycles_down;
  446. if (Core::g_app_core->down_count == 0)
  447. Core::g_app_core->down_count = -1;
  448. }
  449. std::string GetScheduledEventsSummary() {
  450. Event* event = first;
  451. std::string text = "Scheduled events\n";
  452. text.reserve(1000);
  453. while (event) {
  454. unsigned int t = event->type;
  455. if (t >= event_types.size())
  456. LOG_ERROR(Core_Timing, "Invalid event type"); // %i", t);
  457. const char* name = event_types[event->type].name;
  458. if (!name)
  459. name = "[unknown]";
  460. text += Common::StringFromFormat("%s : %i %08x%08x\n", name, (int)event->time,
  461. (u32)(event->userdata >> 32), (u32)(event->userdata));
  462. event = event->next;
  463. }
  464. return text;
  465. }
  466. } // namespace