57
57
"-ffile-compilation-dir=" ,
58
58
)
59
59
60
+ _IGNORED_FILES = (
61
+ # This file seems to cause a crash in the indexer, as well as performance
62
+ # issues.
63
+ "simdutf.cpp" ,
64
+ )
65
+
66
+ _INDEXER_THREADS_PER_MERGE_QUEUE = 16
67
+ _INDEXER_PER_THREAD_MEMORY = 2 * 1024 ** 3 # 2 GiB
68
+
60
69
SRC = Path (os .getenv ("SRC" , "/src" ))
61
70
# On OSS-Fuzz build infra, $OUT is not /out.
62
71
OUT = Path (os .getenv ("OUT" , "/out" ))
63
72
INDEXES_PATH = Path (os .getenv ("INDEXES_PATH" , "/indexes" ))
64
73
FUZZER_ENGINE = os .getenv ("LIB_FUZZING_ENGINE" , "/usr/lib/libFuzzingEngine.a" )
65
74
66
75
76
+ def _get_available_memory () -> int :
77
+ """Returns the available memory in bytes."""
78
+ with open ("/proc/meminfo" , "r" ) as f :
79
+ for line in f :
80
+ if line .startswith ("MemAvailable:" ):
81
+ return int (line .split ()[1 ]) * 1024
82
+
83
+ raise RuntimeError ("Failed to get available memory" )
84
+
85
+
67
86
def rewrite_argv0 (argv : Sequence [str ], clang_toolchain : str ) -> list [str ]:
68
87
"""Rewrite argv[0] to point to the real clang location."""
69
88
# We do this because we've set PATH to our wrapper.
@@ -318,6 +337,16 @@ def run_indexer(build_id: str, linker_commands: dict[str, Any]):
318
337
with (compile_commands_dir / "full_compile_commands.json" ).open ("wt" ) as f :
319
338
json .dump (linker_commands ["full_compile_commands" ], f , indent = 2 )
320
339
340
+ # Auto-tune the number of threads and merge queues according to the number
341
+ # of cores and available memory.
342
+ # Note: this might require further tuning -- this might not work well if there
343
+ # are multiple binaries being linked/indexed at the same time.
344
+ num_cores = len (os .sched_getaffinity (0 ))
345
+ num_threads = max (
346
+ 1 , min (_get_available_memory () // _INDEXER_PER_THREAD_MEMORY , num_cores )
347
+ )
348
+ merge_queues = max (1 , num_threads // _INDEXER_THREADS_PER_MERGE_QUEUE )
349
+
321
350
cmd = [
322
351
_INDEXER_PATH ,
323
352
"--build_dir" ,
@@ -326,6 +355,10 @@ def run_indexer(build_id: str, linker_commands: dict[str, Any]):
326
355
index_dir .as_posix (),
327
356
"--source_dir" ,
328
357
SRC .as_posix (),
358
+ "--index_threads" ,
359
+ str (num_threads ),
360
+ "--merge_queues" ,
361
+ str (merge_queues ),
329
362
]
330
363
result = subprocess .run (cmd , check = False , capture_output = True )
331
364
if result .returncode != 0 :
@@ -425,7 +458,7 @@ def _filter_compile_commands(
425
458
directory = Path (compile_command ["directory" ])
426
459
427
460
cc_path = Path (directory / compile_command ["file" ])
428
- if cc_path in cu_paths :
461
+ if cc_path in cu_paths and cc_path . name not in _IGNORED_FILES :
429
462
filtered_compile_commands .append (compile_command )
430
463
used_cu_paths .add (cc_path )
431
464
else :
0 commit comments