Exemplo n.º 1
0
void parent(int sock) {

	ssize_t size;
	int fd,i;
	struct perf_event_attr pe;

	memset(&pe,0,sizeof(struct perf_event_attr));
	pe.type=PERF_TYPE_HARDWARE;
	pe.size=sizeof(struct perf_event_attr);
	pe.config=PERF_COUNT_HW_INSTRUCTIONS;
	pe.disabled=1;
	pe.exclude_kernel=1;
	pe.exclude_hv=1;

	arch_adjust_domain(&pe,quiet);

	fd=perf_event_open(&pe,0,-1,-1,0);
	if (fd<0) {
		fprintf(stderr,"Error opening leader %llx %s\n",pe.config,strerror(errno));
		test_fail(test_string);
	}

	size = sock_fd_write(sock, "1", 1, fd);
	if (!quiet) printf ("wrote fd %d (size %d)\n", fd, (int)size);

	sleep(1);

	for(i=0;i<20;i++) instructions_million();

}
Exemplo n.º 2
0
int main(int argc, char **argv) {

	struct perf_event_attr pe;
	int fd;
	int quiet=0;

	int kernel_size;

	quiet=test_quiet();

	if (!quiet) {
		printf("This test checks what happens if attr struct is "
		      "exactly what the kernel expects.\n");
	}

	kernel_size=sizeof(struct perf_event_attr);

	memset(&pe,0,kernel_size);
	pe.size=kernel_size;
	pe.type=PERF_TYPE_HARDWARE;
	pe.config=PERF_COUNT_HW_INSTRUCTIONS;
	pe.disabled=1;
	pe.exclude_kernel=1;
	pe.exclude_hv=1;

	arch_adjust_domain(&pe,quiet);

	fd=perf_event_open(&pe,0,-1,-1,0);
	if (fd<0) {
		fprintf(stderr,"Error opening leader %llx %s\n",
			pe.config,strerror(errno));
		test_fail(test_string);
	}

	close(fd);

	test_pass(test_string);

	return 0;
}
int main(int argc, char** argv) {

	int fd1,fd2;
	struct perf_event_attr pe1,pe2;

	int result;
	long long single=0,both=0;

	char test_string[]="Testing PERF_EVENT_IOC_SET_OUTPUT ioctl...";
	char *our_mmap;

	quiet=test_quiet();

	if (!quiet) {
		printf("Testing PERF_EVENT_IOC_SET_OUTPUT ioctl.\n");
	}

	/**********************************/
	/* test 1, creating group, normal */
	/**********************************/

	if (!quiet) {
		printf("1. Testing normal group leader\n");
	}

	/* Create group leader */

	memset(&pe1,0,sizeof(struct perf_event_attr));
	pe1.type=PERF_TYPE_HARDWARE;
	pe1.size=sizeof(struct perf_event_attr);
	pe1.config=PERF_COUNT_HW_INSTRUCTIONS;
	pe1.disabled=1;
	pe1.exclude_kernel=1;
	pe1.exclude_hv=1;
	pe1.sample_period=50000;
	arch_adjust_domain(&pe1,quiet);

	/* Create group leader */
	fd1=perf_event_open(&pe1,0,-1,-1,0);
	if (fd1<0) {
		if (!quiet) {
			fprintf(stderr,"Unexpected error %s\n",strerror(errno));

		}
		test_fail(test_string);
	}

	our_mmap=mmap(NULL, 8192,
		PROT_READ|PROT_WRITE, MAP_SHARED, fd1, 0);
	if (our_mmap==MAP_FAILED) {
		fprintf(stderr,"mmap() failed %s!\n",strerror(errno));
		test_fail(test_string);
	}


	/* Create group member */

	memset(&pe2,0,sizeof(struct perf_event_attr));
	pe2.type=PERF_TYPE_HARDWARE;
	pe2.size=sizeof(struct perf_event_attr);
	pe2.config=PERF_COUNT_HW_INSTRUCTIONS;
	pe2.disabled=0;
	pe2.exclude_kernel=1;
	pe2.exclude_hv=1;
	arch_adjust_domain(&pe2,quiet);

	fd2=perf_event_open(&pe2,0,-1,fd1,0);
	if (fd2<0) {
		if (!quiet) {
			fprintf(stderr,"Unexpected error %s\n",strerror(errno));

		}
		test_fail(test_string);
	}

	/* start */
	ioctl(fd1, PERF_EVENT_IOC_RESET, 0);
	ioctl(fd2, PERF_EVENT_IOC_RESET, 0);
	ioctl(fd1, PERF_EVENT_IOC_ENABLE,0);

	/* million */
	result=instructions_million();

	/* stop */
	ioctl(fd1, PERF_EVENT_IOC_DISABLE,0);

	if (result==CODE_UNIMPLEMENTED) {
		fprintf(stderr,"\tCode unimplemented\n");
		test_skip(test_string);
	}

	/* read mmap */

	single=read_mmap_size(our_mmap);

	/* close */
	munmap(our_mmap,8192);
	close(fd1);
	close(fd2);

	/************************************************/
	/* test 2, reading group, PERF_FLAG_FD_NO_GROUP */
	/************************************************/

	if (!quiet) {
		printf("2. Testing with PERF_EVENT_IOC_SET_OUTPUT\n");
	}

	/* Create group leader */

	memset(&pe1,0,sizeof(struct perf_event_attr));
	pe1.type=PERF_TYPE_HARDWARE;
	pe1.size=sizeof(struct perf_event_attr);
	pe1.config=PERF_COUNT_HW_INSTRUCTIONS;
	pe1.disabled=1;
	pe1.exclude_kernel=1;
	pe1.exclude_hv=1;
	pe1.sample_period=50000;
	arch_adjust_domain(&pe1,quiet);

	fd1=perf_event_open(&pe1,0,-1,-1,0);
	if (fd1<0) {
		if (!quiet) {
			fprintf(stderr,"Unexpected error %s\n",strerror(errno));

		}
		test_fail(test_string);
	}


	/* Create group member */

	memset(&pe2,0,sizeof(struct perf_event_attr));
	pe2.type=PERF_TYPE_HARDWARE;
	pe2.size=sizeof(struct perf_event_attr);
	pe2.config=PERF_COUNT_HW_INSTRUCTIONS;
	pe2.disabled=0;
	pe2.exclude_kernel=1;
	pe2.exclude_hv=1;
	pe2.sample_period=50000;
	arch_adjust_domain(&pe2,quiet);

	fd2=perf_event_open(&pe2,0,-1,fd1,0);
	if (fd2<0) {
		if (!quiet) {
			fprintf(stderr,"Unexpected error on perf_event_open() %s\n",strerror(errno));

		}
		test_fail(test_string);
	}


	our_mmap=mmap(NULL, 8192,
		PROT_READ|PROT_WRITE, MAP_SHARED, fd1, 0);
	if (our_mmap==MAP_FAILED) {
		fprintf(stderr,"mmap() failed %s!\n",strerror(errno));
		test_fail(test_string);
	}

	ioctl(fd2, PERF_EVENT_IOC_SET_OUTPUT, fd1);

	/* start */
	ioctl(fd1, PERF_EVENT_IOC_RESET, 0);
	ioctl(fd2, PERF_EVENT_IOC_RESET, 0);
	ioctl(fd1, PERF_EVENT_IOC_ENABLE,0);

	/* million */
	result=instructions_million();

	/* stop */
	ioctl(fd1, PERF_EVENT_IOC_DISABLE,0);

	if (result==CODE_UNIMPLEMENTED) {
		fprintf(stderr,"\tCode unimplemented\n");
		test_skip(test_string);
	}

	/* read */

	both=read_mmap_size(our_mmap);

	/* close */

	munmap(our_mmap,8192);
	close(fd1);
	close(fd2);

	if (both<=single) {

		if (!quiet) {
			fprintf(stderr,"Expected to get more samples when both together\n");
		}

		test_fail(test_string);
	}

	test_pass(test_string);

	return 0;
}
Exemplo n.º 4
0
int main(int argc, char **argv) {

	int ret;
	int fd;
	int mmap_pages=1+MMAP_DATA_SIZE;

	struct perf_event_attr pe;

	struct sigaction sa;
	char test_string[]="Testing Intel Branch Trace Store...";

	quiet=test_quiet();

	if (!quiet) printf("This tests the intel BTS driver.\n");

	memset(&sa, 0, sizeof(struct sigaction));
        sa.sa_sigaction = our_handler;
        sa.sa_flags = SA_SIGINFO;

        if (sigaction( SIGIO, &sa, NULL) < 0) {
                fprintf(stderr,"Error setting up signal handler\n");
                exit(1);
        }


	/* Find the BTS driver */
	FILE *fff;
	int bts_type=0;

	fff=fopen("/sys/devices/intel_bts/type","r");
	if (fff==NULL) {
		if (!quiet) {
			fprintf(stderr,"Intel BTS driver not found\n");
		}
		test_skip(test_string);
	}

	fscanf(fff,"%d",&bts_type);
	fclose(fff);

	if (!quiet) {
		fprintf(stdout,"Found Intel BTS as type %d\n",bts_type);
	}

        /* Set up BTS Event */
	memset(&pe,0,sizeof(struct perf_event_attr));
	pe.size=sizeof(struct perf_event_attr);
	pe.type=bts_type;
	pe.disabled=1;
//	pe.exclude_kernel=1;
//	pe.exclude_user=1;

	/* pe.config not necessary? */

//	sample_type=PERF_SAMPLE_IP|PERF_SAMPLE_WEIGHT|PERF_SAMPLE_ADDR;
//	read_format=0;
//	pe.sample_period=SAMPLE_FREQUENCY;
//	pe.sample_type=sample_type;
//	pe.read_format=read_format;
//	pe.pinned=1;
//	pe.exclude_hv=1;
//	pe.wakeup_events=1;
//	pe.precise_ip=1;

	arch_adjust_domain(&pe,quiet);

	fd=perf_event_open(&pe,0,-1,-1,0);
	if (fd<0) {
		if (!quiet) {
			fprintf(stderr,"Problem opening leader %s\n",
				strerror(errno));
			test_fail(test_string);
		}
	}

	/* Setup normal mmap page */

	our_mmap=mmap(NULL, mmap_pages*4096,
		PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);

	struct perf_event_mmap_page *mmap_control;

	mmap_control = (struct perf_event_mmap_page *)our_mmap;

	mmap_control->aux_offset=mmap_pages*4096;
	mmap_control->aux_size=AUX_DATA_SIZE*4096;

	/* Setup aux mmap page */

	our_aux=mmap(NULL, mmap_control->aux_size,
		PROT_READ|PROT_WRITE, MAP_SHARED, fd, mmap_control->aux_offset);


	fcntl(fd, F_SETFL, O_RDWR|O_NONBLOCK|O_ASYNC);
	fcntl(fd, F_SETSIG, SIGIO);
	fcntl(fd, F_SETOWN,getpid());

	ioctl(fd, PERF_EVENT_IOC_RESET, 0);

	ret=ioctl(fd, PERF_EVENT_IOC_ENABLE,0);

	if (ret<0) {
		if (!quiet) {
			fprintf(stderr,"Error with PERF_EVENT_IOC_ENABLE "
				"of group leader: %d %s\n",
				errno,strerror(errno));
			exit(1);
		}
	}

	naive_matrix_multiply(quiet);

	ret=ioctl(fd, PERF_EVENT_IOC_DISABLE,0);

	printf("After, aux_head=%llx aux_tail=%llx\n",
		mmap_control->aux_head,mmap_control->aux_tail);

	/* Attempt to parse */

	memcpy(bts_buffer,our_aux,mmap_control->aux_head);

	int i;
	for(i=0;i< (mmap_control->aux_head/sizeof(struct bts_record));i++) {
		printf("From: %llx To: %llx Predicted: %d\n",
			bts_buffer[i].branch_from,
			bts_buffer[i].branch_to,
			!!(bts_buffer[i].predicted&0x10));
	}
	printf("\n");

	/* Clean up */

	munmap(our_aux,AUX_DATA_SIZE*4096);

	munmap(our_mmap,mmap_pages*4096);

	close(fd);

	test_pass(test_string);

	return 0;
}
int main(int argc, char** argv) {

	int ret;
	int mmap_pages=1+MMAP_DATA_SIZE;

	struct perf_event_attr pe;

	struct sigaction sa;
	char test_string[]="Testing record sampling...";

	quiet=test_quiet();

	if (!quiet) printf("This tests the record sampling interface.\n");

	memset(&sa, 0, sizeof(struct sigaction));
	sa.sa_sigaction = our_handler;
	sa.sa_flags = SA_SIGINFO;

	if (sigaction( SIGIO, &sa, NULL) < 0) {
		fprintf(stderr,"Error setting up signal handler\n");
		exit(1);
	}

	/* Set up Instruction Event */

	memset(&pe,0,sizeof(struct perf_event_attr));

 	pe.type=PERF_TYPE_HARDWARE;
	pe.size=sizeof(struct perf_event_attr);
	pe.config=PERF_COUNT_HW_INSTRUCTIONS;
	pe.sample_period=SAMPLE_FREQUENCY;
	pe.sample_type=sample_type;

	pe.read_format=read_format;
	pe.disabled=1;
	pe.pinned=1;
	pe.exclude_kernel=1;
	pe.exclude_hv=1;
	pe.wakeup_events=1;

        pe.branch_sample_type=PERF_SAMPLE_BRANCH_ANY;

	arch_adjust_domain(&pe,quiet);


        fd1=perf_event_open(&pe,0,-1,-1,0);
	if (fd1<0) {
		if (!quiet) {
			fprintf(stderr,"Problem opening leader %s\n",
			strerror(errno));
			fprintf(stderr,"Trying without branches\n");
		}
		sample_type&=~PERF_SAMPLE_BRANCH_STACK;
		pe.sample_type=sample_type;
		fd1=perf_event_open(&pe,0,-1,-1,0);
		if (fd1<0) {
			if (!quiet) {
				fprintf(stderr,"Error opening leader %s\n",
					strerror(errno));
			}
			test_fail(test_string);
		}
	}

	/* Open Cycles Event */

	memset(&pe,0,sizeof(struct perf_event_attr));

	pe.type=PERF_TYPE_HARDWARE;
	pe.size=sizeof(struct perf_event_attr);
	pe.config=PERF_COUNT_HW_CPU_CYCLES;
	pe.sample_type=PERF_SAMPLE_IP;
	pe.read_format=PERF_FORMAT_GROUP|PERF_FORMAT_ID;
	pe.disabled=0;
	pe.exclude_kernel=1;
	pe.exclude_hv=1;

	arch_adjust_domain(&pe,quiet);

	fd2=perf_event_open(&pe,0,-1,fd1,0);
	if (fd2<0) {
		if (!quiet) fprintf(stderr,"Error opening %llx\n",pe.config);
		test_fail(test_string);
	}

	our_mmap=mmap(NULL, mmap_pages*4096,
			PROT_READ|PROT_WRITE, MAP_SHARED, fd1, 0);

	fcntl(fd1, F_SETFL, O_RDWR|O_NONBLOCK|O_ASYNC);
	fcntl(fd1, F_SETSIG, SIGIO);
	fcntl(fd1, F_SETOWN,getpid());

	ioctl(fd1, PERF_EVENT_IOC_RESET, 0);

	ret=ioctl(fd1, PERF_EVENT_IOC_ENABLE,0);

	if (ret<0) {
		if (!quiet) {
			fprintf(stderr,"Error with PERF_EVENT_IOC_ENABLE "
					"of group leader: %d %s\n",
					errno,strerror(errno));
			exit(1);
 		}
	}

 	instructions_million();

	ret=ioctl(fd1, PERF_EVENT_IOC_REFRESH,0);

	if (!quiet) {
		printf("Counts, using mmap buffer %p\n",our_mmap);
		printf("\tPOLL_IN : %d\n",count.in);
		printf("\tPOLL_OUT: %d\n",count.out);
		printf("\tPOLL_MSG: %d\n",count.msg);
		printf("\tPOLL_ERR: %d\n",count.err);
		printf("\tPOLL_PRI: %d\n",count.pri);
		printf("\tPOLL_HUP: %d\n",count.hup);
		printf("\tUNKNOWN : %d\n",count.unknown);
	}

	if (count.total==0) {
		if (!quiet) printf("No overflow events generated.\n");
		test_fail(test_string);
	}

	munmap(our_mmap,mmap_pages*4096);

	close(fd2);
	close(fd1);

	test_pass(test_string);

	return 0;
}
Exemplo n.º 6
0
int main(int argc, char** argv) {

	int fd1,i;
	struct perf_event_attr pe1;
	int errors=0;

	int result;

	char test_string[]="Testing PERF_EVENT_IOC_SET_FILTER ioctl...";

	quiet=test_quiet();

	if (!quiet) {
		printf("Testing PERF_EVENT_IOC_SET_FILTER ioctl.\n");
	}

	/****************************************************/
	/* Check if /sys/kernel/debug/tracing/events exists */
	/****************************************************/
//	result=access("/sys/kernel/debug/tracing/events",F_OK);

	/* Actually this is pointless, as it gives EACCESS */
	/* as a normal user even if the file exists */

	/************************************/
	/* Creating a tracepoint event      */
	/************************************/

	if (!quiet) {
		printf("Creating a tracepoint event\n");
	}

	memset(&pe1,0,sizeof(struct perf_event_attr));
	pe1.type=PERF_TYPE_TRACEPOINT;
	pe1.size=sizeof(struct perf_event_attr);

	/* Find a trace event that will let us add a particular filter */

	/* It should work with */
	/* writeback:writeback_start*/
	/* but we can't get the id of this directly without debugfs/tracefs */
	/* mounted (the id numbers change depending on machine/kernel)      */

	/* Valid filter */
	strcpy(filter,"nr_pages==2");

	if (!quiet) {
		printf("Trying to find an event that will allow filter %s\n",
			filter);
	}

	/* Usually there are fewer than 1000 trace events? */
	for(i=0;i<MAX_SEARCH;i++) {

		pe1.config=i;
		pe1.disabled=1;
		pe1.exclude_kernel=0;
		pe1.exclude_hv=0;
		arch_adjust_domain(&pe1,quiet);

		fd1=perf_event_open(&pe1,0,-1,-1,0);
		if (fd1<0) {
			if (!quiet) {
				// fprintf(stderr,"Failed on %d\n",i);
			}
			continue;
		}

		result=ioctl(fd1, PERF_EVENT_IOC_SET_FILTER, filter);
		if (result==0) {
			if (!quiet) printf("Found proper event %d\n",i);
			close(fd1);
			break;
		}
		else {

		}
		close(fd1);
	}

	if (i==MAX_SEARCH) {
		if (!quiet) {
			printf("Could not find any trace event to filter\n");
		}
		test_skip(test_string);
		errors++;
	}

	pe1.config=i;
	pe1.disabled=1;
	pe1.exclude_kernel=0;
	pe1.exclude_hv=0;
	arch_adjust_domain(&pe1,quiet);

	/* Create group leader */
	fd1=perf_event_open(&pe1,0,-1,-1,0);
	if (fd1<0) {
		if (!quiet) {
			fprintf(stderr,"Unexpected error %s\n",strerror(errno));
		}
		test_fail(test_string);
	}

	for(i=0;i<MAX_FILTER;i++) {
		filter[i]=0xff;
	}

	/* Check a too big value */
	if (!quiet) {
		printf("\t+ Checking a too-big event: ");
	}
	result=ioctl(fd1, PERF_EVENT_IOC_SET_FILTER, filter);
	if ((result==-1) && (errno==EINVAL)) {
		if (!quiet) printf("Failed as expected\n");
	}
	else {
		if (!quiet) printf("Unexpected %d %s\n",result,strerror(errno));
		errors++;
	}

	/* Check off-by-one value */
	/* Size limited to pagesize */
	if (!quiet) {
		printf("\t+ Checking off-by-one filter: ");
	}
	filter[4096]=0;
	result=ioctl(fd1, PERF_EVENT_IOC_SET_FILTER, filter);
	if ((result==-1) && (errno==EINVAL)) {
		if (!quiet) printf("Failed as expected\n");
	}
	else {
		if (!quiet) printf("Unexpected %d %s\n",result,strerror(errno));
		errors++;
	}

	/* Check a just-right value */
	if (!quiet) {
		printf("\t+ Checking max size invalid filter: ");
	}
	filter[4095]=0;
	result=ioctl(fd1, PERF_EVENT_IOC_SET_FILTER, filter);
	if ((result==-1) && (errno==EINVAL)) {
		if (!quiet) printf("Failed as expected\n");
	}
	else {
		if (!quiet) printf("Unexpected %d %s\n",result,strerror(errno));
		errors++;
	}

	/* Check an empty value */
	if (!quiet) {
		printf("\t+ Checking empty filter: ");
	}
	filter[0]=0;
	result=ioctl(fd1, PERF_EVENT_IOC_SET_FILTER, filter);
	if ((result==-1) && (errno==EINVAL)) {
		if (!quiet) printf("Failed as expected\n");
	}
	else {
		if (!quiet) printf("Unexpected %d %s\n",result,strerror(errno));
		errors++;
	}

	/* Clear a filter */
	if (!quiet) {
		printf("\t+ Clear filter (write 0): ");
	}
	filter[0]='0';
	filter[1]=0;
	result=ioctl(fd1, PERF_EVENT_IOC_SET_FILTER, filter);
	if ((result==-1) && (errno==EINVAL)) {
		if (!quiet) printf("Failed as expected\n");
	}
	else {
		if (!quiet) printf("Unexpected %d %s\n",result,strerror(errno));
		errors++;
	}




	/* tracefs usually under /sys/kernel/tracing */



	/* start */
	ioctl(fd1, PERF_EVENT_IOC_RESET, 0);
	ioctl(fd1, PERF_EVENT_IOC_ENABLE,0);

	/* million */
	result=instructions_million();

	/* stop */
	ioctl(fd1, PERF_EVENT_IOC_DISABLE,0);

	close(fd1);

	if (errors) {
		test_fail(test_string);
	}

	test_pass(test_string);

	return 0;
}
Exemplo n.º 7
0
int main(int argc, char **argv) {

	int ret;
	int fd;
	int mmap_pages=1+MMAP_DATA_SIZE;

	struct perf_event_attr pe;

	struct sigaction sa;
	char test_string[]="Testing pebs latency...";

	quiet=test_quiet();

	if (!quiet) printf("This tests the intel PEBS latency.\n");

	        memset(&sa, 0, sizeof(struct sigaction));
        sa.sa_sigaction = our_handler;
        sa.sa_flags = SA_SIGINFO;

        if (sigaction( SIGIO, &sa, NULL) < 0) {
                fprintf(stderr,"Error setting up signal handler\n");
                exit(1);
        }

        /* Set up Instruction Event */

        memset(&pe,0,sizeof(struct perf_event_attr));

	sample_type=PERF_SAMPLE_IP|PERF_SAMPLE_WEIGHT;
	read_format=0;

        pe.type=PERF_TYPE_HARDWARE;
        pe.size=sizeof(struct perf_event_attr);
        pe.config=PERF_COUNT_HW_INSTRUCTIONS;
        pe.sample_period=SAMPLE_FREQUENCY;
        pe.sample_type=sample_type;

        pe.read_format=read_format;
        pe.disabled=1;
        pe.pinned=1;
        pe.exclude_kernel=1;
        pe.exclude_hv=1;
        pe.wakeup_events=1;

	arch_adjust_domain(&pe,quiet);

	fd=perf_event_open(&pe,0,-1,-1,0);
	if (fd<0) {
		if (!quiet) {
			fprintf(stderr,"Problem opening leader %s\n",
				strerror(errno));
			test_fail(test_string);
		}
	}
	our_mmap=mmap(NULL, mmap_pages*4096,
		PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);

	fcntl(fd, F_SETFL, O_RDWR|O_NONBLOCK|O_ASYNC);
	fcntl(fd, F_SETSIG, SIGIO);
	fcntl(fd, F_SETOWN,getpid());

	ioctl(fd, PERF_EVENT_IOC_RESET, 0);

	ret=ioctl(fd, PERF_EVENT_IOC_ENABLE,0);

	if (ret<0) {
		if (!quiet) {
			fprintf(stderr,"Error with PERF_EVENT_IOC_ENABLE "
				"of group leader: %d %s\n",
				errno,strerror(errno));
			exit(1);
		}
	}

	instructions_million();

	ret=ioctl(fd, PERF_EVENT_IOC_REFRESH,0);

	if (!quiet) {
                printf("Counts %d, using mmap buffer %p\n",count_total,our_mmap);
        }

	if (count_total==0) {
		if (!quiet) printf("No overflow events generated.\n");
		test_fail(test_string);
	}
	munmap(our_mmap,mmap_pages*4096);

	close(fd);

	test_pass(test_string);

	return 0;
}
int main(int argc, char** argv) {

	int ret,quiet,i,matches=0;

	struct perf_event_attr pe;

	struct sigaction sa;
	void *our_mmap;
	char test_string[]="Testing simultaneous one-shot group overflow...";

	/* Initialize overflow counts */
	for(i=0;i<NUM_EVENTS;i++) {
		events[i].fd=-1;
		events[i].overflows=0;
	}

	quiet=test_quiet();

	/*********************************************************************/
	if (!quiet) {
		printf("This tests simultaneous overflow within "
			"group in one-shot mode.\n");
	}
	/*********************************************************************/

	/*****************************/
	/* set up our signal handler */
	/*****************************/

	memset(&sa, 0, sizeof(struct sigaction));
	sa.sa_sigaction = our_handler;
	sa.sa_flags = SA_SIGINFO;

	if (sigaction( SIGRTMIN+2, &sa, NULL) < 0) {
		fprintf(stderr,"Error setting up signal handler\n");
		test_fail(test_string);
	}

	/***********************/
	/* get expected counts */
	/***********************/

	if (!quiet) {
		printf("*** Testing running one at a time\n");
	}

	for(i=0;i<NUM_EVENTS;i++) {
		memset(&pe,0,sizeof(struct perf_event_attr));

		pe.type=event_values[i].type;
		pe.size=sizeof(struct perf_event_attr);
		pe.config=event_values[i].config;
		pe.sample_period=event_values[i].period;
		pe.sample_type=0;
		pe.read_format=PERF_FORMAT_GROUP;
		pe.disabled=1;
		pe.pinned=0;
		pe.exclude_kernel=1;
		pe.exclude_hv=1;
		pe.wakeup_events=1;

		arch_adjust_domain(&pe,quiet);

		events[i].fd=perf_event_open(&pe,0,-1,-1,0);
		if (events[i].fd<0) {
			fprintf(stderr,"Error opening leader %llx\n",pe.config);
			test_fail(test_string);
		}

		/* on older kernels you need this even if you don't use it */
		our_mmap=mmap(NULL, (1+MMAP_PAGES)*getpagesize(),
			PROT_READ|PROT_WRITE, MAP_SHARED, events[i].fd, 0);

		fcntl(events[i].fd, F_SETFL, O_RDWR|O_NONBLOCK|O_ASYNC);
		fcntl(events[i].fd, F_SETSIG, SIGRTMIN+2);
		fcntl(events[i].fd, F_SETOWN,getpid());

		ioctl(events[i].fd, PERF_EVENT_IOC_RESET, 0);

		if (!quiet) {
			//printf("Testing matrix matrix multiply\n");
			printf("\tEvent %s with period %d\n",
				event_values[i].name,
				event_values[i].period);
		}

		ret=ioctl(events[i].fd, PERF_EVENT_IOC_REFRESH,1);

		if (!quiet) printf("\t\t");
		naive_matrix_multiply(quiet);

		ret=ioctl(events[i].fd, PERF_EVENT_IOC_DISABLE,0);

		if (!quiet) {
			printf("\t\tfd %d overflows: %d (%s/%d)\n",
				events[i].fd,events[i].overflows,
				event_values[i].name,
				event_values[i].period);
		}

		if (events[i].overflows==0) {
			if (!quiet) printf("No overflow events generated.\n");
			test_fail(test_string);
		}

		close(events[i].fd);

		events[i].individual_overflow=events[i].overflows;

		events[i].overflows=0;
		events[i].fd=-1;
	}

	/************************************************/
	/* test when both events enabled in group	*/
	/************************************************/

	if (!quiet) {
		printf("*** Testing with multiple events at once\n");
		for(i=0;i<NUM_EVENTS;i++) {
			printf("\tEvent %s with period %d\n",
				event_values[i].name,
				event_values[i].period);
		}
	}

	for(i=0;i<NUM_EVENTS;i++) {

		memset(&pe,0,sizeof(struct perf_event_attr));

		pe.type=event_values[i].type;
		pe.size=sizeof(struct perf_event_attr);
		pe.config=event_values[i].config;
		pe.sample_period=event_values[i].period;
		pe.sample_type=0;
		pe.read_format=PERF_FORMAT_GROUP;
		pe.disabled=(i==0)?1:0;
		/* Note: pinning the group leader is what */
		/* caused the bug in 4.11-rc1 */
		pe.pinned=(i==0)?1:0;
		pe.exclude_kernel=1;
		pe.exclude_hv=1;
		pe.wakeup_events=1;

		arch_adjust_domain(&pe,quiet);

		events[i].fd=perf_event_open(&pe,0,-1,(i==0)?-1:events[0].fd,0);
		if (events[i].fd<0) {
			fprintf(stderr,"Error opening leader %llx\n",pe.config);
			test_fail(test_string);
		}

		/* on older kernels you need this even if you don't use it */
		our_mmap=mmap(NULL, (1+MMAP_PAGES)*getpagesize(),
			PROT_READ|PROT_WRITE, MAP_SHARED, events[i].fd, 0);

		fcntl(events[i].fd, F_SETFL, O_RDWR|O_NONBLOCK|O_ASYNC);
		fcntl(events[i].fd, F_SETSIG, SIGRTMIN+2);
		fcntl(events[i].fd, F_SETOWN,getpid());

		ioctl(events[i].fd, PERF_EVENT_IOC_RESET, 0);
	}

	ret=ioctl(events[0].fd, PERF_EVENT_IOC_REFRESH,1);

	if (ret<0) {
		if (!quiet) {
			fprintf(stderr,"Error with PERF_EVENT_IOC_REFRESH "
					"of group leader: %d %s\n",
					errno,strerror(errno));
		}
		test_fail(test_string);
	}

	if (!quiet) printf("\t\t");
	naive_matrix_multiply(quiet);

	ret=ioctl(events[0].fd, PERF_EVENT_IOC_DISABLE,0);

	for(i=0;i<NUM_EVENTS;i++) {
		if (!quiet) {
			printf("\t\tfd %d overflows: %d (%s/%d)\n",
				events[i].fd,events[i].overflows,
				event_values[i].name,event_values[i].period);
		}
	}

	int result;
	long long values[1+1*NUM_EVENTS],counts[NUM_EVENTS];

	/* Read the results, complain if too few bytes read */
	result=read(events[0].fd,&values,8*(1+1*NUM_EVENTS));
	if (result!=8*(1+1*NUM_EVENTS)) {
		if (!quiet) printf("error reading\n");
		test_fail(test_string);
	}

	/* Complain if the number of events field does not match expected */
	if (values[0]!=NUM_EVENTS) {
		if (!quiet) printf("error reading\n");
		test_fail(test_string);
	}

	if (!quiet) printf("\tEnding counts:\n");
	for(i=0;i<NUM_EVENTS;i++) {
		counts[i]=values[i+1];
		if (!quiet) printf("\t\tCount %d: %lld\n",i,counts[i]);

	}

	/* Complain if no overflows at all */
	for(i=0;i<NUM_EVENTS;i++) {
		if (events[i].overflows==0) {
			if (!quiet) printf("No overflow events generated.\n");
			test_fail(test_string);
		}
	}

	for(i=0;i<NUM_EVENTS;i++) {
		close(events[i].fd);
	}

	/* test validity */
	for(i=0;i<NUM_EVENTS;i++) {

		if (!quiet) {
			printf("Event %s/%d Expected %lld Got %d\n",
				event_values[i].name, event_values[i].period,
				counts[i]/event_values[i].period,
				events[i].overflows);
		}

		if (counts[i]/event_values[i].period!=events[i].overflows) {
			/* Problem! */
		}
		else {
			matches++;
		}
	}

	/* Counts will be slightly different because they will count */
	/* while signal handler running.                             */

	double error;

	error=(double)counts[0]-(double)counts[1];
	error/=(double)counts[0];
	error*=100.0;

	if ((error > 1.0) ||  (error < -1.0)) {
		if (!quiet) {
			fprintf(stderr,"Counts should be roughly the same "
				"but found %lf%% error\n",error);
		}
		test_fail(test_string);
	}

	if (matches!=NUM_EVENTS) {
		if (!quiet) fprintf(stderr,"Unexpected event count!\n");
		test_fail(test_string);
	}

	test_pass(test_string);

	(void)our_mmap;

	return 0;
}
Exemplo n.º 9
0
int main(int argc, char** argv) {
   
   int ret,quiet,i;

   struct perf_event_attr pe;

   char test_string[]="Testing if reset clears multiplex fields...";

   quiet=test_quiet();
   
   if (!quiet) {
      printf("The PERF_EVENT_IOC_RESET ioctl() clears the event count.\n");
      printf("  Check to see if it clears other fields too, such as\n");
      printf("  time_enabled and time_running.  Traditionall it does not.\n");
   }

   /* Setup 10 counters */

   for(i=0;i<NUM_EVENTS;i++) {

      memset(&pe,0,sizeof(struct perf_event_attr));
      pe.type=PERF_TYPE_HARDWARE;
      pe.size=sizeof(struct perf_event_attr);
      pe.config=events[i];
      pe.disabled=1;
      pe.exclude_kernel=1;
      pe.exclude_hv=1;
      pe.read_format=PERF_FORMAT_TOTAL_TIME_ENABLED | 
	             PERF_FORMAT_TOTAL_TIME_RUNNING;

      arch_adjust_domain(&pe,quiet);

      fd[i]=perf_event_open(&pe,0,-1,-1,0);
      if (fd[i]<0) {
	 fprintf(stderr,"Failed adding mpx event %d %s\n",i,strerror(errno));
         test_fail(test_string);
	 return -1;
      }
   }


   /**************************/
   /* Start all the counters */
   /**************************/

   for(i=0;i<NUM_EVENTS;i++) {
      ret=ioctl(fd[i], PERF_EVENT_IOC_ENABLE,0);
      if (ret<0) {
	 fprintf(stderr,"Error starting event %d\n",i);
      }
   }

   test_routine();

   /*************************/
   /* Stop all the counters */
   /*************************/

   for(i=0;i<NUM_EVENTS;i++) {
      ret=ioctl(fd[i], PERF_EVENT_IOC_DISABLE,0);
      if (ret<0) {
	 fprintf(stderr,"Error stopping event %d\n",i);
      }
   }

   if (!quiet) printf("Initial results\n");

   /*********************/
   /* Read the counters */
   /*********************/

   for(i=0;i<NUM_EVENTS;i++) {
      ret=read(fd[i],&base_results[i],5*sizeof(long long));
      if (ret<3*sizeof(long long)) {
	fprintf(stderr,"Event %d unexpected read size %d\n",i,ret);
        test_fail(test_string);
      }
      if (!quiet) {
         printf("\t%d %lld %lld %lld\n",i,
	     base_results[i][0],base_results[i][1],base_results[i][2]);
      }
   }

   /**********************/
   /* Reset the counters */
   /**********************/

   for(i=0;i<NUM_EVENTS;i++) {
      ret=ioctl(fd[i], PERF_EVENT_IOC_RESET,0);
      if (ret<0) {
	 fprintf(stderr,"Error resetting event %d\n",i);
      }
   }

   /************************/
   /* Re-read the counters */
   /************************/
     

   if (!quiet) printf("After reset:\n");

   for(i=0;i<NUM_EVENTS;i++) {
      ret=read(fd[i],&mpx_results[i],5*sizeof(long long));
      if (ret<3*sizeof(long long)) {
	fprintf(stderr,"Event %d unexpected read size %d\n",i,ret);
        test_fail(test_string);
      }
      if (!quiet) {
         printf("\t%d %lld %lld %lld\n",i,
	     mpx_results[i][0],mpx_results[i][1],mpx_results[i][2]);
      }
      if (mpx_results[i][0]!=0) {
	 fprintf(stderr,"Event %d not reset to 0\n",i);
         test_fail(test_string);
      }

      if (mpx_results[i][1]==0) {
	 fprintf(stderr,"Enabled %d should not be 0\n",i);
         test_fail(test_string);
      }

      if (mpx_results[i][2]==0) {
	 fprintf(stderr,"Running %d should not be  0\n",i);
         test_fail(test_string);
      }

   }

   test_pass(test_string);
      
   return 0;
}
Exemplo n.º 10
0
int main(int argc, char** argv) {

	int fd,bpf_fd;
	struct perf_event_attr pe1;
	int errors=0;

	unsigned long long text_begin,symbol;

	union bpf_attr battr;

	int result;

	char test_string[]="Testing PERF_EVENT_IOC_SET_BPF ioctl...";

	quiet=test_quiet();

	if (!quiet) {
		printf("Testing PERF_EVENT_IOC_SET_BPF ioctl.\n");
	}

	/*******************************************/
	/* Creating a kprobe tracepoint event      */
	/*******************************************/

	if (!quiet) {
		printf("Creating a kprobe tracepoint event\n");
	}

	FILE *fff;
	int kprobe_id;

	char filename[BUFSIZ];
	char tracefs_location[BUFSIZ];
	char *find_result;

	find_result=find_tracefs_location(tracefs_location,quiet);

	if (find_result==NULL) {

		if (!quiet) {
			fprintf(stderr,"Error finding tracefs location!\n");
		}

		test_skip(test_string);
	}

	sprintf(filename,"%s/kprobe_events",tracefs_location);

	fff=fopen(filename, "w");
	if (fff==NULL) {
		printf("Cannot open %s!\n",filename);
		printf("You may want to: mount -t tracefs nodev /sys/kernel/tracing\n");
		test_fail(test_string);
	}

	text_begin=lookup_symbol("_text");
	symbol=lookup_symbol("handle_mm_fault");

	if ((text_begin==0) || (symbol==0)) {
		fprintf(stderr,"Error finding symbol _text, handle_mm_fault\n");
		test_fail(test_string);
	}

	/*  perf probe -a VMW=handle_mm_fault */
	fprintf(fff,"p:probe/VMW _text+%lld",symbol-text_begin);
	fclose(fff);

	sprintf(filename,"%s/events/probe/VMW/id",tracefs_location);

	fff=fopen(filename, "r");
	if (fff==NULL) {
		printf("Cannot open %s!\n",filename);
		test_fail(test_string);
	}

	fscanf(fff,"%d",&kprobe_id);
	fclose(fff);


	memset(&pe1,0,sizeof(struct perf_event_attr));
	pe1.type=PERF_TYPE_TRACEPOINT;
	pe1.size=sizeof(struct perf_event_attr);

	pe1.config=kprobe_id;

	pe1.disabled=1;
	pe1.exclude_kernel=0;
	pe1.exclude_hv=0;
	arch_adjust_domain(&pe1,quiet);

	/* Create group leader */
	fd=perf_event_open(&pe1,0,-1,-1,0);
	if (fd<0) {
		if (!quiet) {
			fprintf(stderr,"Unexpected error %s\n",strerror(errno));
		}
		printf("Cannot open kprobe id %d\n",kprobe_id);
		test_fail(test_string);
	}

	struct bpf_insn instructions[] = {
		BPF_MOV64_IMM(BPF_REG_0, 0),		/* r0 = 0 */
		BPF_EXIT_INSN(),			/* return r0 */
	};

	unsigned char license[]="GPL";

#define LOG_BUF_SIZE 65536
	static char bpf_log_buf[LOG_BUF_SIZE];

	/* Kernel will EINVAL if unused bits aren't zero */
	memset(&battr,0,sizeof(battr));

	/* Version has to match currently running kernel */

	struct utsname version;
	int major, minor, subminor, version_code;

	uname(&version);

	printf("We are running release %s\n",version.release);


	sscanf(version.release,"%d.%d.%d",&major,&minor,&subminor);

	version_code = (major<<16) | (minor<<8) | subminor;
	printf("Using LINUX_VERSION_CODE: %d\n",
		version_code);

	battr.prog_type = BPF_PROG_TYPE_KPROBE;
//	battr.insn_cnt = sizeof(instructions);
	battr.insn_cnt= sizeof(instructions) / sizeof(struct bpf_insn);
	battr.insns = (uint64_t) (unsigned long) instructions;
	battr.license = (uint64_t) (unsigned long) license;
	battr.log_buf = (uint64_t) (unsigned long) bpf_log_buf;
	battr.log_size = LOG_BUF_SIZE;
	battr.log_level = 1;
	battr.kern_version = version_code;

	bpf_log_buf[0] = 0;

	bpf_fd = sys_bpf(BPF_PROG_LOAD, &battr, sizeof(battr));

	if (bpf_fd < 0) {
		printf("bpf: load: failed to load program, %s\n"
			"-- BEGIN DUMP LOG ---\n%s\n-- END LOG --\n",
			strerror(errno), bpf_log_buf);
			return bpf_fd;
	}


	result=ioctl(fd, PERF_EVENT_IOC_SET_BPF, bpf_fd);
	if (result<0) {
		if (!quiet) printf("Unexpected %d %s\n",result,strerror(errno));
		errors++;
	}

	/* start */
	ioctl(fd, PERF_EVENT_IOC_ENABLE,0);

	/* million */
	result=instructions_million();

	/* stop */
	ioctl(fd, PERF_EVENT_IOC_DISABLE,0);

	close(fd);

	fff=fopen(filename,"a");
	if (fff==NULL) {
		fprintf(stderr,"Couldn't open %s for closing\n",filename);
		test_fail(test_string);
	}
	fprintf(fff,"-:probe/VMW\n");
	fclose(fff);

	if (errors) {
		test_fail(test_string);
	}

	test_pass(test_string);

	return 0;
}
Exemplo n.º 11
0
int main(int argc, char **argv) {

	int ret;
	int fd;
	int mmap_pages=1+MMAP_DATA_SIZE;

	struct perf_event_attr pe;

	struct sigaction sa;
	char test_string[]="Testing pebs...";

	quiet=test_quiet();

	if (!quiet) printf("This tests the intel PEBS interface.\n");

	        memset(&sa, 0, sizeof(struct sigaction));
        sa.sa_sigaction = our_handler;
        sa.sa_flags = SA_SIGINFO;

        if (sigaction( SIGIO, &sa, NULL) < 0) {
                fprintf(stderr,"Error setting up signal handler\n");
                exit(1);
        }

        /* Set up Instruction Event */

        memset(&pe,0,sizeof(struct perf_event_attr));

        pe.type=PERF_TYPE_HARDWARE;
        pe.size=sizeof(struct perf_event_attr);
        pe.config=PERF_COUNT_HW_INSTRUCTIONS;
        pe.sample_period=SAMPLE_FREQUENCY;
//        pe.sample_type=PERF_SAMPLE_IP | PERF_SAMPLE_REGS_USER;
        pe.sample_type=PERF_SAMPLE_IP | PERF_SAMPLE_REGS_INTR;

	global_sample_type=pe.sample_type;

#if defined(__i386__) || defined (__x86_64__)

	/* Bitfield saying which registers we want */
	pe.sample_regs_intr=(1ULL<<PERF_REG_X86_64_MAX)-1;
//	pe.sample_regs_user=(1ULL<<PERF_REG_X86_IP);
	/* DS, ES, FS, and GS not valid on x86_64 */
	/* see  perf_reg_validate() in arch/x86/kernel/perf_regs.c */
	pe.sample_regs_intr&=~(1ULL<<PERF_REG_X86_DS);
	pe.sample_regs_intr&=~(1ULL<<PERF_REG_X86_ES);
	pe.sample_regs_intr&=~(1ULL<<PERF_REG_X86_FS);
	pe.sample_regs_intr&=~(1ULL<<PERF_REG_X86_GS);


	printf("%llx %d\n",pe.sample_regs_user,PERF_REG_X86_DS);

#else
	pe.sample_regs_intr=1;
#endif

	global_sample_regs_user=pe.sample_regs_intr;

        pe.read_format=0;
        pe.disabled=1;
        pe.pinned=1;
        pe.exclude_kernel=1;
        pe.exclude_hv=1;
        pe.wakeup_events=1;

	arch_adjust_domain(&pe,quiet);

	fd=perf_event_open(&pe,0,-1,-1,0);
	if (fd<0) {
		if (!quiet) {
			if (errno==EINVAL) {
				fprintf(stderr,"Problem opening leader "
					"probably need to run a newer kernel: %s\n",
					strerror(errno));
			}
			else {
				fprintf(stderr,"Problem opening leader %s\n",
					strerror(errno));
			}
			test_fail(test_string);
		}
	}
	our_mmap=mmap(NULL, mmap_pages*4096,
		PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);

	fcntl(fd, F_SETFL, O_RDWR|O_NONBLOCK|O_ASYNC);
	fcntl(fd, F_SETSIG, SIGIO);
	fcntl(fd, F_SETOWN,getpid());

	ioctl(fd, PERF_EVENT_IOC_RESET, 0);

	ret=ioctl(fd, PERF_EVENT_IOC_ENABLE,0);

	if (ret<0) {
		if (!quiet) {
			fprintf(stderr,"Error with PERF_EVENT_IOC_ENABLE "
				"of group leader: %d %s\n",
				errno,strerror(errno));
			exit(1);
		}
	}

	instructions_million();

	ret=ioctl(fd, PERF_EVENT_IOC_REFRESH,0);

	if (!quiet) {
                printf("Counts %d, using mmap buffer %p\n",count_total,our_mmap);
        }

	if (count_total==0) {
		if (!quiet) printf("No overflow events generated.\n");
		test_fail(test_string);
	}
	munmap(our_mmap,mmap_pages*4096);

	close(fd);

	test_pass(test_string);

	return 0;
}
Exemplo n.º 12
0
int main(int argc, char** argv) {

	int fd,bpf_fd,test_fd;
	struct perf_event_attr pe1;
	int errors=0;
	int i;

	union bpf_attr battr;

	int result;

	char test_string[]="Testing eBPF load...";

	quiet=test_quiet();

	if (!quiet) {
		printf("Testing eBPF load.\n");
	}

	/*******************************************/
	/* Creating a kprobe tracepoint event      */
	/*******************************************/

	if (!quiet) {
		printf("Creating a kprobe tracepoint event\n");
	}

	FILE *fff;
	int kprobe_id;

	char filename[BUFSIZ];
	char tracefs_location[BUFSIZ];
	char *find_result;

	find_result=find_tracefs_location(tracefs_location,quiet);

	if (find_result==NULL) {

		if (!quiet) {
			fprintf(stderr,"Error finding tracefs location!\n");
			fprintf(stderr,"Try sudo mount -t tracefs tracefs /sys/kernel/tracing/\n");
		}

		test_skip(test_string);
	}

	sprintf(filename,"%s/kprobe_events",tracefs_location);

	fff=fopen(filename, "w+");
	if (fff==NULL) {
		printf("Cannot open %s!\n",filename);
		test_fail(test_string);
	}

	/*  where do these values come from?  */
	fprintf(fff,"p:perf_bpf_probe/func_write _text+2159216");
	fclose(fff);


	sprintf(filename,"%s/events/perf_bpf_probe/func_write/id",tracefs_location);

        fff=fopen(filename, "r");
        if (fff==NULL) {
                printf("Cannot open %s!\n",filename);
                test_fail(test_string);
        }

        fscanf(fff,"%d",&kprobe_id);
        fclose(fff);


	/* Setup BPF */


	struct bpf_insn instructions[] = {
		BPF_MOV64_IMM(BPF_REG_0, 0),		/* r0 = 0 */
		BPF_EXIT_INSN(),			/* return r0 */
	};

	char bpf_prog[BPF_PROG_SIZE];
	int bpf_prog_fd,bpf_prog_size;

	bpf_prog_fd=open("test_bpf_output.o",O_RDONLY);
	if (bpf_prog_fd<0) {
		fprintf(stderr,"Error opening bpf_output.o");
	}
	bpf_prog_size=read(bpf_prog_fd,bpf_prog,BPF_PROG_SIZE);
	close(bpf_prog_fd);



	unsigned char license[]="GPL";


	static char bpf_log_buf[LOG_BUF_SIZE];

	/* Kernel will EINVAL if unused bits aren't zero */
	memset(&battr,0,sizeof(battr));

	/* Version has to match currently running kernel */

	struct utsname version;
	int major, minor, subminor, version_code;

	uname(&version);

	printf("We are running release %s\n",version.release);


	sscanf(version.release,"%d.%d.%d",&major,&minor,&subminor);

	version_code = (major<<16) | (minor<<8) | subminor;
	printf("Using LINUX_VERSION_CODE: %d\n",
		version_code);

	battr.prog_type = BPF_PROG_TYPE_KPROBE;
#if 0
//	battr.insn_cnt = sizeof(instructions);
	battr.insn_cnt= sizeof(instructions) / sizeof(struct bpf_insn);
	battr.insns = (uint64_t) (unsigned long) instructions;
#endif
	battr.insn_cnt= bpf_prog_size / sizeof(struct bpf_insn);
	battr.insns = (uint64_t) (unsigned long) bpf_prog;

	battr.license = (uint64_t) (unsigned long) license;
	battr.log_buf = (uint64_t) (unsigned long) bpf_log_buf;
	battr.log_size = LOG_BUF_SIZE;
	battr.log_level = 1;
	battr.kern_version = version_code;

	bpf_log_buf[0] = 0;

	bpf_fd = sys_bpf(BPF_PROG_LOAD, &battr, sizeof(battr));

	if (bpf_fd < 0) {
		printf("bpf: load: failed to load program, %s\n"
			"-- BEGIN DUMP LOG ---\n%s\n-- END LOG --\n",
			strerror(errno), bpf_log_buf);
			return bpf_fd;
	}

	memset(&pe1,0,sizeof(struct perf_event_attr));
	pe1.type=PERF_TYPE_TRACEPOINT;
	pe1.size=sizeof(struct perf_event_attr);

	pe1.config=kprobe_id;

	pe1.disabled=1;
	pe1.exclude_kernel=0;
	pe1.exclude_hv=0;
	arch_adjust_domain(&pe1,quiet);

	/* Create group leader */
	fd=perf_event_open(&pe1,0,-1,-1,0);
	if (fd<0) {
		if (!quiet) {
			fprintf(stderr,"Unexpected error %s\n",strerror(errno));
		}
		printf("Cannot open kprobe id %d\n",kprobe_id);
		test_fail(test_string);
	}

	result=ioctl(fd, PERF_EVENT_IOC_SET_BPF, bpf_fd);
	if (result<0) {
		if (!quiet) printf("Unexpected %d %s\n",result,strerror(errno));
		errors++;
	}

	test_fd=open("/dev/null",O_WRONLY);
	if (test_fd<0) {
		fprintf(stderr,"Error opening /dev/null\n");
	}

	/* start */
	ioctl(fd, PERF_EVENT_IOC_ENABLE,0);

	for(i=0;i<10;i++) {
		write(test_fd,"Test!\n",7);
	}

	/* stop */
	ioctl(fd, PERF_EVENT_IOC_DISABLE,0);

	close(test_fd);

	long long presult[32];

	read(fd,presult,8);

	close(fd);

	if (errors) {
		test_fail(test_string);
	}

	test_pass(test_string);

	return 0;
}
Exemplo n.º 13
0
int main(int argc, char **argv) {

	struct perf_event_attr pe;
	int i, sum=0, read_result, passes=0;
	long long count;

	void *address;

	address=test_function;

	quiet=test_quiet();

	sprintf(test_string,"Testing hardware breakpoints (%d)...",
					42);


	if (!quiet) {
		printf("This test checks that hardware breakpoints work.\n");
	}

	/*******************************/
	/* Test execution breakpoint   */
	/*******************************/

	if (!quiet) {
		printf("\tTesting HW_BREAKPOINT_X\n");
	}

	memset(&pe,0,sizeof(struct perf_event_attr));
	pe.type=PERF_TYPE_BREAKPOINT;
	pe.size=sizeof(struct perf_event_attr);

	/* setup for an execution breakpoint */
	pe.config=0;
	pe.bp_type=HW_BREAKPOINT_X;
	pe.bp_addr=(unsigned long)address;
	pe.bp_len=sizeof(long);

	pe.disabled=1;
	pe.exclude_kernel=1;
	pe.exclude_hv=1;

	arch_adjust_domain(&pe,quiet);

	fd=perf_event_open(&pe,0,-1,-1,0);
	if (fd<0) {
		if (!quiet) {
			printf("\t\tError opening leader %llx %s\n",
				pe.config,strerror(errno));
		}
		goto skip_execs;
	}

	ioctl(fd, PERF_EVENT_IOC_RESET, 0);
	ioctl(fd, PERF_EVENT_IOC_ENABLE,0);

	/* Access a function 10 times */
	for(i=0;i<EXECUTIONS;i++) {
		sum+=test_function(i,sum,quiet);
	}

	if (!quiet) {
		printf("\t\tSum=%d\n",sum);
	}

	ioctl(fd, PERF_EVENT_IOC_DISABLE,0);
	read_result=read(fd,&count,sizeof(long long));

	if (read_result!=sizeof(long long)) {
		fprintf(stderr,"\tImproper return from read: %d\n",
				read_result);
		test_fail(test_string);
	}

	if (!quiet) {
		printf("\t\tTried %d calls, found %lld times, sum=%d\n",
			EXECUTIONS,count,sum);

		if (count!=EXECUTIONS) {
			fprintf(stderr,"\tWrong number of executions "
					"%lld != %d\n",count,EXECUTIONS);
			test_fail(test_string);
		}
	}

	close(fd);
	passes++;

skip_execs:

	/* Urgh, it's hard to force gcc 4.8+ to not optimize */
	/* this all away.                                    */
	sprintf(test_string,"Testing hardware breakpoints (%d)...",
					sum);

	/*******************************/
	/* Test write breakpoint       */
	/*******************************/

	if (!quiet) printf("\tTesting HW_BREAKPOINT_W\n");

	memset(&pe,0,sizeof(struct perf_event_attr));
	pe.type=PERF_TYPE_BREAKPOINT;
	pe.size=sizeof(struct perf_event_attr);

	/* setup for an execution breakpoint */
	pe.config=0;
	pe.bp_type=HW_BREAKPOINT_W;
	pe.bp_addr=(long)&test_variable;
	pe.bp_len=HW_BREAKPOINT_LEN_4;

	pe.disabled=1;
	pe.exclude_kernel=1;
	pe.exclude_hv=1;

	arch_adjust_domain(&pe,quiet);

	fd=perf_event_open(&pe,0,-1,-1,0);
	if (fd<0) {
		if (!quiet) {
			printf("\t\tError opening leader %llx %s\n",
				pe.config,strerror(errno));
		}
		goto skip_writes;
	}

	ioctl(fd, PERF_EVENT_IOC_RESET, 0);
	ioctl(fd, PERF_EVENT_IOC_ENABLE,0);

	/* Write a variable WRITES times */
	for(i=0;i<WRITES;i++) {
		test_variable=sum+time(NULL);
		if (!quiet) printf("\t\tWrote %d\n",test_variable);
	}

	ioctl(fd, PERF_EVENT_IOC_DISABLE,0);
	read_result=read(fd,&count,sizeof(long long));

	if (read_result!=sizeof(long long)) {
		fprintf(stderr,"\tImproper return from read: %d\n",read_result);
		test_fail(test_string);
	}

	if (!quiet) {
		printf("\t\tTried %d writes, found %lld times, sum=%d\n",
			WRITES,count,sum);
	}

	if (count!=WRITES) {
		fprintf(stderr,"\tWrong number of writes "
				"%lld != %d\n",count,WRITES);
		test_fail(test_string);
	}

	close(fd);
	passes++;
skip_writes:

	/* Urgh, it's hard to force gcc 4.8+ to not optimize */
	/* this all away.                                    */
	sprintf(test_string,"Testing hardware breakpoints (%d)...",
					test_variable);


	/*******************************/
	/* Test read breakpoint        */
	/*******************************/

	if (!quiet) printf("\tTesting HW_BREAKPOINT_R\n");

	memset(&pe,0,sizeof(struct perf_event_attr));
	pe.type=PERF_TYPE_BREAKPOINT;
	pe.size=sizeof(struct perf_event_attr);

	/* setup for an execution breakpoint */
	pe.config=0;
	pe.bp_type=HW_BREAKPOINT_R;
	pe.bp_addr=(long)&test_variable;
	pe.bp_len=HW_BREAKPOINT_LEN_4;

	pe.disabled=1;
	pe.exclude_kernel=1;
	pe.exclude_hv=1;

	arch_adjust_domain(&pe,quiet);

	fd=perf_event_open(&pe,0,-1,-1,0);
	if (fd<0) {
		if (!quiet) {
			printf("\t\tError opening leader %llx %s\n",
				pe.config,strerror(errno));
			printf("\t\tRead breakpoints probably not supported, "
				"skipping\n");
		}
		goto skip_reads;

	}

	ioctl(fd, PERF_EVENT_IOC_RESET, 0);
	ioctl(fd, PERF_EVENT_IOC_ENABLE,0);

	/* Read a variable READS times */
	for(i=0;i<READS;i++) {
		sum+=test_variable;
		if (!quiet) printf("\t\tRead %d\n",sum);
	}

	ioctl(fd, PERF_EVENT_IOC_DISABLE,0);
	read_result=read(fd,&count,sizeof(long long));

	if (read_result!=sizeof(long long)) {
		fprintf(stderr,"\tImproper return from read: %d\n",read_result);
		test_fail(test_string);
	}

	if (!quiet) {
		printf("\t\tTrying %d reads, found %lld times, sum=%d\n",
			READS,count,sum);
	}

	if (count!=READS) {
		fprintf(stderr,"\tWrong number of reads "
			"%lld != %d\n",count,READS);
		test_fail(test_string);
	}

	close(fd);
	passes++;

skip_reads:

	if (passes>0) {
		test_pass(test_string);
	} else {
		test_skip(test_string);
	}

	return 0;
}
Exemplo n.º 14
0
int main(int argc, char** argv) {

	int fd1,quiet;

	struct perf_event_attr pe;

	char test_string[]="Testing flags invalid bits...";

	quiet=test_quiet();

	if (!quiet) {
		printf("Testing flags invalid bits.\n");
		printf("This was broken prior to Linux 3.15\n");
	}

	memset(&pe,0,sizeof(struct perf_event_attr));
	pe.type=PERF_TYPE_HARDWARE;
	pe.size=sizeof(struct perf_event_attr);
	pe.config=PERF_COUNT_HW_INSTRUCTIONS;
	pe.disabled=0;
	pe.exclude_kernel=1;
	pe.exclude_hv=1;
	arch_adjust_domain(&pe,quiet);

	/* test 1, 0x40000000 */

	fd1=perf_event_open(&pe,0,-1,-1,0x4000000);
	if (fd1<0) {

		if (errno==EINVAL) {
			if (!quiet) {
				printf("Correctly failed opening leader with flags %x\n",0x40000000);
			}
		}
		else {
			if (!quiet) {
				fprintf(stderr,"Unexpected error %s\n",strerror(errno));
				test_fail(test_string);
			}
		}
	}
	else {
		close(fd1);
		if (!quiet) {
			fprintf(stderr,"Unexpectedly opened properly with flags %x\n",0x40000000);
		}
		test_fail(test_string);
	}


	/* test 1, 0x800000000 */

	fd1=perf_event_open(&pe,0,-1,-1,0x8000000000ULL);
	if (fd1<0) {
		if (errno==EINVAL) {
			if (!quiet) {
				printf("Correctly failed opening leader with flags %llx\n",0x8000000000ULL);
			}
		}
		else {
			if (!quiet) {
				fprintf(stderr,"Unexpected error %s\n",strerror(errno));
				test_fail(test_string);
			}
		}
	}
	else {
		int version;

		close(fd1);

		version=get_kernel_version();

		if (version<0x30f00) {

			if (!quiet) {
				fprintf(stderr,"Unexpectedly opened properly with flags %llx\n",0x8000000000ULL);
				fprintf(stderr,"This was not fixed until Linux 3.15\n");
			}
			test_fail_kernel(test_string);
		} else {
			if (!quiet) {
				fprintf(stderr,"Unexpectedly opened properly with flags %llx\n",0x8000000000ULL);
			}
			test_fail(test_string);
		}
	}


	test_pass(test_string);

	return 0;
}
Exemplo n.º 15
0
int main(int argc, char **argv) {

	int ret;
	int fd;
	int mmap_pages=1+MMAP_DATA_SIZE;
	FILE *fff;
	struct perf_event_attr pe;

	struct sigaction sa;
	char test_string[]="Testing AMD IBS fetch samples...";

	quiet=test_quiet();

	if (!quiet) printf("This tests AMD IBS...\n");

	/* TODO: detect we have AMD CPU */


	/* Set up signal handler */
	memset(&sa, 0, sizeof(struct sigaction));
	sa.sa_sigaction = our_handler;
	sa.sa_flags = SA_SIGINFO;

	if (sigaction( SIGIO, &sa, NULL) < 0) {
		fprintf(stderr,"Error setting up signal handler\n");
		exit(1);
	}

        /* Set up Event */

        memset(&pe,0,sizeof(struct perf_event_attr));

	sample_type=PERF_SAMPLE_IP |
			PERF_SAMPLE_WEIGHT |
			PERF_SAMPLE_ADDR |
			PERF_SAMPLE_RAW;
	read_format=0;

	pe.size=sizeof(struct perf_event_attr);



	/* Use a proper event */
	/* ./perf record -a -e ibs_fetch/rand_en=1/GH /bin/ls */

	fff=fopen("/sys/devices/ibs_fetch/type","r");
	if (fff==NULL) {
		fprintf(stderr,"Could not open ibs_fetch PMU\n");
		exit(1);
	}
	fscanf(fff,"%d",&pe.type);
	fclose(fff);



	/************************************************/
	/* Set up the event				*/
	/************************************************/
	/* See BKDG MSRC001_1030 IBS Fetch Control (IC_IBS_CTL) */
	/* Writable fields */
	/* 57 = IbsRandEn */
	/* 48 = IbsFetchEn */
	/* 31:16 = IbsFetchCnt (updated by hardware) */
	/* 15:0  = IbsFetchMaxCnt.  Shifted left by 4 */

	/* ibs_fetch/rand_en=1 */
	pe.config = 0x200000000000000ULL;

        pe.sample_period=SAMPLE_FREQUENCY;
        pe.sample_type=sample_type;

        pe.read_format=read_format;
        pe.disabled=1;
//        pe.pinned=1;
  //      pe.exclude_kernel=1;
    //    pe.exclude_hv=1;
    //    pe.wakeup_events=1;
//	pe.precise_ip=1;

	arch_adjust_domain(&pe,quiet);

	fd=perf_event_open(&pe,-1,0,-1,0);
	if (fd<0) {
		if (!quiet) {
			fprintf(stderr,"Problem opening leader %s\n",
				strerror(errno));
			test_fail(test_string);
		}
	}
	our_mmap=mmap(NULL, mmap_pages*4096,
		PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);

	fcntl(fd, F_SETFL, O_RDWR|O_NONBLOCK|O_ASYNC);
	fcntl(fd, F_SETSIG, SIGIO);
	fcntl(fd, F_SETOWN,getpid());

	ioctl(fd, PERF_EVENT_IOC_RESET, 0);

	ret=ioctl(fd, PERF_EVENT_IOC_ENABLE,0);

	if (ret<0) {
		if (!quiet) {
			fprintf(stderr,"Error with PERF_EVENT_IOC_ENABLE "
				"of group leader: %d %s\n",
				errno,strerror(errno));
			exit(1);
		}
	}

       naive_matrix_multiply(quiet);

	ret=ioctl(fd, PERF_EVENT_IOC_REFRESH,0);

	if (!quiet) {
                printf("Counts %d, using mmap buffer %p\n",count_total,our_mmap);
        }

	if (count_total==0) {
		if (!quiet) printf("No overflow events generated.\n");
		test_fail(test_string);
	}
	munmap(our_mmap,mmap_pages*4096);

	close(fd);

	test_pass(test_string);

	return 0;
}
Exemplo n.º 16
0
int main(int argc, char** argv) {

   int ret;
   int mmap_pages;

   struct perf_event_attr pe;

   struct sigaction sa;
   char test_string[]="Checking behavior of various mmap sizes...";

   quiet=test_quiet();

   if (!quiet) printf("This checks a variety of mmap buffer sizes.\n");

   /* set up validation */
   validate.pid=getpid();
   validate.tid=mygettid();
   validate.events=2;

   for(mmap_pages=0;mmap_pages<18;mmap_pages++) {

      if (!quiet) {
         printf("Testing with %d mmap pages\n",mmap_pages);
      }

      if (mmap_pages>0) {
         mmap_data_size=mmap_pages-1;
      }
      else {
         mmap_data_size=0;
      }

      memset(&sa, 0, sizeof(struct sigaction));
      sa.sa_sigaction = our_handler;
      sa.sa_flags = SA_SIGINFO;

      if (sigaction( SIGIO, &sa, NULL) < 0) {
         fprintf(stderr,"Error setting up signal handler\n");
         exit(1);
      }

      memset(&pe,0,sizeof(struct perf_event_attr));

      pe.type=PERF_TYPE_HARDWARE;
      pe.size=sizeof(struct perf_event_attr);
      pe.config=PERF_COUNT_HW_INSTRUCTIONS;
      pe.sample_period=SAMPLE_FREQUENCY;
      pe.sample_type=sample_type;

      pe.read_format=read_format;
      pe.disabled=1;
      pe.pinned=1;
      pe.exclude_kernel=1;
      pe.exclude_hv=1;
      pe.wakeup_events=1;

      arch_adjust_domain(&pe,quiet);

      fd1=perf_event_open(&pe,0,-1,-1,0);
      if (fd1<0) {
         if (!quiet) fprintf(stderr,"Error opening leader %llx\n",pe.config);
         test_fail(test_string);
      }

      memset(&pe,0,sizeof(struct perf_event_attr));

      pe.type=PERF_TYPE_HARDWARE;
      pe.size=sizeof(struct perf_event_attr);
      pe.config=PERF_COUNT_HW_CPU_CYCLES;
      pe.sample_type=PERF_SAMPLE_IP;
      pe.read_format=PERF_FORMAT_GROUP|PERF_FORMAT_ID;
      pe.disabled=0;
      pe.exclude_kernel=1;
      pe.exclude_hv=1;

      arch_adjust_domain(&pe,quiet);

      fd2=perf_event_open(&pe,0,-1,fd1,0);
      if (fd2<0) {
         if (!quiet) fprintf(stderr,"Error opening %llx\n",pe.config);
         test_fail(test_string);
      }

      our_mmap=mmap(NULL, mmap_pages*4096,
                    PROT_READ|PROT_WRITE, MAP_SHARED, fd1, 0);

      if (our_mmap == MAP_FAILED) {
         if (!quiet) printf("\tmmap failed: %d %s\n",errno,strerror(errno));
	 continue;
      }

      fcntl(fd1, F_SETFL, O_RDWR|O_NONBLOCK|O_ASYNC);
      fcntl(fd1, F_SETSIG, SIGIO);
      fcntl(fd1, F_SETOWN,getpid());

      ioctl(fd1, PERF_EVENT_IOC_RESET, 0);

      ret=ioctl(fd1, PERF_EVENT_IOC_ENABLE,0);

      if (ret<0) {
         if (!quiet) {
            fprintf(stderr,
                    "Error with PERF_EVENT_IOC_ENABLE of group leader: "
	            "%d %s\n",errno,strerror(errno));
        }
        exit(1);
      }

      instructions_million();

      ret=ioctl(fd1, PERF_EVENT_IOC_REFRESH,0);

      if (count.total==0) {
         if (!quiet) printf("No overflow events generated.\n");
         test_fail(test_string);
      }

      munmap(our_mmap,mmap_pages*4096);

      close(fd2);
      close(fd1);
   }

   test_pass(test_string);

   return 0;
}
Exemplo n.º 17
0
int main(int argc, char **argv) {

	int ret,status;
	int fd;
	int mmap_pages=1+MMAP_DATA_SIZE;
	int events_read;
	int child;
	int version;

	struct perf_event_attr pe;

	struct sigaction sa;
	char test_string[]="Testing PERF_RECORD_COMM_EXEC...";

	quiet=test_quiet();

	if (!quiet) printf("This tests PERF_RECORD_COMM_EXEC samples:\n");

        memset(&sa, 0, sizeof(struct sigaction));
        sa.sa_sigaction = our_handler;
        sa.sa_flags = SA_SIGINFO;

        if (sigaction( SIGIO, &sa, NULL) < 0) {
                fprintf(stderr,"Error setting up signal handler\n");
                exit(1);
        }

	/* Fork child to measure */
	/* We do this in a child as we have to exec */

	child=fork();
	if (child==0) {
		FILE *fff;
		if (ptrace(PTRACE_TRACEME, 0, 0, 0) == 0) {

			kill(getpid(),SIGTRAP);

			/* The actual thing to measure */
			instructions_million();

			/* prctl */
			if (!quiet) printf("\tprctl(PR_SET_NAME,\"vmw\");\n");
			prctl(PR_SET_NAME,"vmw");

			/* /proc/self/comm */
			if (!quiet) printf("\tcat \"krg krg krg\" > /proc/self/comm\n");
			fff=fopen("/proc/self/comm","w");
			if (fff!=NULL) {
				fprintf(fff,"krg krg krg");
				fclose(fff);
			}

			/* exec */
			if (!quiet) printf("\texecl(\"/bin/false\"); [should have PERF_RECORD_MISC_COMM_EXEC set]\n");
			execl("/bin/false","/bin/true",NULL);

			instructions_million();
			/* Done measuring */
		}
                else {
                        fprintf(stderr,"Failed ptrace...\n");
                }
                return 1;
	}

	/* wait for child to stop */
	child=wait(&status);


        /* Set up Instruction Event */

        memset(&pe,0,sizeof(struct perf_event_attr));

        pe.type=PERF_TYPE_SOFTWARE;
        pe.size=sizeof(struct perf_event_attr);
        pe.config=PERF_COUNT_SW_DUMMY;
        pe.sample_period=SAMPLE_FREQUENCY;

        pe.read_format=0;
        pe.disabled=1;
        pe.pinned=1;
        pe.exclude_kernel=1;
        pe.exclude_hv=1;
        pe.wakeup_events=1;

	pe.comm_exec=1;
	pe.comm=1;

	arch_adjust_domain(&pe,quiet);

	fd=perf_event_open(&pe,child,-1,-1,0);
	if (fd<0) {
		if (!quiet) {
			fprintf(stderr,"Problem opening leader %s\n",
				strerror(errno));
		}

		version=get_kernel_version();

		/* Introduced in 3.16 */
		if (version<0x31000) {

			if (!quiet) {
				fprintf(stderr,"comm_exec support not added until Linux 3.16\n");
			}
			test_fail_kernel(test_string);
		}

		test_fail(test_string);
	}

	our_mmap=mmap(NULL, mmap_pages*4096,
		PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
	if (our_mmap==MAP_FAILED) {
		fprintf(stderr,"mmap() failed %s!\n",strerror(errno));
		test_fail(test_string);
	}


	fcntl(fd, F_SETFL, O_RDWR|O_NONBLOCK|O_ASYNC);
	fcntl(fd, F_SETSIG, SIGIO);
	fcntl(fd, F_SETOWN,getpid());

	ioctl(fd, PERF_EVENT_IOC_RESET, 0);

	ret=ioctl(fd, PERF_EVENT_IOC_ENABLE,0);

	if (ret<0) {
		if (!quiet) {
			fprintf(stderr,"Error with PERF_EVENT_IOC_ENABLE "
				"of group leader: %d %s\n",
				errno,strerror(errno));
			exit(1);
		}
	}

	/* restart child */
	if ( ptrace( PTRACE_CONT, child, NULL, NULL ) == -1 ) {
		fprintf(stderr,"Error continuing child\n");
		test_fail(test_string);
	}

	/* Wait for child to finish */
	waitpid(child,&status,0);

	ret=ioctl(fd, PERF_EVENT_IOC_REFRESH,0);

	if (!quiet) {
                printf("Counts %d, using mmap buffer %p\n",count_total,our_mmap);
        }

	/* Drain any remaining events */
	prev_head=perf_mmap_read(our_mmap,MMAP_DATA_SIZE,prev_head,
                global_sample_type,0,global_sample_regs_user,
                NULL,quiet,&events_read);

	munmap(our_mmap,mmap_pages*4096);

	close(fd);

#define EXPECTED_EVENTS 3

	if (events_read!=EXPECTED_EVENTS) {
		if (!quiet) fprintf(stderr,"Wrong number of events!  Expected %d but got %d\n",
			EXPECTED_EVENTS,events_read);
		test_fail(test_string);
	}

	test_pass(test_string);

	return 0;
}
Exemplo n.º 18
0
int main(int argc, char** argv) {

	int ret,quiet;

	struct perf_event_attr pe;

	struct sigaction sa;
	void *our_mmap;
	char test_string[]="Testing large sample_period...";

	quiet=test_quiet();

	if (!quiet) {
		printf("This tests behavior of large sample_period.\n");
		printf("This was broken prior to Linux 3.15.\n\n");
	}

	memset(&sa, 0, sizeof(struct sigaction));
	sa.sa_sigaction = our_handler;
	sa.sa_flags = SA_SIGINFO;

	if (sigaction( SIGIO, &sa, NULL) < 0) {
		fprintf(stderr,"Error setting up signal handler\n");
		exit(1);
	}

	/*******************************************************/
	/* First try sample period that *will* cause overflows */
	/*******************************************************/

	count.in=0; count.out=0; count.msg=0;
	count.err=0; count.pri=0; count.hup=0;
	count.unknown=0; count.total=0;

	if (!quiet) {
		printf("Trying 1 million instructions with period 100,000\n");
		printf("Should be 10 overflows\n");
	}

	memset(&pe,0,sizeof(struct perf_event_attr));

	pe.type=PERF_TYPE_HARDWARE;
	pe.size=sizeof(struct perf_event_attr);
	pe.config=PERF_COUNT_HW_INSTRUCTIONS;
	pe.sample_period=100000;
	pe.sample_type=PERF_SAMPLE_IP;
	pe.read_format=PERF_FORMAT_GROUP|PERF_FORMAT_ID;
	pe.disabled=1;
	pe.pinned=1;
	pe.exclude_kernel=1;
	pe.exclude_hv=1;
	pe.wakeup_events=1;

	arch_adjust_domain(&pe,quiet);

	fd1=perf_event_open(&pe,0,-1,-1,0);
	if (fd1<0) {
		if (!quiet) fprintf(stderr,"Error opening leader %llx\n",pe.config);
		test_fail(test_string);
	}

	our_mmap=mmap(NULL, (1+MMAP_PAGES)*getpagesize(),
			PROT_READ|PROT_WRITE, MAP_SHARED, fd1, 0);

	fcntl(fd1, F_SETFL, O_RDWR|O_NONBLOCK|O_ASYNC);
	fcntl(fd1, F_SETSIG, SIGIO);
	fcntl(fd1, F_SETOWN,getpid());

	ioctl(fd1, PERF_EVENT_IOC_RESET, 0);

	ret=ioctl(fd1, PERF_EVENT_IOC_REFRESH,1);

	if (ret<0) {
		if (!quiet) {
			fprintf(stderr,"Error with PERF_EVENT_IOC_ENABLE of group leader: "
					"%d %s\n",errno,strerror(errno));
			test_fail(test_string);
		}
	}

	instructions_million();

	ret=ioctl(fd1, PERF_EVENT_IOC_DISABLE,0);
	close(fd1);

	if (!quiet) {
		printf("Counts, using mmap buffer %p\n",our_mmap);
		printf("\tPOLL_IN : %d\n",count.in);
		printf("\tPOLL_OUT: %d\n",count.out);
		printf("\tPOLL_MSG: %d\n",count.msg);
		printf("\tPOLL_ERR: %d\n",count.err);
		printf("\tPOLL_PRI: %d\n",count.pri);
		printf("\tPOLL_HUP: %d\n",count.hup);
		printf("\tUNKNOWN : %d\n",count.unknown);
	}

	if (count.total==0) {
		if (!quiet) printf("No overflow events generated.\n");
		test_fail(test_string);
	}

	if (count.in!=0) {
		if (!quiet) printf("Unexpected POLL_IN interrupt.\n");
		test_fail(test_string);
	}

	if (count.hup!=10) {
		if (!quiet) printf("POLL_HUP value %d, expected %d.\n",
					count.hup,10);
		test_fail(test_string);
	}

	/*******************************************************/
	/* Next try moderate size value, should be no overflow */
	/*******************************************************/

	count.in=0; count.out=0; count.msg=0;
	count.err=0; count.pri=0; count.hup=0;
	count.unknown=0; count.total=0;

	if (!quiet) {
		printf("Trying 1 million instructions with period 10,000,000\n");
		printf("Should be 0 overflows\n");
	}

	memset(&pe,0,sizeof(struct perf_event_attr));

	pe.type=PERF_TYPE_HARDWARE;
	pe.size=sizeof(struct perf_event_attr);
	pe.config=PERF_COUNT_HW_INSTRUCTIONS;
	pe.sample_period=10000000;
	pe.sample_type=PERF_SAMPLE_IP;
	pe.read_format=PERF_FORMAT_GROUP|PERF_FORMAT_ID;
	pe.disabled=1;
	pe.pinned=1;
	pe.exclude_kernel=1;
	pe.exclude_hv=1;
	pe.wakeup_events=1;

	arch_adjust_domain(&pe,quiet);

	fd1=perf_event_open(&pe,0,-1,-1,0);
	if (fd1<0) {
		if (!quiet) fprintf(stderr,"Error opening leader %llx\n",pe.config);
		test_fail(test_string);
	}

	our_mmap=mmap(NULL, (1+MMAP_PAGES)*getpagesize(),
			PROT_READ|PROT_WRITE, MAP_SHARED, fd1, 0);

	fcntl(fd1, F_SETFL, O_RDWR|O_NONBLOCK|O_ASYNC);
	fcntl(fd1, F_SETSIG, SIGIO);
	fcntl(fd1, F_SETOWN,getpid());

	ioctl(fd1, PERF_EVENT_IOC_RESET, 0);

	ret=ioctl(fd1, PERF_EVENT_IOC_REFRESH,1);

	if (ret<0) {
		if (!quiet) {
			fprintf(stderr,"Error with PERF_EVENT_IOC_ENABLE of group leader: "
					"%d %s\n",errno,strerror(errno));
			test_fail(test_string);
		}
	}

	instructions_million();

	ret=ioctl(fd1, PERF_EVENT_IOC_DISABLE,0);
	close(fd1);

	if (!quiet) {
		printf("Counts, using mmap buffer %p\n",our_mmap);
		printf("\tPOLL_IN : %d\n",count.in);
		printf("\tPOLL_OUT: %d\n",count.out);
		printf("\tPOLL_MSG: %d\n",count.msg);
		printf("\tPOLL_ERR: %d\n",count.err);
		printf("\tPOLL_PRI: %d\n",count.pri);
		printf("\tPOLL_HUP: %d\n",count.hup);
		printf("\tUNKNOWN : %d\n",count.unknown);
	}

	if (count.total!=0) {
		if (!quiet) printf("Unexpected overflow events generated.\n");
		test_fail(test_string);
	}


	/*******************************************************/
	/* Next try large size value, should be no overflow    */
	/*******************************************************/

	count.in=0; count.out=0; count.msg=0;
	count.err=0; count.pri=0; count.hup=0;
	count.unknown=0; count.total=0;

	memset(&pe,0,sizeof(struct perf_event_attr));

	pe.type=PERF_TYPE_HARDWARE;
	pe.size=sizeof(struct perf_event_attr);
	pe.config=PERF_COUNT_HW_INSTRUCTIONS;
	pe.sample_period=(1ULL<<63)-1;
	pe.sample_type=PERF_SAMPLE_IP;
	pe.read_format=PERF_FORMAT_GROUP|PERF_FORMAT_ID;
	pe.disabled=1;
	pe.pinned=1;
	pe.exclude_kernel=1;
	pe.exclude_hv=1;
	pe.wakeup_events=1;

	if (!quiet) {
		printf("Trying 1 million instructions with period (2^63)-1 (%llx)\n",
			pe.sample_period);
		printf("Should be 0 overflows\n");
	}

	arch_adjust_domain(&pe,quiet);

	fd1=perf_event_open(&pe,0,-1,-1,0);
	if (fd1<0) {
		if (!quiet) fprintf(stderr,"Error opening leader %llx\n",pe.config);
		test_fail(test_string);
	}

	our_mmap=mmap(NULL, (1+MMAP_PAGES)*getpagesize(),
			PROT_READ|PROT_WRITE, MAP_SHARED, fd1, 0);

	fcntl(fd1, F_SETFL, O_RDWR|O_NONBLOCK|O_ASYNC);
	fcntl(fd1, F_SETSIG, SIGIO);
	fcntl(fd1, F_SETOWN,getpid());

	ioctl(fd1, PERF_EVENT_IOC_RESET, 0);

	ret=ioctl(fd1, PERF_EVENT_IOC_REFRESH,1);

	if (ret<0) {
		if (!quiet) {
			fprintf(stderr,"Error with PERF_EVENT_IOC_ENABLE of group leader: "
					"%d %s\n",errno,strerror(errno));
			test_fail(test_string);
		}
	}

	instructions_million();

	ret=ioctl(fd1, PERF_EVENT_IOC_DISABLE,0);
	close(fd1);

	if (!quiet) {
		printf("Counts, using mmap buffer %p\n",our_mmap);
		printf("\tPOLL_IN : %d\n",count.in);
		printf("\tPOLL_OUT: %d\n",count.out);
		printf("\tPOLL_MSG: %d\n",count.msg);
		printf("\tPOLL_ERR: %d\n",count.err);
		printf("\tPOLL_PRI: %d\n",count.pri);
		printf("\tPOLL_HUP: %d\n",count.hup);
		printf("\tUNKNOWN : %d\n",count.unknown);
	}

	if (count.total!=0) {
		if (!quiet) printf("Unexpected overflow events generated.\n");
		test_fail(test_string);
	}

	/************************************************************/
	/* Next try very large size value, should be no overflow    */
	/* In fact, on 3.15 and later the open should fail          */
	/************************************************************/

	count.in=0; count.out=0; count.msg=0;
	count.err=0; count.pri=0; count.hup=0;
	count.unknown=0; count.total=0;

	memset(&pe,0,sizeof(struct perf_event_attr));

	pe.type=PERF_TYPE_HARDWARE;
	pe.size=sizeof(struct perf_event_attr);
	pe.config=PERF_COUNT_HW_INSTRUCTIONS;
	pe.sample_period=0xc0000000000000bd;
	pe.sample_type=PERF_SAMPLE_IP;
	pe.read_format=PERF_FORMAT_GROUP|PERF_FORMAT_ID;
	pe.disabled=1;
	pe.pinned=1;
	pe.exclude_kernel=1;
	pe.exclude_hv=1;
	pe.wakeup_events=1;

	if (!quiet) {
		printf("Trying 1 million instructions with period %llx\n",
			pe.sample_period);
		printf("Should be 0 overflows\n");
	}

	arch_adjust_domain(&pe,quiet);

	fd1=perf_event_open(&pe,0,-1,-1,0);
	if (fd1<0) {

		if (errno==EINVAL) {
			if (!quiet) {
				fprintf(stderr,"Properly failed with too-large sample_period\n");
				test_pass(test_string);
				exit(0);
			}
		}

		if (!quiet) fprintf(stderr,"Error opening leader %llx\n",pe.config);
		test_fail(test_string);
	}

	our_mmap=mmap(NULL, (1+MMAP_PAGES)*getpagesize(),
			PROT_READ|PROT_WRITE, MAP_SHARED, fd1, 0);

	fcntl(fd1, F_SETFL, O_RDWR|O_NONBLOCK|O_ASYNC);
	fcntl(fd1, F_SETSIG, SIGIO);
	fcntl(fd1, F_SETOWN,getpid());

	ioctl(fd1, PERF_EVENT_IOC_RESET, 0);

	ret=ioctl(fd1, PERF_EVENT_IOC_REFRESH,1);

	if (ret<0) {
		if (!quiet) {
			fprintf(stderr,"Error with PERF_EVENT_IOC_ENABLE of group leader: "
					"%d %s\n",errno,strerror(errno));
			test_fail(test_string);
		}
	}

	instructions_million();

	ret=ioctl(fd1, PERF_EVENT_IOC_DISABLE,0);
	close(fd1);

	if (!quiet) {
		printf("Counts, using mmap buffer %p\n",our_mmap);
		printf("\tPOLL_IN : %d\n",count.in);
		printf("\tPOLL_OUT: %d\n",count.out);
		printf("\tPOLL_MSG: %d\n",count.msg);
		printf("\tPOLL_ERR: %d\n",count.err);
		printf("\tPOLL_PRI: %d\n",count.pri);
		printf("\tPOLL_HUP: %d\n",count.hup);
		printf("\tUNKNOWN : %d\n",count.unknown);
	}

	if (count.total>20000) {
		int version;
		if (!quiet) printf("Stopping early, too many overflows encountered.\n");
		/* This is expected before 3.15 */
		version=get_kernel_version();
		if (version<0x30f00) {
			test_fail_kernel(test_string);
		}
		else {
			test_fail(test_string);
		}
	}

	if (count.total!=0) {
		if (!quiet) printf("Unexpected overflow events generated.\n");
		test_fail(test_string);
	}

	test_pass(test_string);

	return 0;
}
Exemplo n.º 19
0
int main(int argc, char **argv) {
   
   int num_runs=100,i,read_result,result;
   int num_random_branches=500000;
   long long high=0,low=0,average=0,expected=1500000;
   struct perf_event_attr pe;
   
   long long count,total=0;
     
   quiet=test_quiet();

   if (!quiet) {
      printf("\nPart 1\n");   

      printf("Testing a loop with %lld branches (%d times):\n",
          expected,num_runs);
      printf("\tOn a simple loop like this, miss rate should be very small.\n");
   }

   memset(&pe,0,sizeof(struct perf_event_attr));
   
   pe.type=PERF_TYPE_HARDWARE;
   pe.size=sizeof(struct perf_event_attr);
   pe.config=PERF_COUNT_HW_BRANCH_MISSES;
   pe.disabled=1;
   pe.exclude_kernel=1;
   pe.exclude_hv=1;

   arch_adjust_domain(&pe,quiet);

   fd=perf_event_open(&pe,0,-1,-1,0);
   if (fd<0) {
     if (!quiet) fprintf(stderr,"Error opening leader %llx\n",pe.config);
     test_fail(test_string);
   }

   for(i=0;i<num_runs;i++) {
      ioctl(fd, PERF_EVENT_IOC_RESET, 0);
      ioctl(fd, PERF_EVENT_IOC_ENABLE, 0);
 
      result=branches_testcode();
      
      ioctl(fd, PERF_EVENT_IOC_DISABLE, 0);
      read_result=read(fd,&count,sizeof(long long));
      
      if (result==CODE_UNIMPLEMENTED) {
	 if (!quiet) printf("\tNo test code for this architecture\n");
	 test_skip(test_string);
      }
      
      if (read_result!=sizeof(long long)) {
	if (!quiet) printf("Error extra data in read %d\n",read_result);
	 test_fail(test_string);         
      }
      
      if (count>high) high=count;
      if ((low==0) || (count<low)) low=count;
      total+=count;
   }

   average=(total/num_runs);

   if (!quiet) printf("\tAverage number of branch misses: %lld\n",average);

   if (average>1000) {
     if (!quiet) printf("Branch miss rate too high\n");
     test_fail(test_string);
   }

   close(fd);

   /*******************/

   if (!quiet) {
     printf("\nPart 2\n");
   }

   high=0; low=0; total=0;

   memset(&pe,0,sizeof(struct perf_event_attr));
   
   pe.type=PERF_TYPE_HARDWARE;
   pe.size=sizeof(struct perf_event_attr);
   pe.config=PERF_COUNT_HW_BRANCH_INSTRUCTIONS;
   pe.disabled=1;
   pe.exclude_kernel=1;
   pe.exclude_hv=1;

   arch_adjust_domain(&pe,quiet);

   fd=perf_event_open(&pe,0,-1,-1,0);
   if (fd<0) {
     if (!quiet) fprintf(stderr,"Error opening leader %llx\n",pe.config);
     test_fail(test_string);
   }

   for(i=0;i<num_runs;i++) {

      ioctl(fd, PERF_EVENT_IOC_RESET, 0);
      ioctl(fd, PERF_EVENT_IOC_ENABLE, 0);

     result=random_branches_testcode(num_random_branches,1);

      ioctl(fd, PERF_EVENT_IOC_DISABLE, 0);
      read_result=read(fd,&count,sizeof(long long));

     if (count>high) high=count;
     if ((low==0) || (count<low)) low=count;
     total+=count;
   }

   average=total/num_runs;

   expected=average;
   if (!quiet) {
     printf("\nTesting a function that branches based on a random number\n");
     printf("   The loop has %lld branches.\n",expected);
     printf("   %d are random branches; %d of those were taken\n",num_random_branches,result);
   }
   close(fd);

   high=0; low=0; total=0;

   memset(&pe,0,sizeof(struct perf_event_attr));
   
   pe.type=PERF_TYPE_HARDWARE;
   pe.size=sizeof(struct perf_event_attr);
   pe.config=PERF_COUNT_HW_BRANCH_MISSES;
   pe.disabled=1;
   pe.exclude_kernel=1;
   pe.exclude_hv=1;

   arch_adjust_domain(&pe,quiet);

   fd=perf_event_open(&pe,0,-1,-1,0);
   if (fd<0) {
      if (!quiet) fprintf(stderr,"Error opening leader %llx\n",pe.config);
      test_fail(test_string);
   }

   for(i=0;i<num_runs;i++) {

      ioctl(fd, PERF_EVENT_IOC_RESET, 0);
      ioctl(fd, PERF_EVENT_IOC_ENABLE, 0);

      result=random_branches_testcode(num_random_branches,1);

      ioctl(fd, PERF_EVENT_IOC_DISABLE, 0);
      read_result=read(fd,&count,sizeof(long long));

     if (count>high) high=count;
     if ((low==0) || (count<low)) low=count;
     total+=count;
   }

   average=total/num_runs;

   if (!quiet) {

     printf("\nOut of %lld branches, %lld were mispredicted\n",expected,average);
     printf("Assuming a good random number generator and no freaky luck\n");
     printf("The mispredicts should be roughly between %d and %d\n",
	    num_random_branches/4,(num_random_branches/4)*3);
   }

   if ( average < (num_random_branches/4)) {
     if (!quiet) printf("Mispredicts too low\n");
     test_fail(test_string);
   }

   if (average > (num_random_branches/4)*3) { 
     if (!quiet) printf("Mispredicts too high\n");
     test_fail(test_string);
   }
   if (!quiet) printf("\n");

   test_pass( test_string );
   
   return 0;
}
Exemplo n.º 20
0
int main(int argc, char **argv) {

	int ret;
	int fd;
	int mmap_pages=1+MMAP_DATA_SIZE;

	struct perf_event_attr pe;

	struct sigaction sa;
	char test_string[]="Testing PERF_SAMPLE_REGS_INTR...";

	quiet=test_quiet();

	if (!quiet) printf("This tests PERF_SAMPLE_REGS_INTR samples\n");

	        memset(&sa, 0, sizeof(struct sigaction));
        sa.sa_sigaction = our_handler;
        sa.sa_flags = SA_SIGINFO;

        if (sigaction( SIGIO, &sa, NULL) < 0) {
                fprintf(stderr,"Error setting up signal handler\n");
                exit(1);
        }

	memset(&pe,0,sizeof(struct perf_event_attr));
        pe.size=sizeof(struct perf_event_attr);
        pe.sample_period=SAMPLE_FREQUENCY;
        pe.sample_type=PERF_SAMPLE_IP | PERF_SAMPLE_REGS_USER | PERF_SAMPLE_REGS_INTR;
	global_sample_type=pe.sample_type;

#if defined (__x86_64__)

	/* Bitfield saying which registers we want */
	pe.sample_regs_user=(1ULL<<PERF_REG_X86_64_MAX)-1;
	/* DS, ES, FS, and GS not valid on x86_64 */
	/* see  perf_reg_validate() in arch/x86/kernel/perf_regs.c */
	pe.sample_regs_user&=~(1ULL<<PERF_REG_X86_DS);
	pe.sample_regs_user&=~(1ULL<<PERF_REG_X86_ES);
	pe.sample_regs_user&=~(1ULL<<PERF_REG_X86_FS);
	pe.sample_regs_user&=~(1ULL<<PERF_REG_X86_GS);

	pe.sample_regs_intr=pe.sample_regs_user;

        pe.read_format=0;
        pe.disabled=1;
        pe.pinned=1;
        pe.wakeup_events=1;


#elif defined(__i386__)
	pe.sample_regs_user=(1ULL<<PERF_REG_X86_32_MAX)-1;
	pe.sample_regs_intr=pe.sample_regs_user;
#elif defined(__arm__)
	pe.sample_regs_user=(1ULL<<PERF_REG_ARM_MAX)-1;
	pe.sample_regs_intr=pe.sample_regs_user;
#else
	pe.sample_regs_user=1;
	pe.sample_regs_intr=1;
#endif

	global_sample_regs_user=pe.sample_regs_user;


	if (detect_vendor()==VENDOR_AMD) {
		if (!quiet) printf("Using cycles:pp on AMD\n");
		/* On AMD cycles is a precise event */
		pe.type=PERF_TYPE_HARDWARE;
		pe.config=PERF_COUNT_HW_CPU_CYCLES;

		/* on AMD ibs the following must be false */
		/* see bad9ac2d7f878a31cf1ae8c1ee3768077d222bcb */
		/*	.exclude_user   = 1,
		.exclude_kernel = 1,
		.exclude_hv     = 1,
		.exclude_idle   = 1,
		.exclude_host   = 1,
		.exclude_guest  = 1,
		*/

	        pe.exclude_user    = 0;
        	pe.exclude_kernel  = 0;
        	pe.exclude_hv	   = 0;
		pe.exclude_idle	   = 0;
		pe.exclude_host	   = 0;
		pe.exclude_guest   = 0;

	}

	else {
		if (!quiet) printf("Using instructions:pp\n");
		/* Set up Instruction Event */
		//	pe.type=PERF_TYPE_RAW;
		//	pe.config=0x5300c0; // INST_RETIRED:ANY_P
		pe.type=PERF_TYPE_HARDWARE;
		pe.config=PERF_COUNT_HW_INSTRUCTIONS;

		pe.exclude_kernel=0;
		pe.exclude_hv=1;
	}

	arch_adjust_domain(&pe,quiet);

	/* Must be greater than 0 for sample_regs_intr to be interesting? */
	/* Not seeing any difference. */
	pe.precise_ip=2;

	if (detect_vendor()==VENDOR_AMD) {
		/* On AMD needs to be system-wide per-cpu event */
		/* or the IBS PMU won't work.			*/
		fd=perf_event_open(&pe,-1,0,-1,0);
	}
	else {
		fd=perf_event_open(&pe,0,-1,-1,0);
	}
	if (fd<0) {
		if (!quiet) {
			fprintf(stderr,"Problem opening leader %s\n",
				strerror(errno));
		}
		if (errno==EACCES) {
			test_skip(test_string);
		}
		test_fail(test_string);
	}
	our_mmap=mmap(NULL, mmap_pages*getpagesize(),
		PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);

	fcntl(fd, F_SETFL, O_RDWR|O_NONBLOCK|O_ASYNC);
	fcntl(fd, F_SETSIG, SIGIO);
	fcntl(fd, F_SETOWN,getpid());

	ioctl(fd, PERF_EVENT_IOC_RESET, 0);

	ret=ioctl(fd, PERF_EVENT_IOC_ENABLE,0);

	if (ret<0) {
		if (!quiet) {
			fprintf(stderr,"Error with PERF_EVENT_IOC_ENABLE "
				"of group leader: %d %s\n",
				errno,strerror(errno));
			exit(1);
		}
	}

	int i;
	for(i=0;i<10;i++) {
//		write(1,"0",1);
		instructions_million();
	}

	ret=ioctl(fd, PERF_EVENT_IOC_REFRESH,0);

	if (!quiet) {
                printf("Counts %d, using mmap buffer %p\n",count_total,our_mmap);
        }

	if (count_total==0) {
		if (!quiet) printf("No overflow events generated.\n");
		test_fail(test_string);
	}

	ret=ioctl(fd, PERF_EVENT_IOC_DISABLE,0);

	munmap(our_mmap,mmap_pages*getpagesize());

	close(fd);

	test_pass(test_string);

	return 0;
}
Exemplo n.º 21
0
int main(int argc, char **argv) {

	int ret;
	int fd;
	int result,precise_ip;
	int mmap_pages=1+MMAP_DATA_SIZE;

	struct perf_event_attr pe;

	struct sigaction sa;
	char test_string[]="Testing PERF_SAMPLE_WEIGHT...";
	char event_name[BUFSIZ];

	quiet=test_quiet();

	if (!quiet) printf("This tests PERF_SAMPLE_WEIGHT samples\n");

	        memset(&sa, 0, sizeof(struct sigaction));
        sa.sa_sigaction = our_handler;
        sa.sa_flags = SA_SIGINFO;

        if (sigaction( SIGIO, &sa, NULL) < 0) {
                fprintf(stderr,"Error setting up signal handler\n");
                exit(1);
        }

        /* Set up Proper Event */

        memset(&pe,0,sizeof(struct perf_event_attr));

	result=get_latency_load_event(&pe.config,&pe.config1,
					&precise_ip,event_name);
	if (result<0) {
		if (!quiet) fprintf(stderr,"No load latency event available, trying instructions (probably will return 0)\n");
		pe.type=PERF_TYPE_HARDWARE;
		pe.config=PERF_COUNT_HW_INSTRUCTIONS;
	}
	else {
		pe.type=PERF_TYPE_RAW;
		if (!quiet) printf("Using event %s\n",event_name);
	}

        pe.size=sizeof(struct perf_event_attr);
	pe.precise_ip=precise_ip;

        pe.sample_period=SAMPLE_FREQUENCY;
        pe.sample_type=PERF_SAMPLE_IP | PERF_SAMPLE_WEIGHT;

	global_sample_type=pe.sample_type;

        pe.read_format=0;
        pe.disabled=1;
        pe.pinned=1;
        pe.exclude_kernel=1;
        pe.exclude_hv=1;
        pe.wakeup_events=1;

	arch_adjust_domain(&pe,quiet);

	fd=perf_event_open(&pe,0,-1,-1,0);
	if (fd<0) {
		if (!quiet) {
			fprintf(stderr,"Problem opening leader %s\n",
				strerror(errno));
			test_fail(test_string);
		}
	}
	our_mmap=mmap(NULL, mmap_pages*4096,
		PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);

	fcntl(fd, F_SETFL, O_RDWR|O_NONBLOCK|O_ASYNC);
	fcntl(fd, F_SETSIG, SIGIO);
	fcntl(fd, F_SETOWN,getpid());

	ioctl(fd, PERF_EVENT_IOC_RESET, 0);

	ret=ioctl(fd, PERF_EVENT_IOC_ENABLE,0);

	if (ret<0) {
		if (!quiet) {
			fprintf(stderr,"Error with PERF_EVENT_IOC_ENABLE "
				"of group leader: %d %s\n",
				errno,strerror(errno));
			exit(1);
		}
	}

	naive_matrix_multiply(quiet);
//	instructions_million();

	ret=ioctl(fd, PERF_EVENT_IOC_REFRESH,0);

	if (!quiet) {
                printf("Counts %d, using mmap buffer %p\n",count_total,our_mmap);
        }

	if (count_total==0) {
		if (!quiet) printf("No overflow events generated.\n");
		test_fail(test_string);
	}
	munmap(our_mmap,mmap_pages*4096);

	close(fd);

	test_pass(test_string);

	return 0;
}
Exemplo n.º 22
0
int main(int argc, char** argv) {

	int fd[2],i;
	int quiet;
	int validation_errors=0;
	int old_behavior=1;

	long long diff;

	struct perf_event_attr pe;

	struct sigaction sa;

	quiet=test_quiet();

	if (!quiet) {
		printf("This tests the PERF_EVENT_IOC_PERIOD ioctl.\n\n");
	}

	memset(&pe,0,sizeof(struct perf_event_attr));

	pe.type=PERF_TYPE_HARDWARE;
	pe.config=PERF_COUNT_HW_INSTRUCTIONS;
	pe.disabled=1;
	pe.exclude_kernel=1;
	pe.exclude_hv=1;
	pe.sample_period=10000;

	arch_adjust_domain(&pe, quiet);

	fd[0]=perf_event_open(&pe,0,-1,-1,0);
	if (fd[0]<0) {
		if (!quiet) fprintf(stderr,"Error opening\n");
		test_fail(test_string);
	}

	/* Set up overflow */
	memset(&sa, 0, sizeof(struct sigaction));
	sa.sa_sigaction = our_handler;
	sa.sa_flags = SA_SIGINFO;
	if (sigaction( SIGRTMIN+2, &sa, NULL) < 0) {
		printf("Error setting up signal handler\n");
	}
	fcntl(fd[0], F_SETFL, O_RDWR|O_NONBLOCK|O_ASYNC);
	fcntl(fd[0], F_SETSIG, SIGRTMIN+2);
	fcntl(fd[0], F_SETOWN,getpid());

	ioctl(fd[0], PERF_EVENT_IOC_REFRESH,1);

	instructions_million();

	ioctl(fd[0], PERF_EVENT_IOC_DISABLE,0);

	close(fd[0]);

	if (!quiet) {
		printf("Overflows:\n");
		for(i=0;i<overflows;i++) {
			printf("\t%d %lld\n",i,overflow_counts[i]);
		}
	}

	if (ioctl_errors) {

		if (!quiet) {
			if (ioctl_errno==EFAULT) {
				fprintf(stderr,"Known issue with kernels <2.6.36, PERF_IOC_PERIOD always fails due to kernel bug.\n");
			}
			else {
				fprintf(stderr,"Unknown failure with PERF_EVENT_IOC_PERIOD: %s\n",strerror(errno));
			}
		}

		test_fail(test_string);
	}

	/* validate results */
	/* should be 10k apart for 0,1,2,3,4	*/

	for(i=0;i<4;i++) {
		diff=overflow_counts[i+1]-overflow_counts[i];

		if ((diff>11000) || (diff<9000)) {
			if (!quiet) {
				fprintf(stderr,"Overflow %i-%i should have been 10,000, was %lld\n",i,i+1,diff);
			}
			validation_errors++;
		}
	}

	/* 4-5 should be 10k (old behavior) or 100k (new behavior) */

	diff=overflow_counts[5]-overflow_counts[4];

	if ((diff<11000) && (diff>9000)) {
		if (!quiet) {
			fprintf(stderr,"Overflow %i-%i near 10,000 (%lld), old behavior\n",i,i+1,diff);
		}
		old_behavior=1;
	}
	else if ((diff<101000) && (diff>99000)) {
		if (!quiet) {
			fprintf(stderr,"Overflow %i-%i near 100,000 (%lld), new behavior\n",i,i+1,diff);
		}
		old_behavior=0;
	}
	else {
		if (!quiet) {
			fprintf(stderr,"Overflow %i-%i %lld, unexpected\n",i,i+1,diff);
		}
		validation_errors++;
	}


	/* 5-6 and after should be 100k		*/
	for(i=5;i<overflows-1;i++) {
		diff=overflow_counts[i+1]-overflow_counts[i];

		if ((diff>101000) || (diff<99000)) {
			if (!quiet) {
				fprintf(stderr,"Overflow %i-%i should have been 100,000, was %lld\n",i,i+1,diff);
			}
			validation_errors++;
		}
	}

	if (validation_errors) {
		test_fail(test_string);
	}

	if (old_behavior) {
		test_yellow_old_behavior(test_string);
	}
	else {
		test_green_new_behavior(test_string);
	}

	/* FIXME: also check for case where we reset
		overflow on running counter?
	*/

	return 0;
}
Exemplo n.º 23
0
int main(int argc, char** argv) {

	int ret,fd1,quiet,i;
	int result;
	pid_t pid;

	struct perf_event_attr pe;

	char test_string[]="Testing reads in forked children...";

	quiet=test_quiet();

	/* set up group leader */
	memset(&pe,0,sizeof(struct perf_event_attr));
	pe.type=PERF_TYPE_HARDWARE;
	pe.size=sizeof(struct perf_event_attr);
	pe.config=PERF_COUNT_HW_INSTRUCTIONS;
	pe.read_format=PERF_FORMAT_GROUP|PERF_FORMAT_ID;
	pe.disabled=1;
	pe.pinned=1;
	pe.exclude_kernel=1;
	pe.exclude_hv=1;

	arch_adjust_domain(&pe,quiet);

	fd1=perf_event_open(&pe,0,-1,-1,0);
	if (fd1<0) {
		if (!quiet) {
			fprintf(stderr,"Error opening leader %llx\n",pe.config);
		}
		test_fail(test_string);
	}


	if (!quiet) {
		printf("Testing fork behavior\n");
		printf("Even though the child runs longer, the value\n");
		printf("it reads should be that of the parent.\n");
	}

	/* enable counting */
	ret=ioctl(fd1, PERF_EVENT_IOC_ENABLE,0);

	/* Run a million */
	result=instructions_million();

	/* fork off a child */
	pid = fork();

	if ( pid < 0 ) {
		fprintf(stderr,"Failed fork\n");
		test_fail(test_string);
	}

	/* our child */
	if ( pid == 0 ) {
		printf("In child, running an extra million\n");
		/* extra million */
		result=instructions_million();
	}
	else {
		printf("In parent\n");

		/* disable counting */
		ret=ioctl(fd1, PERF_EVENT_IOC_DISABLE,0);
		if (ret<0) {
			if (!quiet) printf("Error disabling\n");
		}
	}



	#define BUFFER_SIZE 32
	long long buffer[BUFFER_SIZE];
	for(i=0;i<BUFFER_SIZE;i++) {
		buffer[i]=-1;
	}

	result=read(fd1,buffer,BUFFER_SIZE*sizeof(long long));
	if (result<0) {
		if (!quiet) {
			fprintf(stderr,"Unexpected read result %d\n",result);
		}
		test_fail(test_string);
	}

	/* should be 1 + 2*num_events */
	/* which is 3 in our case     */
	if (result!=(3)*sizeof(long long)) {
		if (!quiet) {
			fprintf(stderr,"Unexpected read result %d (should be %ld)\n",
					result,3*sizeof(long long));
		}
		test_fail(test_string);
	}

	if (!quiet) {
		printf("Number of events: %lld\n",buffer[0]);
		for(i=0;i<buffer[0];i++) {
			printf("Value    [%d] : %lld\n",i,buffer[1+(i*2)]);
			printf("Format ID[%d] : %lld\n",i,buffer[1+((i*2)+1)]);
		}
	}

	double error;
	long long average,high,low;
	int failure=0;

	average=high=low=buffer[1];

	error=display_error(average,high,low,1000000ULL,quiet);

	if ((error > 1.0) || (error<-1.0)) {
		failure++;
	}

	/* child */
	if (pid==0) {
		return failure;
	}
	else {
		int status;

		waitpid(pid,&status,0);

		if (WIFEXITED(status)) {
			if (WEXITSTATUS(status)!=0) failure++;
		}
	}

	if (failure) {
		test_fail(test_string);
	}


	test_pass(test_string);

	return 0;
}
Exemplo n.º 24
0
int main(int argc, char** argv) {

	int ret,quiet,status;
	pid_t pid,child;

	struct perf_event_attr pe;

	void *our_mmap;
	char test_string[]="Testing catching overflow with poll()...";

	quiet=test_quiet();

	if (!quiet) printf("This tests using poll() to catch overflow.\n");

	/* fork off a child */
	pid=fork();

	if (pid<0) {
		fprintf(stderr,"Failed fork\n");
		test_fail(test_string);
	}

	/* Our child.  Set up ptrace to stop it     */
	/* originally tried kill(getpid(),SIGSTOP); */
	/* but that is unreliable?                  */
	if (pid==0) {
		if (ptrace(PTRACE_TRACEME, 0, 0, 0) == 0) {
			int c;
			kill(getpid(),SIGTRAP);

                        for(c=0;c<10;c++) {
				instructions_million();
			}
                }
                else {
                        fprintf(stderr,"Failed ptrace...\n");
                }
                return 1;
        }

	child=wait(&status);

	/* Make sure child is stopped and waiting */
	if (!quiet) printf( "Monitoring pid %d status %d\n",pid,status);

	if (WIFSTOPPED( status )) {
		if (!quiet) {
			printf( "Child has stopped due to signal %d (%s)\n",
				WSTOPSIG( status ), strsignal(WSTOPSIG( status )) );
		}
	}

	if (WIFSIGNALED( status )) {
		if (!quiet) {
			printf( "Child %d received signal %d (%s)\n",
				child, WTERMSIG(status),
				strsignal(WTERMSIG( status )) );
		}
	}


	/* Create a sampled event and attach to child */

	memset(&pe,0,sizeof(struct perf_event_attr));

	pe.type=PERF_TYPE_HARDWARE;
	pe.size=sizeof(struct perf_event_attr);
	pe.config=PERF_COUNT_HW_INSTRUCTIONS;

	/* 1 million.  Tried 100k but that was too short on */
	/* faster machines, likely triggered overflow while */
	/* poll still was being handled?                    */
	pe.sample_period=1000000;
	pe.sample_type=PERF_SAMPLE_IP;
	pe.read_format=PERF_FORMAT_GROUP|PERF_FORMAT_ID;
	pe.disabled=1;
	pe.pinned=1;
	pe.exclude_kernel=1;
	pe.exclude_hv=1;
	pe.wakeup_events=1;

	arch_adjust_domain(&pe,quiet);

	fd1=perf_event_open(&pe,child,-1,-1,0);
	if (fd1<0) {
		if (!quiet) fprintf(stderr,"Error opening leader %llx\n",pe.config);
		test_fail(test_string);
	}

	our_mmap=mmap(NULL, (1+MMAP_PAGES)*getpagesize(),
			PROT_READ|PROT_WRITE, MAP_SHARED, fd1, 0);


	ioctl(fd1, PERF_EVENT_IOC_RESET, 0);
	ret=ioctl(fd1, PERF_EVENT_IOC_ENABLE,0);

	if (ret<0) {
		if (!quiet) {
			fprintf(stderr,"Error with PERF_EVENT_IOC_ENABLE of group leader: "
					"%d %s\n",errno,strerror(errno));
			test_fail(test_string);
		}
	}

	struct pollfd fds[1];
	int result;

	fds[0].fd=fd1;
	fds[0].events=POLLIN|POLLHUP|POLLNVAL|POLLERR;

	/* Restart child process */
	if (!quiet) printf("Continuing child\n");

	if ( ptrace( PTRACE_CONT, pid, NULL, NULL ) == -1 ) {
		fprintf(stderr,"Error continuing\n");
		test_fail(test_string);
	}

//	kill(child,SIGCONT);

	while(1) {
		result=poll(fds,1,100);
		if (result==0) {
			waitpid(child,&status,WNOHANG);
			if (WIFEXITED(status)) break;
			if (WIFSIGNALED(status)) {
				printf("Signalled %d!\n",WTERMSIG(status));
				break;
			}
		}
		else if (result==-1) {
			printf("Error: %s\n",strerror(errno));
			break;

		}

		if (fds[0].revents&POLLIN) count.in++;
		if (fds[0].revents&POLLHUP) count.hup++;

		if (fds[0].revents&POLLERR) {
			if (!quiet) printf("Returned error!\n");
			break;
		}

		/* On 3.18 and newer we get infinite POLLHUP	*/
		/* When the child exits	rather than an error	*/

		if (fds[0].revents&POLLHUP) {
			if (!quiet) printf("Returned HUP!\n");
			break;
		}

//		printf("%d %d\n",result,fds[0].revents);
	}

	close(fd1);

	count.total=count.in+count.hup;

	if (!quiet) {
		printf("Counts, using mmap buffer %p\n",our_mmap);
		printf("\tPOLL_IN : %d\n",count.in);
		printf("\tPOLL_OUT: %d\n",count.out);
		printf("\tPOLL_MSG: %d\n",count.msg);
		printf("\tPOLL_ERR: %d\n",count.err);
		printf("\tPOLL_PRI: %d\n",count.pri);
		printf("\tPOLL_HUP: %d\n",count.hup);
		printf("\tUNKNOWN : %d\n",count.unknown);
	}

	/* I think it is exected that we get POLL_IN for each time the */
	/* wakeup value is triggered (indicating data is ready) and we */
	/* only get POLL_HUP if the monitored process exits (hangs up) */
	/* I think older (pre-3.18?) did this differently.             */

	if (count.total==0) {
		if (!quiet) printf("No overflow events generated.\n");
		test_fail(test_string);
	}

	if (count.in!=10) {
		if (!quiet) printf("Unexpected POLL_IN interrupt.\n");
		test_fail(test_string);
	}

	if (count.hup!=1) {
		if (!quiet) {
			printf("POLL_HUP value %d, expected %d.\n",
					count.hup,1);
			printf("Expected if kernel older than 3.18, "
				"as poll() would get an error rather than "
				"POLL_HUP if the monitored process detached\n");
		}
		test_fail(test_string);
	}

	test_pass(test_string);

	return 0;
}
int main(int argc, char** argv) {

	int ret,quiet,i;

	struct perf_event_attr pe;

	struct sigaction sa;
	void *our_mmap;
	char test_string[]="Testing overflows on sibling...";

	quiet=test_quiet();

	if (!quiet) printf("This tests that overflows of siblings work.\n");

	memset(&sa, 0, sizeof(struct sigaction));
	sa.sa_sigaction = our_handler;
	sa.sa_flags = SA_SIGINFO;

	if (sigaction( SIGIO, &sa, NULL) < 0) {
		fprintf(stderr,"Error setting up signal handler\n");
		exit(1);
	}

	memset(&pe,0,sizeof(struct perf_event_attr));

	pe.type=PERF_TYPE_HARDWARE;
	pe.size=sizeof(struct perf_event_attr);
	pe.config=PERF_COUNT_HW_CPU_CYCLES;
	pe.sample_period=0;
	pe.sample_type=0;
	pe.read_format=PERF_FORMAT_GROUP|PERF_FORMAT_ID;
	pe.disabled=1;
	pe.pinned=0;
	pe.exclude_kernel=1;
	pe.exclude_hv=1;
	pe.wakeup_events=1;

	arch_adjust_domain(&pe,quiet);

	fd1=perf_event_open(&pe,0,-1,-1,0);
	if (fd1<0) {
		fprintf(stderr,"Error opening leader %llx\n",pe.config);
		test_fail(test_string);
	}

	pe.type=PERF_TYPE_HARDWARE;
	pe.config=PERF_COUNT_HW_INSTRUCTIONS;
	pe.sample_period=100000;
	pe.sample_type=PERF_SAMPLE_IP;
	pe.read_format=0;
	pe.disabled=0;
	pe.pinned=0;
	pe.exclude_kernel=1;
	pe.exclude_hv=1;

	arch_adjust_domain(&pe,quiet);

	fd2=perf_event_open(&pe,0,-1,fd1,0);
	if (fd2<0) {
		fprintf(stderr,"Error opening %llx\n",pe.config);
			test_fail(test_string);
	}

	/* large enough that threshold not a problem */
	our_mmap=mmap(NULL, (1+MMAP_PAGES)*getpagesize(),
		PROT_READ|PROT_WRITE, MAP_SHARED, fd2, 0);

	fcntl(fd2, F_SETFL, O_RDWR|O_NONBLOCK|O_ASYNC);
	fcntl(fd2, F_SETSIG, SIGIO);
	fcntl(fd2, F_SETOWN,getpid());

	ioctl(fd1, PERF_EVENT_IOC_RESET, 0);
	ioctl(fd2, PERF_EVENT_IOC_RESET, 0);

	ret=ioctl(fd1, PERF_EVENT_IOC_ENABLE,0);

	if (ret<0) {
		if (!quiet) fprintf(stderr,"Error with PERF_EVENT_IOC_ENABLE of group leader: "
			"%d %s\n",errno,strerror(errno));
		test_fail(test_string);
	}

	for(i=0;i<100;i++) {
		instructions_million();
	}

	ret=ioctl(fd1, PERF_EVENT_IOC_DISABLE,0);

	if (!quiet) printf("Count: %d %p\n",count.total,our_mmap);

	if (count.total==0) {
		if (!quiet) printf("No overflow events generated.\n");
		test_fail(test_string);
	}

	if (count.total!=1000) {
		if (!quiet) printf("Expected %d overflows, got %d.\n",
				count.total,100);
		test_fail(test_string);
	}

	close(fd1);
	close(fd2);

	test_pass(test_string);

	return 0;
}
Exemplo n.º 26
0
int main(int argc, char **argv) {

   int num_runs=100,i,read_result,result;
   long long high=0,low=0,average=0,expected=1500000;
   double error;
   struct perf_event_attr pe;

   long long count,total=0;

   quiet=test_quiet();

   if (!quiet) {
      printf("\n");

      printf("Testing a loop with %lld branches (%d times):\n",
          expected,num_runs);
   }

   memset(&pe,0,sizeof(struct perf_event_attr));

   pe.type=PERF_TYPE_HARDWARE;
   pe.size=sizeof(struct perf_event_attr);
   pe.config=PERF_COUNT_HW_BRANCH_INSTRUCTIONS;
   pe.disabled=1;
   pe.exclude_kernel=1;
   pe.exclude_hv=1;

   arch_adjust_domain(&pe,quiet);

   fd=perf_event_open(&pe,0,-1,-1,0);
   if (fd<0) {
     if (!quiet) fprintf(stderr,"Error opening leader %llx\n",pe.config);
     test_fail(test_string);
   }

   for(i=0;i<num_runs;i++) {
      ioctl(fd, PERF_EVENT_IOC_RESET, 0);
      ioctl(fd, PERF_EVENT_IOC_ENABLE, 0);

      result=branches_testcode();

      ioctl(fd, PERF_EVENT_IOC_DISABLE, 0);
      read_result=read(fd,&count,sizeof(long long));

      if (result==CODE_UNIMPLEMENTED) {
	if (!quiet) printf("\tNo test code for this architecture\n");
	 test_skip(test_string);
      }

      if (read_result!=sizeof(long long)) {
 	 if (!quiet) printf("Error extra data in read %d\n",read_result);		test_fail(test_string);
      }

      if (count>high) high=count;
      if ((low==0) || (count<low)) low=count;
      total+=count;
   }

   average=(total/num_runs);

   error=display_error(average,high,low,expected,quiet);

   if ((error > 1.0) || (error<-1.0)) {
     if (!quiet) printf("Instruction count off by more than 1%%\n");
     test_fail(test_string);
   }
   if (!quiet) printf("\n");

   test_pass( test_string );

   return 0;
}