BaseTools: Improve the file saving and copying reliability

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 <liming.gao@intel.com>
Cc: Bob Feng <bob.c.feng@intel.com>
Signed-off-by: Steven Shi <steven.shi@intel.com>
Reviewed-by: Bob Feng <bob.c.feng@intel.com>
This commit is contained in:
Shi, Steven 2019-08-15 22:26:21 +08:00 committed by Feng, Bob C
parent d01a998612
commit 94459080c1
8 changed files with 119 additions and 42 deletions

View File

@ -133,7 +133,7 @@ class AutoGenManager(threading.Thread):
def kill(self): def kill(self):
self.feedback_q.put(None) self.feedback_q.put(None)
class AutoGenWorkerInProcess(mp.Process): 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) mp.Process.__init__(self)
self.module_queue = module_queue self.module_queue = module_queue
self.data_pipe_file_path =data_pipe_file_path self.data_pipe_file_path =data_pipe_file_path
@ -141,6 +141,7 @@ class AutoGenWorkerInProcess(mp.Process):
self.feedback_q = feedback_q self.feedback_q = feedback_q
self.PlatformMetaFileSet = {} self.PlatformMetaFileSet = {}
self.file_lock = file_lock self.file_lock = file_lock
self.cache_lock = cache_lock
self.share_data = share_data self.share_data = share_data
self.log_q = log_q self.log_q = log_q
self.error_event = error_event self.error_event = error_event
@ -184,9 +185,10 @@ class AutoGenWorkerInProcess(mp.Process):
GlobalData.gDatabasePath = self.data_pipe.Get("DatabasePath") GlobalData.gDatabasePath = self.data_pipe.Get("DatabasePath")
GlobalData.gBinCacheSource = self.data_pipe.Get("BinCacheSource") GlobalData.gBinCacheSource = self.data_pipe.Get("BinCacheSource")
GlobalData.gBinCacheDest = self.data_pipe.Get("BinCacheDest") 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.gEnableGenfdsMultiThread = self.data_pipe.Get("EnableGenfdsMultiThread")
GlobalData.file_lock = self.file_lock GlobalData.file_lock = self.file_lock
GlobalData.cache_lock = self.cache_lock
CommandTarget = self.data_pipe.Get("CommandTarget") CommandTarget = self.data_pipe.Get("CommandTarget")
pcd_from_build_option = [] pcd_from_build_option = []
for pcd_tuple in self.data_pipe.Get("BuildOptPcd"): for pcd_tuple in self.data_pipe.Get("BuildOptPcd"):

View File

@ -24,5 +24,6 @@ class ModuleBuildCacheIR():
self.MakeHashDigest = None self.MakeHashDigest = None
self.MakeHashHexDigest = None self.MakeHashHexDigest = None
self.MakeHashChain = [] self.MakeHashChain = []
self.CacheCrash = False
self.PreMakeCacheHit = False self.PreMakeCacheHit = False
self.MakeCacheHit = False self.MakeCacheHit = False

View File

@ -163,6 +163,4 @@ class MemoryDataPipe(DataPipe):
self.DataContainer = {"BinCacheDest":GlobalData.gBinCacheDest} self.DataContainer = {"BinCacheDest":GlobalData.gBinCacheDest}
self.DataContainer = {"CacheIR":GlobalData.gCacheIR}
self.DataContainer = {"EnableGenfdsMultiThread":GlobalData.gEnableGenfdsMultiThread} self.DataContainer = {"EnableGenfdsMultiThread":GlobalData.gEnableGenfdsMultiThread}

0
BaseTools/Source/Python/AutoGen/GenC.py Normal file → Executable file
View File

View File

