Commit 6645b95d authored by Melanie Subbiah's avatar Melanie Subbiah Committed by Facebook Github Bot 0

RequestContext changes to facilitate thread tracing

Summary: In order to trigger trace points when a new thread begins/finishes executing, I added functionality to call onSet and onUnset methods on all RequestData objects whenever setContext is called. The main question: will this approach cost us too much overhead?

Reviewed By: djwatson

Differential Revision: D3604948

fbshipit-source-id: 3b704ca0f2b713458427aa49be12f776939057f8
parent 066a868c
...@@ -34,6 +34,8 @@ namespace folly { ...@@ -34,6 +34,8 @@ namespace folly {
class RequestData { class RequestData {
public: public:
virtual ~RequestData() = default; virtual ~RequestData() = default;
virtual void onSet(){};
virtual void onUnset(){};
}; };
class RequestContext; class RequestContext;
...@@ -43,7 +45,7 @@ class RequestContext; ...@@ -43,7 +45,7 @@ class RequestContext;
// copied between threads. // copied between threads.
class RequestContext { class RequestContext {
public: public:
// Create a unique requext context for this request. // Create a unique request context for this request.
// It will be passed between queues / threads (where implemented), // It will be passed between queues / threads (where implemented),
// so it should be valid for the lifetime of the request. // so it should be valid for the lifetime of the request.
static void create() { static void create() {
...@@ -105,6 +107,24 @@ class RequestContext { ...@@ -105,6 +107,24 @@ class RequestContext {
} }
} }
void onSet() {
folly::RWSpinLock::ReadHolder guard(lock);
for (auto const& ent : data_) {
if (RequestData* data = ent.second.get()) {
data->onSet();
}
}
}
void onUnset() {
folly::RWSpinLock::ReadHolder guard(lock);
for (auto const& ent : data_) {
if (RequestData* data = ent.second.get()) {
data->onUnset();
}
}
}
void clearContextData(const std::string& val) { void clearContextData(const std::string& val) {
folly::RWSpinLock::WriteHolder guard(lock); folly::RWSpinLock::WriteHolder guard(lock);
data_.erase(val); data_.erase(val);
...@@ -122,8 +142,17 @@ class RequestContext { ...@@ -122,8 +142,17 @@ class RequestContext {
// multiple threads, or do post-send processing, etc. // multiple threads, or do post-send processing, etc.
static std::shared_ptr<RequestContext> static std::shared_ptr<RequestContext>
setContext(std::shared_ptr<RequestContext> ctx) { setContext(std::shared_ptr<RequestContext> ctx) {
using std::swap; auto& prev = getStaticContext();
swap(ctx, getStaticContext()); if (ctx != prev) {
using std::swap;
if (ctx) {
ctx->onSet();
}
if (prev) {
prev->onUnset();
}
swap(ctx, prev);
}
return ctx; return ctx;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment