Add various benchmarks.

These are based on ones from system/extras/tests/bionic/.

Change-Id: I7b1ae15a2ca5d1031a6a511d97b88125c1770047
diff --git a/benchmarks/Android.mk b/benchmarks/Android.mk
index f64c108..797ce62 100644
--- a/benchmarks/Android.mk
+++ b/benchmarks/Android.mk
@@ -33,8 +33,12 @@
     benchmark_main.cpp \
     math_benchmark.cpp \
     property_benchmark.cpp \
+    pthread_benchmark.cpp \
+    semaphore_benchmark.cpp \
+    stdio_benchmark.cpp \
     string_benchmark.cpp \
     time_benchmark.cpp \
+    unistd_benchmark.cpp \
 
 # Build benchmarks for the device (with bionic's .so). Run with:
 #   adb shell bionic-benchmarks
diff --git a/benchmarks/pthread_benchmark.cpp b/benchmarks/pthread_benchmark.cpp
new file mode 100644
index 0000000..621fcb6
--- /dev/null
+++ b/benchmarks/pthread_benchmark.cpp
@@ -0,0 +1,104 @@
+/*
+ * Copyright (C) 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 "benchmark.h"
+
+#include <pthread.h>
+
+static void BM_pthread_self(int iters) {
+  StartBenchmarkTiming();
+
+  for (int i = 0; i < iters; ++i) {
+    pthread_self();
+  }
+
+  StopBenchmarkTiming();
+}
+BENCHMARK(BM_pthread_self);
+
+static void BM_pthread_getspecific(int iters) {
+  StopBenchmarkTiming();
+  pthread_key_t key;
+  pthread_key_create(&key, NULL);
+  StartBenchmarkTiming();
+
+  for (int i = 0; i < iters; ++i) {
+    pthread_getspecific(key);
+  }
+
+  StopBenchmarkTiming();
+  pthread_key_delete(key);
+}
+BENCHMARK(BM_pthread_getspecific);
+
+static void DummyPthreadOnceInitFunction() {
+}
+
+static void BM_pthread_once(int iters) {
+  StopBenchmarkTiming();
+  pthread_once_t once = PTHREAD_ONCE_INIT;
+  pthread_once(&once, DummyPthreadOnceInitFunction);
+  StartBenchmarkTiming();
+
+  for (int i = 0; i < iters; ++i) {
+    pthread_once(&once, DummyPthreadOnceInitFunction);
+  }
+
+  StopBenchmarkTiming();
+}
+BENCHMARK(BM_pthread_once);
+
+static void BM_pthread_mutex_lock(int iters) {
+  StopBenchmarkTiming();
+  pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
+  StartBenchmarkTiming();
+
+  for (int i = 0; i < iters; ++i) {
+    pthread_mutex_lock(&mutex);
+    pthread_mutex_unlock(&mutex);
+  }
+
+  StopBenchmarkTiming();
+}
+BENCHMARK(BM_pthread_mutex_lock);
+
+static void BM_pthread_mutex_lock_ERRORCHECK(int iters) {
+  StopBenchmarkTiming();
+  pthread_mutex_t mutex = PTHREAD_ERRORCHECK_MUTEX_INITIALIZER;
+  StartBenchmarkTiming();
+
+  for (int i = 0; i < iters; ++i) {
+    pthread_mutex_lock(&mutex);
+    pthread_mutex_unlock(&mutex);
+  }
+
+  StopBenchmarkTiming();
+}
+BENCHMARK(BM_pthread_mutex_lock_ERRORCHECK);
+
+static void BM_pthread_mutex_lock_RECURSIVE(int iters) {
+  StopBenchmarkTiming();
+  pthread_mutex_t mutex = PTHREAD_RECURSIVE_MUTEX_INITIALIZER;
+  StartBenchmarkTiming();
+
+  for (int i = 0; i < iters; ++i) {
+    pthread_mutex_lock(&mutex);
+    pthread_mutex_unlock(&mutex);
+  }
+
+  StopBenchmarkTiming();
+}
+BENCHMARK(BM_pthread_mutex_lock_RECURSIVE);
diff --git a/benchmarks/semaphore_benchmark.cpp b/benchmarks/semaphore_benchmark.cpp
new file mode 100644
index 0000000..a11fcc1
--- /dev/null
+++ b/benchmarks/semaphore_benchmark.cpp
@@ -0,0 +1,49 @@
+/*
+ * Copyright (C) 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 "benchmark.h"
+
+#include <semaphore.h>
+
+static void BM_semaphore_sem_getvalue(int iters) {
+  StopBenchmarkTiming();
+  sem_t semaphore;
+  sem_init(&semaphore, 1, 1);
+  StartBenchmarkTiming();
+
+  for (int i = 0; i < iters; ++i) {
+    int dummy;
+    sem_getvalue(&semaphore, &dummy);
+  }
+
+  StopBenchmarkTiming();
+}
+BENCHMARK(BM_semaphore_sem_getvalue);
+
+static void BM_semaphore_sem_wait_sem_post(int iters) {
+  StopBenchmarkTiming();
+  sem_t semaphore;
+  sem_init(&semaphore, 1, 1);
+  StartBenchmarkTiming();
+
+  for (int i = 0; i < iters; ++i) {
+    sem_wait(&semaphore);
+    sem_post(&semaphore);
+  }
+
+  StopBenchmarkTiming();
+}
+BENCHMARK(BM_semaphore_sem_wait_sem_post);
diff --git a/benchmarks/stdio_benchmark.cpp b/benchmarks/stdio_benchmark.cpp
new file mode 100644
index 0000000..e899df7
--- /dev/null
+++ b/benchmarks/stdio_benchmark.cpp
@@ -0,0 +1,61 @@
+/*
+ * Copyright (C) 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 "benchmark.h"
+
+#include <stdio.h>
+
+#define KB 1024
+#define MB 1024*KB
+
+#define AT_COMMON_SIZES \
+    Arg(1)->Arg(2)->Arg(3)->Arg(4)->Arg(8)->Arg(16)->Arg(32)->Arg(64)->Arg(512)-> \
+    Arg(1*KB)->Arg(4*KB)->Arg(8*KB)->Arg(16*KB)->Arg(64*KB)
+
+static void BM_stdio_fread(int iters, int chunk_size) {
+  StopBenchmarkTiming();
+  FILE* fp = fopen("/dev/zero", "rw");
+  char* buf = new char[chunk_size];
+  StartBenchmarkTiming();
+
+  for (int i = 0; i < iters; ++i) {
+    fread(buf, chunk_size, 1, fp);
+  }
+
+  StopBenchmarkTiming();
+  SetBenchmarkBytesProcessed(int64_t(iters) * int64_t(chunk_size));
+  delete[] buf;
+  fclose(fp);
+}
+BENCHMARK(BM_stdio_fread)->AT_COMMON_SIZES;
+
+
+static void BM_stdio_fwrite(int iters, int chunk_size) {
+  StopBenchmarkTiming();
+  FILE* fp = fopen("/dev/zero", "rw");
+  char* buf = new char[chunk_size];
+  StartBenchmarkTiming();
+
+  for (int i = 0; i < iters; ++i) {
+      fwrite(buf, chunk_size, 1, fp);
+  }
+
+  StopBenchmarkTiming();
+  SetBenchmarkBytesProcessed(int64_t(iters) * int64_t(chunk_size));
+  delete[] buf;
+  fclose(fp);
+}
+BENCHMARK(BM_stdio_fwrite)->AT_COMMON_SIZES;
diff --git a/benchmarks/unistd_benchmark.cpp b/benchmarks/unistd_benchmark.cpp
new file mode 100644
index 0000000..e839bf8
--- /dev/null
+++ b/benchmarks/unistd_benchmark.cpp
@@ -0,0 +1,30 @@
+/*
+ * Copyright (C) 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 "benchmark.h"
+
+#include <unistd.h>
+
+static void BM_unistd_getpid(int iters) {
+  StartBenchmarkTiming();
+
+  for (int i = 0; i < iters; ++i) {
+    getpid();
+  }
+
+  StopBenchmarkTiming();
+}
+BENCHMARK(BM_unistd_getpid);