1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
|
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
/// \file
/// This file implements OnDiskCASLogger. The logger will write the timestamp
/// and events to a log file using filestream. The logger should be thread-safe
/// and process-safe because each write is small enough to atomically update the
/// file.
///
/// The logger can be enabled via `LLVM_CAS_LOG` environmental variable.
//
//===----------------------------------------------------------------------===//
#include "llvm/CAS/OnDiskCASLogger.h"
#include "llvm/ADT/StringSwitch.h"
#include "llvm/ADT/Twine.h"
#include "llvm/Support/Error.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/Path.h"
#include "llvm/Support/Process.h"
#include "llvm/Support/Threading.h"
#include "llvm/Support/raw_ostream.h"
#ifdef __APPLE__
#include <sys/time.h>
#endif
using namespace llvm;
using namespace llvm::cas;
using namespace llvm::cas::ondisk;
// The version number in this log should be bumped if the log format is changed
// in an incompatible way. It is currently a human-readable text file, so in
// practice this would be if the log changed to binary or other machine-
// readable format.
static constexpr StringLiteral Filename = "v1.log";
OnDiskCASLogger::OnDiskCASLogger(raw_fd_ostream &OS, bool LogAllocations)
: OS(OS), LogAllocations(LogAllocations) {}
OnDiskCASLogger::~OnDiskCASLogger() {
OS.flush();
delete &OS;
}
static bool isDisabledEnv(StringRef V) {
return StringSwitch<bool>(V)
.Case("0", true)
.CaseLower("no", true)
.CaseLower("false", true)
.Default(false);
}
Expected<std::unique_ptr<OnDiskCASLogger>>
OnDiskCASLogger::openIfEnabled(const Twine &Path) {
const char *V = getenv("LLVM_CAS_LOG");
if (V && !isDisabledEnv(V)) {
int LogLevel = -1;
StringRef(V).getAsInteger(10, LogLevel);
return OnDiskCASLogger::open(Path, /*LogAllocations=*/LogLevel > 1 ? true
: false);
}
return nullptr;
}
Expected<std::unique_ptr<OnDiskCASLogger>>
OnDiskCASLogger::open(const Twine &Path, bool LogAllocations) {
std::error_code EC;
SmallString<128> FullPath;
Path.toVector(FullPath);
sys::path::append(FullPath, Filename);
auto OS =
std::make_unique<raw_fd_ostream>(FullPath, EC, sys::fs::CD_OpenAlways,
sys::fs::FA_Write, sys::fs::OF_Append);
if (EC)
return createFileError(FullPath, EC);
// Buffer is not thread-safe.
OS->SetUnbuffered();
return std::unique_ptr<OnDiskCASLogger>(
new OnDiskCASLogger{*OS.release(), LogAllocations});
}
static uint64_t getTimestampMillis() {
#ifdef __APPLE__
// Using chrono is roughly 50% slower.
struct timeval T;
gettimeofday(&T, 0);
return T.tv_sec * 1000 + T.tv_usec / 1000;
#else
auto Time = std::chrono::system_clock::now();
auto Millis = std::chrono::duration_cast<std::chrono::milliseconds>(
Time.time_since_epoch());
return Millis.count();
#endif
}
namespace {
/// Helper to log a single line that adds the timestamp, pid, and tid. The line
/// is buffered and written in a single call to write() so that if the
/// underlying OS syscall is handled atomically so is this log message.
class TextLogLine : public raw_svector_ostream {
public:
TextLogLine(raw_ostream &LogOS) : raw_svector_ostream(Buffer), LogOS(LogOS) {
startLogMsg(*this);
}
~TextLogLine() {
finishLogMsg(*this);
LogOS.write(Buffer.data(), Buffer.size());
}
static void startLogMsg(raw_ostream &OS) {
auto Millis = getTimestampMillis();
OS << format("%lld.%0.3lld", Millis / 1000, Millis % 1000);
OS << ' ' << sys::Process::getProcessId() << ' ' << get_threadid() << ": ";
}
static void finishLogMsg(raw_ostream &OS) { OS << '\n'; }
private:
raw_ostream &LogOS;
SmallString<128> Buffer;
};
} // anonymous namespace
static void formatTrieOffset(raw_ostream &OS, int64_t Off) {
if (Off < 0) {
OS << '-';
Off = -Off;
}
OS << format_hex(Off, 0);
}
void OnDiskCASLogger::logSubtrieHandleCmpXchg(void *Region, TrieOffset Trie,
size_t SlotI, TrieOffset Expected,
TrieOffset New,
TrieOffset Previous) {
TextLogLine Log(OS);
Log << "cmpxcgh subtrie region=" << Region << " offset=";
formatTrieOffset(Log, Trie);
Log << " slot=" << SlotI << " expected=";
formatTrieOffset(Log, Expected);
Log << " new=";
formatTrieOffset(Log, New);
Log << " prev=";
formatTrieOffset(Log, Previous);
}
void OnDiskCASLogger::logSubtrieHandleCreate(void *Region, TrieOffset Trie,
uint32_t StartBit,
uint32_t NumBits) {
TextLogLine Log(OS);
Log << "create subtrie region=" << Region << " offset=";
formatTrieOffset(Log, Trie);
Log << " start-bit=" << StartBit << " num-bits=" << NumBits;
}
void OnDiskCASLogger::logHashMappedTrieHandleCreateRecord(
void *Region, TrieOffset Off, ArrayRef<uint8_t> Hash) {
TextLogLine Log(OS);
Log << "create record region=" << Region << " offset=";
formatTrieOffset(Log, Off);
Log << " hash=" << format_bytes(Hash, std::nullopt, 32, 32);
}
void OnDiskCASLogger::logMappedFileRegionArenaResizeFile(StringRef Path,
size_t Before,
size_t After) {
TextLogLine Log(OS);
Log << "resize mapped file '" << Path << "' from=" << Before
<< " to=" << After;
}
void OnDiskCASLogger::logMappedFileRegionArenaCreate(StringRef Path, int FD,
void *Region,
size_t Capacity,
size_t Size) {
sys::fs::file_status Stat;
std::error_code EC = status(FD, Stat);
TextLogLine Log(OS);
Log << "mmap '" << Path << "' " << Region;
Log << " size=" << Size << " capacity=" << Capacity;
if (EC) {
Log << " failed status with error: " << EC.message();
return;
}
Log << " dev=" << format_hex(Stat.getUniqueID().getDevice(), 4);
Log << " inode=" << format_hex(Stat.getUniqueID().getFile(), 4);
}
void OnDiskCASLogger::logMappedFileRegionArenaOom(StringRef Path,
size_t Capacity, size_t Size,
size_t AllocSize) {
TextLogLine Log(OS);
Log << "oom '" << Path << "' old-size=" << Size << " capacity=" << Capacity
<< "alloc-size=" << AllocSize;
}
void OnDiskCASLogger::logMappedFileRegionArenaClose(StringRef Path) {
TextLogLine Log(OS);
Log << "close mmap '" << Path << "'";
}
void OnDiskCASLogger::logMappedFileRegionArenaAllocate(void *Region,
TrieOffset Off,
size_t Size) {
if (!LogAllocations)
return;
TextLogLine Log(OS);
Log << "alloc " << Region << " offset=";
formatTrieOffset(Log, Off);
Log << " size=" << Size;
}
void OnDiskCASLogger::logUnifiedOnDiskCacheCollectGarbage(StringRef Path) {
TextLogLine Log(OS);
Log << "collect garbage '" << Path << "'";
}
void OnDiskCASLogger::logUnifiedOnDiskCacheValidateIfNeeded(
StringRef Path, uint64_t BootTime, uint64_t ValidationTime, bool CheckHash,
bool AllowRecovery, bool Force, std::optional<StringRef> LLVMCas,
StringRef ValidationError, bool Skipped, bool Recovered) {
TextLogLine Log(OS);
Log << "validate-if-needed '" << Path << "'";
Log << " boot=" << BootTime << " last-valid=" << ValidationTime;
Log << " check-hash=" << CheckHash << " allow-recovery=" << AllowRecovery;
Log << " force=" << Force;
if (LLVMCas)
Log << " llvm-cas=" << *LLVMCas;
if (Skipped)
Log << " skipped";
if (Recovered)
Log << " recovered";
if (!ValidationError.empty())
Log << " data was invalid " << ValidationError;
}
void OnDiskCASLogger::logTempFileCreate(StringRef Name) {
TextLogLine Log(OS);
Log << "standalone file create '" << Name << "'";
}
void OnDiskCASLogger::logTempFileKeep(StringRef TmpName, StringRef Name,
std::error_code EC) {
TextLogLine Log(OS);
Log << "standalone file rename '" << TmpName << "' to '" << Name << "'";
if (EC)
Log << " error: " << EC.message();
}
void OnDiskCASLogger::logTempFileRemove(StringRef TmpName, std::error_code EC) {
TextLogLine Log(OS);
Log << "standalone file remove '" << TmpName << "'";
if (EC)
Log << " error: " << EC.message();
}
|