Commit bc6ed542 authored by Florian Fischer's avatar Florian Fischer
Browse files

[bench.c] calculate used time

parent aa6b493a
......@@ -7,6 +7,7 @@
#include <stdlib.h>
#include <string.h>
#include <sys/eventfd.h>
#include <sys/resource.h>
#include "stats.h"
#include "stopwatch.h"
......@@ -19,6 +20,19 @@ unsigned iterations = 1000000;
#define BUFSIZE 64
// Calculate and return total time of this process in usec
static uint64_t calc_proc_time(struct timeval *utime, struct timeval *stime) {
uint64_t t = utime->tv_sec * 1000 * 1000 + utime->tv_usec;
t += stime->tv_sec * 1000 * 1000 + stime->tv_usec;
return t;
}
static uint64_t get_proc_time() {
struct rusage r_usage;
getrusage(RUSAGE_SELF, &r_usage);
return calc_proc_time(&r_usage.ru_utime, &r_usage.ru_stime);
}
static int create_eventfd() {
int fd = eventfd(UINT_MAX, EFD_SEMAPHORE);
if (fd == -1) {
......@@ -30,8 +44,7 @@ static int create_eventfd() {
int main(int argc, char *argv[]) {
bool print_data = false;
bool print_stats = false;
if (argc > 2)
errx(EXIT_SUCCESS, "Usage: %s [--print-data]", argv[0]);
if (argc > 2) errx(EXIT_SUCCESS, "Usage: %s [--print-data]", argv[0]);
if (argc == 2) {
if ((strcmp(argv[1], "--print-data") == 0))
......@@ -58,19 +71,23 @@ int main(int argc, char *argv[]) {
for (unsigned i = 0; i < exp_warmup; ++i) do_read(fd, &read_buf, sizeof(read_buf));
uint64_t proc_time_start = get_proc_time();
for (unsigned i = 1; i <= exp_iterations; ++i) {
do_read(fd, &read_buf, sizeof(read_buf));
nanos[i - 1] = clock_diff_nanos();
cycles[i - 1] = clock_diff_cycles();
}
uint64_t proc_time_end = get_proc_time();
uint64_t proc_time_used = proc_time_end - proc_time_start;
if (print_data) {
for (unsigned i = 0; i < exp_iterations; ++i)
printf("%lu,%lu\n", nanos[i], cycles[i]);
for (unsigned i = 0; i < exp_iterations; ++i) printf("%lu,%lu\n", nanos[i], cycles[i]);
} else if (print_stats) {
print_desc_stats("nanos", " ", nanos, exp_iterations);
print_desc_stats("cycles", " ", cycles, exp_iterations);
} else {
printf("time-spent: %lu usec ", proc_time_used);
uint64_t avg_nanos = calc_mean(nanos, exp_iterations);
uint64_t avg_cycles = calc_mean(cycles, exp_iterations);
printf("avg-nanos: %lu, avg-cycles: %lu\n", avg_nanos, avg_cycles);
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment