Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 8d1fdb50 authored by Mark Salyzyn's avatar Mark Salyzyn
Browse files

liblog: Add liblog test suite

Change-Id: Ia457d518b4e7ff37e840336ff0c48583709700d4
parent 706fad2b
Loading
Loading
Loading
Loading
+2 −2
Original line number Diff line number Diff line
/*
 * Copyright (C) 2005-2013 The Android Open Source Project
 * Copyright (C) 2005-2014 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
@@ -563,4 +563,4 @@ int __android_log_buf_print(int bufID, int prio, const char *tag, const char *fm
}
#endif

#endif // _LIBS_CUTILS_LOG_H
#endif /* _LIBS_LOG_LOG_H */

include/log/log_read.h

0 → 100644
+79 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2013-2014 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef _LIBS_LOG_LOG_READ_H
#define _LIBS_LOG_LOG_READ_H

#include <time.h>

#define NS_PER_SEC 1000000000ULL
#ifdef __cplusplus
struct log_time : public timespec {
public:
    log_time(timespec &T)
    {
        tv_sec = T.tv_sec;
        tv_nsec = T.tv_nsec;
    }
    log_time(void)
    {
    }
    log_time(clockid_t id)
    {
        clock_gettime(id, (timespec *) this);
    }
    log_time(const char *T)
    {
        const uint8_t *c = (const uint8_t *) T;
        tv_sec = c[0] | (c[1] << 8) | (c[2] << 16) | (c[3] << 24);
        tv_nsec = c[4] | (c[5] << 8) | (c[6] << 16) | (c[7] << 24);
    }
    bool operator== (const timespec &T) const
    {
        return (tv_sec == T.tv_sec) && (tv_nsec == T.tv_nsec);
    }
    bool operator!= (const timespec &T) const
    {
        return !(*this == T);
    }
    bool operator< (const timespec &T) const
    {
        return (tv_sec < T.tv_sec)
            || ((tv_sec == T.tv_sec) && (tv_nsec < T.tv_nsec));
    }
    bool operator>= (const timespec &T) const
    {
        return !(*this < T);
    }
    bool operator> (const timespec &T) const
    {
        return (tv_sec > T.tv_sec)
            || ((tv_sec == T.tv_sec) && (tv_nsec > T.tv_nsec));
    }
    bool operator<= (const timespec &T) const
    {
        return !(*this > T);
    }
    uint64_t nsec(void) const
    {
        return static_cast<uint64_t>(tv_sec) * NS_PER_SEC + tv_nsec;
    }
};
#else
typedef struct timespec log_time;
#endif

#endif /* define _LIBS_LOG_LOG_READ_H */
+3 −2
Original line number Diff line number Diff line
#
# Copyright (C) 2008 The Android Open Source Project
# Copyright (C) 2008-2014 The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
@@ -67,7 +67,6 @@ LOCAL_LDLIBS := -lpthread
LOCAL_CFLAGS := -DFAKE_LOG_DEVICE=1 -m64
include $(BUILD_HOST_STATIC_LIBRARY)


# Shared and static library for target
# ========================================================
include $(CLEAR_VARS)
@@ -79,3 +78,5 @@ include $(CLEAR_VARS)
LOCAL_MODULE := liblog
LOCAL_WHOLE_STATIC_LIBRARIES := liblog
include $(BUILD_SHARED_LIBRARY)

include $(call first-makefiles-under,$(LOCAL_PATH))
+77 −0
Original line number Diff line number Diff line
#
# Copyright (C) 2013-2014 The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

LOCAL_PATH := $(call my-dir)

# -----------------------------------------------------------------------------
# Benchmarks.
# -----------------------------------------------------------------------------

test_module_prefix := liblog-
test_tags := tests

benchmark_c_flags := \
    -Ibionic/tests \
    -Wall -Wextra \
    -Werror \
    -fno-builtin \
    -std=gnu++11

benchmark_src_files := \
    benchmark_main.cpp \
    liblog_benchmark.cpp \

# Build benchmarks for the device. Run with:
#   adb shell liblog-benchmarks
include $(CLEAR_VARS)
LOCAL_MODULE := $(test_module_prefix)benchmarks
LOCAL_MODULE_TAGS := $(test_tags)
LOCAL_ADDITIONAL_DEPENDENCIES := $(LOCAL_PATH)/Android.mk
LOCAL_CFLAGS += $(benchmark_c_flags)
LOCAL_SHARED_LIBRARIES += liblog libm
LOCAL_SRC_FILES := $(benchmark_src_files)
ifndef LOCAL_SDK_VERSION
LOCAL_C_INCLUDES += bionic bionic/libstdc++/include external/stlport/stlport
LOCAL_SHARED_LIBRARIES += libstlport
endif
LOCAL_MODULE_PATH := $(TARGET_OUT_DATA_NATIVE_TESTS)/$(LOCAL_MODULE)
include $(BUILD_EXECUTABLE)

# -----------------------------------------------------------------------------
# Unit tests.
# -----------------------------------------------------------------------------

test_c_flags := \
    -fstack-protector-all \
    -g \
    -Wall -Wextra \
    -Werror \
    -fno-builtin \

test_src_files := \
    liblog_test.cpp \

# Build tests for the device (with .so). Run with:
#   adb shell /data/nativetest/liblog-unit-tests/liblog-unit-tests
include $(CLEAR_VARS)
LOCAL_MODULE := $(test_module_prefix)unit-tests
LOCAL_MODULE_TAGS := $(test_tags)
LOCAL_ADDITIONAL_DEPENDENCIES := $(LOCAL_PATH)/Android.mk
LOCAL_CFLAGS += $(test_c_flags)
LOCAL_LDLIBS := -lpthread
LOCAL_SHARED_LIBRARIES := liblog
LOCAL_SRC_FILES := $(test_src_files)
include $(BUILD_NATIVE_TEST)
+147 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2012-2014 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <vector>

#ifndef BIONIC_BENCHMARK_H_
#define BIONIC_BENCHMARK_H_

namespace testing {

class Benchmark;
template <typename T> class BenchmarkWantsArg;
template <typename T> class BenchmarkWithArg;

void BenchmarkRegister(Benchmark* bm);
int PrettyPrintInt(char* str, int len, unsigned int arg);

class Benchmark {
 public:
  Benchmark(const char* name, void (*fn)(int)) : name_(strdup(name)), fn_(fn) {
    BenchmarkRegister(this);
  }
  Benchmark(const char* name) : name_(strdup(name)), fn_(NULL) {}

  virtual ~Benchmark() {
    free(name_);
  }

  const char* Name() { return name_; }
  virtual const char* ArgName() { return NULL; }
  virtual void RunFn(int iterations) { fn_(iterations); }

 protected:
  char* name_;

 private:
  void (*fn_)(int);
};

template <typename T>
class BenchmarkWantsArgBase : public Benchmark {
 public:
  BenchmarkWantsArgBase(const char* name, void (*fn)(int, T)) : Benchmark(name) {
    fn_arg_ = fn;
  }

  BenchmarkWantsArgBase<T>* Arg(const char* arg_name, T arg) {
    BenchmarkRegister(new BenchmarkWithArg<T>(name_, fn_arg_, arg_name, arg));
    return this;
  }

 protected:
  virtual void RunFn(int) { printf("can't run arg benchmark %s without arg\n", Name()); }
  void (*fn_arg_)(int, T);
};

template <typename T>
class BenchmarkWithArg : public BenchmarkWantsArg<T> {
 public:
  BenchmarkWithArg(const char* name, void (*fn)(int, T), const char* arg_name, T arg) :
      BenchmarkWantsArg<T>(name, fn), arg_(arg) {
    arg_name_ = strdup(arg_name);
  }

  virtual ~BenchmarkWithArg() {
    free(arg_name_);
  }

  virtual const char* ArgName() { return arg_name_; }

 protected:
  virtual void RunFn(int iterations) { BenchmarkWantsArg<T>::fn_arg_(iterations, arg_); }

 private:
  T arg_;
  char* arg_name_;
};

template <typename T>
class BenchmarkWantsArg : public BenchmarkWantsArgBase<T> {
 public:
  BenchmarkWantsArg<T>(const char* name, void (*fn)(int, T)) :
    BenchmarkWantsArgBase<T>(name, fn) { }
};

template <>
class BenchmarkWantsArg<int> : public BenchmarkWantsArgBase<int> {
 public:
  BenchmarkWantsArg<int>(const char* name, void (*fn)(int, int)) :
    BenchmarkWantsArgBase<int>(name, fn) { }

  BenchmarkWantsArg<int>* Arg(int arg) {
    char arg_name[100];
    PrettyPrintInt(arg_name, sizeof(arg_name), arg);
    BenchmarkRegister(new BenchmarkWithArg<int>(name_, fn_arg_, arg_name, arg));
    return this;
  }
};

static inline Benchmark* BenchmarkFactory(const char* name, void (*fn)(int)) {
  return new Benchmark(name, fn);
}

template <typename T>
static inline BenchmarkWantsArg<T>* BenchmarkFactory(const char* name, void (*fn)(int, T)) {
  return new BenchmarkWantsArg<T>(name, fn);
}

}  // namespace testing

template <typename T>
static inline void BenchmarkAddArg(::testing::Benchmark* b, const char* name, T arg) {
  ::testing::BenchmarkWantsArg<T>* ba;
  ba = static_cast< ::testing::BenchmarkWantsArg<T>* >(b);
  ba->Arg(name, arg);
}

void SetBenchmarkBytesProcessed(uint64_t);
void ResetBenchmarkTiming(void);
void StopBenchmarkTiming(void);
void StartBenchmarkTiming(void);
void StartBenchmarkTiming(uint64_t);
void StopBenchmarkTiming(uint64_t);

#define BENCHMARK(f) \
    static ::testing::Benchmark* _benchmark_##f __attribute__((unused)) = \
        (::testing::Benchmark*)::testing::BenchmarkFactory(#f, f)

#endif // BIONIC_BENCHMARK_H_
Loading