core_timing.cpp 14 KB

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