Yiwei Zhang | 7e63303 | 2019-03-01 17:25:27 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2019 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 | #include "GpuStatsPuller.h" |
| 18 | |
| 19 | #include <binder/IServiceManager.h> |
| 20 | #include <graphicsenv/GpuStatsInfo.h> |
| 21 | #include <graphicsenv/IGpuService.h> |
| 22 | |
| 23 | #include "logd/LogEvent.h" |
| 24 | |
| 25 | #include "stats_log_util.h" |
| 26 | #include "statslog.h" |
| 27 | |
| 28 | namespace android { |
| 29 | namespace os { |
| 30 | namespace statsd { |
| 31 | |
| 32 | GpuStatsPuller::GpuStatsPuller(const int tagId) : StatsPuller(tagId) { |
| 33 | } |
| 34 | |
| 35 | static sp<IGpuService> getGpuService() { |
| 36 | const sp<IBinder> binder = defaultServiceManager()->checkService(String16("gpu")); |
| 37 | if (!binder) { |
| 38 | ALOGE("Failed to get gpu service"); |
| 39 | return nullptr; |
| 40 | } |
| 41 | |
| 42 | return interface_cast<IGpuService>(binder); |
| 43 | } |
| 44 | |
| 45 | static bool pullGpuStatsGlobalInfo(const sp<IGpuService>& gpuService, |
| 46 | std::vector<std::shared_ptr<LogEvent>>* data) { |
| 47 | std::vector<GpuStatsGlobalInfo> stats; |
| 48 | status_t status = gpuService->getGpuStatsGlobalInfo(&stats); |
| 49 | if (status != OK) { |
| 50 | return false; |
| 51 | } |
| 52 | |
| 53 | data->clear(); |
| 54 | data->reserve(stats.size()); |
| 55 | for (const auto& info : stats) { |
| 56 | std::shared_ptr<LogEvent> event = make_shared<LogEvent>( |
| 57 | android::util::GPU_STATS_GLOBAL_INFO, getWallClockNs(), getElapsedRealtimeNs()); |
| 58 | if (!event->write(info.driverPackageName)) return false; |
| 59 | if (!event->write(info.driverVersionName)) return false; |
| 60 | if (!event->write((int64_t)info.driverVersionCode)) return false; |
| 61 | if (!event->write(info.driverBuildTime)) return false; |
| 62 | if (!event->write((int64_t)info.glLoadingCount)) return false; |
| 63 | if (!event->write((int64_t)info.glLoadingFailureCount)) return false; |
| 64 | if (!event->write((int64_t)info.vkLoadingCount)) return false; |
| 65 | if (!event->write((int64_t)info.vkLoadingFailureCount)) return false; |
| 66 | event->init(); |
| 67 | data->emplace_back(event); |
| 68 | } |
| 69 | |
| 70 | return true; |
| 71 | } |
| 72 | |
| 73 | bool GpuStatsPuller::PullInternal(std::vector<std::shared_ptr<LogEvent>>* data) { |
| 74 | const sp<IGpuService> gpuService = getGpuService(); |
| 75 | if (!gpuService) { |
| 76 | return false; |
| 77 | } |
| 78 | |
| 79 | switch (mTagId) { |
| 80 | case android::util::GPU_STATS_GLOBAL_INFO: |
| 81 | return pullGpuStatsGlobalInfo(gpuService, data); |
| 82 | default: |
| 83 | break; |
| 84 | } |
| 85 | |
| 86 | return false; |
| 87 | } |
| 88 | |
| 89 | } // namespace statsd |
| 90 | } // namespace os |
| 91 | } // namespace android |