-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathAppSharedMemory.lua
354 lines (301 loc) · 10.6 KB
/
AppSharedMemory.lua
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
-- AppSharedMemory.lua
local arg = {...}
local ffi = require("ffi")
local util = require "lib_util"
local numStringLength = util.numStringLength
local shm = require "lib_shared_memory"
local mmapStatusInWait = shm.mmapStatusInWait
local mmapInRead = shm.mmapInRead
local mmapOutWrite = shm.mmapOutWrite
local ProFi = require 'ProFi'
ProFi:setGetTimeMethod( util.microSeconds )
local useProfilier=false
-- local pos = 0
local readData = "--- not read yet ---" -- this is the length of a input buffer
local readDataLen = string.len(readData)
local readData_c = util.cstr(readData)
local sendData = "--- not sent yet ---"
local sendDataLen = string.len(sendData)
local sendData_c = util.cstr(sendData)
print()
print(" -- AppSharedMemory.lua START -- ")
if util.isLinux then
print()
print(" *** AppSharedMemory.lua needs to be run with 'sudo' in Linux *** ")
print()
end
local isServer, isClient
if arg[1] == "s" then
isServer = true
elseif arg[1] == "c" then
isServer = false
else
print()
print("param 1 MUST be c(lient) or s(erver), is set to server")
print()
isServer = true --os.exit() -- not inside host app!
end
isClient = not isServer
if isServer then
print(" -- this is shared mem Server -- ")
else
print(" -- this is shared mem Client -- ")
end
print(" -- jit.version: " .. jit.version)
local arg2 = tonumber(arg[2]) or 0
print("parameter 2 (use changing send data = 1): "..arg2)
local arg2abs = math.abs(arg2)
local useSetData = arg2abs == 1 or false
local useReply = arg[3] == "r" or true --arg2abs==1 or arg2abs==4
local bigLoops = tonumber(arg[4]) or 1
local loopCount = tonumber(arg[5]) or 5000000
if arg2 < 0 then
useProfilier=true
loopCount = loopCount / 200
--ProFi:start()
end
if not jit.version then -- plain Lua
loopCount = loopCount / 100
end
local function argPrint()
if useSetData then
print("change data in every loop: TRUE")
else
print("change data in every loop: FALSE")
end
if useReply then
print("read reply (2-way communication): TRUE")
else
print("read reply (2-way communication): FALSE")
end
end
argPrint()
local fileNameTest = "TestShm_test.shm"
local fileNameS = "TestShm_srv.shm"
local fileNameC = "TestShm_cli.shm" --"AppSharedMemory-c.shm"
local sendBufferSize, readBufferSize
local i
local bufferSize = 4096 -- in windows this is 65536
local timer
local connectSleepMs = 500
local statusIdx = 0
local sentCount = 0
local readCount = 0
-- status
local base64char = {[0]='A',[1]='B',[2]='C',[3]='D',[4]='E',[5]='F',[6]='G',[7]='H',[8]='I',[9]='J',[10]='K',[11]='L',[12]='M',[13]='N',[14]='O',[15]='P',[16]='Q',[17]='R',[18]='S',[19]='T',[20]='U',[21]='V',[22]='W',[23]='X',[24]='Y',[25]='Z',[26]='a',[27]='b',[28]='c',[29]='d',[30]='e',[31]='f',[32]='g',[33]='h',[34]='i',[35]='j',[36]='k',[37]='l',[38]='m',[39]='n',[40]='o',[41]='p',[42]='q',[43]='r',[44]='s',[45]='t',[46]='u',[47]='v',[48]='w',[49]='x',[50]='y',[51]='z',[52]='0',[53]='1',[54]='2',[55]='3',[56]='4',[57]='5',[58]='6',[59]='7',[60]='8',[61]='9',[62]='-',[63]='_'}
local base64ascii = {}
for i = 0, 63 do
base64ascii[i] = string.byte(base64char[i])
--io.write(base64char[i]..base64ascii[i].." ")
end
print()
--i = shm.mmapOutCreate(fileNameTest, bufferSize)
--if i > bufferSize then bufferSize = i end -- test windows buffersize, in win7 is 65536, not 4096
--readBufferSize = shm.mmapInConnect(fileNameTest, bufferSize)
--readBufferSize = shm.mmapInDisonnect(fileNameTest)
--i = shm.mmapOutDestroy(fileNameTest)
-- both client and server will crate send file
if( isServer ) then
-- delete client send buffer
--i = shm.mmapOutCreate(fileNameC , bufferSize)
--print(" ..shm.mmapOutCreate result is: " .. i)
--i = shm.mmapOutDestroy(fileNameC)
--print(" ..shm.mmapOutCreate result is: " .. i)
sendBufferSize = shm.mmapOutCreate(fileNameS , bufferSize)
else
sendBufferSize = shm.mmapOutCreate(fileNameC , bufferSize * 2)
end
print(" ..shm.mmapOutCreate result is: " .. sendBufferSize)
if( sendBufferSize < bufferSize ) then
print("sendBufferSize < bufferSize")
os.exit() -- not inside host app!
end
-- both client and server will connect
timer = util.seconds()
repeat
if( isServer ) then
readBufferSize = shm.mmapInConnect(fileNameC, bufferSize * 2)
if util.seconds(timer) > 2 then
print(" ..server shm.mmapInConnect to "..fileNameC.." result is: " .. readBufferSize)
timer = util.seconds()
end
else
readBufferSize = shm.mmapInConnect(fileNameS, bufferSize)
if util.seconds(timer) > 2 then
print(" ..client shm.mmapInConnect to "..fileNameS.." result is: " .. readBufferSize)
timer = util.seconds()
end
end
if( readBufferSize < bufferSize ) then
util.sleep(connectSleepMs)
end
until readBufferSize > 0
local function send_data_set(loop)
if loop == 0 then -- init all
if isServer then
sendData = "srv-"
else
sendData = "cli-"
end
sendData = sendData .. string.rep("0", string.len(tostring(loopCount)))
-- loopCount is max number, fill with as many zeroes, for ex 22000 -> 00000
sendDataLen = string.len(sendData)
sendData_c = util.cstr(sendData)
readDataLen = sendDataLen
else
local loopStr = tostring(loop)
local numCount = numStringLength(loop)
for idx=1,numCount do
local cidx = sendDataLen - numCount + idx - 1
sendData_c[cidx] = string.byte(string.sub(loopStr, idx, idx))
end
end
end
local function statusIdxAdd()
statusIdx = statusIdx + 1
if statusIdx > 63 then statusIdx = 0 end
end
local status_to_wait_char
local function send_data(loop)
local doPrint = loop % (loopCount/5) == 0 or loop < 3 or loop > loopCount-2
if doPrint then
-- print(loop..". send_data() before, shm.mmapInStatus / shm.mmapOutStatus / wait: "
-- ..string.char(shm.mmapInStatus())..string.char(shm.mmapOutStatus()).." "..base64char[statusIdx])
status_to_wait_char = base64char[statusIdx]
end
mmapStatusInWait(base64ascii[statusIdx])
statusIdxAdd()
local status = mmapOutWrite(base64ascii[statusIdx], 0, sendData_c, sendDataLen )
sentCount = sentCount + 1
if doPrint then
print(loop..". sent_data, shm.mmapInStatus, shm.mmapOutStatus, wait: "..ffi.string(sendData_c, sendDataLen).." "
..string.char(shm.mmapInStatus())..string.char(shm.mmapOutStatus())..status_to_wait_char) -- ..base64char[statusIdx]
if isServer then
print()
end
end
if status ~= 0 then
print()
print("BREAK: send_data failed with error: " .. status)
end
return status
end
local function read_data(loop)
local doPrint = loop % (loopCount/5) == 0 or loop < 3 or loop > loopCount-2
if doPrint then
-- print(loop..". read_data() before, shm.mmapInStatus / shm.mmapOutStatus / wait: "
-- ..string.char(shm.mmapInStatus())..string.char(shm.mmapOutStatus()).." "..base64char[statusIdx])
status_to_wait_char = base64char[statusIdx]
end
statusIdxAdd()
mmapStatusInWait(base64ascii[statusIdx])
local status = mmapInRead(base64ascii[statusIdx], 0, readData_c, readDataLen)
readCount = readCount + 1
if doPrint then
local readDataIn = ffi.string(readData_c, readDataLen)
print(loop..". read_data, shm.mmapInStatus, shm.mmapOutStatus, wait: "..readDataIn.." "
..string.char(shm.mmapInStatus())..string.char(shm.mmapOutStatus())..status_to_wait_char) -- ..base64char[statusIdx]
if isClient then
print()
end
end
if status ~= 0 then
print()
print("BREAK: read_data failed with error: " .. status)
end
return status
end
shm.mmapAddressSet()
send_data_set(0)
shm.mmapStatusOutSet(base64ascii[0]-1) -- ascii(65-1) = "@"
print("mmapStatusInWait()")
mmapStatusInWait(base64ascii[0]-1) -- wait for other partner to set same value
util.sleep(connectSleepMs+200) -- must be bigger than connectSleepMs so that another process can catch us
-- give another time to wait base64ascii[0]-1 and set base64ascii[0]
shm.mmapStatusOutSet(base64ascii[0]) -- ascii(65) = "A"- we are ready for loop
timer = util.seconds()
if useProfilier then ProFi:start() end
-- server starts with send and does not wait last
-- client starts with reply
local bigloop = 0
--local runtime = util.seconds()
repeat
bigloop = bigloop + 1
for loop=1, loopCount do
if isServer or (useReply and loop > 1) then
if useSetData then
if isClient and useReply then
sendData_c = readData_c
else
send_data_set(loop)
end
end
if send_data(loop) ~= 0 then break end
end
if isClient or useReply then
if read_data(loop) ~= 0 then break end
--if readDataIn ~= nil and sendData ~= nil then
--if tonumber(readDataIn) ~= tonumber(sendData)-1 then io.write(" -- FAIL") end
--end
end
end
if isClient and useReply then -- after loop answer last call to server
sendData_c = readData_c
send_data(loopCount+1)
end
print()
print("*** loopCount total: "..util.format_num(bigloop * loopCount, 0))
print()
until bigloop >= bigLoops --util.seconds(runtime) >= 60*60*2 -- 5 hours -- 60*60*5
loopCount = bigloop * loopCount
if useProfilier then ProFi:stop() end
local timeUsed = util.seconds(timer)
local msgCount = sentCount + readCount
print()
print(" ..for loop=1, " .. util.format_num( loopCount, 0 ) .. " write+read time: " .. util.format_num( timeUsed, 4 ) .. " sec")
print(" ..sentCount: " .. util.format_num( sentCount, 0 ) .. ", readCount: " .. util.format_num( readCount, 0 )
.. ", messageCount: " .. util.format_num( msgCount, 0 ))
print(" ..for loop: " .. util.format_num( loopCount/timeUsed, 0 ) .. " loop / sec")
print(" ..for loop: " .. util.format_num( msgCount/timeUsed, 0 ) .. " msg / sec")
print(" ..for loop: " .. util.format_num( (timeUsed*1000*1000*1000) / msgCount, 0 ) .. " ns / msg")
print(" ..latency : " .. util.format_num( (timeUsed*1000*1000*1000) / loopCount, 0 ) .. " ns / msg")
print(" ..for loop max message len: " .. sendDataLen)
print(" ..status read wait count : " .. util.format_num( shm.mmapWaitCount(), 0 ))
argPrint()
print()
local filename
if( isServer ) then
filename = fileNameC
else
filename = fileNameS
end
i = shm.mmapInDisconnect(filename)
print(" ..shm.mmapInDisconnect("..filename..") result is: " .. i)
util.sleep(500) -- wait for another to disconnect before shm.mmapOutDestroy()
--i = shm.mmapOutFlush()
--print(" ..shm.mmapOutFlush() result is: " .. i)
if( isServer ) then
filename = fileNameS
else
filename = fileNameC
end
i = shm.mmapOutDestroy(filename)
print(" ..shm.mmapOutDestroy("..filename..") result is: " .. i)
--[[
-- delete client send file too
i = shm.mmapOutCreate(fileNameC , bufferSize)
print(" ..shm.mmapOutCreate(fileNameC , bufferSize) result is: " .. i)
i = shm.mmapOutDestroy(fileNameC)
print(" ..shm.mmapOutDestroy(fileNameC) result is: " .. i)
]]
if useProfilier then
if isServer then
ProFi:writeReport("SharedMemReport2Srv.txt")
os.execute("edit SharedMemReport2Srv.txt")
else
ProFi:writeReport("SharedMemReport2Cli.txt")
os.execute("edit SharedMemReport2Cli.txt")
end
end
print(" -- AppSharedMemory.lua end -- ")
print()