core_timing.cpp 14 KB

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