Adrian Roos | 1e1a128 | 2017-11-01 19:05:31 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017 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 | #undef LOG_TAG |
| 17 | #define LOG_TAG "SurfaceTracing" |
| 18 | #define ATRACE_TAG ATRACE_TAG_GRAPHICS |
| 19 | |
| 20 | #include "SurfaceTracing.h" |
| 21 | |
| 22 | #include <android-base/file.h> |
| 23 | #include <log/log.h> |
| 24 | #include <utils/SystemClock.h> |
| 25 | #include <utils/Trace.h> |
| 26 | |
| 27 | namespace android { |
| 28 | |
| 29 | void SurfaceTracing::enable() { |
Chia-I Wu | a3e7ddc | 2018-09-20 11:42:46 -0700 | [diff] [blame] | 30 | ATRACE_CALL(); |
| 31 | std::lock_guard<std::mutex> protoGuard(mTraceMutex); |
| 32 | |
Adrian Roos | 1e1a128 | 2017-11-01 19:05:31 +0100 | [diff] [blame] | 33 | if (mEnabled) { |
| 34 | return; |
| 35 | } |
Adrian Roos | 1e1a128 | 2017-11-01 19:05:31 +0100 | [diff] [blame] | 36 | mEnabled = true; |
Adrian Roos | 1e1a128 | 2017-11-01 19:05:31 +0100 | [diff] [blame] | 37 | |
Chia-I Wu | a3e7ddc | 2018-09-20 11:42:46 -0700 | [diff] [blame] | 38 | mTrace = std::make_unique<LayersTraceFileProto>(); |
| 39 | mTrace->set_magic_number(uint64_t(LayersTraceFileProto_MagicNumber_MAGIC_NUMBER_H) << 32 | |
| 40 | LayersTraceFileProto_MagicNumber_MAGIC_NUMBER_L); |
Adrian Roos | 1e1a128 | 2017-11-01 19:05:31 +0100 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | status_t SurfaceTracing::disable() { |
Chia-I Wu | a3e7ddc | 2018-09-20 11:42:46 -0700 | [diff] [blame] | 44 | ATRACE_CALL(); |
| 45 | std::lock_guard<std::mutex> protoGuard(mTraceMutex); |
| 46 | |
Adrian Roos | 1e1a128 | 2017-11-01 19:05:31 +0100 | [diff] [blame] | 47 | if (!mEnabled) { |
| 48 | return NO_ERROR; |
| 49 | } |
Adrian Roos | 1e1a128 | 2017-11-01 19:05:31 +0100 | [diff] [blame] | 50 | mEnabled = false; |
| 51 | status_t err(writeProtoFileLocked()); |
| 52 | ALOGE_IF(err == PERMISSION_DENIED, "Could not save the proto file! Permission denied"); |
| 53 | ALOGE_IF(err == NOT_ENOUGH_DATA, "Could not save the proto file! There are missing fields"); |
Chia-I Wu | a3e7ddc | 2018-09-20 11:42:46 -0700 | [diff] [blame] | 54 | mTrace.reset(); |
Adrian Roos | 1e1a128 | 2017-11-01 19:05:31 +0100 | [diff] [blame] | 55 | return err; |
| 56 | } |
| 57 | |
Yichi Chen | adc6961 | 2018-09-15 14:51:18 +0800 | [diff] [blame] | 58 | bool SurfaceTracing::isEnabled() const { |
Chia-I Wu | a3e7ddc | 2018-09-20 11:42:46 -0700 | [diff] [blame] | 59 | std::lock_guard<std::mutex> protoGuard(mTraceMutex); |
Adrian Roos | 1e1a128 | 2017-11-01 19:05:31 +0100 | [diff] [blame] | 60 | return mEnabled; |
| 61 | } |
| 62 | |
| 63 | void SurfaceTracing::traceLayers(const char* where, LayersProto layers) { |
| 64 | std::lock_guard<std::mutex> protoGuard(mTraceMutex); |
Vishnu Nair | f770fcc | 2018-06-12 14:39:30 -0700 | [diff] [blame] | 65 | if (!mEnabled) { |
| 66 | return; |
| 67 | } |
Chia-I Wu | a3e7ddc | 2018-09-20 11:42:46 -0700 | [diff] [blame] | 68 | LayersTraceProto* entry = mTrace->add_entry(); |
Adrian Roos | 1e1a128 | 2017-11-01 19:05:31 +0100 | [diff] [blame] | 69 | entry->set_elapsed_realtime_nanos(elapsedRealtimeNano()); |
| 70 | entry->set_where(where); |
| 71 | entry->mutable_layers()->Swap(&layers); |
Yichi Chen | adc6961 | 2018-09-15 14:51:18 +0800 | [diff] [blame] | 72 | |
| 73 | constexpr int maxBufferedEntryCount = 3600; |
Chia-I Wu | a3e7ddc | 2018-09-20 11:42:46 -0700 | [diff] [blame] | 74 | if (mTrace->entry_size() >= maxBufferedEntryCount) { |
Yichi Chen | adc6961 | 2018-09-15 14:51:18 +0800 | [diff] [blame] | 75 | // TODO: flush buffered entries without disabling tracing |
| 76 | ALOGE("too many buffered frames; force disable tracing"); |
| 77 | mEnabled = false; |
| 78 | writeProtoFileLocked(); |
Chia-I Wu | a3e7ddc | 2018-09-20 11:42:46 -0700 | [diff] [blame] | 79 | mTrace.reset(); |
Yichi Chen | adc6961 | 2018-09-15 14:51:18 +0800 | [diff] [blame] | 80 | } |
Adrian Roos | 1e1a128 | 2017-11-01 19:05:31 +0100 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | status_t SurfaceTracing::writeProtoFileLocked() { |
| 84 | ATRACE_CALL(); |
| 85 | |
Chia-I Wu | a3e7ddc | 2018-09-20 11:42:46 -0700 | [diff] [blame] | 86 | if (!mTrace->IsInitialized()) { |
Adrian Roos | 1e1a128 | 2017-11-01 19:05:31 +0100 | [diff] [blame] | 87 | return NOT_ENOUGH_DATA; |
| 88 | } |
| 89 | std::string output; |
Chia-I Wu | a3e7ddc | 2018-09-20 11:42:46 -0700 | [diff] [blame] | 90 | if (!mTrace->SerializeToString(&output)) { |
Adrian Roos | 1e1a128 | 2017-11-01 19:05:31 +0100 | [diff] [blame] | 91 | return PERMISSION_DENIED; |
| 92 | } |
| 93 | if (!android::base::WriteStringToFile(output, mOutputFileName, true)) { |
| 94 | return PERMISSION_DENIED; |
| 95 | } |
| 96 | |
| 97 | return NO_ERROR; |
| 98 | } |
| 99 | |
Yichi Chen | adc6961 | 2018-09-15 14:51:18 +0800 | [diff] [blame] | 100 | void SurfaceTracing::dump(String8& result) const { |
| 101 | std::lock_guard<std::mutex> protoGuard(mTraceMutex); |
| 102 | |
| 103 | result.appendFormat("Tracing state: %s\n", mEnabled ? "enabled" : "disabled"); |
Chia-I Wu | a3e7ddc | 2018-09-20 11:42:46 -0700 | [diff] [blame] | 104 | result.appendFormat(" number of entries: %d\n", mTrace ? mTrace->entry_size() : 0); |
Yichi Chen | adc6961 | 2018-09-15 14:51:18 +0800 | [diff] [blame] | 105 | } |
| 106 | |
Adrian Roos | 1e1a128 | 2017-11-01 19:05:31 +0100 | [diff] [blame] | 107 | } // namespace android |