int main(int argc, char *argv[]) { int64_t sum = 0; unsigned finish = 5; int prio = 0; int num_processors; int acc_type = READ; int opt; cpu_set_t cmask; int iterations = 0; int i; struct sched_param param; /* * get command line options */ while ((opt = getopt(argc, argv, "m:a:n:t:c:i:p:r:f:l:xh")) != -1) { switch (opt) { case 'm': /* set memory size */ g_mem_size = 1024 * strtol(optarg, NULL, 0); break; case 'a': /* set access type */ if (!strcmp(optarg, "read")) acc_type = READ; else if (!strcmp(optarg, "write")) acc_type = WRITE; else exit(1); break; case 't': /* set time in secs to run */ finish = strtol(optarg, NULL, 0); break; case 'c': /* set CPU affinity */ cpuid = strtol(optarg, NULL, 0); num_processors = sysconf(_SC_NPROCESSORS_CONF); CPU_ZERO(&cmask); CPU_SET(cpuid % num_processors, &cmask); if (sched_setaffinity(0, num_processors, &cmask) < 0) perror("error"); else fprintf(stderr, "assigned to cpu %d\n", cpuid); break; case 'r': prio = strtol(optarg, NULL, 0); param.sched_priority = prio; /* 1(low)- 99(high) for SCHED_FIFO or SCHED_RR 0 for SCHED_OTHER or SCHED_BATCH */ if(sched_setscheduler(0, SCHED_FIFO, ¶m) == -1) { perror("sched_setscheduler failed"); } break; case 'p': /* set priority */ prio = strtol(optarg, NULL, 0); if (setpriority(PRIO_PROCESS, 0, prio) < 0) perror("error"); else fprintf(stderr, "assigned priority %d\n", prio); break; case 'i': /* iterations */ iterations = strtol(optarg, NULL, 0); break; case 'h': usage(argc, argv); break; } } /* * allocate contiguous region of memory */ g_mem_ptr = (int *)malloc(g_mem_size); memset((char *)g_mem_ptr, 1, g_mem_size); for (i = 0; i < g_mem_size / sizeof(int); i++) g_mem_ptr[i] = i; /* print experiment info before starting */ printf("memsize=%d KB, type=%s, cpuid=%d\n", g_mem_size/1024, ((acc_type==READ) ?"read": "write"), cpuid); printf("stop at %d\n", finish); /* set signals to terminate once time has been reached */ signal(SIGINT, &quit); if (finish > 0) { signal(SIGALRM, &quit); alarm(finish); } /* * actual memory access */ g_start = get_usecs(); for (i=0;; i++) { switch (acc_type) { case READ: sum += bench_read(); break; case WRITE: sum += bench_write(); break; } if (iterations > 0 && i >= iterations) break; } printf("total sum = %ld\n", (long)sum); quit(0); return 0; }
int main(int argc, char** argv){ printf("testing disk performance\n"); int i; struct opts * options = (struct opts*) malloc(sizeof(struct opts)); options->read = 0; options->threaded = 0; options->bsize = 1; long s; for (i =1; i < argc; i++){ if (argv[i][0] == '-'){ switch(argv[i][1]){ case 'b': s = 1; switch (argv[i][2]){ case 'G': s = s << 10; case 'M': s = s << 10; case 'K': s = s << 10; case 'B': s = s; break; default: fprintf(stderr,"invalid block size\n%s\n", usage); exit(1); } options->bsize = s; break; case 'r': options->read |= 1; if (argv[i][2] == 't') options->threaded |= 1; break; case 't': options->threaded |= 1; if(argv[i][2] == 'r') options->read |= 1; break; case 'h': fprintf(stdout, "%s\n", usage); exit(0); default: fprintf(stderr, "unrecognized option\n%s\n", usage); exit(1); } } } printf("Block size: %lu B\n%s mode\nThreaded: %s\n", options->bsize, options->read ? "Read" : "Write", options->threaded ? "Yes" : "No"); double time; double ratio = BENCH / GB; if (options->read){ time = bench_read(options); printf("%s at %f GB/s\n", "Read", 1000/time*ratio); } else{ time = bench_write(options); printf("%s at %f MB/s\n", "Write", 1000*1024/time*ratio); } printf("%s,%ld,%c,%c,%f\n", NAME, options->bsize, options->read ? 'R' : 'W', options->threaded ? 'Y' : 'N', options->threaded ? (1000/time*BENCH*2) : (1000/time*BENCH)); return 0; }
int main(int argc, char *argv[]) { long sum = 0; unsigned finish = 5; int prio = 0; int num_processors; int acc_type = READ; int opt; cpu_set_t cmask; int use_mmap = 0; int iterations = 0; int i; /* * get command line options */ while ((opt = getopt(argc, argv, "m:a:n:t:c:i:p:f:l:xh")) != -1) { switch (opt) { case 'm': /* set memory size */ g_mem_size = 1024 * strtol(optarg, NULL, 0); break; case 'a': /* set access type */ if (!strcmp(optarg, "read")) acc_type = READ; else if (!strcmp(optarg, "write")) acc_type = WRITE; else if (!strcmp(optarg, "rdwr")) acc_type = RDWR; else exit(1); break; case 'n': /* set access pattern */ /* sequential */ if( strcmp(optarg,"Seq") == 0 ) { g_indx = 0; g_next = (CACHE_LINE_SIZE/4); } /* same bank */ #if P4080_MCTRL_INTRV_NONE else if( strcmp(optarg,"Row") == 0 ) { g_indx = 0; g_next = (CACHE_LINE_SIZE/4) * 1024; } /* diff bank */ else if( strcmp(optarg,"Bank") == 0 ) { g_indx = 128*(CACHE_LINE_SIZE/4); g_next = (CACHE_LINE_SIZE/4) * 1024; } #elif P4080_MCTRL_INTRV_CLCS else if( strcmp(optarg,"Row") == 0 ) { g_indx = 0; g_next = (CACHE_LINE_SIZE/4) * 1024 * 8;// 2^19 } /* diff bank */ else if( strcmp(optarg,"Bank") == 0 ) { g_indx = 256*(CACHE_LINE_SIZE/4); // 2^16 g_next = (CACHE_LINE_SIZE/4) * 1024 * 8;// 2^19 } #endif else exit(1); break; case 't': /* set time in secs to run */ finish = strtol(optarg, NULL, 0); break; case 'c': /* set CPU affinity */ cpuid = strtol(optarg, NULL, 0); num_processors = sysconf(_SC_NPROCESSORS_CONF); CPU_ZERO(&cmask); CPU_SET(cpuid % num_processors, &cmask); if (sched_setaffinity(0, num_processors, &cmask) < 0) perror("error"); else fprintf(stderr, "assigned to cpu %d\n", cpuid); break; case 'p': /* set priority */ prio = strtol(optarg, NULL, 0); if (setpriority(PRIO_PROCESS, 0, prio) < 0) perror("error"); else fprintf(stderr, "assigned priority %d\n", prio); break; case 'i': /* iterations */ iterations = strtol(optarg, NULL, 0); break; case 'l': /* set label */ g_label = strdup(optarg); break; case 'f': /* set file descriptor */ g_fd = fopen(optarg, "a+"); if (g_fd == NULL) perror("error"); break; case 'x': /* mapping to /dev/mem. !! DANGEROUS !! */ use_mmap = 1; break; case 'h': usage(argc, argv); break; } } g_indx *= cpuid; /* * allocate contiguous region of memory */ if (use_mmap) { /* open /dev/mem for accessing memory in physical addr. */ int fd = -1; unsigned long offset; fprintf(stderr, "Use mmap| g_indx: 0x%x g_next: 0x%x\n", g_indx, g_next); fd = open("/dev/mem", O_RDWR | O_SYNC); if(fd == -1) { fprintf(stderr, "ERROR Opening /dev/mem\n"); exit(1); } /* offset variable is used to allocate each cpu to a different offset from each other */ offset = ADDR_2ND_RANK; /* + cpuid*g_mem_size;*/ fprintf(stderr, "offset: %p\n", (void *)offset); /* use mmap to allocate each cpu to the specific address in memory */ g_mem_ptr = (int *)mmap(NULL, g_mem_size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, offset); if(g_mem_ptr == NULL) { fprintf(stderr, "could not allocate memarea"); exit(1); } fprintf(stderr, "mmap was successful: addr=%p\n", g_mem_ptr); } else { printf("Use standard malloc\n"); g_mem_ptr = (int *)malloc(g_mem_size); } for (i = 0; i < g_mem_size / sizeof(int); i++) g_mem_ptr[i] = i; memset((char *)g_mem_ptr, 1, g_mem_size); fprintf(stderr, "VADDR: %p-%p\n", (char *)g_mem_ptr, (char *)g_mem_ptr + g_mem_size); /* print experiment info before starting */ printf("memsize=%d KB, type=%s, cpuid=%d\n", g_mem_size/1024, ((acc_type==READ) ?"read": (acc_type==WRITE)? "write" : (acc_type==RDWR) ? "rdwr" : "worst"), cpuid); printf("stop at %d\n", finish); /* set signals to terminate once time has been reached */ signal(SIGINT, &quit); if (finish > 0) { signal(SIGALRM, &quit); alarm(finish); } /* * actual memory access */ g_start = get_usecs(); for (i=0;; i++) { switch (acc_type) { case READ: sum += bench_read(); break; case WRITE: sum += bench_write(); break; case RDWR: sum += bench_rdwr(); break; } if (iterations > 0 && i >= iterations) break; } printf("total sum = %ld\n", sum); quit(0); }