Skip to content

Commit f75abbb

Browse files
authored
Merge pull request #98992 from AThousandShips/cache_clean_improve_3_x
[3.x] [Buildsystem] Improve cache handling
2 parents 9c92716 + 24118b9 commit f75abbb

File tree

2 files changed

+64
-38
lines changed

2 files changed

+64
-38
lines changed

SConstruct

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -796,12 +796,13 @@ elif selected_platform != "":
796796

797797
# The following only makes sense when the 'env' is defined, and assumes it is.
798798
if "env" in locals():
799-
# FIXME: This method mixes both cosmetic progress stuff and cache handling...
800799
methods.show_progress(env)
801800
# TODO: replace this with `env.Dump(format="json")`
802801
# once we start requiring SCons 4.0 as min version.
803802
methods.dump(env)
804803

804+
methods.clean_cache(env)
805+
805806

806807
def print_elapsed_time():
807808
elapsed_time_sec = round(time.time() - time_at_start, 3)

methods.py

Lines changed: 62 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1126,21 +1126,19 @@ def show_progress(env):
11261126
"fname": str(env.Dir("#")) + "/.scons_node_count",
11271127
}
11281128

1129-
import time, math
1129+
import math
11301130

11311131
class cache_progress:
1132-
# The default is 1 GB cache and 12 hours half life
1133-
def __init__(self, path=None, limit=1073741824, half_life=43200):
1132+
# The default is 1 GB cache
1133+
def __init__(self, path=None, limit=pow(1024, 3)):
11341134
self.path = path
11351135
self.limit = limit
1136-
self.exponent_scale = math.log(2) / half_life
11371136
if env["verbose"] and path != None:
11381137
screen.write(
11391138
"Current cache limit is {} (used: {})\n".format(
11401139
self.convert_size(limit), self.convert_size(self.get_size(path))
11411140
)
11421141
)
1143-
self.delete(self.file_list())
11441142

11451143
def __call__(self, node, *args, **kw):
11461144
if show_progress:
@@ -1158,12 +1156,65 @@ def __call__(self, node, *args, **kw):
11581156
screen.write("\r[Initial build] ")
11591157
screen.flush()
11601158

1159+
def convert_size(self, size_bytes):
1160+
if size_bytes == 0:
1161+
return "0 bytes"
1162+
size_name = ("bytes", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB")
1163+
i = int(math.floor(math.log(size_bytes, 1024)))
1164+
p = math.pow(1024, i)
1165+
s = round(size_bytes / p, 2)
1166+
return "%s %s" % (int(s) if i == 0 else s, size_name[i])
1167+
1168+
def get_size(self, start_path="."):
1169+
total_size = 0
1170+
for dirpath, dirnames, filenames in os.walk(start_path):
1171+
for f in filenames:
1172+
fp = os.path.join(dirpath, f)
1173+
total_size += os.path.getsize(fp)
1174+
return total_size
1175+
1176+
def progress_finish(target, source, env):
1177+
try:
1178+
with open(node_count_data["fname"], "w") as f:
1179+
f.write("%d\n" % node_count_data["count"])
1180+
except Exception:
1181+
pass
1182+
1183+
try:
1184+
with open(node_count_data["fname"]) as f:
1185+
node_count_data["max"] = int(f.readline())
1186+
except Exception:
1187+
pass
1188+
1189+
cache_directory = os.environ.get("SCONS_CACHE")
1190+
# Simple cache pruning, attached to SCons' progress callback. Trim the
1191+
# cache directory to a size not larger than cache_limit.
1192+
cache_limit = float(os.getenv("SCONS_CACHE_LIMIT", 1024)) * 1024 * 1024
1193+
progressor = cache_progress(cache_directory, cache_limit)
1194+
Progress(progressor, interval=node_count_data["interval"])
1195+
1196+
progress_finish_command = Command("progress_finish", [], progress_finish)
1197+
AlwaysBuild(progress_finish_command)
1198+
1199+
1200+
def clean_cache(env):
1201+
import atexit
1202+
import time
1203+
1204+
class cache_clean:
1205+
def __init__(self, path=None, limit=pow(1024, 3)):
1206+
self.path = path
1207+
self.limit = limit
1208+
1209+
def clean(self):
1210+
self.delete(self.file_list())
1211+
11611212
def delete(self, files):
11621213
if len(files) == 0:
11631214
return
11641215
if env["verbose"]:
11651216
# Utter something
1166-
screen.write("\rPurging %d %s from cache...\n" % (len(files), len(files) > 1 and "files" or "file"))
1217+
print("Purging %d %s from cache..." % (len(files), "files" if len(files) > 1 else "file"))
11671218
[os.remove(f) for f in files]
11681219

11691220
def file_list(self):
@@ -1197,46 +1248,20 @@ def file_list(self):
11971248
else:
11981249
return [x[0] for x in file_stat[mark:]]
11991250

1200-
def convert_size(self, size_bytes):
1201-
if size_bytes == 0:
1202-
return "0 bytes"
1203-
size_name = ("bytes", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB")
1204-
i = int(math.floor(math.log(size_bytes, 1024)))
1205-
p = math.pow(1024, i)
1206-
s = round(size_bytes / p, 2)
1207-
return "%s %s" % (int(s) if i == 0 else s, size_name[i])
1208-
1209-
def get_size(self, start_path="."):
1210-
total_size = 0
1211-
for dirpath, dirnames, filenames in os.walk(start_path):
1212-
for f in filenames:
1213-
fp = os.path.join(dirpath, f)
1214-
total_size += os.path.getsize(fp)
1215-
return total_size
1216-
1217-
def progress_finish(target, source, env):
1251+
def cache_finally():
1252+
nonlocal cleaner
12181253
try:
1219-
with open(node_count_data["fname"], "w") as f:
1220-
f.write("%d\n" % node_count_data["count"])
1221-
progressor.delete(progressor.file_list())
1254+
cleaner.clean()
12221255
except Exception:
12231256
pass
12241257

1225-
try:
1226-
with open(node_count_data["fname"]) as f:
1227-
node_count_data["max"] = int(f.readline())
1228-
except Exception:
1229-
pass
1230-
12311258
cache_directory = os.environ.get("SCONS_CACHE")
12321259
# Simple cache pruning, attached to SCons' progress callback. Trim the
12331260
# cache directory to a size not larger than cache_limit.
12341261
cache_limit = float(os.getenv("SCONS_CACHE_LIMIT", 1024)) * 1024 * 1024
1235-
progressor = cache_progress(cache_directory, cache_limit)
1236-
Progress(progressor, interval=node_count_data["interval"])
1262+
cleaner = cache_clean(cache_directory, cache_limit)
12371263

1238-
progress_finish_command = Command("progress_finish", [], progress_finish)
1239-
AlwaysBuild(progress_finish_command)
1264+
atexit.register(cache_finally)
12401265

12411266

12421267
def dump(env):

0 commit comments

Comments
 (0)