blob: acf2d313df47688c4b2544c05770a2ad0aadbf72 [file] [log] [blame]
Glenn Kasten11d8dfc2013-01-14 14:53:13 -08001/*
2 * Copyright (C) 2013 The Android Open Source Project
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
17// Non-blocking event logger intended for safe communication between processes via shared memory
18
19#ifndef ANDROID_MEDIA_NBLOG_H
20#define ANDROID_MEDIA_NBLOG_H
21
22#include <binder/IMemory.h>
23#include <utils/Mutex.h>
Glenn Kasten535e1612016-12-05 12:19:36 -080024#include <audio_utils/fifo.h>
Glenn Kasten11d8dfc2013-01-14 14:53:13 -080025
26namespace android {
27
Glenn Kasten4e01ef62013-07-11 14:29:59 -070028class String8;
29
Glenn Kasten11d8dfc2013-01-14 14:53:13 -080030class NBLog {
31
32public:
33
34class Writer;
35class Reader;
36
37private:
38
39enum Event {
40 EVENT_RESERVED,
41 EVENT_STRING, // ASCII string, not NUL-terminated
42 EVENT_TIMESTAMP, // clock_gettime(CLOCK_MONOTONIC)
43};
44
45// ---------------------------------------------------------------------------
46
47// representation of a single log entry in private memory
48struct Entry {
49 Entry(Event event, const void *data, size_t length)
50 : mEvent(event), mLength(length), mData(data) { }
51 /*virtual*/ ~Entry() { }
52
53 int readAt(size_t offset) const;
54
55private:
56 friend class Writer;
57 Event mEvent; // event type
Glenn Kasten535e1612016-12-05 12:19:36 -080058 uint8_t mLength; // length of additional data, 0 <= mLength <= kMaxLength
Glenn Kasten11d8dfc2013-01-14 14:53:13 -080059 const void *mData; // event type-specific data
Glenn Kasten535e1612016-12-05 12:19:36 -080060 static const size_t kMaxLength = 255;
61public:
62 static const size_t kOverhead = 3; // mEvent, mLength, mData[...], duplicate mLength
Glenn Kasten11d8dfc2013-01-14 14:53:13 -080063};
64
65// representation of a single log entry in shared memory
66// byte[0] mEvent
67// byte[1] mLength
68// byte[2] mData[0]
69// ...
70// byte[2+i] mData[i]
71// ...
72// byte[2+mLength-1] mData[mLength-1]
73// byte[2+mLength] duplicate copy of mLength to permit reverse scan
74// byte[3+mLength] start of next log entry
75
Glenn Kasten535e1612016-12-05 12:19:36 -080076public:
77
78// Located in shared memory, must be POD.
79// Exactly one process must explicitly call the constructor or use placement new.
80// Since this is a POD, the destructor is empty and unnecessary to call it explicitly.
Glenn Kasten11d8dfc2013-01-14 14:53:13 -080081struct Shared {
Glenn Kasten535e1612016-12-05 12:19:36 -080082 Shared() /* mRear initialized via default constructor */ { }
Glenn Kasten11d8dfc2013-01-14 14:53:13 -080083 /*virtual*/ ~Shared() { }
84
Glenn Kasten535e1612016-12-05 12:19:36 -080085 audio_utils_fifo_index mRear; // index one byte past the end of most recent Entry
86 char mBuffer[0]; // circular buffer for entries
Glenn Kasten11d8dfc2013-01-14 14:53:13 -080087};
88
89public:
90
91// ---------------------------------------------------------------------------
92
93// FIXME Timeline was intended to wrap Writer and Reader, but isn't actually used yet.
94// For now it is just a namespace for sharedSize().
95class Timeline : public RefBase {
96public:
97#if 0
98 Timeline(size_t size, void *shared = NULL);
99 virtual ~Timeline();
100#endif
101
Glenn Kastenfb1fdc92013-07-10 17:03:19 -0700102 // Input parameter 'size' is the desired size of the timeline in byte units.
103 // Returns the size rounded up to a power-of-2, plus the constant size overhead for indices.
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800104 static size_t sharedSize(size_t size);
105
106#if 0
107private:
108 friend class Writer;
109 friend class Reader;
110
111 const size_t mSize; // circular buffer size in bytes, must be a power of 2
112 bool mOwn; // whether I own the memory at mShared
113 Shared* const mShared; // pointer to shared memory
114#endif
115};
116
117// ---------------------------------------------------------------------------
118
119// Writer is thread-safe with respect to Reader, but not with respect to multiple threads
120// calling Writer methods. If you need multi-thread safety for writing, use LockedWriter.
121class Writer : public RefBase {
122public:
123 Writer(); // dummy nop implementation without shared memory
Glenn Kastenfb1fdc92013-07-10 17:03:19 -0700124
125 // Input parameter 'size' is the desired size of the timeline in byte units.
126 // The size of the shared memory must be at least Timeline::sharedSize(size).
Glenn Kasten535e1612016-12-05 12:19:36 -0800127 Writer(void *shared, size_t size);
128 Writer(const sp<IMemory>& iMemory, size_t size);
Glenn Kastenfb1fdc92013-07-10 17:03:19 -0700129
Glenn Kasten535e1612016-12-05 12:19:36 -0800130 virtual ~Writer();
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800131
132 virtual void log(const char *string);
Glenn Kastenab7d72f2013-02-27 09:05:28 -0800133 virtual void logf(const char *fmt, ...) __attribute__ ((format (printf, 2, 3)));
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800134 virtual void logvf(const char *fmt, va_list ap);
135 virtual void logTimestamp();
136 virtual void logTimestamp(const struct timespec& ts);
137
138 virtual bool isEnabled() const;
139
140 // return value for all of these is the previous isEnabled()
141 virtual bool setEnabled(bool enabled); // but won't enable if no shared memory
142 bool enable() { return setEnabled(true); }
143 bool disable() { return setEnabled(false); }
144
145 sp<IMemory> getIMemory() const { return mIMemory; }
146
147private:
Glenn Kasten535e1612016-12-05 12:19:36 -0800148 // 0 <= length <= kMaxLength
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800149 void log(Event event, const void *data, size_t length);
150 void log(const Entry *entry, bool trusted = false);
151
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800152 Shared* const mShared; // raw pointer to shared memory
Glenn Kasten535e1612016-12-05 12:19:36 -0800153 sp<IMemory> mIMemory; // ref-counted version, initialized in constructor and then const
154 audio_utils_fifo * const mFifo; // FIFO itself,
155 // non-NULL unless constructor fails
156 audio_utils_fifo_writer * const mFifoWriter; // used to write to FIFO,
157 // non-NULL unless dummy constructor used
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800158 bool mEnabled; // whether to actually log
159};
160
161// ---------------------------------------------------------------------------
162
163// Similar to Writer, but safe for multiple threads to call concurrently
164class LockedWriter : public Writer {
165public:
166 LockedWriter();
Glenn Kasten535e1612016-12-05 12:19:36 -0800167 LockedWriter(void *shared, size_t size);
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800168
169 virtual void log(const char *string);
Glenn Kastenab7d72f2013-02-27 09:05:28 -0800170 virtual void logf(const char *fmt, ...) __attribute__ ((format (printf, 2, 3)));
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800171 virtual void logvf(const char *fmt, va_list ap);
172 virtual void logTimestamp();
173 virtual void logTimestamp(const struct timespec& ts);
174
175 virtual bool isEnabled() const;
176 virtual bool setEnabled(bool enabled);
177
178private:
179 mutable Mutex mLock;
180};
181
182// ---------------------------------------------------------------------------
183
184class Reader : public RefBase {
185public:
Glenn Kastenfb1fdc92013-07-10 17:03:19 -0700186
187 // Input parameter 'size' is the desired size of the timeline in byte units.
188 // The size of the shared memory must be at least Timeline::sharedSize(size).
Glenn Kasten535e1612016-12-05 12:19:36 -0800189 Reader(const void *shared, size_t size);
190 Reader(const sp<IMemory>& iMemory, size_t size);
Glenn Kastenfb1fdc92013-07-10 17:03:19 -0700191
Glenn Kasten535e1612016-12-05 12:19:36 -0800192 virtual ~Reader();
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800193
194 void dump(int fd, size_t indent = 0);
195 bool isIMemory(const sp<IMemory>& iMemory) const;
196
197private:
Glenn Kasten535e1612016-12-05 12:19:36 -0800198 /*const*/ Shared* const mShared; // raw pointer to shared memory, actually const but not
199 // declared as const because audio_utils_fifo() constructor
200 sp<IMemory> mIMemory; // ref-counted version, assigned only in constructor
Glenn Kasten4e01ef62013-07-11 14:29:59 -0700201 int mFd; // file descriptor
202 int mIndent; // indentation level
Glenn Kasten535e1612016-12-05 12:19:36 -0800203 audio_utils_fifo * const mFifo; // FIFO itself,
204 // non-NULL unless constructor fails
205 audio_utils_fifo_reader * const mFifoReader; // used to read from FIFO,
206 // non-NULL unless constructor fails
Glenn Kasten4e01ef62013-07-11 14:29:59 -0700207
208 void dumpLine(const String8& timestamp, String8& body);
Glenn Kasten11d8dfc2013-01-14 14:53:13 -0800209
210 static const size_t kSquashTimestamp = 5; // squash this many or more adjacent timestamps
211};
212
213}; // class NBLog
214
215} // namespace android
216
217#endif // ANDROID_MEDIA_NBLOG_H