core_timing.cpp 14 KB

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