• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (C) 2019 The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //      http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef PREFETCHER_SESSION_MANAGER_H_
16 #define PREFETCHER_SESSION_MANAGER_H_
17 
18 #include <optional>
19 #include <ostream>
20 #include <memory>
21 
22 namespace iorap {
23 namespace prefetcher {
24 
25 class Session;
26 
27 enum class SessionKind : uint32_t {
28   kInProcessDirect,
29   kOutOfProcessIpc,
30   kOutOfProcessSocket,
31 };
32 
33 inline std::ostream& operator<<(std::ostream& os, SessionKind kind) {
34   if (kind == SessionKind::kInProcessDirect) {
35     os << "kInProcessDirect";
36   } else if (kind == SessionKind::kOutOfProcessIpc) {
37     os << "kOutOfProcessIpc";
38   } else if (kind == SessionKind::kOutOfProcessSocket) {
39     os << "kOutOfProcessSocket";
40   } else {
41     os << "(invalid)";
42   }
43   return os;
44 }
45 
46 class SessionManager {
47  public:
48   static std::unique_ptr<SessionManager> CreateManager(SessionKind kind);
49 
50   // Create a new session. The description is used by Dump.
51   // Manager maintains a strong ref to this session, so DestroySession must also
52   // be called prior to all refs dropping to 0.
53   virtual std::shared_ptr<Session> CreateSession(size_t session_id,
54                                                  std::string description) = 0;
55 
56   // Create a new session. The description is used by Dump.
57   // Manager maintains a strong ref to this session, so DestroySession must also
58   // be called prior to all refs dropping to 0.
CreateSession(size_t session_id,std::string description,std::optional<int> fd)59   virtual std::shared_ptr<Session> CreateSession(size_t session_id,
60                                                  std::string description,
61                                                  std::optional<int> fd) {
62     return CreateSession(session_id, description);
63   }
64 
65   // Look up an existing session that was already created.
66   // Returns null if there is no such session.
67   virtual std::shared_ptr<Session> FindSession(size_t session_id) const = 0;
68 
69   // Drop all manager references to an existing session.
70   // Returns false if the session does not exist already.
71   virtual bool DestroySession(size_t session_id) = 0;
72 
73   // Multi-line detailed dump, e.g. for dumpsys.
74   // Single-line summary dump, e.g. for logcat.
75   virtual void Dump(std::ostream& os, bool multiline) const = 0;
76 
77   // Note: session lifetime is tied to manager. The manager has strong pointers to sessions.
~SessionManager()78   virtual ~SessionManager() {}
79 
80  protected:
81   SessionManager();
82 };
83 
84 // Single-line summary dump of Session.
85 std::ostream& operator<<(std::ostream&os, const SessionManager& session);
86 
87 }  // namespace prefetcher
88 }  // namespace iorap
89 
90 #endif
91 
92