aboutsummaryrefslogtreecommitdiff
path: root/site/udo/twist/checkpointing_hold.udo
blob: 523d4239e55dcfec4ccc1505fd2e86ef2456be9a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
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