示例#1
0
文件: test_malloc.cpp 项目: CCJY/ACE
static int
malloc_recurse (int count)
{
  static char default_char = 0;

  if (count <= 0)
    {
      if (Options::instance ()->debug ())
        {
          // Note that you'll need to #define ACE_HAS_MALLOC_STATS in
          // the main ACE config.h file and remake ACE to enable this.
          ACE_MALLOC_STATS (Malloc::instance ()->print_stats ());
        }
    }
  else
    {
      int alloc_size = gen_size ();
      void *ptr = Malloc::instance ()->malloc (alloc_size);

      if (ptr == 0)
        ACE_ERROR ((LM_ERROR,
                    "(%P|%t) *** malloc of size %d failed, %p\n%a",
                   "malloc",
                    alloc_size));
      else
        {
          ACE_OS::memset (ptr, default_char++, alloc_size);

          if (Options::instance ()->debug ())
            ACE_DEBUG ((LM_INFO,
                        "(%P|%t) %u (alloc), size = %d\n",
                        ptr,
                        alloc_size));

          // Call ourselves recursively
          malloc_recurse (count - 1);

          if (Options::instance ()->debug ())
            ACE_DEBUG ((LM_INFO,
                        "(%P|%t) %u (free), size = %d\n",
                        ptr,
                        alloc_size));
          Malloc::instance ()->free (ptr);
        }
    }

  return 0;
}
示例#2
0
// the test program with defined testing paramaters
int main(int argc, char** argv){
	clock_t start, stop;
	double cpu_time;
	start = clock();
	time_t t;
	FILE* f = fopen("log","w");
	int paramaters[6] = {ntrials, pctget, pctlarge, small_limit, large_limit, -1};
	setup(paramaters, argc, argv);
	if(paramaters[5] < 0) srand((unsigned)time(&t));
	else srand((unsigned)paramaters[5]);
	int i;
	
	for(i=0; i<paramaters[0]; i++){
		int function_choice = makechoice(paramaters[1]);
		int size_choice = makechoice(paramaters[2]);
		int small = paramaters[3];
		int large = paramaters[4];
		if(function_choice){
			int size = gen_size(size_choice, small, large);
			getmem(size);
		}else{
			random_free();
		}
	}
	print_heap(f);
	uintptr_t *total_size = (uintptr_t *)malloc(sizeof(uintptr_t));
	uintptr_t *total_free = (uintptr_t *)malloc(sizeof(uintptr_t));
	uintptr_t *n_free_blocks = (uintptr_t *)malloc(sizeof(uintptr_t));
	// get and print the mem stats to file 
	get_mem_stats(total_size, total_free, n_free_blocks);
	stop = clock();
    cpu_time = ((double) (stop - start)) / CLOCKS_PER_SEC;
    fprintf(f,"cpu_time: %f  seconds\n",cpu_time);
    fprintf(f,"total_size: %lu bytes\n",*total_size);
//    printf("n_free_blocks: %lu blocks\n",*n_free_blocks);
    fprintf(f,"n_free_blocks: %lu blocks\n",*n_free_blocks);
    fprintf(f,"total_free size :%lu bytes\n",*total_free);
	fclose(f);
	free(total_size);
	free(total_free);
	free(n_free_blocks);
	return 0;
}
示例#3
0
static void
raidtest_genfile(int argc, char *argv[])
{
	uintmax_t i, nreqs, mediasize, nsectors, nbytes, nrreqs, nwreqs;
	unsigned secsize, maxsec;
	const char *file = NULL;
	struct ioreq iorq;
	int ch, fd, flags, rdonly, wronly;

	nreqs = 0;
	mediasize = 0;
	secsize = 512;
	rdonly = wronly = 0;
	flags = O_WRONLY | O_CREAT | O_EXCL | O_TRUNC;
	while ((ch = getopt(argc, argv, "fn:rs:S:w")) != -1) {
		switch (ch) {
		case 'f':
			flags &= ~O_EXCL;
			break;
		case 'n':
			errno = 0;
			nreqs = strtoumax(optarg, NULL, 0);
			if (errno != 0) {
				err(EXIT_FAILURE,
				    "Invalid value for '%c' argument.", ch);
			}
			break;
		case 'r':
			rdonly = 1;
			break;
		case 's':
			errno = 0;
			mediasize = strtoumax(optarg, NULL, 0);
			if (errno != 0) {
				err(EXIT_FAILURE,
				    "Invalid value for '%c' argument.", ch);
			}
			break;
		case 'S':
			errno = 0;
			secsize = strtoul(optarg, NULL, 0);
			if (errno != 0) {
				err(EXIT_FAILURE,
				    "Invalid value for '%c' argument.", ch);
			}
			break;
		case 'w':
			wronly = 1;
			break;
		default:
			usage();
		}
	}
	argc -= optind;
	argv += optind;
	if (nreqs == 0)
		errx(EXIT_FAILURE, "Option '%c' not specified.", 'n');
	if (mediasize == 0)
		errx(EXIT_FAILURE, "Option '%c' not specified.", 's');
	if (rdonly && wronly) {
		errx(EXIT_FAILURE, "Both '%c' and '%c' options were specified.",
		    'r', 'w');
	}
	if (argc == 0)
		file = DEFAULT_DATA_FILE;
	else if (argc == 1)
		file = argv[0];
	else
		usage();
	fd = open(file, flags, 0644);
	if (fd < 0)
		err(EXIT_FAILURE, "Cannot create '%s' file", file);
	nsectors = mediasize / secsize;
	nbytes = nrreqs = nwreqs = 0;
	for (i = 0; i < nreqs; i++) {
		/* Generate I/O request length. */
		iorq.iorq_length = gen_size(secsize);
		/* Generate I/O request offset. */
		maxsec = nsectors - (iorq.iorq_length / secsize);
		iorq.iorq_offset = (arc4random() % maxsec) * secsize;
		/* Generate I/O request type. */
		if (rdonly)
			iorq.iorq_type = IO_TYPE_READ;
		else if (wronly)
			iorq.iorq_type = IO_TYPE_WRITE;
		else
			iorq.iorq_type = arc4random() % 2;
		nbytes += iorq.iorq_length;
		switch (iorq.iorq_type) {
		case IO_TYPE_READ:
			nrreqs++; 
			break;
		case IO_TYPE_WRITE:
			nwreqs++; 
			break;
		}
		if (write_ioreq(fd, &iorq) != 0) {
			unlink(file);
			err(EXIT_FAILURE, "Error while writing");
		}
	}
	printf("File %s generated.\n", file);
	printf("Number of READ requests: %ju.\n", nrreqs);
	printf("Number of WRITE requests: %ju.\n", nwreqs);
	printf("Number of bytes to transmit: %ju.\n", nbytes);
}