blob: 45120d58ff31d14fd02934c8cd6158454a6c94d4 [file] [log] [blame]
Igor Murashkin6c3686a2019-09-12 15:37:02 -07001// 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
Igor Murashkinefa52152019-09-17 11:23:32 -070018#include <optional>
Igor Murashkin6c3686a2019-09-12 15:37:02 -070019#include <ostream>
20#include <memory>
21
22namespace iorap {
23namespace prefetcher {
24
25class Session;
26
27enum class SessionKind : uint32_t {
28 kInProcessDirect,
29 kOutOfProcessIpc,
Igor Murashkinefa52152019-09-17 11:23:32 -070030 kOutOfProcessSocket,
Igor Murashkin6c3686a2019-09-12 15:37:02 -070031};
32
33inline 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";
Igor Murashkinefa52152019-09-17 11:23:32 -070038 } else if (kind == SessionKind::kOutOfProcessSocket) {
39 os << "kOutOfProcessSocket";
Igor Murashkin6c3686a2019-09-12 15:37:02 -070040 } else {
41 os << "(invalid)";
42 }
43 return os;
44}
45
46class 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
Igor Murashkinefa52152019-09-17 11:23:32 -070056 // 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.
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
Igor Murashkin6c3686a2019-09-12 15:37:02 -070065 // 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.
78 virtual ~SessionManager() {}
79
80 protected:
81 SessionManager();
82};
83
84// Single-line summary dump of Session.
85std::ostream& operator<<(std::ostream&os, const SessionManager& session);
86
87} // namespace prefetcher
88} // namespace iorap
89
90#endif
91