core_timing.cpp 14 KB

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