1/**
2 * Copyright (c) Glow Contributors. See CONTRIBUTORS file.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16#ifndef GLOW_RUNTIME_REQUESTDATA_H
17#define GLOW_RUNTIME_REQUESTDATA_H
18
19#include "folly/io/async/Request.h"
20#include <atomic>
21#include <chrono>
22
23namespace glow {
24namespace runtime {
25
26class RequestData : public folly::RequestData {
27public:
28 int64_t appLevelRequestId{0};
29 int64_t accessId;
30 /// Start and stop times for the glow request, relative to request start time.
31 uint64_t startTime{0};
32 uint64_t stopTime{0};
33
34 /// Total size of inputs and outputs transferred to device.
35 std::atomic<uint64_t> inputSize{0};
36 std::atomic<uint64_t> outputSize{0};
37
38 /// Request start time.
39 uint64_t requestStartTime{0};
40
41 /// Time spent waiting on the device stack sum of time of all partitions.
42 std::atomic<uint64_t> deviceRuntime{0};
43
44 int64_t currentBatchSize{0};
45 RequestData() {
46 accessId = std::chrono::duration_cast<std::chrono::microseconds>(
47 std::chrono::system_clock::now().time_since_epoch())
48 .count();
49 }
50
51 RequestData(int64_t id) : accessId(id) {}
52
53 static RequestData *get() {
54 auto data = dynamic_cast<RequestData *>(
55 folly::RequestContext::get()->getContextData(kContextDataName));
56 if (!data) {
57 return nullptr;
58 }
59 return data;
60 }
61
62 static void set(std::unique_ptr<RequestData> data) {
63 folly::RequestContext::get()->setContextData(kContextDataName,
64 std::move(data));
65 }
66
67 bool hasCallback() override { return false; }
68
69private:
70 static constexpr const char *kContextDataName{"Glow request data"};
71};
72
73} // namespace runtime
74} // namespace glow
75#endif // GLOW_RUNTIME_REQUESTDATA_H
76