File: memorySpeed.c

package info (click to toggle)
nws 2.11-3
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 2,700 kB
  • ctags: 2,820
  • sloc: ansic: 28,849; sh: 3,289; java: 1,205; makefile: 697; perl: 12
file content (246 lines) | stat: -rw-r--r-- 5,267 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
/* $Id: */

#include "config_nws.h"

#include <stdio.h>
#include <stdlib.h>
#ifdef HAVE_LIMITS_H
#	include <limits.h>
#endif

#include "diagnostic.h"
#include "osutil.h"
#include "skills.h"
#include "strutil.h"

/* let's be sure we have some resonable definition */
#ifndef INT_MAX
#	define INT_MAX	(2147483647)
#endif
#ifndef SHRT_MAX
#	define SHRT_MAX	(32767)
#endif
#ifndef LONG_MAX
#	define LONG_MAX	(2147483647L)
#endif
#define KB 1024

/*
 * returns the best size to use during memory access (in bytes)
 */
static int
howManyChar() {
	register int x, i, till;
	short best = 4;
	double before, after;
	void *mem;
	long bestTime = INT_MAX;
#define MY_LOOP_SIZE 512

	/* allocate MY_LOOP_SIZE KB */
	mem = MALLOC(KB*MY_LOOP_SIZE);
	if (mem == NULL) {
		printf("out of memory\n");
		exit(1);
	}
	
	memset(mem, '0', KB*MY_LOOP_SIZE);

	till = (MY_LOOP_SIZE*KB)/sizeof(short);
	before = MicroTime();
	for (x=0; x < 50; x++) {
		for (i=0; i < till; i++) {
			((short *)(mem))[i] = SHRT_MAX;
		}
	}
	after = MicroTime();
	if (bestTime > (after - before)) {
		bestTime = after - before;
		best = sizeof(short);
	}

	till = (MY_LOOP_SIZE*KB)/sizeof(int);
	before = MicroTime();
	for (x=0; x < 50; x++) {
		for (i=0; i < till; i++) {
			((int *)(mem))[i] = INT_MAX;
		}
	}
	after = MicroTime();
	if (bestTime > (after - before)) {
		bestTime = after - before;
		best = sizeof(int);
	}
	till = (MY_LOOP_SIZE*KB)/sizeof(long);
	before = MicroTime();
	for (x=0; x < 50; x++) {
		for (i=0; i < till; i++) {
			((long *)(mem))[i] = INT_MAX;
		}
	}
	after = MicroTime();
	if (bestTime > (after - before)) {
		bestTime = after - before;
		best = sizeof(long);
	}
	free(mem);
	DDEBUG1("howManyChar: best word size is %d\n", best);

	return best;
}

/*
 * time memory
 */
static double
timeMemory(	unsigned int size,
		unsigned int loops,
		unsigned int wordSize,
		unsigned int sequential) {
	register unsigned int i, till;	
	unsigned int z, *r, j;
	char *mem;			/* memory we are using */
	double before, after, avg;
	unsigned long randSeed;

	/* let's allocate the buffer */
	mem = MALLOC(size * KB);
	if (mem == NULL) {
		ERROR("timeMemory: out of memory\n");
		return -1;
	}

	/* checking the wordSize */
	if (wordSize != sizeof(short) && wordSize != sizeof(char) 
			&& wordSize != sizeof(int) && wordSize != sizeof(long)
	   ) {
		ERROR1("timeMemory: bogus word size (%d): using int\n", wordSize);
		wordSize = sizeof(int);
	}


	/* this is how many try we have to loop through the memory */
	till = (size * KB)/wordSize;
	r = MALLOC(sizeof(int) * till);
	if (r == NULL) {
		FREE(mem);
		ERROR("timeMemory: out of memory\n");
		return -1;
	}
	memset(r, -1, wordSize * till);

	randSeed = CurrentTime();

	for (avg=0, z=0; z < loops; z++) { 
		for (i=0; i < till; i++) {
			/* this is the sequential part */
			if (sequential) {
				r[i] = i;
			} else { 
				/* this is the random part */
				randSeed = randSeed * 1103515245 + 12345;
				j = (unsigned int)(randSeed) % till;
				while (r[j] != -1) {
					j = (j + 1) % till;
				}
				r[j] = i;
			}
		}


		/* let's try to clean the cache */
		before = MicroTime();
		memset(mem, 0, size * KB);
		after = MicroTime() - before;

		after = ((double)size*1000000)/(after*KB);
		DDEBUG1("timeMemory: memset got %.2f MB/s\n", after);

		/* read (r[i]) and write tests */
		if (wordSize == sizeof(short)) {
			before = MicroTime();
			for (i=0; i < till; i++) {
				((short *)(mem))[r[i]] = (short)i;
			}
			after = MicroTime();
		} else if (wordSize == sizeof(char)) {
			before = MicroTime();
			for (i=0; i < till; i++) {
				((char *)(mem))[r[i]] = (char)i;
			}
			after = MicroTime();
		} else if (wordSize == sizeof(int)) {
			before = MicroTime();
			for (i=0; i < till; i++) {
				((int *)(mem))[r[i]] = (int)i;
			}
			after = MicroTime();
		} else if (wordSize == sizeof(long)) {
			before = MicroTime();
			for (i=0; i < till; i++) {
				((long *)(mem))[r[i]] = (long)i;
			}
			after = MicroTime();
		}

		avg = avg + (after - before)/1000000;
	}
	free(mem);
	free(r);

	return ((2*loops*size)/(avg*KB));
}

int
MemorySpeedAvailable(	const char *opts) {
	/* always available */
	return 1;
}


void
MemorySpeedUseSkill(	const char *opts,
			int *length,
			SkillResult **results) {
	unsigned int size, loops, sequential;
	char name[64+1];
	double res; 
	const char *c;
	char *tmp, o[255 + 1];

	/* let's parse the options*/
	sequential = 0;
	tmp = GetOptionValue(opts, "mode", "random");
	if (strcmp(tmp, "sequential") == 0) {
		sequential = 1;
	}
	FREE(tmp);
	loops = 1;
	tmp = GetOptionValue(opts, "loops", "1");
	loops = strtol(tmp, NULL, 10);
	FREE(tmp);
	if (loops <= 0) {
		loops = 1;
	}

	tmp = GetOptionValue(opts, "size", "8192");
	for (c = tmp; GETTOK(name, c, ",", &c);) {
		size = strtol(name, NULL, 10);
		DDEBUG1("MemorySpeedUseSkill: using %d KB\n", size);

		res = timeMemory(size, loops, howManyChar(), sequential);

		/* let's get the right options */
		snprintf(o, sizeof(o), "mode:%s\tloops:%d\tsize:%d", (sequential ? "sequential" : "random"), loops, size);

		DDEBUG1("MemorySpeedUseSkill: timed memory at %.2f MB/s\n", res);

		if (res < 0) {
			AppendResult(memorySpeed, o, 0, 0.0, length, results);
		} else {
			AppendResult(memorySpeed, o, 1, res, length, results);
		}
	 }
	FREE(tmp);
}