@ -28,6 +28,7 @@ from Common.caching import cached_class_function
from AutoGen.ModuleAutoGenHelper import PlatformInfo,WorkSpaceInfo from AutoGen.ModuleAutoGenHelper import PlatformInfo,WorkSpaceInfo
from AutoGen.CacheIR import ModuleBuildCacheIR from AutoGen.CacheIR import ModuleBuildCacheIR
import json import json
import tempfile
## Mapping Makefile type ## Mapping Makefile type
gMakeTypeMap = {TAB_COMPILER_MSFT:"nmake", "GCC":"gmake"} gMakeTypeMap = {TAB_COMPILER_MSFT:"nmake", "GCC":"gmake"}
@ -1702,9 +1703,8 @@ class ModuleAutoGen(AutoGen):
try: try:
ModuleHashPairList = [] # tuple list: [tuple(PreMakefileHash, MakeHash)] ModuleHashPairList = [] # tuple list: [tuple(PreMakefileHash, MakeHash)]
if os.path.exists(ModuleHashPair): if os.path.exists(ModuleHashPair):
f = open(ModuleHashPair, 'r') with open(ModuleHashPair, 'r') as f:
ModuleHashPairList = json.load(f) ModuleHashPairList = json.load(f)
f.close()
PreMakeHash = gDict[(self.MetaFile.Path, self.Arch)].PreMakefileHashHexDigest PreMakeHash = gDict[(self.MetaFile.Path, self.Arch)].PreMakefileHashHexDigest
MakeHash = gDict[(self.MetaFile.Path, self.Arch)].MakeHashHexDigest MakeHash = gDict[(self.MetaFile.Path, self.Arch)].MakeHashHexDigest
ModuleHashPairList.append((PreMakeHash, MakeHash)) ModuleHashPairList.append((PreMakeHash, MakeHash))
@ -1766,10 +1766,12 @@ class ModuleAutoGen(AutoGen):
if os.path.exists (self.TimeStampPath): if os.path.exists (self.TimeStampPath):
os.remove (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: for f in FileSet:
fd.write(f) tf.write(f)
fd.write("\n") tf.write("\n")
tempname = tf.name
SaveFileOnChange(self.TimeStampPath, tempname, False)
# Ignore generating makefile when it is a binary module # Ignore generating makefile when it is a binary module
if self.IsBinaryModule: if self.IsBinaryModule:
@ -1806,7 +1808,7 @@ class ModuleAutoGen(AutoGen):
MewIR.MakefilePath = MakefilePath MewIR.MakefilePath = MakefilePath
MewIR.DependencyHeaderFileSet = Makefile.DependencyHeaderFileSet MewIR.DependencyHeaderFileSet = Makefile.DependencyHeaderFileSet
MewIR.CreateMakeFileDone = True MewIR.CreateMakeFileDone = True
with GlobalData.file_lock: with GlobalData.cache_lock:
try: try:
IR = gDict[(self.MetaFile.Path, self.Arch)] IR = gDict[(self.MetaFile.Path, self.Arch)]
IR.MakefilePath = MakefilePath IR.MakefilePath = MakefilePath
@ -1891,7 +1893,7 @@ class ModuleAutoGen(AutoGen):
self.IsCodeFileCreated = True self.IsCodeFileCreated = True
MewIR = ModuleBuildCacheIR(self.MetaFile.Path, self.Arch) MewIR = ModuleBuildCacheIR(self.MetaFile.Path, self.Arch)
MewIR.CreateCodeFileDone = True MewIR.CreateCodeFileDone = True
with GlobalData.file_lock: with GlobalData.cache_lock:
try: try:
IR = gDict[(self.MetaFile.Path, self.Arch)] IR = gDict[(self.MetaFile.Path, self.Arch)]
IR.CreateCodeFileDone = True IR.CreateCodeFileDone = True
@ -1951,9 +1953,8 @@ class ModuleAutoGen(AutoGen):
m.update(GlobalData.gModuleHash[self.Arch][Lib.Name].encode('utf-8')) m.update(GlobalData.gModuleHash[self.Arch][Lib.Name].encode('utf-8'))
# Add Module self # Add Module self
f = open(str(self.MetaFile), 'rb') with open(str(self.MetaFile), 'rb') as f:
Content = f.read() Content = f.read()
f.close()
m.update(Content) m.update(Content)
# Add Module's source files # Add Module's source files
@ -1974,6 +1975,11 @@ class ModuleAutoGen(AutoGen):
if gDict[(self.MetaFile.Path, self.Arch)].ModuleFilesChain: if gDict[(self.MetaFile.Path, self.Arch)].ModuleFilesChain:
return gDict[(self.MetaFile.Path, self.Arch)] 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() DependencyFileSet = set()
# Add Module Meta file # Add Module Meta file
DependencyFileSet.add(self.MetaFile) DependencyFileSet.add(self.MetaFile)
@ -2021,9 +2027,8 @@ class ModuleAutoGen(AutoGen):
if not os.path.exists(str(File)): 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)) EdkLogger.quiet("[cache warning]: header file %s is missing for module: %s[%s]" % (File, self.MetaFile.Path, self.Arch))
continue continue
f = open(str(File), 'rb') with open(str(File), 'rb') as f:
Content = f.read() Content = f.read()
f.close()
m.update(Content) m.update(Content)
FileList.append((str(File), hashlib.md5(Content).hexdigest())) FileList.append((str(File), hashlib.md5(Content).hexdigest()))
@ -2032,7 +2037,7 @@ class ModuleAutoGen(AutoGen):
MewIR.ModuleFilesHashDigest = m.digest() MewIR.ModuleFilesHashDigest = m.digest()
MewIR.ModuleFilesHashHexDigest = m.hexdigest() MewIR.ModuleFilesHashHexDigest = m.hexdigest()
MewIR.ModuleFilesChain = FileList MewIR.ModuleFilesChain = FileList
with GlobalData.file_lock: with GlobalData.cache_lock:
try: try:
IR = gDict[(self.MetaFile.Path, self.Arch)] IR = gDict[(self.MetaFile.Path, self.Arch)]
IR.ModuleFilesHashDigest = m.digest() IR.ModuleFilesHashDigest = m.digest()
@ -2050,6 +2055,11 @@ class ModuleAutoGen(AutoGen):
gDict[(self.MetaFile.Path, self.Arch)].PreMakefileHashHexDigest: gDict[(self.MetaFile.Path, self.Arch)].PreMakefileHashHexDigest:
return gDict[(self.MetaFile.Path, self.Arch)] 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 # skip binary module
if self.IsBinaryModule: if self.IsBinaryModule:
return return
@ -2091,7 +2101,7 @@ class ModuleAutoGen(AutoGen):
# Add Module self # Add Module self
m.update(gDict[(self.MetaFile.Path, self.Arch)].ModuleFilesHashDigest) 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 = gDict[(self.MetaFile.Path, self.Arch)]
IR.PreMakefileHashHexDigest = m.hexdigest() IR.PreMakefileHashHexDigest = m.hexdigest()
gDict[(self.MetaFile.Path, self.Arch)] = IR gDict[(self.MetaFile.Path, self.Arch)] = IR
@ -2104,6 +2114,11 @@ class ModuleAutoGen(AutoGen):
gDict[(self.MetaFile.Path, self.Arch)].MakeHeaderFilesHashDigest: gDict[(self.MetaFile.Path, self.Arch)].MakeHeaderFilesHashDigest:
return gDict[(self.MetaFile.Path, self.Arch)] 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 # skip binary module
if self.IsBinaryModule: if self.IsBinaryModule:
return return
@ -2159,7 +2174,7 @@ class ModuleAutoGen(AutoGen):
m.update(Content) m.update(Content)
FileList.append((str(File), hashlib.md5(Content).hexdigest())) FileList.append((str(File), hashlib.md5(Content).hexdigest()))
with GlobalData.file_lock: with GlobalData.cache_lock:
IR = gDict[(self.MetaFile.Path, self.Arch)] IR = gDict[(self.MetaFile.Path, self.Arch)]
IR.AutoGenFileList = self.AutoGenFileList.keys() IR.AutoGenFileList = self.AutoGenFileList.keys()
IR.MakeHeaderFilesHashChain = FileList IR.MakeHeaderFilesHashChain = FileList
@ -2174,6 +2189,11 @@ class ModuleAutoGen(AutoGen):
gDict[(self.MetaFile.Path, self.Arch)].MakeHashChain: gDict[(self.MetaFile.Path, self.Arch)].MakeHashChain:
return gDict[(self.MetaFile.Path, self.Arch)] 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 # skip binary module
if self.IsBinaryModule: if self.IsBinaryModule:
return return
@ -2222,7 +2242,7 @@ class ModuleAutoGen(AutoGen):
New.sort(key=lambda x: str(x)) New.sort(key=lambda x: str(x))
MakeHashChain += New MakeHashChain += New
with GlobalData.file_lock: with GlobalData.cache_lock:
IR = gDict[(self.MetaFile.Path, self.Arch)] IR = gDict[(self.MetaFile.Path, self.Arch)]
IR.MakeHashDigest = m.digest() IR.MakeHashDigest = m.digest()
IR.MakeHashHexDigest = m.hexdigest() IR.MakeHashHexDigest = m.hexdigest()
@ -2236,6 +2256,12 @@ class ModuleAutoGen(AutoGen):
if not GlobalData.gBinCacheSource: if not GlobalData.gBinCacheSource:
return False 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 Module is binary, do not skip by cache
if self.IsBinaryModule: if self.IsBinaryModule:
return False return False
@ -2255,12 +2281,15 @@ class ModuleAutoGen(AutoGen):
ModuleHashPair = path.join(FileDir, self.Name + ".ModuleHashPair") ModuleHashPair = path.join(FileDir, self.Name + ".ModuleHashPair")
if not os.path.exists(ModuleHashPair): if not os.path.exists(ModuleHashPair):
EdkLogger.quiet("[cache warning]: Cannot find ModuleHashPair file: %s" % 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 return False
try: try:
f = open(ModuleHashPair, 'r') with open(ModuleHashPair, 'r') as f:
ModuleHashPairList = json.load(f) ModuleHashPairList = json.load(f)
f.close()
except: except:
EdkLogger.quiet("[cache warning]: fail to load ModuleHashPair file: %s" % ModuleHashPair) EdkLogger.quiet("[cache warning]: fail to load ModuleHashPair file: %s" % ModuleHashPair)
return False return False
@ -2300,7 +2329,7 @@ class ModuleAutoGen(AutoGen):
if self.Name == "PcdPeim" or self.Name == "PcdDxe": if self.Name == "PcdPeim" or self.Name == "PcdDxe":
CreatePcdDatabaseCode(self, TemplateString(), TemplateString()) CreatePcdDatabaseCode(self, TemplateString(), TemplateString())
with GlobalData.file_lock: with GlobalData.cache_lock:
IR = gDict[(self.MetaFile.Path, self.Arch)] IR = gDict[(self.MetaFile.Path, self.Arch)]
IR.PreMakeCacheHit = True IR.PreMakeCacheHit = True
gDict[(self.MetaFile.Path, self.Arch)] = IR gDict[(self.MetaFile.Path, self.Arch)] = IR
@ -2313,6 +2342,12 @@ class ModuleAutoGen(AutoGen):
if not GlobalData.gBinCacheSource: if not GlobalData.gBinCacheSource:
return False 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 Module is binary, do not skip by cache
if self.IsBinaryModule: if self.IsBinaryModule:
print("[cache miss]: checkpoint_Makefile: binary module:", self.MetaFile.Path, self.Arch) print("[cache miss]: checkpoint_Makefile: binary module:", self.MetaFile.Path, self.Arch)
@ -2321,7 +2356,7 @@ class ModuleAutoGen(AutoGen):
# .inc is contains binary information so do not skip by hash as well # .inc is contains binary information so do not skip by hash as well
for f_ext in self.SourceFileList: for f_ext in self.SourceFileList:
if '.inc' in str(f_ext): if '.inc' in str(f_ext):
with GlobalData.file_lock: with GlobalData.cache_lock:
IR = gDict[(self.MetaFile.Path, self.Arch)] IR = gDict[(self.MetaFile.Path, self.Arch)]
IR.MakeCacheHit = False IR.MakeCacheHit = False
gDict[(self.MetaFile.Path, self.Arch)] = IR gDict[(self.MetaFile.Path, self.Arch)] = IR
@ -2338,12 +2373,15 @@ class ModuleAutoGen(AutoGen):
ModuleHashPair = path.join(FileDir, self.Name + ".ModuleHashPair") ModuleHashPair = path.join(FileDir, self.Name + ".ModuleHashPair")
if not os.path.exists(ModuleHashPair): if not os.path.exists(ModuleHashPair):
EdkLogger.quiet("[cache warning]: Cannot find ModuleHashPair file: %s" % 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 return False
try: try:
f = open(ModuleHashPair, 'r') with open(ModuleHashPair, 'r') as f:
ModuleHashPairList = json.load(f) ModuleHashPairList = json.load(f)
f.close()
except: except:
EdkLogger.quiet("[cache warning]: fail to load ModuleHashPair file: %s" % ModuleHashPair) EdkLogger.quiet("[cache warning]: fail to load ModuleHashPair file: %s" % ModuleHashPair)
return False return False
@ -2383,7 +2421,7 @@ class ModuleAutoGen(AutoGen):
if self.Name == "PcdPeim" or self.Name == "PcdDxe": if self.Name == "PcdPeim" or self.Name == "PcdDxe":
CreatePcdDatabaseCode(self, TemplateString(), TemplateString()) CreatePcdDatabaseCode(self, TemplateString(), TemplateString())
with GlobalData.file_lock: with GlobalData.cache_lock:
IR = gDict[(self.MetaFile.Path, self.Arch)] IR = gDict[(self.MetaFile.Path, self.Arch)]
IR.MakeCacheHit = True IR.MakeCacheHit = True
gDict[(self.MetaFile.Path, self.Arch)] = IR gDict[(self.MetaFile.Path, self.Arch)] = IR
@ -2395,6 +2433,10 @@ class ModuleAutoGen(AutoGen):
if not GlobalData.gBinCacheSource: if not GlobalData.gBinCacheSource:
return return
# skip if the module cache already crashed
if gDict[(self.MetaFile.Path, self.Arch)].CacheCrash:
return
# skip binary module # skip binary module
if self.IsBinaryModule: if self.IsBinaryModule:
return return
@ -2420,9 +2462,8 @@ class ModuleAutoGen(AutoGen):
return return
try: try:
f = open(ModuleHashPair, 'r') with open(ModuleHashPair, 'r') as f:
ModuleHashPairList = json.load(f) ModuleHashPairList = json.load(f)
f.close()
except: except:
EdkLogger.quiet("[cache insight]: Cannot load ModuleHashPair file for module: %s[%s]" % (self.MetaFile.Path, self.Arch)) EdkLogger.quiet("[cache insight]: Cannot load ModuleHashPair file for module: %s[%s]" % (self.MetaFile.Path, self.Arch))
return return

View File

@ -122,6 +122,8 @@ gBuildHashSkipTracking = dict()
# Common dictionary to share module cache intermediate result and state # Common dictionary to share module cache intermediate result and state
gCacheIR = None gCacheIR = None
# Common lock for the module cache intermediate data
cache_lock = None
# Common lock for the file access in multiple process AutoGens # Common lock for the file access in multiple process AutoGens
file_lock = None file_lock = None
# Common dictionary to share platform libraries' constant Pcd # Common dictionary to share platform libraries' constant Pcd

44
BaseTools/Source/Python/Common/Misc.py Normal file → Executable file
View File

@ -448,7 +448,7 @@ def RemoveDirectory(Directory, Recursively=False):
# @retval True If the file content is changed and the file is renewed # @retval True If the file content is changed and the file is renewed
# @retval False If the file content is the same # @retval False If the file content is the same
# #
def SaveFileOnChange(File, Content, IsBinaryFile=True): def SaveFileOnChange(File, Content, IsBinaryFile=True, FileLock=None):
if os.path.exists(File): if os.path.exists(File):
if IsBinaryFile: if IsBinaryFile:
@ -479,6 +479,13 @@ def SaveFileOnChange(File, Content, IsBinaryFile=True):
if IsBinaryFile: if IsBinaryFile:
OpenMode = "wb" OpenMode = "wb"
# use default file_lock if no input new lock
if not FileLock:
FileLock = GlobalData.file_lock
if FileLock:
FileLock.acquire()
if GlobalData.gIsWindows and not os.path.exists(File): if GlobalData.gIsWindows and not os.path.exists(File):
# write temp file, then rename the temp file to the real file # write temp file, then rename the temp file to the real file
# to make sure the file be immediate saved to disk # to make sure the file be immediate saved to disk
@ -487,14 +494,26 @@ def SaveFileOnChange(File, Content, IsBinaryFile=True):
tempname = tf.name tempname = tf.name
try: try:
os.rename(tempname, File) os.rename(tempname, File)
except: except IOError as X:
EdkLogger.error(None, FILE_CREATE_FAILURE, ExtraData='IOError %s' % X) if GlobalData.gBinCacheSource:
EdkLogger.quiet("[cache error]:fails to save file with error: %s" % (X))
else:
EdkLogger.error(None, FILE_CREATE_FAILURE, ExtraData='IOError %s' % X)
finally:
if FileLock:
FileLock.release()
else: else:
try: try:
with open(File, OpenMode) as Fd: with open(File, OpenMode) as Fd:
Fd.write(Content) Fd.write(Content)
except IOError as X: except IOError as X:
EdkLogger.error(None, FILE_CREATE_FAILURE, ExtraData='IOError %s' % X) if GlobalData.gBinCacheSource:
EdkLogger.quiet("[cache error]:fails to save file with error: %s" % (X))
else:
EdkLogger.error(None, FILE_CREATE_FAILURE, ExtraData='IOError %s' % X)
finally:
if FileLock:
FileLock.release()
return True return True
@ -510,7 +529,7 @@ def SaveFileOnChange(File, Content, IsBinaryFile=True):
# @retval True The two files content are different and the file is copied # @retval True The two files content are different and the file is copied
# @retval False No copy really happen # @retval False No copy really happen
# #
def CopyFileOnChange(SrcFile, Dst): def CopyFileOnChange(SrcFile, Dst, FileLock=None):
if not os.path.exists(SrcFile): if not os.path.exists(SrcFile):
return False return False
@ -531,6 +550,12 @@ def CopyFileOnChange(SrcFile, Dst):
if not os.access(DirName, os.W_OK): if not os.access(DirName, os.W_OK):
EdkLogger.error(None, PERMISSION_FAILURE, "Do not have write permission on directory %s" % DirName) EdkLogger.error(None, PERMISSION_FAILURE, "Do not have write permission on directory %s" % DirName)
# use default file_lock if no input new lock
if not FileLock:
FileLock = GlobalData.file_lock
if FileLock:
FileLock.acquire()
# os.replace and os.rename are the atomic operations in python 3 and 2. # os.replace and os.rename are the atomic operations in python 3 and 2.
# we use these two atomic operations to ensure the file copy is atomic: # we use these two atomic operations to ensure the file copy is atomic:
# copy the src to a temp file in the dst same folder firstly, then # copy the src to a temp file in the dst same folder firstly, then
@ -546,9 +571,14 @@ def CopyFileOnChange(SrcFile, Dst):
if GlobalData.gIsWindows and os.path.exists(DstFile): if GlobalData.gIsWindows and os.path.exists(DstFile):
os.remove(DstFile) os.remove(DstFile)
os.rename(tempname, DstFile) os.rename(tempname, DstFile)
except IOError as X: except IOError as X:
EdkLogger.error(None, FILE_COPY_FAILURE, ExtraData='IOError %s' % X) if GlobalData.gBinCacheSource:
EdkLogger.quiet("[cache error]:fails to copy file with error: %s" % (X))
else:
EdkLogger.error(None, FILE_COPY_FAILURE, ExtraData='IOError %s' % X)
finally:
if FileLock:
FileLock.release()
return True return True

View File

@ -820,13 +820,15 @@ class Build():
file_lock = mp.Lock() file_lock = mp.Lock()
error_event = mp.Event() error_event = mp.Event()
GlobalData.file_lock = file_lock GlobalData.file_lock = file_lock
cache_lock = mp.Lock()
GlobalData.cache_lock = cache_lock
FfsCmd = DataPipe.Get("FfsCommand") FfsCmd = DataPipe.Get("FfsCommand")
if FfsCmd is None: if FfsCmd is None:
FfsCmd = {} FfsCmd = {}
GlobalData.FfsCmd = FfsCmd GlobalData.FfsCmd = FfsCmd
GlobalData.libConstPcd = DataPipe.Get("LibConstPcd") GlobalData.libConstPcd = DataPipe.Get("LibConstPcd")
GlobalData.Refes = DataPipe.Get("REFS") GlobalData.Refes = DataPipe.Get("REFS")
auto_workers = [AutoGenWorkerInProcess(mqueue,DataPipe.dump_file,feedback_q,file_lock,share_data,self.log_q,error_event) for _ in range(self.ThreadNumber)] auto_workers = [AutoGenWorkerInProcess(mqueue,DataPipe.dump_file,feedback_q,file_lock,cache_lock,share_data,self.log_q,error_event) for _ in range(self.ThreadNumber)]
self.AutoGenMgr = AutoGenManager(auto_workers,feedback_q,error_event) self.AutoGenMgr = AutoGenManager(auto_workers,feedback_q,error_event)
self.AutoGenMgr.start() self.AutoGenMgr.start()
for w in auto_workers: for w in auto_workers:
@ -1826,6 +1828,7 @@ class Build():
for PkgName in GlobalData.gPackageHash.keys(): for PkgName in GlobalData.gPackageHash.keys():
GlobalData.gCacheIR[(PkgName, 'PackageHash')] = GlobalData.gPackageHash[PkgName] GlobalData.gCacheIR[(PkgName, 'PackageHash')] = GlobalData.gPackageHash[PkgName]
GlobalData.file_lock = mp.Lock() GlobalData.file_lock = mp.Lock()
GlobalData.cache_lock = mp.Lock()
GlobalData.FfsCmd = CmdListDict GlobalData.FfsCmd = CmdListDict
self.Progress.Stop("done!") self.Progress.Stop("done!")