Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/freebsd-ports-gnome
Path: blob/main/misc/buffer/files/patch-buffer.c
16126 views
1
--- buffer.c.orig Sat Jan 19 20:47:17 2002
2
+++ buffer.c Tue Dec 13 11:57:38 2005
3
@@ -114,12 +114,17 @@
4
#include <signal.h>
5
#include <fcntl.h>
6
#include <errno.h>
7
+#include <machine/param.h>
8
#include <sys/types.h>
9
#include <sys/stat.h>
10
#include <sys/ipc.h>
11
#include <sys/shm.h>
12
#include <sys/sem.h>
13
#include <sys/wait.h>
14
+#include <sys/time.h>
15
+#include <limits.h>
16
+#include <stdlib.h>
17
+#include <strings.h>
18
#include "sem.h"
19
20
#ifndef lint
21
@@ -127,7 +132,6 @@
22
#endif
23
24
#ifndef __alpha
25
-extern char *shmat();
26
#endif /* __alpha */
27
28
/* General macros */
29
@@ -252,6 +256,8 @@
30
/* Number of K output */
31
unsigned long outk = 0;
32
33
+struct timeval starttime;
34
+
35
int
36
main( argc, argv )
37
int argc;
38
@@ -263,6 +269,8 @@
39
40
buffer_allocate();
41
42
+ gettimeofday(&starttime, NULL);
43
+
44
start_reader_and_writer();
45
46
byee( 0 );
47
@@ -384,8 +392,8 @@
48
fprintf( stderr, "Usage: %s [-B] [-t] [-S size] [-m memsize] [-b blocks] [-p percent] [-s blocksize] [-u pause] [-i infile] [-o outfile] [-z size]\n",
49
progname );
50
fprintf( stderr, "-B = blocked device - pad out last block\n" );
51
- fprintf( stderr, "-t = show total amount writen at end\n" );
52
- fprintf( stderr, "-S size = show amount writen every size bytes\n" );
53
+ fprintf( stderr, "-t = show total amount written at end\n" );
54
+ fprintf( stderr, "-S size = show amount written every size bytes\n" );
55
fprintf( stderr, "-m size = size of shared mem chunk to grab\n" );
56
fprintf( stderr, "-b num = number of blocks in queue\n" );
57
fprintf( stderr, "-p percent = don't start writing until percent blocks filled\n" );
58
@@ -398,6 +406,11 @@
59
}
60
}
61
62
+ if (argc > optind) {
63
+ fprintf( stderr, "too many arguments\n" );
64
+ byee( -1 );
65
+ }
66
+
67
if (zflag) showevery = blocksize;
68
69
/* If -b was not given try and work out the max buffer size */
70
@@ -507,9 +520,9 @@
71
get_buffer();
72
73
if( debug )
74
- fprintf( stderr, "%s pbuffer is 0x%08x, buffer_size is %d [%d x %d]\n",
75
+ fprintf( stderr, "%s pbuffer is 0x%08lx, buffer_size is %d [%d x %d]\n",
76
proc_string,
77
- (char *)pbuffer, buffer_size, blocks, blocksize );
78
+ (unsigned long)pbuffer, buffer_size, blocks, blocksize );
79
80
#ifdef SYS5
81
memset( (char *)pbuffer, '\0', buffer_size );
82
@@ -526,9 +539,11 @@
83
lock( pbuffer->semid, pbuffer->blocks_used_lock );
84
85
pbuffer->blocks_free_lock = 1;
86
- /* start this off so lock() can be called on it for each block
87
- * till all the blocks are used up */
88
- sem_set( pbuffer->semid, pbuffer->blocks_free_lock, blocks - 1 );
89
+ /* Initializing the semaphore to "blocks - 1" causes a hang when using option
90
+ * "-p 100" because it always keeps one block free, so we'll never reach 100% fill
91
+ * level. However, there doesn't seem to be a good reason to keep one block free,
92
+ * so we initialize the semaphore to "blocks" instead. */
93
+ sem_set( pbuffer->semid, pbuffer->blocks_free_lock, blocks );
94
95
/* Detattach the shared memory so the fork doesnt do anything odd */
96
shmdt( (char *)pbuffer );
97
@@ -648,7 +663,7 @@
98
int
99
fill_block()
100
{
101
- int bytes;
102
+ int bytes = 0;
103
char *start;
104
int toread;
105
static char eof_reached = 0;
106
@@ -707,7 +722,7 @@
107
{
108
int filled = 0;
109
int maxfilled = (blocks * percent) / 100;
110
- int first_block;
111
+ int first_block = 0;
112
113
if( debug )
114
fprintf( stderr, "\tW: Entering writer\n blocks = %d\n maxfilled = %d\n",
115
@@ -914,13 +929,12 @@
116
do_size( arg )
117
char *arg;
118
{
119
- char format[ 20 ];
120
- int ret;
121
+ int ret = 0;
122
123
- *format = '\0';
124
- sscanf( arg, "%d%s", &ret, format );
125
+ char unit = '\0';
126
+ sscanf( arg, "%d%c", &ret, &unit );
127
128
- switch( *format ){
129
+ switch( unit ){
130
case 'm':
131
case 'M':
132
ret = ret K K;
133
@@ -941,7 +955,36 @@
134
void
135
pr_out()
136
{
137
- fprintf( stderr, " %10luK\r", outk );
138
+ struct timeval now;
139
+ unsigned long ms_delta, k_per_s;
140
+
141
+ gettimeofday(&now, NULL);
142
+ ms_delta = (now.tv_sec - starttime.tv_sec) * 1000
143
+ + (now.tv_usec - starttime.tv_usec) / 1000;
144
+ if (ms_delta) {
145
+ /* Use increased accuracy for small amounts of data,
146
+ * decreased accuracy for *huge* throughputs > 4.1GB/s
147
+ * to avoid division by 0. This will overflow if your
148
+ * machine's throughput exceeds 4TB/s - you deserve to
149
+ * loose if you're still using 32 bit longs on such a
150
+ * beast ;-)
151
+ * <[email protected]>
152
+ */
153
+ if (outk < ULONG_MAX / 1000) {
154
+ k_per_s = (outk * 1000) / ms_delta;
155
+ } else if (ms_delta >= 1000) {
156
+ k_per_s = outk / (ms_delta / 1000);
157
+ } else {
158
+ k_per_s = (outk / ms_delta) * 1000;
159
+ }
160
+ fprintf( stderr, " %10luK, %10luK/s\r", outk, k_per_s );
161
+ } else {
162
+ if (outk) {
163
+ fprintf( stderr, " %10luK, ?K/s\r", outk );
164
+ } else {
165
+ fprintf( stderr, " 0K, 0K/s\r");
166
+ }
167
+ }
168
}
169
170
#ifdef SYS5
171
172