diff options
author | Shi, Steven <steven.shi@intel.com> | 2019-08-15 22:26:21 +0800 |
---|---|---|
committer | Feng, Bob C <bob.c.feng@intel.com> | 2019-08-20 14:06:07 +0800 |
commit | 94459080c118049aba927ec0444ba5b750b7d2c9 (patch) | |
tree | 0f9590dd9d9abd4315ad3663dbb21b8fe74308b7 /BaseTools/Source/Python/AutoGen | |
parent | d01a998612422a70366b3b6b4639d5f54de053a0 (diff) | |
download | edk2-94459080c118049aba927ec0444ba5b750b7d2c9.tar.gz edk2-94459080c118049aba927ec0444ba5b750b7d2c9.tar.bz2 edk2-94459080c118049aba927ec0444ba5b750b7d2c9.zip |
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>
Diffstat (limited to 'BaseTools/Source/Python/AutoGen')
-rwxr-xr-x | BaseTools/Source/Python/AutoGen/AutoGenWorker.py | 6 | ||||
-rwxr-xr-x | BaseTools/Source/Python/AutoGen/CacheIR.py | 1 | ||||
-rwxr-xr-x | BaseTools/Source/Python/AutoGen/DataPipe.py | 2 | ||||
-rwxr-xr-x[-rw-r--r--] | BaseTools/Source/Python/AutoGen/GenC.py | 0 | ||||
-rwxr-xr-x | BaseTools/Source/Python/AutoGen/ModuleAutoGen.py | 101 |
5 files changed, 76 insertions, 34 deletions
diff --git a/BaseTools/Source/Python/AutoGen/AutoGenWorker.py b/BaseTools/Source/Python/AutoGen/AutoGenWorker.py index 30d2f96fc7..2e68538b1c 100755 --- a/BaseTools/Source/Python/AutoGen/AutoGenWorker.py +++ b/BaseTools/Source/Python/AutoGen/AutoGenWorker.py @@ -133,7 +133,7 @@ class AutoGenManager(threading.Thread): 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
@@ -141,6 +141,7 @@ class AutoGenWorkerInProcess(mp.Process): 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
@@ -184,9 +185,10 @@ class AutoGenWorkerInProcess(mp.Process): 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"):
diff --git a/BaseTools/Source/Python/AutoGen/CacheIR.py b/BaseTools/Source/Python/AutoGen/CacheIR.py index 2d9ffe3f0b..715be5273c 100755 --- a/BaseTools/Source/Python/AutoGen/CacheIR.py +++ b/BaseTools/Source/Python/AutoGen/CacheIR.py @@ -24,5 +24,6 @@ class ModuleBuildCacheIR(): self.MakeHashDigest = None
self.MakeHashHexDigest = None
self.MakeHashChain = []
+ self.CacheCrash = False
self.PreMakeCacheHit = False
self.MakeCacheHit = False
diff --git a/BaseTools/Source/Python/AutoGen/DataPipe.py b/BaseTools/Source/Python/AutoGen/DataPipe.py index 87a1a125c8..2ca4f9ff4a 100755 --- a/BaseTools/Source/Python/AutoGen/DataPipe.py +++ b/BaseTools/Source/Python/AutoGen/DataPipe.py @@ -163,6 +163,4 @@ class MemoryDataPipe(DataPipe): self.DataContainer = {"BinCacheDest":GlobalData.gBinCacheDest}
- self.DataContainer = {"CacheIR":GlobalData.gCacheIR}
-
self.DataContainer = {"EnableGenfdsMultiThread":GlobalData.gEnableGenfdsMultiThread}
\ No newline at end of file diff --git a/BaseTools/Source/Python/AutoGen/GenC.py b/BaseTools/Source/Python/AutoGen/GenC.py index 910c8fe370..910c8fe370 100644..100755 --- a/BaseTools/Source/Python/AutoGen/GenC.py +++ b/BaseTools/Source/Python/AutoGen/GenC.py diff --git a/BaseTools/Source/Python/AutoGen/ModuleAutoGen.py b/BaseTools/Source/Python/AutoGen/ModuleAutoGen.py index ffa4727e59..2cd0d3859e 100755 --- a/BaseTools/Source/Python/AutoGen/ModuleAutoGen.py +++ b/BaseTools/Source/Python/AutoGen/ModuleAutoGen.py @@ -28,6 +28,7 @@ from Common.caching import cached_class_function 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"}
@@ -1702,9 +1703,8 @@ class ModuleAutoGen(AutoGen): 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))
@@ -1766,10 +1766,12 @@ class ModuleAutoGen(AutoGen): 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:
@@ -1806,7 +1808,7 @@ class ModuleAutoGen(AutoGen): 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
@@ -1891,7 +1893,7 @@ class ModuleAutoGen(AutoGen): 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
@@ -1951,9 +1953,8 @@ class ModuleAutoGen(AutoGen): 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
@@ -1974,6 +1975,11 @@ class ModuleAutoGen(AutoGen): 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)
@@ -2021,9 +2027,8 @@ class ModuleAutoGen(AutoGen): 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()))
@@ -2032,7 +2037,7 @@ class ModuleAutoGen(AutoGen): 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()
@@ -2050,6 +2055,11 @@ class ModuleAutoGen(AutoGen): 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
@@ -2091,7 +2101,7 @@ class ModuleAutoGen(AutoGen): # 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
@@ -2104,6 +2114,11 @@ class ModuleAutoGen(AutoGen): 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
@@ -2159,7 +2174,7 @@ class ModuleAutoGen(AutoGen): 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
@@ -2174,6 +2189,11 @@ class ModuleAutoGen(AutoGen): 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
@@ -2222,7 +2242,7 @@ class ModuleAutoGen(AutoGen): 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()
@@ -2236,6 +2256,12 @@ class ModuleAutoGen(AutoGen): 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
@@ -2255,12 +2281,15 @@ class ModuleAutoGen(AutoGen): 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
@@ -2300,7 +2329,7 @@ class ModuleAutoGen(AutoGen): 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
@@ -2313,6 +2342,12 @@ class ModuleAutoGen(AutoGen): 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)
@@ -2321,7 +2356,7 @@ class ModuleAutoGen(AutoGen): # .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
@@ -2338,12 +2373,15 @@ class ModuleAutoGen(AutoGen): 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
@@ -2383,7 +2421,7 @@ class ModuleAutoGen(AutoGen): 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
@@ -2395,6 +2433,10 @@ class ModuleAutoGen(AutoGen): 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
@@ -2420,9 +2462,8 @@ class ModuleAutoGen(AutoGen): 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
|