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 #include	<pthread.h>
22 
23 /* This test was from the Hoard package. It tests cache-locality.  */
24 
25 #define		OBJSIZE		10
26 #define		REPETITION	10
27 #define		ITERATION	10000
28 
29 #ifdef VMALLOC
30 #define		malloc(x)	vmalloc(Vmregion, (x))
31 #define		free(x)		vmfree(Vmregion, (x))
32 #define		realloc(x,z)	vmresize(Vmregion, (x), (z), VM_RSCOPY)
33 #endif
34 
35 typedef struct _worker_s
36 {
37 	int	objsize;
38 	int	repetition;
39 	int	iteration;
40 	int	filler[16];
41 } Worker_t;
42 
worker(void * arg)43 static void* worker(void* arg)
44 {
45 	int		i, j, k;
46 	Worker_t	*w = (Worker_t*)arg;
47 
48 	for(i = 0; i < w->iteration; ++i)
49 	{	char	*obj;
50 
51 		if(!(obj = (char*)malloc(w->objsize)) )
52 			terror("malloc failed");
53 
54 		/* write into obj a bunch of times */
55 		for(j = 0; j < w->repetition; ++j)
56 		{	for(k = 0; k < w->objsize; ++k)
57 			{	volatile char	ch;
58 				obj[k] = 'a';
59 				ch = obj[k];
60 				ch += 1;
61 			}
62 		}
63 
64 		free(obj);
65 	}
66 
67 	free(w);
68 
69 	return (void*)0;
70 }
71 
tmain()72 tmain()
73 {
74 	int		i, rv;
75 	void		*status;
76 	pthread_t	thread[N_THREAD];
77 	int		nthreads = N_THREAD;
78 	int		iteration = ITERATION;
79 	int		objsize = OBJSIZE;
80 	int		repetition = REPETITION;
81 
82 	for(; argc > 1; --argc, ++argv)
83 	{	if(argv[1][0] != '-')
84 			continue;
85 		else if(argv[1][1] == 't')
86 			nthreads = atoi(argv[1]+2);
87 		else if(argv[1][1] == 'z')
88 			objsize = atoi(argv[1]+2);
89 		else if(argv[1][1] == 'i')
90 			iteration = atoi(argv[1]+2);
91 		else if(argv[1][1] == 'r')
92 			repetition = atoi(argv[1]+2);
93 	}
94 
95 	if(nthreads <= 0 || nthreads > N_THREAD)
96 		terror("nthreads=%d must be in 1..%d", nthreads, N_THREAD);
97 	if(repetition < nthreads)
98 		repetition = 0;
99 
100 	tinfo("nthreads=%d iteration=%d objsize=%d repetition=%d",
101 		nthreads, iteration, objsize, repetition );
102 
103 	tresource(-1, 0);
104 
105 	for(i = 0; i < nthreads; ++i)
106 	{	Worker_t	*w = (Worker_t*)malloc(sizeof(Worker_t));
107 		w->objsize = objsize;
108 		w->iteration = iteration;
109 		w->repetition = repetition/nthreads;
110 		if((rv = pthread_create(&thread[i], NULL, worker, (void*)w)) != 0)
111 			terror("Failed to create thread %d", i);
112 	}
113 	for(i = 0; i < nthreads; ++i)
114 		if((rv = pthread_join(thread[i], &status)) != 0)
115 			terror("Failed waiting for thread %d", i);
116 
117 	tresource(0, 0);
118 
119 #ifdef VMALLOC
120 {	Vmstat_t vmst;
121 	vmstat(0, &vmst);
122 	tinfo(vmst.mesg);
123 }
124 #endif
125 
126 	texit(0);
127 }
128