From 9fbf91db06a6d4f4b5cd8bb45389a731bb86bf22 Mon Sep 17 00:00:00 2001 From: Richard Date: Sun, 13 Apr 2025 18:48:02 +0100 Subject: initial --- site/udo/twist/checkpointing_hold.udo | 196 ++++++++++++++++++++++++++++++++++ 1 file changed, 196 insertions(+) create mode 100755 site/udo/twist/checkpointing_hold.udo (limited to 'site/udo/twist/checkpointing_hold.udo') diff --git a/site/udo/twist/checkpointing_hold.udo b/site/udo/twist/checkpointing_hold.udo new file mode 100755 index 0000000..523d423 --- /dev/null +++ b/site/udo/twist/checkpointing_hold.udo @@ -0,0 +1,196 @@ +#ifndef UDO_TWIST_CHECKPOINTING +#define UDO_TWIST_CHECKPOINTING ## + +; file checkpointing broken due to WASM FS issue, but keep in memory maybe better anyway + +#ifdef TWIST_FTCHECKPOINTING + +gitwst_checkpoints[][] init imaxinstances, gitwst_maxundolevels +gitwst_checkpointstate[] init imaxinstances +gitwst_checkpointencodemult = 10000 + +opcode twst_checkpoint, 0, j + instanceindex xin + imaxundo chnget "maxundo" + imaxundo = (imaxundo == -1) ? gitwst_maxundolevels : imaxundo + if (imaxundo == 0 || imaxundo > gitwst_maxundolevels) then + goto complete + endif + + instanceindex = (instanceindex == -1) ? gitwst_instanceindex : instanceindex + icheckpointnumber = gitwst_checkpointstate[instanceindex] + + if (icheckpointnumber >= imaxundo) then + ftfree gitwst_checkpoints[instanceindex][0], 0 + index = 1 + while (index < gitwst_maxundolevels) do + gitwst_checkpoints[instanceindex][index - 1] = gitwst_checkpoints[instanceindex][index] + index += 1 + od + else + gitwst_checkpointstate[instanceindex] = icheckpointnumber + 1 + endif + + ifnL = gitwst_bufferL[instanceindex] + ifnR = gitwst_bufferR[instanceindex] + ilen = ftlen(ifnL) + + ifnCheckpointL ftgen 0, 0, ilen, 2, 0 + tableicopy ifnCheckpointL, ifnL + ifnEncodedL = ifnCheckpointL * gitwst_checkpointencodemult + if (ifnR > 0) then + ifnCheckpointR ftgen 0, 0, ilen, 2, 0 + tableicopy ifnCheckpointR, ifnR + ifnEncodedR = ifnCheckpointR / gitwst_checkpointencodemult + else + ifnEncodedR = 0 + endif + + iencoded = ifnEncodedL + ifnEncodedR + gitwst_checkpoints[instanceindex][icheckpointnumber] = iencoded + +complete: +endop + +opcode twst_undo, i, j + instanceindex xin + instanceindex = (instanceindex == -1) ? gitwst_instanceindex : instanceindex + Sloaded = "" + + icheckpointnumber = gitwst_checkpointstate[instanceindex] + + icheckpointnumber -= 1 + if (icheckpointnumber < 0) then + istatus = -1 + goto complete + endif + + icheckpointEncoded = gitwst_checkpoints[instanceindex][icheckpointnumber] + ifnCheckpointL = icheckpointEncoded / gitwst_checkpointencodemult + ifnCheckpointR = icheckpointEncoded * gitwst_checkpointencodemult + + ftfree gitwst_bufferL[instanceindex], 0 + gitwst_bufferL[instanceindex] = ifnCheckpointL + + if (ifnCheckpointR > 0) then + ftfree gitwst_bufferR[instanceindex], 0 + gitwst_bufferR[instanceindex] = ifnCheckpointR + endif + + istatus = 1 + +complete: + xout istatus +endop + +#else + +gitwst_checkpoint[][] init imaxinstances, 2 +gitwst_checkpointlengths[][] init imaxinstances, gitwst_maxundolevels +opcode twst_checkpointfilename, S, jj + instanceindex, icheckpointnumber xin + instanceindex = (instanceindex == -1) ? gitwst_instanceindex : instanceindex + icheckpointnumber = (icheckpointnumber == -1) ? gitwst_checkpoint[instanceindex][0] : icheckpointnumber + Sfile = sprintf("checkpoint-%d-%d", instanceindex, icheckpointnumber) + Sfile = strcat(host_tempdir(), strcat("/", Sfile)) + xout Sfile +endop + +opcode twst_checkpoint, 0, j + instanceindex xin + imaxundo chnget "maxundo" + if (imaxundo == 0 || imaxundo > gitwst_maxundolevels) then + goto complete + endif + + instanceindex = (instanceindex == -1) ? gitwst_instanceindex : instanceindex + icheckpointnumber = gitwst_checkpoint[instanceindex][0] + icheckpointoffset = gitwst_checkpoint[instanceindex][1] + + if (icheckpointoffset + icheckpointnumber >= imaxundo) then + Sdelete = twst_checkpointfilename(icheckpointoffset) + io_sendstring("callback", sprintf("{\"cbid\":%d,\"delete\": [\"%s\", \"%s.len\"]}", giwst_globalcbid, Sdelete, Sdelete)) + icheckpointoffset += 1 + gitwst_checkpoint[instanceindex][1] = icheckpointoffset + endif + + Sfile = twst_checkpointfilename(instanceindex) + + gitwst_checkpointlengths[instanceindex][icheckpointnumber - icheckpointoffset] = ftlen(gitwst_bufferL[instanceindex]) + /* can't use this as WASM not writing/closing files properly + ilen = ftlen(gitwst_bufferL[instanceindex]) + ihf fiopen strcat(Sfile, ".len"), 0 + fouti ihf, 0, 0, ilen + ficlose ihf* + ; nor this ;fprints strcat(Sfile, ".len"), "%d\n", ftlen(gitwst_bufferL[instanceindex]) + */ + + if (gitwst_channels[instanceindex] == 1) then + ftsave Sfile, 0, gitwst_bufferL[instanceindex] + else + ftsave Sfile, 0, gitwst_bufferL[instanceindex], gitwst_bufferR[instanceindex] + endif + gitwst_checkpoint[instanceindex][0] = icheckpointnumber + 1 +complete: +endop + +opcode twst_undo, iS, j + instanceindex xin + instanceindex = (instanceindex == -1) ? gitwst_instanceindex : instanceindex + Sloaded = "" + + icheckpointnumber = gitwst_checkpoint[instanceindex][0] + icheckpointoffset = gitwst_checkpoint[instanceindex][1] + + if (icheckpointnumber - icheckpointoffset <= 0) then + istatus = -1 + goto complete + endif + + icheckpointnumber -= 1 + gitwst_checkpoint[instanceindex][0] = icheckpointnumber + Sfile = twst_checkpointfilename(instanceindex) + + ilen = gitwst_checkpointlengths[instanceindex][icheckpointnumber - icheckpointoffset] + /* can't use this as WASM not writing/closing files properly thus checkpoint step doesn't write + SlenFile = strcat(Sfile, ".len") + if (filevalid(Sfile) != 1 || filevalid(SlenFile) != 1) then + istatus = -2 + goto complete + endif + + ilen init 0 + ihf fiopen SlenFile, 1 + fini ihf, 0, 1, ilen + ficlose ihf + */ + + if (ilen == 0) then + istatus = -3 + goto complete + endif + + if (ilen != ftlen(gitwst_bufferL[instanceindex])) then + twst_clearbuffers gitwst_bufferL[instanceindex], gitwst_bufferR[instanceindex] + gitwst_bufferL[instanceindex] ftgen 0, 0, -ilen, -2, 0 + if (gitwst_channels[instanceindex] == 2) then + gitwst_bufferR[instanceindex] ftgen 0, 0, -ilen, -2, 0 + else + gitwst_bufferR[instanceindex] = 0 + endif + endif + + if (gitwst_channels[instanceindex] == 1) then + ftload Sfile, 0, gitwst_bufferL[instanceindex] + else + ftload Sfile, 0, gitwst_bufferL[instanceindex], gitwst_bufferR[instanceindex] + endif + Sloaded = Sfile + istatus = 1 + +complete: + xout istatus, Sloaded +endop +#end + +#end \ No newline at end of file -- cgit v1.2.3