-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathioping.c
137 lines (115 loc) · 4.16 KB
/
ioping.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <time.h>
#include <string.h>
#include <math.h>
#include "common.h"
static void* aligned_buffer;
uint32_t count = 4, interval = 1;
uint64_t pinglen = 1024*1024; // @todo customizable
char *pingPath = "/var/tmp/ping.tmp";
void printHelp() {
printf("naive-ioping - v1.0 by Hendrik 'Sharky' Schumann - sharky@sharky.pw\n"
"usage:\n"
" ioping [-c --count count (default: 4)] [-i --interval interval (default: 1)]\n"
" [-p --path path (default: /var/tmp/ping.tmp)]\n"
" [-s --size size (in power of two, default: 20 (=1MB))]\n");
}
int main(int argc, char** argv) {
if(argc > 1) {
for(int i = 1; i < argc; i++) {
if(strcmp(argv[i], "--help") == 0 ||
strcmp(argv[i], "-h") == 0) {
printHelp();
return 0;
}
if(strcmp(argv[i], "--count") == 0 ||
strcmp(argv[i], "-c") == 0) {
if(argc <= i+1) {
continue;
}
count = atoi(argv[i+1]);
if(count <= 0) count = 1;
++i;
continue;
}
if(strcmp(argv[i], "--interval") == 0 ||
strcmp(argv[i], "-i") == 0) {
if(argc <= i+1) {
continue;
}
interval = atoi(argv[i+1]);
if(interval < 0) interval = 0;
++i;
continue;
}
if(strcmp(argv[i], "--size") == 0 ||
strcmp(argv[i], "-s") == 0) {
if(argc <= i+1) {
continue;
}
pinglen = atoi(argv[i+1]);
if(pinglen > 30) pinglen = 30;
pinglen = (uint64_t)pow(2, pinglen);
++i;
continue;
}
if(strcmp(argv[i], "--path") == 0 ||
strcmp(argv[i], "-p") == 0) {
if(argc <= i+1) {
continue;
}
pingPath = argv[i+1];
++i;
continue;
}
}
}
if(count == 1) {
printf("Give me a ping, Vasili. One ping only, please.\n");
} else {
printf("%u ioping(s)\n", count);
}
printf("pinglen is %ld bytes\n", pinglen);
printf("Path to file used for ioping: %s\n", pingPath);
// check if there already is a file at pingPath, we don't want to overwrite anything important
int accessTest = access(pingPath, F_OK);
if(accessTest == 0) {
printf("The file seems to exist! Aborting ...\n");
exit(1);
}
if( posix_memalign(&aligned_buffer, pinglen, pinglen) != 0) {
fprintf(stderr, "Unable to align buffer!\nerrno: %d %s\n", errno, strerror(errno));
exit(1);
}
size_t randomLen = get_random_data(aligned_buffer, pinglen);
if(randomLen != pinglen) {
printf("get_random_data() returned less than expected! Aborting ...\n");
exit(1);
}
double total = 0, maxTime = -1, minTime = (unsigned int)~0;
timediff_t* runs[count];
for(int i = 0; i < count; ++i) {
timediff_t *r = runs[i] = do_ping((const void*)aligned_buffer, pinglen, pingPath);
if(r == NULL) {
fprintf(stderr, "ioping returned NULL?!\n");
exit(1);
}
double val = (r->diffsec * 1e9) + r->diffnano;
if(val > maxTime) maxTime = val;
if(val < minTime) minTime = val;
printf("%u bytes from %s, seq=%d, time=%lf ms\n", randomLen, pingPath, i + 1, ((r->diffsec * 1e9) + r->diffnano)*1e-9);
total += (r->diffsec * 1e9) + r->diffnano;
if(count > 1) sleep(interval);
}
printf("\n--- ioping statistics ---\n");
double averageTime = (total / (double)count);
printf("%d iopings written, %ld bytes written, time %lf ms\n", count, pinglen * count, total * 1e-9);
printf("min/avg/max %lf/%lf/%lf ms\n", minTime * 1e-9, averageTime * 1e-9, maxTime * 1e-9);
for(int i = 0; i < count; ++i) {
free(runs[i]);
}
free(aligned_buffer);
return 0;
}