summaryrefslogtreecommitdiff
path: root/src/prefetcher/session.h
blob: a4a9e6bc17765af7f08b2dab69915a48d27995d5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
// Copyright (C) 2019 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef PREFETCHER_SESSION_H_
#define PREFETCHER_SESSION_H_

#include <android-base/chrono_utils.h>
#include <android-base/unique_fd.h>

#include <optional>
#include <memory>
#include <string>
#include <string_view>
#include <unordered_map>
#include <vector>

namespace iorap {
namespace prefetcher {

#ifndef READ_AHEAD_KIND
#define READ_AHEAD_KIND 1
enum class ReadAheadKind : uint32_t {
  kFadvise = 0,
  kMmapLocked = 1,
  kMlock = 2,
};
#endif

class Session {
 public:
  virtual bool RegisterFilePath(size_t path_id, std::string_view file_path) = 0;
  virtual bool UnregisterFilePath(size_t path_id) = 0;

  // Immediately perform a readahead now.
  // Fadvise: the readahead will have been queued by the kernel.
  // MmapLocked/Mlock: the memory is pinned by the requested process.
  virtual bool ReadAhead(size_t path_id, ReadAheadKind kind, size_t length, size_t offset) = 0;

  // Cancels a readahead previously done.
  // The length/offset should match the call of ReadAhead.
  virtual bool UnreadAhead(size_t path_id, ReadAheadKind kind, size_t length, size_t offset) = 0;

  // Multi-line detailed dump, e.g. for dumpsys.
  // Single-line summary dump, e.g. for logcat.
  virtual void Dump(std::ostream& os, bool multiline) const = 0;

  // Process the FD for kCreateFdSession.
  // Assumes there's a compiled_trace.pb at the fd, calling this function
  // will immediately process it and execute any read-aheads.
  //
  // FD is borrowed only for the duration of the function call.
  virtual bool ProcessFd(int fd) = 0;

  // Get the session ID associated with this session.
  // Session IDs are distinct, they are not used for new sessions.
  virtual size_t SessionId() const = 0;

  // Get this session's description.
  // Only useful for logging/dumping.
  virtual const std::string& SessionDescription() const = 0;

  // Implicitly unregister any remaining file paths.
  // All read-aheads are also cancelled.
  virtual ~Session() {}

 protected:
  Session();
};

// Single-line summary dump of Session.
std::ostream& operator<<(std::ostream&os, const Session& session);

class SessionBase : public Session {
 public:
  virtual void Dump(std::ostream& os, bool multiline) const override;
  virtual ~SessionBase() {}

  virtual size_t SessionId() const override {
    return session_id_;
  }

  virtual const std::string& SessionDescription() const override {
    return description_;
  }

  virtual bool ProcessFd(int fd) override;

 protected:
  SessionBase(size_t session_id, std::string description);
  std::optional<std::string_view> GetFilePath(size_t path_id) const;
  bool RemoveFilePath(size_t path_id);
  bool InsertFilePath(size_t path_id, std::string file_path);

  android::base::Timer timer_{};
 private:
  // Note: Store filename for easier debugging and for dumping.
  std::unordered_map</*path_id*/size_t, std::string> path_map_;
  size_t session_id_;
  std::string description_;
};

// In-process session.
class SessionDirect : public SessionBase {
 public:
  virtual bool RegisterFilePath(size_t path_id, std::string_view file_path) override;

  virtual bool UnregisterFilePath(size_t path_id) override;
  virtual bool ReadAhead(size_t path_id,
                         ReadAheadKind kind,
                         size_t length,
                         size_t offset);

  virtual bool UnreadAhead(size_t path_id,
                           ReadAheadKind kind,
                           size_t length,
                           size_t offset) override;

  virtual bool ProcessFd(int fd) override;

  virtual void Dump(std::ostream& os, bool multiline) const override;

  virtual ~SessionDirect();

  SessionDirect(size_t session_id, std::string description)
    : SessionBase{session_id, std::move(description)} {
  }
 protected:
  struct Entry {
    size_t path_id;
    ReadAheadKind kind;
    size_t length;
    size_t offset;

    constexpr bool operator==(const Entry& other) const {
      return path_id == other.path_id &&
        kind == other.kind &&
        length == other.length &&
        offset == other.offset;
    }
    constexpr bool operator!=(const Entry& other) const {
      return !(*this == other);
    }

    friend std::ostream& operator<<(std::ostream& os, const Entry& entry);
  };

  struct EntryMapping {
    Entry entry;
    void* address;
    bool success;
  };

  // bool EntryReadAhead(const Entry& entry) const;
  // bool EntryUnReadAhead(const Entry& entry) const;

  void UnmapWithoutErase(const EntryMapping& entry_mapping);
  std::optional<android::base::unique_fd*> GetFdForPath(size_t path_id);

 private:
  std::unordered_map</*path_id*/size_t, std::vector<EntryMapping>> entry_list_map_;
  std::unordered_map</*path_id*/size_t, android::base::unique_fd> path_fd_map_;

 public:
  friend std::ostream& operator<<(std::ostream& os, const SessionDirect::Entry& entry);
};

std::ostream& operator<<(std::ostream& os, const SessionDirect::Entry& entry);

class PrefetcherDaemon;

// Out-of-process session. Requires prefetcher daemon.
class SessionIndirect : public SessionBase {
 public:
  virtual bool RegisterFilePath(size_t path_id, std::string_view file_path) override;

  virtual bool UnregisterFilePath(size_t path_id) override;
  virtual bool ReadAhead(size_t path_id,
                         ReadAheadKind kind,
                         size_t length,
                         size_t offset) override;

  virtual bool UnreadAhead(size_t path_id,
                           ReadAheadKind kind,
                           size_t length,
                           size_t offset) override;

  virtual void Dump(std::ostream& os, bool multiline) const override;

  // Creates a new session indirectly.
  // Writes to daemon the new session command.
  SessionIndirect(size_t session_id,
                  std::string description,
                  std::shared_ptr<PrefetcherDaemon> daemon,
                  bool send_command = true);

  // Destroys the current session.
  // Writes to daemon that the session is to be destroyed.
  virtual ~SessionIndirect();

 protected:
  std::shared_ptr<PrefetcherDaemon> daemon_;
};

// Out-of-process session. Requires prefetcher daemon.
class SessionIndirectSocket : public SessionIndirect {
 public:
  // Creates a new session indirectly.
  // Writes to daemon the new session command.
  SessionIndirectSocket(size_t session_id,
                        int fd,
                        std::string description,
                        std::shared_ptr<PrefetcherDaemon> daemon);
  // Destroys the current session.
  // Writes to daemon that the session is to be destroyed.
  virtual ~SessionIndirectSocket();

 private:
};


}  // namespace prefetcher
}  // namespace iorap

#endif