Tags:
create new tag
view all tags
/* compile with
cc -Wall ccOpen.c cycle_counter.c -o ccOpen
try also adding some optimization
cc -Wall -O0 ccOpen.c cycle_counter.c -o ccOpen
cc -Wall -O1 ccOpen.c cycle_counter.c -o ccOpen
...
until -O3 (maybe the realcc doesn't work for some
optimization level).
*/

#include "cycle_counter.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <errno.h>
#include <unistd.h>
#include <stdlib.h>

/* resources not allocated yet */
int fd = -1;
long *time_open = NULL, *time_close = NULL;

/*  use		cat /proc/cpuinfo			to determine the MHz */

#define MHz 1210.804260

void myexit(int ret);
CPU_clock_num guess_realcc_overhead(void);

int main(int argc,char *argv[])
{
	char string[256];
	int nprove,i,cc;
	CPU_clock_num before,after,cc_oh;

	if (argc != 3) {
		printf("Uso: %s <numero di prove> <nome del file>\n",argv[0]);
		myexit(5);
	}

	if (sscanf(argv[1],"%d",&nprove) != 1 || nprove <= 0) {
		printf("%s non va bene come <numero di prove>\n",argv[1]);
		myexit(5);
	}

	if ( (time_open = malloc( nprove * sizeof(long) )) == NULL ) {
		sprintf(string,"Errore allocando %d bytes per time_open",nprove * sizeof(long));
		perror(string); myexit(5);
	}

	if ( (time_close = malloc( nprove * sizeof(long) )) == NULL ) {
		sprintf(string,"Errore allocando %d bytes per time_close",nprove * sizeof(long));
		perror(string); myexit(5);
	}

	cc_oh = guess_realcc_overhead();
	printf("1sec cicli clock %6ld  == %12.6f microsecs\n",(long)(after-before),((long)(after-before))/MHz);
	
	for ( i = 0 ; i < nprove ; i++) {

		before = realcc();
		fd = open(argv[2], O_WRONLY | O_CREAT | O_TRUNC , 00644);
		after  = realcc();
		time_open[i] = after-before-cc_oh;

		if ( fd == -1 ) {
			sprintf(string,"Apertura di %1.200s fallita",argv[2]);
			perror(string); myexit(5);
		}
		
		before = realcc();
		cc = close(fd);
		after  = realcc();
		time_close[i] = after-before-cc_oh;
		
		if ( cc == -1 ) {
			sprintf(string,"Chiusura di %1.200s fallita",argv[2]);
			perror(string); myexit(5);
		}
	}

	for ( i = 0 ; i < nprove ; i++) {
		printf("%4d cicli clock open %6ld  == %12.6f microsecs		 ",i,time_open[i],time_open[i]/MHz);
		printf("cicli clock close %6ld  == %12.6f microsecs\n",time_close[i],time_close[i]/MHz);
	}

	myexit(0);
}

void myexit(int ret)
{
/* free the (eventually) allocated resources */
	if (time_open  !=  0) free(time_open);
	if (time_close !=  0) free(time_close);
	exit(ret);
}

CPU_clock_num guess_realcc_overhead(void) {

	register int i;
	CPU_clock_num before,after,delta,sum;
	int cnt;
	
	sum = cnt = 0L;
	
	/* cache clock code */
	before = realcc();
	after  = realcc();
	delta  = after-before;
	
	for (i = 0 ; i < 20 ; ++i ) {
		before = realcc();
		after = realcc();
		delta = after-before;
		if (delta < 100) {
			sum += delta;
			cnt++;
		}
	}
 	if (!cnt) /* horrible special case */
		return 20L;
	else
		return sum/cnt;
}

-- AntonioValletta - 23 Nov 2001
Topic revision: r1 - 2001-11-23 - AntonioValletta






 
Questo sito usa cookies, usandolo ne accettate la presenza. (CookiePolicy)
Torna al Dipartimento di Informatica
This site is powered by the TWiki collaboration platform Powered by PerlCopyright © 2008-2021 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback