• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "tuningfork/tuningfork.h"
18 #include "tuningfork_internal.h"
19 #include <jni.h>
20 
21 #include <cstdlib>
22 
23 namespace {
24 
ToProtobufSerialization(const CProtobufSerialization & cpbs)25 tuningfork::ProtobufSerialization ToProtobufSerialization(const CProtobufSerialization& cpbs) {
26     return tuningfork::ProtobufSerialization(cpbs.bytes, cpbs.bytes + cpbs.size);
27 }
ToCProtobufSerialization(const tuningfork::ProtobufSerialization & pbs,CProtobufSerialization * cpbs)28 void ToCProtobufSerialization(const tuningfork::ProtobufSerialization& pbs,
29                               CProtobufSerialization* cpbs) {
30     cpbs->bytes = (uint8_t*)::malloc(pbs.size());
31     memcpy(cpbs->bytes, pbs.data(), pbs.size());
32     cpbs->size = pbs.size();
33     cpbs->dealloc = ::free;
34 }
35 
36 } // anonymous namespace
37 
38 extern "C" {
39 
40 // init must be called before any other functions
41 //  If no backend is passed, a debug version is used which returns empty fidelity params
42 //   and outputs histograms in protobuf text format to logcat.
43 //  If no timeProvider is passed, std::chrono::steady_clock is used.
TuningFork_init(const CProtobufSerialization * settings,JNIEnv * env,jobject activity)44 void TuningFork_init(const CProtobufSerialization *settings, JNIEnv* env, jobject activity) {
45     if(settings)
46         tuningfork::Init(ToProtobufSerialization(*settings), env, activity);
47 }
48 
49 // Blocking call to get fidelity parameters from the server.
50 // Returns true if parameters could be downloaded within the timeout, false otherwise.
51 // Note that once fidelity parameters are downloaded, any timing information is recorded
52 //  as being associated with those parameters.
TuningFork_getFidelityParameters(const CProtobufSerialization * defaultParams,CProtobufSerialization * params,size_t timeout_ms)53 bool TuningFork_getFidelityParameters(const CProtobufSerialization *defaultParams,
54                                       CProtobufSerialization *params, size_t timeout_ms) {
55     tuningfork::ProtobufSerialization defaults;
56     if(defaultParams)
57         defaults = ToProtobufSerialization(*defaultParams);
58     tuningfork::ProtobufSerialization s;
59     bool result = tuningfork::GetFidelityParameters(defaults, s, timeout_ms);
60     if(result && params)
61         ToCProtobufSerialization(s, params);
62     return result;
63 }
64 
65 // Protobuf serialization of the current annotation
66 // Returns 0 if the annotation could be set, -1 if not
TuningFork_setCurrentAnnotation(const CProtobufSerialization * annotation)67 int TuningFork_setCurrentAnnotation(const CProtobufSerialization *annotation) {
68     if(annotation)
69         // Note that SetCurrentAnnotation returns the internal annotation id if it could be set
70         //  or -1 if it could not.
71         if(tuningfork::SetCurrentAnnotation(ToProtobufSerialization(*annotation))==-1)
72             return -1;
73         else
74             return 0;
75     else
76         return -1;
77 }
78 
79 // Record a frame tick that will be associated with the instrumentation key and the current
80 //   annotation
TuningFork_frameTick(TFInstrumentKey id)81 void TuningFork_frameTick(TFInstrumentKey id) {
82     tuningfork::FrameTick(id);
83 }
84 
85 // Record a frame tick using an external time, rather than system time
TuningFork_frameDeltaTimeNanos(TFInstrumentKey id,TFDuration dt)86 void TuningFork_frameDeltaTimeNanos(TFInstrumentKey id, TFDuration dt) {
87     tuningfork::FrameDeltaTimeNanos(id, std::chrono::nanoseconds(dt));
88 }
89 
90 // Start a trace segment
TuningFork_startTrace(TFInstrumentKey key)91 TFTraceHandle TuningFork_startTrace(TFInstrumentKey key) {
92     return tuningfork::StartTrace(key);
93 }
94 
95 // Record a trace with the key and annotation set using startTrace
TuningFork_endTrace(TFTraceHandle h)96 void TuningFork_endTrace(TFTraceHandle h) {
97     tuningfork::EndTrace(h);
98 }
99 
100 } // extern "C" {
101