-
Notifications
You must be signed in to change notification settings - Fork 4
/
utils.c
67 lines (53 loc) · 1.48 KB
/
utils.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include "utils.h"
#define _POSIX_C_SOURCE 199309L
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
void *progress_function(void *arg) {
double elapsed_time;
size_t iterations_completed;
size_t iterations_total;
const char *lib_name;
uint8_t percent_completed;
Progress *progress = arg;
iterations_total = progress->iterations_total;
lib_name = progress->lib_name;
do {
sleep(1);
iterations_completed = progress->iterations_completed;
elapsed_time = seconds() - progress->start_time;
if ((uint64_t) elapsed_time % 10 == 0){
percent_completed = 100 * iterations_completed / iterations_total;
printf("[%s] Iteration %zu/%zu (%hhu%%), elapsed time: %f\n", lib_name, iterations_completed, iterations_total, percent_completed, elapsed_time);
}
} while (iterations_completed < iterations_total - 1);
}
double seconds() {
struct timespec ts;
if (clock_gettime(CLOCK_MONOTONIC_RAW, &ts) == -1) {
printf("seconds(): clock_gettime() failed!\n");
return 0.0;
}
return (double)ts.tv_sec + (double)ts.tv_nsec / 1000000000.0;
}
bool validate(const size_t size, const void *buf_1, const void *buf_2) {
if (!buf_1 || !buf_2) {
// Return true if both buffers are NULL
return (!buf_1 && !buf_2);
}
return memcmp(buf_1, buf_2, size) == 0;
}
void *zero_malloc(const size_t size) {
if (!size) {
return NULL;
}
void *ptr = malloc(size);
if (!ptr) {
return NULL;
}
memset(ptr, 0, size);
return ptr;
}