Skip to content

Commit

Permalink
BaseTools: Improve the file saving and copying reliability
Browse files Browse the repository at this point in the history
BZ:https://bugzilla.tianocore.org/show_bug.cgi?id=2079

The Basetool CopyFileOnChange() and SaveFileOnChange()
functions might raise the IOError occasionally when build
in Windows with multi-process and build cache enabled.
The CopyFileOnChange() and SaveFileOnChange() might be invoked
in multiple sub-processes simultaneously, and this patch adds
global locks to sync these functions invoking which can
harden their reliability.

Cc: Liming Gao <[email protected]>
Cc: Bob Feng <[email protected]>
Signed-off-by: Steven Shi <[email protected]>
Reviewed-by: Bob Feng <[email protected]>
  • Loading branch information
shijunjing authored and BobCF committed Aug 20, 2019
1 parent d01a998 commit 9445908
Show file tree
Hide file tree
Showing 8 changed files with 119 additions and 42 deletions.
6 changes: 4 additions & 2 deletions BaseTools/Source/Python/AutoGen/AutoGenWorker.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,14 +133,15 @@ def TerminateWorkers(self):
def kill(self):
self.feedback_q.put(None)
class AutoGenWorkerInProcess(mp.Process):
def __init__(self,module_queue,data_pipe_file_path,feedback_q,file_lock, share_data,log_q,error_event):
def __init__(self,module_queue,data_pipe_file_path,feedback_q,file_lock,cache_lock,share_data,log_q,error_event):
mp.Process.__init__(self)
self.module_queue = module_queue
self.data_pipe_file_path =data_pipe_file_path
self.data_pipe = None
self.feedback_q = feedback_q
self.PlatformMetaFileSet = {}
self.file_lock = file_lock
self.cache_lock = cache_lock
self.share_data = share_data
self.log_q = log_q
self.error_event = error_event
Expand Down Expand Up @@ -184,9 +185,10 @@ def run(self):
GlobalData.gDatabasePath = self.data_pipe.Get("DatabasePath")
GlobalData.gBinCacheSource = self.data_pipe.Get("BinCacheSource")
GlobalData.gBinCacheDest = self.data_pipe.Get("BinCacheDest")
GlobalData.gCacheIR = self.data_pipe.Get("CacheIR")
GlobalData.gCacheIR = self.share_data
GlobalData.gEnableGenfdsMultiThread = self.data_pipe.Get("EnableGenfdsMultiThread")
GlobalData.file_lock = self.file_lock
GlobalData.cache_lock = self.cache_lock
CommandTarget = self.data_pipe.Get("CommandTarget")
pcd_from_build_option = []
for pcd_tuple in self.data_pipe.Get("BuildOptPcd"):
Expand Down
1 change: 1 addition & 0 deletions BaseTools/Source/Python/AutoGen/CacheIR.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,6 @@ def __init__(self, Path, Arch):
self.MakeHashDigest = None
self.MakeHashHexDigest = None
self.MakeHashChain = []
self.CacheCrash = False
self.PreMakeCacheHit = False
self.MakeCacheHit = False
2 changes: 0 additions & 2 deletions BaseTools/Source/Python/AutoGen/DataPipe.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,4 @@ def FillData(self,PlatformInfo):

self.DataContainer = {"BinCacheDest":GlobalData.gBinCacheDest}

self.DataContainer = {"CacheIR":GlobalData.gCacheIR}

self.DataContainer = {"EnableGenfdsMultiThread":GlobalData.gEnableGenfdsMultiThread}
Empty file modified BaseTools/Source/Python/AutoGen/GenC.py
100644 → 100755
Empty file.
101 changes: 71 additions & 30 deletions BaseTools/Source/Python/AutoGen/ModuleAutoGen.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
from AutoGen.ModuleAutoGenHelper import PlatformInfo,WorkSpaceInfo
from AutoGen.CacheIR import ModuleBuildCacheIR
import json
import tempfile

