sapling/eden/fuse/RequestData.h
Wez Furlong 66ec00f294 connect the fuse stats to fb303
Summary:
Tweaks the stats stuff so that we can name the histograms and export them with p50, p90, p99 percentiles.

This is made a bit ugly because the fb303 stats code isn't yet open source, so
there's a moderately ugly preprocessor directive that we assume to be true for
our internal build.   We'll need to force that to be 0 for the open source build
until we open the stats code and make it available.

Reviewed By: bolinfest

Differential Revision: D4801868

fbshipit-source-id: 643909e63bd4a74b2cfa580be131f65c5673bc94
2017-03-31 11:39:48 -07:00

152 lines
4.9 KiB
C++

/*
* Copyright (c) 2016-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
*/
#pragma once
#include <folly/ThreadLocal.h>
#include <folly/futures/Future.h>
#include <folly/io/async/Request.h>
#include "eden/fuse/EdenStats.h"
#include "eden/fuse/fuse_headers.h"
namespace facebook {
namespace eden {
namespace fusell {
class Channel;
class Dispatcher;
class RequestData : public folly::RequestData {
std::atomic<fuse_req_t> req_;
// We're managed by this context, so we only keep a weak ref
std::weak_ptr<folly::RequestContext> requestContext_;
// Needed to track stats
std::chrono::time_point<std::chrono::steady_clock> startTime_;
EdenStats::HistogramPtr latencyHistogram_{nullptr};
folly::ThreadLocal<EdenStats>* stats_{nullptr};
static void interrupter(fuse_req_t req, void* data);
fuse_req_t stealReq();
struct Cancel {
folly::Future<folly::Unit> fut_;
explicit Cancel(folly::Future<folly::Unit>&& fut) : fut_(std::move(fut)) {}
};
public:
static const std::string kKey;
RequestData(const RequestData&) = delete;
RequestData& operator=(const RequestData&) = delete;
RequestData(RequestData&&) = default;
RequestData& operator=(RequestData&&) = default;
explicit RequestData(fuse_req_t req);
static RequestData& get();
static RequestData& create(fuse_req_t req);
// Returns true if the current context is being called from inside
// a FUSE request, false otherwise.
static bool isFuseRequest();
folly::Future<folly::Unit> startRequest(
folly::ThreadLocal<EdenStats>* stats,
EdenStats::HistogramPtr histogram);
void finishRequest();
// Returns the request context, which holds uid, gid, pid and umask info
const fuse_ctx& getContext() const;
// Returns the dispatcher embedded in the request
Dispatcher* getDispatcher() const;
// Returns the channel associated with the request
Channel& getChannel() const;
// Returns the underlying fuse request, throwing an error if it has
// already been released
fuse_req_t getReq() const;
// Check whether the request has already been interrupted
bool wasInterrupted() const;
/** Register the future chain associated with this request so that
* we can cancel it when we receive an interrupt.
* This function will append error handling to the future chain by
* passing it to catchErrors() prior to registering the cancellation
* handler.
*/
template <typename FUTURE>
void setRequestFuture(FUTURE&& fut) {
this->interrupter_ =
std::make_unique<Cancel>(this->catchErrors(std::move(fut)));
}
/** Append error handling clauses to a future chain
* These clauses result in reporting a fuse request error back to the
* kernel. */
template <typename FUTURE>
folly::Future<folly::Unit> catchErrors(FUTURE&& fut) {
return fut
.onError([](const std::system_error& err) {
int errnum = EIO;
if (err.code().category() == std::system_category()) {
errnum = err.code().value();
}
VLOG(5) << folly::exceptionStr(err);
RequestData::get().replyError(errnum);
})
.onError([](const std::exception& err) {
VLOG(5) << folly::exceptionStr(err);
RequestData::get().replyError(EIO);
})
.ensure([] { RequestData::get().finishRequest(); });
}
// Returns the supplementary group IDs for the process making the
// current request
std::vector<gid_t> getGroups() const;
// The various fuse_reply_XXX functions implicity free the request
// pointer. We prefer to avoid keeping a stale pointer, hence these
// methods to maintain consistency.
// If the replyXXX function returns false, it means that the request
// was interrupted and that the dispatcher may need to clean up some
// of its state.
// Reply with a negative errno value or 0 for success
void replyError(int err);
// Don't send a reply, just release req_
void replyNone();
// Reply with a directory entry
void replyEntry(const struct fuse_entry_param& e);
// Reply with a directory entry and open params
bool replyCreate(const struct fuse_entry_param& e,
const struct fuse_file_info& fi);
void replyAttr(const struct stat& attr, double attr_timeout);
void replyReadLink(const std::string& link);
bool replyOpen(const struct fuse_file_info& fi);
void replyWrite(size_t count);
void replyBuf(const char* buf, size_t size);
void replyIov(const struct iovec* iov, int count);
void replyStatfs(const struct statvfs& st);
void replyXattr(size_t count);
void replyLock(struct flock& lock);
void replyBmap(uint64_t idx);
void replyIoctl(int result, const struct iovec* iov, int count);
void replyPoll(unsigned revents);
private:
std::unique_ptr<Cancel> interrupter_;
};
}
}
}