blob: 6e8940140bfed38b4de75e624a9f06104b2d3fbe [file] [log] [blame]
njnf131b3a2005-12-14 05:33:35 +00001// This artificial program runs a lot of code. The exact amount depends on
philippe8e1bee42013-10-18 00:08:20 +00002// the command line -- if an arg "0" is given, it does exactly
njnf131b3a2005-12-14 05:33:35 +00003// the same amount of work, but using four times as much code.
philippe8e1bee42013-10-18 00:08:20 +00004// If an arg >= 1 is given, the amount of code is multiplied by this arg.
njnf131b3a2005-12-14 05:33:35 +00005//
6// It's a stress test for Valgrind's translation speed; natively the two
7// modes run in about the same time (the I-cache effects aren't big enough
8// to make a difference), but under Valgrind the one running more code is
9// significantly slower due to the extra translation time.
10
11#include <stdio.h>
12#include <string.h>
philippe8e1bee42013-10-18 00:08:20 +000013#include <stdlib.h>
sewardj0d3a1a82005-12-15 16:11:25 +000014#include <assert.h>
petarj213b0d32013-09-15 22:16:38 +000015#if defined(__mips__)
16#include <asm/cachectl.h>
17#include <sys/syscall.h>
18#endif
njn83b62cb2009-04-15 03:12:43 +000019#include "tests/sys_mman.h"
njnf131b3a2005-12-14 05:33:35 +000020
21#define FN_SIZE 996 // Must be big enough to hold the compiled f()
22#define N_LOOPS 20000 // Should be divisible by four
23#define RATIO 4 // Ratio of code sizes between the two modes
24
25int f(int x, int y)
26{
27 int i;
28 for (i = 0; i < 5000; i++) {
29 switch (x % 8) {
30 case 1: y += 3;
31 case 2: y += x;
32 case 3: y *= 2;
33 default: y--;
34 }
35 }
36 return y;
37}
38
njnf131b3a2005-12-14 05:33:35 +000039int main(int argc, char* argv[])
40{
41 int h, i, sum1 = 0, sum2 = 0, sum3 = 0, sum4 = 0;
42 int n_fns, n_reps;
43
njnf131b3a2005-12-14 05:33:35 +000044 if (argc <= 1) {
45 // Mode 1: not so much code
46 n_fns = N_LOOPS / RATIO;
47 n_reps = RATIO;
48 printf("mode 1: ");
49 } else {
50 // Mode 2: lots of code
philippe8e1bee42013-10-18 00:08:20 +000051 const int mul = atoi(argv[1]);
52 if (mul == 0)
53 n_fns = N_LOOPS;
54 else
55 n_fns = N_LOOPS * mul;
njnf131b3a2005-12-14 05:33:35 +000056 n_reps = 1;
57 printf("mode 1: ");
58 }
59 printf("%d copies of f(), %d reps\n", n_fns, n_reps);
60
philippe8e1bee42013-10-18 00:08:20 +000061 char* a = mmap(0, FN_SIZE * n_fns,
62 PROT_EXEC|PROT_WRITE,
63 MAP_PRIVATE|MAP_ANONYMOUS, -1,0);
64 assert(a != (char*)MAP_FAILED);
65
njnf131b3a2005-12-14 05:33:35 +000066 // Make a whole lot of copies of f(). FN_SIZE is much bigger than f()
67 // will ever be (we hope).
68 for (i = 0; i < n_fns; i++) {
69 memcpy(&a[FN_SIZE*i], f, FN_SIZE);
70 }
petarj213b0d32013-09-15 22:16:38 +000071
72#if defined(__mips__)
73 syscall(__NR_cacheflush, a, FN_SIZE * n_fns, ICACHE);
74#endif
75
njnf131b3a2005-12-14 05:33:35 +000076 for (h = 0; h < n_reps; h += 1) {
77 for (i = 0; i < n_fns; i += 4) {
78 int(*f1)(int,int) = (void*)&a[FN_SIZE*(i+0)];
79 int(*f2)(int,int) = (void*)&a[FN_SIZE*(i+1)];
80 int(*f3)(int,int) = (void*)&a[FN_SIZE*(i+2)];
81 int(*f4)(int,int) = (void*)&a[FN_SIZE*(i+3)];
82 sum1 += f1(i+0, n_fns-i+0);
83 sum2 += f2(i+1, n_fns-i+1);
84 sum3 += f3(i+2, n_fns-i+2);
85 sum4 += f4(i+3, n_fns-i+3);
86 if (i % 1000 == 0)
87 printf(".");
88 }
89 }
90 printf("result = %d\n", sum1 + sum2 + sum3 + sum4);
91 return 0;
92}