## Mapping Makefile type
gMakeTypeMap = {TAB_COMPILER_MSFT:"nmake", "GCC":"gmake"}
Expand Down Expand Up @@ -1702,9 +1703,8 @@ def SaveHashChainFileToCache(self, gDict):
try:
ModuleHashPairList = [] # tuple list: [tuple(PreMakefileHash, MakeHash)]
if os.path.exists(ModuleHashPair):
f = open(ModuleHashPair, 'r')
ModuleHashPairList = json.load(f)
f.close()
with open(ModuleHashPair, 'r') as f:
ModuleHashPairList = json.load(f)
PreMakeHash = gDict[(self.MetaFile.Path, self.Arch)].PreMakefileHashHexDigest
MakeHash = gDict[(self.MetaFile.Path, self.Arch)].MakeHashHexDigest
ModuleHashPairList.append((PreMakeHash, MakeHash))
Expand Down Expand Up @@ -1766,10 +1766,12 @@ def CreateTimeStamp():

if os.path.exists (self.TimeStampPath):
os.remove (self.TimeStampPath)
with open(self.TimeStampPath, 'w+') as fd:
with tempfile.NamedTemporaryFile('w+', dir=os.path.dirname(self.TimeStampPath), delete=False) as tf:
for f in FileSet:
fd.write(f)
fd.write("\n")
tf.write(f)
tf.write("\n")
tempname = tf.name
SaveFileOnChange(self.TimeStampPath, tempname, False)

# Ignore generating makefile when it is a binary module
if self.IsBinaryModule:
Expand Down Expand Up @@ -1806,7 +1808,7 @@ def CreateTimeStamp():
MewIR.MakefilePath = MakefilePath
MewIR.DependencyHeaderFileSet = Makefile.DependencyHeaderFileSet
MewIR.CreateMakeFileDone = True
with GlobalData.file_lock:
with GlobalData.cache_lock:
try:
IR = gDict[(self.MetaFile.Path, self.Arch)]
IR.MakefilePath = MakefilePath
Expand Down Expand Up @@ -1891,7 +1893,7 @@ def CreateCodeFile(self, CreateLibraryCodeFile=True):
self.IsCodeFileCreated = True
MewIR = ModuleBuildCacheIR(self.MetaFile.Path, self.Arch)
MewIR.CreateCodeFileDone = True
with GlobalData.file_lock:
with GlobalData.cache_lock:
try:
IR = gDict[(self.MetaFile.Path, self.Arch)]
IR.CreateCodeFileDone = True
Expand Down Expand Up @@ -1951,9 +1953,8 @@ def GenModuleHash(self):
m.update(GlobalData.gModuleHash[self.Arch][Lib.Name].encode('utf-8'))

# Add Module self
f = open(str(self.MetaFile), 'rb')
Content = f.read()
f.close()
with open(str(self.MetaFile), 'rb') as f:
Content = f.read()
m.update(Content)

# Add Module's source files
Expand All @@ -1974,6 +1975,11 @@ def GenModuleFilesHash(self, gDict):
if gDict[(self.MetaFile.Path, self.Arch)].ModuleFilesChain:
return gDict[(self.MetaFile.Path, self.Arch)]

# skip if the module cache already crashed
if (self.MetaFile.Path, self.Arch) in gDict and \
gDict[(self.MetaFile.Path, self.Arch)].CacheCrash:
return

DependencyFileSet = set()
# Add Module Meta file
DependencyFileSet.add(self.MetaFile)
Expand Down Expand Up @@ -2021,9 +2027,8 @@ def GenModuleFilesHash(self, gDict):
if not os.path.exists(str(File)):
EdkLogger.quiet("[cache warning]: header file %s is missing for module: %s[%s]" % (File, self.MetaFile.Path, self.Arch))
continue
f = open(str(File), 'rb')
Content = f.read()
f.close()
with open(str(File), 'rb') as f:
Content = f.read()
m.update(Content)
FileList.append((str(File), hashlib.md5(Content).hexdigest()))

Expand All @@ -2032,7 +2037,7 @@ def GenModuleFilesHash(self, gDict):
MewIR.ModuleFilesHashDigest = m.digest()
MewIR.ModuleFilesHashHexDigest = m.hexdigest()
MewIR.ModuleFilesChain = FileList
with GlobalData.file_lock:
with GlobalData.cache_lock:
try:
IR = gDict[(self.MetaFile.Path, self.Arch)]
IR.ModuleFilesHashDigest = m.digest()
Expand All @@ -2050,6 +2055,11 @@ def GenPreMakefileHash(self, gDict):
gDict[(self.MetaFile.Path, self.Arch)].PreMakefileHashHexDigest:
return gDict[(self.MetaFile.Path, self.Arch)]

