1 /***********************************************************************
2 *                                                                      *
3 *               This software is part of the ast package               *
4 *          Copyright (c) 1999-2013 AT&T Intellectual Property          *
5 *                      and is licensed under the                       *
6 *                 Eclipse Public License, Version 1.0                  *
7 *                    by AT&T Intellectual Property                     *
8 *                                                                      *
9 *                A copy of the License is available at                 *
10 *          http://www.eclipse.org/org/documents/epl-v10.html           *
11 *         (with md5 checksum b35adb5213ca9657e911e9befb180842)         *
12 *                                                                      *
13 *              Information and Software Systems Research               *
14 *                            AT&T Research                             *
15 *                           Florham Park NJ                            *
16 *                                                                      *
17 *               Glenn Fowler <glenn.s.fowler@gmail.com>                *
18 *                                                                      *
19 ***********************************************************************/
20 #include	"vmtest.h"
21 
22 #include	<pthread.h>
23 
24 /* This test was from the Hoard package. It tests cache locality. */
25 
26 #define		OBJSIZE		10
27 #define		REPETITIONS	10
28 #define		ITERATIONS	10000
29 
30 typedef struct _worker_s
31 {
32 	char*	object;
33 	int	objsize;
34 	int	repetitions;
35 	int	iterations;
36 } Worker_t;
37 
worker(void * arg)38 static void* worker(void* arg)
39 {
40 	int		i, j, k;
41 	Worker_t	*w = (Worker_t*)arg;
42 
43 	if(w->object)
44 		free(w->object);
45 
46 	for(i = 0; i < w->iterations; ++i)
47 	{	char	*obj;
48 
49 		if(!(obj = (char*)malloc(w->objsize)) )
50 			terror("malloc failed");
51 
52 		/* write into obj a bunch of times */
53 		for(j = 0; j < w->repetitions; ++j)
54 		{	for(k = 0; k < w->objsize; ++k)
55 			{	volatile char	ch;
56 				obj[k] = (char)k;
57 				ch = obj[k];
58 				ch += 1;
59 			}
60 		}
61 
62 		free(obj);
63 	}
64 
65 	free(w);
66 
67 	return (void*)0;
68 }
69 
tmain()70 tmain()
71 {
72 	int		i, rv;
73 	void		*status;
74 	char		*objs[N_THREAD];
75 	pthread_t	thread[N_THREAD];
76 	int		nthreads = N_THREAD;
77 	int		iterations = ITERATIONS;
78 	int		objsize = OBJSIZE;
79 	int		repetitions = REPETITIONS;
80 #ifdef VMALLOC
81 	Vmstat_t	vmst;
82 #endif
83 
84 	tresource(-1, 0);
85 
86 	while(argc > 1)
87 	{	if(argv[1][0] == '-' && argv[1][1] == 't')
88 			nthreads = atoi(argv[1]+2);
89 		else if(argv[1][0] == '-' && argv[1][1] == 'i')
90 			iterations = atoi(argv[1]+2);
91 		else if(argv[1][0] == '-' && argv[1][1] == 'r')
92 			repetitions = atoi(argv[1]+2);
93 		else if(argv[1][0] == '-' && argv[1][1] == 'z')
94 			objsize = atoi(argv[1]+2);
95 		argc--; argv++;
96 	}
97 
98 	if(nthreads <= 0 || nthreads > N_THREAD)
99 		terror("nthreads=%d must be in 1..%d", nthreads, N_THREAD);
100 	if(repetitions < nthreads)
101 		repetitions = 0;
102 
103 	tinfo("nthreads=%d iterations=%d objsize=%d repetitions=%d",
104 		nthreads, iterations, objsize, repetitions );
105 
106 	for(i = 0; i < nthreads; ++i)
107 		if(!(objs[i] = (char*)malloc(objsize)) )
108 			terror("Can't allocate objs[%d]", i);
109 
110 	for(i = 0; i < nthreads; ++i)
111 	{	Worker_t	*w = (Worker_t*)malloc(sizeof(Worker_t));
112 		w->object = objs[i];
113 		w->objsize = objsize;
114 		w->iterations = iterations;
115 		w->repetitions = repetitions/nthreads;
116 		if((rv = pthread_create(&thread[i], NULL, worker, (void*)w)) != 0)
117 			terror("Failed to create thread %d", i);
118 	}
119 	for(i = 0; i < nthreads; ++i)
120 		if((rv = pthread_join(thread[i], &status)) != 0)
121 			terror("Failed waiting for thread %d", i);
122 
123 	tresource(0, 0);
124 
125 #ifdef VMALLOC
126 	vmstat(0, &vmst);
127 	twarn(vmst.mesg);
128 #endif
129 
130 	texit(0);
131 }
132