blob: bad92098dedb7892f2d88b0775e995be1b8588d1 [file] [log] [blame]
The Android Open Source Projecte16cb842009-03-03 19:32:58 -08001/*
Christopher Ferris1348ce22013-04-11 17:50:18 -07002 * Copyright (C) 2013 The Android Open Source Project
The Android Open Source Projecte16cb842009-03-03 19:32:58 -08003 *
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 <stdio.h>
18#include <stdlib.h>
19#include <string.h>
Christopher Ferris1348ce22013-04-11 17:50:18 -070020#include <ctype.h>
The Android Open Source Projecte16cb842009-03-03 19:32:58 -080021#include <sys/time.h>
22#include <time.h>
23#include <unistd.h>
The Android Open Source Projecte16cb842009-03-03 19:32:58 -080024#include <sys/resource.h>
25#include <sys/syscall.h>
26#include <sys/types.h>
27#include <sys/mman.h>
28
Christopher Ferris1348ce22013-04-11 17:50:18 -070029#include "memtest.h"
The Android Open Source Projecte16cb842009-03-03 19:32:58 -080030
Christopher Ferris8f1da8f2015-11-05 12:53:10 -080031nsecs_t system_time() {
The Android Open Source Projecte16cb842009-03-03 19:32:58 -080032 struct timespec t;
33 t.tv_sec = t.tv_nsec = 0;
34 clock_gettime(CLOCK_MONOTONIC, &t);
35 return nsecs_t(t.tv_sec)*1000000000LL + t.tv_nsec;
36}
37
The Android Open Source Projecte16cb842009-03-03 19:32:58 -080038static void usage(char* p) {
Christopher Ferris1348ce22013-04-11 17:50:18 -070039 printf("Usage: %s <test> <options>\n"
40 "<test> is one of the following:\n"
41 " copy_bandwidth [--size BYTES_TO_COPY]\n"
42 " write_bandwidth [--size BYTES_TO_WRITE]\n"
Christopher Ferris1a3794a2013-05-02 15:12:11 -070043 " read_bandwidth [--size BYTES_TO_COPY]\n"
Christopher Ferris1348ce22013-04-11 17:50:18 -070044 " per_core_bandwidth [--size BYTES]\n"
Christopher Ferris65d2c782013-07-02 16:38:45 -070045 " --type copy_ldrd_strd | copy_ldmia_stmia | copy_vld1_vst1 |\n"
46 " copy_vldr_vstr | copy_vldmia_vstmia | memcpy | write_strd |\n"
47 " write_stmia | write_vst1 | write_vstr | write_vstmia | memset |\n"
48 " read_ldrd | read_ldmia | read_vld1 | read_vldr | read_vldmia\n"
Christopher Ferris1348ce22013-04-11 17:50:18 -070049 " multithread_bandwidth [--size BYTES]\n"
Christopher Ferris65d2c782013-07-02 16:38:45 -070050 " --type copy_ldrd_strd | copy_ldmia_stmia | copy_vld1_vst1 |\n"
51 " copy_vldr_vstr | copy_vldmia_vstmia | memcpy | write_strd |\n"
52 " write_stmia | write_vst1 | write_vstr | write_vstmia | memset |\n"
53 " read_ldrd | read_ldmia | read_vld1 | read_vldr | read_vldmia\n"
Christopher Ferris1348ce22013-04-11 17:50:18 -070054 " --num_threads NUM_THREADS_TO_RUN\n"
55 " malloc [fill]\n"
56 " madvise\n"
57 " resampler\n"
Christopher Ferris1348ce22013-04-11 17:50:18 -070058 " stack (stack smasher)\n"
59 " crawl\n"
60 , p);
The Android Open Source Projecte16cb842009-03-03 19:32:58 -080061}
62
Christopher Ferris1348ce22013-04-11 17:50:18 -070063int copy_bandwidth(int argc, char** argv);
64int write_bandwidth(int argc, char** argv);
Christopher Ferris1a3794a2013-05-02 15:12:11 -070065int read_bandwidth(int argc, char** argv);
Christopher Ferris1348ce22013-04-11 17:50:18 -070066int per_core_bandwidth(int argc, char** argv);
67int multithread_bandwidth(int argc, char** argv);
The Android Open Source Projecte16cb842009-03-03 19:32:58 -080068int malloc_test(int argc, char** argv);
69int madvise_test(int argc, char** argv);
The Android Open Source Projecte16cb842009-03-03 19:32:58 -080070int stack_smasher_test(int argc, char** argv);
71int crawl_test(int argc, char** argv);
Jason Sams3cadc3a2010-07-19 16:16:25 -070072int fp_test(int argc, char** argv);
The Android Open Source Projecte16cb842009-03-03 19:32:58 -080073
Christopher Ferris1348ce22013-04-11 17:50:18 -070074typedef struct {
75 const char *cmd_name;
76 int (*func)(int argc, char** argv);
77} function_t;
78
79function_t function_table[] = {
80 { "malloc", malloc_test },
81 { "madvise", madvise_test },
Christopher Ferris1348ce22013-04-11 17:50:18 -070082 { "stack", stack_smasher_test },
83 { "crawl", crawl_test },
84 { "fp", fp_test },
85 { "copy_bandwidth", copy_bandwidth },
Christopher Ferris1a3794a2013-05-02 15:12:11 -070086 { "write_bandwidth", write_bandwidth },
87 { "read_bandwidth", read_bandwidth },
Christopher Ferris1348ce22013-04-11 17:50:18 -070088 { "per_core_bandwidth", per_core_bandwidth },
89 { "multithread_bandwidth", multithread_bandwidth },
90};
The Android Open Source Projecte16cb842009-03-03 19:32:58 -080091
Christopher Ferris8f1da8f2015-11-05 12:53:10 -080092int main(int argc, char** argv) {
The Android Open Source Projecte16cb842009-03-03 19:32:58 -080093 if (argc == 1) {
94 usage(argv[0]);
95 return 0;
96 }
97 int err = -1;
Christopher Ferris1348ce22013-04-11 17:50:18 -070098 for (unsigned int i = 0; i < sizeof(function_table)/sizeof(function_t); i++) {
99 if (strcmp(argv[1], function_table[i].cmd_name) == 0) {
100 err = (*function_table[i].func)(argc-1, argv+1);
The Android Open Source Projecte16cb842009-03-03 19:32:58 -0800101 break;
102 }
103 }
Christopher Ferris1348ce22013-04-11 17:50:18 -0700104 if (err) {
105 usage(argv[0]);
The Android Open Source Projecte16cb842009-03-03 19:32:58 -0800106 }
Christopher Ferris1348ce22013-04-11 17:50:18 -0700107 return err;
The Android Open Source Projecte16cb842009-03-03 19:32:58 -0800108}
109
Christopher Ferris8f1da8f2015-11-05 12:53:10 -0800110int malloc_test(int argc, char** argv) {
The Android Open Source Projecte16cb842009-03-03 19:32:58 -0800111 bool fill = (argc>=2 && !strcmp(argv[1], "fill"));
112 size_t total = 0;
113 size_t size = 0x40000000;
114 while (size) {
115 void* addr = malloc(size);
116 if (addr == 0) {
Kenny Root7a91aed2010-09-15 15:32:01 -0700117 printf("size = %9zd failed\n", size);
The Android Open Source Projecte16cb842009-03-03 19:32:58 -0800118 size >>= 1;
119 } else {
120 total += size;
Kenny Root7a91aed2010-09-15 15:32:01 -0700121 printf("size = %9zd, addr = %p (total = %9zd (%zd MB))\n",
The Android Open Source Projecte16cb842009-03-03 19:32:58 -0800122 size, addr, total, total / (1024*1024));
123 if (fill) {
124 printf("filling...\n");
125 fflush(stdout);
126 memset(addr, 0, size);
127 }
Kenny Root7a91aed2010-09-15 15:32:01 -0700128 size = size + (size>>1);
The Android Open Source Projecte16cb842009-03-03 19:32:58 -0800129 }
130 }
Kenny Root7a91aed2010-09-15 15:32:01 -0700131 printf("done. allocated %zd MB\n", total / (1024*1024));
The Android Open Source Projecte16cb842009-03-03 19:32:58 -0800132 return 0;
133}
134
Christopher Ferris8f1da8f2015-11-05 12:53:10 -0800135int madvise_test(int, char**) {
The Android Open Source Projecte16cb842009-03-03 19:32:58 -0800136 for (int i=0 ; i<2 ; i++) {
137 size_t size = i==0 ? 4096 : 48*1024*1024; // 48 MB
Kenny Root7a91aed2010-09-15 15:32:01 -0700138 printf("Allocating %zd MB... ", size/(1024*1024)); fflush(stdout);
The Android Open Source Projecte16cb842009-03-03 19:32:58 -0800139 void* addr1 = mmap(0, size, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
140 printf("%p (%s)\n", addr1, addr1==(void*)-1 ? "failed" : "OK"); fflush(stdout);
141
142 printf("touching %p...\n", addr1); fflush(stdout);
143 memset(addr1, 0x55, size);
144
145 printf("advising DONTNEED...\n"); fflush(stdout);
146 madvise(addr1, size, MADV_DONTNEED);
147
148 printf("reading back %p...\n", addr1); fflush(stdout);
149 if (*(long*)addr1 == 0) {
150 printf("madvise freed some pages\n");
151 } else if (*(long*)addr1 == 0x55555555) {
152 printf("pages are still there\n");
153 } else {
154 printf("getting garbage back\n");
155 }
156
Kenny Root7a91aed2010-09-15 15:32:01 -0700157 printf("Allocating %zd MB... ", size/(1024*1024)); fflush(stdout);
The Android Open Source Projecte16cb842009-03-03 19:32:58 -0800158 void* addr2 = mmap(0, size, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
159 printf("%p (%s)\n", addr2, addr2==(void*)-1 ? "failed" : "OK"); fflush(stdout);
160
161 printf("touching %p...\n", addr2); fflush(stdout);
162 memset(addr2, 0xAA, size);
163
164 printf("unmap %p ...\n", addr2); fflush(stdout);
165 munmap(addr2, size);
166
167 printf("touching %p...\n", addr1); fflush(stdout);
168 memset(addr1, 0x55, size);
169
170 printf("unmap %p ...\n", addr1); fflush(stdout);
171 munmap(addr1, size);
172 }
173
174 printf("Done\n"); fflush(stdout);
175 return 0;
176}
177
Christopher Ferris8f1da8f2015-11-05 12:53:10 -0800178int stack_smasher_test(int, char**) {
The Android Open Source Projecte16cb842009-03-03 19:32:58 -0800179 int dummy = 0;
180 printf("corrupting our stack...\n");
181 *(volatile long long*)&dummy = 0;
182 return 0;
183}
184
185// --------------------------------------------------------------------
186
187extern "C" void thumb_function_1(int*p);
188extern "C" void thumb_function_2(int*p);
189extern "C" void arm_function_3(int*p);
190extern "C" void arm_function_2(int*p);
191extern "C" void arm_function_1(int*p);
192
Christopher Ferris8f1da8f2015-11-05 12:53:10 -0800193void arm_function_3(int*) {
The Android Open Source Projecte16cb842009-03-03 19:32:58 -0800194 int a = 0;
195 thumb_function_2(&a);
196}
197
Christopher Ferris8f1da8f2015-11-05 12:53:10 -0800198void arm_function_2(int*) {
The Android Open Source Projecte16cb842009-03-03 19:32:58 -0800199 int a = 0;
200 thumb_function_1(&a);
201}
202
Christopher Ferris8f1da8f2015-11-05 12:53:10 -0800203void arm_function_1(int*) {
The Android Open Source Projecte16cb842009-03-03 19:32:58 -0800204 int a = 0;
205 arm_function_2(&a);
206}
207
Christopher Ferris8f1da8f2015-11-05 12:53:10 -0800208int crawl_test(int, char**) {
The Android Open Source Projecte16cb842009-03-03 19:32:58 -0800209 int a = 0;
210 arm_function_1(&a);
211 return 0;
212}