authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-01-03 20:34:17-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-01-03 20:34:51-07:00
logc8e44d82bd4dcd8f6beeb771027d38a576674364
treed816481335ee9087e60dc5bac976607d0be5e23d
parent404dc9692e33099cc59925d5cf03805224fcb36e

stage2: remove the Cache deadlock detection code

It's more trouble than it's worth; it didn't even catch the most recent incident because it was across process boundaries anyway.

3 files changed, 0 insertions(+), 68 deletions(-)

src/Cache.zig-66
......@@ -12,15 +12,6 @@ const mem = std.mem;
1212const fmt = std.fmt;
1313const Allocator = std.mem.Allocator;
1414
15/// Process-scoped map keeping track of all locked Cache hashes, to detect deadlocks.
16/// This protection is conditionally compiled depending on `want_debug_deadlock`.
17var all_cache_digest_set: std.AutoHashMapUnmanaged(BinDigest, void) = .{};
18var all_cache_digest_lock: std.Mutex = .{};
19var all_cache_digest_allocator: ?*Allocator = null;
20const want_debug_deadlock = std.debug.runtime_safety;
21const DebugBinDigest = if (want_debug_deadlock) BinDigest else void;
22const null_debug_bin_digest = if (want_debug_deadlock) ([1]u8{0} ** bin_digest_len) else {};
23
2415/// Be sure to call `Manifest.deinit` after successful initialization.
2516pub fn obtain(cache: *const Cache) Manifest {
2617 return Manifest{
......@@ -169,15 +160,8 @@ pub const HashHelper = struct {
169160
170161pub const Lock = struct {
171162 manifest_file: fs.File,
172 debug_bin_digest: DebugBinDigest,
173163
174164 pub fn release(lock: *Lock) void {
175 if (want_debug_deadlock) {
176 const held = all_cache_digest_lock.acquire();
177 defer held.release();
178
179 all_cache_digest_set.removeAssertDiscard(lock.debug_bin_digest);
180 }
181165 lock.manifest_file.close();
182166 lock.* = undefined;
183167 }
......@@ -194,7 +178,6 @@ pub const Manifest = struct {
194178 manifest_dirty: bool,
195179 files: std.ArrayListUnmanaged(File) = .{},
196180 hex_digest: [hex_digest_len]u8,
197 debug_bin_digest: DebugBinDigest = null_debug_bin_digest,
198181 /// Populated when hit() returns an error because of one
199182 /// of the files listed in the manifest.
200183 failed_file_index: ?usize = null,
......@@ -267,31 +250,6 @@ pub const Manifest = struct {
267250 var bin_digest: BinDigest = undefined;
268251 self.hash.hasher.final(&bin_digest);
269252
270 if (want_debug_deadlock) {
271 self.debug_bin_digest = bin_digest;
272
273 const held = all_cache_digest_lock.acquire();
274 defer held.release();
275
276 if (all_cache_digest_allocator) |prev_gpa| {
277 if (prev_gpa != self.cache.gpa) {
278 @panic("The deadlock debug code in Cache depends on using the same allocator for everything");
279 }
280 } else {
281 all_cache_digest_allocator = self.cache.gpa;
282 }
283
284 const gop = try all_cache_digest_set.getOrPut(self.cache.gpa, bin_digest);
285 if (gop.found_existing) {
286 std.debug.print("Cache deadlock detected in Cache.hit. Manifest has {d} files:\n", .{self.files.items.len});
287 for (self.files.items) |file| {
288 const p: []const u8 = file.path orelse "(null)";
289 std.debug.print(" file: {s}\n", .{p});
290 }
291 @panic("Cache deadlock detected");
292 }
293 }
294
295253 _ = std.fmt.bufPrint(&self.hex_digest, "{x}", .{bin_digest}) catch unreachable;
296254
297255 self.hash.hasher = hasher_init;
......@@ -628,10 +586,8 @@ pub const Manifest = struct {
628586 pub fn toOwnedLock(self: *Manifest) Lock {
629587 const lock: Lock = .{
630588 .manifest_file = self.manifest_file.?,
631 .debug_bin_digest = self.debug_bin_digest,
632589 };
633590 self.manifest_file = null;
634 self.debug_bin_digest = null_debug_bin_digest;
635591 return lock;
636592 }
637593
......@@ -639,14 +595,6 @@ pub const Manifest = struct {
639595 /// `Manifest.hit` must be called first.
640596 /// Don't forget to call `writeManifest` before this!
641597 pub fn deinit(self: *Manifest) void {
642 if (want_debug_deadlock) {
643 if (!mem.eql(u8, &self.debug_bin_digest, &null_debug_bin_digest)) {
644 const held = all_cache_digest_lock.acquire();
645 defer held.release();
646
647 all_cache_digest_set.removeAssertDiscard(self.debug_bin_digest);
648 }
649 }
650598 if (self.manifest_file) |file| {
651599 file.close();
652600 }
......@@ -725,22 +673,11 @@ fn isProblematicTimestamp(fs_clock: i128) bool {
725673 return wall_nsec == fs_nsec and wall_sec == fs_sec;
726674}
727675
728pub fn deinitDebugMap() void {
729 if (!want_debug_deadlock) return;
730
731 if (all_cache_digest_set.count() != 0) {
732 @panic("there's a Cache not deinitialized somewhere");
733 }
734 const gpa = all_cache_digest_allocator orelse return;
735 all_cache_digest_set.clearAndFree(gpa);
736}
737
738676test "cache file and then recall it" {
739677 if (std.Target.current.os.tag == .wasi) {
740678 // https://github.com/ziglang/zig/issues/5437
741679 return error.SkipZigTest;
742680 }
743 defer deinitDebugMap();
744681
745682 const cwd = fs.cwd();
746683
......@@ -819,7 +756,6 @@ test "check that changing a file makes cache fail" {
819756 // https://github.com/ziglang/zig/issues/5437
820757 return error.SkipZigTest;
821758 }
822 defer deinitDebugMap();
823759 const cwd = fs.cwd();
824760
825761 const temp_file = "cache_hash_change_file_test.txt";
......@@ -896,7 +832,6 @@ test "no file inputs" {
896832 // https://github.com/ziglang/zig/issues/5437
897833 return error.SkipZigTest;
898834 }
899 defer deinitDebugMap();
900835 const cwd = fs.cwd();
901836 const temp_manifest_dir = "no_file_inputs_manifest_dir";
902837 defer cwd.deleteTree(temp_manifest_dir) catch {};
......@@ -942,7 +877,6 @@ test "Manifest with files added after initial hash work" {
942877 // https://github.com/ziglang/zig/issues/5437
943878 return error.SkipZigTest;
944879 }
945 defer deinitDebugMap();
946880 const cwd = fs.cwd();
947881
948882 const temp_file1 = "cache_hash_post_file_test1.txt";
src/main.zig-1
......@@ -3292,7 +3292,6 @@ fn detectNativeTargetInfo(gpa: *Allocator, cross_target: std.zig.CrossTarget) !s
32923292/// calls exit(0), and does not return.
32933293pub fn cleanExit() void {
32943294 if (std.builtin.mode == .Debug) {
3295 Cache.deinitDebugMap();
32963295 return;
32973296 } else {
32983297 process.exit(0);
src/test.zig-1
......@@ -518,7 +518,6 @@ pub const TestContext = struct {
518518 case.updates.deinit();
519519 }
520520 self.cases.deinit();
521 @import("Cache.zig").deinitDebugMap();
522521 self.* = undefined;
523522 }
524523