Further abstract time types and functions. (#68)

* replace OSTime with Time
* replace osGetTime with timeGetTime
* replace OS_CYCLES_TO_USEC with timeMicroseconds
* replace OS_CYCLES_TO_NSEC with timeNanoseconds

also
* lastReportStart of ProfileData was used to store both cycles and microseconds, now it stores only microseconds
* fix typo: ns instead of us
This commit is contained in:
Giacomo Garbin 2024-05-28 06:59:29 +02:00 committed by GitHub
parent 855f798a18
commit 6c791e0ae0
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 39 additions and 21 deletions

View file

@ -5,6 +5,7 @@
#ifdef PORTAL64_WITH_DEBUGGER
#include "../debugger/serial.h"
#include "system/time.h"
#endif
#include <string.h>
@ -132,7 +133,7 @@ void profileTask(OSSched* scheduler, OSThread* currentThread, OSTask* task, u16*
osWritebackDCacheAll();
#ifdef PORTAL64_WITH_DEBUGGER
OSTime start = osGetTime();
Time start = timeGetTime();
#endif
osSpTaskStart(task);
OSMesg recv;
@ -142,9 +143,9 @@ void profileTask(OSSched* scheduler, OSThread* currentThread, OSTask* task, u16*
} while ((int)recv != RDP_DONE_MSG);
#ifdef PORTAL64_WITH_DEBUGGER
OSTime result = osGetTime() - start;
Time result = timeGetTime() - start;
u64 us = OS_CYCLES_TO_NSEC(result);
uint64_t ns = timeNanoseconds(result);
#endif
// wait for DP to be available
@ -163,8 +164,8 @@ void profileTask(OSSched* scheduler, OSThread* currentThread, OSTask* task, u16*
total,
curr->words.w0,
curr->words.w1,
(int)(us / 1000000),
(int)(us % 1000000)
(int)(ns / 1000000),
(int)(ns % 1000000)
);
gdbSendMessage(GDBDataTypeText, message, messageLen);
#endif

View file

@ -310,7 +310,7 @@ static void gameProc(void* arg) {
}
if (pendingGFX < 2 && drawingEnabled) {
u64 renderStart = profileStart();
Time renderStart = profileStart();
graphicsCreateTask(&gGraphicsTasks[drawBufferIndex], gSceneCallbacks->graphicsCallback, gSceneCallbacks->data);
profileEnd(renderStart, 1);
drawBufferIndex = drawBufferIndex ^ 1;
@ -325,7 +325,7 @@ static void gameProc(void* arg) {
if (inputIgnore) {
--inputIgnore;
} else {
u64 updateStart = profileStart();
Time updateStart = profileStart();
gSceneCallbacks->updateCallback(gSceneCallbacks->data);
profileEnd(updateStart, 0);
drawingEnabled = 1;

View file

@ -604,7 +604,7 @@ void sceneUpdate(struct Scene* scene) {
scene->checkpointState = SceneCheckpointStateSaved;
}
OSTime frameStart = osGetTime();
Time frameStart = timeGetTime();
scene->lastFrameTime = frameStart - scene->lastFrameStart;
if (gGameMenu.state != GameMenuStateResumeGame) {
@ -775,7 +775,7 @@ void sceneUpdate(struct Scene* scene) {
cutscenesUpdate();
scene->cpuTime = osGetTime() - frameStart;
scene->cpuTime = timeGetTime() - frameStart;
scene->lastFrameStart = frameStart;
ControllerStick freecam_stick = controllerGetStick(2);

View file

@ -65,9 +65,9 @@ struct Scene {
struct SavedPortal savedPortal;
struct Effects effects;
struct Hud hud;
OSTime cpuTime;
OSTime lastFrameStart;
OSTime lastFrameTime;
Time cpuTime;
Time lastFrameStart;
Time lastFrameTime;
u8 buttonCount;
u8 decorCount;
u8 triggerListenerCount;

View file

@ -13,6 +13,18 @@ void timeInit() {
osCreateMesgQueue(&timerQueue, &timerQueueBuf, 1);
}
Time timeGetTime() {
return (Time)(osGetTime());
}
uint64_t timeMicroseconds(Time time) {
return (uint64_t)(OS_CYCLES_TO_USEC((OSTime)(time)));
}
uint64_t timeNanoseconds(Time time) {
return (uint64_t)(OS_CYCLES_TO_NSEC((OSTime)(time)));
}
void timeUSleep(uint64_t usec) {
OSTimer timer;
OSTime countdown = OS_USEC_TO_CYCLES((u64)(usec));

View file

@ -10,7 +10,12 @@ extern float gFixedDeltaTime;
#define FRAME_SKIP 1
#define FIXED_DELTA_TIME gFixedDeltaTime
typedef uint64_t Time;
void timeInit();
Time timeGetTime();
uint64_t timeMicroseconds(Time time);
uint64_t timeNanoseconds(Time time);
void timeUSleep(uint64_t usec);
void timeUpdateDelta();
void timeSetFrameRate(int fps);

View file

@ -5,21 +5,21 @@
#endif
struct ProfileData {
u64 lastReportStart;
u64 timeAccumulation[MAX_PROFILE_BINS];
uint64_t lastReportStart;
uint64_t timeAccumulation[MAX_PROFILE_BINS];
};
struct ProfileData gProfileData;
void profileEnd(u64 startTime, int bin) {
gProfileData.timeAccumulation[bin] += OS_CYCLES_TO_USEC(osGetTime() - startTime);
void profileEnd(Time startTime, int bin) {
gProfileData.timeAccumulation[bin] += timeMicroseconds(timeGetTime() - startTime);
}
void profileReport() {
#ifdef PORTAL64_WITH_DEBUGGER
OSTime reportStartTime = osGetTime();
uint64_t reportStartTime = timeMicroseconds(timeGetTime());
gProfileData.lastReportStart = OS_CYCLES_TO_USEC(reportStartTime - gProfileData.lastReportStart);
gProfileData.lastReportStart = reportStartTime - gProfileData.lastReportStart;
// gdbSendMessage(GDBDataTypeRawBinary, (char*)&gProfileData, sizeof(struct ProfileData));
for (int i = 0; i < MAX_PROFILE_BINS; ++i) {

View file

@ -1,10 +1,10 @@
#ifndef __UTIL_PROFILE_H__
#define __UTIL_PROFILE_H__
#include <ultra64.h>
#include "system/time.h"
#define profileStart() osGetTime()
void profileEnd(u64 startTime, int bin);
#define profileStart() timeGetTime()
void profileEnd(Time startTime, int bin);
void profileReport();