Home | History | Annotate | Download | only in external
      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 #undef LOG_TAG
     18 #define LOG_TAG "GpuStatsPuller_test"
     19 
     20 #include <gmock/gmock.h>
     21 #include <gtest/gtest.h>
     22 
     23 #include <graphicsenv/GpuStatsInfo.h>
     24 #include <log/log.h>
     25 
     26 #include "src/external/GpuStatsPuller.h"
     27 
     28 #ifdef __ANDROID__
     29 
     30 namespace android {
     31 namespace os {
     32 namespace statsd {
     33 
     34 // clang-format off
     35 static const std::string DRIVER_PACKAGE_NAME      = "TEST_DRIVER";
     36 static const std::string DRIVER_VERSION_NAME      = "TEST_DRIVER_VERSION";
     37 static const std::string APP_PACKAGE_NAME         = "TEST_APP";
     38 static const int64_t TIMESTAMP_WALLCLOCK          = 111;
     39 static const int64_t TIMESTAMP_ELAPSED            = 222;
     40 static const int64_t DRIVER_VERSION_CODE          = 333;
     41 static const int64_t DRIVER_BUILD_TIME            = 444;
     42 static const int64_t GL_LOADING_COUNT             = 3;
     43 static const int64_t GL_LOADING_FAILURE_COUNT     = 1;
     44 static const int64_t VK_LOADING_COUNT             = 4;
     45 static const int64_t VK_LOADING_FAILURE_COUNT     = 0;
     46 static const int64_t ANGLE_LOADING_COUNT          = 2;
     47 static const int64_t ANGLE_LOADING_FAILURE_COUNT  = 1;
     48 static const int64_t GL_DRIVER_LOADING_TIME_0     = 555;
     49 static const int64_t GL_DRIVER_LOADING_TIME_1     = 666;
     50 static const int64_t VK_DRIVER_LOADING_TIME_0     = 777;
     51 static const int64_t VK_DRIVER_LOADING_TIME_1     = 888;
     52 static const int64_t VK_DRIVER_LOADING_TIME_2     = 999;
     53 static const int64_t ANGLE_DRIVER_LOADING_TIME_0  = 1010;
     54 static const int64_t ANGLE_DRIVER_LOADING_TIME_1  = 1111;
     55 static const int32_t VULKAN_VERSION               = 1;
     56 static const int32_t CPU_VULKAN_VERSION           = 2;
     57 static const int32_t GLES_VERSION                 = 3;
     58 static const bool CPU_VULKAN_IN_USE               = true;
     59 static const size_t NUMBER_OF_VALUES_GLOBAL       = 13;
     60 static const size_t NUMBER_OF_VALUES_APP          = 6;
     61 // clang-format on
     62 
     63 class MockGpuStatsPuller : public GpuStatsPuller {
     64 public:
     65     MockGpuStatsPuller(const int tagId, vector<std::shared_ptr<LogEvent>>* data)
     66         : GpuStatsPuller(tagId), mData(data){};
     67 
     68 private:
     69     bool PullInternal(vector<std::shared_ptr<LogEvent>>* data) override {
     70         *data = *mData;
     71         return true;
     72     }
     73 
     74     vector<std::shared_ptr<LogEvent>>* mData;
     75 };
     76 
     77 class GpuStatsPuller_test : public ::testing::Test {
     78 public:
     79     GpuStatsPuller_test() {
     80         const ::testing::TestInfo* const test_info =
     81                 ::testing::UnitTest::GetInstance()->current_test_info();
     82         ALOGD("**** Setting up for %s.%s\n", test_info->test_case_name(), test_info->name());
     83     }
     84 
     85     ~GpuStatsPuller_test() {
     86         const ::testing::TestInfo* const test_info =
     87                 ::testing::UnitTest::GetInstance()->current_test_info();
     88         ALOGD("**** Tearing down after %s.%s\n", test_info->test_case_name(), test_info->name());
     89     }
     90 };
     91 
     92 TEST_F(GpuStatsPuller_test, PullGpuStatsGlobalInfo) {
     93     vector<std::shared_ptr<LogEvent>> inData, outData;
     94     std::shared_ptr<LogEvent> event = make_shared<LogEvent>(android::util::GPU_STATS_GLOBAL_INFO,
     95                                                             TIMESTAMP_WALLCLOCK, TIMESTAMP_ELAPSED);
     96     EXPECT_TRUE(event->write(DRIVER_PACKAGE_NAME));
     97     EXPECT_TRUE(event->write(DRIVER_VERSION_NAME));
     98     EXPECT_TRUE(event->write(DRIVER_VERSION_CODE));
     99     EXPECT_TRUE(event->write(DRIVER_BUILD_TIME));
    100     EXPECT_TRUE(event->write(GL_LOADING_COUNT));
    101     EXPECT_TRUE(event->write(GL_LOADING_FAILURE_COUNT));
    102     EXPECT_TRUE(event->write(VK_LOADING_COUNT));
    103     EXPECT_TRUE(event->write(VK_LOADING_FAILURE_COUNT));
    104     EXPECT_TRUE(event->write(VULKAN_VERSION));
    105     EXPECT_TRUE(event->write(CPU_VULKAN_VERSION));
    106     EXPECT_TRUE(event->write(GLES_VERSION));
    107     EXPECT_TRUE(event->write(ANGLE_LOADING_COUNT));
    108     EXPECT_TRUE(event->write(ANGLE_LOADING_FAILURE_COUNT));
    109     event->init();
    110     inData.emplace_back(event);
    111     MockGpuStatsPuller mockPuller(android::util::GPU_STATS_GLOBAL_INFO, &inData);
    112     mockPuller.ForceClearCache();
    113     mockPuller.Pull(&outData);
    114 
    115     ASSERT_EQ(1, outData.size());
    116     EXPECT_EQ(android::util::GPU_STATS_GLOBAL_INFO, outData[0]->GetTagId());
    117     ASSERT_EQ(NUMBER_OF_VALUES_GLOBAL, outData[0]->size());
    118     EXPECT_EQ(DRIVER_PACKAGE_NAME, outData[0]->getValues()[0].mValue.str_value);
    119     EXPECT_EQ(DRIVER_VERSION_NAME, outData[0]->getValues()[1].mValue.str_value);
    120     EXPECT_EQ(DRIVER_VERSION_CODE, outData[0]->getValues()[2].mValue.long_value);
    121     EXPECT_EQ(DRIVER_BUILD_TIME, outData[0]->getValues()[3].mValue.long_value);
    122     EXPECT_EQ(GL_LOADING_COUNT, outData[0]->getValues()[4].mValue.long_value);
    123     EXPECT_EQ(GL_LOADING_FAILURE_COUNT, outData[0]->getValues()[5].mValue.long_value);
    124     EXPECT_EQ(VK_LOADING_COUNT, outData[0]->getValues()[6].mValue.long_value);
    125     EXPECT_EQ(VK_LOADING_FAILURE_COUNT, outData[0]->getValues()[7].mValue.long_value);
    126     EXPECT_EQ(VULKAN_VERSION, outData[0]->getValues()[8].mValue.int_value);
    127     EXPECT_EQ(CPU_VULKAN_VERSION, outData[0]->getValues()[9].mValue.int_value);
    128     EXPECT_EQ(GLES_VERSION, outData[0]->getValues()[10].mValue.int_value);
    129     EXPECT_EQ(ANGLE_LOADING_COUNT, outData[0]->getValues()[11].mValue.long_value);
    130     EXPECT_EQ(ANGLE_LOADING_FAILURE_COUNT, outData[0]->getValues()[12].mValue.long_value);
    131 }
    132 
    133 TEST_F(GpuStatsPuller_test, PullGpuStatsAppInfo) {
    134     vector<std::shared_ptr<LogEvent>> inData, outData;
    135     std::shared_ptr<LogEvent> event = make_shared<LogEvent>(android::util::GPU_STATS_APP_INFO,
    136                                                             TIMESTAMP_WALLCLOCK, TIMESTAMP_ELAPSED);
    137     EXPECT_TRUE(event->write(APP_PACKAGE_NAME));
    138     EXPECT_TRUE(event->write(DRIVER_VERSION_CODE));
    139     std::vector<int64_t> glDriverLoadingTime;
    140     glDriverLoadingTime.emplace_back(GL_DRIVER_LOADING_TIME_0);
    141     glDriverLoadingTime.emplace_back(GL_DRIVER_LOADING_TIME_1);
    142     std::vector<int64_t> vkDriverLoadingTime;
    143     vkDriverLoadingTime.emplace_back(VK_DRIVER_LOADING_TIME_0);
    144     vkDriverLoadingTime.emplace_back(VK_DRIVER_LOADING_TIME_1);
    145     vkDriverLoadingTime.emplace_back(VK_DRIVER_LOADING_TIME_2);
    146     std::vector<int64_t> angleDriverLoadingTime;
    147     angleDriverLoadingTime.emplace_back(ANGLE_DRIVER_LOADING_TIME_0);
    148     angleDriverLoadingTime.emplace_back(ANGLE_DRIVER_LOADING_TIME_1);
    149     EXPECT_TRUE(event->write(int64VectorToProtoByteString(glDriverLoadingTime)));
    150     EXPECT_TRUE(event->write(int64VectorToProtoByteString(vkDriverLoadingTime)));
    151     EXPECT_TRUE(event->write(int64VectorToProtoByteString(angleDriverLoadingTime)));
    152     EXPECT_TRUE(event->write(CPU_VULKAN_IN_USE));
    153     event->init();
    154     inData.emplace_back(event);
    155     MockGpuStatsPuller mockPuller(android::util::GPU_STATS_APP_INFO, &inData);
    156     mockPuller.ForceClearCache();
    157     mockPuller.Pull(&outData);
    158 
    159     ASSERT_EQ(1, outData.size());
    160     EXPECT_EQ(android::util::GPU_STATS_APP_INFO, outData[0]->GetTagId());
    161     ASSERT_EQ(NUMBER_OF_VALUES_APP, outData[0]->size());
    162     EXPECT_EQ(APP_PACKAGE_NAME, outData[0]->getValues()[0].mValue.str_value);
    163     EXPECT_EQ(DRIVER_VERSION_CODE, outData[0]->getValues()[1].mValue.long_value);
    164     EXPECT_EQ(int64VectorToProtoByteString(glDriverLoadingTime),
    165               outData[0]->getValues()[2].mValue.str_value);
    166     EXPECT_EQ(int64VectorToProtoByteString(vkDriverLoadingTime),
    167               outData[0]->getValues()[3].mValue.str_value);
    168     EXPECT_EQ(int64VectorToProtoByteString(angleDriverLoadingTime),
    169               outData[0]->getValues()[4].mValue.str_value);
    170     EXPECT_EQ(CPU_VULKAN_IN_USE, outData[0]->getValues()[5].mValue.int_value);
    171 }
    172 
    173 }  // namespace statsd
    174 }  // namespace os
    175 }  // namespace android
    176 #else
    177 GTEST_LOG_(INFO) << "This test does nothing.\n";
    178 #endif
    179