add unit conversion and formatting from old code. remove hungarian gobbledeygook
Этот коммит содержится в:
родитель
d18ce8e918
Коммит
e92877004e
11
src/Makefile
11
src/Makefile
@ -1,5 +1,5 @@
|
||||
CFLAGS=-g -Wall
|
||||
OBJS=main.o timer.o net.o tcp_window_size.o
|
||||
OBJS=main.o timer.o net.o tcp_window_size.o units.o
|
||||
|
||||
all: iperf
|
||||
|
||||
@ -7,11 +7,16 @@ iperf: $(OBJS)
|
||||
$(CC) -pthread -o iperf $(OBJS)
|
||||
#$(CC) -pg -pthread -o iperf-profile $(OBJS)
|
||||
|
||||
test: t_timer
|
||||
test: t_timer t_units
|
||||
./t_timer
|
||||
./t_units
|
||||
|
||||
t_timer: timer.o t_timer.o
|
||||
$(CC) -o t_timer timer.o t_timer.o
|
||||
|
||||
t_units: units.o t_units.o
|
||||
$(CC) -o t_units units.o t_units.o
|
||||
|
||||
|
||||
clean:
|
||||
rm -f *.o iperf iperf-profile t_timer
|
||||
rm -f *.o iperf iperf-profile t_timer t_units
|
||||
|
2
src/iperf.h
Обычный файл
2
src/iperf.h
Обычный файл
@ -0,0 +1,2 @@
|
||||
|
||||
typedef uint64_t iperf_size_t;
|
19
src/main.c
19
src/main.c
@ -27,8 +27,10 @@
|
||||
|
||||
#include <sys/time.h>
|
||||
|
||||
#include "iperf.h"
|
||||
#include "timer.h"
|
||||
#include "net.h"
|
||||
#include "units.h"
|
||||
#include "tcp_window_size.h"
|
||||
|
||||
enum {
|
||||
@ -93,11 +95,11 @@ struct settings
|
||||
char *client;
|
||||
int port;
|
||||
int sock;
|
||||
uint64_t bw;
|
||||
iperf_size_t bw;
|
||||
int duration;
|
||||
int threads;
|
||||
long int bufsize;
|
||||
long int window;
|
||||
iperf_size_t bufsize;
|
||||
iperf_size_t window;
|
||||
struct sockaddr_in sa;
|
||||
};
|
||||
|
||||
@ -424,7 +426,7 @@ server()
|
||||
struct stream *sp;
|
||||
struct sockaddr_in sa_peer;
|
||||
socklen_t len;
|
||||
char buf[settings.bufsize];
|
||||
char buf[settings.bufsize], ubuf[11];
|
||||
|
||||
s = netannounce(settings.proto, NULL, settings.port);
|
||||
if(s < 0)
|
||||
@ -441,8 +443,9 @@ server()
|
||||
if((x = getsock_tcp_windowsize(s, SO_RCVBUF)) < 0)
|
||||
perror("SO_RCVBUF");
|
||||
|
||||
printf("%s: %d\n",
|
||||
settings.proto == Ptcp ? "TCP window size" : "UDP buffer size", x);
|
||||
unit_snprintf(ubuf, 11, (double) x, 'A');
|
||||
printf("%s: %s\n",
|
||||
settings.proto == Ptcp ? "TCP window size" : "UDP buffer size", ubuf);
|
||||
printf("-----------------------------------------------------------\n");
|
||||
|
||||
len = sizeof sa_peer;
|
||||
@ -508,13 +511,13 @@ main(int argc, char **argv)
|
||||
settings.threads = atoi(optarg);
|
||||
break;
|
||||
case 'b':
|
||||
settings.bw = atoll(optarg);
|
||||
settings.bw = unit_atoi(optarg);
|
||||
break;
|
||||
case 'l':
|
||||
settings.bufsize = atol(optarg);
|
||||
break;
|
||||
case 'w':
|
||||
settings.window = atoi(optarg);
|
||||
settings.window = unit_atoi(optarg);
|
||||
break;
|
||||
}
|
||||
|
||||
|
@ -7,16 +7,16 @@
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
struct timer *tp;
|
||||
tp = new_timer(10);
|
||||
tp = new_timer(3, 0);
|
||||
|
||||
sleep(5);
|
||||
sleep(2);
|
||||
|
||||
if(tp->expired(tp)) {
|
||||
printf("timer should not have expired\n");
|
||||
exit(-1);
|
||||
}
|
||||
|
||||
sleep(5);
|
||||
sleep(1);
|
||||
|
||||
if(!tp->expired(tp)) {
|
||||
printf("timer should have expired\n");
|
||||
|
62
src/t_units.c
Обычный файл
62
src/t_units.c
Обычный файл
@ -0,0 +1,62 @@
|
||||
#include <assert.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "iperf.h"
|
||||
#include "units.h"
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
iperf_size_t llu;
|
||||
double d;
|
||||
char s[11];
|
||||
|
||||
assert(1024.0 * 0.5 == unit_atof("0.5K"));
|
||||
assert(1024.0 == unit_atof("1K"));
|
||||
assert(1024.0*1024.0 == unit_atof("1M"));
|
||||
assert(4.0*1024.0*1024.0*1024.0 == unit_atof("4G"));
|
||||
|
||||
assert(1000.0 * 0.5 == unit_atof("0.5k"));
|
||||
assert(1000.0 == unit_atof("1k"));
|
||||
assert(1000.0*1000.0 == unit_atof("1m"));
|
||||
assert(4.0*1000.0*1000.0*1000.0 == unit_atof("4g"));
|
||||
|
||||
|
||||
assert(1024 * 0.5 == unit_atoi("0.5K"));
|
||||
assert(1024 == unit_atoi("1K"));
|
||||
assert(1024*1024 == unit_atoi("1M"));
|
||||
d = 4.0* 1024* 1024* 1024;
|
||||
llu = (iperf_size_t) d;
|
||||
assert(llu == unit_atoi("4G"));
|
||||
|
||||
assert(1000 * 0.5 == unit_atoi("0.5k"));
|
||||
assert(1000 == unit_atoi("1k"));
|
||||
assert(1000*1000 == unit_atoi("1m"));
|
||||
d = 4.0 * 1000* 1000* 1000;
|
||||
llu = (iperf_size_t) d;
|
||||
assert(llu == unit_atoi("4g"));
|
||||
|
||||
unit_snprintf(s, 11, 1024.0, 'A');
|
||||
assert( strncmp(s, "1.00 KByte", 11) == 0);
|
||||
|
||||
unit_snprintf(s, 11, 1024.0 * 1024.0, 'A');
|
||||
assert( strncmp(s, "1.00 MByte", 11) == 0);
|
||||
|
||||
unit_snprintf(s, 11, 1000.0, 'k');
|
||||
assert( strncmp(s, "8.00 Kbit", 11) == 0);
|
||||
|
||||
unit_snprintf(s, 11, 1000.0 * 1000.0, 'a');
|
||||
assert( strncmp(s, "8.00 Mbit", 11) == 0);
|
||||
|
||||
d = 4.0 * 1024* 1024* 1024;
|
||||
unit_snprintf(s, 11, d, 'A');
|
||||
assert( strncmp(s, "4.00 GByte", 11) == 0);
|
||||
|
||||
unit_snprintf(s, 11, d, 'a');
|
||||
assert( strncmp(s, "34.4 Gbit", 11) == 0);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
152
src/units.c
152
src/units.c
@ -51,95 +51,99 @@
|
||||
* input and output numbers, converting with kilo, mega, giga
|
||||
* ------------------------------------------------------------------- */
|
||||
|
||||
#include "headers.h"
|
||||
#include "util.h"
|
||||
#include <stdio.h>
|
||||
#include <assert.h>
|
||||
#include <ctype.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#include "iperf.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
const long kKilo_to_Unit = 1024;
|
||||
const long kMega_to_Unit = 1024 * 1024;
|
||||
const long kGiga_to_Unit = 1024 * 1024 * 1024;
|
||||
const long KILO_UNIT = 1024;
|
||||
const long MEGA_UNIT = 1024 * 1024;
|
||||
const long GIGA_UNIT = 1024 * 1024 * 1024;
|
||||
|
||||
const long kkilo_to_Unit = 1000;
|
||||
const long kmega_to_Unit = 1000 * 1000;
|
||||
const long kgiga_to_Unit = 1000 * 1000 * 1000;
|
||||
const long KILO_UNIT_SI = 1000;
|
||||
const long MEGA_UNIT_SI = 1000 * 1000;
|
||||
const long GIGA_UNIT_SI = 1000 * 1000 * 1000;
|
||||
|
||||
/* -------------------------------------------------------------------
|
||||
* byte_atof
|
||||
* unit_atof
|
||||
*
|
||||
* Given a string of form #x where # is a number and x is a format
|
||||
* character listed below, this returns the interpreted integer.
|
||||
* Gg, Mm, Kk are giga, mega, kilo respectively
|
||||
* ------------------------------------------------------------------- */
|
||||
|
||||
double byte_atof( const char *inString ) {
|
||||
double theNum;
|
||||
double unit_atof( const char *s ) {
|
||||
double n;
|
||||
char suffix = '\0';
|
||||
|
||||
assert( inString != NULL );
|
||||
assert( s != NULL );
|
||||
|
||||
/* scan the number and any suffices */
|
||||
sscanf( inString, "%lf%c", &theNum, &suffix );
|
||||
sscanf( s, "%lf%c", &n, &suffix );
|
||||
|
||||
/* convert according to [Gg Mm Kk] */
|
||||
switch ( suffix ) {
|
||||
case 'G': theNum *= kGiga_to_Unit; break;
|
||||
case 'M': theNum *= kMega_to_Unit; break;
|
||||
case 'K': theNum *= kKilo_to_Unit; break;
|
||||
case 'g': theNum *= kgiga_to_Unit; break;
|
||||
case 'm': theNum *= kmega_to_Unit; break;
|
||||
case 'k': theNum *= kkilo_to_Unit; break;
|
||||
case 'G': n *= GIGA_UNIT; break;
|
||||
case 'M': n *= MEGA_UNIT; break;
|
||||
case 'K': n *= KILO_UNIT; break;
|
||||
case 'g': n *= GIGA_UNIT_SI; break;
|
||||
case 'm': n *= MEGA_UNIT_SI; break;
|
||||
case 'k': n *= KILO_UNIT_SI; break;
|
||||
default: break;
|
||||
}
|
||||
return theNum;
|
||||
} /* end byte_atof */
|
||||
return n;
|
||||
} /* end unit_atof */
|
||||
|
||||
/* -------------------------------------------------------------------
|
||||
* byte_atoi
|
||||
* unit_atoi
|
||||
*
|
||||
* Given a string of form #x where # is a number and x is a format
|
||||
* character listed below, this returns the interpreted integer.
|
||||
* Gg, Mm, Kk are giga, mega, kilo respectively
|
||||
* ------------------------------------------------------------------- */
|
||||
|
||||
max_size_t byte_atoi( const char *inString ) {
|
||||
double theNum;
|
||||
iperf_size_t unit_atoi( const char *s ) {
|
||||
double n;
|
||||
char suffix = '\0';
|
||||
|
||||
assert( inString != NULL );
|
||||
assert( s != NULL );
|
||||
|
||||
/* scan the number and any suffices */
|
||||
sscanf( inString, "%lf%c", &theNum, &suffix );
|
||||
sscanf( s, "%lf%c", &n, &suffix );
|
||||
|
||||
/* convert according to [Gg Mm Kk] */
|
||||
switch ( suffix ) {
|
||||
case 'G': theNum *= kGiga_to_Unit; break;
|
||||
case 'M': theNum *= kMega_to_Unit; break;
|
||||
case 'K': theNum *= kKilo_to_Unit; break;
|
||||
case 'g': theNum *= kgiga_to_Unit; break;
|
||||
case 'm': theNum *= kmega_to_Unit; break;
|
||||
case 'k': theNum *= kkilo_to_Unit; break;
|
||||
case 'G': n *= GIGA_UNIT; break;
|
||||
case 'M': n *= MEGA_UNIT; break;
|
||||
case 'K': n *= KILO_UNIT; break;
|
||||
case 'g': n *= GIGA_UNIT_SI; break;
|
||||
case 'm': n *= MEGA_UNIT_SI; break;
|
||||
case 'k': n *= KILO_UNIT_SI; break;
|
||||
default: break;
|
||||
}
|
||||
return (max_size_t) theNum;
|
||||
} /* end byte_atof */
|
||||
return (iperf_size_t) n;
|
||||
} /* end unit_atof */
|
||||
|
||||
/* -------------------------------------------------------------------
|
||||
* constants for byte_printf
|
||||
* ------------------------------------------------------------------- */
|
||||
|
||||
/* used as indices into kConversion[], kLabel_Byte[], and kLabel_bit[] */
|
||||
/* used as indices into conversion_bytes[], label_byte[], and label_bit[] */
|
||||
enum {
|
||||
kConv_Unit,
|
||||
kConv_Kilo,
|
||||
kConv_Mega,
|
||||
kConv_Giga
|
||||
UNIT_CONV,
|
||||
KILO_CONV,
|
||||
MEGA_CONV,
|
||||
GIGA_CONV
|
||||
};
|
||||
|
||||
/* factor to multiply the number by */
|
||||
const double kConversion[] =
|
||||
const double conversion_bytes[] =
|
||||
{
|
||||
1.0, /* unit */
|
||||
1.0 / 1024, /* kilo */
|
||||
@ -148,7 +152,7 @@ const double kConversion[] =
|
||||
};
|
||||
|
||||
/* factor to multiply the number by for bits*/
|
||||
const double kConversionForBits[] =
|
||||
const double conversion_bits[] =
|
||||
{
|
||||
1.0, /* unit */
|
||||
1.0 / 1000, /* kilo */
|
||||
@ -158,7 +162,7 @@ const double kConversionForBits[] =
|
||||
|
||||
|
||||
/* labels for Byte formats [KMG] */
|
||||
const char* kLabel_Byte[] =
|
||||
const char* label_byte[] =
|
||||
{
|
||||
"Byte",
|
||||
"KByte",
|
||||
@ -167,7 +171,7 @@ const char* kLabel_Byte[] =
|
||||
};
|
||||
|
||||
/* labels for bit formats [kmg] */
|
||||
const char* kLabel_bit[] =
|
||||
const char* label_bit[] =
|
||||
{
|
||||
"bit",
|
||||
"Kbit",
|
||||
@ -176,18 +180,18 @@ const char* kLabel_bit[] =
|
||||
};
|
||||
|
||||
/* -------------------------------------------------------------------
|
||||
* byte_snprintf
|
||||
* unit_snprintf
|
||||
*
|
||||
* Given a number in bytes and a format, converts the number and
|
||||
* prints it out with a bits or bytes label.
|
||||
* B, K, M, G, A for Byte, Kbyte, Mbyte, Gbyte, adaptive byte
|
||||
* b, k, m, g, a for bit, Kbit, Mbit, Gbit, adaptive bit
|
||||
* adaptive picks the "best" one based on the number.
|
||||
* outString should be at least 11 chars long
|
||||
* s should be at least 11 chars long
|
||||
* (4 digits + space + 5 chars max + null)
|
||||
* ------------------------------------------------------------------- */
|
||||
|
||||
void byte_snprintf( char* outString, int inLen,
|
||||
void unit_snprintf( char *s, int inLen,
|
||||
double inNum, char inFormat ) {
|
||||
int conv;
|
||||
const char* suffix;
|
||||
@ -199,23 +203,23 @@ void byte_snprintf( char* outString, int inLen,
|
||||
}
|
||||
|
||||
switch ( toupper(inFormat) ) {
|
||||
case 'B': conv = kConv_Unit; break;
|
||||
case 'K': conv = kConv_Kilo; break;
|
||||
case 'M': conv = kConv_Mega; break;
|
||||
case 'G': conv = kConv_Giga; break;
|
||||
case 'B': conv = UNIT_CONV; break;
|
||||
case 'K': conv = KILO_CONV; break;
|
||||
case 'M': conv = MEGA_CONV; break;
|
||||
case 'G': conv = GIGA_CONV; break;
|
||||
|
||||
default:
|
||||
case 'A': {
|
||||
double tmpNum = inNum;
|
||||
conv = kConv_Unit;
|
||||
conv = UNIT_CONV;
|
||||
|
||||
if ( isupper((int)inFormat) ) {
|
||||
while ( tmpNum >= 1024.0 && conv <= kConv_Giga ) {
|
||||
while ( tmpNum >= 1024.0 && conv <= GIGA_CONV ) {
|
||||
tmpNum /= 1024.0;
|
||||
conv++;
|
||||
}
|
||||
} else {
|
||||
while ( tmpNum >= 1000.0 && conv <= kConv_Giga ) {
|
||||
while ( tmpNum >= 1000.0 && conv <= GIGA_CONV ) {
|
||||
tmpNum /= 1000.0;
|
||||
conv++;
|
||||
}
|
||||
@ -225,11 +229,11 @@ void byte_snprintf( char* outString, int inLen,
|
||||
}
|
||||
|
||||
if ( ! isupper ((int)inFormat) ) {
|
||||
inNum *= kConversionForBits[ conv ];
|
||||
suffix = kLabel_bit[conv];
|
||||
inNum *= conversion_bits[ conv ];
|
||||
suffix = label_bit[conv];
|
||||
} else {
|
||||
inNum *= kConversion [conv];
|
||||
suffix = kLabel_Byte[ conv ];
|
||||
inNum *= conversion_bytes [conv];
|
||||
suffix = label_byte[ conv ];
|
||||
}
|
||||
|
||||
/* print such that we always fit in 4 places */
|
||||
@ -244,39 +248,9 @@ void byte_snprintf( char* outString, int inLen,
|
||||
* this code will not control spaces*/
|
||||
format = "%4.0f %s"; /* #### */
|
||||
}
|
||||
snprintf( outString, inLen, format, inNum, suffix );
|
||||
} /* end byte_snprintf */
|
||||
|
||||
/* -------------------------------------------------------------------
|
||||
* redirect
|
||||
*
|
||||
* redirect the stdout into a specified file
|
||||
* return: none
|
||||
* ------------------------------------------------------------------- */
|
||||
|
||||
void redirect(const char *inOutputFileName) {
|
||||
#ifdef WIN32
|
||||
|
||||
FILE *fp;
|
||||
|
||||
if ( inOutputFileName == NULL ) {
|
||||
fprintf(stderr, "should specify the output file name.\n");
|
||||
return;
|
||||
}
|
||||
|
||||
fp = freopen(inOutputFileName, "a+", stdout);
|
||||
if ( fp == NULL ) {
|
||||
fprintf(stderr, "redirect stdout failed!\n");
|
||||
return;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
snprintf( s, inLen, format, inNum, suffix );
|
||||
} /* end unit_snprintf */
|
||||
|
||||
#ifdef __cplusplus
|
||||
} /* end extern "C" */
|
||||
#endif
|
||||
|
||||
|
3
src/units.h
Обычный файл
3
src/units.h
Обычный файл
@ -0,0 +1,3 @@
|
||||
double unit_atof( const char *s );
|
||||
iperf_size_t unit_atoi( const char *s );
|
||||
void unit_snprintf( char *s, int inLen, double inNum, char inFormat );
|
Загрузка…
x
Ссылка в новой задаче
Block a user