# skip if the module cache already crashed
if (self.MetaFile.Path, self.Arch) in gDict and \
gDict[(self.MetaFile.Path, self.Arch)].CacheCrash:
return

# skip binary module
if self.IsBinaryModule:
return
Expand Down Expand Up @@ -2091,7 +2101,7 @@ def GenPreMakefileHash(self, gDict):
# Add Module self
m.update(gDict[(self.MetaFile.Path, self.Arch)].ModuleFilesHashDigest)

with GlobalData.file_lock:
with GlobalData.cache_lock:
IR = gDict[(self.MetaFile.Path, self.Arch)]
IR.PreMakefileHashHexDigest = m.hexdigest()
gDict[(self.MetaFile.Path, self.Arch)] = IR
Expand All @@ -2104,6 +2114,11 @@ def GenMakeHeaderFilesHash(self, gDict):
gDict[(self.MetaFile.Path, self.Arch)].MakeHeaderFilesHashDigest:
return gDict[(self.MetaFile.Path, self.Arch)]

# skip if the module cache already crashed
if (self.MetaFile.Path, self.Arch) in gDict and \
gDict[(self.MetaFile.Path, self.Arch)].CacheCrash:
return

# skip binary module
if self.IsBinaryModule:
return
Expand Down Expand Up @@ -2159,7 +2174,7 @@ def GenMakeHeaderFilesHash(self, gDict):
m.update(Content)
FileList.append((str(File), hashlib.md5(Content).hexdigest()))

with GlobalData.file_lock:
with GlobalData.cache_lock:
IR = gDict[(self.MetaFile.Path, self.Arch)]
IR.AutoGenFileList = self.AutoGenFileList.keys()
IR.MakeHeaderFilesHashChain = FileList
Expand All @@ -2174,6 +2189,11 @@ def GenMakeHash(self, gDict):
gDict[(self.MetaFile.Path, self.Arch)].MakeHashChain:
return gDict[(self.MetaFile.Path, self.Arch)]

# skip if the module cache already crashed
if (self.MetaFile.Path, self.Arch) in gDict and \
gDict[(self.MetaFile.Path, self.Arch)].CacheCrash:
return

# skip binary module
if self.IsBinaryModule:
return
Expand Down Expand Up @@ -2222,7 +2242,7 @@ def GenMakeHash(self, gDict):
New.sort(key=lambda x: str(x))
MakeHashChain += New

with GlobalData.file_lock:
with GlobalData.cache_lock:
IR = gDict[(self.MetaFile.Path, self.Arch)]
IR.MakeHashDigest = m.digest()
IR.MakeHashHexDigest = m.hexdigest()
Expand All @@ -2236,6 +2256,12 @@ def CanSkipbyPreMakefileCache(self, gDict):
if not GlobalData.gBinCacheSource:
return False

if gDict[(self.MetaFile.Path, self.Arch)].PreMakeCacheHit:
return True

if gDict[(self.MetaFile.Path, self.Arch)].CacheCrash:
return False

# If Module is binary, do not skip by cache
if self.IsBinaryModule:
return False
Expand All @@ -2255,12 +2281,15 @@ def CanSkipbyPreMakefileCache(self, gDict):
ModuleHashPair = path.join(FileDir, self.Name + ".ModuleHashPair")
if not os.path.exists(ModuleHashPair):
EdkLogger.quiet("[cache warning]: Cannot find ModuleHashPair file: %s" % ModuleHashPair)
with GlobalData.cache_lock:
IR = gDict[(self.MetaFile.Path, self.Arch)]
IR.CacheCrash = True
gDict[(self.MetaFile.Path, self.Arch)] = IR
return False

try:
f = open(ModuleHashPair, 'r')
ModuleHashPairList = json.load(f)
f.close()
with open(ModuleHashPair, 'r') as f:
ModuleHashPairList = json.load(f)
except:
EdkLogger.quiet("[cache warning]: fail to load ModuleHashPair file: %s" % ModuleHashPair)
return False
Expand Down Expand Up @@ -2300,7 +2329,7 @@ def CanSkipbyPreMakefileCache(self, gDict):
if self.Name == "PcdPeim" or self.Name == "PcdDxe":
CreatePcdDatabaseCode(self, TemplateString(), TemplateString())

