Home | History | Annotate | Download | only in libmeasurement
      1 /*
      2  * Copyright 2016 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 "vts_measurement.h"
     18 
     19 #if USE_CTIME
     20 #include <time.h>
     21 #else
     22 #include <sys/time.h>
     23 #endif
     24 #include <stdlib.h>
     25 
     26 #include <iostream>
     27 
     28 #define COVERAGE_SANCOV 0
     29 
     30 #if COVERAGE_SANCOV
     31 #include <sanitizer/coverage_interface.h>
     32 #endif
     33 
     34 #include <vector>
     35 
     36 using namespace std;
     37 
     38 #if COVERAGE_SANCOV
     39 
     40 extern "C" {
     41 // Re-declare some of the sanitizer functions as "weak" so that
     42 // libFuzzer can be linked w/o the sanitizers and sanitizer-coverage
     43 // (in which case it will complain at start-up time).
     44 __attribute__((weak)) void __sanitizer_print_stack_trace();
     45 __attribute__((weak)) void __sanitizer_reset_coverage();
     46 __attribute__((weak)) size_t __sanitizer_get_total_unique_caller_callee_pairs();
     47 __attribute__((weak)) size_t __sanitizer_get_total_unique_coverage();
     48 __attribute__((weak)) void __sanitizer_set_death_callback(
     49     void (*callback)(void));
     50 __attribute__((weak)) size_t __sanitizer_get_number_of_counters();
     51 __attribute__((weak)) uintptr_t
     52 __sanitizer_update_counter_bitset_and_clear_counters(uint8_t* bitset);
     53 }
     54 
     55 #define CHECK_WEAK_API_FUNCTION(fn)       \
     56   do {                                    \
     57     if (!fn) MissingWeakApiFunction(#fn); \
     58   } while (false)
     59 
     60 static void MissingWeakApiFunction(const char* FnName) {
     61   cerr << "ERROR: " << FnName << " is not defined. Exiting.\n"
     62        << "Did you use -fsanitize-coverage=... to build your code?" << endl;
     63   exit(1);
     64 }
     65 #endif
     66 
     67 namespace android {
     68 namespace vts {
     69 
     70 void VtsMeasurement::Start() {
     71 #if COVERAGE_SANCOV
     72   cout << "reset coverage";
     73   CHECK_WEAK_API_FUNCTION(__sanitizer_reset_coverage);
     74   __sanitizer_reset_coverage();
     75   cout << endl;
     76 #endif
     77 
     78 #if USE_CTIME
     79   gettimeofday(&tv_, NULL);
     80 #else
     81   clock_gettime(CLOCK_REALTIME, &ts_);
     82 #endif
     83 }
     84 
     85 vector<float>* VtsMeasurement::Stop() {
     86 #if USE_CTIME
     87   struct timeval curr_tv;
     88   gettimeofday(&curr_tv, NULL);
     89 #else
     90   timespec ts_now;
     91   clock_gettime(CLOCK_REALTIME, &ts_now);
     92 #endif
     93 
     94   vector<float>* result = new vector<float>;
     95 
     96 #if USE_CTIME
     97   float stop_time_usecs;
     98   float start_time_usecs;
     99   stop_time_usecs = curr_tv.tv_sec + curr_tv.tv_usec / 1000000.0;
    100   start_time_usecs = tv_.tv_sec + tv_.tv_usec / 1000000.0;
    101   result->push_back(stop_time_usecs - start_time_usecs);
    102 #else
    103   float stop_time_usecs;
    104   float start_time_usecs;
    105   stop_time_usecs = ts_now.tv_sec + ts_now.tv_nsec / 1000000000.0;
    106   start_time_usecs = ts_.tv_sec + ts_.tv_nsec / 1000000000.0;
    107   result->push_back(stop_time_usecs - start_time_usecs);
    108 #endif
    109 
    110 #if COVERAGE_SANCOV
    111   cout << "coverage: ";
    112   cout << __sanitizer_get_total_unique_caller_callee_pairs() << endl;
    113 #endif
    114 
    115   return result;
    116 }
    117 
    118 }  // namespace vts
    119 }  // namespace android
    120