lat_cache.c

来自「pebble」· C语言 代码 · 共 95 行

C
95
字号
/* 
 * Copyright 1999, 2000, 2001, 2002 Lucent Technologies Inc.
 * All Rights Reserved.
 * Information Sciences Research Center, Bell Labs.
 *
 * LUCENT TECHNOLOGIES DOES NOT CLAIM MERCHANTABILITY OF THIS SOFTWARE 
 * OR THE SUITABILITY OF THIS SOFTWARE FOR ANY PARTICULAR PURPOSE. The
 * software is provided "as is" without expressed or implied warranty 
 * of any kind.
 *
 * These notices must be retained in any copies of any part of this
 * software.
 *
 */

#include "pebble.h"
#include "unistd.h"
#include "types.h"
#include "time.h"
#include "synch.h"
#include "perfcount.h"

#define MEM_START       0
#define MEM_SIZE        4*1024*1024
#define SMALLEST_SIZE   8*1024
#define CACHE_LINE      32

int
main(void)
{

	volatile char *p, *end;
	char *area;
	volatile int *q;
	int i, ntimes;
	int size, sum;
	int perf_ix;
	uint perf_count;
	uvlong start_time, elapsed;

	printf("lat_cache benchmark starting\n");

	if ((int)(area = (char *)sbrk(MEM_SIZE)) <= 0)
		panic("sbrk failed:");

	/* a quick memory test */
	end = area + MEM_SIZE;
	for (q = (int *)area; q < (int *)end; q++)
		*q = (int)q;

	for (q = (int *)area; q < (int *)end; q++)
		if (*q != (int)q) {
			printf("memory test failed for address %08x = %08x\n",
				(uint)q, *q);
			task_exit(1);
		}

	for (size = SMALLEST_SIZE; size <= MEM_SIZE; size <<= 1) {
		printf("=============\n");
		printf("testing memory scan of %d bytes area\n", size);

		ntimes = MEM_SIZE / size;
		sum = 0;

		/* initial scan to fill the cache */
		end = area + size;
		for (p = area; p < end; p += CACHE_LINE)
		        sum += *p;

		for (perf_ix = 0; perf_vec[perf_ix].name != NULL; perf_ix++) {

			set_perf_ctrl(perf_vec[perf_ix].ctrl);
			start_time = hrtime();

			for (i = 0; i < ntimes; i++) {
				for (p = area; p < end; p += CACHE_LINE)
					sum += *p;
			}

			perf_count = get_perf_count();
			elapsed = 2 * (hrtime() - start_time);

			printf("%s: %d elapsed time: %d cycles (%d cycles/access) sum=%d\n",
				perf_vec[perf_ix].name, perf_count,
				(int)elapsed,
				(int)(elapsed / (MEM_SIZE/CACHE_LINE)),
				sum);
		}
	}

	printf("LAT_CACHE ENDED\n");
	task_exit(0);
	return(1);
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?