with GlobalData.file_lock:
with GlobalData.cache_lock:
IR = gDict[(self.MetaFile.Path, self.Arch)]
IR.PreMakeCacheHit = True
gDict[(self.MetaFile.Path, self.Arch)] = IR
Expand All @@ -2313,6 +2342,12 @@ def CanSkipbyMakeCache(self, gDict):
if not GlobalData.gBinCacheSource:
return False

if gDict[(self.MetaFile.Path, self.Arch)].MakeCacheHit:
return True

if gDict[(self.MetaFile.Path, self.Arch)].CacheCrash:
return False

# If Module is binary, do not skip by cache
if self.IsBinaryModule:
print("[cache miss]: checkpoint_Makefile: binary module:", self.MetaFile.Path, self.Arch)
Expand All @@ -2321,7 +2356,7 @@ def CanSkipbyMakeCache(self, gDict):
# .inc is contains binary information so do not skip by hash as well
for f_ext in self.SourceFileList:
if '.inc' in str(f_ext):
with GlobalData.file_lock:
with GlobalData.cache_lock:
IR = gDict[(self.MetaFile.Path, self.Arch)]
IR.MakeCacheHit = False
gDict[(self.MetaFile.Path, self.Arch)] = IR
Expand All @@ -2338,12 +2373,15 @@ def CanSkipbyMakeCache(self, gDict):
ModuleHashPair = path.join(FileDir, self.Name + ".ModuleHashPair")
if not os.path.exists(ModuleHashPair):
EdkLogger.quiet("[cache warning]: Cannot find ModuleHashPair file: %s" % ModuleHashPair)
with GlobalData.cache_lock:
IR = gDict[(self.MetaFile.Path, self.Arch)]
IR.CacheCrash = True
gDict[(self.MetaFile.Path, self.Arch)] = IR
return False

try:
f = open(ModuleHashPair, 'r')
ModuleHashPairList = json.load(f)
f.close()
with open(ModuleHashPair, 'r') as f:
ModuleHashPairList = json.load(f)
except:
EdkLogger.quiet("[cache warning]: fail to load ModuleHashPair file: %s" % ModuleHashPair)
return False
Expand Down Expand Up @@ -2383,7 +2421,7 @@ def CanSkipbyMakeCache(self, gDict):

if self.Name == "PcdPeim" or self.Name == "PcdDxe":
CreatePcdDatabaseCode(self, TemplateString(), TemplateString())
with GlobalData.file_lock:
with GlobalData.cache_lock:
IR = gDict[(self.MetaFile.Path, self.Arch)]
IR.MakeCacheHit = True
gDict[(self.MetaFile.Path, self.Arch)] = IR
Expand All @@ -2395,6 +2433,10 @@ def PrintFirstMakeCacheMissFile(self, gDict):
if not GlobalData.gBinCacheSource:
return

# skip if the module cache already crashed
if gDict[(self.MetaFile.Path, self.Arch)].CacheCrash:
return

# skip binary module
if self.IsBinaryModule:
return
Expand All @@ -2420,9 +2462,8 @@ def PrintFirstMakeCacheMissFile(self, gDict):
return

try:
f = open(ModuleHashPair, 'r')
ModuleHashPairList = json.load(f)
f.close()
with open(ModuleHashPair, 'r') as f:
ModuleHashPairList = json.load(f)
except:
EdkLogger.quiet("[cache insight]: Cannot load ModuleHashPair file for module: %s[%s]" % (self.MetaFile.Path, self.Arch))
return
Expand Down
2 changes: 2 additions & 0 deletions BaseTools/Source/Python/Common/GlobalData.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,8 @@

# Common dictionary to share module cache intermediate result and state
gCacheIR = None
# Common lock for the module cache intermediate data
cache_lock = None
# Common lock for the file access in multiple process AutoGens
file_lock = None
# Common dictionary to share platform libraries' constant Pcd
Expand Down
Loading

0 comments on commit 9445908

Please sign in to comment.