authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-03-11 12:47:55-04:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2019-03-11 12:47:55-04:00
log80e5af2be21427b8590c31e21c8e6b4cae1b7a6e
tree318a78764265ac2c97287fe0eda33e57a1d46cf8
parent9efa18f687a8c05f6651df6a7455a39c3d42d212
parent3ff0e8bd96bc6bf1d8eb4985c6d56766dab578f2
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #2049 from ziglang/problematic-mtime-detection

stage1 caching system: detect problematic mtimes

10 files changed, 189 insertions(+), 85 deletions(-)

doc/docgen.zig-6
......@@ -1088,8 +1088,6 @@ fn genHtml(allocator: *mem.Allocator, tokenizer: *Tokenizer, toc: *Toc, out: var
10881088 tmp_source_file_name,
10891089 "--output-dir",
10901090 tmp_dir_name,
1091 "--cache",
1092 "off",
10931091 });
10941092 try out.print("<pre><code class=\"shell\">$ zig test {}.zig", code.name);
10951093 switch (code.mode) {
......@@ -1127,8 +1125,6 @@ fn genHtml(allocator: *mem.Allocator, tokenizer: *Tokenizer, toc: *Toc, out: var
11271125 tmp_source_file_name,
11281126 "--output-dir",
11291127 tmp_dir_name,
1130 "--cache",
1131 "off",
11321128 });
11331129 try out.print("<pre><code class=\"shell\">$ zig test {}.zig", code.name);
11341130 switch (code.mode) {
......@@ -1186,8 +1182,6 @@ fn genHtml(allocator: *mem.Allocator, tokenizer: *Tokenizer, toc: *Toc, out: var
11861182 tmp_source_file_name,
11871183 "--output-dir",
11881184 tmp_dir_name,
1189 "--cache",
1190 "off",
11911185 });
11921186 switch (code.mode) {
11931187 builtin.Mode.Debug => {},
src/cache_hash.cpp+71-26
......@@ -158,8 +158,10 @@ static void base64_encode(Slice<uint8_t> dest, Slice<uint8_t> source) {
158158
159159// Ported from std/base64.zig
160160static Error base64_decode(Slice<uint8_t> dest, Slice<uint8_t> source) {
161 assert(source.len % 4 == 0);
162 assert(dest.len == (source.len / 4) * 3);
161 if (source.len % 4 != 0)
162 return ErrorInvalidFormat;
163 if (dest.len != (source.len / 4) * 3)
164 return ErrorInvalidFormat;
163165
164166 // In Zig this is comptime computed. In C++ it's not worth it to do that.
165167 uint8_t char_to_index[256];
......@@ -218,15 +220,41 @@ static Error hash_file(uint8_t *digest, OsFile handle, Buf *contents) {
218220 }
219221}
220222
223// If the wall clock time, rounded to the same precision as the
224// mtime, is equal to the mtime, then we cannot rely on this mtime
225// yet. We will instead save an mtime value that indicates the hash
226// must be unconditionally computed.
227static bool is_problematic_timestamp(const OsTimeStamp *fs_clock) {
228 OsTimeStamp wall_clock = os_timestamp_calendar();
229 // First make all the least significant zero bits in the fs_clock, also zero bits in the wall clock.
230 if (fs_clock->nsec == 0) {
231 wall_clock.nsec = 0;
232 if (fs_clock->sec == 0) {
233 wall_clock.sec = 0;
234 } else {
235 wall_clock.sec &= (-1ull) << ctzll(fs_clock->sec);
236 }
237 } else {
238 wall_clock.nsec &= (-1ull) << ctzll(fs_clock->nsec);
239 }
240 return wall_clock.nsec == fs_clock->nsec && wall_clock.sec == fs_clock->sec;
241}
242
221243static Error populate_file_hash(CacheHash *ch, CacheHashFile *chf, Buf *contents) {
222244 Error err;
223245
224246 assert(chf->path != nullptr);
225247
226248 OsFile this_file;
227 if ((err = os_file_open_r(chf->path, &this_file, &chf->mtime)))
249 if ((err = os_file_open_r(chf->path, &this_file, &chf->attr)))
228250 return err;
229251
252 if (is_problematic_timestamp(&chf->attr.mtime)) {
253 chf->attr.mtime.sec = 0;
254 chf->attr.mtime.nsec = 0;
255 chf->attr.inode = 0;
256 }
257
230258 if ((err = hash_file(chf->bin_digest, this_file, contents))) {
231259 os_file_close(this_file);
232260 return err;
......@@ -278,6 +306,7 @@ Error cache_hit(CacheHash *ch, Buf *out_digest) {
278306
279307 size_t input_file_count = ch->files.length;
280308 bool any_file_changed = false;
309 Error return_code = ErrorNone;
281310 size_t file_i = 0;
282311 SplitIterator line_it = memSplit(buf_to_slice(&line_buf), str("\n"));
283312 for (;; file_i += 1) {
......@@ -299,7 +328,7 @@ Error cache_hit(CacheHash *ch, Buf *out_digest) {
299328 blake2b_update(&ch->blake, ch->files.at(file_i).bin_digest, 48);
300329 }
301330 // caller can notice that out_digest is unmodified.
302 return ErrorNone;
331 return return_code;
303332 } else if (!opt_line.is_some) {
304333 break;
305334 } else {
......@@ -312,57 +341,73 @@ Error cache_hit(CacheHash *ch, Buf *out_digest) {
312341
313342 SplitIterator it = memSplit(opt_line.value, str(" "));
314343
344 Optional<Slice<uint8_t>> opt_inode = SplitIterator_next(&it);
345 if (!opt_inode.is_some) {
346 return_code = ErrorInvalidFormat;
347 break;
348 }
349 chf->attr.inode = strtoull((const char *)opt_inode.value.ptr, nullptr, 10);
350
315351 Optional<Slice<uint8_t>> opt_mtime_sec = SplitIterator_next(&it);
316352 if (!opt_mtime_sec.is_some) {
317 os_file_close(ch->manifest_file);
318 return ErrorInvalidFormat;
353 return_code = ErrorInvalidFormat;
354 break;
319355 }
320 chf->mtime.sec = strtoull((const char *)opt_mtime_sec.value.ptr, nullptr, 10);
356 chf->attr.mtime.sec = strtoull((const char *)opt_mtime_sec.value.ptr, nullptr, 10);
321357
322358 Optional<Slice<uint8_t>> opt_mtime_nsec = SplitIterator_next(&it);
323359 if (!opt_mtime_nsec.is_some) {
324 os_file_close(ch->manifest_file);
325 return ErrorInvalidFormat;
360 return_code = ErrorInvalidFormat;
361 break;
326362 }
327 chf->mtime.nsec = strtoull((const char *)opt_mtime_nsec.value.ptr, nullptr, 10);
363 chf->attr.mtime.nsec = strtoull((const char *)opt_mtime_nsec.value.ptr, nullptr, 10);
328364
329365 Optional<Slice<uint8_t>> opt_digest = SplitIterator_next(&it);
330366 if (!opt_digest.is_some) {
331 os_file_close(ch->manifest_file);
332 return ErrorInvalidFormat;
367 return_code = ErrorInvalidFormat;
368 break;
333369 }
334370 if ((err = base64_decode({chf->bin_digest, 48}, opt_digest.value))) {
335 os_file_close(ch->manifest_file);
336 return ErrorInvalidFormat;
371 return_code = ErrorInvalidFormat;
372 break;
337373 }
338374
339375 Slice<uint8_t> file_path = SplitIterator_rest(&it);
340376 if (file_path.len == 0) {
341 os_file_close(ch->manifest_file);
342 return ErrorInvalidFormat;
377 return_code = ErrorInvalidFormat;
378 break;
343379 }
344380 Buf *this_path = buf_create_from_slice(file_path);
345381 if (chf->path != nullptr && !buf_eql_buf(this_path, chf->path)) {
346 os_file_close(ch->manifest_file);
347 return ErrorInvalidFormat;
382 return_code = ErrorInvalidFormat;
383 break;
348384 }
349385 chf->path = this_path;
350386
351387 // if the mtime matches we can trust the digest
352388 OsFile this_file;
353 OsTimeStamp actual_mtime;
354 if ((err = os_file_open_r(chf->path, &this_file, &actual_mtime))) {
389 OsFileAttr actual_attr;
390 if ((err = os_file_open_r(chf->path, &this_file, &actual_attr))) {
355391 fprintf(stderr, "Unable to open %s\n: %s", buf_ptr(chf->path), err_str(err));
356392 os_file_close(ch->manifest_file);
357393 return ErrorCacheUnavailable;
358394 }
359 if (chf->mtime.sec == actual_mtime.sec && chf->mtime.nsec == actual_mtime.nsec) {
395 if (chf->attr.mtime.sec == actual_attr.mtime.sec &&
396 chf->attr.mtime.nsec == actual_attr.mtime.nsec &&
397 chf->attr.inode == actual_attr.inode)
398 {
360399 os_file_close(this_file);
361400 } else {
362401 // we have to recompute the digest.
363402 // later we'll rewrite the manifest with the new mtime/digest values
364403 ch->manifest_dirty = true;
365 chf->mtime = actual_mtime;
404 chf->attr = actual_attr;
405
406 if (is_problematic_timestamp(&actual_attr.mtime)) {
407 chf->attr.mtime.sec = 0;
408 chf->attr.mtime.nsec = 0;
409 chf->attr.inode = 0;
410 }
366411
367412 uint8_t actual_digest[48];
368413 if ((err = hash_file(actual_digest, this_file, nullptr))) {
......@@ -381,7 +426,7 @@ Error cache_hit(CacheHash *ch, Buf *out_digest) {
381426 blake2b_update(&ch->blake, chf->bin_digest, 48);
382427 }
383428 }
384 if (file_i < input_file_count || file_i == 0) {
429 if (file_i < input_file_count || file_i == 0 || return_code != ErrorNone) {
385430 // manifest file is empty or missing entries, so this is a cache miss
386431 ch->manifest_dirty = true;
387432 for (; file_i < input_file_count; file_i += 1) {
......@@ -392,7 +437,7 @@ Error cache_hit(CacheHash *ch, Buf *out_digest) {
392437 return ErrorCacheUnavailable;
393438 }
394439 }
395 return ErrorNone;
440 return return_code;
396441 }
397442 // Cache Hit
398443 return cache_final(ch, out_digest);
......@@ -499,8 +544,8 @@ static Error write_manifest_file(CacheHash *ch) {
499544 for (size_t i = 0; i < ch->files.length; i += 1) {
500545 CacheHashFile *chf = &ch->files.at(i);
501546 base64_encode({encoded_digest, 64}, {chf->bin_digest, 48});
502 buf_appendf(&contents, "%" ZIG_PRI_u64 " %" ZIG_PRI_u64 " %s %s\n",
503 chf->mtime.sec, chf->mtime.nsec, encoded_digest, buf_ptr(chf->path));
547 buf_appendf(&contents, "%" ZIG_PRI_u64 " %" ZIG_PRI_u64 " %" ZIG_PRI_u64 " %s %s\n",
548 chf->attr.inode, chf->attr.mtime.sec, chf->attr.mtime.nsec, encoded_digest, buf_ptr(chf->path));
504549 }
505550 if ((err = os_file_overwrite(ch->manifest_file, &contents)))
506551 return err;
src/cache_hash.hpp+3-1
......@@ -15,7 +15,7 @@ struct LinkLib;
1515
1616struct CacheHashFile {
1717 Buf *path;
18 OsTimeStamp mtime;
18 OsFileAttr attr;
1919 uint8_t bin_digest[48];
2020 Buf *contents;
2121};
......@@ -57,6 +57,8 @@ void cache_file_opt(CacheHash *ch, Buf *path);
5757// added any files before calling cache_hit. CacheHash::b64_digest becomes
5858// available for use after this call, even in the case of a miss, and it
5959// is a hash of the input parameters only.
60// If this function returns ErrorInvalidFormat, that error may be treated
61// as a cache miss.
6062Error ATTRIBUTE_MUST_USE cache_hit(CacheHash *ch, Buf *out_b64_digest);
6163
6264// If you did not get a cache hit, call this function for every file
src/codegen.cpp+20-10
......@@ -7724,8 +7724,11 @@ static Error define_builtin_compile_vars(CodeGen *g) {
77247724
77257725 Buf digest = BUF_INIT;
77267726 buf_resize(&digest, 0);
7727 if ((err = cache_hit(&cache_hash, &digest)))
7728 return err;
7727 if ((err = cache_hit(&cache_hash, &digest))) {
7728 // Treat an invalid format error as a cache miss.
7729 if (err != ErrorInvalidFormat)
7730 return err;
7731 }
77297732
77307733 // We should always get a cache hit because there are no
77317734 // files in the input hash.
......@@ -8342,12 +8345,14 @@ static void gen_c_object(CodeGen *g, Buf *self_exe_path, CFile *c_file) {
83428345 Buf digest = BUF_INIT;
83438346 buf_resize(&digest, 0);
83448347 if ((err = cache_hit(cache_hash, &digest))) {
8345 if (err == ErrorCacheUnavailable) {
8346 // already printed error
8347 } else {
8348 fprintf(stderr, "unable to check cache when compiling C object: %s\n", err_str(err));
8348 if (err != ErrorInvalidFormat) {
8349 if (err == ErrorCacheUnavailable) {
8350 // already printed error
8351 } else {
8352 fprintf(stderr, "unable to check cache when compiling C object: %s\n", err_str(err));
8353 }
8354 exit(1);
83498355 }
8350 exit(1);
83518356 }
83528357 bool is_cache_miss = (buf_len(&digest) == 0);
83538358 if (is_cache_miss) {
......@@ -8993,7 +8998,10 @@ void codegen_print_timing_report(CodeGen *g, FILE *f) {
89938998}
89948999
89959000void codegen_add_time_event(CodeGen *g, const char *name) {
8996 g->timing_events.append({os_get_time(), name});
9001 OsTimeStamp timestamp = os_timestamp_monotonic();
9002 double seconds = (double)timestamp.sec;
9003 seconds += ((double)timestamp.nsec) / 1000000000.0;
9004 g->timing_events.append({seconds, name});
89979005}
89989006
89999007static void add_cache_pkg(CodeGen *g, CacheHash *ch, ZigPackage *pkg) {
......@@ -9090,8 +9098,10 @@ static Error check_cache(CodeGen *g, Buf *manifest_dir, Buf *digest) {
90909098 cache_list_of_file(ch, g->link_objects.items, g->link_objects.length);
90919099
90929100 buf_resize(digest, 0);
9093 if ((err = cache_hit(ch, digest)))
9094 return err;
9101 if ((err = cache_hit(ch, digest))) {
9102 if (err != ErrorInvalidFormat)
9103 return err;
9104 }
90959105
90969106 if (ch->manifest_file_path != nullptr) {
90979107 g->caches_to_release.append(ch);
src/compiler.cpp+4-2
......@@ -75,8 +75,10 @@ Error get_compiler_id(Buf **result) {
7575 cache_file(ch, &self_exe_path);
7676
7777 buf_resize(&saved_compiler_id, 0);
78 if ((err = cache_hit(ch, &saved_compiler_id)))
79 return err;
78 if ((err = cache_hit(ch, &saved_compiler_id))) {
79 if (err != ErrorInvalidFormat)
80 return err;
81 }
8082 if (buf_len(&saved_compiler_id) != 0) {
8183 cache_release(ch);
8284 *result = &saved_compiler_id;
src/ir.cpp+4-2
......@@ -18732,8 +18732,10 @@ static IrInstruction *ir_analyze_instruction_c_import(IrAnalyze *ira, IrInstruct
1873218732 Buf tmp_c_file_digest = BUF_INIT;
1873318733 buf_resize(&tmp_c_file_digest, 0);
1873418734 if ((err = cache_hit(cache_hash, &tmp_c_file_digest))) {
18735 ir_add_error_node(ira, node, buf_sprintf("C import failed: unable to check cache: %s", err_str(err)));
18736 return ira->codegen->invalid_instruction;
18735 if (err != ErrorInvalidFormat) {
18736 ir_add_error_node(ira, node, buf_sprintf("C import failed: unable to check cache: %s", err_str(err)));
18737 return ira->codegen->invalid_instruction;
18738 }
1873718739 }
1873818740 ira->codegen->caches_to_release.append(cache_hash);
1873918741
src/os.cpp+61-29
......@@ -70,9 +70,10 @@ typedef SSIZE_T ssize_t;
7070#endif
7171
7272#if defined(ZIG_OS_WINDOWS)
73static double win32_time_resolution;
73static uint64_t windows_perf_freq;
7474#elif defined(__MACH__)
75static clock_serv_t cclock;
75static clock_serv_t macos_calendar_clock;
76static clock_serv_t macos_monotonic_clock;
7677#endif
7778
7879#include <stdlib.h>
......@@ -1233,28 +1234,60 @@ Error os_rename(Buf *src_path, Buf *dest_path) {
12331234 return ErrorNone;
12341235}
12351236
1236double os_get_time(void) {
12371237#if defined(ZIG_OS_WINDOWS)
1238 unsigned __int64 time;
1239 QueryPerformanceCounter((LARGE_INTEGER*) &time);
1240 return time * win32_time_resolution;
1238static void windows_filetime_to_os_timestamp(FILETIME *ft, OsTimeStamp *mtime) {
1239 mtime->sec = (((ULONGLONG) ft->dwHighDateTime) << 32) + ft->dwLowDateTime;
1240 mtime->nsec = 0;
1241}
1242#endif
1243
1244OsTimeStamp os_timestamp_calendar(void) {
1245 OsTimeStamp result;
1246#if defined(ZIG_OS_WINDOWS)
1247 FILETIME ft;
1248 GetSystemTimeAsFileTime(&ft);
1249 windows_filetime_to_os_timestamp(&ft, &result);
12411250#elif defined(__MACH__)
12421251 mach_timespec_t mts;
12431252
1244 kern_return_t err = clock_get_time(cclock, &mts);
1253 kern_return_t err = clock_get_time(macos_calendar_clock, &mts);
12451254 assert(!err);
12461255
1247 double seconds = (double)mts.tv_sec;
1248 seconds += ((double)mts.tv_nsec) / 1000000000.0;
1256 result.sec = mts.tv_sec;
1257 result.nsec = mts.tv_nsec;
1258#else
1259 struct timespec tms;
1260 clock_gettime(CLOCK_REALTIME, &tms);
1261
1262 result.sec = tms.tv_sec;
1263 result.nsec = tms.tv_nsec;
1264#endif
1265 return result;
1266}
12491267
1250 return seconds;
1268OsTimeStamp os_timestamp_monotonic(void) {
1269 OsTimeStamp result;
1270#if defined(ZIG_OS_WINDOWS)
1271 uint64_t counts;
1272 QueryPerformanceCounter((LARGE_INTEGER*)&counts);
1273 result.sec = counts / windows_perf_freq;
1274 result.nsec = (counts % windows_perf_freq) * 1000000000u / windows_perf_freq;
1275#elif defined(__MACH__)
1276 mach_timespec_t mts;
1277
1278 kern_return_t err = clock_get_time(macos_monotonic_clock, &mts);
1279 assert(!err);
1280
1281 result.sec = mts.tv_sec;
1282 result.nsec = mts.tv_nsec;
12511283#else
12521284 struct timespec tms;
12531285 clock_gettime(CLOCK_MONOTONIC, &tms);
1254 double seconds = (double)tms.tv_sec;
1255 seconds += ((double)tms.tv_nsec) / 1000000000.0;
1256 return seconds;
1286
1287 result.sec = tms.tv_sec;
1288 result.nsec = tms.tv_nsec;
12571289#endif
1290 return result;
12581291}
12591292
12601293Error os_make_path(Buf *path) {
......@@ -1352,14 +1385,12 @@ int os_init(void) {
13521385#if defined(ZIG_OS_WINDOWS)
13531386 _setmode(fileno(stdout), _O_BINARY);
13541387 _setmode(fileno(stderr), _O_BINARY);
1355 unsigned __int64 frequency;
1356 if (QueryPerformanceFrequency((LARGE_INTEGER*) &frequency)) {
1357 win32_time_resolution = 1.0 / (double) frequency;
1358 } else {
1388 if (!QueryPerformanceFrequency((LARGE_INTEGER*)&windows_perf_freq)) {
13591389 return ErrorSystemResources;
13601390 }
13611391#elif defined(__MACH__)
1362 host_get_clock_service(mach_host_self(), SYSTEM_CLOCK, &cclock);
1392 host_get_clock_service(mach_host_self(), SYSTEM_CLOCK, &macos_monotonic_clock);
1393 host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, &macos_calendar_clock);
13631394#endif
13641395 return 0;
13651396}
......@@ -1780,7 +1811,7 @@ Error os_self_exe_shared_libs(ZigList<Buf *> &paths) {
17801811#endif
17811812}
17821813
1783Error os_file_open_r(Buf *full_path, OsFile *out_file, OsTimeStamp *mtime) {
1814Error os_file_open_r(Buf *full_path, OsFile *out_file, OsFileAttr *attr) {
17841815#if defined(ZIG_OS_WINDOWS)
17851816 // TODO use CreateFileW
17861817 HANDLE result = CreateFileA(buf_ptr(full_path), GENERIC_READ, FILE_SHARE_READ, nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr);
......@@ -1808,14 +1839,14 @@ Error os_file_open_r(Buf *full_path, OsFile *out_file, OsTimeStamp *mtime) {
18081839 }
18091840 *out_file = result;
18101841
1811 if (mtime != nullptr) {
1812 FILETIME last_write_time;
1813 if (!GetFileTime(result, nullptr, nullptr, &last_write_time)) {
1842 if (attr != nullptr) {
1843 BY_HANDLE_FILE_INFORMATION file_info;
1844 if (!GetFileInformationByHandle(result, &file_info)) {
18141845 CloseHandle(result);
18151846 return ErrorUnexpected;
18161847 }
1817 mtime->sec = (((ULONGLONG) last_write_time.dwHighDateTime) << 32) + last_write_time.dwLowDateTime;
1818 mtime->nsec = 0;
1848 windows_filetime_to_os_timestamp(&file_info.ftLastWriteTime, &attr->mtime);
1849 attr->inode = (((uint64_t)file_info.nFileIndexHigh) << 32) | file_info.nFileIndexLow;
18191850 }
18201851
18211852 return ErrorNone;
......@@ -1851,13 +1882,14 @@ Error os_file_open_r(Buf *full_path, OsFile *out_file, OsTimeStamp *mtime) {
18511882 }
18521883 *out_file = fd;
18531884
1854 if (mtime != nullptr) {
1885 if (attr != nullptr) {
1886 attr->inode = statbuf.st_ino;
18551887#if defined(ZIG_OS_DARWIN)
1856 mtime->sec = statbuf.st_mtimespec.tv_sec;
1857 mtime->nsec = statbuf.st_mtimespec.tv_nsec;
1888 attr->mtime.sec = statbuf.st_mtimespec.tv_sec;
1889 attr->mtime.nsec = statbuf.st_mtimespec.tv_nsec;
18581890#else
1859 mtime->sec = statbuf.st_mtim.tv_sec;
1860 mtime->nsec = statbuf.st_mtim.tv_nsec;
1891 attr->mtime.sec = statbuf.st_mtim.tv_sec;
1892 attr->mtime.nsec = statbuf.st_mtim.tv_nsec;
18611893#endif
18621894 }
18631895 return ErrorNone;
src/os.hpp+8-2
......@@ -85,6 +85,11 @@ struct OsTimeStamp {
8585 uint64_t nsec;
8686};
8787
88struct OsFileAttr {
89 OsTimeStamp mtime;
90 uint64_t inode;
91};
92
8893int os_init(void);
8994
9095void os_spawn_process(const char *exe, ZigList<const char *> &args, Termination *term);
......@@ -103,7 +108,7 @@ bool os_path_is_absolute(Buf *path);
103108Error ATTRIBUTE_MUST_USE os_make_path(Buf *path);
104109Error ATTRIBUTE_MUST_USE os_make_dir(Buf *path);
105110
106Error ATTRIBUTE_MUST_USE os_file_open_r(Buf *full_path, OsFile *out_file, OsTimeStamp *mtime);
111Error ATTRIBUTE_MUST_USE os_file_open_r(Buf *full_path, OsFile *out_file, OsFileAttr *attr);
107112Error ATTRIBUTE_MUST_USE os_file_open_lock_rw(Buf *full_path, OsFile *out_file);
108113Error ATTRIBUTE_MUST_USE os_file_read(OsFile file, void *ptr, size_t *len);
109114Error ATTRIBUTE_MUST_USE os_file_read_all(OsFile file, Buf *contents);
......@@ -126,7 +131,8 @@ Error os_delete_file(Buf *path);
126131Error ATTRIBUTE_MUST_USE os_file_exists(Buf *full_path, bool *result);
127132
128133Error os_rename(Buf *src_path, Buf *dest_path);
129double os_get_time(void);
134OsTimeStamp os_timestamp_monotonic(void);
135OsTimeStamp os_timestamp_calendar(void);
130136
131137bool os_is_sep(uint8_t c);
132138
src/util.hpp+17
......@@ -63,10 +63,27 @@ static inline int clzll(unsigned long long mask) {
6363 return 63 - lz;
6464#endif
6565}
66static inline int ctzll(unsigned long long mask) {
67 unsigned long result;
68#if defined(_WIN64)
69 if (_BitScanForward64(&result, mask))
70 return result;
71 zig_unreachable();
72#else
73 if (_BitScanForward(&result, mask & 0xffffffff))
74 return result;
75 }
76 if (_BitScanForward(&result, mask >> 32))
77 return 32 + result;
78 zig_unreachable();
79#endif
80}
6681#else
6782#define clzll(x) __builtin_clzll(x)
83#define ctzll(x) __builtin_ctzll(x)
6884#endif
6985
86
7087template<typename T>
7188ATTRIBUTE_RETURNS_NOALIAS static inline T *allocate_nonzero(size_t count) {
7289#ifndef NDEBUG
test/tests.zig+1-7
......@@ -158,7 +158,6 @@ pub fn addGenHTests(b: *build.Builder, test_filter: ?[]const u8) *build.Step {
158158 .step = b.step("test-gen-h", "Run the C header file generation tests"),
159159 .test_index = 0,
160160 .test_filter = test_filter,
161 .counter = 0,
162161 };
163162
164163 gen_h.addCases(cases);
......@@ -1105,7 +1104,6 @@ pub const GenHContext = struct {
11051104 step: *build.Step,
11061105 test_index: usize,
11071106 test_filter: ?[]const u8,
1108 counter: usize,
11091107
11101108 const TestCase = struct {
11111109 name: []const u8,
......@@ -1208,11 +1206,7 @@ pub const GenHContext = struct {
12081206 }
12091207
12101208 pub fn add(self: *GenHContext, name: []const u8, source: []const u8, expected_lines: ...) void {
1211 // MacOS appears to not be returning nanoseconds in fstat mtime,
1212 // which causes fast test executions to think the file contents are unchanged.
1213 const modified_name = self.b.fmt("test-{}.zig", self.counter);
1214 self.counter += 1;
1215 const tc = self.create(modified_name, name, source, expected_lines);
1209 const tc = self.create("test.zig", name, source, expected_lines);
12161210 self.addCase(tc);
12171211 }
12181212