Skip to content

Commit

Permalink
Merge pull request #78 from SuperV1234/master
Browse files Browse the repository at this point in the history
Various minor improvements
  • Loading branch information
aras-p authored Nov 26, 2021
2 parents 5d40542 + 0a34415 commit 31077a0
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 31 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ _TraceOutput.json
_TraceOutput.bin

.DS_Store
.vscode/settings.json
45 changes: 23 additions & 22 deletions src/BuildEvents.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ static void DebugPrintEvents(const BuildEvents& events, const BuildNames& names)
for (size_t i = 0; i < events.size(); ++i)
{
const BuildEvent& event = events[EventIndex(int(i))];
printf("%4zi: t=%i t1=%7ld t2=%7ld par=%4i ch=%4zi det=%s\n", i, (int) event.type, event.ts, event.ts+event.dur, event.parent.idx, event.children.size(), std::string(names[event.detailIndex].substr(0,130)).c_str());
const std::string_view namesSubstr = names[event.detailIndex].substr(0, 130);
printf("%4zi: t=%i t1=%7ld t2=%7ld par=%4i ch=%4zi det=%.*s\n", i, (int) event.type, event.ts, event.ts+event.dur, event.parent.idx, event.children.size(), namesSubstr.size(), namesSubstr.data());
}
}

Expand Down Expand Up @@ -97,7 +98,7 @@ static void FindParentChildrenIndices(BuildEvents& events)
evRoot->children.push_back(EventIndex(i));
break;
}

root = evRoot->parent.idx;
if (root != -1)
evRoot = &events[sortedIndices[root]];
Expand All @@ -118,7 +119,7 @@ static void FindParentChildrenIndices(BuildEvents& events)
if (e.parent.idx != -1)
e.parent = sortedIndices[e.parent.idx];
}

#ifndef NDEBUG
for (int i = 0, n = (int)events.size(); i != n; ++i)
{
Expand All @@ -134,7 +135,7 @@ struct BuildEventsParser
// make sure zero index is empty
NameToIndex("", resultNameToIndex);
resultNames.push_back(std::string_view(resultNameToIndex.begin()->first.str, 0));

resultEvents.reserve(2048);
resultNames.reserve(2048);
}
Expand All @@ -144,19 +145,19 @@ struct BuildEventsParser
NameToIndexMap resultNameToIndex;
std::mutex resultMutex;
std::mutex arenaMutex;

void AddEvents(BuildEvents& add, const NameToIndexMap& nameToIndex)
{
// we got job-local build events and name-to-index mapping;
// add them to the global result with any necessary remapping.
// gotta take a mutex since we're modifying shared state here.
std::scoped_lock lock(resultMutex);

// move events to end of result events list
int offset = (int)resultEvents.size();
std::move(add.begin(), add.end(), std::back_inserter(resultEvents));
add.clear();

// create remapping from name indices, adding them to global remapping
// list if necessary.
ska::bytell_hash_map<DetailIndex, DetailIndex> detailRemap;
Expand All @@ -175,7 +176,7 @@ struct BuildEventsParser
detailRemap[kvp.second] = existing->second;
}
}

// adjust the added event indices
for (size_t i = offset, n = resultEvents.size(); i != n; ++i)
{
Expand All @@ -191,7 +192,7 @@ struct BuildEventsParser
assert(ev.detailIndex.idx >= 0 && ev.detailIndex.idx < resultNameToIndex.size());
}
}

assert(resultNameToIndex.size() == resultNames.size());
}

Expand Down Expand Up @@ -279,7 +280,7 @@ struct BuildEventsParser
resultEvents.clear();
return;
}

BuildEvent event;
bool valid = true;
std::string_view detailPtr;
Expand Down Expand Up @@ -340,7 +341,7 @@ struct BuildEventsParser
;
else
{
printf("%sWARN: unknown trace event '%s' in '%s', skipping.%s\n", col::kYellow, std::string(name).c_str(), curFileName.c_str(), col::kReset);
printf("%sWARN: unknown trace event '%.*s' in '%s', skipping.%s\n", col::kYellow, name.size(), name.data(), curFileName.c_str(), col::kReset);
}
}
else if (StrEqual(nodeKey, kTs))
Expand Down Expand Up @@ -368,7 +369,7 @@ struct BuildEventsParser
}
}
};

if (event.type== BuildEventType::kUnknown || !valid)
return;

Expand Down Expand Up @@ -428,16 +429,16 @@ bool ParseBuildEvents(BuildEventsParser* parser, const std::string& fileName)
printf("%sWARN: JSON parse error %s.%s\n", col::kYellow, error_message(error), col::kReset);
return false;
}

