Skip to content

Commit 7461588

Browse files
committed
profile I/O in lock
Signed-off-by: KantaTamura <[email protected]>
1 parent 68b68a4 commit 7461588

File tree

2 files changed

+23
-13
lines changed

2 files changed

+23
-13
lines changed

pfio/cache/file_cache.py

+11-6
Original file line numberDiff line numberDiff line change
@@ -234,12 +234,14 @@ def _get(self, i):
234234

235235
offset = self.buflen * i
236236
with self.lock.rdlock(), record("pfio.cache.file:get:lock", trace=self.trace):
237-
buf = os.pread(self.cachefp.fileno(), self.buflen, offset)
237+
with record("pfio.cache.file:get:read_index", trace=self.trace):
238+
buf = os.pread(self.cachefp.fileno(), self.buflen, offset)
238239
(o, l) = unpack('Qq', buf)
239240
if l < 0 or o < 0:
240241
return None
241242

242-
data = os.pread(self.cachefp.fileno(), l, o)
243+
with record("pfio.cache.file:get:read_data", trace=self.trace):
244+
data = os.pread(self.cachefp.fileno(), l, o)
243245
assert len(data) == l
244246
return data
245247

@@ -276,7 +278,8 @@ def _put(self, i, data):
276278

277279
offset = self.buflen * i
278280
with self.lock.wrlock(), record("pfio.cache.file:put:lock", trace=self.trace):
279-
buf = os.pread(self.cachefp.fileno(), self.buflen, offset)
281+
with record("pfio.cache.file:put:read_index", trace=self.trace):
282+
buf = os.pread(self.cachefp.fileno(), self.buflen, offset)
280283
(o, l) = unpack('Qq', buf)
281284
if l >= 0 and o >= 0:
282285
# Already data exists
@@ -301,13 +304,15 @@ def _put(self, i, data):
301304
302305
'''
303306
buf = pack('Qq', pos, len(data))
304-
r = os.pwrite(self.cachefp.fileno(), buf, offset)
307+
with record("pfio.cache.file:put:write_index", trace=self.trace):
308+
r = os.pwrite(self.cachefp.fileno(), buf, offset)
305309
assert r == self.buflen
306310

307311
current_pos = pos
308312
while current_pos - pos < len(data):
309-
r = os.pwrite(self.cachefp.fileno(),
310-
data[current_pos-pos:], current_pos)
313+
with record("pfio.cache.file:put:write_data", trace=self.trace):
314+
r = os.pwrite(self.cachefp.fileno(),
315+
data[current_pos-pos:], current_pos)
311316
assert r > 0
312317
current_pos += r
313318
assert current_pos - pos == len(data)

pfio/cache/multiprocess_file_cache.py

+12-7
Original file line numberDiff line numberDiff line change
@@ -234,13 +234,14 @@ def _get(self, i):
234234

235235
fcntl.flock(self.cache_fd, fcntl.LOCK_SH)
236236
with record(f"pfio.cache.multiprocessfile:get:lock-{self.cache_fd}", trace=self.trace):
237-
index_entry = os.pread(self.cache_fd, self.buflen, offset)
237+
with record("pfio.cache.multiprocessfile:get:read_index", trace=self.trace):
238+
index_entry = os.pread(self.cache_fd, self.buflen, offset)
238239
(o, l) = unpack('Qq', index_entry)
239240
if l < 0 or o < 0:
240241
fcntl.flock(self.cache_fd, fcntl.LOCK_UN)
241242
return None
242243

243-
with record("pfio.cache.multiprocessfile:get:read", trace=self.trace):
244+
with record("pfio.cache.multiprocessfile:get:read_data", trace=self.trace):
244245
data = os.pread(self.cache_fd, l, o)
245246
assert len(data) == l
246247
fcntl.flock(self.cache_fd, fcntl.LOCK_UN)
@@ -279,26 +280,30 @@ def _put(self, i, data):
279280

280281
fcntl.flock(self.cache_fd, fcntl.LOCK_EX)
281282
with record(f"pfio.cache.multiprocessfile:put:lock-{self.cache_fd}", trace=self.trace):
282-
buf = os.pread(self.cache_fd, self.buflen, index_ofst)
283+
with record("pfio.cache.multiprocessfile:put:read_index", trace=self.trace):
284+
buf = os.pread(self.cache_fd, self.buflen, index_ofst)
283285
(o, l) = unpack('Qq', buf)
284286

285287
if l >= 0 and o >= 0:
286288
# Already data exists
287289
fcntl.flock(self.cache_fd, fcntl.LOCK_UN)
288290
return False
289291

290-
data_pos = os.lseek(self.cache_fd, 0, os.SEEK_END)
292+
with record("pfio.cache.multiprocessfile:put:seek", trace=self.trace):
293+
data_pos = os.lseek(self.cache_fd, 0, os.SEEK_END)
291294
if self.cache_size_limit:
292295
if self.cache_size_limit < (data_pos + len(data)):
293296
self._frozen = True
294297
fcntl.flock(self.cache_fd, fcntl.LOCK_UN)
295298
return False
296299

297300
index_entry = pack('Qq', data_pos, len(data))
298-
assert os.pwrite(self.cache_fd, index_entry, index_ofst) == self.buflen
299-
with record("pfio.cache.multiprocessfile:put:write", trace=self.trace):
301+
with record("pfio.cache.multiprocessfile:put:write_index", trace=self.trace):
302+
assert os.pwrite(self.cache_fd, index_entry, index_ofst) == self.buflen
303+
with record("pfio.cache.multiprocessfile:put:write_data", trace=self.trace):
300304
assert os.pwrite(self.cache_fd, data, data_pos) == len(data)
301-
os.fsync(self.cache_fd)
305+
with record("pfio.cache.multiprocessfile:put:sync", trace=self.trace):
306+
os.fsync(self.cache_fd)
302307
fcntl.flock(self.cache_fd, fcntl.LOCK_UN)
303308

304309
return True

0 commit comments

Comments
 (0)