This commit is contained in:
Lukas Joswiak 2021-06-15 15:03:44 -07:00
parent 4eca095644
commit eb3fb811d4
8 changed files with 64 additions and 171 deletions

View File

@ -30,7 +30,7 @@
using namespace std::literals;
// TODO: For debugging, remove
LineageReference<ActorLineage>* curLineage() {
LineageReference* curLineage() {
return currentLineage;
}

View File

@ -35,7 +35,7 @@ void samplingProfilerUpdateFrequency(std::optional<std::any> freq);
void samplingProfilerUpdateWindow(std::optional<std::any> window);
// TODO: For debugging, remove
LineageReference<ActorLineage>* curLineage();
LineageReference* curLineage();
struct IALPCollectorBase {
virtual std::optional<std::any> collect(ActorLineage*) = 0;

View File

@ -25,5 +25,5 @@ std::vector<StringRef> getActorStackTrace() {
}
namespace {
// StackLineageCollector stackLineageCollector;
StackLineageCollector stackLineageCollector;
}

View File

@ -194,14 +194,4 @@ bool operator!=(const Reference<P>& lhs, const Reference<P>& rhs) {
return !(lhs == rhs);
}
template <class P>
class LineageReference : public Reference<P> {
// TODO: Make private
public:
LineageReference() : Reference<P>(nullptr), referencesSelf(false) {}
explicit LineageReference(P* ptr) : Reference<P>(ptr), referencesSelf(false) {}
LineageReference(const LineageReference& r) : Reference<P>(r), referencesSelf(false) {}
bool referencesSelf;
};
#endif

View File

@ -452,8 +452,6 @@ namespace actorcompiler
fullClassName,
string.Join(", ", actor.parameters.Select(p => p.name).ToArray()));
// if (actor.IsCancellable())
// writer.WriteLine("\trestore_lineage _;");
if (actor.returnType != null)
writer.WriteLine("\treturn Future<{1}>({0});", newActor, actor.returnType);
else
@ -1288,8 +1286,8 @@ namespace actorcompiler
constructor.WriteLine("{");
constructor.Indent(+1);
ProbeEnter(constructor, actor.name);
constructor.WriteLine("CurrentLineageReplace _(&this->lineage);");
// constructor.WriteLine("getCurrentLineage()->modify(&StackLineage::actorName) = LiteralStringRef(\"{0}\");", actor.name);
constructor.WriteLine("CurrentLineageScope _(&this->lineage);");
constructor.WriteLine("getCurrentLineage()->modify(&StackLineage::actorName) = LiteralStringRef(\"{0}\");", actor.name);
constructor.WriteLine("this->{0};", body.call());
ProbeExit(constructor, actor.name);
WriteFunction(writer, constructor, constructor.BodyText);

View File

@ -27,17 +27,13 @@
#include <cinttypes>
LineageReference<ActorLineage> rootLineage;
// LineageReference<ActorLineage> rootLineage(new ActorLineage());
std::atomic<bool> startSampling = false;
// TODO: Fix this (ideally get rid of allocation, otherwise memory leak?)
thread_local LineageReference<ActorLineage>* currentLineage = &rootLineage;//new LineageReference<ActorLineage>();
LineageReference rootLineage;
thread_local LineageReference* currentLineage = &rootLineage;
LineagePropertiesBase::~LineagePropertiesBase() {}
ActorLineage::ActorLineage() : properties(), parent(*currentLineage) {
// TraceEvent("LUKAS_ActorLineage").detail("CurrentLineagePtr", reinterpret_cast<uintptr_t>(currentLineage)).detail("CurrentLineageRefPtr", reinterpret_cast<uintptr_t>(currentLineage->getPtr()));
}
ActorLineage::ActorLineage() : properties(), parent(*currentLineage) {}
ActorLineage::~ActorLineage() {
for (auto property : properties) {
@ -46,55 +42,19 @@ ActorLineage::~ActorLineage() {
}
Reference<ActorLineage> getCurrentLineage() {
// if (!currentLineage.isValid()/* || currentLineage == currentLineage->getParent()*/) {
if (!currentLineage->isValid() || !currentLineage->referencesSelf) {
// replaceLineage(LineageReference<ActorLineage>{ new ActorLineage() });
// *currentLineage = LineageReference<ActorLineage>{ new ActorLineage() };
// ActorLineage* lineage = new ActorLineage();
// *currentLineage = LineageReference<ActorLineage>::addRef(lineage);
// *currentLineage = LineageReference<ActorLineage>(new ActorLineage());
// currentLineage->referencesSelf = true;
// TraceEvent("LUKAS_getCurrent").detail("Ptr", reinterpret_cast<uintptr_t>(currentLineage->getPtr()));
currentLineage->setPtrUnsafe(new ActorLineage());
currentLineage->referencesSelf = true;
// currentLineage = &rootLineage; // TODO: Undo
// currentLineage->referencesSelf = true;
if (!currentLineage->isValid() || !currentLineage->isAllocated()) {
currentLineage->allocate();
}
return *currentLineage;
}
// void sample(const Reference<ActorLineage>& lineage);
void sample(Reference<ActorLineage>* ptr);
void replaceLineage(LineageReference<ActorLineage>* lineage) {
// if (lineage.isValid()) {
// auto name = lineage->get(&StackLineage::actorName);
// if (name.has_value()) {
// TraceEvent("LUKAS_replaceLineage").detail("Name", name.value());
// }
// }
// if (currentLineage->isValid()) {
// auto name = (*currentLineage)->get(&StackLineage::actorName);
// if (name.has_value()) {
// TraceEvent("LUKAS_replaceLineageCurrentLineage").detail("Name", name.value());
// }
// }
// TraceEvent("LUKAS_replaceLineage").detail("Ptr", reinterpret_cast<uintptr_t>(lineage));
// TraceEvent("LUKAS_replaceLineage2").detail("IsValid", lineage->isValid()).detail("PtrPtr", reinterpret_cast<uintptr_t>(lineage->getPtr()));
void replaceLineage(LineageReference* lineage) {
if (!startSampling) {
currentLineage = lineage;
} else {
startSampling = false;
// if (currentLineage->isValid()) {
// std::string stack = "";
// auto vec = (*currentLineage)->stack(&StackLineage::actorName);
// for (const auto& str : vec) {
// stack += std::string(reinterpret_cast<const char*>(str.begin()), str.size()) + " ";
// }
// TraceEvent("LUKAS_replaceLineage").detail("Stack", stack);
// }
sample(currentLineage);
currentLineage = lineage;
}

View File

@ -449,7 +449,7 @@ struct LineageProperties : LineagePropertiesBase {
};
struct ActorLineage : ThreadSafeReferenceCounted<ActorLineage> {
friend class LocalLineage;
friend class LineageReference;
struct Property {
std::string_view name;
@ -463,6 +463,7 @@ private:
using Lock = std::unique_lock<std::mutex>;
using Iterator = std::vector<Property>::const_iterator;
ActorLineage();
Iterator find(const std::string_view& name) const {
for (auto it = properties.cbegin(); it != properties.cend(); ++it) {
if (it->name == name) {
@ -482,7 +483,6 @@ private:
}
public:
ActorLineage();
~ActorLineage();
bool isRoot() const {
Lock _{ mutex };
@ -540,24 +540,55 @@ public:
}
};
// A Reference subclass with knowledge on the true owner of the contained
// ActorLineage object. This class enables lazy allocation of ActorLineages.
// LineageReference copies are generally made by child actors, which should
// create their own ActorLineage when attempting to add lineage properties (see
// getCurrentLineage()).
class LineageReference : public Reference<ActorLineage> {
public:
LineageReference() : Reference<ActorLineage>(nullptr), allocated(false) {}
explicit LineageReference(ActorLineage* ptr) : Reference<ActorLineage>(ptr), allocated(false) {}
LineageReference(const LineageReference& r) : Reference<ActorLineage>(r), allocated(false) {}
void allocate() {
Reference<ActorLineage>::setPtrUnsafe(new ActorLineage());
allocated = true;
}
bool isAllocated() { return allocated; }
private:
bool allocated;
};
extern std::atomic<bool> startSampling;
// TODO: ThreadUnsafe?
extern thread_local LineageReference<ActorLineage>* currentLineage;
extern thread_local LineageReference* currentLineage;
Reference<ActorLineage> getCurrentLineage();
void replaceLineage(LineageReference* lineage);
struct StackLineage : LineageProperties<StackLineage> {
static const std::string_view name;
StringRef actorName;
};
Reference<ActorLineage> getCurrentLineage();
void replaceLineage(LineageReference<ActorLineage>* lineage);
struct CurrentLineageScope {
LineageReference* oldLineage;
CurrentLineageScope(LineageReference* with) : oldLineage(currentLineage) {
replaceLineage(with);
}
~CurrentLineageScope() {
replaceLineage(oldLineage);
}
};
// This class can be used in order to modify all lineage properties
// of actors created within a (non-actor) scope
struct LocalLineage {
LineageReference<ActorLineage> lineage = LineageReference<ActorLineage>{ new ActorLineage() };
LineageReference<ActorLineage>* oldLineage;
LineageReference lineage;
LineageReference* oldLineage;
LocalLineage() {
lineage.allocate();
oldLineage = currentLineage;
replaceLineage(&lineage);
}
@ -566,24 +597,6 @@ struct LocalLineage {
}
};
// TODO: No longer want this because we are now setting a global instead of just the field in a class
struct restore_lineage {
// Reference<ActorLineage> lineage;
LineageReference<ActorLineage>* prev;
// LineageReference<ActorLineage> prev;
// restore_lineage() : prev(*currentLineage) {
restore_lineage() : prev(currentLineage) {
// if (currentLineage != nullptr && currentLineage->isValid()) {
// prev = *currentLineage;
// }
// prev = currentLineage;
// replaceLineage(lineage);
}
~restore_lineage() {
replaceLineage(prev);
}
};
// SAV is short for Single Assignment Variable: It can be assigned for only once!
template <class T>
struct SAV : private Callback<T>, FastAllocated<SAV<T>> {
@ -1159,53 +1172,16 @@ static inline void destruct(T& t) {
t.~T();
}
// TODO: Rename, move to better spot (above)
struct CurrentLineageReplace {
LineageReference<ActorLineage>* oldLineage;
CurrentLineageReplace(LineageReference<ActorLineage>* with) : oldLineage(currentLineage) {
// currentLineage = with;
replaceLineage(with);
}
~CurrentLineageReplace() {
// currentLineage = oldLineage;
replaceLineage(oldLineage);
// replaceLineage(new LineageReference<ActorLineage>());
}
};
template <class ReturnValue>
struct Actor : SAV<ReturnValue> {
// LineageReference<ActorLineage>* prev = currentLineage;
LineageReference<ActorLineage> lineage = *currentLineage;
// Reference<ActorLineage> lineage;
LineageReference lineage = *currentLineage;
int8_t actor_wait_state; // -1 means actor is cancelled; 0 means actor is not waiting; 1-N mean waiting in callback
// group #
Actor() : SAV<ReturnValue>(1, 1), actor_wait_state(0) {
/*++actorCount;*/
// if (currentLineage != nullptr && currentLineage->isValid()) {
// lineage = *currentLineage;
// }
// replaceLineage(&lineage);
// lineage.referencesSelf = false;
// currentLineage = &lineage;
}
~Actor() {
//--actorCount;
// replaceLineage(new LineageReference<ActorLineage>());
// replaceLineage(prev);
}
Actor() : SAV<ReturnValue>(1, 1), actor_wait_state(0) { /*++actorCount;*/ }
// ~Actor() { --actorCount; }
Reference<ActorLineage> setLineage() {
Reference<ActorLineage> res = *currentLineage;
// if (currentLineage != nullptr && currentLineage->isValid()) {
// res = *currentLineage;
// }
replaceLineage(&lineage);
return res;
}
LineageReference<ActorLineage>* lineageAddr() {
LineageReference* lineageAddr() {
return std::addressof(lineage);
}
};
@ -1214,36 +1190,14 @@ template <>
struct Actor<void> {
// This specialization is for a void actor (one not returning a future, hence also uncancellable)
// LineageReference<ActorLineage>* prev = currentLineage;
LineageReference<ActorLineage> lineage = *currentLineage;
LineageReference lineage = *currentLineage;
// Reference<ActorLineage> lineage;
int8_t actor_wait_state; // 0 means actor is not waiting; 1-N mean waiting in callback group #
Actor() : actor_wait_state(0) {
/*++actorCount;*/
// if (currentLineage != nullptr && currentLineage->isValid()) {
// lineage = *currentLineage;
// }
// replaceLineage(&lineage);
// lineage.referencesSelf = false;
// currentLineage = &lineage;
}
~Actor() {
//--actorCount;
// replaceLineage(new LineageReference<ActorLineage>());
// replaceLineage(prev);
}
Actor() : actor_wait_state(0) { /*++actorCount;*/ }
// ~Actor() { --actorCount; }
Reference<ActorLineage> setLineage() {
Reference<ActorLineage> res = *currentLineage;
// if (currentLineage != nullptr && currentLineage->isValid()) {
// res = *currentLineage;
// }
replaceLineage(&lineage);
return res;
}
LineageReference<ActorLineage>* lineageAddr() {
LineageReference* lineageAddr() {
return std::addressof(lineage);
}
};
@ -1251,13 +1205,11 @@ struct Actor<void> {
template <class ActorType, int CallbackNumber, class ValueType>
struct ActorCallback : Callback<ValueType> {
virtual void fire(ValueType const& value) override {
// auto _ = static_cast<ActorType*>(this)->setLineage();
CurrentLineageReplace _(static_cast<ActorType*>(this)->lineageAddr());
CurrentLineageScope _(static_cast<ActorType*>(this)->lineageAddr());
static_cast<ActorType*>(this)->a_callback_fire(this, value);
}
virtual void error(Error e) override {
// auto _ = static_cast<ActorType*>(this)->setLineage();
CurrentLineageReplace _(static_cast<ActorType*>(this)->lineageAddr());
CurrentLineageScope _(static_cast<ActorType*>(this)->lineageAddr());
static_cast<ActorType*>(this)->a_callback_error(this, e);
}
};
@ -1265,18 +1217,15 @@ struct ActorCallback : Callback<ValueType> {
template <class ActorType, int CallbackNumber, class ValueType>
struct ActorSingleCallback : SingleCallback<ValueType> {
void fire(ValueType const& value) override {
// auto _ = static_cast<ActorType*>(this)->setLineage();
CurrentLineageReplace _(static_cast<ActorType*>(this)->lineageAddr());
CurrentLineageScope _(static_cast<ActorType*>(this)->lineageAddr());
static_cast<ActorType*>(this)->a_callback_fire(this, value);
}
void fire(ValueType&& value) override {
// auto _ = static_cast<ActorType*>(this)->setLineage();
CurrentLineageReplace _(static_cast<ActorType*>(this)->lineageAddr());
CurrentLineageScope _(static_cast<ActorType*>(this)->lineageAddr());
static_cast<ActorType*>(this)->a_callback_fire(this, std::move(value));
}
void error(Error e) override {
// auto _ = static_cast<ActorType*>(this)->setLineage();
CurrentLineageReplace _(static_cast<ActorType*>(this)->lineageAddr());
CurrentLineageScope _(static_cast<ActorType*>(this)->lineageAddr());
static_cast<ActorType*>(this)->a_callback_error(this, e);
}
};

View File

@ -1565,11 +1565,7 @@ struct YieldedFutureActor : SAV<Void>, ActorCallback<YieldedFutureActor, 1, Void
void destroy() override { delete this; }
Reference<ActorLineage> setLineage() {
return *currentLineage;
}
LineageReference<ActorLineage>* lineageAddr() {
LineageReference* lineageAddr() {
return currentLineage;
}