return parser->ParseRoot(doc, fileName);
//DebugPrintEvents(outEvents, outNames);
}

struct BufferedWriter
{
BufferedWriter(FILE* f)
: file(f)
, size(0)
: size(0)
, file(f)
{
hasher = XXH64_createState();
XXH64_reset(hasher, 0);
Expand All @@ -450,7 +451,7 @@ struct BufferedWriter
fclose(file);
XXH64_freeState(hasher);
}

template<typename T> void Write(const T& t)
{
Write(&t, sizeof(t));
Expand All @@ -475,14 +476,14 @@ struct BufferedWriter
size += sz;
}


void Flush()
{
fwrite(buffer, size, 1, file);
XXH64_update(hasher, buffer, size);
size = 0;
}

enum { kBufferSize = 65536 };
uint8_t buffer[kBufferSize];
size_t size;
Expand All @@ -507,7 +508,7 @@ struct BufferedReader
{
delete[] buffer;
}

template<typename T> void Read(T& t)
{
Read(&t, sizeof(t));
Expand All @@ -522,7 +523,7 @@ struct BufferedReader
memcpy(ptr, &buffer[pos], sz);
pos += sz;
}

uint8_t* buffer;
size_t pos;
size_t bufferSize;
Expand All @@ -538,7 +539,7 @@ bool SaveBuildEvents(BuildEventsParser* parser, const std::string& fileName)
printf("%sERROR: failed to save to file '%s'%s\n", col::kRed, fileName.c_str(), col::kReset);
return false;
}

BufferedWriter w(f);

w.Write(kFileMagic);
Expand Down Expand Up @@ -577,7 +578,7 @@ bool LoadBuildEvents(const std::string& fileName, BuildEvents& outEvents, BuildN
printf("%sERROR: failed to open file '%s'%s\n", col::kRed, fileName.c_str(), col::kReset);
return false;
}

BufferedReader r(f);
if (r.bufferSize < 12) // 4 bytes magic header, 8 bytes hash at end
{
Expand Down
13 changes: 11 additions & 2 deletions src/Utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,15 +100,24 @@ bool utils::IsHeader(const std::string_view& path)
return false;
}

static void makeSubstr(std::string& str, std::size_t pos = 0, std::size_t len = std::string::npos)
{
str.erase(0, pos);

if(str.size() - len <= str.size())
{
str.erase(len, str.size() - len);
}
}

std::string utils::GetNicePath(const std::string_view& path)
{
std::string res(path);
ForwardSlashify(res);
if (BeginsWith(res, s_CurrentDir))
res = res.substr(s_CurrentDir.size(), res.size() - s_CurrentDir.size());
makeSubstr(res, s_CurrentDir.size(), res.size() - s_CurrentDir.size());
if (BeginsWith(res, s_Root))
res = res.substr(s_Root.size(), res.size() - s_Root.size());
makeSubstr(res, s_Root.size(), res.size() - s_Root.size());
return res;
}

Expand Down
10 changes: 5 additions & 5 deletions src/Utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ namespace utils
{
void Initialize();

std::string GetNicePath(const std::string_view& path);
std::string_view GetFilename(const std::string_view& path);
[[nodiscard]] std::string GetNicePath(const std::string_view& path);
[[nodiscard]] std::string_view GetFilename(const std::string_view& path);

bool IsHeader(const std::string_view& path);
[[nodiscard]] bool IsHeader(const std::string_view& path);

void Lowercase(std::string& path);
void ForwardSlashify(std::string& path);

bool BeginsWith(const std::string& str, const std::string& prefix);
bool EndsWith(const std::string_view& str, const std::string& suffix);
[[nodiscard]] bool BeginsWith(const std::string& str, const std::string& prefix);
[[nodiscard]] bool EndsWith(const std::string_view& str, const std::string& suffix);
}
4 changes: 2 additions & 2 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ static int RunStart(int argc, const char* argv[])
return 0;
}

#ifdef _MSC_VER
#ifdef WIN32
static time_t FiletimeToTime(const FILETIME& ft)
{
ULARGE_INTEGER ull;
Expand Down Expand Up @@ -129,7 +129,7 @@ struct JsonFileFinder
if (!cf_get_file_time(f->path, &mtime))
return;
time_t fileModTime;
#ifdef _MSC_VER
#ifdef WIN32
fileModTime = FiletimeToTime(mtime.time);
#else
fileModTime = mtime.time;
Expand Down

0 comments on commit 31077a0

Please sign in to comment.