int main(int argc, char *argv[]) { FILE *fd = NULL; Tstate prev_state; char filename[256]; time_t rstart, rend, tnow; struct tm tm; char tag[32] = "default"; char *home = getenv("HOME"); // check for explicit tag name if (argc > 2 && argv[1][1] == 't') { strncpy(tag, argv[2], sizeof(tag)); argc -= 2; argv += 2; } if (argc < 2) { printf("usage: %s", usage); exit(EXIT_SUCCESS); } snprintf(filename, sizeof(filename), "%s/.wtimed/%s", home, tag); if ((fd = fopen(filename, "r+")) == NULL) { if ((fd = fopen(filename, "w+")) == NULL) { fprintf(stderr, "Error while opening data file '%s'.\n", filename); exit(EXIT_FAILURE); } } if (argv[1][0] != '-') { fprintf(stderr, "Unknown parameter: %s\n", argv[1]); } else { if ((prev_state = cur_state(fd)) == UNKNOWN) { fprintf(stderr, "Corrupted data file '%s'.\n", filename); exit(EXIT_FAILURE); } switch (argv[1][1]) { case 'h': printf("usage: %s", usage); break; case 'a': if (prev_state == STARTED) fprintf(stderr, "We are allready counting.\n"); else start_counting(fd); break; case 's': if (prev_state != STARTED) fprintf(stderr, "We are not counting.\n"); else stop_counting(fd); break; case 'r': if (argc == 4) { // end if (strptime(argv[3], "%d-%m-%Y", &tm) == NULL) { fprintf(stderr, "Malformated end date\n"); exit(EXIT_FAILURE); } tm.tm_min = tm.tm_sec = 59; tm.tm_hour = 23; rend = mktime(&tm); } else { rend = time(NULL); } if (argc >= 3) { // start if (strptime(argv[2], "%d-%m-%Y", &tm) == NULL) { fprintf(stderr, "Malformated start date\n"); exit(EXIT_FAILURE); } tm.tm_sec = tm.tm_min = tm.tm_hour = 0; rstart = mktime(&tm); } else { // beginning of month tnow = time(NULL); tm = *localtime(&tnow); tm.tm_mday = 1; tm.tm_sec = tm.tm_min = tm.tm_hour = 0; rstart = mktime(&tm); } if (rstart > rend) { fprintf(stderr, "Start date is further then end date.\n"); exit(EXIT_FAILURE); } print_report(fd, rstart, rend); break; case 'c': if (prev_state != STARTED) { fprintf(stderr, "We are not counting.\n"); exit(EXIT_FAILURE); } else print_elapsed(fd); break; default: fprintf(stderr, "Unknown parameter: %s\n", argv[1]); break; } } if (fd) fclose(fd); return (0); }
int main(int argc, char *argv[]) { int floatingPoint = 0; int integer = 0; int trials = 0; int arrLength = 0; int range = 0; char* endptr; struct timespec start, stop; int opt; while ((opt = getopt( argc, argv, "fin:a:r:")) != -1) { switch (opt) { case 'f': floatingPoint = 1; break; case 'i': integer = 1; break; case 'n': trials = strtol( optarg, &endptr, 10); break; case 'a': arrLength = strtol( optarg, &endptr, 10); break; case 'r': range = strtol( optarg, &endptr, 10); break; case '?': display_usage(); break; } } // printf( "float: %u, int: %u, trials: %u, array length: %u\n", floatingPoint, integer, trials, arrLength); int k = 0; int i = 0; if( integer == 1) { int a[arrLength]; int b[arrLength]; int c[arrLength]; srand(time(0)); for( i = 0; i < arrLength; i++) { a[i] = rand() % range; b[i] = rand() % range; } //"Warm-up" to remove initial memory-acces overhead intAdd( a, b, c, arrLength); //clock_gettime(CLOCK_REALTIME, &start); start_counting(ARMV6_EVENT_INSTR_EXEC, ARMV6_EVENT_CPU_CYCLES) ; //start_counting(ARMV6_EVENT_DTLB_MISS, ARMV6_EVENT_MAIN_TLB_MISS) ; //start_counting(ARMV6_EVENT_DCACHE_CACCESS, ARMV6_EVENT_DCACHE_MISS) ; for( k = trials; k > 0; k--) { intAdd( a, b, c, arrLength); } stop_counting() ; if (create_result_file("output.dat") == 0) { printf ("Error cannot create file"); exit( EXIT_FAILURE ) ; } fprintf(result_file,"Performance Monitor events\n") ; print_counts(result_file) ; //clock_gettime(CLOCK_REALTIME, &stop); double diff = ( stop.tv_sec - start.tv_sec ) + ( stop.tv_nsec - start.tv_nsec ) / 1E9; printf( "%u, %u, %lf\n", trials, arrLength, diff); }else if ( floatingPoint == 1) { double a[arrLength]; double b[arrLength]; double c[arrLength]; srand(time(0)); for( i = 0; i < arrLength; i++) { a[i] = (double)rand() / (double)(RAND_MAX/range); b[i] = (double)rand() / (double)(RAND_MAX/range); } /*"Warm-up" to remove initial memory-acces overhead fpAdd( a, b, c, arrLength); clock_gettime(CLOCK_REALTIME, &start); for( k = trials; k > 0; k--) { fpAdd( a, b, c, arrLength); } clock_gettime(CLOCK_REALTIME, &stop); */ double diff = ( stop.tv_sec - start.tv_sec ) + ( stop.tv_nsec - start.tv_nsec ) / 1E9; printf( "%u, %u, %lf\n", trials, arrLength, diff); } return 0; }