Skip to content

Commit 18b9201

Browse files
authored
Merge pull request #1879 from antmicro/fix-race-condition
Fix race condition
2 parents 8ef904d + b784f78 commit 18b9201

35 files changed

+201
-159
lines changed

.github/workflows/scripts/db.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ echo "----------------------------------------"
7373
# Looking for the failing directories and packing them
7474
# example of line from which the failing fuzzer directory gets extracted:
7575
# - Makefile:87: recipe for target '000-db-init/000-init-db/run.xc7a100tfgg676-1.ok' failed --> fuzzers/000-db-init
76-
grep -Po "recipe for target '\K(.*)(?=\/run\..*\.ok')" $tmp | sed -e 's/^/fuzzers\//' | xargs tar -zcf fuzzers/fails.tgz
76+
grep -Po "recipe for target '\K(.*)(?=\/run.*\.ok')" $tmp | sed -e 's/^/fuzzers\//' | xargs tar -zcf fuzzers/fails.tgz
7777
echo "----------------------------------------"
7878
echo "A failure occurred during Database build."
7979
echo "----------------------------------------"

prjxray/bitfilter.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
# https://opensource.org/licenses/ISC
99
#
1010
# SPDX-License-Identifier: ISC
11+
12+
from prjxray.util import OpenSafeFile
13+
1114
class Bitfilter(object):
1215
def __init__(
1316
self, frames_to_include=None, frames_to_exclude=[],

prjxray/bitstream.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
# SPDX-License-Identifier: ISC
1111
import json
1212
import os
13-
from prjxray import util
13+
from prjxray.util import block_type_s2i
1414

1515
# Break frames into WORD_SIZE bit words.
1616
WORD_SIZE_BITS = 32
@@ -119,7 +119,7 @@ def addr_bits2word(block_type, top_bottom, cfg_row, cfg_col, minor_addr):
119119
"""Convert a deconstructed address to a 32 bit word"""
120120
# https://www.xilinx.com/support/documentation/user_guides/ug470_7Series_Config.pdf
121121
ret = 0
122-
ret |= util.block_type_s2i[block_type] << 23
122+
ret |= block_type_s2i[block_type] << 23
123123
ret |= {"top": 0, "bottom": 1}[top_bottom] << 22
124124
ret |= cfg_row << 17
125125
ret |= cfg_col << 7

prjxray/lib.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
import re
1515
from collections import namedtuple
1616

17+
from prjxray.util import OpenSafeFile
18+
1719

1820
def read_root_csv(root_dir):
1921
""" Reads root.csv from raw db directory.
@@ -24,7 +26,7 @@ def read_root_csv(root_dir):
2426
tiles = {}
2527
nodes = []
2628

27-
with open(os.path.join(root_dir, 'root.csv')) as f:
29+
with OpenSafeFile(os.path.join(root_dir, 'root.csv')) as f:
2830
for d in csv.DictReader(f):
2931
if d['filetype'] == 'tile':
3032
if d['subtype'] not in tiles:
@@ -123,17 +125,17 @@ def load_from_root_csv(self, nodes):
123125
import pyjson5 as json5
124126
import progressbar
125127
for node in progressbar.progressbar(nodes):
126-
with open(node) as f:
128+
with OpenSafeFile(node) as f:
127129
node_wires = json5.load(f)
128130
assert node_wires['node'] not in self.nodes
129131
self.nodes[node_wires['node']] = node_wires['wires']
130132

131133
def load_from_file(self, fname):
132-
with open(fname, 'rb') as f:
134+
with OpenSafeFile(fname, 'rb') as f:
133135
self.nodes = pickle.load(f)
134136

135137
def save_to_file(self, fname):
136-
with open(fname, 'wb') as f:
138+
with OpenSafeFile(fname, 'wb') as f:
137139
pickle.dump(self.nodes, f)
138140

139141
def site_pin_node_to_wires(self, tile, node):

prjxray/lms_solver.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@
5454
import numpy as np
5555
import numpy.linalg as linalg
5656

57+
from prjxray.util import OpenSafeFile
58+
5759
# =============================================================================
5860

5961

@@ -83,7 +85,7 @@ def load_data(file_name, tagfilter=lambda tag: True, address_map=None):
8385
segdata = None
8486
all_segdata = []
8587

86-
with open(file_name, "r") as fp:
88+
with OpenSafeFile(file_name, "r") as fp:
8789
for line in fp.readlines():
8890
line = line.strip()
8991

@@ -174,7 +176,7 @@ def write_segbits(file_name, all_tags, all_bits, W):
174176

175177
lines.append(all_tags[r] + " " + " ".join(bits) + "\n")
176178

177-
with open(file_name, "w") as fp:
179+
with OpenSafeFile(file_name, "w") as fp:
178180
for line in lines:
179181
fp.write(line)
180182

@@ -702,7 +704,7 @@ def build_address_map(tilegrid_file):
702704
address_map = {}
703705

704706
# Load tilegrid
705-
with open(tilegrid_file, "r") as fp:
707+
with OpenSafeFile(tilegrid_file, "r") as fp:
706708
tilegrid = json.load(fp)
707709

708710
# Loop over tiles
@@ -982,7 +984,7 @@ def tagfilter(tag):
982984

983985
# Dump to CSV
984986
if args.x is not None:
985-
with open(args.x, "w") as fp:
987+
with OpenSafeFile(args.x, "w") as fp:
986988
dump_solution_to_csv(fp, tags_to_solve, bits_to_solve, X)
987989

988990
# Dump results

prjxray/node_lookup.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import pyjson5 as json5
1414
import os.path
1515

16+
from prjxray.util import OpenSafeFile
1617

1718
def create_tables(conn):
1819
c = conn.cursor()
@@ -63,7 +64,7 @@ def build_database(self, nodes, tiles):
6364

6465
nodes_processed = set()
6566
for node in progressbar.progressbar(nodes):
66-
with open(node) as f:
67+
with OpenSafeFile(node) as f:
6768
node_wires = json5.load(f)
6869
assert node_wires['node'] not in nodes_processed
6970
nodes_processed.add(node_wires['node'])

prjxray/segmaker.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
'''
2525

2626
import os, json, re
27-
from prjxray import util
27+
from prjxray.util import OpenSafeFile, get_db_root, get_fabric
2828

2929
BLOCK_TYPES = set(('CLB_IO_CLK', 'BLOCK_RAM', 'CFG_CLB'))
3030

@@ -85,12 +85,12 @@ class Segmaker:
8585
def __init__(self, bitsfile, verbose=None, db_root=None, fabric=None):
8686
self.db_root = db_root
8787
if self.db_root is None:
88-
self.db_root = util.get_db_root()
88+
self.db_root = get_db_root()
8989
assert self.db_root, "No db root specified."
9090

9191
self.fabric = fabric
9292
if self.fabric is None:
93-
self.fabric = util.get_fabric()
93+
self.fabric = get_fabric()
9494
assert self.fabric, "No fabric specified."
9595

9696
self.verbose = verbose if verbose is not None else os.getenv(
@@ -129,7 +129,7 @@ def set_def_bt(self, block_type):
129129

130130
def load_grid(self):
131131
'''Load self.grid holding tile addresses'''
132-
with open(os.path.join(self.db_root, self.fabric, "tilegrid.json"),
132+
with OpenSafeFile(os.path.join(self.db_root, self.fabric, "tilegrid.json"),
133133
"r") as f:
134134
self.grid = json.load(f)
135135
assert "segments" not in self.grid, "Old format tilegrid.json"
@@ -152,7 +152,7 @@ def load_bits(self, bitsfile):
152152
'''
153153
self.bits = dict()
154154
print("Loading bits from %s." % bitsfile)
155-
with open(bitsfile, "r") as f:
155+
with OpenSafeFile(bitsfile, "r") as f:
156156
for line in f:
157157
# ex: bit_00020500_000_17
158158
line = line.split("_")
@@ -446,7 +446,7 @@ def write(self, suffix=None, roi=False, allow_empty=False):
446446
segments = self.segments_by_type[segtype]
447447
if segments:
448448
print("Writing %s." % filename)
449-
with open(filename, "w") as f:
449+
with OpenSafeFile(filename, "w") as f:
450450
for segname, segdata in sorted(segments.items()):
451451
# seg 00020300_010
452452
print("seg %s" % segname, file=f)

prjxray/tile.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import json
1414
from prjxray import lib
1515
from prjxray.timing import fast_slow_tuple_to_corners, RcElement
16+
from prjxray.util import OpenSafeFile
1617

1718
TileDbs = namedtuple(
1819
'TileDbs', 'segbits block_ram_segbits ppips mask tile_type')
@@ -313,7 +314,7 @@ def yield_pips(pips):
313314
backward_timing=get_pip_timing(pip.get('dst_to_src')),
314315
)
315316

316-
with open(self.tile_dbs.tile_type) as f:
317+
with OpenSafeFile(self.tile_dbs.tile_type) as f:
317318
tile_type = json.load(f)
318319
assert self.tilename_upper == tile_type['tile_type']
319320
self.wires = get_wires(tile_type['wires'])

prjxray/tile_segbits.py

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
# SPDX-License-Identifier: ISC
1111
from collections import namedtuple
1212
from prjxray import bitstream
13-
from prjxray import util
1413
from prjxray.grid_types import BlockType
14+
from prjxray.util import OpenSafeFile
1515
import enum
1616

1717

@@ -84,22 +84,16 @@ def __init__(self, tile_db):
8484
self.feature_addresses = {}
8585

8686
if tile_db.ppips is not None:
87-
with open(tile_db.ppips) as f:
88-
util.lock_file(f, 10)
87+
with OpenSafeFile(tile_db.ppips) as f:
8988
self.ppips = read_ppips(f)
90-
util.unlock_file(f)
9189

9290
if tile_db.segbits is not None:
93-
with open(tile_db.segbits) as f:
94-
util.lock_file(f, 10)
91+
with OpenSafeFile(tile_db.segbits) as f:
9592
self.segbits[BlockType.CLB_IO_CLK] = read_segbits(f)
96-
util.unlock_file(f)
9793

9894
if tile_db.block_ram_segbits is not None:
99-
with open(tile_db.block_ram_segbits) as f:
100-
util.lock_file(f, 10)
95+
with OpenSafeFile(tile_db.block_ram_segbits) as f:
10196
self.segbits[BlockType.BLOCK_RAM] = read_segbits(f)
102-
util.unlock_file(f)
10397

10498
for block_type in self.segbits:
10599
for feature in self.segbits[block_type]:

prjxray/tile_segbits_alias.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
from prjxray import bitstream
2222
from prjxray.grid_types import Bits
2323
from prjxray.tile_segbits import read_ppips
24+
from prjxray.util import OpenSafeFile
2425

2526

2627
class TileSegbitsAlias(object):
@@ -67,7 +68,7 @@ def __init__(self, db, tile_type, bits_map):
6768
self.ppips = {}
6869

6970
if tile_db.ppips is not None:
70-
with open(tile_db.ppips) as f:
71+
with OpenSafeFile(tile_db.ppips) as f:
7172
self.ppips = read_ppips(f)
7273
self.tile_segbits = db.get_tile_segbits(self.alias_tile_type)
7374

0 commit comments

Comments
 (0)