Home | History | Annotate | Download | only in tuningfork
      1 /*
      2  * Copyright 2018 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 #pragma once
     18 
     19 #include "tuningfork/tuningfork.h"
     20 #include "tuningfork/tuningfork_extra.h"
     21 
     22 #ifdef PROTOBUF_NANO
     23 #include "pb_encode.h"
     24 #include "pb_decode.h"
     25 #endif
     26 
     27 #include <stdint.h>
     28 #include <string>
     29 #include <chrono>
     30 #include <vector>
     31 #include <jni.h>
     32 
     33 class AAsset;
     34 
     35 namespace tuningfork {
     36 
     37 typedef std::vector<uint8_t> ProtobufSerialization;
     38 
     39 // The instrumentation key identifies a tick point within a frame or a trace segment
     40 typedef uint16_t InstrumentationKey;
     41 typedef uint64_t TraceHandle;
     42 typedef std::chrono::steady_clock::time_point TimePoint;
     43 typedef std::chrono::steady_clock::duration Duration;
     44 
     45 struct Settings {
     46     struct AggregationStrategy {
     47         enum Submission {
     48             TICK_BASED,
     49             TIME_BASED
     50         };
     51         Submission method;
     52         int32_t intervalms_or_count;
     53         int32_t max_instrumentation_keys;
     54         std::vector<int> annotation_enum_size;
     55     };
     56     struct Histogram {
     57         int32_t instrument_key;
     58         float bucket_min;
     59         float bucket_max;
     60         int32_t n_buckets;
     61     };
     62     AggregationStrategy aggregation_strategy;
     63     std::vector<Histogram> histograms;
     64 };
     65 
     66 // Extra information that is uploaded with the ClearCut proto.
     67 struct ExtraUploadInfo {
     68     std::string experiment_id;
     69     std::string session_id;
     70     uint64_t total_memory_bytes;
     71     uint32_t gl_es_version;
     72     std::string build_fingerprint;
     73     std::string build_version_sdk;
     74     std::vector<uint64_t> cpu_max_freq_hz;
     75     std::string apk_package_name;
     76     int apk_version_code;
     77     int tuningfork_version;
     78 };
     79 
     80 class Backend {
     81 public:
     82     virtual ~Backend() {};
     83     virtual bool Process(const ProtobufSerialization &tuningfork_log_event) = 0;
     84 };
     85 
     86 class ParamsLoader {
     87 public:
     88     virtual ~ParamsLoader() {};
     89     virtual bool GetFidelityParams(ProtobufSerialization &fidelity_params, size_t timeout_ms) {
     90         return false;
     91     }
     92 };
     93 
     94 class ProtoPrint {
     95 public:
     96     virtual ~ProtoPrint() {};
     97     virtual void Print(const ProtobufSerialization &tuningfork_log_event);
     98 };
     99 
    100 class DebugBackend : public Backend {
    101 public:
    102     ~DebugBackend() override;
    103     bool Process(const ProtobufSerialization &tuningfork_log_event) override;
    104 };
    105 
    106 // You can provide your own time source rather than steady_clock by inheriting this and passing
    107 //   it to init.
    108 class ITimeProvider {
    109 public:
    110     virtual std::chrono::steady_clock::time_point NowNs() = 0;
    111 };
    112 
    113 // init must be called before any other functions
    114 // If no backend is passed, a debug version is used which returns empty fidelity params
    115 // and outputs histograms in protobuf text format to logcat.
    116 // If no timeProvider is passed, std::chrono::steady_clock is used.
    117 void Init(const ProtobufSerialization &settings, const ExtraUploadInfo& extra_info,
    118           Backend *backend = 0, ParamsLoader *loader = 0, ITimeProvider *time_provider = 0);
    119 
    120 // Init must be called before any other functions
    121 void Init(const ProtobufSerialization &settings, JNIEnv* env, jobject activity);
    122 
    123 // Blocking call to get fidelity parameters from the server.
    124 // Returns true if parameters could be downloaded within the timeout, false otherwise.
    125 // Note that once fidelity parameters are downloaded, any timing information is recorded
    126 //  as being associated with those parameters.
    127 // If you subsequently call GetFidelityParameters, any data that is already collected will be
    128 // submitted to the backend.
    129 bool GetFidelityParameters(const ProtobufSerialization& defaultParams,
    130                            ProtobufSerialization &params, size_t timeout_ms);
    131 
    132 // Protobuf serialization of the current annotation
    133 // Returns the internal annotation id if it was set or -1 if not
    134 uint64_t SetCurrentAnnotation(const ProtobufSerialization &annotation);
    135 
    136 // Record a frame tick that will be associated with the instrumentation key and the current
    137 //   annotation
    138 void FrameTick(InstrumentationKey id);
    139 
    140 // Record a frame tick using an external time, rather than system time
    141 void FrameDeltaTimeNanos(InstrumentationKey id, Duration dt);
    142 
    143 // Start a trace segment
    144 TraceHandle StartTrace(InstrumentationKey key);
    145 
    146 // Record a trace with the key and annotation set using startTrace
    147 void EndTrace(TraceHandle h);
    148 
    149 void SetUploadCallback(ProtoCallback cbk);
    150 
    151 } // namespace tuningfork
    152